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) {
await ark.createAccount(userNpub, userPubkey, userPrivkey);
await ark.createAccount({
id: userNpub,
pubkey: userPubkey,
privkey: userPrivkey,
});
await ark.createEvent({
kind: NDKKind.RelayList,
tags: [ark.relays],

View File

@@ -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;
});

View File

@@ -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 });

View File

@@ -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<Account> = 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,

View File

@@ -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 || ' ... ';