fix(nip59) formated code

This commit is contained in:
ciegovolador
2024-10-17 23:33:13 -03:00
committed by fiatjaf_
parent 7aa4f09769
commit bf975c9a87
2 changed files with 16 additions and 24 deletions

View File

@@ -3,39 +3,38 @@ import { wrapEvent, unwrapEvent } from './nip59.ts'
import { decode } from './nip19.ts' import { decode } from './nip19.ts'
import { getPublicKey } from './pure.ts' import { getPublicKey } from './pure.ts'
const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data const senderPrivateKey = decode(`nsec1p0ht6p3wepe47sjrgesyn4m50m6avk2waqudu9rl324cg2c4ufesyp6rdg`).data
const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data const recipientPrivateKey = decode(`nsec1uyyrnx7cgfp40fcskcr2urqnzekc20fj0er6de0q8qvhx34ahazsvs9p36`).data
const recipientPublicKey = getPublicKey(recipientPrivateKey) const recipientPublicKey = getPublicKey(recipientPrivateKey)
const event = { const event = {
kind: 1, kind: 1,
content: "Are you going to the party tonight?", content: 'Are you going to the party tonight?',
} }
const wrapedEvent = wrapEvent(event, senderPrivateKey, recipientPublicKey) const wrapedEvent = wrapEvent(event, senderPrivateKey, recipientPublicKey)
test('wrapEvent', () => { test('wrapEvent', () => {
const expected = { const expected = {
content: '', id: '', created_at: 1728537932, kind: 1059, pubkey: '', sig: '', tags: [ content: '',
[ id: '',
"p", created_at: 1728537932,
"166bf3765ebd1fc55decfe395beff2ea3b2a4e0a8946e7eb578512b555737c99" kind: 1059,
] pubkey: '',
], sig: '',
tags: [['p', '166bf3765ebd1fc55decfe395beff2ea3b2a4e0a8946e7eb578512b555737c99']],
[Symbol('verified')]: true, [Symbol('verified')]: true,
} }
const result = wrapEvent(event, senderPrivateKey, recipientPublicKey) const result = wrapEvent(event, senderPrivateKey, recipientPublicKey)
expect(result.kind).toEqual(expected.kind) expect(result.kind).toEqual(expected.kind)
expect(result.tags).toEqual(expected.tags) expect(result.tags).toEqual(expected.tags)
}) })
test('unwrapEvent', () => { test('unwrapEvent', () => {
const expected = { const expected = {
kind: 1, kind: 1,
content: "Are you going to the party tonight?", content: 'Are you going to the party tonight?',
pubkey: "611df01bfcf85c26ae65453b772d8f1dfd25c264621c0277e1fc1518686faef9", pubkey: '611df01bfcf85c26ae65453b772d8f1dfd25c264621c0277e1fc1518686faef9',
tags: [], tags: [],
} }
const result = unwrapEvent(wrapedEvent, recipientPrivateKey) const result = unwrapEvent(wrapedEvent, recipientPrivateKey)
@@ -44,6 +43,4 @@ test('unwrapEvent', () => {
expect(result.content).toEqual(expected.content) expect(result.content).toEqual(expected.content)
expect(result.pubkey).toEqual(expected.pubkey) expect(result.pubkey).toEqual(expected.pubkey)
expect(result.tags).toEqual(expected.tags) expect(result.tags).toEqual(expected.tags)
}) })

View File

@@ -1,4 +1,3 @@
import { EventTemplate, UnsignedEvent, Event } from './core.ts' import { EventTemplate, UnsignedEvent, Event } from './core.ts'
import { getConversationKey, decrypt, encrypt } from './nip44.ts' import { getConversationKey, decrypt, encrypt } from './nip44.ts'
import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts' import { getEventHash, generateSecretKey, finalizeEvent, getPublicKey } from './pure.ts'
@@ -9,11 +8,9 @@ type Rumor = UnsignedEvent & { id: string }
const TWO_DAYS = 2 * 24 * 60 * 60 const TWO_DAYS = 2 * 24 * 60 * 60
const now = () => Math.round(Date.now() / 1000) const now = () => Math.round(Date.now() / 1000)
const randomNow = () => Math.round(now() - (Math.random() * TWO_DAYS)) const randomNow = () => Math.round(now() - Math.random() * TWO_DAYS)
const nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) => getConversationKey(privateKey, publicKey)
const nip44ConversationKey = (privateKey: Uint8Array, publicKey: string) =>
getConversationKey(privateKey, publicKey)
const nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) => const nip44Encrypt = (data: EventTemplate, privateKey: Uint8Array, publicKey: string) =>
encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey)) encrypt(JSON.stringify(data), nip44ConversationKey(privateKey, publicKey))
@@ -24,7 +21,7 @@ const nip44Decrypt = (data: Event, privateKey: Uint8Array) =>
export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array) { export function createRumor(event: Partial<UnsignedEvent>, privateKey: Uint8Array) {
const rumor = { const rumor = {
created_at: now(), created_at: now(),
content: "", content: '',
tags: [], tags: [],
...event, ...event,
pubkey: getPublicKey(privateKey), pubkey: getPublicKey(privateKey),
@@ -43,7 +40,7 @@ export function createSeal(rumor: Rumor, privateKey: Uint8Array, recipientPublic
created_at: randomNow(), created_at: randomNow(),
tags: [], tags: [],
}, },
privateKey privateKey,
) as Event ) as Event
} }
@@ -55,14 +52,13 @@ export function createWrap(seal: Event, recipientPublicKey: string) {
kind: GiftWrap, kind: GiftWrap,
content: nip44Encrypt(seal, randomKey, recipientPublicKey), content: nip44Encrypt(seal, randomKey, recipientPublicKey),
created_at: randomNow(), created_at: randomNow(),
tags: [["p", recipientPublicKey]], tags: [['p', recipientPublicKey]],
}, },
randomKey randomKey,
) as Event ) as Event
} }
export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8Array, recipientPublicKey: string) { export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8Array, recipientPublicKey: string) {
const rumor = createRumor(event, senderPrivateKey) const rumor = createRumor(event, senderPrivateKey)
const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey) const seal = createSeal(rumor, senderPrivateKey, recipientPublicKey)
@@ -70,7 +66,6 @@ export function wrapEvent(event: Partial<UnsignedEvent>, senderPrivateKey: Uint8
} }
export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array) { export function unwrapEvent(wrap: Event, recipientPrivateKey: Uint8Array) {
const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey) const unwrappedSeal = nip44Decrypt(wrap, recipientPrivateKey)
return nip44Decrypt(unwrappedSeal, recipientPrivateKey) return nip44Decrypt(unwrappedSeal, recipientPrivateKey)
} }