diff --git a/package.json b/package.json index b235fe8..fa09535 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "type": "module", "name": "@btc-vision/bsi-binary", - "version": "1.0.9", + "version": "1.0.10", "description": "", "main": "build/index.js", "types": "build/index.d.ts", @@ -22,7 +22,7 @@ "install": "gulp && gulp cjs" }, "browser": { - "crypto": "./src/crypto/crypto-browser.js", + "crypto": false, "stream": false }, "author": "BlobMaster41", @@ -30,6 +30,7 @@ "devDependencies": { "@babel/preset-env": "^7.24.3", "@types/node": "^20.11.30", + "@types/sha.js": "^2.4.4", "eslint": "^8.57.0", "jest": "^29.7.0", "prettier": "3.2.5", @@ -47,6 +48,7 @@ "gulp-cached": "^1.1.1", "gulp-logger": "^0.0.2", "gulp-typescript": "^6.0.0-alpha.1", + "sha.js": "^2.4.11", "tiny-secp256k1": "^2.2.3", "ts-jest": "^29.1.2", "ts-node": "^10.9.2" diff --git a/src/abi/ABICoder.ts b/src/abi/ABICoder.ts index 8d295de..8ed96e4 100644 --- a/src/abi/ABICoder.ts +++ b/src/abi/ABICoder.ts @@ -1,4 +1,5 @@ -import { createHash } from 'crypto'; +import shajs from 'sha.js'; + import { BinaryReader } from '../buffer/BinaryReader.js'; import { BufferHelper } from '../utils/BufferHelper.js'; @@ -111,6 +112,6 @@ export class ABICoder { } private sha256(buffer: Buffer | string | Uint8Array): Buffer { - return createHash('sha256').update(buffer).digest(); + return new shajs.sha256().update(buffer).digest(); } } diff --git a/src/crypto/crypto-browser.js b/src/crypto/crypto-browser.js deleted file mode 100644 index 3f64a21..0000000 --- a/src/crypto/crypto-browser.js +++ /dev/null @@ -1,71 +0,0 @@ -/* Browser Crypto Shims */ -import { hmac } from '@noble/hashes/hmac'; -import { pbkdf2 } from '@noble/hashes/pbkdf2'; -import { sha256 } from '@noble/hashes/sha256'; -import { sha512 } from '@noble/hashes/sha512'; -import { assertArgument } from 'ethers'; - -function getGlobal() { - if (typeof self !== 'undefined') { - return self; - } - if (typeof window !== 'undefined') { - return window; - } - if (typeof global !== 'undefined') { - return global; - } - throw new Error('unable to locate global object'); -} - -const anyGlobal = getGlobal(); -const crypto = anyGlobal.crypto || anyGlobal.msCrypto; - -export function createHash(algo) { - switch (algo) { - case 'sha256': - return sha256.create(); - case 'sha512': - return sha512.create(); - } - assertArgument(false, 'invalid hashing algorithm name', 'algorithm', algo); -} - -export function createHmac(_algo, key) { - const algo = { sha256, sha512 }[_algo]; - assertArgument(algo != null, 'invalid hmac algorithm', 'algorithm', _algo); - return hmac.create(algo, key); -} - -export function pbkdf2Sync(password, salt, iterations, keylen, _algo) { - const algo = { sha256, sha512 }[_algo]; - assertArgument(algo != null, 'invalid pbkdf2 algorithm', 'algorithm', _algo); - return pbkdf2(algo, password, salt, { c: iterations, dkLen: keylen }); -} - -export function randomBytes(length) { - assert( - crypto != null, - 'platform does not support secure random numbers', - 'UNSUPPORTED_OPERATION', - { - operation: 'randomBytes', - }, - ); - assertArgument( - Number.isInteger(length) && length > 0 && length <= 1024, - 'invalid length', - 'length', - length, - ); - const result = new Uint8Array(length); - crypto.getRandomValues(result); - return result; -} - -export default { - createHash, - createHmac, - pbkdf2Sync, - randomBytes, -};