From 6440680898fe2e684cc2ffee06ba358025ba54c8 Mon Sep 17 00:00:00 2001 From: reya Date: Sat, 9 Dec 2023 09:34:20 +0700 Subject: [PATCH] fix ark --- src/app/auth/create.tsx | 7 ++++++- src/app/auth/follow.tsx | 4 ++-- src/app/auth/import.tsx | 2 +- src/libs/ark/ark.ts | 30 +++++++++++++++++++++++------- src/utils/shortenKey.ts | 2 +- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/app/auth/create.tsx b/src/app/auth/create.tsx index a1ebd64c..100ac88d 100644 --- a/src/app/auth/create.tsx +++ b/src/app/auth/create.tsx @@ -70,7 +70,12 @@ export function CreateAccountScreen() { }); if (publish) { - await ark.createAccount(userNpub, userPubkey, userPrivkey); + await ark.createAccount({ + id: userNpub, + pubkey: userPubkey, + privkey: userPrivkey, + }); + await ark.createEvent({ kind: NDKKind.RelayList, tags: [ark.relays], diff --git a/src/app/auth/follow.tsx b/src/app/auth/follow.tsx index 6805f6c7..79b5c2ba 100644 --- a/src/app/auth/follow.tsx +++ b/src/app/auth/follow.tsx @@ -69,14 +69,14 @@ export function FollowScreen() { const publish = await ark.createEvent({ kind: NDKKind.Contacts, tags: follows.map((item) => { - if (item.startsWith('npub')) return ['p', nip19.decode(item).data as string]; + if (item.startsWith('npub1')) return ['p', nip19.decode(item).data as string]; return ['p', item]; }), }); if (publish) { ark.account.contacts = follows.map((item) => { - if (item.startsWith('npub')) return nip19.decode(item).data as string; + if (item.startsWith('npub1')) return nip19.decode(item).data as string; return item; }); diff --git a/src/app/auth/import.tsx b/src/app/auth/import.tsx index 4ba02515..541d0591 100644 --- a/src/app/auth/import.tsx +++ b/src/app/auth/import.tsx @@ -76,7 +76,7 @@ export function ImportAccountScreen() { setLoading(true); // add account to db - await ark.createAccount(npub, pubkey); + await ark.createAccount({ id: npub, pubkey }); // get account contacts await ark.getUserContacts({ pubkey }); diff --git a/src/libs/ark/ark.ts b/src/libs/ark/ark.ts index 27d316cb..d9089653 100644 --- a/src/libs/ark/ark.ts +++ b/src/libs/ark/ark.ts @@ -81,6 +81,9 @@ export class Ark { async #initNostrSigner({ nsecbunker }: { nsecbunker?: boolean }) { const account = await this.getActiveAccount(); + if (!account) return null; + + // update active account this.account = account; try { @@ -225,7 +228,15 @@ export class Ark { } } - public async createAccount(npub: string, pubkey: string, privkey?: string) { + public async createAccount({ + id, + pubkey, + privkey, + }: { + id: string; + pubkey: string; + privkey?: string; + }) { const existAccounts: Array = await this.#storage.select( 'SELECT * FROM accounts WHERE pubkey = $1 ORDER BY id DESC LIMIT 1;', [pubkey] @@ -239,13 +250,17 @@ export class Ark { } else { await this.#storage.execute( 'INSERT OR IGNORE INTO accounts (id, pubkey, is_active) VALUES ($1, $2, $3);', - [npub, pubkey, 1] + [id, pubkey, 1] ); if (privkey) await this.#keyring_save(pubkey, privkey); } - return await this.getActiveAccount(); + const account = await this.getActiveAccount(); + this.account = account; + this.account.contacts = []; + + return account; } /** @@ -416,10 +431,10 @@ export class Ark { event.kind = kind; event.tags = tags; - if (!publish) { - const publish = await event.publish(); - if (!publish) throw new Error('cannot publish error'); - return publish.size; + if (publish) { + const publishedEvent = await event.publish(); + if (!publishedEvent) throw new Error('Failed to publish event'); + return publishedEvent.size; } return event; @@ -430,6 +445,7 @@ export class Ark { public async getUserProfile({ pubkey }: { pubkey: string }) { try { + console.log(pubkey); const user = this.#ndk.getUser({ pubkey }); const profile = await user.fetchProfile({ cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST, diff --git a/src/utils/shortenKey.ts b/src/utils/shortenKey.ts index cf950fd3..42f7bad3 100644 --- a/src/utils/shortenKey.ts +++ b/src/utils/shortenKey.ts @@ -6,7 +6,7 @@ export function shortenKey(pubkey: string) { } export function displayNpub(pubkey: string, len: number, separator?: string) { - const npub = nip19.npubEncode(pubkey) as string; + const npub = pubkey.startsWith('npub1') ? pubkey : (nip19.npubEncode(pubkey) as string); if (npub.length <= len) return npub; separator = separator || ' ... ';