File tree Expand file tree Collapse file tree 5 files changed +58
-1
lines changed Expand file tree Collapse file tree 5 files changed +58
-1
lines changed Original file line number Diff line number Diff line change 5656 "@bitgo/utxo-lib" : " ^11.6.3" ,
5757 "@bitgo/wasm-miniscript" : " 2.0.0-beta.7" ,
5858 "bip174" :
" npm:@bitgo-forks/[email protected] " ,
59- "bitcoinjs-message" :
" npm:@bitgo-forks/[email protected] " 59+ "bitcoinjs-message" :
" npm:@bitgo-forks/[email protected] " ,
60+ "fast-sha256" : " ^1.3.0"
6061 },
6162 "gitHead" : " 18e460ddf02de2dbf13c2aa243478188fb539f0c"
6263}
Original file line number Diff line number Diff line change 1+ export * from './toSpend' ;
Original file line number Diff line number Diff line change 1+ import { Hash } from 'fast-sha256' ;
2+
3+ export const BIP322_TAG = 'BIP0322-signed-message' ;
4+
5+ /**
6+ * Perform a tagged hash
7+ *
8+ * @param {string | Buffer } message - The message to hash as a Buffer or utf-8 string
9+ * @param {Buffer } [tag=BIP322_TAG] - The tag to use for hashing, defaults to BIP322_TAG.
10+ * @returns {Buffer } - The resulting hash of the message with the tag.
11+ */
12+ export function hashMessageWithTag ( message : string | Buffer , tag = BIP322_TAG ) : Buffer {
13+ // Compute the message hash - SHA256(SHA256(tag) || SHA256(tag) || message)
14+ // Reference: https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki#full
15+ const tagHasher = new Hash ( ) ;
16+ tagHasher . update ( Buffer . from ( BIP322_TAG ) ) ;
17+ const tagHash = tagHasher . digest ( ) ;
18+ const messageHasher = new Hash ( ) ;
19+ messageHasher . update ( tagHash ) ;
20+ messageHasher . update ( tagHash ) ;
21+ messageHasher . update ( Buffer . from ( message ) ) ;
22+ const messageHash = messageHasher . digest ( ) ;
23+ return Buffer . from ( messageHash ) ;
24+ }
Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ export * as descriptor from './descriptor';
33export * as testutil from './testutil' ;
44export * as paygo from './paygo' ;
55export * as bip32utils from './bip32utils' ;
6+ export * as bip322 from './bip322' ;
67export * from './dustThreshold' ;
78export * from './Output' ;
89export * from './xOnlyPubkey' ;
Original file line number Diff line number Diff line change 1+ import assert from 'assert' ;
2+
3+ import { hashMessageWithTag } from '../../src/bip322' ;
4+
5+ describe ( 'to_spend' , function ( ) {
6+ describe ( 'Message hashing' , function ( ) {
7+ // Test vectors from BIP322
8+ // Source: https://github.com/bitcoin/bips/blob/master/bip-0322.mediawiki#message-hashing
9+ const fixtures = [
10+ {
11+ message : '' ,
12+ hash : 'c90c269c4f8fcbe6880f72a721ddfbf1914268a794cbb21cfafee13770ae19f1' ,
13+ } ,
14+ {
15+ message : 'Hello World' ,
16+ hash : 'f0eb03b1a75ac6d9847f55c624a99169b5dccba2a31f5b23bea77ba270de0a7a' ,
17+ } ,
18+ ] ;
19+ fixtures . forEach ( ( { message, hash } ) => {
20+ it ( `should hash the message "${ message } "` , function ( ) {
21+ const result = hashMessageWithTag ( Buffer . from ( message ) ) ;
22+ assert . deepStrictEqual (
23+ result . toString ( 'hex' ) ,
24+ hash ,
25+ `Hash for message "${ message } " does not match expected value`
26+ ) ;
27+ } ) ;
28+ } ) ;
29+ } ) ;
30+ } ) ;
You can’t perform that action at this time.
0 commit comments