const expectedChecksum = address.slice(-8)\r
const keyBase32 = address.slice(address.indexOf('_') + 1, -8)\r
const keyBuf = base32.toBytes(keyBase32)\r
- const actualChecksumBuf = new Blake2b(5).update(keyBuf).digest() as Uint8Array\r
+ const actualChecksumBuf = new Blake2b(5).update(keyBuf).digest()\r
actualChecksumBuf.reverse()\r
const actualChecksum = bytes.toBase32(actualChecksumBuf)\r
\r
static #addressToKey (v: string): string {\r
const keyBytes = base32.toBytes(v.substring(0, 52))\r
const checksumBytes = base32.toBytes(v.substring(52, 60))\r
- const blakeHash = new Blake2b(5).update(keyBytes).digest() as Uint8Array\r
+ const blakeHash = new Blake2b(5).update(keyBytes).digest()\r
blakeHash.reverse()\r
if (bytes.toHex(checksumBytes) !== bytes.toHex(blakeHash)) {\r
throw new Error('Checksum mismatch in address')\r
\r
static #keyToAddress (key: string): string {\r
const publicKeyBytes = hex.toBytes(key)\r
- const checksum = new Blake2b(5).update(publicKeyBytes).digest() as Uint8Array\r
+ const checksum = new Blake2b(5).update(publicKeyBytes).digest()\r
checksum.reverse()\r
const encoded = bytes.toBase32(publicKeyBytes)\r
const encodedChecksum = bytes.toBase32(checksum)\r
*/
export async function hash (data: string | string[], encoding?: 'hex'): Promise<string> {
if (!Array.isArray(data)) data = [data]
- const stream = new Blake2b(32)
+ const hash = new Blake2b(32)
if (encoding === 'hex') {
- data.forEach(str => stream.update(hex.toBytes(str)))
+ data.forEach(str => hash.update(hex.toBytes(str)))
} else {
const enc = new TextEncoder()
- data.forEach(str => stream.update(enc.encode(str)))
+ data.forEach(str => hash.update(enc.encode(str)))
}
- const hash = stream.digest('hex') as string
- return hash.toUpperCase()
+ return hash.digest('hex').toUpperCase()
}
/**
const inputHex = `${this.seed}${indexHex}`.padStart(72, '0')\r
const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16))\r
const inputBytes = Uint8Array.from(inputArray)\r
- const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex') as string\r
+ const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex')\r
return { privateKey, index }\r
})\r
console.log(`ckd: ${-now + (now = performance.now())} ms`)\r