From: Chris Duncan Date: Mon, 2 Dec 2024 19:59:01 +0000 (-0800) Subject: Tools hash function is only used within Tools itself, so don't bother exporting it... X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=38b1dd0a71519e1965a703bbcabf5f53851098b2;p=libnemo.git Tools hash function is only used within Tools itself, so don't bother exporting it. Arbitrary hashing falls outside our scope as a nano project. --- diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 26c7789..8ea4a74 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -10,6 +10,18 @@ import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './wallet.js' import { SendBlock } from './block.js' import { NanoNaCl } from './nano-nacl.js' +function hash (data: string | string[], encoding?: 'hex'): string { + 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 hash.digest('hex').toUpperCase() +} + /** * Converts a decimal amount of nano from one unit divider to another. * @@ -60,24 +72,6 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu return `${i}${f ? '.' : ''}${f}` } -/** -* Converts one or more strings to hexadecimal and hashes them with BLAKE2b. -* -* @param {string|string[]} data - Input to hash -* @returns {Promise} 64-character hexadecimal hash of the input -*/ -export async function hash (data: string | string[], encoding?: 'hex'): Promise { - 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 hash.digest('hex').toUpperCase() -} - /** * Signs arbitrary strings with a private key using the Ed25519 signature scheme. * @@ -87,7 +81,7 @@ export async function hash (data: string | string[], encoding?: 'hex'): Promise< */ export async function sign (key: string, ...input: string[]): Promise { const account = await Account.fromPrivateKey(key) - const data = await hash(input) + const data = hash(input) const signature = NanoNaCl.sign( hex.toBytes(data), hex.toBytes(`${account.privateKey}${account.publicKey}`)) @@ -158,11 +152,11 @@ export async function sweep (rpc: Rpc | string | URL, wallet: Blake2bWallet | Bi * @returns {boolean} True if the data was signed by the public key's matching private key */ export async function verify (key: string, signature: string, ...input: string[]): Promise { - const data = await hash(input) + const data = hash(input) return NanoNaCl.verify( hex.toBytes(data), hex.toBytes(signature), hex.toBytes(key)) } -export default { convert, hash, sign, sweep, verify } +export const Tools = { convert, sign, sweep, verify } diff --git a/src/main.ts b/src/main.ts index 98f034e..0b4fc2b 100644 --- a/src/main.ts +++ b/src/main.ts @@ -7,7 +7,7 @@ import { Pow } from './lib/pow.js' import { Rpc } from './lib/rpc.js' import { Rolodex } from './lib/rolodex.js' import { Safe } from './lib/safe.js' -import Tools from './lib/tools.js' +import { Tools } from './lib/tools.js' import { Bip44Wallet, Blake2bWallet, LedgerWallet } from './lib/wallet.js' export { Account, SendBlock, ReceiveBlock, ChangeBlock, Pow, Rpc, Rolodex, Safe, Tools, Bip44Wallet, Blake2bWallet, LedgerWallet }