This repository has been archived by the owner on Dec 14, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 60
/
ecpair.js
205 lines (170 loc) · 4.87 KB
/
ecpair.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
var base58check = require('bs58check')
var bcrypto = require('./crypto')
var ecdsa = require('./ecdsa')
var ECSignature = require('./ecsignature')
var randomBytes = require('randombytes')
var typeforce = require('typeforce')
var types = require('./types')
var wif = require('wif')
var NETWORKS = require('./networks')
var BigInteger = require('bigi')
var ecurve = require('ecurve')
var secp256k1 = ecurve.getCurveByName('secp256k1')
var secp256k1native = require('secp256k1')
/**
* Provide either `d` or `Q` but not both.
*
* @constructor
* @param {BigInteger} [d] Private key.
* @param {Point} [Q] Public key.
* @param {object} [options]
* @param {boolean} [options.compressed=true]
* @param {Network} [options.network=networks.ark]
*/
function ECPair (d, Q, options) {
if (options) {
typeforce({
compressed: types.maybe(types.Boolean),
network: types.maybe(types.Network)
}, options)
}
options = options || {}
if (d) {
if (d.signum() <= 0) throw new Error('Private key must be greater than 0')
if (d.compareTo(secp256k1.n) >= 0) throw new Error('Private key must be less than the curve order')
if (Q) throw new TypeError('Unexpected publicKey parameter')
this.d = d
} else {
typeforce(types.ECPoint, Q)
this.__Q = Q
}
/** @type {boolean} */ this.compressed = options.compressed === undefined ? true : options.compressed
/** @type {Network} */ this.network = options.network || NETWORKS.ark
}
Object.defineProperty(ECPair.prototype, 'Q', {
get: function () {
if (!this.__Q && this.d) {
this.__Q = secp256k1.G.multiply(this.d)
}
return this.__Q
}
})
/**
* @param {Buffer} buffer
* @param {Network} [network=networks.ark]
* @returns {ECPair}
*/
ECPair.fromPublicKeyBuffer = function (buffer, network) {
var Q = ecurve.Point.decodeFrom(secp256k1, buffer)
return new ECPair(null, Q, {
compressed: Q.compressed,
network: network
})
}
/**
* @param {string} string
* @param {Network[]|Network} network
* @returns {ECPair}
*/
ECPair.fromWIF = function (string, network) {
var decoded = wif.decode(string)
var version = decoded.version
// [network, ...]
if (types.Array(network)) {
network = network.filter(function (network) {
return version === network.wif
}).pop()
if (!network) throw new Error('Unknown network version')
// network
} else {
network = network || NETWORKS.ark
if (version !== network.wif) throw new Error('Invalid network version')
}
var d = BigInteger.fromBuffer(decoded.privateKey)
return new ECPair(d, null, {
compressed: decoded.compressed,
network: network
})
}
/**
* @param {object} [options]
* @param {function} [options.rng]
* @param {boolean} [options.compressed=true]
* @param {Network} [options.network=networks.ark]
*/
ECPair.makeRandom = function (options) {
options = options || {}
var rng = options.rng || randomBytes
var d
do {
var buffer = rng(32)
typeforce(types.Buffer256bit, buffer)
d = BigInteger.fromBuffer(buffer)
} while (d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0)
return new ECPair(d, null, options)
}
/**
* @param {string} seed
* @param {object} [options]
* @param {boolean} [options.compressed=true]
* @param {Network} [options.network=networks.ark]
* @returns {ECPair}
*/
ECPair.fromSeed = function(seed, options) {
var hash = bcrypto.sha256(new Buffer(seed,"utf-8"))
var d = BigInteger.fromBuffer(hash)
if(d.signum() <= 0 || d.compareTo(secp256k1.n) >= 0){
throw new Error("seed cannot resolve to a compatible private key")
}
else{
return new ECPair(d, null, options)
}
}
/**
* @returns {string}
*/
ECPair.prototype.getAddress = function () {
var payload = new Buffer(21)
var hash = bcrypto.ripemd160(this.getPublicKeyBuffer())
var version = this.getNetwork().pubKeyHash
payload.writeUInt8(version, 0)
hash.copy(payload, 1)
return base58check.encode(payload)
}
/**
* @returns {Network}
*/
ECPair.prototype.getNetwork = function () {
return this.network
}
ECPair.prototype.getPublicKeyBuffer = function () {
return this.Q.getEncoded(this.compressed)
}
/**
* Requires a private key (`d`) to be set.
*
* @param {Buffer} hash
* @returns {ECSignature}
*/
ECPair.prototype.sign = function (hash) {
if (!this.d) throw new Error('Missing private key')
var native=secp256k1native.sign(hash, this.d.toBuffer(32))
return ECSignature.parseNativeSecp256k1(native).signature
}
/**
* Requires a private key (`d`) to be set.
*
* @returns {string}
*/
ECPair.prototype.toWIF = function () {
if (!this.d) throw new Error('Missing private key')
return wif.encode(this.network.wif, this.d.toBuffer(32), this.compressed)
}
/**
* @param {Buffer} hash
* @returns {boolean}
*/
ECPair.prototype.verify = function (hash, signature) {
return secp256k1native.verify(hash, signature.toNativeSecp256k1(), this.Q.getEncoded(this.compressed))
}
module.exports = ECPair