@@ -7,7 +7,7 @@ let Secp256k1 = require("@dashincubator/secp256k1");
7
7
* @typedef KeyUtilsPartial
8
8
* @prop {KeySet } set
9
9
* @prop {KeySignAsn1 } signAsn1
10
- * @prop {KeySignEth } signEth
10
+ * @prop {KeySignMagic } magicSign
11
11
* @prop {KeySignP1363 } signP1363
12
12
* @prop {ASN1ToP1363Signature } asn1ToP1363Signature
13
13
*/
@@ -27,9 +27,10 @@ let Secp256k1 = require("@dashincubator/secp256k1");
27
27
*/
28
28
29
29
/**
30
- * @callback KeySignEth
31
- * @param {Uint8Array } privateKey
32
- * @param {Uint8Array } hashBytes
30
+ * @callback KeySignMagic
31
+ * @param {Object } opts
32
+ * @param {Uint8Array } opts.privKeyBytes
33
+ * @param {Uint8Array } opts.doubleSha256Bytes
33
34
* @returns {Promise<Uint8Array> }
34
35
*/
35
36
@@ -84,23 +85,27 @@ KeyUtils.signAsn1 = async function (privKeyBytes, hashBytes) {
84
85
return sigBytes ;
85
86
} ;
86
87
87
- KeyUtils . signEth = async function ( privKeyBytes , hashBytes ) {
88
- let ETH_OFFSET = 27 ; // maybe the right number... maybe not
88
+ KeyUtils . magicSign = async function ( { privKeyBytes, doubleSha256Bytes } ) {
89
+ if ( doubleSha256Bytes ?. length !== 32 ) {
90
+ throw new Error ( `'doubleSha256Bytes' must be a 32-byte double sha256 hash` ) ;
91
+ }
92
+
93
+ let MAGIC_OFFSET = 27 + 4 ; // 27 because bitcoin, 4 because "compressed" key
89
94
let testing = true ;
90
95
let sigOpts = { canonical : true , der : false , recovered : true } ;
91
96
if ( ! testing ) {
92
97
Object . assign ( { extraEntropy : true } ) ;
93
98
}
94
- let recoverySig = await Secp256k1 . sign ( hashBytes , privKeyBytes , sigOpts ) ;
95
- let ethSig = new Uint8Array ( 65 ) ;
96
- let recovery = ETH_OFFSET + recoverySig [ 1 ] ;
97
- ethSig [ 0 ] = recovery ;
98
- ethSig . set ( recoverySig [ 0 ] , 1 ) ;
99
- // console.log(`DEBUG [SIGNATURE] rawBytes:` );
100
- // console.log(recoverySig[0]);
101
- // console.log(`DEBUG [RECOVERY] rawBytes:`) ;
102
- // console.log (recoverySig[1] );
103
- return ethSig ;
99
+ let recoverySig = await Secp256k1 . sign (
100
+ doubleSha256Bytes ,
101
+ privKeyBytes ,
102
+ sigOpts ,
103
+ ) ;
104
+ let magicSig = new Uint8Array ( 65 ) ;
105
+ // the magic byte is prepended (the signature is NOT reversed)
106
+ magicSig [ 0 ] = MAGIC_OFFSET + recoverySig [ 1 ] ;
107
+ magicSig . set ( recoverySig [ 0 ] , 1 ) ;
108
+ return magicSig ;
104
109
} ;
105
110
106
111
KeyUtils . signP1363 = async function ( privKeyBytes , hashBytes , sigBytes ) {
0 commit comments