Skip to content

Commit 52559f8

Browse files
authored
Merge pull request #1879 from paulmillr/master
Switch from create-hash, ripemd160 to noble-hashes
2 parents 708fe38 + 4bf8a84 commit 52559f8

File tree

10 files changed

+212
-233
lines changed

10 files changed

+212
-233
lines changed

package-lock.json

+179-185
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+5-10
Original file line numberDiff line numberDiff line change
@@ -49,30 +49,25 @@
4949
"src"
5050
],
5151
"dependencies": {
52+
"@noble/hashes": "^1.2.0",
5253
"bech32": "^2.0.0",
5354
"bip174": "^2.1.0",
54-
"bs58check": "^2.1.2",
55-
"create-hash": "^1.1.0",
56-
"ripemd160": "^2.0.2",
55+
"bs58check": "^3.0.1",
5756
"typeforce": "^1.11.3",
58-
"varuint-bitcoin": "^1.1.2",
59-
"wif": "^2.0.1"
57+
"varuint-bitcoin": "^1.1.2"
6058
},
6159
"devDependencies": {
6260
"@types/bs58": "^4.0.0",
6361
"@types/bs58check": "^2.1.0",
64-
"@types/create-hash": "^1.2.2",
6562
"@types/mocha": "^5.2.7",
6663
"@types/node": "^16.11.7",
6764
"@types/proxyquire": "^1.3.28",
6865
"@types/randombytes": "^2.0.0",
69-
"@types/ripemd160": "^2.0.0",
70-
"@types/wif": "^2.0.2",
7166
"@typescript-eslint/eslint-plugin": "^5.45.0",
7267
"@typescript-eslint/parser": "^5.45.0",
7368
"better-npm-audit": "^3.7.3",
74-
"bip32": "^3.0.1",
75-
"bip39": "^3.0.2",
69+
"bip32": "^4.0.0",
70+
"bip39": "^3.1.0",
7671
"bip65": "^1.0.1",
7772
"bip68": "^1.0.3",
7873
"bs58": "^4.0.0",

src/address.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ function _toFutureSegwitAddress(output, network) {
4242
return toBech32(data, version, network.bech32);
4343
}
4444
function fromBase58Check(address) {
45-
const payload = bs58check.decode(address);
45+
const payload = Buffer.from(bs58check.decode(address));
4646
// TODO: 4.0.0, move to "toOutputScript"
4747
if (payload.length < 21) throw new TypeError(address + ' is too short');
4848
if (payload.length > 21) throw new TypeError(address + ' is too long');
49-
const version = payload.readUInt8(0);
49+
const version = payload.readUint8(0);
5050
const hash = payload.slice(1);
5151
return { version, hash };
5252
}

src/crypto.js

+12-15
Original file line numberDiff line numberDiff line change
@@ -9,34 +9,31 @@ exports.taggedHash =
99
exports.sha1 =
1010
exports.ripemd160 =
1111
void 0;
12-
const createHash = require('create-hash');
13-
const RipeMd160 = require('ripemd160');
12+
const ripemd160_1 = require('@noble/hashes/ripemd160');
13+
const sha1_1 = require('@noble/hashes/sha1');
14+
const sha256_1 = require('@noble/hashes/sha256');
1415
function ripemd160(buffer) {
15-
try {
16-
return createHash('rmd160').update(buffer).digest();
17-
} catch (err) {
18-
try {
19-
return createHash('ripemd160').update(buffer).digest();
20-
} catch (err2) {
21-
return new RipeMd160().update(buffer).digest();
22-
}
23-
}
16+
return Buffer.from((0, ripemd160_1.ripemd160)(Uint8Array.from(buffer)));
2417
}
2518
exports.ripemd160 = ripemd160;
2619
function sha1(buffer) {
27-
return createHash('sha1').update(buffer).digest();
20+
return Buffer.from((0, sha1_1.sha1)(Uint8Array.from(buffer)));
2821
}
2922
exports.sha1 = sha1;
3023
function sha256(buffer) {
31-
return createHash('sha256').update(buffer).digest();
24+
return Buffer.from((0, sha256_1.sha256)(Uint8Array.from(buffer)));
3225
}
3326
exports.sha256 = sha256;
3427
function hash160(buffer) {
35-
return ripemd160(sha256(buffer));
28+
return Buffer.from(
29+
(0, ripemd160_1.ripemd160)((0, sha256_1.sha256)(Uint8Array.from(buffer))),
30+
);
3631
}
3732
exports.hash160 = hash160;
3833
function hash256(buffer) {
39-
return sha256(sha256(buffer));
34+
return Buffer.from(
35+
(0, sha256_1.sha256)((0, sha256_1.sha256)(Uint8Array.from(buffer))),
36+
);
4037
}
4138
exports.hash256 = hash256;
4239
exports.TAGS = [

src/payments/p2pkh.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ function p2pkh(a, opts) {
2727
a,
2828
);
2929
const _address = lazy.value(() => {
30-
const payload = bs58check.decode(a.address);
30+
const payload = Buffer.from(bs58check.decode(a.address));
3131
const version = payload.readUInt8(0);
3232
const hash = payload.slice(1);
3333
return { version, hash };

src/payments/p2sh.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ function p2sh(a, opts) {
4848
}
4949
const o = { network };
5050
const _address = lazy.value(() => {
51-
const payload = bs58check.decode(a.address);
51+
const payload = Buffer.from(bs58check.decode(a.address));
5252
const version = payload.readUInt8(0);
5353
const hash = payload.slice(1);
5454
return { version, hash };

ts_src/address.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,13 @@ function _toFutureSegwitAddress(output: Buffer, network: Network): string {
5353
}
5454

5555
export function fromBase58Check(address: string): Base58CheckResult {
56-
const payload = bs58check.decode(address);
56+
const payload = Buffer.from(bs58check.decode(address));
5757

5858
// TODO: 4.0.0, move to "toOutputScript"
5959
if (payload.length < 21) throw new TypeError(address + ' is too short');
6060
if (payload.length > 21) throw new TypeError(address + ' is too long');
6161

62-
const version = payload.readUInt8(0);
62+
const version = payload.readUint8(0);
6363
const hash = payload.slice(1);
6464

6565
return { version, hash };

ts_src/crypto.ts

+8-15
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
1-
import * as createHash from 'create-hash';
2-
import * as RipeMd160 from 'ripemd160';
1+
import { ripemd160 as _ripemd160 } from '@noble/hashes/ripemd160';
2+
import { sha1 as _sha1 } from '@noble/hashes/sha1';
3+
import { sha256 as _sha256 } from '@noble/hashes/sha256';
34

45
export function ripemd160(buffer: Buffer): Buffer {
5-
try {
6-
return createHash('rmd160').update(buffer).digest();
7-
} catch (err) {
8-
try {
9-
return createHash('ripemd160').update(buffer).digest();
10-
} catch (err2) {
11-
return new RipeMd160().update(buffer).digest();
12-
}
13-
}
6+
return Buffer.from(_ripemd160(Uint8Array.from(buffer)));
147
}
158

169
export function sha1(buffer: Buffer): Buffer {
17-
return createHash('sha1').update(buffer).digest();
10+
return Buffer.from(_sha1(Uint8Array.from(buffer)));
1811
}
1912

2013
export function sha256(buffer: Buffer): Buffer {
21-
return createHash('sha256').update(buffer).digest();
14+
return Buffer.from(_sha256(Uint8Array.from(buffer)));
2215
}
2316

2417
export function hash160(buffer: Buffer): Buffer {
25-
return ripemd160(sha256(buffer));
18+
return Buffer.from(_ripemd160(_sha256(Uint8Array.from(buffer))));
2619
}
2720

2821
export function hash256(buffer: Buffer): Buffer {
29-
return sha256(sha256(buffer));
22+
return Buffer.from(_sha256(_sha256(Uint8Array.from(buffer))));
3023
}
3124

3225
export const TAGS = [

ts_src/payments/p2pkh.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function p2pkh(a: Payment, opts?: PaymentOpts): Payment {
2929
);
3030

3131
const _address = lazy.value(() => {
32-
const payload = bs58check.decode(a.address!);
32+
const payload = Buffer.from(bs58check.decode(a.address!));
3333
const version = payload.readUInt8(0);
3434
const hash = payload.slice(1);
3535
return { version, hash };

ts_src/payments/p2sh.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export function p2sh(a: Payment, opts?: PaymentOpts): Payment {
5757
const o: Payment = { network };
5858

5959
const _address = lazy.value(() => {
60-
const payload = bs58check.decode(a.address!);
60+
const payload = Buffer.from(bs58check.decode(a.address!));
6161
const version = payload.readUInt8(0);
6262
const hash = payload.slice(1);
6363
return { version, hash };

0 commit comments

Comments
 (0)