This commit is contained in:
2023-12-09 09:34:20 +07:00
parent e507187044
commit 6440680898
5 changed files with 33 additions and 12 deletions

View File

@@ -70,7 +70,12 @@ export function CreateAccountScreen() {
}); });
if (publish) { if (publish) {
await ark.createAccount(userNpub, userPubkey, userPrivkey); await ark.createAccount({
id: userNpub,
pubkey: userPubkey,
privkey: userPrivkey,
});
await ark.createEvent({ await ark.createEvent({
kind: NDKKind.RelayList, kind: NDKKind.RelayList,
tags: [ark.relays], tags: [ark.relays],

View File

@@ -69,14 +69,14 @@ export function FollowScreen() {
const publish = await ark.createEvent({ const publish = await ark.createEvent({
kind: NDKKind.Contacts, kind: NDKKind.Contacts,
tags: follows.map((item) => { 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]; return ['p', item];
}), }),
}); });
if (publish) { if (publish) {
ark.account.contacts = follows.map((item) => { 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; return item;
}); });

View File

@@ -76,7 +76,7 @@ export function ImportAccountScreen() {
setLoading(true); setLoading(true);
// add account to db // add account to db
await ark.createAccount(npub, pubkey); await ark.createAccount({ id: npub, pubkey });
// get account contacts // get account contacts
await ark.getUserContacts({ pubkey }); await ark.getUserContacts({ pubkey });

View File

@@ -81,6 +81,9 @@ export class Ark {
async #initNostrSigner({ nsecbunker }: { nsecbunker?: boolean }) { async #initNostrSigner({ nsecbunker }: { nsecbunker?: boolean }) {
const account = await this.getActiveAccount(); const account = await this.getActiveAccount();
if (!account) return null;
// update active account
this.account = account; this.account = account;
try { 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<Account> = await this.#storage.select( const existAccounts: Array<Account> = await this.#storage.select(
'SELECT * FROM accounts WHERE pubkey = $1 ORDER BY id DESC LIMIT 1;', 'SELECT * FROM accounts WHERE pubkey = $1 ORDER BY id DESC LIMIT 1;',
[pubkey] [pubkey]
@@ -239,13 +250,17 @@ export class Ark {
} else { } else {
await this.#storage.execute( await this.#storage.execute(
'INSERT OR IGNORE INTO accounts (id, pubkey, is_active) VALUES ($1, $2, $3);', '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); 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.kind = kind;
event.tags = tags; event.tags = tags;
if (!publish) { if (publish) {
const publish = await event.publish(); const publishedEvent = await event.publish();
if (!publish) throw new Error('cannot publish error'); if (!publishedEvent) throw new Error('Failed to publish event');
return publish.size; return publishedEvent.size;
} }
return event; return event;
@@ -430,6 +445,7 @@ export class Ark {
public async getUserProfile({ pubkey }: { pubkey: string }) { public async getUserProfile({ pubkey }: { pubkey: string }) {
try { try {
console.log(pubkey);
const user = this.#ndk.getUser({ pubkey }); const user = this.#ndk.getUser({ pubkey });
const profile = await user.fetchProfile({ const profile = await user.fetchProfile({
cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST, cacheUsage: NDKSubscriptionCacheUsage.CACHE_FIRST,

View File

@@ -6,7 +6,7 @@ export function shortenKey(pubkey: string) {
} }
export function displayNpub(pubkey: string, len: number, separator?: 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; if (npub.length <= len) return npub;
separator = separator || ' ... '; separator = separator || ' ... ';