Skip to content

feat(crypto): restore ripemd160, sha1 and sha256 hash helpers #2248

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion src/cjs/crypto.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,12 @@ var __importStar =
return result;
};
Object.defineProperty(exports, '__esModule', { value: true });
exports.TAGGED_HASH_PREFIXES = exports.TAGS = void 0;
exports.TAGGED_HASH_PREFIXES =
exports.TAGS =
exports.sha1 =
exports.sha256 =
exports.ripemd160 =
void 0;
exports.hash160 = hash160;
exports.hash256 = hash256;
exports.taggedHash = taggedHash;
Expand All @@ -55,8 +60,27 @@ exports.taggedHash = taggedHash;
* @packageDocumentation
*/
const ripemd160_1 = require('@noble/hashes/ripemd160');
Object.defineProperty(exports, 'ripemd160', {
enumerable: true,
get: function () {
return ripemd160_1.ripemd160;
},
});
const sha256_1 = require('@noble/hashes/sha256');
Object.defineProperty(exports, 'sha256', {
enumerable: true,
get: function () {
return sha256_1.sha256;
},
});
const tools = __importStar(require('uint8array-tools'));
var sha1_1 = require('@noble/hashes/sha1');
Object.defineProperty(exports, 'sha1', {
enumerable: true,
get: function () {
return sha1_1.sha1;
},
});
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
Expand Down
11 changes: 10 additions & 1 deletion src/cjs/crypto.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
/**
* A module for hashing functions.
* include ripemd160、sha1、sha256、hash160、hash256、taggedHash
*
* @packageDocumentation
*/
import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
export { ripemd160, sha256 };
export { sha1 } from '@noble/hashes/sha1';
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
Expand Down Expand Up @@ -43,4 +53,3 @@ export declare const TAGGED_HASH_PREFIXES: TaggedHashPrefixes;
* @returns The resulting tagged hash as a `Uint8Array`.
*/
export declare function taggedHash(prefix: TaggedHashPrefix, data: Uint8Array): Uint8Array;
export {};
2 changes: 2 additions & 0 deletions src/esm/crypto.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
import * as tools from 'uint8array-tools';
export { ripemd160, sha256 };
export { sha1 } from '@noble/hashes/sha1';
/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
Expand Down
2 changes: 1 addition & 1 deletion test/crypto.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { TAGS, TAGGED_HASH_PREFIXES } from 'bitcoinjs-lib/src/crypto';
import { sha256 } from '@noble/hashes/sha256';

describe('crypto', () => {
['hash160', 'hash256'].forEach(algorithm => {
['hash160', 'hash256', 'ripemd160', 'sha1', 'sha256'].forEach(algorithm => {
describe(algorithm, () => {
fixtures.hashes.forEach(f => {
const fn = (bcrypto as any)[algorithm];
Expand Down
20 changes: 16 additions & 4 deletions test/fixtures/crypto.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,34 @@
{
"hex": "0000000000000001",
"hash160": "cdb00698f02afd929ffabea308340fa99ac2afa8",
"hash256": "3ae5c198d17634e79059c2cd735491553d22c4e09d1d9fea3ecf214565df2284"
"hash256": "3ae5c198d17634e79059c2cd735491553d22c4e09d1d9fea3ecf214565df2284",
"ripemd160": "8d1a05d1bc08870968eb8a81ad4393fd3aac6633",
"sha1": "cb473678976f425d6ec1339838f11011007ad27d",
"sha256": "cd2662154e6d76b2b2b92e70c0cac3ccf534f9b74eb5b89819ec509083d00a50"
},
{
"hex": "0101010101010101",
"hash160": "abaf1119f83e384210fe8e222eac76e2f0da39dc",
"hash256": "728338d99f356175c4945ef5cccfa61b7b56143cbbf426ddd0e0fc7cfe8c3c23"
"hash256": "728338d99f356175c4945ef5cccfa61b7b56143cbbf426ddd0e0fc7cfe8c3c23",
"ripemd160": "5825701b4b9767fd35063b286dca3582853e0630",
"sha1": "c0357a32ed1f6a03be92dd094476f7f1a2e214ec",
"sha256": "04abc8821a06e5a30937967d11ad10221cb5ac3b5273e434f1284ee87129a061"
},
{
"hex": "ffffffffffffffff",
"hash160": "f86221f5a1fca059a865c0b7d374dfa9d5f3aeb4",
"hash256": "752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad"
"hash256": "752adad0a7b9ceca853768aebb6965eca126a62965f698a0c1bc43d83db632ad",
"ripemd160": "cb760221600ed34337ca3ab70016b5f58c838120",
"sha1": "be673e8a56eaa9d8c1d35064866701c11ef8e089",
"sha256": "12a3ae445661ce5dee78d0650d33362dec29c4f82af05e7e57fb595bbbacf0ca"
},
{
"hex": "4c6f72656d20697073756d20646f6c6f722073697420616d65742c20636f6e73656374657475722061646970697363696e6720656c69742e20446f6e65632061742066617563696275732073617069656e2c2076656c20666163696c6973697320617263752e20536564207574206d61737361206e6962682e205574206d6f6c6c69732070756c76696e6172206d617373612e20557420756c6c616d636f7270657220646f6c6f7220656e696d2c20696e206d6f6c657374696520656e696d20636f6e64696d656e74756d2061632e20416c697175616d206572617420766f6c75747061742e204e756c6c6120736f64616c657320617420647569206e656320",
"hash160": "9763e6b367c363bd6b88a7b361c98e6beee243a5",
"hash256": "033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799"
"hash256": "033588797115feb3545052670cac2a46584ab3cb460de63756ee0275e66b5799",
"ripemd160": "cad8593dcdef12ee334c97bab9787f07b3f3a1a5",
"sha1": "10d96fb43aca84e342206887bbeed3065d4e4344",
"sha256": "a7fb8276035057ed6479c5f2305a96da100ac43f0ac10f277e5ab8c5457429da"
}
],
"taggedHash": [
Expand Down
3 changes: 3 additions & 0 deletions ts_src/crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ import { ripemd160 } from '@noble/hashes/ripemd160';
import { sha256 } from '@noble/hashes/sha256';
import * as tools from 'uint8array-tools';

export { ripemd160, sha256 };
export { sha1 } from '@noble/hashes/sha1';

/**
* Computes the HASH160 (RIPEMD-160 after SHA-256) of the given buffer.
*
Expand Down
Loading