forked from cryptocoinjs/secp256k1-node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
232 lines (204 loc) · 5.42 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
function pad32 (msg) {
var buf
if (msg.length < 32) {
buf = new Buffer(32)
buf.fill(0)
msg.copy(buf, 32 - msg.length)
return buf
} else {
return msg
}
}
/**
* This module provides native bindings to ecdsa [secp256k1](https://github.com/bitcoin/secp256k1) functions
* @module secp256k1
*/
var secpNode = require('bindings')('secp256k1')
/**
* Verify an ECDSA secret key.
* @method verifySecetKey
* @param {Buffer} sercetKey the sercet Key to verify
* @return {Boolean} `true` if sercet key is valid, `false` sercet key is invalid
*/
exports.verifySecretKey = function (sercetKey) {
return Boolean(secpNode.secKeyVerify(sercetKey))
}
/**
* Verify an ECDSA public key.
* @method verifyPublicKey
* @param {Buffer} publicKey the public Key to verify
* @return {Boolean} `true` if public key is valid, `false` sercet key is invalid
*/
exports.verifyPublicKey = function (publicKey) {
return Boolean(secpNode.pubKeyVerify(publicKey))
}
/**
* Create an ECDSA signature.
* @method sign
* @param {Buffer} secretkey a 32-byte secret key (assumed to be valid)
* @param {Buffer} msg he message being signed
* @param {Function} cb the callback given. The callback is given the signature
* @returns {Buffer} if no callback is given a 72-byte signature is returned
*/
exports.sign = function (msg, secretKey, DER, cb) {
if (typeof DER === 'function') {
cb = DER
DER = false
}
if (!DER) {
DER = false
}
var result
if (typeof cb === 'function') {
secpNode.sign(pad32(msg), secretKey, DER, function (err, sig, recid) {
if (!DER) {
cb(err, {
signature: sig,
recovery: recid
})
} else {
cb(err, sig)
}
})
} else {
result = secpNode.sign(pad32(msg), secretKey, DER)
if (DER) {
return result[0]
} else {
return {
signature: result[0],
recovery: result[1]
}
}
}
}
/**
* Verify an ECDSA signature.
* @method verify
* @param {Buffer} mgs the message
* @param {Buffer|Object} sig the signature
* @param {Buffer} pubKey the public key
* @return {Integer}
*
* - 1: correct signature
* - 0: incorrect signature
*/
exports.verify = function (msg, sig, pubKey, cb) {
var recid = recid ? sig.recovery : -1
if (sig.signature) {
sig = sig.signature
}
var DER = true
if (sig.length === 64) {
DER = false
}
if (!Buffer.isBuffer(pubKey)) {
throw new Error('the Public Key needs to be a buffer')
}
if (cb) {
secpNode.verify(pubKey, pad32(msg), sig, recid, DER, cb)
} else {
return secpNode.verify(pubKey, pad32(msg), sig, recid, DER)
}
}
/**
* Recover an ECDSA public key from a compact signature. In the process also verifing it.
* @method recoverCompact
* @param {Buffer} msg the message assumed to be signed
* @param {Buffer} sig the signature as 64 byte buffer
* @param {Integer} recid the recovery id (as returned by ecdsa_sign_compact)
* @param {Boolean} compressed whether to recover a compressed or uncompressed pubkey
* @param {Function} [cb]
* @return {Buffer} the pubkey, a 33 or 65 byte buffer
*/
exports.recover = function (msg, sig, compressed, cb) {
var recid = sig.recovery !== undefined ? sig.recovery : -1
if (sig.signature) {
sig = sig.signature
}
var DER = true
if (sig.length === 64) {
DER = false
}
if (typeof compressed === 'function') {
cb = compressed
compressed = true
}
if (compressed === undefined) {
compressed = true
}
if (!DER && (recid < 0 || recid > 3)) {
var error = new Error('recovery id must be >= 0 && recid <= 3')
if (typeof cb !== 'function') {
throw error
} else {
return cb(error)
}
}
if (!cb) {
return secpNode.recover(pad32(msg), sig, recid, compressed, DER)
} else {
secpNode.recover(pad32(msg), sig, recid, compressed, DER, cb)
}
}
/**
* Compute the public key for a secret key.
* @method createPubKey
* @param {Buffer} secKey a 32-byte private key.
* @param {Boolean} [compressed=0] whether the computed public key should be compressed
* @return {Buffer} a 33-byte (if compressed) or 65-byte (if uncompressed) area to store the public key.
*/
exports.createPublicKey = function (secKey, compressed) {
if (!secKey) {
throw new Error('invalid private key')
}
compressed = compressed ? 1 : 0
return secpNode.pubKeyCreate(secKey, compressed)
}
/**
* @method exportPrivateKey
* @param {Buffer} secertKey
* @param {Boolean} compressed
* @return {Buffer} privateKey
*/
exports.exportPrivateKey = secpNode.privKeyExport
/**
* @method importPrivateKey
* @param {Buffer} privateKey
* @return {Buffer} secertKey
*/
exports.importPrivateKey = secpNode.privKeyImport
/**
* @method decompressPublickey
* @param {Buffer} secretKey
* @return {Buffer}
*/
exports.decompressPublicKey = secpNode.pubKeyDecompress
/**
* @method privKeyTweakAdd
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privKeyTweakAdd = secpNode.privKeyTweakAdd
/**
* @method privKeyTweakMul
* @param {Buffer} privateKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.privKeyTweakMul = secpNode.privKeyTweakMul
/**
* @method pubKeyTweakAdd
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.pubKeyTweakAdd = secpNode.pubKeyTweakAdd
/**
* @method pubKeyTweakMul
* @param {Buffer} publicKey
* @param {Buffer} tweak
* @return {Buffer}
*/
exports.pubKeyTweakMul = secpNode.pubKeyTweakMul