1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 | import { Codes, VirtualSizes } from '@bitgo/unspents';
|
15 |
|
16 | const TransactionBuilder = require('./transactionBuilder');
|
17 | import * as bitcoin from '@bitgo/utxo-lib';
|
18 | const PendingApproval = require('./pendingapproval');
|
19 |
|
20 | import * as common from './common';
|
21 | import * as Bluebird from 'bluebird';
|
22 | const co = Bluebird.coroutine;
|
23 | import * as _ from 'lodash';
|
24 | import { hdPath, makeRandomKey, getNetwork } from './bitcoin';
|
25 | const request = require('superagent');
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | const Wallet = function(bitgo, wallet) {
|
31 | (this.bitgo as any) = bitgo;
|
32 | this.wallet = wallet;
|
33 | this.keychains = [];
|
34 |
|
35 | if (wallet.private) {
|
36 | this.keychains = wallet.private.keychains;
|
37 | }
|
38 | };
|
39 |
|
40 | Wallet.prototype.toJSON = function() {
|
41 | return this.wallet;
|
42 | };
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 | Wallet.prototype.id = function() {
|
49 | return this.wallet.id;
|
50 | };
|
51 |
|
52 |
|
53 |
|
54 |
|
55 |
|
56 | Wallet.prototype.label = function() {
|
57 | return this.wallet.label;
|
58 | };
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 | Wallet.prototype.balance = function() {
|
65 | return this.wallet.balance;
|
66 | };
|
67 |
|
68 |
|
69 |
|
70 |
|
71 |
|
72 |
|
73 | Wallet.prototype.spendableBalance = function() {
|
74 | return this.wallet.spendableBalance;
|
75 | };
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | Wallet.prototype.confirmedBalance = function() {
|
82 | return this.wallet.confirmedBalance;
|
83 | };
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 | Wallet.prototype.canSendInstant = function() {
|
91 | return this.wallet && this.wallet.canSendInstant;
|
92 | };
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 | Wallet.prototype.instantBalance = function() {
|
100 | if (!this.canSendInstant()) {
|
101 | throw new Error('not an instant wallet');
|
102 | }
|
103 | return this.wallet.instantBalance;
|
104 | };
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 | Wallet.prototype.unconfirmedSends = function() {
|
111 | return this.wallet.unconfirmedSends;
|
112 | };
|
113 |
|
114 |
|
115 |
|
116 |
|
117 |
|
118 | Wallet.prototype.unconfirmedReceives = function() {
|
119 | return this.wallet.unconfirmedReceives;
|
120 | };
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 | Wallet.prototype.type = function() {
|
127 | return this.wallet.type;
|
128 | };
|
129 |
|
130 | Wallet.prototype.url = function(extra) {
|
131 | extra = extra || '';
|
132 | return this.bitgo.url('/wallet/' + this.id() + extra);
|
133 | };
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 | Wallet.prototype.pendingApprovals = function() {
|
140 | const self = this;
|
141 | return this.wallet.pendingApprovals.map(function(p) {
|
142 | return new PendingApproval(self.bitgo, p, self);
|
143 | });
|
144 | };
|
145 |
|
146 |
|
147 |
|
148 |
|
149 |
|
150 | Wallet.prototype.approvalsRequired = function() {
|
151 | return this.wallet.approvalsRequired || 1;
|
152 | };
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 | Wallet.prototype.get = function(params, callback): Bluebird<any> {
|
159 | params = params || {};
|
160 | common.validateParams(params, [], [], callback);
|
161 |
|
162 | const self = this;
|
163 |
|
164 | return this.bitgo.get(this.url())
|
165 | .result()
|
166 | .then(function(res) {
|
167 | self.wallet = res;
|
168 | return self;
|
169 | })
|
170 | .nodeify(callback);
|
171 | };
|
172 |
|
173 |
|
174 |
|
175 |
|
176 |
|
177 |
|
178 |
|
179 | Wallet.prototype.updateApprovalsRequired = function(params, callback): Bluebird<any> {
|
180 | params = params || {};
|
181 | common.validateParams(params, [], [], callback);
|
182 | if (params.approvalsRequired === undefined ||
|
183 | !_.isInteger(params.approvalsRequired) ||
|
184 | params.approvalsRequired < 1
|
185 | ) {
|
186 | throw new Error('invalid approvalsRequired: must be a nonzero positive number');
|
187 | }
|
188 |
|
189 | const self = this;
|
190 | const currentApprovalsRequired = this.approvalsRequired();
|
191 | if (currentApprovalsRequired === params.approvalsRequired) {
|
192 |
|
193 | return Bluebird.try(function() {
|
194 | return self.wallet;
|
195 | })
|
196 | .nodeify(callback);
|
197 | }
|
198 |
|
199 | return this.bitgo.put(this.url())
|
200 | .send(params)
|
201 | .result()
|
202 | .nodeify(callback);
|
203 | };
|
204 |
|
205 |
|
206 |
|
207 |
|
208 | Wallet.prototype.getChangeChain = function(params) {
|
209 | let useSegwitChange = !!this.bitgo.getConstants().enableSegwit;
|
210 | if (!_.isUndefined(params.segwitChange)) {
|
211 | if (!_.isBoolean(params.segwitChange)) {
|
212 | throw new Error('segwitChange must be a boolean');
|
213 | }
|
214 |
|
215 |
|
216 | useSegwitChange = this.bitgo.getConstants().enableSegwit && params.segwitChange;
|
217 | }
|
218 | return useSegwitChange ? Codes.internal.p2shP2wsh : Codes.internal.p2sh;
|
219 | };
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 | Wallet.prototype.createAddress = function(params, callback) {
|
226 | const self = this;
|
227 | params = params || {};
|
228 | common.validateParams(params, [], [], callback);
|
229 | if (this.type() === 'safe') {
|
230 | throw new Error('You are using a legacy wallet that cannot create a new address');
|
231 | }
|
232 |
|
233 |
|
234 | const shouldValidate = params.validate !== undefined ? params.validate : this.bitgo.getValidate();
|
235 |
|
236 | const allowExisting = params.allowExisting;
|
237 | if (typeof allowExisting !== 'boolean') {
|
238 | params.allowExisting = (allowExisting === 'true');
|
239 | }
|
240 |
|
241 | const isSegwit = this.bitgo.getConstants().enableSegwit;
|
242 | const defaultChain = isSegwit ? Codes.external.p2shP2wsh : Codes.external.p2sh;
|
243 |
|
244 | let chain = params.chain;
|
245 | if (chain === null || chain === undefined) {
|
246 | chain = defaultChain;
|
247 | }
|
248 | return this.bitgo.post(this.url('/address/' + chain))
|
249 | .send(params)
|
250 | .result()
|
251 | .then(function(addr) {
|
252 | if (shouldValidate) {
|
253 | self.validateAddress(addr);
|
254 | }
|
255 | return addr;
|
256 | })
|
257 | .nodeify(callback);
|
258 | };
|
259 |
|
260 |
|
261 |
|
262 |
|
263 |
|
264 |
|
265 | Wallet.prototype.generateAddress = function({ segwit, path, keychains, threshold }) {
|
266 | const isSegwit = !!segwit;
|
267 | let signatureThreshold = 2;
|
268 | if (_.isInteger(threshold)) {
|
269 | signatureThreshold = threshold;
|
270 | if (signatureThreshold <= 0) {
|
271 | throw new Error('threshold has to be positive');
|
272 | }
|
273 | }
|
274 |
|
275 | const pathRegex = /^\/1?[01]\/\d+$/;
|
276 | if (!path.match(pathRegex)) {
|
277 | throw new Error('unsupported path: ' + path);
|
278 | }
|
279 |
|
280 | let rootKeys = this.keychains;
|
281 | if (Array.isArray(keychains)) {
|
282 | rootKeys = keychains;
|
283 | }
|
284 |
|
285 | const network = common.Environments[this.bitgo.getEnv()].network;
|
286 |
|
287 | const derivedKeys = rootKeys.map(function(k) {
|
288 | const hdnode = bitcoin.HDNode.fromBase58(k.xpub);
|
289 | let derivationPath = k.path + path;
|
290 | if (k.walletSubPath) {
|
291 |
|
292 | derivationPath = k.path + k.walletSubPath + path;
|
293 | }
|
294 | if (!derivationPath.startsWith('m')) {
|
295 |
|
296 | derivationPath = `m/${derivationPath}`;
|
297 | }
|
298 | return hdPath(hdnode).deriveKey(derivationPath).getPublicKeyBuffer();
|
299 | });
|
300 |
|
301 | const pathComponents = path.split('/');
|
302 | const normalizedPathComponents = _.map(pathComponents, (component) => {
|
303 | if (component && component.length > 0) {
|
304 | return parseInt(component, 10);
|
305 | }
|
306 | });
|
307 | const pathDetails = _.filter(normalizedPathComponents, _.isInteger);
|
308 |
|
309 | const addressDetails: any = {
|
310 | chainPath: path,
|
311 | path: path,
|
312 | chain: pathDetails[0],
|
313 | index: pathDetails[1],
|
314 | wallet: this.id()
|
315 | };
|
316 |
|
317 |
|
318 | const inputScript = bitcoin.script.multisig.output.encode(signatureThreshold, derivedKeys);
|
319 | const inputScriptHash = bitcoin.crypto.hash160(inputScript);
|
320 | let outputScript = bitcoin.script.scriptHash.output.encode(inputScriptHash);
|
321 | addressDetails.redeemScript = inputScript.toString('hex');
|
322 |
|
323 | if (isSegwit) {
|
324 | const witnessScriptHash = bitcoin.crypto.sha256(inputScript);
|
325 | const redeemScript = bitcoin.script.witnessScriptHash.output.encode(witnessScriptHash);
|
326 | const redeemScriptHash = bitcoin.crypto.hash160(redeemScript);
|
327 | outputScript = bitcoin.script.scriptHash.output.encode(redeemScriptHash);
|
328 | addressDetails.witnessScript = inputScript.toString('hex');
|
329 | addressDetails.redeemScript = redeemScript.toString('hex');
|
330 | }
|
331 |
|
332 | addressDetails.outputScript = outputScript.toString('hex');
|
333 | addressDetails.address = bitcoin.address.fromOutputScript(outputScript, getNetwork(network));
|
334 |
|
335 | return addressDetails;
|
336 | };
|
337 |
|
338 |
|
339 |
|
340 |
|
341 |
|
342 | Wallet.prototype.validateAddress = function(params) {
|
343 | common.validateParams(params, ['address', 'path'], []);
|
344 | const isSegwit = !!params.witnessScript && params.witnessScript.length > 0;
|
345 |
|
346 | const generatedAddress = this.generateAddress({ path: params.path, segwit: isSegwit });
|
347 | if (generatedAddress.address !== params.address) {
|
348 | throw new Error('address validation failure: ' + params.address + ' vs. ' + generatedAddress.address);
|
349 | }
|
350 | };
|
351 |
|
352 |
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 | Wallet.prototype.addresses = function(params, callback) {
|
359 | params = params || {};
|
360 | common.validateParams(params, [], [], callback);
|
361 |
|
362 | const query: any = {};
|
363 | if (params.details) {
|
364 | query.details = 1;
|
365 | }
|
366 |
|
367 | const chain = params.chain;
|
368 | if (chain !== null && chain !== undefined) {
|
369 | if (Array.isArray(chain)) {
|
370 | query.chain = _.uniq(_.filter(chain, _.isInteger));
|
371 | } else {
|
372 | if (chain !== 0 && chain !== 1) {
|
373 | throw new Error('invalid chain argument, expecting 0 or 1');
|
374 | }
|
375 | query.chain = chain;
|
376 | }
|
377 | }
|
378 | if (params.limit) {
|
379 | if (!_.isInteger(params.limit)) {
|
380 | throw new Error('invalid limit argument, expecting number');
|
381 | }
|
382 | query.limit = params.limit;
|
383 | }
|
384 | if (params.skip) {
|
385 | if (!_.isInteger(params.skip)) {
|
386 | throw new Error('invalid skip argument, expecting number');
|
387 | }
|
388 | query.skip = params.skip;
|
389 | }
|
390 | if (params.sort) {
|
391 | if (!_.isNumber(params.sort)) {
|
392 | throw new Error('invalid sort argument, expecting number');
|
393 | }
|
394 | query.sort = params.sort;
|
395 | }
|
396 |
|
397 | const url = this.url('/addresses');
|
398 | return this.bitgo.get(url)
|
399 | .query(query)
|
400 | .result()
|
401 | .nodeify(callback);
|
402 | };
|
403 |
|
404 | Wallet.prototype.stats = function(params, callback) {
|
405 | params = params || {};
|
406 | common.validateParams(params, [], [], callback);
|
407 | const args: string[] = [];
|
408 | if (params.limit) {
|
409 | if (!_.isInteger(params.limit)) {
|
410 | throw new Error('invalid limit argument, expecting number');
|
411 | }
|
412 | args.push('limit=' + params.limit);
|
413 | }
|
414 | let query = '';
|
415 | if (args.length) {
|
416 | query = '?' + args.join('&');
|
417 | }
|
418 |
|
419 | const url = this.url('/stats' + query);
|
420 |
|
421 | return this.bitgo.get(url)
|
422 | .result()
|
423 | .nodeify(callback);
|
424 | };
|
425 |
|
426 |
|
427 |
|
428 |
|
429 |
|
430 |
|
431 | Wallet.prototype.refresh = function(params, callback) {
|
432 | return co(function *() {
|
433 |
|
434 | const query = _.extend({}, _.pick(params, ['gpk']));
|
435 | const res = yield this.bitgo.get(this.url()).query(query).result();
|
436 | this.wallet = res;
|
437 | return this;
|
438 | }).call(this).asCallback(callback);
|
439 | };
|
440 |
|
441 |
|
442 |
|
443 |
|
444 |
|
445 |
|
446 |
|
447 |
|
448 | Wallet.prototype.address = function(params, callback) {
|
449 | params = params || {};
|
450 | common.validateParams(params, ['address'], [], callback);
|
451 |
|
452 | const url = this.url('/addresses/' + params.address);
|
453 |
|
454 | return this.bitgo.get(url)
|
455 | .result()
|
456 | .nodeify(callback);
|
457 | };
|
458 |
|
459 |
|
460 |
|
461 |
|
462 |
|
463 | Wallet.prototype.freeze = function(params, callback) {
|
464 | params = params || {};
|
465 | common.validateParams(params, [], [], callback);
|
466 |
|
467 | if (params.duration) {
|
468 | if (!_.isNumber(params.duration)) {
|
469 | throw new Error('invalid duration - should be number of seconds');
|
470 | }
|
471 | }
|
472 |
|
473 | return this.bitgo.post(this.url('/freeze'))
|
474 | .send(params)
|
475 | .result()
|
476 | .nodeify(callback);
|
477 | };
|
478 |
|
479 |
|
480 |
|
481 |
|
482 |
|
483 | Wallet.prototype.delete = function(params, callback) {
|
484 | params = params || {};
|
485 | common.validateParams(params, [], [], callback);
|
486 |
|
487 | return this.bitgo.del(this.url())
|
488 | .result()
|
489 | .nodeify(callback);
|
490 | };
|
491 |
|
492 |
|
493 |
|
494 |
|
495 |
|
496 | Wallet.prototype.labels = function(params, callback) {
|
497 | params = params || {};
|
498 | common.validateParams(params, [], [], callback);
|
499 |
|
500 | const url = this.bitgo.url('/labels/' + this.id());
|
501 |
|
502 | return this.bitgo.get(url)
|
503 | .result('labels')
|
504 | .nodeify(callback);
|
505 | };
|
506 |
|
507 |
|
508 |
|
509 |
|
510 |
|
511 |
|
512 |
|
513 |
|
514 | Wallet.prototype.setWalletName = function(params, callback) {
|
515 | params = params || {};
|
516 | common.validateParams(params, ['label'], [], callback);
|
517 |
|
518 | const url = this.bitgo.url('/wallet/' + this.id());
|
519 | return this.bitgo.put(url)
|
520 | .send({ label: params.label })
|
521 | .result()
|
522 | .nodeify(callback);
|
523 | };
|
524 |
|
525 |
|
526 |
|
527 |
|
528 |
|
529 | Wallet.prototype.setLabel = function(params, callback) {
|
530 | params = params || {};
|
531 | common.validateParams(params, ['address', 'label'], [], callback);
|
532 |
|
533 | const self = this;
|
534 |
|
535 | if (!self.bitgo.verifyAddress({ address: params.address })) {
|
536 | throw new Error('Invalid bitcoin address: ' + params.address);
|
537 | }
|
538 |
|
539 | const url = this.bitgo.url('/labels/' + this.id() + '/' + params.address);
|
540 |
|
541 | return this.bitgo.put(url)
|
542 | .send({ label: params.label })
|
543 | .result()
|
544 | .nodeify(callback);
|
545 | };
|
546 |
|
547 |
|
548 |
|
549 |
|
550 |
|
551 | Wallet.prototype.deleteLabel = function(params, callback) {
|
552 | params = params || {};
|
553 | common.validateParams(params, ['address'], [], callback);
|
554 |
|
555 | const self = this;
|
556 |
|
557 | if (!self.bitgo.verifyAddress({ address: params.address })) {
|
558 | throw new Error('Invalid bitcoin address: ' + params.address);
|
559 | }
|
560 |
|
561 | const url = this.bitgo.url('/labels/' + this.id() + '/' + params.address);
|
562 |
|
563 | return this.bitgo.del(url)
|
564 | .result()
|
565 | .nodeify(callback);
|
566 | };
|
567 |
|
568 |
|
569 |
|
570 |
|
571 |
|
572 |
|
573 |
|
574 |
|
575 |
|
576 |
|
577 |
|
578 |
|
579 | Wallet.prototype.unspents = function(params, callback) {
|
580 | params = params || {};
|
581 | common.validateParams(params, [], [], callback);
|
582 |
|
583 | const allUnspents: any[] = [];
|
584 | const self = this;
|
585 |
|
586 | const getUnspentsBatch = function(skip, limit?) {
|
587 |
|
588 | const queryObject = _.cloneDeep(params);
|
589 | if (skip > 0) {
|
590 | queryObject.skip = skip;
|
591 | }
|
592 | if (limit && limit > 0) {
|
593 | queryObject.limit = limit;
|
594 | }
|
595 |
|
596 | return self.unspentsPaged(queryObject)
|
597 | .then(function(result) {
|
598 |
|
599 |
|
600 | for (let i = 0; i < result.unspents.length; i++) {
|
601 | const unspent = result.unspents[i];
|
602 | allUnspents.push(unspent);
|
603 | }
|
604 |
|
605 |
|
606 |
|
607 | if (allUnspents.length >= params.limit) {
|
608 | return allUnspents;
|
609 | }
|
610 |
|
611 | const totalUnspentCount = result.total;
|
612 |
|
613 | if (!params.target && totalUnspentCount && totalUnspentCount > allUnspents.length) {
|
614 |
|
615 |
|
616 | const newSkip = skip + result.count;
|
617 | let newLimit: number | undefined;
|
618 | if (limit > 0) {
|
619 |
|
620 | newLimit = limit - allUnspents.length;
|
621 | }
|
622 | return getUnspentsBatch(newSkip, newLimit);
|
623 | }
|
624 |
|
625 | return allUnspents;
|
626 | });
|
627 | };
|
628 |
|
629 | return getUnspentsBatch(0, params.limit)
|
630 | .nodeify(callback);
|
631 | };
|
632 |
|
633 |
|
634 |
|
635 |
|
636 |
|
637 |
|
638 |
|
639 |
|
640 |
|
641 |
|
642 |
|
643 |
|
644 |
|
645 |
|
646 |
|
647 |
|
648 |
|
649 | Wallet.prototype.unspentsPaged = function(params, callback) {
|
650 | params = params || {};
|
651 | common.validateParams(params, [], [], callback);
|
652 |
|
653 | if (!_.isUndefined(params.limit) && !_.isInteger(params.limit)) {
|
654 | throw new Error('invalid limit - should be number');
|
655 | }
|
656 | if (!_.isUndefined(params.skip) && !_.isInteger(params.skip)) {
|
657 | throw new Error('invalid skip - should be number');
|
658 | }
|
659 | if (!_.isUndefined(params.minConfirms) && !_.isInteger(params.minConfirms)) {
|
660 | throw new Error('invalid minConfirms - should be number');
|
661 | }
|
662 | if (!_.isUndefined(params.target) && !_.isNumber(params.target)) {
|
663 | throw new Error('invalid target - should be number');
|
664 | }
|
665 | if (!_.isUndefined(params.instant) && !_.isBoolean(params.instant)) {
|
666 | throw new Error('invalid instant flag - should be boolean');
|
667 | }
|
668 | if (!_.isUndefined(params.segwit) && !_.isBoolean(params.segwit)) {
|
669 | throw new Error('invalid segwit flag - should be boolean');
|
670 | }
|
671 | if (!_.isUndefined(params.targetWalletUnspents) && !_.isInteger(params.targetWalletUnspents)) {
|
672 | throw new Error('invalid targetWalletUnspents flag - should be number');
|
673 | }
|
674 | if (!_.isUndefined(params.minSize) && !_.isNumber(params.minSize)) {
|
675 | throw new Error('invalid argument: minSize must be a number');
|
676 | }
|
677 | if (!_.isUndefined(params.instant) && !_.isUndefined(params.minConfirms)) {
|
678 | throw new Error('only one of instant and minConfirms may be defined');
|
679 | }
|
680 | if (!_.isUndefined(params.targetWalletUnspents) && _.isUndefined(params.target)) {
|
681 | throw new Error('targetWalletUnspents can only be specified in conjunction with a target');
|
682 | }
|
683 | if (!_.isUndefined(params.allowLedgerSegwit) && !_.isBoolean(params.allowLedgerSegwit)) {
|
684 | throw new Error('invalid argument: allowLedgerSegwit must be a boolean');
|
685 | }
|
686 |
|
687 | const queryObject = _.cloneDeep(params);
|
688 |
|
689 | if (!_.isUndefined(params.target)) {
|
690 |
|
691 | delete queryObject.skip;
|
692 | delete queryObject.limit;
|
693 | }
|
694 |
|
695 | queryObject.segwit = true;
|
696 | if (!_.isUndefined(params.segwit)) {
|
697 | queryObject.segwit = params.segwit;
|
698 | }
|
699 |
|
700 | if (!_.isUndefined(params.allowLedgerSegwit)) {
|
701 | queryObject.allowLedgerSegwit = params.allowLedgerSegwit;
|
702 | }
|
703 |
|
704 | return this.bitgo.get(this.url('/unspents'))
|
705 | .query(queryObject)
|
706 | .result()
|
707 | .nodeify(callback);
|
708 | };
|
709 |
|
710 |
|
711 |
|
712 |
|
713 |
|
714 |
|
715 | Wallet.prototype.transactions = function(params, callback) {
|
716 | params = params || {};
|
717 | common.validateParams(params, [], [], callback);
|
718 |
|
719 | const args: string[] = [];
|
720 | if (params.limit) {
|
721 | if (!_.isInteger(params.limit)) {
|
722 | throw new Error('invalid limit argument, expecting number');
|
723 | }
|
724 | args.push('limit=' + params.limit);
|
725 | }
|
726 | if (params.skip) {
|
727 | if (!_.isInteger(params.skip)) {
|
728 | throw new Error('invalid skip argument, expecting number');
|
729 | }
|
730 | args.push('skip=' + params.skip);
|
731 | }
|
732 | if (params.minHeight) {
|
733 | if (!_.isInteger(params.minHeight)) {
|
734 | throw new Error('invalid minHeight argument, expecting number');
|
735 | }
|
736 | args.push('minHeight=' + params.minHeight);
|
737 | }
|
738 | if (params.maxHeight) {
|
739 | if (!_.isInteger(params.maxHeight) || params.maxHeight < 0) {
|
740 | throw new Error('invalid maxHeight argument, expecting positive integer');
|
741 | }
|
742 | args.push('maxHeight=' + params.maxHeight);
|
743 | }
|
744 | if (params.minConfirms) {
|
745 | if (!_.isInteger(params.minConfirms) || params.minConfirms < 0) {
|
746 | throw new Error('invalid minConfirms argument, expecting positive integer');
|
747 | }
|
748 | args.push('minConfirms=' + params.minConfirms);
|
749 | }
|
750 | if (!_.isUndefined(params.compact)) {
|
751 | if (!_.isBoolean(params.compact)) {
|
752 | throw new Error('invalid compact argument, expecting boolean');
|
753 | }
|
754 | args.push('compact=' + params.compact);
|
755 | }
|
756 | let query = '';
|
757 | if (args.length) {
|
758 | query = '?' + args.join('&');
|
759 | }
|
760 |
|
761 | const url = this.url('/tx' + query);
|
762 |
|
763 | return this.bitgo.get(url)
|
764 | .result()
|
765 | .nodeify(callback);
|
766 | };
|
767 |
|
768 |
|
769 |
|
770 |
|
771 | Wallet.prototype.getTransaction = function(params, callback) {
|
772 | params = params || {};
|
773 | common.validateParams(params, ['id'], [], callback);
|
774 |
|
775 | const url = this.url('/tx/' + params.id);
|
776 |
|
777 | return this.bitgo.get(url)
|
778 | .result()
|
779 | .nodeify(callback);
|
780 | };
|
781 |
|
782 |
|
783 |
|
784 |
|
785 |
|
786 |
|
787 |
|
788 |
|
789 | Wallet.prototype.pollForTransaction = function(params, callback) {
|
790 | const self = this;
|
791 | params = params || {};
|
792 | common.validateParams(params, ['id'], [], callback);
|
793 | if (params.delay && !_.isNumber(params.delay)) {
|
794 | throw new Error('invalid delay parameter');
|
795 | }
|
796 | if (params.timeout && !_.isNumber(params.timeout)) {
|
797 | throw new Error('invalid timeout parameter');
|
798 | }
|
799 | params.delay = params.delay || 1000;
|
800 | params.timeout = params.timeout || 10000;
|
801 |
|
802 | const start = new Date();
|
803 |
|
804 | const doNextPoll = function() {
|
805 | return self.getTransaction(params)
|
806 | .then(function(res) {
|
807 | return res;
|
808 | })
|
809 | .catch(function(err) {
|
810 | if (err.status !== 404 || new Date().valueOf() - start.valueOf() > params.timeout) {
|
811 | throw err;
|
812 | }
|
813 | return Bluebird.delay(params.delay)
|
814 | .then(function() {
|
815 | return doNextPoll();
|
816 | });
|
817 | });
|
818 | };
|
819 |
|
820 | return doNextPoll();
|
821 | };
|
822 |
|
823 |
|
824 |
|
825 |
|
826 | Wallet.prototype.getWalletTransactionBySequenceId = function(params, callback) {
|
827 | params = params || {};
|
828 | common.validateParams(params, ['sequenceId'], [], callback);
|
829 |
|
830 | const url = this.url('/tx/sequence/' + params.sequenceId);
|
831 |
|
832 | return this.bitgo.get(url)
|
833 | .result()
|
834 | .nodeify(callback);
|
835 | };
|
836 |
|
837 |
|
838 |
|
839 |
|
840 |
|
841 |
|
842 | Wallet.prototype.getEncryptedUserKeychain = function(params, callback) {
|
843 | return co(function *() {
|
844 | params = params || {};
|
845 | common.validateParams(params, [], [], callback);
|
846 | const self = this;
|
847 |
|
848 | const tryKeyChain = co(function *(index) {
|
849 | if (!self.keychains || index >= self.keychains.length) {
|
850 | const error: any = new Error('No encrypted keychains on this wallet.');
|
851 | error.code = 'no_encrypted_keychain_on_wallet';
|
852 | throw error;
|
853 | }
|
854 |
|
855 | const params = { xpub: self.keychains[index].xpub };
|
856 |
|
857 | const keychain = yield self.bitgo.keychains().get(params);
|
858 |
|
859 | keychain.walletSubPath = self.keychains[index].path;
|
860 | if (keychain.encryptedXprv) {
|
861 | return keychain;
|
862 | }
|
863 | return tryKeyChain(index + 1);
|
864 | });
|
865 |
|
866 | return tryKeyChain(0);
|
867 | }).call(this).asCallback(callback);
|
868 | };
|
869 |
|
870 |
|
871 |
|
872 |
|
873 |
|
874 |
|
875 |
|
876 |
|
877 |
|
878 |
|
879 |
|
880 |
|
881 |
|
882 |
|
883 |
|
884 | Wallet.prototype.createTransaction = function(params, callback) {
|
885 | params = _.extend({}, params);
|
886 | common.validateParams(params, [], [], callback);
|
887 |
|
888 | if ((!_.isNumber(params.fee) && !_.isUndefined(params.fee)) ||
|
889 | (!_.isNumber(params.feeRate) && !_.isUndefined(params.feeRate)) ||
|
890 | (!_.isNumber(params.minConfirms) && !_.isUndefined(params.minConfirms)) ||
|
891 | (!_.isBoolean(params.forceChangeAtEnd) && !_.isUndefined(params.forceChangeAtEnd)) ||
|
892 | (!_.isString(params.changeAddress) && !_.isUndefined(params.changeAddress)) ||
|
893 | (!_.isBoolean(params.validate) && !_.isUndefined(params.validate)) ||
|
894 | (!_.isBoolean(params.instant) && !_.isUndefined(params.instant))) {
|
895 | throw new Error('invalid argument');
|
896 | }
|
897 |
|
898 | if (!_.isObject(params.recipients)) {
|
899 | throw new Error('expecting recipients object');
|
900 | }
|
901 |
|
902 | params.validate = params.validate !== undefined ? params.validate : this.bitgo.getValidate();
|
903 | params.wallet = this;
|
904 |
|
905 | return TransactionBuilder.createTransaction(params)
|
906 | .nodeify(callback);
|
907 | };
|
908 |
|
909 |
|
910 |
|
911 |
|
912 |
|
913 |
|
914 |
|
915 |
|
916 |
|
917 |
|
918 |
|
919 |
|
920 |
|
921 |
|
922 |
|
923 | Wallet.prototype.signTransaction = function(params, callback) {
|
924 | params = _.extend({}, params);
|
925 | common.validateParams(params, ['transactionHex'], [], callback);
|
926 |
|
927 | if (!Array.isArray(params.unspents)) {
|
928 | throw new Error('expecting the unspents array');
|
929 | }
|
930 |
|
931 | if ((!_.isObject(params.keychain) || !params.keychain.xprv) && !_.isString(params.signingKey)) {
|
932 |
|
933 | const error: any = new Error('expecting keychain object with xprv or signingKey WIF');
|
934 | error.code = 'missing_keychain_or_signingKey';
|
935 | throw error;
|
936 | }
|
937 |
|
938 | params.validate = params.validate !== undefined ? params.validate : this.bitgo.getValidate();
|
939 | params.bitgo = this.bitgo;
|
940 | return TransactionBuilder.signTransaction(params)
|
941 | .then(function(result) {
|
942 | return {
|
943 | tx: result.transactionHex
|
944 | };
|
945 | })
|
946 | .nodeify(callback);
|
947 | };
|
948 |
|
949 |
|
950 |
|
951 |
|
952 |
|
953 |
|
954 |
|
955 |
|
956 |
|
957 | Wallet.prototype.sendTransaction = function(params, callback) {
|
958 | params = params || {};
|
959 | common.validateParams(params, ['tx'], ['message', 'otp'], callback);
|
960 |
|
961 | return this.bitgo.post(this.bitgo.url('/tx/send'))
|
962 | .send(params)
|
963 | .result()
|
964 | .then(function(body) {
|
965 | if (body.pendingApproval) {
|
966 | return _.extend(body, { status: 'pendingApproval' });
|
967 | }
|
968 |
|
969 | if (body.otp) {
|
970 | return _.extend(body, { status: 'otp' });
|
971 | }
|
972 |
|
973 | return {
|
974 | status: 'accepted',
|
975 | tx: body.transaction,
|
976 | hash: body.transactionHash,
|
977 | instant: body.instant,
|
978 | instantId: body.instantId
|
979 | };
|
980 | })
|
981 | .nodeify(callback);
|
982 | };
|
983 |
|
984 |
|
985 |
|
986 |
|
987 |
|
988 |
|
989 |
|
990 |
|
991 | Wallet.prototype.createShare = function(params, callback) {
|
992 | params = params || {};
|
993 | common.validateParams(params, ['user', 'permissions'], [], callback);
|
994 |
|
995 | if (params.keychain && !_.isEmpty(params.keychain)) {
|
996 | if (!params.keychain.xpub || !params.keychain.encryptedXprv || !params.keychain.fromPubKey || !params.keychain.toPubKey || !params.keychain.path) {
|
997 | throw new Error('requires keychain parameters - xpub, encryptedXprv, fromPubKey, toPubKey, path');
|
998 | }
|
999 | }
|
1000 |
|
1001 | return this.bitgo.post(this.url('/share'))
|
1002 | .send(params)
|
1003 | .result()
|
1004 | .nodeify(callback);
|
1005 | };
|
1006 |
|
1007 |
|
1008 |
|
1009 |
|
1010 |
|
1011 |
|
1012 |
|
1013 |
|
1014 |
|
1015 | Wallet.prototype.createInvite = function(params, callback) {
|
1016 | params = params || {};
|
1017 | common.validateParams(params, ['email', 'permissions'], ['message'], callback);
|
1018 |
|
1019 | const options: any = {
|
1020 | toEmail: params.email,
|
1021 | permissions: params.permissions
|
1022 | };
|
1023 |
|
1024 | if (params.message) {
|
1025 | options.message = params.message;
|
1026 | }
|
1027 |
|
1028 | return this.bitgo.post(this.url('/invite'))
|
1029 | .send(options)
|
1030 | .result()
|
1031 | .nodeify(callback);
|
1032 | };
|
1033 |
|
1034 |
|
1035 |
|
1036 |
|
1037 |
|
1038 |
|
1039 |
|
1040 |
|
1041 |
|
1042 |
|
1043 | Wallet.prototype.confirmInviteAndShareWallet = function(params, callback) {
|
1044 | params = params || {};
|
1045 | common.validateParams(params, ['walletInviteId'], ['walletPassphrase'], callback);
|
1046 |
|
1047 | const self = this;
|
1048 | return this.bitgo.wallets().listInvites()
|
1049 | .then(function(invites) {
|
1050 | const outgoing = invites.outgoing;
|
1051 | const invite = _.find(outgoing, function(out) {
|
1052 | return out.id === params.walletInviteId;
|
1053 | });
|
1054 | if (!invite) {
|
1055 | throw new Error('wallet invite not found');
|
1056 | }
|
1057 |
|
1058 | const options = {
|
1059 | email: invite.toEmail,
|
1060 | permissions: invite.permissions,
|
1061 | message: invite.message,
|
1062 | walletPassphrase: params.walletPassphrase
|
1063 | };
|
1064 |
|
1065 | return self.shareWallet(options);
|
1066 | })
|
1067 | .then(function() {
|
1068 | return this.bitgo.put(this.bitgo.url('/walletinvite/' + params.walletInviteId));
|
1069 | })
|
1070 | .nodeify(callback);
|
1071 | };
|
1072 |
|
1073 |
|
1074 |
|
1075 |
|
1076 |
|
1077 |
|
1078 |
|
1079 |
|
1080 |
|
1081 |
|
1082 |
|
1083 |
|
1084 |
|
1085 |
|
1086 |
|
1087 |
|
1088 |
|
1089 |
|
1090 |
|
1091 | Wallet.prototype.sendCoins = function(params, callback) {
|
1092 | params = params || {};
|
1093 | common.validateParams(params, ['address'], ['message'], callback);
|
1094 |
|
1095 | if (!_.isNumber(params.amount)) {
|
1096 | throw new Error('invalid argument for amount - number expected');
|
1097 | }
|
1098 |
|
1099 | params.recipients = {};
|
1100 | params.recipients[params.address] = params.amount;
|
1101 |
|
1102 | return this.sendMany(params)
|
1103 | .nodeify(callback);
|
1104 | };
|
1105 |
|
1106 |
|
1107 |
|
1108 |
|
1109 |
|
1110 |
|
1111 |
|
1112 |
|
1113 |
|
1114 |
|
1115 |
|
1116 |
|
1117 |
|
1118 |
|
1119 |
|
1120 |
|
1121 |
|
1122 | Wallet.prototype.sendMany = function(params, callback) {
|
1123 | params = params || {};
|
1124 | common.validateParams(params, [], ['message', 'otp'], callback);
|
1125 | const self = this;
|
1126 |
|
1127 | if (!_.isObject(params.recipients)) {
|
1128 | throw new Error('expecting recipients object');
|
1129 | }
|
1130 |
|
1131 | if (params.fee && !_.isNumber(params.fee)) {
|
1132 | throw new Error('invalid argument for fee - number expected');
|
1133 | }
|
1134 |
|
1135 | if (params.feeRate && !_.isNumber(params.feeRate)) {
|
1136 | throw new Error('invalid argument for feeRate - number expected');
|
1137 | }
|
1138 |
|
1139 | if (params.instant && !_.isBoolean(params.instant)) {
|
1140 | throw new Error('invalid argument for instant - boolean expected');
|
1141 | }
|
1142 |
|
1143 | let bitgoFee;
|
1144 | let travelInfos;
|
1145 | let finalResult;
|
1146 | let unspentsUsed;
|
1147 |
|
1148 | const acceptedBuildParams = [
|
1149 | 'numBlocks', 'feeRate', 'minConfirms', 'enforceMinConfirmsForChange',
|
1150 | 'targetWalletUnspents', 'message', 'minValue', 'maxValue',
|
1151 | 'noSplitChange', 'comment'
|
1152 | ];
|
1153 | const preservedBuildParams = _.pick(params, acceptedBuildParams);
|
1154 |
|
1155 |
|
1156 | const retPromise = this.createAndSignTransaction(params)
|
1157 | .then(function(transaction) {
|
1158 |
|
1159 | bitgoFee = transaction.bitgoFee;
|
1160 | travelInfos = transaction.travelInfos;
|
1161 | unspentsUsed = transaction.unspents;
|
1162 | return self.sendTransaction({
|
1163 | tx: transaction.tx,
|
1164 | message: params.message,
|
1165 | sequenceId: params.sequenceId,
|
1166 | instant: params.instant,
|
1167 | otp: params.otp,
|
1168 |
|
1169 | estimatedSize: transaction.estimatedSize,
|
1170 | buildParams: preservedBuildParams
|
1171 | });
|
1172 | })
|
1173 | .then(function(result) {
|
1174 | const tx = bitcoin.Transaction.fromHex(result.tx);
|
1175 | const inputsSum = _.sumBy(unspentsUsed, 'value');
|
1176 | const outputsSum = _.sumBy(tx.outs, 'value');
|
1177 | const feeUsed = inputsSum - outputsSum;
|
1178 | if (isNaN(feeUsed)) {
|
1179 | throw new Error('invalid feeUsed');
|
1180 | }
|
1181 | result.fee = feeUsed,
|
1182 | result.feeRate = feeUsed * 1000 / tx.virtualSize();
|
1183 | result.travelInfos = travelInfos;
|
1184 | if (bitgoFee) {
|
1185 | result.bitgoFee = bitgoFee;
|
1186 | }
|
1187 | finalResult = result;
|
1188 |
|
1189 |
|
1190 |
|
1191 | if (travelInfos && travelInfos.length) {
|
1192 | try {
|
1193 | return self.pollForTransaction({ id: result.hash })
|
1194 | .then(function() {
|
1195 | return self.bitgo.travelRule().sendMany(result);
|
1196 | })
|
1197 | .then(function(res) {
|
1198 | finalResult.travelResult = res;
|
1199 | })
|
1200 | .catch(function(err) {
|
1201 |
|
1202 | finalResult.travelResult = { error: err.message };
|
1203 | });
|
1204 | } catch (err) {
|
1205 |
|
1206 | finalResult.travelResult = { error: err.message };
|
1207 | }
|
1208 | }
|
1209 | })
|
1210 | .then(function() {
|
1211 | return finalResult;
|
1212 | });
|
1213 | return Bluebird.resolve(retPromise).nodeify(callback);
|
1214 | };
|
1215 |
|
1216 |
|
1217 |
|
1218 |
|
1219 |
|
1220 |
|
1221 |
|
1222 |
|
1223 |
|
1224 |
|
1225 |
|
1226 |
|
1227 |
|
1228 |
|
1229 |
|
1230 | Wallet.prototype.accelerateTransaction = function accelerateTransaction(params, callback) {
|
1231 |
|
1232 | const self = this;
|
1233 | |
1234 |
|
1235 |
|
1236 |
|
1237 |
|
1238 |
|
1239 |
|
1240 |
|
1241 |
|
1242 |
|
1243 | const estimateTxVSize = (inputs) => {
|
1244 | const segwit = inputs.segwit || 0;
|
1245 | const P2SH = inputs.P2SH || 0;
|
1246 | const P2PKH = inputs.P2PKH || 0;
|
1247 |
|
1248 | const childFeeInfo = TransactionBuilder.calculateMinerFeeInfo({
|
1249 | nP2shInputs: P2SH,
|
1250 | nP2pkhInputs: P2PKH,
|
1251 | nP2shP2wshInputs: segwit,
|
1252 | nOutputs: 1,
|
1253 | feeRate: 1
|
1254 | });
|
1255 |
|
1256 | return childFeeInfo.size;
|
1257 | };
|
1258 |
|
1259 | |
1260 |
|
1261 |
|
1262 |
|
1263 |
|
1264 |
|
1265 |
|
1266 |
|
1267 |
|
1268 | const estimateChildFee = ({ inputs, parentFee, parentVSize, feeRate }) => {
|
1269 |
|
1270 |
|
1271 | const additionalParentFee = _.ceil(parentVSize * feeRate / 1000) - parentFee;
|
1272 |
|
1273 |
|
1274 |
|
1275 | const childFee = estimateTxVSize(inputs) * feeRate / 1000;
|
1276 |
|
1277 | return _.ceil(childFee + additionalParentFee);
|
1278 | };
|
1279 |
|
1280 | |
1281 |
|
1282 |
|
1283 |
|
1284 |
|
1285 |
|
1286 |
|
1287 |
|
1288 |
|
1289 |
|
1290 |
|
1291 |
|
1292 |
|
1293 | const findAdditionalUnspents = ({ inputs, parentOutputValue, parentFee, parentVSize, maxUnspents }) => {
|
1294 | return co(function *coFindAdditionalUnspents() {
|
1295 |
|
1296 | const additionalUnspents: any[] = [];
|
1297 |
|
1298 |
|
1299 |
|
1300 |
|
1301 |
|
1302 | let currentChildFeeEstimate = estimateChildFee({ inputs, parentFee, parentVSize, feeRate: params.feeRate });
|
1303 | let uncoveredChildFee = currentChildFeeEstimate - parentOutputValue;
|
1304 |
|
1305 | while (uncoveredChildFee > 0 && additionalUnspents.length < maxUnspents) {
|
1306 |
|
1307 | const unspents = (yield this.unspents({
|
1308 | minConfirms: 1,
|
1309 | target: uncoveredChildFee,
|
1310 | limit: maxUnspents - additionalUnspents.length
|
1311 | })) as any;
|
1312 |
|
1313 | if (unspents.length === 0) {
|
1314 |
|
1315 | break;
|
1316 | }
|
1317 |
|
1318 | let additionalUnspentValue = 0;
|
1319 |
|
1320 |
|
1321 |
|
1322 |
|
1323 |
|
1324 |
|
1325 | _.forEach(unspents, (unspent) => {
|
1326 |
|
1327 | const unspentChain = getChain(unspent);
|
1328 | if (unspentChain === Codes.p2shP2wsh.external || unspentChain === Codes.p2shP2wsh.internal) {
|
1329 | inputs.segwit++;
|
1330 | } else {
|
1331 | inputs.P2SH++;
|
1332 | }
|
1333 |
|
1334 | additionalUnspents.push(unspent);
|
1335 | additionalUnspentValue += unspent.value;
|
1336 | });
|
1337 |
|
1338 | currentChildFeeEstimate = estimateChildFee({ inputs, parentFee, parentVSize, feeRate: params.feeRate });
|
1339 | uncoveredChildFee = currentChildFeeEstimate - parentOutputValue - additionalUnspentValue;
|
1340 | }
|
1341 |
|
1342 | if (uncoveredChildFee > 0) {
|
1343 |
|
1344 | throw new Error(`Insufficient confirmed unspents available to cover the child fee`);
|
1345 | }
|
1346 |
|
1347 |
|
1348 | return {
|
1349 | additional: additionalUnspents,
|
1350 | newChildFee: currentChildFeeEstimate,
|
1351 | newInputs: inputs
|
1352 | };
|
1353 | }).call(this);
|
1354 | };
|
1355 |
|
1356 | |
1357 |
|
1358 |
|
1359 |
|
1360 |
|
1361 |
|
1362 |
|
1363 |
|
1364 |
|
1365 |
|
1366 | async function getParentTxHex({ parentTxId }: { parentTxId: string }): Promise<string> {
|
1367 | const explorerBaseUrl = common.Environments[self.bitgo.getEnv()].btcExplorerBaseUrl;
|
1368 | const result = await request.get(`${explorerBaseUrl}/tx/${parentTxId}/hex`);
|
1369 |
|
1370 | if (!result.text || !/([a-f0-9]{2})+/.test(result.text)) {
|
1371 | throw new Error(`Did not successfully receive parent tx hex. Received '${_.truncate(result.text, { length: 100 })}' instead.`);
|
1372 | }
|
1373 |
|
1374 | return result.text;
|
1375 | }
|
1376 |
|
1377 | |
1378 |
|
1379 |
|
1380 |
|
1381 |
|
1382 |
|
1383 | const getChain = (outputOrUnspent) => {
|
1384 | if (outputOrUnspent.chain !== undefined) {
|
1385 | return outputOrUnspent.chain;
|
1386 | }
|
1387 |
|
1388 | if (outputOrUnspent.chainPath !== undefined) {
|
1389 | return _.toNumber(outputOrUnspent.chainPath.split('/')[1]);
|
1390 | }
|
1391 |
|
1392 |
|
1393 |
|
1394 |
|
1395 |
|
1396 | throw Error(`Could not get chain for output on account ${outputOrUnspent.account || outputOrUnspent.address}`);
|
1397 | };
|
1398 |
|
1399 | |
1400 |
|
1401 |
|
1402 |
|
1403 |
|
1404 |
|
1405 |
|
1406 |
|
1407 |
|
1408 |
|
1409 | const effectiveValue = (outputOrUnspent) => {
|
1410 | const chain = getChain(outputOrUnspent);
|
1411 | if (chain === Codes.p2shP2wsh.external || chain === Codes.p2shP2wsh.internal) {
|
1412 |
|
1413 | return outputOrUnspent.value - (VirtualSizes.txP2shP2wshInputSize * params.feeRate / 1000);
|
1414 | }
|
1415 |
|
1416 | return outputOrUnspent.value - (VirtualSizes.txP2shInputSize * params.feeRate / 1000);
|
1417 | };
|
1418 |
|
1419 | |
1420 |
|
1421 |
|
1422 |
|
1423 |
|
1424 |
|
1425 |
|
1426 |
|
1427 |
|
1428 |
|
1429 |
|
1430 |
|
1431 |
|
1432 | return co(function *coAccelerateTransaction(): any {
|
1433 | params = params || {};
|
1434 | common.validateParams(params, ['transactionID'], [], callback);
|
1435 |
|
1436 |
|
1437 | if (params.feeRate === undefined) {
|
1438 | throw new Error('Missing parameter: feeRate');
|
1439 | }
|
1440 | if (!_.isFinite(params.feeRate) || params.feeRate <= 0) {
|
1441 | throw new Error('Expecting positive finite number for parameter: feeRate');
|
1442 | }
|
1443 |
|
1444 |
|
1445 | if (params.maxAdditionalUnspents === undefined) {
|
1446 |
|
1447 | params.maxAdditionalUnspents = 100;
|
1448 | }
|
1449 |
|
1450 | if (!_.isInteger(params.maxAdditionalUnspents) || params.maxAdditionalUnspents <= 0) {
|
1451 | throw Error('Expecting positive integer for parameter: maxAdditionalUnspents');
|
1452 | }
|
1453 |
|
1454 | const parentTx = yield this.getTransaction({ id: params.transactionID });
|
1455 | if (parentTx.confirmations > 0) {
|
1456 | throw new Error(`Transaction ${params.transactionID} is already confirmed and cannot be accelerated`);
|
1457 | }
|
1458 |
|
1459 |
|
1460 | const walletOutputs = _.filter(parentTx.outputs, (output) => output.isMine);
|
1461 |
|
1462 | if (walletOutputs.length === 0) {
|
1463 | throw new Error(`Transaction ${params.transactionID} contains no outputs to this wallet, and thus cannot be accelerated`);
|
1464 | }
|
1465 |
|
1466 |
|
1467 |
|
1468 |
|
1469 |
|
1470 |
|
1471 | const sortedOutputs = _.sortBy(walletOutputs, effectiveValue);
|
1472 | let parentUnspentToUse;
|
1473 | let outputToUse;
|
1474 |
|
1475 | while (sortedOutputs.length > 0 && parentUnspentToUse === undefined) {
|
1476 | outputToUse = sortedOutputs.pop();
|
1477 |
|
1478 |
|
1479 |
|
1480 |
|
1481 | const unspentsResult = yield this.unspents({
|
1482 | minSize: outputToUse.value,
|
1483 | maxSize: outputToUse.value
|
1484 | });
|
1485 |
|
1486 | parentUnspentToUse = _.find(unspentsResult, (unspent) => {
|
1487 |
|
1488 | if (unspent.tx_hash !== params.transactionID) {
|
1489 | return false;
|
1490 | }
|
1491 |
|
1492 | return unspent.tx_output_n === outputToUse.vout;
|
1493 | });
|
1494 | }
|
1495 |
|
1496 | if (parentUnspentToUse === undefined) {
|
1497 | throw new Error(`Could not find unspent output from parent tx to use as child input`);
|
1498 | }
|
1499 |
|
1500 |
|
1501 | const parentTxHex = yield getParentTxHex({ parentTxId: params.transactionID });
|
1502 | const decodedParent = bitcoin.Transaction.fromHex(parentTxHex);
|
1503 | const parentVSize = decodedParent.virtualSize();
|
1504 |
|
1505 |
|
1506 |
|
1507 |
|
1508 | if (decodedParent.getId() !== params.transactionID) {
|
1509 | throw new Error(`Decoded transaction id is ${decodedParent.getId()}, which does not match given txid ${params.transactionID}`);
|
1510 | }
|
1511 |
|
1512 |
|
1513 |
|
1514 | const parentRate = 1000 * parentTx.fee / parentVSize;
|
1515 | if (params.feeRate <= parentRate) {
|
1516 | throw new Error(`Cannot lower fee rate! (Parent tx fee rate is ${parentRate} sat/kB, and requested fee rate was ${params.feeRate} sat/kB)`);
|
1517 | }
|
1518 |
|
1519 |
|
1520 | const isParentOutputSegwit =
|
1521 | outputToUse.chain === Codes.p2shP2wsh.external ||
|
1522 | outputToUse.chain === Codes.p2shP2wsh.internal;
|
1523 |
|
1524 | let childInputs = {
|
1525 | segwit: isParentOutputSegwit ? 1 : 0,
|
1526 | P2SH: isParentOutputSegwit ? 0 : 1
|
1527 | };
|
1528 |
|
1529 | let childFee = estimateChildFee({
|
1530 | inputs: childInputs,
|
1531 | parentFee: parentTx.fee,
|
1532 | feeRate: params.feeRate,
|
1533 | parentVSize
|
1534 | });
|
1535 |
|
1536 | const unspentsToUse = [parentUnspentToUse];
|
1537 |
|
1538 |
|
1539 |
|
1540 | const minChangeSize = this.bitgo.getConstants().minChangeSize || 1e7;
|
1541 |
|
1542 | if (outputToUse.value < childFee + minChangeSize) {
|
1543 |
|
1544 |
|
1545 | const { additional, newChildFee, newInputs } = yield findAdditionalUnspents({
|
1546 | inputs: childInputs,
|
1547 | parentOutputValue: outputToUse.value,
|
1548 | parentFee: parentTx.fee,
|
1549 | maxUnspents: params.maxAdditionalUnspents,
|
1550 | parentVSize
|
1551 | });
|
1552 | childFee = newChildFee;
|
1553 | childInputs = newInputs;
|
1554 | unspentsToUse.push(... additional);
|
1555 | }
|
1556 |
|
1557 |
|
1558 |
|
1559 |
|
1560 | const maxFeeRate = this.bitgo.getConstants().maxFeeRate;
|
1561 | const childVSize = estimateTxVSize(childInputs);
|
1562 | const combinedVSize = childVSize + parentVSize;
|
1563 | const combinedFee = parentTx.fee + childFee;
|
1564 |
|
1565 | const combinedFeeRate = 1000 * combinedFee / combinedVSize;
|
1566 |
|
1567 | if (combinedFeeRate > maxFeeRate) {
|
1568 | throw new Error(`Transaction cannot be accelerated. Combined fee rate of ${combinedFeeRate} sat/kB exceeds maximum fee rate of ${maxFeeRate} sat/kB`);
|
1569 | }
|
1570 |
|
1571 |
|
1572 |
|
1573 |
|
1574 |
|
1575 | const changeAmount = _.sumBy(unspentsToUse, (unspent) => unspent.value) - childFee;
|
1576 | const changeChain = this.getChangeChain({});
|
1577 | const changeAddress = yield this.createAddress({ chain: changeChain });
|
1578 |
|
1579 |
|
1580 | const tx = yield this.createAndSignTransaction({
|
1581 | unspents: unspentsToUse,
|
1582 | recipients: [{
|
1583 | address: changeAddress.address,
|
1584 | amount: changeAmount
|
1585 | }],
|
1586 | fee: childFee,
|
1587 | bitgoFee: {
|
1588 | amount: 0,
|
1589 | address: ''
|
1590 | },
|
1591 | xprv: params.xprv,
|
1592 | walletPassphrase: params.walletPassphrase
|
1593 | });
|
1594 |
|
1595 |
|
1596 |
|
1597 | const childFeeRate = 1000 * childFee / childVSize;
|
1598 | if (childFeeRate > maxFeeRate) {
|
1599 |
|
1600 |
|
1601 | tx.ignoreMaxFeeRate = true;
|
1602 | }
|
1603 |
|
1604 | return this.sendTransaction(tx);
|
1605 | }).call(this).asCallback(callback);
|
1606 | };
|
1607 |
|
1608 |
|
1609 |
|
1610 |
|
1611 |
|
1612 |
|
1613 |
|
1614 |
|
1615 |
|
1616 |
|
1617 |
|
1618 | Wallet.prototype.createAndSignTransaction = function(params, callback) {
|
1619 | return co(function *() {
|
1620 | params = params || {};
|
1621 | common.validateParams(params, [], [], callback);
|
1622 |
|
1623 | if (!_.isObject(params.recipients)) {
|
1624 | throw new Error('expecting recipients object');
|
1625 | }
|
1626 |
|
1627 | if (params.fee && !_.isNumber(params.fee)) {
|
1628 | throw new Error('invalid argument for fee - number expected');
|
1629 | }
|
1630 |
|
1631 | if (params.feeRate && !_.isNumber(params.feeRate)) {
|
1632 | throw new Error('invalid argument for feeRate - number expected');
|
1633 | }
|
1634 |
|
1635 | if (params.dynamicFeeConfirmTarget && !_.isNumber(params.dynamicFeeConfirmTarget)) {
|
1636 | throw new Error('invalid argument for confirmTarget - number expected');
|
1637 | }
|
1638 |
|
1639 | if (params.instant && !_.isBoolean(params.instant)) {
|
1640 | throw new Error('invalid argument for instant - boolean expected');
|
1641 | }
|
1642 |
|
1643 | const transaction = (yield this.createTransaction(params)) as any;
|
1644 | const fee = transaction.fee;
|
1645 | const feeRate = transaction.feeRate;
|
1646 | const estimatedSize = transaction.estimatedSize;
|
1647 | const bitgoFee = transaction.bitgoFee;
|
1648 | const travelInfos = transaction.travelInfos;
|
1649 | const unspents = transaction.unspents;
|
1650 |
|
1651 |
|
1652 | try {
|
1653 | const keychain = yield this.getAndPrepareSigningKeychain(params);
|
1654 | transaction.keychain = keychain;
|
1655 | } catch (e) {
|
1656 | if (e.code !== 'no_encrypted_keychain_on_wallet') {
|
1657 | throw e;
|
1658 | }
|
1659 |
|
1660 | yield this.refresh({ gpk: true });
|
1661 | const safeUserKey = _.get(this.wallet, 'private.userPrivKey');
|
1662 | if (_.isString(safeUserKey) && _.isString(params.walletPassphrase)) {
|
1663 | transaction.signingKey = this.bitgo.decrypt({ password: params.walletPassphrase, input: safeUserKey });
|
1664 | } else {
|
1665 | throw e;
|
1666 | }
|
1667 | }
|
1668 |
|
1669 | transaction.feeSingleKeyWIF = params.feeSingleKeyWIF;
|
1670 | const result = yield this.signTransaction(transaction);
|
1671 | return _.extend(result, {
|
1672 | fee,
|
1673 | feeRate,
|
1674 | instant: params.instant,
|
1675 | bitgoFee,
|
1676 | travelInfos,
|
1677 | estimatedSize,
|
1678 | unspents
|
1679 | });
|
1680 | }).call(this).asCallback(callback);
|
1681 | };
|
1682 |
|
1683 |
|
1684 |
|
1685 |
|
1686 |
|
1687 |
|
1688 |
|
1689 |
|
1690 |
|
1691 |
|
1692 |
|
1693 |
|
1694 |
|
1695 |
|
1696 |
|
1697 |
|
1698 | Wallet.prototype.getAndPrepareSigningKeychain = function(params, callback) {
|
1699 | params = params || {};
|
1700 |
|
1701 |
|
1702 | if (_.isObject(params.keychain) && params.keychain.xprv) {
|
1703 | return Bluebird.resolve(params.keychain);
|
1704 | }
|
1705 |
|
1706 | common.validateParams(params, [], ['walletPassphrase', 'xprv'], callback);
|
1707 |
|
1708 | if ((params.walletPassphrase && params.xprv) || (!params.walletPassphrase && !params.xprv)) {
|
1709 | throw new Error('must provide exactly one of xprv or walletPassphrase');
|
1710 | }
|
1711 |
|
1712 | const self = this;
|
1713 |
|
1714 |
|
1715 | if (params.walletPassphrase) {
|
1716 | return self.getEncryptedUserKeychain()
|
1717 | .then(function(keychain) {
|
1718 |
|
1719 | try {
|
1720 | keychain.xprv = self.bitgo.decrypt({ password: params.walletPassphrase, input: keychain.encryptedXprv });
|
1721 | } catch (e) {
|
1722 | throw new Error('Unable to decrypt user keychain');
|
1723 | }
|
1724 | return keychain;
|
1725 | });
|
1726 | }
|
1727 |
|
1728 |
|
1729 | let xpub;
|
1730 | try {
|
1731 | xpub = bitcoin.HDNode.fromBase58(params.xprv).neutered().toBase58();
|
1732 | } catch (e) {
|
1733 | throw new Error('Unable to parse the xprv');
|
1734 | }
|
1735 |
|
1736 | if (xpub === params.xprv) {
|
1737 | throw new Error('xprv provided was not a private key (found xpub instead)');
|
1738 | }
|
1739 |
|
1740 | const walletXpubs = _.map(self.keychains, 'xpub');
|
1741 | if (!_.includes(walletXpubs, xpub)) {
|
1742 | throw new Error('xprv provided was not a keychain on this wallet!');
|
1743 | }
|
1744 |
|
1745 |
|
1746 | return self.bitgo.keychains().get({ xpub: xpub })
|
1747 | .then(function(keychain) {
|
1748 | keychain.xprv = params.xprv;
|
1749 | return keychain;
|
1750 | });
|
1751 | };
|
1752 |
|
1753 |
|
1754 |
|
1755 |
|
1756 |
|
1757 |
|
1758 |
|
1759 |
|
1760 |
|
1761 |
|
1762 |
|
1763 | Wallet.prototype.fanOutUnspents = function(params, callback) {
|
1764 | const self = this;
|
1765 | return Bluebird.coroutine(function *() {
|
1766 |
|
1767 |
|
1768 | const MAX_FANOUT_INPUT_COUNT = 80;
|
1769 |
|
1770 | const MAX_FANOUT_OUTPUT_COUNT = 300;
|
1771 | params = params || {};
|
1772 | common.validateParams(params, [], ['walletPassphrase', 'xprv'], callback);
|
1773 | const validate = params.validate === undefined ? true : params.validate;
|
1774 |
|
1775 | const target = params.target;
|
1776 |
|
1777 | if (!_.isNumber(target) || target < 2 || (target % 1) !== 0) {
|
1778 | throw new Error('Target needs to be a positive integer');
|
1779 | }
|
1780 | if (target > MAX_FANOUT_OUTPUT_COUNT) {
|
1781 | throw new Error('Fan out target too high');
|
1782 | }
|
1783 |
|
1784 | let minConfirms = params.minConfirms;
|
1785 | if (minConfirms === undefined) {
|
1786 | minConfirms = 1;
|
1787 | }
|
1788 | if (!_.isNumber(minConfirms) || minConfirms < 0) {
|
1789 | throw new Error('minConfirms needs to be an integer >= 0');
|
1790 | }
|
1791 |
|
1792 | |
1793 |
|
1794 |
|
1795 |
|
1796 |
|
1797 |
|
1798 |
|
1799 |
|
1800 |
|
1801 | const splitNumberIntoCloseNaturalNumbers = function(total, partCount) {
|
1802 | const partSize = Math.floor(total / partCount);
|
1803 | const remainder = total - partSize * partCount;
|
1804 |
|
1805 | const almostEqualParts = new Array(partCount);
|
1806 |
|
1807 | _.fill(almostEqualParts, partSize + 1, 0, remainder);
|
1808 |
|
1809 | _.fill(almostEqualParts, partSize, remainder);
|
1810 |
|
1811 |
|
1812 | if (_(almostEqualParts).sum() !== total || _(almostEqualParts).size() !== partCount) {
|
1813 | throw new Error('part sum or part count mismatch');
|
1814 | }
|
1815 | return almostEqualParts;
|
1816 | };
|
1817 |
|
1818 |
|
1819 | const allUnspents = (yield self.unspents({ minConfirms: minConfirms })) as any;
|
1820 | if (allUnspents.length < 1) {
|
1821 | throw new Error('No unspents to branch out');
|
1822 | }
|
1823 |
|
1824 |
|
1825 | if (allUnspents.length >= target) {
|
1826 | throw new Error('Fan out target has to be bigger than current number of unspents');
|
1827 | }
|
1828 |
|
1829 |
|
1830 |
|
1831 | if (allUnspents.length > MAX_FANOUT_INPUT_COUNT) {
|
1832 | throw new Error('Too many unspents');
|
1833 | }
|
1834 |
|
1835 |
|
1836 | const grossAmount = _(allUnspents).map('value').sum();
|
1837 |
|
1838 |
|
1839 | const txParams = _.extend({}, params);
|
1840 | txParams.unspents = allUnspents;
|
1841 | txParams.recipients = {};
|
1842 |
|
1843 |
|
1844 | const newAddressPromises = _.range(target)
|
1845 | .map(() => self.createAddress({ chain: self.getChangeChain(params), validate: validate }));
|
1846 | const newAddresses = yield Bluebird.all(newAddressPromises);
|
1847 |
|
1848 | const splitAmounts = splitNumberIntoCloseNaturalNumbers(grossAmount, target);
|
1849 |
|
1850 | txParams.recipients = _.zipObject(_.map(newAddresses, 'address'), splitAmounts);
|
1851 | txParams.noSplitChange = true;
|
1852 |
|
1853 | try {
|
1854 | yield self.sendMany(txParams);
|
1855 | } catch (error) {
|
1856 |
|
1857 |
|
1858 |
|
1859 | if (!error.fee && (!error.result || !error.result.fee)) {
|
1860 |
|
1861 | const debugParams = _.omit(txParams, ['walletPassphrase', 'xprv']);
|
1862 | error.message += `\n\nTX PARAMS:\n ${JSON.stringify(debugParams, null, 4)}`;
|
1863 | throw error;
|
1864 | }
|
1865 | const baseFee = error.fee || error.result.fee;
|
1866 | let totalFee = baseFee;
|
1867 | if (error.result.bitgoFee && error.result.bitgoFee.amount) {
|
1868 | totalFee += error.result.bitgoFee.amount;
|
1869 | txParams.bitgoFee = error.result.bitgoFee;
|
1870 | }
|
1871 |
|
1872 |
|
1873 | delete txParams.fee;
|
1874 | txParams.originalFeeRate = txParams.feeRate;
|
1875 | delete txParams.feeRate;
|
1876 | delete txParams.feeTxConfirmTarget;
|
1877 | txParams.fee = baseFee;
|
1878 |
|
1879 |
|
1880 | const netAmount = error.result.available - totalFee;
|
1881 |
|
1882 | const remainingSplitAmounts = splitNumberIntoCloseNaturalNumbers(netAmount, target);
|
1883 |
|
1884 | txParams.recipients = _.zipObject(_.map(newAddresses, 'address'), remainingSplitAmounts);
|
1885 | }
|
1886 |
|
1887 |
|
1888 | let fanoutTx;
|
1889 | try {
|
1890 | fanoutTx = yield self.sendMany(txParams);
|
1891 | } catch (e) {
|
1892 | const debugParams = _.omit(txParams, ['walletPassphrase', 'xprv']);
|
1893 | e.message += `\n\nTX PARAMS:\n ${JSON.stringify(debugParams, null, 4)}`;
|
1894 | throw e;
|
1895 | }
|
1896 |
|
1897 | return Bluebird.resolve(fanoutTx).asCallback(callback);
|
1898 | })().asCallback(callback);
|
1899 | };
|
1900 |
|
1901 |
|
1902 |
|
1903 |
|
1904 |
|
1905 |
|
1906 |
|
1907 | Wallet.prototype.regroupUnspents = function(params, callback) {
|
1908 | params = params || {};
|
1909 | const target = params.target;
|
1910 | if (!_.isNumber(target) || target < 1 || (target % 1) !== 0) {
|
1911 |
|
1912 | throw new Error('Target needs to be a positive integer');
|
1913 | }
|
1914 |
|
1915 | let minConfirms = params.minConfirms;
|
1916 | if (minConfirms === undefined) {
|
1917 | minConfirms = 1;
|
1918 | }
|
1919 | if ((!_.isNumber(minConfirms) || minConfirms < 0)) {
|
1920 | throw new Error('minConfirms needs to be an integer equal to or bigger than 0');
|
1921 | }
|
1922 |
|
1923 | const self = this;
|
1924 | return self.unspents({ minConfirms: minConfirms })
|
1925 | .then(function(unspents) {
|
1926 | if (unspents.length === target) {
|
1927 | return unspents;
|
1928 | } else if (unspents.length > target) {
|
1929 | return self.consolidateUnspents(params, callback);
|
1930 | } else if (unspents.length < target) {
|
1931 | return self.fanOutUnspents(params, callback);
|
1932 | }
|
1933 | });
|
1934 | };
|
1935 |
|
1936 |
|
1937 |
|
1938 |
|
1939 |
|
1940 |
|
1941 |
|
1942 |
|
1943 |
|
1944 |
|
1945 |
|
1946 |
|
1947 |
|
1948 | Wallet.prototype.consolidateUnspents = function(params, callback) {
|
1949 | params = params || {};
|
1950 | common.validateParams(params, [], ['walletPassphrase', 'xprv'], callback);
|
1951 | const validate = params.validate === undefined ? true : params.validate;
|
1952 |
|
1953 | let target = params.target;
|
1954 | if (target === undefined) {
|
1955 | target = 1;
|
1956 | } else if (!_.isNumber(target) || target < 1 || (target % 1) !== 0) {
|
1957 |
|
1958 | throw new Error('Target needs to be a positive integer');
|
1959 | }
|
1960 |
|
1961 | if (params.maxSize && !_.isNumber(params.maxSize)) {
|
1962 | throw new Error('maxSize should be a number');
|
1963 | }
|
1964 |
|
1965 | if (params.minSize && !_.isNumber(params.minSize)) {
|
1966 | throw new Error('minSize should be a number');
|
1967 | }
|
1968 |
|
1969 |
|
1970 | const MAX_INPUT_COUNT = 200;
|
1971 | let maxInputCount = params.maxInputCountPerConsolidation;
|
1972 | if (maxInputCount === undefined) {
|
1973 | maxInputCount = MAX_INPUT_COUNT;
|
1974 | }
|
1975 | if (typeof (maxInputCount) !== 'number' || maxInputCount < 2 || (maxInputCount % 1) !== 0) {
|
1976 | throw new Error('Maximum consolidation input count needs to be an integer equal to or bigger than 2');
|
1977 | } else if (maxInputCount > MAX_INPUT_COUNT) {
|
1978 | throw new Error('Maximum consolidation input count cannot be bigger than ' + MAX_INPUT_COUNT);
|
1979 | }
|
1980 |
|
1981 | const maxIterationCount = params.maxIterationCount || -1;
|
1982 | if (params.maxIterationCount && (!_.isNumber(maxIterationCount) || maxIterationCount < 1) || (maxIterationCount % 1) !== 0) {
|
1983 | throw new Error('Maximum iteration count needs to be an integer equal to or bigger than 1');
|
1984 | }
|
1985 |
|
1986 | let minConfirms = params.minConfirms;
|
1987 | if (minConfirms === undefined) {
|
1988 | minConfirms = 1;
|
1989 | }
|
1990 | if ((!_.isNumber(minConfirms) || minConfirms < 0)) {
|
1991 | throw new Error('minConfirms needs to be an integer equal to or bigger than 0');
|
1992 | }
|
1993 |
|
1994 | let minSize = params.minSize || 0;
|
1995 | if (params.feeRate) {
|
1996 |
|
1997 | const feeBasedMinSize = Math.ceil(VirtualSizes.txP2shInputSize * params.feeRate / 1000);
|
1998 | if (params.minSize && minSize < feeBasedMinSize) {
|
1999 | throw new Error('provided minSize too low due to too high fee rate');
|
2000 | }
|
2001 | minSize = Math.max(feeBasedMinSize, minSize);
|
2002 |
|
2003 | if (!params.minSize) {
|
2004 |
|
2005 | console.log('Only consolidating unspents larger than ' + minSize + ' satoshis to avoid wasting money on fees. To consolidate smaller unspents, use a lower fee rate.');
|
2006 | }
|
2007 | }
|
2008 |
|
2009 | let iterationCount = 0;
|
2010 |
|
2011 | const self = this;
|
2012 | let consolidationIndex = 0;
|
2013 |
|
2014 | |
2015 |
|
2016 |
|
2017 |
|
2018 | const runNextConsolidation = co(function *() {
|
2019 | const consolidationTransactions: any[] = [];
|
2020 | let isFinalConsolidation = false;
|
2021 | iterationCount++;
|
2022 | |
2023 |
|
2024 |
|
2025 |
|
2026 |
|
2027 |
|
2028 |
|
2029 |
|
2030 |
|
2031 |
|
2032 |
|
2033 |
|
2034 | const queryParams: any = {
|
2035 | limit: target + maxInputCount,
|
2036 | minConfirms: minConfirms,
|
2037 | minSize: minSize
|
2038 | };
|
2039 | if (params.maxSize) {
|
2040 | queryParams.maxSize = params.maxSize;
|
2041 | }
|
2042 | const allUnspents = (yield self.unspents(queryParams)) as any;
|
2043 |
|
2044 | if (allUnspents.length <= target) {
|
2045 | if (iterationCount <= 1) {
|
2046 |
|
2047 | throw new Error('Fewer unspents than consolidation target. Use fanOutUnspents instead.');
|
2048 | } else {
|
2049 |
|
2050 | throw new Error('Done');
|
2051 | }
|
2052 | }
|
2053 |
|
2054 | const allUnspentsCount = allUnspents.length;
|
2055 |
|
2056 |
|
2057 |
|
2058 | let targetInputCount = allUnspentsCount - target + 1;
|
2059 | targetInputCount = Math.min(targetInputCount, allUnspents.length);
|
2060 |
|
2061 |
|
2062 | const inputCount = Math.min(targetInputCount, maxInputCount);
|
2063 |
|
2064 |
|
2065 |
|
2066 | isFinalConsolidation = (inputCount === targetInputCount || iterationCount === maxIterationCount);
|
2067 |
|
2068 | const currentChunk = allUnspents.splice(0, inputCount);
|
2069 | const changeChain = self.getChangeChain(params);
|
2070 | const newAddress = (yield self.createAddress({ chain: changeChain, validate: validate })) as any;
|
2071 | const txParams = _.extend({}, params);
|
2072 | const currentAddress = newAddress;
|
2073 |
|
2074 | const grossAmount = _(currentChunk).map('value').sum();
|
2075 |
|
2076 | txParams.unspents = currentChunk;
|
2077 | txParams.recipients = {};
|
2078 | txParams.recipients[newAddress.address] = grossAmount;
|
2079 | txParams.noSplitChange = true;
|
2080 |
|
2081 | if (txParams.unspents.length <= 1) {
|
2082 | throw new Error('Done');
|
2083 | }
|
2084 |
|
2085 |
|
2086 | try {
|
2087 | yield self.sendMany(txParams);
|
2088 | } catch (error) {
|
2089 |
|
2090 |
|
2091 | if (!error.fee && (!error.result || !error.result.fee)) {
|
2092 |
|
2093 | const debugParams = _.omit(txParams, ['walletPassphrase', 'xprv']);
|
2094 | error.message += `\n\nTX PARAMS:\n ${JSON.stringify(debugParams, null, 4)}`;
|
2095 | throw error;
|
2096 | }
|
2097 | const baseFee = error.fee || error.result.fee;
|
2098 | let bitgoFee = 0;
|
2099 | let totalFee = baseFee;
|
2100 | if (error.result.bitgoFee && error.result.bitgoFee.amount) {
|
2101 | bitgoFee = error.result.bitgoFee.amount;
|
2102 | totalFee += bitgoFee;
|
2103 | txParams.bitgoFee = error.result.bitgoFee;
|
2104 | }
|
2105 |
|
2106 |
|
2107 | const netAmount = Math.max(error.result.available - totalFee, self.bitgo.getConstants().minOutputSize);
|
2108 |
|
2109 | delete txParams.fee;
|
2110 | txParams.originalFeeRate = txParams.feeRate;
|
2111 | delete txParams.feeRate;
|
2112 | delete txParams.feeTxConfirmTarget;
|
2113 |
|
2114 |
|
2115 | txParams.fee = error.result.available - netAmount - bitgoFee;
|
2116 | txParams.recipients[newAddress.address] = netAmount;
|
2117 | }
|
2118 |
|
2119 | let sentTx;
|
2120 | try {
|
2121 | sentTx = yield self.sendMany(txParams);
|
2122 | } catch (e) {
|
2123 | const debugParams = _.omit(txParams, ['walletPassphrase', 'xprv']);
|
2124 | e.message += `\n\nTX PARAMS:\n ${JSON.stringify(debugParams, null, 4)}`;
|
2125 | throw e;
|
2126 | }
|
2127 | consolidationTransactions.push(sentTx);
|
2128 | if (_.isFunction(params.progressCallback)) {
|
2129 | params.progressCallback({
|
2130 | txid: sentTx.hash,
|
2131 | destination: currentAddress,
|
2132 | amount: grossAmount,
|
2133 | fee: sentTx.fee,
|
2134 | inputCount: inputCount,
|
2135 | index: consolidationIndex
|
2136 | });
|
2137 | }
|
2138 | consolidationIndex++;
|
2139 | if (!isFinalConsolidation) {
|
2140 |
|
2141 |
|
2142 |
|
2143 | yield Bluebird.delay(1000);
|
2144 | consolidationTransactions.push(...((yield runNextConsolidation()) as any));
|
2145 | }
|
2146 |
|
2147 | return consolidationTransactions;
|
2148 | });
|
2149 |
|
2150 | return runNextConsolidation(this, target)
|
2151 | .catch(function(err) {
|
2152 | if (err.message === 'Done') {
|
2153 | return;
|
2154 | }
|
2155 | throw err;
|
2156 | })
|
2157 | .nodeify(callback);
|
2158 | };
|
2159 |
|
2160 | Wallet.prototype.shareWallet = function(params, callback) {
|
2161 | params = params || {};
|
2162 | common.validateParams(params, ['email', 'permissions'], ['walletPassphrase', 'message'], callback);
|
2163 |
|
2164 | if (params.reshare !== undefined && !_.isBoolean(params.reshare)) {
|
2165 | throw new Error('Expected reshare to be a boolean.');
|
2166 | }
|
2167 |
|
2168 | if (params.skipKeychain !== undefined && !_.isBoolean(params.skipKeychain)) {
|
2169 | throw new Error('Expected skipKeychain to be a boolean. ');
|
2170 | }
|
2171 | const needsKeychain = !params.skipKeychain && params.permissions.indexOf('spend') !== -1;
|
2172 |
|
2173 | if (params.disableEmail !== undefined && !_.isBoolean(params.disableEmail)) {
|
2174 | throw new Error('Expected disableEmail to be a boolean.');
|
2175 | }
|
2176 |
|
2177 | const self = this;
|
2178 | let sharing;
|
2179 | let sharedKeychain;
|
2180 | return this.bitgo.getSharingKey({ email: params.email.toLowerCase() })
|
2181 | .then(function(result) {
|
2182 | sharing = result;
|
2183 |
|
2184 | if (needsKeychain) {
|
2185 | return self.getEncryptedUserKeychain({})
|
2186 | .then(function(keychain) {
|
2187 |
|
2188 | if (keychain.encryptedXprv) {
|
2189 | if (!params.walletPassphrase) {
|
2190 | throw new Error('Missing walletPassphrase argument');
|
2191 | }
|
2192 | try {
|
2193 | keychain.xprv = self.bitgo.decrypt({ password: params.walletPassphrase, input: keychain.encryptedXprv });
|
2194 | } catch (e) {
|
2195 | throw new Error('Unable to decrypt user keychain');
|
2196 | }
|
2197 |
|
2198 | const eckey = makeRandomKey();
|
2199 | const secret = self.bitgo.getECDHSecret({ eckey: eckey, otherPubKeyHex: sharing.pubkey });
|
2200 | const newEncryptedXprv = self.bitgo.encrypt({ password: secret, input: keychain.xprv });
|
2201 |
|
2202 | sharedKeychain = {
|
2203 | xpub: keychain.xpub,
|
2204 | encryptedXprv: newEncryptedXprv,
|
2205 | fromPubKey: eckey.getPublicKeyBuffer().toString('hex'),
|
2206 | toPubKey: sharing.pubkey,
|
2207 | path: sharing.path
|
2208 | };
|
2209 | }
|
2210 | });
|
2211 | }
|
2212 | })
|
2213 | .then(function() {
|
2214 | interface Options {
|
2215 | user: any;
|
2216 | permissions: string;
|
2217 | reshare: boolean;
|
2218 | message: string;
|
2219 | disableEmail: any;
|
2220 | keychain?: any;
|
2221 | skipKeychain?: boolean
|
2222 | }
|
2223 |
|
2224 | const options: Options = {
|
2225 | user: sharing.userId,
|
2226 | permissions: params.permissions,
|
2227 | reshare: params.reshare,
|
2228 | message: params.message,
|
2229 | disableEmail: params.disableEmail
|
2230 | };
|
2231 | if (sharedKeychain) {
|
2232 | options.keychain = sharedKeychain;
|
2233 | } else if (params.skipKeychain) {
|
2234 | options.keychain = {};
|
2235 | }
|
2236 |
|
2237 | return self.createShare(options);
|
2238 | })
|
2239 | .nodeify(callback);
|
2240 | };
|
2241 |
|
2242 | Wallet.prototype.removeUser = function(params, callback) {
|
2243 | params = params || {};
|
2244 | common.validateParams(params, ['user'], [], callback);
|
2245 |
|
2246 | return this.bitgo.del(this.url('/user/' + params.user))
|
2247 | .send()
|
2248 | .result()
|
2249 | .nodeify(callback);
|
2250 | };
|
2251 |
|
2252 | Wallet.prototype.getPolicy = function(params, callback) {
|
2253 | params = params || {};
|
2254 | common.validateParams(params, [], [], callback);
|
2255 |
|
2256 | return this.bitgo.get(this.url('/policy'))
|
2257 | .send()
|
2258 | .result()
|
2259 | .nodeify(callback);
|
2260 | };
|
2261 |
|
2262 | Wallet.prototype.getPolicyStatus = function(params, callback) {
|
2263 | params = params || {};
|
2264 | common.validateParams(params, [], [], callback);
|
2265 |
|
2266 | return this.bitgo.get(this.url('/policy/status'))
|
2267 | .send()
|
2268 | .result()
|
2269 | .nodeify(callback);
|
2270 | };
|
2271 |
|
2272 | Wallet.prototype.setPolicyRule = function(params, callback) {
|
2273 | params = params || {};
|
2274 | common.validateParams(params, ['id', 'type'], ['message'], callback);
|
2275 |
|
2276 | if (!_.isObject(params.condition)) {
|
2277 | throw new Error('missing parameter: conditions object');
|
2278 | }
|
2279 |
|
2280 | if (!_.isObject(params.action)) {
|
2281 | throw new Error('missing parameter: action object');
|
2282 | }
|
2283 |
|
2284 | return this.bitgo.put(this.url('/policy/rule'))
|
2285 | .send(params)
|
2286 | .result()
|
2287 | .nodeify(callback);
|
2288 | };
|
2289 |
|
2290 | Wallet.prototype.removePolicyRule = function(params, callback) {
|
2291 | params = params || {};
|
2292 | common.validateParams(params, ['id'], ['message'], callback);
|
2293 |
|
2294 | return this.bitgo.del(this.url('/policy/rule'))
|
2295 | .send(params)
|
2296 | .result()
|
2297 | .nodeify(callback);
|
2298 | };
|
2299 |
|
2300 | Wallet.prototype.listWebhooks = function(params, callback) {
|
2301 | params = params || {};
|
2302 | common.validateParams(params, [], [], callback);
|
2303 |
|
2304 | return this.bitgo.get(this.url('/webhooks'))
|
2305 | .send()
|
2306 | .result()
|
2307 | .nodeify(callback);
|
2308 | };
|
2309 |
|
2310 |
|
2311 |
|
2312 |
|
2313 |
|
2314 |
|
2315 |
|
2316 |
|
2317 |
|
2318 |
|
2319 | Wallet.prototype.simulateWebhook = function(params, callback) {
|
2320 | params = params || {};
|
2321 | common.validateParams(params, ['webhookId'], ['txHash', 'pendingApprovalId'], callback);
|
2322 |
|
2323 | const hasTxHash = !!params.txHash;
|
2324 | const hasPendingApprovalId = !!params.pendingApprovalId;
|
2325 |
|
2326 | if ((hasTxHash && hasPendingApprovalId) || (!hasTxHash && !hasPendingApprovalId)) {
|
2327 | throw new Error('must supply either txHash or pendingApprovalId, but not both');
|
2328 | }
|
2329 |
|
2330 |
|
2331 |
|
2332 |
|
2333 |
|
2334 | const filteredParams = _.pick(params, ['txHash', 'pendingApprovalId']);
|
2335 |
|
2336 | const webhookId = params.webhookId;
|
2337 | return this.bitgo.post(this.url('/webhooks/' + webhookId + '/simulate'))
|
2338 | .send(filteredParams)
|
2339 | .result()
|
2340 | .nodeify(callback);
|
2341 | };
|
2342 |
|
2343 | Wallet.prototype.addWebhook = function(params, callback) {
|
2344 | params = params || {};
|
2345 | common.validateParams(params, ['url', 'type'], [], callback);
|
2346 |
|
2347 | return this.bitgo.post(this.url('/webhooks'))
|
2348 | .send(params)
|
2349 | .result()
|
2350 | .nodeify(callback);
|
2351 | };
|
2352 |
|
2353 | Wallet.prototype.removeWebhook = function(params, callback) {
|
2354 | params = params || {};
|
2355 | common.validateParams(params, ['url', 'type'], [], callback);
|
2356 |
|
2357 | return this.bitgo.del(this.url('/webhooks'))
|
2358 | .send(params)
|
2359 | .result()
|
2360 | .nodeify(callback);
|
2361 | };
|
2362 |
|
2363 | Wallet.prototype.estimateFee = function(params, callback) {
|
2364 | common.validateParams(params, [], [], callback);
|
2365 |
|
2366 | if (params.amount && params.recipients) {
|
2367 | throw new Error('cannot specify both amount as well as recipients');
|
2368 | }
|
2369 | if (params.recipients && !_.isObject(params.recipients)) {
|
2370 | throw new Error('recipients must be array of { address: abc, amount: 100000 } objects');
|
2371 | }
|
2372 | if (params.amount && !_.isNumber(params.amount)) {
|
2373 | throw new Error('invalid amount argument, expecting number');
|
2374 | }
|
2375 |
|
2376 | const recipients = params.recipients || [];
|
2377 |
|
2378 | if (params.amount) {
|
2379 |
|
2380 | recipients.push({
|
2381 | address: common.Environments[this.bitgo.env].signingAddress,
|
2382 | amount: params.amount
|
2383 | });
|
2384 | }
|
2385 |
|
2386 | const transactionParams = _.extend({}, params);
|
2387 | transactionParams.amount = undefined;
|
2388 | transactionParams.recipients = recipients;
|
2389 |
|
2390 | return this.createTransaction(transactionParams)
|
2391 | .then(function(tx) {
|
2392 | return {
|
2393 | estimatedSize: tx.estimatedSize,
|
2394 | fee: tx.fee,
|
2395 | feeRate: tx.feeRate
|
2396 | };
|
2397 | });
|
2398 | };
|
2399 |
|
2400 |
|
2401 | Wallet.prototype.updatePolicyRule = function(params, callback) {
|
2402 | params = params || {};
|
2403 | common.validateParams(params, ['id', 'type'], [], callback);
|
2404 |
|
2405 | return this.bitgo.put(this.url('/policy/rule'))
|
2406 | .send(params)
|
2407 | .result()
|
2408 | .nodeify(callback);
|
2409 | };
|
2410 |
|
2411 | Wallet.prototype.deletePolicyRule = function(params, callback) {
|
2412 | params = params || {};
|
2413 | common.validateParams(params, ['id'], [], callback);
|
2414 |
|
2415 | return this.bitgo.del(this.url('/policy/rule'))
|
2416 | .send(params)
|
2417 | .result()
|
2418 | .nodeify(callback);
|
2419 | };
|
2420 |
|
2421 |
|
2422 |
|
2423 |
|
2424 |
|
2425 | Wallet.prototype.getBitGoFee = function(params, callback) {
|
2426 | params = params || {};
|
2427 | common.validateParams(params, [], [], callback);
|
2428 | if (!_.isNumber(params.amount)) {
|
2429 | throw new Error('invalid amount argument');
|
2430 | }
|
2431 | if (params.instant && !_.isBoolean(params.instant)) {
|
2432 | throw new Error('invalid instant argument');
|
2433 | }
|
2434 | return this.bitgo.get(this.url('/billing/fee'))
|
2435 | .query(params)
|
2436 | .result()
|
2437 | .nodeify(callback);
|
2438 | };
|
2439 |
|
2440 | export = Wallet;
|