From febfcb0dc212ad7842bd033292cce4eea7eeafe7 Mon Sep 17 00:00:00 2001 From: AlexAndrei98 Date: Sun, 3 Dec 2023 17:45:05 -0500 Subject: [PATCH] using @nobles/hashes --- package-lock.json | 8 +------- package.json | 2 +- src/keri/core/diger.ts | 12 +++++++----- src/keri/core/prefixer.ts | 7 ++++--- src/keri/core/saider.ts | 5 ++--- src/ready.ts | 10 +--------- test/core/diger.test.ts | 8 ++++---- 7 files changed, 20 insertions(+), 32 deletions(-) diff --git a/package-lock.json b/package-lock.json index b031795d..5e548444 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,7 @@ "examples/*" ], "dependencies": { - "blake3": "^2.1.7", + "@noble/hashes": "^1.3.2", "buffer": "^6.0.3", "ecdsa-secp256r1": "^1.3.3", "libsodium-wrappers-sumo": "^0.7.9", @@ -3367,12 +3367,6 @@ "ieee754": "^1.1.13" } }, - "node_modules/blake3": { - "version": "2.1.7", - "resolved": "https://registry.npmjs.org/blake3/-/blake3-2.1.7.tgz", - "integrity": "sha512-5d+TdKJvju96IyEaGJ0eO6CHbckWi+NBrCezGYM/WsnI3R03aLL2TWfsuZSh1rs0fTv/L3ps/r0vykjYurcIwA==", - "hasInstallScript": true - }, "node_modules/bluebird": { "version": "3.7.2", "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.7.2.tgz", diff --git a/package.json b/package.json index 2572e996..686da766 100644 --- a/package.json +++ b/package.json @@ -57,7 +57,7 @@ "whatwg-fetch": "^3.6.19" }, "dependencies": { - "blake3": "^2.1.7", + "@noble/hashes": "^1.3.2", "buffer": "^6.0.3", "ecdsa-secp256r1": "^1.3.3", "libsodium-wrappers-sumo": "^0.7.9", diff --git a/src/keri/core/diger.ts b/src/keri/core/diger.ts index 6ee17063..8bcbfb22 100644 --- a/src/keri/core/diger.ts +++ b/src/keri/core/diger.ts @@ -1,4 +1,4 @@ -import { createHash } from 'blake3'; +import { blake3 } from '@noble/hashes/blake3'; import { Matter, MatterArgs, MtrDex } from './matter'; @@ -25,8 +25,9 @@ export class Diger extends Matter { } if (code === MtrDex.Blake3_256) { - const hasher = createHash(); - const dig = hasher.update(ser).digest(''); + const dig = Buffer.from( + blake3.create({ dkLen: 32 }).update(ser).digest() + ); super({ raw: dig, code: code }); } else { throw new Error(`Unsupported code = ${code} for digester.`); @@ -74,8 +75,9 @@ export class Diger extends Matter { } blake3_256(ser: Uint8Array, dig: any) { - const hasher = createHash(); - const digest = hasher.update(ser).digest(''); + const digest = Buffer.from( + blake3.create({ dkLen: 32 }).update(ser).digest() + ); return digest.toString() === dig.toString(); } } diff --git a/src/keri/core/prefixer.ts b/src/keri/core/prefixer.ts index 53b7fe55..09b99ccb 100644 --- a/src/keri/core/prefixer.ts +++ b/src/keri/core/prefixer.ts @@ -4,7 +4,7 @@ import { Dict, Ilks } from './core'; import { sizeify } from './serder'; import { Verfer } from './verfer'; -import { createHash } from 'blake3'; +import { blake3 } from '@noble/hashes/blake3'; const Dummy: string = '#'; @@ -148,8 +148,9 @@ export class Prefixer extends Matter { kd['i'] = ''.padStart(Matter.Sizes.get(MtrDex.Blake3_256)!.fs!, Dummy); kd['d'] = ked['i']; const [raw] = sizeify(ked); - const hasher = createHash(); - const dig = hasher.update(raw).digest(''); + const dig = Buffer.from( + blake3.create({ dkLen: 32 }).update(raw).digest() + ); return [dig, MtrDex.Blake3_256]; } diff --git a/src/keri/core/saider.ts b/src/keri/core/saider.ts index af9ee7fc..d2e55d90 100644 --- a/src/keri/core/saider.ts +++ b/src/keri/core/saider.ts @@ -3,7 +3,7 @@ import { deversify, Dict, Serials } from './core'; import { EmptyMaterialError } from './kering'; import { dumps, sizeify } from './serder'; -import { createHash } from 'blake3'; +import { blake3 } from '@noble/hashes/blake3'; const Dummy = '#'; @@ -74,8 +74,7 @@ export class Saider extends Matter { _digest_size: number, _length: number ) { - const hasher = createHash(); - return hasher.update(ser).digest(''); + return Buffer.from(blake3.create({ dkLen: 32 }).update(ser).digest()); } private static _derive( diff --git a/src/ready.ts b/src/ready.ts index 7558c50d..23585f26 100644 --- a/src/ready.ts +++ b/src/ready.ts @@ -1,13 +1,5 @@ import _sodium from 'libsodium-wrappers-sumo'; export const ready: () => Promise = async () => { - try { - const b3 = await import('blake3/browser-async'); - await b3.default( - 'https://cdn.jsdelivr.net/npm/blake3@2.1.7/dist/wasm/web/blake3_js_bg.wasm' - ); - await _sodium.ready; - } catch (e) { - await _sodium.ready; - } + await _sodium.ready; }; diff --git a/test/core/diger.test.ts b/test/core/diger.test.ts index 7e083c12..1d0dfc41 100644 --- a/test/core/diger.test.ts +++ b/test/core/diger.test.ts @@ -1,7 +1,6 @@ import { Matter } from '../../src/keri/core/matter'; - -import { createHash } from 'blake3'; import { strict as assert } from 'assert'; +import { blake3 } from '@noble/hashes/blake3'; import { Diger } from '../../src/keri/core/diger'; import { MtrDex } from '../../src/keri/core/matter'; @@ -14,8 +13,9 @@ describe('Diger', () => { 'abcdefghijklmnopqrstuvwxyz0123456789', 'binary' ); - const hasher = createHash(); - const digest = hasher.update(ser).digest(''); + const digest = Buffer.from( + blake3.create({ dkLen: 32 }).update(ser).digest() + ); let diger = new Diger({ raw: digest }); assert.deepStrictEqual(diger.code, MtrDex.Blake3_256);