From: Chris Duncan Date: Mon, 2 Dec 2024 19:47:36 +0000 (-0800) Subject: Remove type assertions rendered unnecessary by improvements to blake2b typings. X-Git-Url: https://zoso.dev/?a=commitdiff_plain;h=fab1315c167f5b2596888f5a0092dbe3656d4a4c;p=libnemo.git Remove type assertions rendered unnecessary by improvements to blake2b typings. --- diff --git a/src/lib/account.ts b/src/lib/account.ts index 197553d..8afda65 100644 --- a/src/lib/account.ts +++ b/src/lib/account.ts @@ -186,7 +186,7 @@ export class Account { const expectedChecksum = address.slice(-8) const keyBase32 = address.slice(address.indexOf('_') + 1, -8) const keyBuf = base32.toBytes(keyBase32) - const actualChecksumBuf = new Blake2b(5).update(keyBuf).digest() as Uint8Array + const actualChecksumBuf = new Blake2b(5).update(keyBuf).digest() actualChecksumBuf.reverse() const actualChecksum = bytes.toBase32(actualChecksumBuf) @@ -230,7 +230,7 @@ export class Account { static #addressToKey (v: string): string { const keyBytes = base32.toBytes(v.substring(0, 52)) const checksumBytes = base32.toBytes(v.substring(52, 60)) - const blakeHash = new Blake2b(5).update(keyBytes).digest() as Uint8Array + const blakeHash = new Blake2b(5).update(keyBytes).digest() blakeHash.reverse() if (bytes.toHex(checksumBytes) !== bytes.toHex(blakeHash)) { throw new Error('Checksum mismatch in address') @@ -240,7 +240,7 @@ export class Account { static #keyToAddress (key: string): string { const publicKeyBytes = hex.toBytes(key) - const checksum = new Blake2b(5).update(publicKeyBytes).digest() as Uint8Array + const checksum = new Blake2b(5).update(publicKeyBytes).digest() checksum.reverse() const encoded = bytes.toBase32(publicKeyBytes) const encodedChecksum = bytes.toBase32(checksum) diff --git a/src/lib/tools.ts b/src/lib/tools.ts index 9c168ec..26c7789 100644 --- a/src/lib/tools.ts +++ b/src/lib/tools.ts @@ -68,15 +68,14 @@ export async function convert (amount: bigint | string, inputUnit: string, outpu */ export async function hash (data: string | string[], encoding?: 'hex'): Promise { 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() } /** diff --git a/src/lib/wallet.ts b/src/lib/wallet.ts index 5b97b21..59eaa48 100644 --- a/src/lib/wallet.ts +++ b/src/lib/wallet.ts @@ -580,7 +580,7 @@ export class Blake2bWallet extends Wallet { const inputHex = `${this.seed}${indexHex}`.padStart(72, '0') const inputArray = (inputHex.match(/.{1,2}/g) ?? []).map(h => parseInt(h, 16)) const inputBytes = Uint8Array.from(inputArray) - const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex') as string + const privateKey: string = new Blake2b(32).update(inputBytes).digest('hex') return { privateKey, index } }) console.log(`ckd: ${-now + (now = performance.now())} ms`)