dec.toHex(this.balance, 32),
this.link
]
- return Tools.blake2b(data)
+ const hash = await Tools.hash(data, 'hex')
+ return hex.toBytes(hash)
}
/**
dec.toHex(this.balance, 32),
this.link
]
+ const hash = await Tools.hash(data, 'hex')
return Ed25519.verify(
- await Tools.blake2b(data),
+ hex.toBytes(hash),
hex.toBytes(key),
hex.toBytes(this.signature ?? '')
)
// SPDX-FileCopyrightText: 2024 Chris Duncan <chris@zoso.dev>
// SPDX-License-Identifier: GPL-3.0-or-later
-import blake from 'blake2b'
+import blake2b from 'blake2b'
import { Account } from './account.js'
import { UNITS } from './constants.js'
import { bytes, hex } from './convert.js'
import { SendBlock } from './block.js'
/**
-* Hashes data using BLAKE2b.
-*
-* @param {string|string[]} data - Hexadecimal-formatted data to hash
-* @returns {Promise<Uint8Array>} Array of bytes representing hashed input data
-*/
-export async function blake2b (data: string | string[]): Promise<Uint8Array> {
- if (!Array.isArray(data)) data = [data]
- const hash = blake(32)
- data.forEach(str => hash.update(hex.toBytes(str)))
- return hash.digest()
-}
-
-/**
-* Converts a decimal amount from one unit divider to another.
+* Converts a decimal amount of nano from one unit divider to another.
*
* @param {bigint|string} amount - Decimal amount to convert
* @param {string} inputUnit - Current denomination
* Converts one or more strings to hexadecimal and hashes them with BLAKE2b.
*
* @param {string|string[]} data - Input to hash
-* @returns {Promise<string>} Hash of the input
+* @returns {Promise<string>} 64-character hexadecimal hash of the input
*/
-export async function hash (data: string | string[]): Promise<string> {
+export async function hash (data: string | string[], encoding?: 'hex'): Promise<string> {
if (!Array.isArray(data)) data = [data]
- const enc = new TextEncoder()
- data = data.map(str => { return bytes.toHex(enc.encode(str)) })
- const hash = await blake2b(data)
- return bytes.toHex(hash)
+ const hash = 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()
}
/**
* @returns {Promise<string>} Hexadecimal-formatted signature
*/
export async function sign (key: string, ...input: string[]): Promise<string> {
- const enc = new TextEncoder()
- const data = input.map(str => { return bytes.toHex(enc.encode(str)) })
+ const data = await hash(input)
const signature = Ed25519.sign(
- await blake2b(data),
+ hex.toBytes(data),
hex.toBytes(key))
return bytes.toHex(signature)
}
* @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 enc = new TextEncoder()
- const data = input.map(str => { return bytes.toHex(enc.encode(str)) })
+ const data = await hash(input)
return Ed25519.verify(
- await blake2b(data),
+ hex.toBytes(data),
hex.toBytes(key),
hex.toBytes(signature))
}
-export default { blake2b, convert, hash, sign, sweep, verify }
+export default { convert, hash, sign, sweep, verify }