From: Chris Duncan Date: Wed, 23 Jul 2025 19:48:09 +0000 (-0700) Subject: Reorder tool functions and explicitly export a const to bundle the tools. X-Git-Tag: v0.10.5~55^2~8 X-Git-Url: https://git.codecow.com/?a=commitdiff_plain;h=023a493290443c4844c733885734306799f5ee5f;p=libnemo.git Reorder tool functions and explicitly export a const to bundle the tools. --- diff --git a/src/lib/rolodex.ts b/src/lib/rolodex.ts index 05047ff..a45aa0d 100644 --- a/src/lib/rolodex.ts +++ b/src/lib/rolodex.ts @@ -3,6 +3,7 @@ import { Account } from './account' import { bytes, utf8 } from './convert' +import { verify } from './tools' import { NamedData } from '#types' import { SafeWorker } from '#workers' @@ -197,7 +198,6 @@ export class Rolodex { * @returns {Promise} True if the signature was used to sign the data, else false */ static async verify (name: string, signature: string, ...data: string[]): Promise { - const { verify } = await import('./tools.js') const addresses = await this.getAddresses(name) for (const address of addresses) { const { publicKey } = Account.import(address) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 4bb753c..8fe7597 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -17,21 +17,6 @@ type SweepResult = { message: string } -function hash (data: string | string[], encoding?: 'hex'): Uint8Array -function hash (data: string | string[], encoding?: 'hex', format?: 'hex'): string | Uint8Array { - if (!Array.isArray(data)) data = [data] - const hash = new Blake2b(32) - if (encoding === 'hex') { - data.forEach(str => hash.update(hex.toBytes(str))) - } else { - const enc = new TextEncoder() - data.forEach(str => hash.update(enc.encode(str))) - } - return format === 'hex' - ? hash.digest('hex').toUpperCase() - : hash.digest() -} - /** * Converts a decimal amount of nano from one unit divider to another. * @@ -82,6 +67,21 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu return `${i}${f ? '.' : ''}${f}` } +function hash (data: string | string[], encoding?: 'hex'): Uint8Array +function hash (data: string | string[], encoding?: 'hex', format?: 'hex'): string | Uint8Array { + if (!Array.isArray(data)) data = [data] + const hash = new Blake2b(32) + if (encoding === 'hex') { + data.forEach(str => hash.update(hex.toBytes(str))) + } else { + const enc = new TextEncoder() + data.forEach(str => hash.update(enc.encode(str))) + } + return format === 'hex' + ? hash.digest('hex').toUpperCase() + : hash.digest() +} + /** * Signs arbitrary strings with a private key using the Ed25519 signature scheme. * @@ -190,3 +190,5 @@ export async function verify (key: Key, signature: string, ...input: string[]): bytes.erase(key) } } + +export const Tools = { convert, hash, sign, sweep, verify } diff --git a/src/main.ts b/src/main.ts index 2e45bc5..fe7b46f 100644 --- a/src/main.ts +++ b/src/main.ts @@ -3,10 +3,10 @@ import { Account } from './lib/account' import { Blake2b } from './lib/blake2b' -import { SendBlock, ReceiveBlock, ChangeBlock } from './lib/block' +import { ChangeBlock, ReceiveBlock, SendBlock } from './lib/block' import { Rolodex } from './lib/rolodex' import { Rpc } from './lib/rpc' +import { Tools } from './lib/tools' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './lib/wallets' -import * as Tools from './lib/tools' -export { Account, Blake2b, SendBlock, ReceiveBlock, ChangeBlock, Rpc, Rolodex, Bip44Wallet, Blake2bWallet, LedgerWallet, Tools } +export { Account, Blake2b, ChangeBlock, ReceiveBlock, SendBlock, Rolodex, Rpc, Tools, Bip44Wallet, Blake2bWallet, LedgerWallet }