From 4a6253f16bf1c7bb1a0bbd68d331ab712165306f Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 6 Dec 2021 11:15:36 +0100 Subject: [PATCH 1/4] Replace ripemd160 dependencies with @noble/hashes --- .pnp.cjs | 30 +++++++++---------- ...hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip | 3 ++ ...emd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip | 3 -- packages/crypto/package.json | 3 +- packages/crypto/src/ripemd.ts | 8 ++--- yarn.lock | 19 +++++------- 6 files changed, 30 insertions(+), 36 deletions(-) create mode 100644 .yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip delete mode 100644 .yarn/cache/@types-ripemd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip diff --git a/.pnp.cjs b/.pnp.cjs index a6ff4091..02242451 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -342,6 +342,10 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "@ledgerhq/logs", "npm:5.50.0" ], + [ + "@noble/hashes", + "npm:1.0.0" + ], [ "@nodelib/fs.scandir", "npm:2.1.4" @@ -582,10 +586,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "@types/range-parser", "npm:1.2.3" ], - [ - "@types/ripemd160", - "npm:2.0.0" - ], [ "@types/semver", "npm:7.3.6" @@ -3372,6 +3372,7 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@cosmjs/math", "workspace:packages/math"], ["@cosmjs/utils", "workspace:packages/utils"], ["@istanbuljs/nyc-config-typescript", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:1.0.1"], + ["@noble/hashes", "npm:1.0.0"], ["@types/bn.js", "npm:5.1.0"], ["@types/elliptic", "npm:6.4.12"], ["@types/eslint-plugin-prettier", "npm:3.1.0"], @@ -3381,7 +3382,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@types/karma-jasmine-html-reporter", "npm:1.5.1"], ["@types/libsodium-wrappers", "npm:0.7.9"], ["@types/node", "npm:15.3.1"], - ["@types/ripemd160", "npm:2.0.0"], ["@types/sha.js", "npm:2.4.0"], ["@typescript-eslint/eslint-plugin", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:4.28.4"], ["@typescript-eslint/parser", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:4.28.4"], @@ -3409,7 +3409,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["libsodium-wrappers", "npm:0.7.9"], ["nyc", "npm:15.1.0"], ["prettier", "npm:2.4.1"], - ["ripemd160", "npm:2.0.2"], ["ses", "npm:0.11.1"], ["sha.js", "npm:2.4.11"], ["source-map-support", "npm:0.5.19"], @@ -4224,6 +4223,15 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD", }] ]], + ["@noble/hashes", [ + ["npm:1.0.0", { + "packageLocation": "./.yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip/node_modules/@noble/hashes/", + "packageDependencies": [ + ["@noble/hashes", "npm:1.0.0"] + ], + "linkType": "HARD", + }] + ]], ["@nodelib/fs.scandir", [ ["npm:2.1.4", { "packageLocation": "./.yarn/cache/@nodelib-fs.scandir-npm-2.1.4-6f6ddb2372-18c2150ab5.zip/node_modules/@nodelib/fs.scandir/", @@ -4847,16 +4855,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD", }] ]], - ["@types/ripemd160", [ - ["npm:2.0.0", { - "packageLocation": "./.yarn/cache/@types-ripemd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip/node_modules/@types/ripemd160/", - "packageDependencies": [ - ["@types/ripemd160", "npm:2.0.0"], - ["@types/node", "npm:15.3.1"] - ], - "linkType": "HARD", - }] - ]], ["@types/semver", [ ["npm:7.3.6", { "packageLocation": "./.yarn/cache/@types-semver-npm-7.3.6-4add0df8f9-933b115a59.zip/node_modules/@types/semver/", diff --git a/.yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip b/.yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip new file mode 100644 index 00000000..f19012b4 --- /dev/null +++ b/.yarn/cache/@noble-hashes-npm-1.0.0-893cbd74b3-bdf1c28a4b.zip @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:94104e20e08120f2dbf76552bc7b3d05bca55416c304990baeb335e3c9dfea81 +size 97291 diff --git a/.yarn/cache/@types-ripemd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip b/.yarn/cache/@types-ripemd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip deleted file mode 100644 index 8404bdd6..00000000 --- a/.yarn/cache/@types-ripemd160-npm-2.0.0-0e635eba3e-eb0eb2d380.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:48fa2838edad2eae418762a057ea6c816e1f82a9976c1c93447d2fbe827a2dc3 -size 2531 diff --git a/packages/crypto/package.json b/packages/crypto/package.json index 72aee309..81aa7ddd 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -44,12 +44,12 @@ "@cosmjs/encoding": "workspace:packages/encoding", "@cosmjs/math": "workspace:packages/math", "@cosmjs/utils": "workspace:packages/utils", + "@noble/hashes": "^1", "bip39": "^3.0.2", "bn.js": "^5.2.0", "elliptic": "^6.5.3", "js-sha3": "^0.8.0", "libsodium-wrappers": "^0.7.6", - "ripemd160": "^2.0.2", "sha.js": "^2.4.11" }, "devDependencies": { @@ -63,7 +63,6 @@ "@types/karma-jasmine-html-reporter": "^1", "@types/libsodium-wrappers": "^0.7.7", "@types/node": "^15.0.1", - "@types/ripemd160": "^2.0.0", "@types/sha.js": "^2.4.0", "@typescript-eslint/eslint-plugin": "^4.28", "@typescript-eslint/parser": "^4.28", diff --git a/packages/crypto/src/ripemd.ts b/packages/crypto/src/ripemd.ts index 258a43e0..263e573c 100644 --- a/packages/crypto/src/ripemd.ts +++ b/packages/crypto/src/ripemd.ts @@ -1,11 +1,11 @@ -import RIPEMD160 from "ripemd160"; +import { ripemd160 as nobleRipemd160 } from "@noble/hashes/ripemd160"; import { HashFunction } from "./hash"; export class Ripemd160 implements HashFunction { public readonly blockSize = 512 / 8; - private readonly impl = new RIPEMD160(); + private readonly impl = nobleRipemd160.create(); public constructor(firstData?: Uint8Array) { if (firstData) { @@ -14,12 +14,12 @@ export class Ripemd160 implements HashFunction { } public update(data: Uint8Array): Ripemd160 { - this.impl.update(Buffer.from(data)); + this.impl.update(data); return this; } public digest(): Uint8Array { - return Uint8Array.from(this.impl.digest()); + return this.impl.digest(); } } diff --git a/yarn.lock b/yarn.lock index 38ffcfda..6e9dbf0e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -455,6 +455,7 @@ __metadata: "@cosmjs/math": "workspace:packages/math" "@cosmjs/utils": "workspace:packages/utils" "@istanbuljs/nyc-config-typescript": ^1.0.1 + "@noble/hashes": ^1 "@types/bn.js": ^5 "@types/elliptic": ^6.4.12 "@types/eslint-plugin-prettier": ^3 @@ -464,7 +465,6 @@ __metadata: "@types/karma-jasmine-html-reporter": ^1 "@types/libsodium-wrappers": ^0.7.7 "@types/node": ^15.0.1 - "@types/ripemd160": ^2.0.0 "@types/sha.js": ^2.4.0 "@typescript-eslint/eslint-plugin": ^4.28 "@typescript-eslint/parser": ^4.28 @@ -492,7 +492,6 @@ __metadata: libsodium-wrappers: ^0.7.6 nyc: ^15.1.0 prettier: ^2.4.1 - ripemd160: ^2.0.2 ses: ^0.11.0 sha.js: ^2.4.11 source-map-support: ^0.5.19 @@ -1248,6 +1247,13 @@ __metadata: languageName: node linkType: hard +"@noble/hashes@npm:^1": + version: 1.0.0 + resolution: "@noble/hashes@npm:1.0.0" + checksum: bdf1c28a4b587e72ec6b0c504903239c6f96680b2c15a6d90d367512f468eeca12f2ee7bd25967a9529be2bedbf3f8d0a50c33368937f8dfef2a973d0661c7b5 + languageName: node + linkType: hard + "@nodelib/fs.scandir@npm:2.1.4": version: 2.1.4 resolution: "@nodelib/fs.scandir@npm:2.1.4" @@ -1779,15 +1785,6 @@ __metadata: languageName: node linkType: hard -"@types/ripemd160@npm:^2.0.0": - version: 2.0.0 - resolution: "@types/ripemd160@npm:2.0.0" - dependencies: - "@types/node": "*" - checksum: eb0eb2d380734a957a1e584f22af874552fe2ce1b28bc079ea5e361dec4bfcee5285d34dc6acdbce31616ac93210343a6c92809a45e18e3b3fd7f462c65b5166 - languageName: node - linkType: hard - "@types/semver@npm:^7.3.4": version: 7.3.6 resolution: "@types/semver@npm:7.3.6" From 218c341f6761deb85a5d737fa60885c4c7aa4e7c Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 22 Jan 2022 13:32:07 +0100 Subject: [PATCH 2/4] Use sha3 from noble --- .pnp.cjs | 14 -------------- .../js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip | 3 --- packages/crypto/package.json | 1 - packages/crypto/src/keccak.ts | 8 +++----- yarn.lock | 8 -------- 5 files changed, 3 insertions(+), 31 deletions(-) delete mode 100644 .yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip diff --git a/.pnp.cjs b/.pnp.cjs index 02242451..17fa7897 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -1770,10 +1770,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "jest-worker", "npm:26.6.2" ], - [ - "js-sha3", - "npm:0.8.0" - ], [ "js-sha512", "npm:0.8.0" @@ -3400,7 +3396,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["jasmine", "npm:3.8.0"], ["jasmine-core", "npm:3.8.0"], ["jasmine-spec-reporter", "npm:6.0.0"], - ["js-sha3", "npm:0.8.0"], ["karma", "npm:6.3.2"], ["karma-chrome-launcher", "npm:3.1.0"], ["karma-firefox-launcher", "npm:2.1.0"], @@ -9241,15 +9236,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD", }] ]], - ["js-sha3", [ - ["npm:0.8.0", { - "packageLocation": "./.yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip/node_modules/js-sha3/", - "packageDependencies": [ - ["js-sha3", "npm:0.8.0"] - ], - "linkType": "HARD", - }] - ]], ["js-sha512", [ ["npm:0.8.0", { "packageLocation": "./.yarn/cache/js-sha512-npm-0.8.0-48a1a122ac-32ca371ebd.zip/node_modules/js-sha512/", diff --git a/.yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip b/.yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip deleted file mode 100644 index 3363d1e0..00000000 --- a/.yarn/cache/js-sha3-npm-0.8.0-decf3ddcfa-75df77c1fc.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:b4a9d083a3756ebd077fe289ecaa5556400d3bf42742acb7f6b9691918682325 -size 16366 diff --git a/packages/crypto/package.json b/packages/crypto/package.json index 81aa7ddd..6547faf8 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -48,7 +48,6 @@ "bip39": "^3.0.2", "bn.js": "^5.2.0", "elliptic": "^6.5.3", - "js-sha3": "^0.8.0", "libsodium-wrappers": "^0.7.6", "sha.js": "^2.4.11" }, diff --git a/packages/crypto/src/keccak.ts b/packages/crypto/src/keccak.ts index ea4e7fc3..66f647a7 100644 --- a/packages/crypto/src/keccak.ts +++ b/packages/crypto/src/keccak.ts @@ -1,15 +1,13 @@ -import jssha3 from "js-sha3"; +import { keccak_256 } from "@noble/hashes/sha3"; import { HashFunction } from "./hash"; export class Keccak256 implements HashFunction { public readonly blockSize = 512 / 8; - private readonly impl: jssha3.Hasher; + private readonly impl = keccak_256.create(); public constructor(firstData?: Uint8Array) { - this.impl = jssha3.keccak256.create(); - if (firstData) { this.update(firstData); } @@ -21,7 +19,7 @@ export class Keccak256 implements HashFunction { } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } diff --git a/yarn.lock b/yarn.lock index 6e9dbf0e..53006282 100644 --- a/yarn.lock +++ b/yarn.lock @@ -483,7 +483,6 @@ __metadata: jasmine: ^3.8 jasmine-core: ^3.7.1 jasmine-spec-reporter: ^6 - js-sha3: ^0.8.0 karma: ^6.1.1 karma-chrome-launcher: ^3.1.0 karma-firefox-launcher: ^2.1.0 @@ -4848,13 +4847,6 @@ fsevents@~2.3.1: languageName: node linkType: hard -"js-sha3@npm:^0.8.0": - version: 0.8.0 - resolution: "js-sha3@npm:0.8.0" - checksum: 75df77c1fc266973f06cce8309ce010e9e9f07ec35ab12022ed29b7f0d9c8757f5a73e1b35aa24840dced0dea7059085aa143d817aea9e188e2a80d569d9adce - languageName: node - linkType: hard - "js-sha512@npm:^0.8.0": version: 0.8.0 resolution: "js-sha512@npm:0.8.0" From 3c7d886eeed49761b1d46c3853db6433ec6888ef Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 26 Jan 2022 21:29:49 +0100 Subject: [PATCH 3/4] Use Sha2 from @noble/hashes --- .pnp.cjs | 16 ---------------- ...es-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip | 3 --- packages/crypto/package.json | 4 +--- packages/crypto/src/sha.ts | 16 ++++++---------- yarn.lock | 11 ----------- 5 files changed, 7 insertions(+), 43 deletions(-) delete mode 100644 .yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip diff --git a/.pnp.cjs b/.pnp.cjs index 17fa7897..d462e57f 100755 --- a/.pnp.cjs +++ b/.pnp.cjs @@ -594,10 +594,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "@types/serve-static", "npm:1.13.9" ], - [ - "@types/sha.js", - "npm:2.4.0" - ], [ "@types/ws", "npm:6.0.4" @@ -3378,7 +3374,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["@types/karma-jasmine-html-reporter", "npm:1.5.1"], ["@types/libsodium-wrappers", "npm:0.7.9"], ["@types/node", "npm:15.3.1"], - ["@types/sha.js", "npm:2.4.0"], ["@typescript-eslint/eslint-plugin", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:4.28.4"], ["@typescript-eslint/parser", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:4.28.4"], ["bip39", "npm:3.0.4"], @@ -3405,7 +3400,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { ["nyc", "npm:15.1.0"], ["prettier", "npm:2.4.1"], ["ses", "npm:0.11.1"], - ["sha.js", "npm:2.4.11"], ["source-map-support", "npm:0.5.19"], ["stream-browserify", "npm:3.0.0"], ["ts-node", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:8.10.2"], @@ -4870,16 +4864,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "linkType": "HARD", }] ]], - ["@types/sha.js", [ - ["npm:2.4.0", { - "packageLocation": "./.yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip/node_modules/@types/sha.js/", - "packageDependencies": [ - ["@types/sha.js", "npm:2.4.0"], - ["@types/node", "npm:15.3.1"] - ], - "linkType": "HARD", - }] - ]], ["@types/ws", [ ["npm:6.0.4", { "packageLocation": "./.yarn/cache/@types-ws-npm-6.0.4-4b7cc6a57b-b2656a76bf.zip/node_modules/@types/ws/", diff --git a/.yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip b/.yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip deleted file mode 100644 index 5b0a9b89..00000000 --- a/.yarn/cache/@types-sha.js-npm-2.4.0-844623a8de-0e1bd1a98d.zip +++ /dev/null @@ -1,3 +0,0 @@ -version https://git-lfs.github.com/spec/v1 -oid sha256:eab162464749baa1a5eddd5d3f11a297572577d1dc4366e382c459efc7bee1b4 -size 2570 diff --git a/packages/crypto/package.json b/packages/crypto/package.json index 6547faf8..6330c3b0 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -48,8 +48,7 @@ "bip39": "^3.0.2", "bn.js": "^5.2.0", "elliptic": "^6.5.3", - "libsodium-wrappers": "^0.7.6", - "sha.js": "^2.4.11" + "libsodium-wrappers": "^0.7.6" }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.1", @@ -62,7 +61,6 @@ "@types/karma-jasmine-html-reporter": "^1", "@types/libsodium-wrappers": "^0.7.7", "@types/node": "^15.0.1", - "@types/sha.js": "^2.4.0", "@typescript-eslint/eslint-plugin": "^4.28", "@typescript-eslint/parser": "^4.28", "buffer": "^6.0.3", diff --git a/packages/crypto/src/sha.ts b/packages/crypto/src/sha.ts index 47a3df01..25656aed 100644 --- a/packages/crypto/src/sha.ts +++ b/packages/crypto/src/sha.ts @@ -1,16 +1,14 @@ -import { Hash } from "crypto"; -import shajs from "sha.js"; +import { sha256 as nobleSha256 } from "@noble/hashes/sha256"; +import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; import { HashFunction } from "./hash"; export class Sha256 implements HashFunction { public readonly blockSize = 512 / 8; - private readonly impl: Hash; + private readonly impl = nobleSha256.create(); public constructor(firstData?: Uint8Array) { - this.impl = shajs("sha256"); - if (firstData) { this.update(firstData); } @@ -22,7 +20,7 @@ export class Sha256 implements HashFunction { } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } @@ -34,11 +32,9 @@ export function sha256(data: Uint8Array): Uint8Array { export class Sha512 implements HashFunction { public readonly blockSize = 1024 / 8; - private readonly impl: Hash; + private readonly impl = nobleSha512.create(); public constructor(firstData?: Uint8Array) { - this.impl = shajs("sha512"); - if (firstData) { this.update(firstData); } @@ -50,7 +46,7 @@ export class Sha512 implements HashFunction { } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } diff --git a/yarn.lock b/yarn.lock index 53006282..af9095f8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -465,7 +465,6 @@ __metadata: "@types/karma-jasmine-html-reporter": ^1 "@types/libsodium-wrappers": ^0.7.7 "@types/node": ^15.0.1 - "@types/sha.js": ^2.4.0 "@typescript-eslint/eslint-plugin": ^4.28 "@typescript-eslint/parser": ^4.28 bip39: ^3.0.2 @@ -492,7 +491,6 @@ __metadata: nyc: ^15.1.0 prettier: ^2.4.1 ses: ^0.11.0 - sha.js: ^2.4.11 source-map-support: ^0.5.19 stream-browserify: ^3.0.0 ts-node: ^8 @@ -1801,15 +1799,6 @@ __metadata: languageName: node linkType: hard -"@types/sha.js@npm:^2.4.0": - version: 2.4.0 - resolution: "@types/sha.js@npm:2.4.0" - dependencies: - "@types/node": "*" - checksum: 0e1bd1a98d88dbbf87180b8ef81ab2bf8c2bd066a99858c76299fb599fd912df807072a55a6f484077498dd972a1b55eaa03aae6ea2654027a4dd0331db7e201 - languageName: node - linkType: hard - "@types/ws@npm:^6.0.1": version: 6.0.4 resolution: "@types/ws@npm:6.0.4" From 8b775c84bb57580468bc7d7a618dcfeaaa0a8f29 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Mon, 24 Jan 2022 17:09:28 +0100 Subject: [PATCH 4/4] Add toRealUint8Array --- packages/crypto/src/keccak.ts | 3 ++- packages/crypto/src/ripemd.ts | 3 ++- packages/crypto/src/sha.ts | 5 +++-- packages/crypto/src/utils.ts | 5 +++++ 4 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 packages/crypto/src/utils.ts diff --git a/packages/crypto/src/keccak.ts b/packages/crypto/src/keccak.ts index 66f647a7..4f82b047 100644 --- a/packages/crypto/src/keccak.ts +++ b/packages/crypto/src/keccak.ts @@ -1,6 +1,7 @@ import { keccak_256 } from "@noble/hashes/sha3"; import { HashFunction } from "./hash"; +import { toRealUint8Array } from "./utils"; export class Keccak256 implements HashFunction { public readonly blockSize = 512 / 8; @@ -14,7 +15,7 @@ export class Keccak256 implements HashFunction { } public update(data: Uint8Array): Keccak256 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } diff --git a/packages/crypto/src/ripemd.ts b/packages/crypto/src/ripemd.ts index 263e573c..2aad9402 100644 --- a/packages/crypto/src/ripemd.ts +++ b/packages/crypto/src/ripemd.ts @@ -1,6 +1,7 @@ import { ripemd160 as nobleRipemd160 } from "@noble/hashes/ripemd160"; import { HashFunction } from "./hash"; +import { toRealUint8Array } from "./utils"; export class Ripemd160 implements HashFunction { public readonly blockSize = 512 / 8; @@ -14,7 +15,7 @@ export class Ripemd160 implements HashFunction { } public update(data: Uint8Array): Ripemd160 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } diff --git a/packages/crypto/src/sha.ts b/packages/crypto/src/sha.ts index 25656aed..4e59e81d 100644 --- a/packages/crypto/src/sha.ts +++ b/packages/crypto/src/sha.ts @@ -2,6 +2,7 @@ import { sha256 as nobleSha256 } from "@noble/hashes/sha256"; import { sha512 as nobleSha512 } from "@noble/hashes/sha512"; import { HashFunction } from "./hash"; +import { toRealUint8Array } from "./utils"; export class Sha256 implements HashFunction { public readonly blockSize = 512 / 8; @@ -15,7 +16,7 @@ export class Sha256 implements HashFunction { } public update(data: Uint8Array): Sha256 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } @@ -41,7 +42,7 @@ export class Sha512 implements HashFunction { } public update(data: Uint8Array): Sha512 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } diff --git a/packages/crypto/src/utils.ts b/packages/crypto/src/utils.ts new file mode 100644 index 00000000..9dbec0d0 --- /dev/null +++ b/packages/crypto/src/utils.ts @@ -0,0 +1,5 @@ +// See https://github.com/paulmillr/noble-hashes/issues/25 for why this is needed +export function toRealUint8Array(data: ArrayLike): Uint8Array { + if (data instanceof Uint8Array) return data; + else return Uint8Array.from(data); +}