1 | 'use strict';
|
2 |
|
3 | var _ = require('lodash');
|
4 | var Address = require('./address');
|
5 | var Base58Check = require('./encoding/base58check');
|
6 | var BN = require('./crypto/bn');
|
7 | var JSUtil = require('./util/js');
|
8 | var Networks = require('./networks');
|
9 | var Point = require('./crypto/point');
|
10 | var PublicKey = require('./publickey');
|
11 | var Random = require('./crypto/random');
|
12 | var $ = require('./util/preconditions');
|
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 | function PrivateKey(data, network) {
|
38 |
|
39 |
|
40 |
|
41 | if (!(this instanceof PrivateKey)) {
|
42 | return new PrivateKey(data, network);
|
43 | }
|
44 | if (data instanceof PrivateKey) {
|
45 | return data;
|
46 | }
|
47 |
|
48 | var info = this._classifyArguments(data, network);
|
49 |
|
50 |
|
51 | if (!info.bn || info.bn.cmp(new BN(0)) === 0){
|
52 | throw new TypeError('Number can not be equal to zero, undefined, null or false');
|
53 | }
|
54 | if (!info.bn.lt(Point.getN())) {
|
55 | throw new TypeError('Number must be less than N');
|
56 | }
|
57 | if (typeof(info.network) === 'undefined') {
|
58 | throw new TypeError('Must specify the network ("livenet" or "testnet")');
|
59 | }
|
60 |
|
61 | JSUtil.defineImmutable(this, {
|
62 | bn: info.bn,
|
63 | compressed: info.compressed,
|
64 | network: info.network
|
65 | });
|
66 |
|
67 | Object.defineProperty(this, 'publicKey', {
|
68 | configurable: false,
|
69 | enumerable: true,
|
70 | get: this.toPublicKey.bind(this)
|
71 | });
|
72 |
|
73 | return this;
|
74 |
|
75 | };
|
76 |
|
77 | /**
|
78 | * Internal helper to instantiate PrivateKey internal `info` object from
|
79 | * different kinds of arguments passed to the constructor.
|
80 | *
|
81 | * @param {*} data
|
82 | * @param {Network|string=} network - a {@link Network} object, or a string with the network name
|
83 | * @return {Object}
|
84 | */
|
85 | PrivateKey.prototype._classifyArguments = function(data, network) {
|
86 |
|
87 | var info = {
|
88 | compressed: true,
|
89 | network: network ? Networks.get(network) : Networks.defaultNetwork
|
90 | };
|
91 |
|
92 |
|
93 | if (_.isUndefined(data) || _.isNull(data)){
|
94 | info.bn = PrivateKey._getRandomBN();
|
95 | } else if (data instanceof BN) {
|
96 | info.bn = data;
|
97 | } else if (data instanceof Buffer || data instanceof Uint8Array) {
|
98 | info = PrivateKey._transformBuffer(data, network);
|
99 | } else if (data.bn && data.network){
|
100 | info = PrivateKey._transformObject(data);
|
101 | } else if (!network && Networks.get(data)) {
|
102 | info.bn = PrivateKey._getRandomBN();
|
103 | info.network = Networks.get(data);
|
104 | } else if (typeof(data) === 'string'){
|
105 | if (JSUtil.isHexa(data)) {
|
106 | info.bn = new BN(Buffer.from(data, 'hex'));
|
107 | } else {
|
108 | info = PrivateKey._transformWIF(data, network);
|
109 | }
|
110 | } else {
|
111 | throw new TypeError('First argument is an unrecognized data type.');
|
112 | }
|
113 | return info;
|
114 | };
|
115 |
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 | PrivateKey._getRandomBN = function(){
|
123 | var condition;
|
124 | var bn;
|
125 | do {
|
126 | var privbuf = Random.getRandomBuffer(32);
|
127 | bn = BN.fromBuffer(privbuf);
|
128 | condition = bn.lt(Point.getN());
|
129 | } while (!condition);
|
130 | return bn;
|
131 | };
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 | PrivateKey._transformBuffer = function(buf, network) {
|
142 |
|
143 | var info = {};
|
144 |
|
145 | if (buf.length === 32) {
|
146 | return PrivateKey._transformBNBuffer(buf, network);
|
147 | }
|
148 |
|
149 | info.network = Networks.get(buf[0], 'privatekey');
|
150 |
|
151 | if (!info.network) {
|
152 | throw new Error('Invalid network');
|
153 | }
|
154 |
|
155 | if (network && info.network !== Networks.get(network)) {
|
156 | throw new TypeError('Private key network mismatch');
|
157 | }
|
158 |
|
159 | if (buf.length === 1 + 32 + 1 && buf[1 + 32 + 1 - 1] === 1) {
|
160 | info.compressed = true;
|
161 | } else if (buf.length === 1 + 32) {
|
162 | info.compressed = false;
|
163 | } else {
|
164 | throw new Error('Length of buffer must be 33 (uncompressed) or 34 (compressed)');
|
165 | }
|
166 |
|
167 | info.bn = BN.fromBuffer(buf.slice(1, 32 + 1));
|
168 |
|
169 | return info;
|
170 | };
|
171 |
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 |
|
180 | PrivateKey._transformBNBuffer = function(buf, network) {
|
181 | var info = {};
|
182 | info.network = Networks.get(network) || Networks.defaultNetwork;
|
183 | info.bn = BN.fromBuffer(buf);
|
184 | info.compressed = false;
|
185 | return info;
|
186 | };
|
187 |
|
188 |
|
189 |
|
190 |
|
191 |
|
192 |
|
193 |
|
194 |
|
195 | PrivateKey._transformWIF = function(str, network) {
|
196 | return PrivateKey._transformBuffer(Base58Check.decode(str), network);
|
197 | };
|
198 |
|
199 |
|
200 |
|
201 |
|
202 |
|
203 |
|
204 |
|
205 |
|
206 | PrivateKey.fromBuffer = function(arg, network) {
|
207 | return new PrivateKey(arg, network);
|
208 | };
|
209 |
|
210 |
|
211 |
|
212 |
|
213 |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 | PrivateKey._transformObject = function(json) {
|
219 | var bn = new BN(json.bn, 'hex');
|
220 | var network = Networks.get(json.network);
|
221 | return {
|
222 | bn: bn,
|
223 | network: network,
|
224 | compressed: json.compressed
|
225 | };
|
226 | };
|
227 |
|
228 |
|
229 |
|
230 |
|
231 |
|
232 |
|
233 |
|
234 | PrivateKey.fromString = PrivateKey.fromWIF = function(str) {
|
235 | $.checkArgument(_.isString(str), 'First argument is expected to be a string.');
|
236 | return new PrivateKey(str);
|
237 | };
|
238 |
|
239 |
|
240 |
|
241 |
|
242 |
|
243 |
|
244 | PrivateKey.fromObject = function(obj) {
|
245 | $.checkArgument(_.isObject(obj), 'First argument is expected to be an object.');
|
246 | return new PrivateKey(obj);
|
247 | };
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | PrivateKey.fromRandom = function(network) {
|
256 | var bn = PrivateKey._getRandomBN();
|
257 | return new PrivateKey(bn, network);
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 |
|
266 |
|
267 |
|
268 | PrivateKey.getValidationError = function(data, network) {
|
269 | var error;
|
270 | try {
|
271 |
|
272 | new PrivateKey(data, network);
|
273 | } catch (e) {
|
274 | error = e;
|
275 | }
|
276 | return error;
|
277 | };
|
278 |
|
279 |
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 | PrivateKey.isValid = function(data, network){
|
287 | if (!data) {
|
288 | return false;
|
289 | }
|
290 | return !PrivateKey.getValidationError(data, network);
|
291 | };
|
292 |
|
293 |
|
294 |
|
295 |
|
296 |
|
297 |
|
298 | PrivateKey.prototype.toString = function() {
|
299 | return this.toBuffer().toString('hex');
|
300 | };
|
301 |
|
302 |
|
303 |
|
304 |
|
305 |
|
306 |
|
307 | PrivateKey.prototype.toWIF = function() {
|
308 | var network = this.network;
|
309 | var compressed = this.compressed;
|
310 |
|
311 | var buf;
|
312 | if (compressed) {
|
313 | buf = Buffer.concat([Buffer.from([network.privatekey]),
|
314 | this.bn.toBuffer({size: 32}),
|
315 | Buffer.from([0x01])]);
|
316 | } else {
|
317 | buf = Buffer.concat([Buffer.from([network.privatekey]),
|
318 | this.bn.toBuffer({size: 32})]);
|
319 | }
|
320 |
|
321 | return Base58Check.encode(buf);
|
322 | };
|
323 |
|
324 |
|
325 |
|
326 |
|
327 |
|
328 |
|
329 | PrivateKey.prototype.toBigNumber = function(){
|
330 | return this.bn;
|
331 | };
|
332 |
|
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | PrivateKey.prototype.toBuffer = function(){
|
339 | return this.bn.toBuffer({size: 32});
|
340 | };
|
341 |
|
342 |
|
343 |
|
344 |
|
345 |
|
346 |
|
347 |
|
348 |
|
349 |
|
350 | PrivateKey.prototype.toBufferNoPadding = function() {
|
351 | return this.bn.toBuffer();
|
352 | };
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 | PrivateKey.prototype.toPublicKey = function(){
|
360 | if (!this._pubkey) {
|
361 | this._pubkey = PublicKey.fromPrivateKey(this);
|
362 | }
|
363 | return this._pubkey;
|
364 | };
|
365 |
|
366 |
|
367 |
|
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 | PrivateKey.prototype.toAddress = function(network, type) {
|
375 | var pubkey = this.toPublicKey();
|
376 | return Address.fromPublicKey(pubkey, network || this.network, type);
|
377 | };
|
378 |
|
379 |
|
380 |
|
381 |
|
382 | PrivateKey.prototype.toObject = PrivateKey.prototype.toJSON = function toObject() {
|
383 | return {
|
384 | bn: this.bn.toString('hex'),
|
385 | compressed: this.compressed,
|
386 | network: this.network.toString()
|
387 | };
|
388 | };
|
389 |
|
390 |
|
391 |
|
392 |
|
393 |
|
394 |
|
395 | PrivateKey.prototype.inspect = function() {
|
396 | var uncompressed = !this.compressed ? ', uncompressed' : '';
|
397 | return '<PrivateKey: ' + this.toString() + ', network: ' + this.network + uncompressed + '>';
|
398 | };
|
399 |
|
400 | module.exports = PrivateKey;
|