Skip to content

Commit

Permalink
Merge pull request #174 from lenkan/noble-hashes-lenkan
Browse files Browse the repository at this point in the history
using @nobles/hashes (with rebase)
  • Loading branch information
lenkan authored Dec 9, 2023
2 parents cfb001b + febfcb0 commit 858dea2
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 32 deletions.
8 changes: 1 addition & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
12 changes: 7 additions & 5 deletions src/keri/core/diger.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createHash } from 'blake3';
import { blake3 } from '@noble/hashes/blake3';

import { Matter, MatterArgs, MtrDex } from './matter';

Expand All @@ -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.`);
Expand Down Expand Up @@ -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();
}
}
7 changes: 4 additions & 3 deletions src/keri/core/prefixer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '#';

Expand Down Expand Up @@ -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];
}

Expand Down
5 changes: 2 additions & 3 deletions src/keri/core/saider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '#';

Expand Down Expand Up @@ -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(
Expand Down
10 changes: 1 addition & 9 deletions src/ready.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import _sodium from 'libsodium-wrappers-sumo';

export const ready: () => Promise<void> = async () => {
try {
const b3 = await import('blake3/browser-async');
await b3.default(
'https://cdn.jsdelivr.net/npm/[email protected]/dist/wasm/web/blake3_js_bg.wasm'
);
await _sodium.ready;
} catch (e) {
await _sodium.ready;
}
await _sodium.ready;
};
8 changes: 4 additions & 4 deletions test/core/diger.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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);
Expand Down

0 comments on commit 858dea2

Please sign in to comment.