]> git.codecow.com Git - libnemo.git/commitdiff
Rename primary worker method for clarity.
authorChris Duncan <chris@zoso.dev>
Fri, 25 Jul 2025 11:28:02 +0000 (04:28 -0700)
committerChris Duncan <chris@zoso.dev>
Fri, 25 Jul 2025 11:28:02 +0000 (04:28 -0700)
src/lib/account.ts
src/lib/block.ts
src/lib/rolodex.ts
src/lib/tools.ts
src/lib/wallets/bip44-wallet.ts
src/lib/wallets/wallet.ts
src/lib/workers/worker-queue.ts

index 36b54989238e37a9b2a6aa48577a485cdb648c32..c8ef4e0c1b7666864f0a2f82f9447e7ff3a76ab5 100644 (file)
@@ -70,7 +70,7 @@ export class Account {
        * allow garbage collection.\r
        */\r
        async destroy (): Promise<void> {\r
-               await SafeWorker.assign({\r
+               await SafeWorker.request({\r
                        method: 'destroy',\r
                        store: 'Account',\r
                        [this.publicKey]: this.publicKey\r
@@ -244,7 +244,7 @@ export class Account {
        async sign (block: ChangeBlock | ReceiveBlock | SendBlock, password: Key): Promise<string> {\r
                if (typeof password === 'string') password = utf8.toBytes(password)\r
                try {\r
-                       const { signature } = await NanoNaClWorker.assign<ArrayBuffer>({\r
+                       const { signature } = await NanoNaClWorker.request<ArrayBuffer>({\r
                                method: 'detached',\r
                                privateKey: (await this.#export(password)).buffer,\r
                                msg: hex.toBytes(block.hash).buffer\r
@@ -318,7 +318,7 @@ export class Account {
                        throw new Error('Password must be string or bytes')\r
                }\r
                try {\r
-                       const response = await SafeWorker.assign<ArrayBuffer>({\r
+                       const response = await SafeWorker.request<ArrayBuffer>({\r
                                method: 'fetch',\r
                                name: this.publicKey,\r
                                store: 'Account',\r
@@ -361,7 +361,7 @@ export class Account {
                        this.#validateKey(privateKey)\r
                        if (typeof privateKey === 'string') privateKey = hex.toBytes(privateKey)\r
                        try {\r
-                               const result = await NanoNaClWorker.assign<ArrayBuffer>({\r
+                               const result = await NanoNaClWorker.request<ArrayBuffer>({\r
                                        method: 'convert',\r
                                        privateKey: new Uint8Array(privateKey).buffer\r
                                })\r
@@ -376,7 +376,7 @@ export class Account {
                }\r
 \r
                try {\r
-                       const { result } = await SafeWorker.assign(privateAccounts)\r
+                       const { result } = await SafeWorker.request(privateAccounts)\r
                        if (!result) {\r
                                throw null\r
                        }\r
index a56d2a1e7d964c563c5d4da2f049b522c84762ec..71a99be615fe3af1dce8e37066964bf83a5be493 100644 (file)
@@ -188,7 +188,7 @@ abstract class Block {
                        throw new Error('Provide a key for block signature verification.')
                }
                try {
-                       const { isVerified } = await NanoNaClWorker.assign<boolean>({
+                       const { isVerified } = await NanoNaClWorker.request<boolean>({
                                method: 'verify',
                                msg: hex.toBytes(this.hash).buffer,
                                signature: hex.toBytes(this.signature ?? '').buffer,
index a45aa0ddc0175bc62e08dde50ba81c16424a87b9..0668001c0d60eab993d608c003b7e79c412b739f 100644 (file)
@@ -64,7 +64,7 @@ export class Rolodex {
                        const existingAddresses = await this.getAddresses(name)
                        existingAddresses.push(account.address)
                        data[name] = utf8.toBytes(JSON.stringify(existingAddresses)).buffer
-                       const { result } = await SafeWorker.assign<boolean>(data)
+                       const { result } = await SafeWorker.request<boolean>(data)
                        return result
                } catch (err) {
                        throw new Error('failed to add address', { cause: err })
@@ -83,7 +83,7 @@ export class Rolodex {
                        return false
                }
                const addresses = (await this.getAddresses(name)).filter(a => a !== address).sort()
-               const { result: isUpdated } = await SafeWorker.assign<boolean>({
+               const { result: isUpdated } = await SafeWorker.request<boolean>({
                        method: 'store',
                        store: 'Rolodex',
                        password: utf8.toBytes('').buffer,
@@ -92,7 +92,7 @@ export class Rolodex {
                if (!isUpdated) {
                        throw new Error('failed to remove address from existing name')
                }
-               const { result } = await SafeWorker.assign<boolean>({
+               const { result } = await SafeWorker.request<boolean>({
                        method: 'destroy',
                        store: 'Rolodex',
                        [address]: address
@@ -116,7 +116,7 @@ export class Rolodex {
                for (const address of addresses) {
                        data[address] = address
                }
-               const { result } = await SafeWorker.assign<boolean>(data)
+               const { result } = await SafeWorker.request<boolean>(data)
                return result
        }
 
@@ -128,7 +128,7 @@ export class Rolodex {
        */
        static async getAddresses (name: string): Promise<string[]> {
                try {
-                       const response = await SafeWorker.assign<ArrayBuffer>({
+                       const response = await SafeWorker.request<ArrayBuffer>({
                                method: 'fetch',
                                name,
                                store: 'Rolodex',
@@ -151,7 +151,7 @@ export class Rolodex {
        */
        static async getAllNames (): Promise<string[]> {
                try {
-                       const response = await SafeWorker.assign<ArrayBuffer>({
+                       const response = await SafeWorker.request<ArrayBuffer>({
                                method: 'export',
                                name: '',
                                store: 'Rolodex',
@@ -172,7 +172,7 @@ export class Rolodex {
        */
        static async getName (address: string): Promise<string | null> {
                try {
-                       const response = await SafeWorker.assign<ArrayBuffer>({
+                       const response = await SafeWorker.request<ArrayBuffer>({
                                method: 'fetch',
                                name: address,
                                store: 'Rolodex',
index 8fe75973d19807619f620d540e7a39ee45101c08..d8935d75f3d9d59f0f0e9dfbe680f135cc79a437 100644 (file)
@@ -92,7 +92,7 @@ function hash (data: string | string[], encoding?: 'hex', format?: 'hex'): strin
 export async function sign (key: Key, ...input: string[]): Promise<string> {
        if (typeof key === 'string') key = hex.toBytes(key)
        try {
-               const { signature } = await NanoNaClWorker.assign<ArrayBuffer>({
+               const { signature } = await NanoNaClWorker.request<ArrayBuffer>({
                        method: 'detached',
                        privateKey: key.buffer,
                        msg: hash(input).buffer
@@ -177,7 +177,7 @@ export async function sweep (
 export async function verify (key: Key, signature: string, ...input: string[]): Promise<boolean> {
        if (typeof key === 'string') key = hex.toBytes(key)
        try {
-               const { isVerified } = await NanoNaClWorker.assign<boolean>({
+               const { isVerified } = await NanoNaClWorker.request<boolean>({
                        method: 'verify',
                        msg: hash(input).buffer,
                        signature: hex.toBytes(signature).buffer,
index 426b2c5f22b5beab5e453dcf54fef0e5e8b17e7a..02cf6a40d88671a39aa0cf89968fb2c88726117e 100644 (file)
@@ -215,7 +215,7 @@ export class Bip44Wallet extends Wallet {
                if (this.isLocked) {\r
                        throw new Error('wallet must be unlocked to derive accounts')\r
                }\r
-               const results = await Bip44CkdWorker.assign({\r
+               const results = await Bip44CkdWorker.request({\r
                        indexes,\r
                        seed: hex.toBytes(this.seed).buffer\r
                })\r
index fc5d92429d8b0678045643f4777c40ac0874bff3..36532de66fb7dd5f9d6eb9b13077581e2eb11e21 100644 (file)
@@ -39,6 +39,21 @@ export abstract class Wallet {
                return bytes.toHex(this.#s)\r
        }\r
 \r
+       static async export () {\r
+               try {\r
+                       const response = await SafeWorker.request<ArrayBuffer>({\r
+                               method: 'export',\r
+                               name: '',\r
+                               store: 'Rolodex',\r
+                               password: utf8.toBytes('').buffer\r
+                       })\r
+                       return Object.keys(response).filter(v => v.slice(0, 5) !== 'nano_')\r
+               } catch (err) {\r
+                       console.error(err)\r
+                       return []\r
+               }\r
+       }\r
+\r
        constructor (id: Entropy, seed?: Uint8Array<ArrayBuffer>, mnemonic?: Bip39Mnemonic) {\r
                if (this.constructor === Wallet) {\r
                        throw new Error('Wallet is an abstract class and cannot be instantiated directly.')\r
@@ -154,7 +169,7 @@ export abstract class Wallet {
                        bytes.erase(this.#s)\r
                        this.#m = undefined\r
                        this.#s = undefined\r
-                       await SafeWorker.assign<boolean>({\r
+                       await SafeWorker.request<boolean>({\r
                                store: 'Wallet',\r
                                method: 'destroy',\r
                                [this.id]: this.id\r
@@ -186,7 +201,7 @@ export abstract class Wallet {
                                seed: this.#s == null ? this.#s : bytes.toHex(this.#s)\r
                        })\r
                        const encoded = utf8.toBytes(serialized)\r
-                       const success = await SafeWorker.assign({\r
+                       const success = await SafeWorker.request({\r
                                method: 'store',\r
                                store: 'Wallet',\r
                                [this.id]: encoded.buffer,\r
@@ -270,7 +285,7 @@ export abstract class Wallet {
                        if (password == null || !(password instanceof Uint8Array)) {\r
                                throw new Error('password must be string or bytes')\r
                        }\r
-                       const response = await SafeWorker.assign<ArrayBuffer>({\r
+                       const response = await SafeWorker.request<ArrayBuffer>({\r
                                method: 'fetch',\r
                                name: this.id,\r
                                store: 'Wallet',\r
index bcc91869fcc51f177c2f7b1282665514dc36b5a3..717058dbfe9c35f8e8e36a14355965b68f265725 100644 (file)
@@ -52,14 +52,14 @@ export class WorkerQueue {
                WorkerQueue.#instances.push(this)
        }
 
-       async assign<T extends Data> (data: NamedData): Promise<NamedData<T>> {
-               return this.#assign<T>(data, task => this.#queue.push(task))
-       }
-
        async prioritize<T extends Data> (data: NamedData): Promise<NamedData<T>> {
                return this.#assign<T>(data, task => this.#queue.unshift(task))
        }
 
+       async request<T extends Data> (data: NamedData): Promise<NamedData<T>> {
+               return this.#assign<T>(data, task => this.#queue.push(task))
+       }
+
        terminate (): void {
                this.#job = undefined
                this.#worker.terminate()