]> git.codecow.com Git - libnemo.git/commitdiff
Fix rolodex contact management.
authorChris Duncan <chris@zoso.dev>
Sat, 2 Aug 2025 21:21:50 +0000 (14:21 -0700)
committerChris Duncan <chris@zoso.dev>
Sat, 2 Aug 2025 21:21:50 +0000 (14:21 -0700)
src/lib/rolodex.ts

index 06a434d7d0b80ef39daae9a5448ebcacf843707b..5f24a098d8e0809bd2a0a884fd4e2ebf66686879 100644 (file)
@@ -53,15 +53,26 @@ export class Rolodex {
                                return true
                        }
                        const data: NamedData = {
-                               [address]: utf8.toBuffer(name)
+                               [address]: {
+                                       id: address,
+                                       name
+                               }
                        }
                        if (existingName != null) {
-                               const existingAddresses = await this.getAddresses(existingName)
-                               data[existingName] = existingAddresses.filter(a => a !== address).sort()
+                               const addresses = (await this.getAddresses(existingName))
+                                       .filter(a => a !== address)
+                                       .sort()
+                               data[existingName] = {
+                                       id: existingName,
+                                       addresses
+                               }
+                       }
+                       const addresses = await this.getAddresses(name)
+                       addresses.push(account.address)
+                       data[name] = {
+                               id: name,
+                               addresses
                        }
-                       const existingAddresses = await this.getAddresses(name)
-                       existingAddresses.push(account.address)
-                       data[name] = existingAddresses
                        const results = await Database.put(data, this.#DB_NAME)
                        if (results.length !== Object.keys(data).length) {
                                throw new Error('Unexpected results from adding address', { cause: results })
@@ -85,7 +96,10 @@ export class Rolodex {
                }
                const addresses = (await this.getAddresses(name)).filter(a => a !== address).sort()
                const data = {
-                       [name]: addresses
+                       [name]: {
+                               id: name,
+                               addresses
+                       }
                }
                const isUpdated = await Database.put(data, this.#DB_NAME)
                if (!isUpdated) {
@@ -102,9 +116,9 @@ export class Rolodex {
        * @returns {Promise<boolean>} Promise for true if name and related addresses successfully removed, else false
        */
        static async deleteName (name: string): Promise<boolean> {
-               const data = await this.getAddresses(name)
-               data.push(name)
-               return await Database.delete(data, this.#DB_NAME)
+               const records = await this.getAddresses(name)
+               records.push(name)
+               return await Database.delete(records, this.#DB_NAME)
        }
 
        /**
@@ -115,10 +129,10 @@ export class Rolodex {
        */
        static async getAddresses (name: string): Promise<string[]> {
                try {
-                       const response = await Database.get<string[]>(name, this.#DB_NAME)
-                       const addresses = response[name]
-                       return addresses
-                               ? addresses.sort()
+                       const records = await Database.get<NamedData<string[]>>(name, this.#DB_NAME)
+                       const record = records[name]
+                       return record?.addresses
+                               ? record.addresses.sort()
                                : []
                } catch (err) {
                        console.error(err)
@@ -133,8 +147,8 @@ export class Rolodex {
        */
        static async getAllNames (): Promise<string[]> {
                try {
-                       const response = await Database.getAll(this.#DB_NAME)
-                       return Object.keys(response).filter(v => v.slice(0, 5) !== 'nano_')
+                       const records = await Database.getAll<NamedData>(this.#DB_NAME)
+                       return Object.keys(records).filter(v => v.slice(0, 5) !== 'nano_')
                } catch (err) {
                        console.error(err)
                        return []
@@ -149,9 +163,9 @@ export class Rolodex {
        */
        static async getName (address: string): Promise<string | null> {
                try {
-                       const response = await Database.get<string>(address, this.#DB_NAME)
-                       const name = response[address]
-                       return name
+                       const records = await Database.get<NamedData<string>>(address, this.#DB_NAME)
+                       const record = records[address]
+                       return record?.name ?? null
                } catch (err) {
                        console.error(err)
                        return null