diff --git a/.pnp.cjs b/.pnp.cjs index a6ff409130..d462e57f9d 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" @@ -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" @@ -1770,10 +1766,6 @@ function $$SETUP_STATE(hydrateRuntimeState, basePath) { "jest-worker", "npm:26.6.2" ], - [ - "js-sha3", - "npm:0.8.0" - ], [ "js-sha512", "npm:0.8.0" @@ -3372,6 +3364,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,8 +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/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"], ["bip39", "npm:3.0.4"], @@ -3400,7 +3391,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"], @@ -3409,9 +3399,7 @@ 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"], ["stream-browserify", "npm:3.0.0"], ["ts-node", "virtual:4f1584ad4aba8733a24be7c8aebbffafef25607f2d00f4b314cf96717145c692763628a31c2b85d4686fbb091ff21ebffa3cc337399c042c19a32b9bdb786464#npm:8.10.2"], @@ -4224,6 +4212,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 +4844,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/", @@ -4877,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/", @@ -9243,15 +9220,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/@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 0000000000..f19012b4d1 --- /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 8404bdd625..0000000000 --- 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/.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 5b0a9b8951..0000000000 --- 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/.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 3363d1e08c..0000000000 --- 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 72aee309ad..6330c3b0e8 100644 --- a/packages/crypto/package.json +++ b/packages/crypto/package.json @@ -44,13 +44,11 @@ "@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" + "libsodium-wrappers": "^0.7.6" }, "devDependencies": { "@istanbuljs/nyc-config-typescript": "^1.0.1", @@ -63,8 +61,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", "buffer": "^6.0.3", diff --git a/packages/crypto/src/keccak.ts b/packages/crypto/src/keccak.ts index ea4e7fc3ad..4f82b047ef 100644 --- a/packages/crypto/src/keccak.ts +++ b/packages/crypto/src/keccak.ts @@ -1,27 +1,26 @@ -import jssha3 from "js-sha3"; +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; - 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); } } public update(data: Uint8Array): Keccak256 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } diff --git a/packages/crypto/src/ripemd.ts b/packages/crypto/src/ripemd.ts index 258a43e014..2aad9402f5 100644 --- a/packages/crypto/src/ripemd.ts +++ b/packages/crypto/src/ripemd.ts @@ -1,11 +1,12 @@ -import RIPEMD160 from "ripemd160"; +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; - private readonly impl = new RIPEMD160(); + private readonly impl = nobleRipemd160.create(); public constructor(firstData?: Uint8Array) { if (firstData) { @@ -14,12 +15,12 @@ export class Ripemd160 implements HashFunction { } public update(data: Uint8Array): Ripemd160 { - this.impl.update(Buffer.from(data)); + this.impl.update(toRealUint8Array(data)); return this; } public digest(): Uint8Array { - return Uint8Array.from(this.impl.digest()); + return this.impl.digest(); } } diff --git a/packages/crypto/src/sha.ts b/packages/crypto/src/sha.ts index 47a3df01f8..4e59e81d73 100644 --- a/packages/crypto/src/sha.ts +++ b/packages/crypto/src/sha.ts @@ -1,28 +1,27 @@ -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"; +import { toRealUint8Array } from "./utils"; 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); } } public update(data: Uint8Array): Sha256 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } @@ -34,23 +33,21 @@ 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); } } public update(data: Uint8Array): Sha512 { - this.impl.update(data); + this.impl.update(toRealUint8Array(data)); return this; } public digest(): Uint8Array { - return new Uint8Array(this.impl.digest()); + return this.impl.digest(); } } diff --git a/packages/crypto/src/utils.ts b/packages/crypto/src/utils.ts new file mode 100644 index 0000000000..9dbec0d0e0 --- /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); +} diff --git a/yarn.lock b/yarn.lock index 38ffcfda99..af9095f817 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,8 +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 bip39: ^3.0.2 @@ -483,7 +482,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 @@ -492,9 +490,7 @@ __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 stream-browserify: ^3.0.0 ts-node: ^8 @@ -1248,6 +1244,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 +1782,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" @@ -1805,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" @@ -4851,13 +4836,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"