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.
*
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<string>} 64-character hexadecimal hash of the input
-*/
-export async function hash (data: string | string[], encoding?: 'hex'): Promise<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()
-}
-
/**
* Signs arbitrary strings with a private key using the Ed25519 signature scheme.
*
*/
export async function sign (key: string, ...input: string[]): Promise<string> {
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}`))
* @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<boolean> {
- 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 }
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 }