This repository was archived by the owner on Apr 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathprivatekey.js
More file actions
400 lines (357 loc) · 10.5 KB
/
Copy pathprivatekey.js
File metadata and controls
400 lines (357 loc) · 10.5 KB
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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
'use strict';
var _ = require('lodash');
var Address = require('./address');
var Base58Check = require('./encoding/base58check');
var BN = require('./crypto/bn');
var JSUtil = require('./util/js');
var Networks = require('./networks');
var Point = require('./crypto/point');
var PublicKey = require('./publickey');
var Random = require('./crypto/random');
var $ = require('./util/preconditions');
/**
* Instantiate a PrivateKey from a BN, Buffer and WIF.
*
* @example
* ```javascript
* // generate a new random key
* var key = PrivateKey();
*
* // get the associated address
* var address = key.toAddress();
*
* // encode into wallet export format
* var exported = key.toWIF();
*
* // instantiate from the exported (and saved) private key
* var imported = PrivateKey.fromWIF(exported);
* ```
*
* @param {string} data - The encoded data in various formats
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
* @returns {PrivateKey} A new valid instance of an PrivateKey
* @constructor
*/
function PrivateKey(data, network) {
/* jshint maxstatements: 20 */
/* jshint maxcomplexity: 8 */
if (!(this instanceof PrivateKey)) {
return new PrivateKey(data, network);
}
if (data instanceof PrivateKey) {
return data;
}
var info = this._classifyArguments(data, network);
// validation
if (!info.bn || info.bn.cmp(new BN(0)) === 0){
throw new TypeError('Number can not be equal to zero, undefined, null or false');
}
if (!info.bn.lt(Point.getN())) {
throw new TypeError('Number must be less than N');
}
if (typeof(info.network) === 'undefined') {
throw new TypeError('Must specify the network ("livenet" or "testnet")');
}
JSUtil.defineImmutable(this, {
bn: info.bn,
compressed: info.compressed,
network: info.network
});
Object.defineProperty(this, 'publicKey', {
configurable: false,
enumerable: true,
get: this.toPublicKey.bind(this)
});
return this;
};
/**
* Internal helper to instantiate PrivateKey internal `info` object from
* different kinds of arguments passed to the constructor.
*
* @param {*} data
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
* @return {Object}
*/
PrivateKey.prototype._classifyArguments = function(data, network) {
/* jshint maxcomplexity: 10 */
var info = {
compressed: true,
network: network ? Networks.get(network) : Networks.defaultNetwork
};
// detect type of data
if (_.isUndefined(data) || _.isNull(data)){
info.bn = PrivateKey._getRandomBN();
} else if (data instanceof BN) {
info.bn = data;
} else if (data instanceof Buffer || data instanceof Uint8Array) {
info = PrivateKey._transformBuffer(data, network);
} else if (data.bn && data.network){
info = PrivateKey._transformObject(data);
} else if (!network && Networks.get(data)) {
info.bn = PrivateKey._getRandomBN();
info.network = Networks.get(data);
} else if (typeof(data) === 'string'){
if (JSUtil.isHexa(data)) {
info.bn = new BN(data, 'hex');
} else {
info = PrivateKey._transformWIF(data, network);
}
} else {
throw new TypeError('First argument is an unrecognized data type.');
}
return info;
};
/**
* Internal function to get a random Big Number (BN)
*
* @returns {BN} A new randomly generated BN
* @private
*/
PrivateKey._getRandomBN = function(){
var condition;
var bn;
do {
var privbuf = Random.getRandomBuffer(32);
bn = BN.fromBuffer(privbuf);
condition = bn.lt(Point.getN());
} while (!condition);
return bn;
};
/**
* Internal function to transform a WIF Buffer into a private key
*
* @param {Buffer} buf - An WIF string
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
* @returns {Object} An object with keys: bn, network and compressed
* @private
*/
PrivateKey._transformBuffer = function(buf, network) {
var info = {};
if (buf.length === 32) {
return PrivateKey._transformBNBuffer(buf, network);
}
info.network = Networks.get(buf[0], 'privatekey');
if (!info.network) {
throw new Error('Invalid network');
}
if (network && info.network !== Networks.get(network)) {
throw new TypeError('Private key network mismatch');
}
if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
info.compressed = true;
} else if (buf.length === 1 + 32) {
info.compressed = false;
} else {
throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
}
info.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
return info;
};
/**
* Internal function to transform a BN buffer into a private key
*
* @param {Buffer} buf
* @param {Network|string=} network - a {@link Network} object, or a string with the network name
* @returns {object} an Object with keys: bn, network, and compressed
* @private
*/
PrivateKey._transformBNBuffer = function(buf, network) {
var info = {};
info.network = Networks.get(network) || Networks.defaultNetwork;
info.bn = BN.fromBuffer(buf);
info.compressed = false;
return info;
};
/**
* Internal function to transform a WIF string into a private key
*
* @param {string} buf - An WIF string
* @returns {Object} An object with keys: bn, network and compressed
* @private
*/
PrivateKey._transformWIF = function(str, network) {
return PrivateKey._transformBuffer(Base58Check.decode(str), network);
};
/**
* Instantiate a PrivateKey from a Buffer with the DER or WIF representation
*
* @param {Buffer} arg
* @param {Network} network
* @return {PrivateKey}
*/
PrivateKey.fromBuffer = function(arg, network) {
return new PrivateKey(arg, network);
};
/**
* Internal function to transform a JSON string on plain object into a private key
* return this.
*
* @param {string} json - A JSON string or plain object
* @returns {Object} An object with keys: bn, network and compressed
* @private
*/
PrivateKey._transformObject = function(json) {
var bn = new BN(json.bn, 'hex');
var network = Networks.get(json.network);
return {
bn: bn,
network: network,
compressed: json.compressed
};
};
/**
* Instantiate a PrivateKey from a WIF string
*
* @param {string} str - The WIF encoded private key string
* @returns {PrivateKey} A new valid instance of PrivateKey
*/
PrivateKey.fromString = PrivateKey.fromWIF = function(str) {
$.checkArgument(_.isString(str), 'First argument is expected to be a string.');
return new PrivateKey(str);
};
/**
* Instantiate a PrivateKey from a plain JavaScript object
*
* @param {Object} obj - The output from privateKey.toObject()
*/
PrivateKey.fromObject = function(obj) {
$.checkArgument(_.isObject(obj), 'First argument is expected to be an object.');
return new PrivateKey(obj);
};
/**
* Instantiate a PrivateKey from random bytes
*
* @param {string=} network - Either "livenet" or "testnet"
* @returns {PrivateKey} A new valid instance of PrivateKey
*/
PrivateKey.fromRandom = function(network) {
var bn = PrivateKey._getRandomBN();
return new PrivateKey(bn, network);
};
/**
* Check if there would be any errors when initializing a PrivateKey
*
* @param {string} data - The encoded data in various formats
* @param {string=} network - Either "livenet" or "testnet"
* @returns {null|Error} An error if exists
*/
PrivateKey.getValidationError = function(data, network) {
var error;
try {
/* jshint nonew: false */
new PrivateKey(data, network);
} catch (e) {
error = e;
}
return error;
};
/**
* Check if the parameters are valid
*
* @param {string} data - The encoded data in various formats
* @param {string=} network - Either "livenet" or "testnet"
* @returns {Boolean} If the private key is would be valid
*/
PrivateKey.isValid = function(data, network){
if (!data) {
return false;
}
return !PrivateKey.getValidationError(data, network);
};
/**
* Will output the PrivateKey encoded as hex string
*
* @returns {string}
*/
PrivateKey.prototype.toString = function() {
return this.toBuffer().toString('hex');
};
/**
* Will output the PrivateKey to a WIF string
*
* @returns {string} A WIP representation of the private key
*/
PrivateKey.prototype.toWIF = function() {
var network = this.network;
var compressed = this.compressed;
var buf;
if (compressed) {
buf = Buffer.concat([new Buffer([network.privatekey]),
this.bn.toBuffer({size: 32}),
new Buffer([0x01])]);
} else {
buf = Buffer.concat([new Buffer([network.privatekey]),
this.bn.toBuffer({size: 32})]);
}
return Base58Check.encode(buf);
};
/**
* Will return the private key as a BN instance
*
* @returns {BN} A BN instance of the private key
*/
PrivateKey.prototype.toBigNumber = function(){
return this.bn;
};
/**
* Will return the private key as a BN buffer
*
* @returns {Buffer} A buffer of the private key
*/
PrivateKey.prototype.toBuffer = function(){
// TODO: use `return this.bn.toBuffer({ size: 32 })` in v1.0.0
return this.bn.toBuffer();
};
/**
* WARNING: This method will not be officially supported until v1.0.0.
*
*
* Will return the private key as a BN buffer without leading zero padding
*
* @returns {Buffer} A buffer of the private key
*/
PrivateKey.prototype.toBufferNoPadding = function() {
return this.bn.toBuffer();
};
/**
* Will return the corresponding public key
*
* @returns {PublicKey} A public key generated from the private key
*/
PrivateKey.prototype.toPublicKey = function(){
if (!this._pubkey) {
this._pubkey = PublicKey.fromPrivateKey(this);
}
return this._pubkey;
};
/**
* Will return an address for the private key
* @param {Network=} network - optional parameter specifying
* the desired network for the address
*
* @returns {Address} An address generated from the private key
*/
PrivateKey.prototype.toAddress = function(network) {
var pubkey = this.toPublicKey();
return Address.fromPublicKey(pubkey, network || this.network);
};
/**
* @returns {Object} A plain object representation
*/
PrivateKey.prototype.toObject = PrivateKey.prototype.toJSON = function toObject() {
return {
bn: this.bn.toString('hex'),
compressed: this.compressed,
network: this.network.toString()
};
};
/**
* Will return a string formatted for the console
*
* @returns {string} Private key
*/
PrivateKey.prototype.inspect = function() {
var uncompressed = !this.compressed ? ', uncompressed' : '';
return '<PrivateKey: ' + this.toString() + ', network: ' + this.network + uncompressed + '>';
};
module.exports = PrivateKey;