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