@@ -7,6 +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
11
* @prop {KeySignP1363 } signP1363
11
12
* @prop {ASN1ToP1363Signature } asn1ToP1363Signature
12
13
*/
@@ -25,6 +26,13 @@ let Secp256k1 = require("@dashincubator/secp256k1");
25
26
* @returns {Promise<Uint8Array> }
26
27
*/
27
28
29
+ /**
30
+ * @callback KeySignEth
31
+ * @param {Uint8Array } privateKey
32
+ * @param {Uint8Array } hashBytes
33
+ * @returns {Promise<Uint8Array> }
34
+ */
35
+
28
36
/**
29
37
* @callback KeySignP1363
30
38
* @param {Uint8Array } privateKey
@@ -76,13 +84,32 @@ KeyUtils.signAsn1 = async function (privKeyBytes, hashBytes) {
76
84
return sigBytes ;
77
85
} ;
78
86
87
+ KeyUtils . signEth = async function ( privKeyBytes , hashBytes ) {
88
+ let ETH_OFFSET = 27 ; // maybe the right number... maybe not
89
+ let testing = true ;
90
+ let sigOpts = { canonical : true , der : false , recovered : true } ;
91
+ if ( ! testing ) {
92
+ Object . assign ( { extraEntropy : true } ) ;
93
+ }
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 ;
104
+ } ;
105
+
79
106
KeyUtils . signP1363 = async function ( privKeyBytes , hashBytes , sigBytes ) {
80
107
let asn1Bytes = await KeyUtils . signAsn1 ( privKeyBytes , hashBytes ) ;
81
108
let p1363Bytes = KeyUtils . asn1ToP1363Signature ( asn1Bytes , sigBytes ) ;
82
109
// TODO DEBUG TESTING
83
- for ( let i = 0 ; i < p1363Bytes . length ; i += 1 ) {
84
- p1363Bytes [ i ] = 0xff ;
85
- }
110
+ // for (let i = 0; i < p1363Bytes.length; i += 1) {
111
+ // p1363Bytes[i] = 0xff;
112
+ // }
86
113
return p1363Bytes ;
87
114
} ;
88
115
0 commit comments