copay/js/models/core/Wallet.js

623 lines
16 KiB
JavaScript
Raw Normal View History

2014-04-10 13:57:41 -07:00
'use strict';
2014-04-24 16:56:36 -07:00
var imports = require('soop').imports();
2014-04-14 13:17:56 -07:00
2014-04-24 16:56:36 -07:00
var bitcore = require('bitcore');
var coinUtil = bitcore.util;
2014-04-14 14:30:08 -07:00
var buffertools = bitcore.buffertools;
2014-04-24 16:56:36 -07:00
var Builder = bitcore.TransactionBuilder;
var http = require('http');
var EventEmitter = imports.EventEmitter || require('events').EventEmitter;
var copay = copay || require('../../../copay');
var SecureRandom = bitcore.SecureRandom;
var Base58Check = bitcore.Base58.base58Check;
2014-04-10 13:57:41 -07:00
2014-04-16 13:50:10 -07:00
function Wallet(opts) {
var self = this;
2014-04-14 13:17:56 -07:00
2014-04-16 13:50:10 -07:00
//required params
['storage', 'network', 'blockchain',
'requiredCopayers', 'totalCopayers', 'spendUnconfirmed',
2014-05-14 17:02:01 -07:00
'publicKeyRing', 'txProposals', 'privateKey', 'version'
2014-04-24 16:56:36 -07:00
].forEach(function(k) {
2014-04-16 13:50:10 -07:00
if (typeof opts[k] === 'undefined') throw new Error('missing key:' + k);
self[k] = opts[k];
});
2014-04-14 13:17:56 -07:00
2014-04-24 16:56:36 -07:00
this.log('creating ' + opts.requiredCopayers + ' of ' + opts.totalCopayers + ' wallet');
2014-04-16 15:45:22 -07:00
2014-04-16 13:50:10 -07:00
this.id = opts.id || Wallet.getRandomId();
this.name = opts.name;
2014-04-30 08:58:40 -07:00
this.netKey = opts.netKey || SecureRandom.getRandomBuffer(8).toString('base64');
2014-04-16 18:12:30 -07:00
this.verbose = opts.verbose;
2014-04-16 13:50:10 -07:00
this.publicKeyRing.walletId = this.id;
this.txProposals.walletId = this.id;
2014-04-24 19:13:55 -07:00
this.network.maxPeers = this.totalCopayers;
2014-05-01 05:41:18 -07:00
this.registeredPeerIds = [];
2014-04-14 13:42:10 -07:00
}
2014-04-10 13:57:41 -07:00
2014-04-24 16:56:36 -07:00
Wallet.parent = EventEmitter;
Wallet.prototype.log = function() {
if (!this.verbose) return;
2014-04-20 16:24:24 -07:00
if (console)
2014-04-24 16:56:36 -07:00
console.log.apply(console, arguments);
2014-04-16 13:50:10 -07:00
};
2014-04-15 10:28:49 -07:00
2014-04-16 13:50:10 -07:00
Wallet.getRandomId = function() {
2014-04-24 11:49:37 -07:00
var r = bitcore.SecureRandom.getPseudoRandomBuffer(8).toString('hex');
return r;
2014-04-16 13:50:10 -07:00
};
2014-04-15 10:28:49 -07:00
2014-05-09 10:35:57 -07:00
Wallet.prototype.seedCopayer = function(pubKey) {
this.seededCopayerId = pubKey;
};
2014-05-01 17:48:17 -07:00
Wallet.prototype.connectToAll = function() {
var all = this.publicKeyRing.getAllCopayerIds();
this.network.connectToCopayers(all);
2014-05-09 10:35:57 -07:00
if (this.seededCopayerId) {
this.sendWalletReady(this.seededCopayerId);
this.seededCopayerId = null;
2014-05-01 17:48:17 -07:00
}
};
2014-04-16 13:50:10 -07:00
Wallet.prototype._handlePublicKeyRing = function(senderId, data, isInbound) {
2014-04-24 16:56:36 -07:00
this.log('RECV PUBLICKEYRING:', data);
2014-04-16 13:50:10 -07:00
var recipients, pkr = this.publicKeyRing;
var inPKR = copay.PublicKeyRing.fromObj(data.publicKeyRing);
2014-04-18 14:25:51 -07:00
var hasChanged = pkr.merge(inPKR, true);
2014-04-24 16:56:36 -07:00
if (hasChanged) {
2014-05-01 17:48:17 -07:00
this.connectToAll();
2014-04-24 19:13:55 -07:00
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
}
2014-04-28 11:15:09 -07:00
this.log('### BROADCASTING PKR');
2014-05-01 17:48:17 -07:00
2014-04-28 11:15:09 -07:00
recipients = null;
this.sendPublicKeyRing(recipients);
2014-04-16 13:50:10 -07:00
}
this.emit('publicKeyRingUpdated');
2014-04-16 13:50:10 -07:00
this.store();
2014-04-14 13:42:10 -07:00
};
2014-04-14 13:17:56 -07:00
2014-04-14 14:30:08 -07:00
2014-04-16 13:50:10 -07:00
Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
this.log('RECV TXPROPOSAL:', data);
2014-04-14 13:17:56 -07:00
2014-04-16 13:50:10 -07:00
var recipients;
var inTxp = copay.TxProposals.fromObj(data.txProposals);
2014-05-15 13:43:41 -07:00
var ids = inTxp.getNtxids();
if (ids.lenght>1) {
this.emit('badMessage', senderId);
this.log('Received BAD TxProposal messsage FROM:', senderId); //TODO
return;
}
var newId = ids[0];
2014-04-16 13:50:10 -07:00
var mergeInfo = this.txProposals.merge(inTxp, true);
var addSeen = this.addSeenToTxProposals();
2014-04-24 16:56:36 -07:00
if (mergeInfo.hasChanged || addSeen) {
this.log('### BROADCASTING txProposals. ');
2014-04-16 13:50:10 -07:00
recipients = null;
2014-05-15 13:43:41 -07:00
this.sendTxProposals(recipients, newId);
}
if (data.lastInBatch) {
this.emit('txProposalsUpdated', this.txProposals);
this.store();
2014-04-24 19:13:55 -07:00
}
2014-04-14 13:42:10 -07:00
};
2014-04-16 13:50:10 -07:00
Wallet.prototype._handleData = function(senderId, data, isInbound) {
2014-04-20 12:16:09 -07:00
// TODO check message signature
if (this.id !== data.walletId) {
2014-04-24 16:56:36 -07:00
this.emit('badMessage', senderId);
this.log('badMessage FROM:', senderId); //TODO
return;
}
2014-04-24 16:56:36 -07:00
switch (data.type) {
2014-04-20 13:16:21 -07:00
// This handler is repeaded on WalletFactory (#join). TODO
case 'walletId':
this.sendWalletReady(senderId);
break;
2014-04-20 12:16:09 -07:00
case 'walletReady':
this.sendPublicKeyRing(senderId);
2014-05-15 12:39:22 -07:00
this.sendTxProposals(senderId); // send old
2014-04-20 12:16:09 -07:00
break;
2014-04-16 13:50:10 -07:00
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
2014-04-24 16:56:36 -07:00
break;
2014-04-16 13:50:10 -07:00
case 'txProposals':
this._handleTxProposals(senderId, data, isInbound);
2014-04-24 16:56:36 -07:00
break;
2014-04-16 13:50:10 -07:00
}
};
2014-04-14 13:42:10 -07:00
2014-05-09 10:35:57 -07:00
Wallet.prototype._handleConnect = function(newCopayerId) {
2014-04-23 12:02:23 -07:00
if (newCopayerId) {
2014-04-25 13:36:00 -07:00
this.log('#### Setting new COPAYER:', newCopayerId);
2014-04-23 09:44:20 -07:00
this.sendWalletId(newCopayerId);
}
2014-05-09 10:35:57 -07:00
var peerID = this.network.peerFromCopayer(newCopayerId)
this.emit('connect', peerID);
2014-04-14 13:42:10 -07:00
};
2014-05-09 10:44:05 -07:00
Wallet.prototype._handleDisconnect = function(peerID) {
this.emit('disconnect', peerID);
2014-05-09 10:35:57 -07:00
};
2014-04-20 12:16:09 -07:00
2014-04-24 16:56:36 -07:00
Wallet.prototype._optsToObj = function() {
2014-04-17 14:02:20 -07:00
var obj = {
id: this.id,
spendUnconfirmed: this.spendUnconfirmed,
requiredCopayers: this.requiredCopayers,
totalCopayers: this.totalCopayers,
name: this.name,
2014-04-30 08:58:40 -07:00
netKey: this.netKey,
2014-05-14 17:02:01 -07:00
version: this.version,
2014-04-17 14:02:20 -07:00
};
return obj;
};
2014-04-18 10:40:16 -07:00
2014-04-23 09:44:20 -07:00
Wallet.prototype.getCopayerId = function(index) {
return this.publicKeyRing.getCopayerId(index || 0);
2014-04-20 08:41:28 -07:00
};
2014-04-18 14:25:51 -07:00
2014-04-23 09:44:20 -07:00
Wallet.prototype.getMyCopayerId = function() {
return this.getCopayerId(0);
2014-04-18 10:40:16 -07:00
};
2014-04-30 08:58:40 -07:00
Wallet.prototype.getSecret = function() {
var i = new Buffer(this.getMyCopayerId(), 'hex');
var k = new Buffer(this.netKey, 'base64');
var b = Buffer.concat([i, k]);
2014-04-30 08:58:40 -07:00
var str = Base58Check.encode(b);
return str;
};
Wallet.decodeSecret = function(secretB) {
var secret = Base58Check.decode(secretB);
var netKeyBuf = secret.slice(-8);
var pubKeyBuf = secret.slice(0, 33);
2014-04-30 08:58:40 -07:00
return {
pubKey: pubKeyBuf.toString('hex'),
netKey: netKeyBuf.toString('base64'),
}
};
2014-04-24 19:13:55 -07:00
Wallet.prototype._lockIncomming = function() {
this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds());
};
2014-04-16 15:18:19 -07:00
Wallet.prototype.netStart = function() {
2014-04-16 13:50:10 -07:00
var self = this;
var net = this.network;
2014-04-17 12:50:48 -07:00
net.removeAllListeners();
2014-05-09 10:35:57 -07:00
net.on('connect', self._handleConnect.bind(self));
net.on('disconnect', self._handleDisconnect.bind(self));
2014-04-24 16:56:36 -07:00
net.on('data', self._handleData.bind(self));
net.on('openError', function() {
2014-04-18 14:25:51 -07:00
self.log('[Wallet.js.132:openError:] GOT openError'); //TODO
self.emit('openError');
});
net.on('close', function() {
self.emit('close');
});
2014-04-20 08:41:28 -07:00
2014-04-23 09:44:20 -07:00
var myId = self.getMyCopayerId();
2014-04-24 16:56:36 -07:00
var startOpts = {
2014-04-23 18:43:17 -07:00
copayerId: myId,
2014-04-24 19:13:55 -07:00
maxPeers: self.totalCopayers,
2014-04-30 08:58:40 -07:00
netKey: this.netKey,
2014-04-20 08:41:28 -07:00
};
2014-04-24 19:13:55 -07:00
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
}
2014-04-24 16:56:36 -07:00
net.start(startOpts, function() {
self.emit('ready', net.getPeer());
setTimeout(function(){
2014-05-16 14:48:17 -07:00
console.log('[EMIT publicKeyRingUpdated:]'); //TODO
self.emit('publicKeyRingUpdated');
2014-05-16 14:48:17 -07:00
console.log('[CONNECT:]'); //TODO
self.connectToAll();
self.emit('txProposalsUpdated');
},10);
2014-04-23 18:43:17 -07:00
});
2014-04-16 13:50:10 -07:00
};
2014-04-15 06:22:50 -07:00
2014-04-24 12:07:49 -07:00
Wallet.prototype.getOnlinePeerIDs = function() {
return this.network.getOnlinePeerIDs();
};
Wallet.prototype.getRegisteredCopayerIds = function() {
var l = this.publicKeyRing.registeredCopayers();
var copayers = [];
for (var i = 0; i < l; i++) {
var cid = this.getCopayerId(i);
copayers.push(cid);
}
return copayers;
};
2014-04-24 12:07:49 -07:00
Wallet.prototype.getRegisteredPeerIds = function() {
2014-05-01 05:41:18 -07:00
var l = this.publicKeyRing.registeredCopayers();
if (this.registeredPeerIds.length !== l) {
this.registeredPeerIds = [];
var copayers = this.getRegisteredCopayerIds();
2014-05-01 05:41:18 -07:00
for (var i = 0; i < l; i++) {
var cid = copayers[i];
2014-05-01 05:41:18 -07:00
var pid = this.network.peerFromCopayer(cid);
this.registeredPeerIds.push({
peerId: pid,
nick: this.publicKeyRing.nicknameForCopayer(cid)
});
}
2014-04-24 12:07:49 -07:00
}
2014-05-01 05:41:18 -07:00
return this.registeredPeerIds;
2014-04-24 12:07:49 -07:00
};
Wallet.prototype.store = function() {
2014-04-17 14:02:20 -07:00
var wallet = this.toObj();
this.storage.setFromObj(this.id, wallet);
this.log('Wallet stored');
2014-04-17 14:02:20 -07:00
};
Wallet.prototype.toObj = function() {
var optsObj = this._optsToObj();
var walletObj = {
opts: optsObj,
publicKeyRing: this.publicKeyRing.toObj(),
2014-04-17 14:02:20 -07:00
txProposals: this.txProposals.toObj(),
privateKey: this.privateKey.toObj()
};
return walletObj;
2014-04-15 06:22:50 -07:00
};
Wallet.fromObj = function(o, storage, network, blockchain) {
var opts = JSON.parse(JSON.stringify(o.opts));
opts.publicKeyRing = copay.PublicKeyRing.fromObj(o.publicKeyRing);
opts.txProposals = copay.TxProposals.fromObj(o.txProposals);
opts.privateKey = copay.PrivateKey.fromObj(o.privateKey);
opts.storage = storage;
opts.network = network;
opts.blockchain = blockchain;
2014-04-17 14:02:20 -07:00
var w = new Wallet(opts);
return w;
};
2014-04-15 08:17:28 -07:00
2014-05-01 14:32:22 -07:00
Wallet.prototype.toEncryptedObj = function() {
var walletObj = this.toObj();
return this.storage.export(walletObj);
};
2014-05-15 12:39:22 -07:00
Wallet.prototype.sendTxProposals = function(recipients, ntxid) {
2014-04-24 16:56:36 -07:00
this.log('### SENDING txProposals TO:', recipients || 'All', this.txProposals);
2014-04-15 08:17:28 -07:00
2014-05-15 13:43:41 -07:00
var toSend = ntxid ? [ntxid] : this.txProposals.getNtxids();
var last = toSend[toSend];
2014-05-15 12:39:22 -07:00
for(var i in toSend) {
var id = toSend[i];
2014-05-15 13:43:41 -07:00
var lastInBatch = (i == toSend.length - 1);
2014-05-15 12:39:22 -07:00
this.network.send(recipients, {
type: 'txProposals',
txProposals: this.txProposals.toObj(id),
walletId: this.id,
2014-05-15 13:43:41 -07:00
lastInBatch: lastInBatch,
2014-05-15 12:39:22 -07:00
});
}
2014-04-15 08:17:28 -07:00
};
2014-04-20 12:16:09 -07:00
Wallet.prototype.sendWalletReady = function(recipients) {
this.log('### SENDING WalletReady TO:', recipients);
2014-04-24 16:56:36 -07:00
this.network.send(recipients, {
type: 'walletReady',
2014-04-20 12:16:09 -07:00
walletId: this.id,
});
};
2014-04-16 16:58:57 -07:00
Wallet.prototype.sendWalletId = function(recipients) {
2014-04-25 13:36:00 -07:00
this.log('### SENDING walletId TO:', recipients || 'All', this.id);
2014-04-16 16:58:57 -07:00
2014-04-24 16:56:36 -07:00
this.network.send(recipients, {
type: 'walletId',
2014-04-16 16:58:57 -07:00
walletId: this.id,
2014-04-18 14:25:51 -07:00
opts: this._optsToObj()
2014-04-16 16:58:57 -07:00
});
};
2014-04-15 08:17:28 -07:00
Wallet.prototype.sendPublicKeyRing = function(recipients) {
2014-04-24 16:56:36 -07:00
this.log('### SENDING publicKeyRing TO:', recipients || 'All', this.publicKeyRing.toObj());
2014-04-15 08:17:28 -07:00
2014-04-24 16:56:36 -07:00
this.network.send(recipients, {
type: 'publicKeyRing',
2014-04-15 08:17:28 -07:00
publicKeyRing: this.publicKeyRing.toObj(),
walletId: this.id,
});
};
Wallet.prototype.getName = function() {
return this.name || this.id;
};
Wallet.prototype._doGenerateAddress = function(isChange) {
return this.publicKeyRing.generateAddress(isChange);
};
Wallet.prototype.generateAddress = function(isChange, cb) {
var addr = this._doGenerateAddress(isChange);
2014-04-16 13:50:10 -07:00
this.sendPublicKeyRing();
this.store();
if (cb) return cb(addr);
2014-04-15 12:50:16 -07:00
return addr;
};
2014-04-15 08:17:28 -07:00
2014-04-22 22:01:54 -07:00
2014-04-15 11:25:55 -07:00
Wallet.prototype.getTxProposals = function() {
var ret = [];
var copayers = this.getRegisteredCopayerIds();
2014-04-24 16:56:36 -07:00
for (var k in this.txProposals.txps) {
var i = this.txProposals.getTxProposal(k, copayers);
2014-04-24 16:56:36 -07:00
i.signedByUs = i.signedBy[this.getMyCopayerId()] ? true : false;
i.rejectedByUs = i.rejectedBy[this.getMyCopayerId()] ? true : false;
if (this.totalCopayers - i.rejectCount < this.requiredCopayers)
i.finallyRejected = true;
2014-04-21 03:27:45 -07:00
2014-04-15 11:25:55 -07:00
ret.push(i);
2014-04-16 13:50:10 -07:00
}
return ret;
};
2014-04-22 22:01:54 -07:00
Wallet.prototype.reject = function(ntxid) {
2014-04-24 16:56:36 -07:00
var myId = this.getMyCopayerId();
2014-04-18 15:28:28 -07:00
var txp = this.txProposals.txps[ntxid];
2014-04-22 22:01:54 -07:00
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
txp.rejectedBy[myId] = Date.now();
2014-05-15 12:39:22 -07:00
this.sendTxProposals(null, ntxid);
this.store();
this.emit('txProposalsUpdated');
2014-04-18 15:28:28 -07:00
};
2014-04-16 13:50:10 -07:00
2014-04-22 22:01:54 -07:00
Wallet.prototype.sign = function(ntxid, cb) {
2014-04-18 15:28:28 -07:00
var self = this;
setTimeout(function() {
var myId = self.getMyCopayerId();
var txp = self.txProposals.txps[ntxid];
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) return;
2014-04-16 13:50:10 -07:00
var pkr = self.publicKeyRing;
var keys = self.privateKey.getAll(pkr.addressIndex, pkr.changeAddressIndex);
2014-04-18 15:28:28 -07:00
var b = txp.builder;
var before = b.signaturesAdded;
b.sign(keys);
2014-04-18 15:28:28 -07:00
var ret = false;
if (b.signaturesAdded > before) {
txp.signedBy[myId] = Date.now();
self.sendTxProposals(null, ntxid);
self.store();
self.emit('txProposalsUpdated');
ret = true;
}
if (cb) return cb(ret);
},10);
2014-04-15 11:25:55 -07:00
};
2014-04-20 17:53:54 -07:00
Wallet.prototype.sendTx = function(ntxid, cb) {
2014-04-18 15:28:28 -07:00
var txp = this.txProposals.txps[ntxid];
if (!txp) return;
var tx = txp.builder.build();
if (!tx.isComplete()) return;
this.log('[Wallet.js.231] BROADCASTING TX!!!'); //TODO
var txHex = tx.serialize().toString('hex');
2014-04-24 16:56:36 -07:00
this.log('[Wallet.js.261:txHex:]', txHex); //TODO
2014-04-18 15:28:28 -07:00
var self = this;
2014-04-21 07:28:25 -07:00
2014-04-18 15:28:28 -07:00
this.blockchain.sendRawTransaction(txHex, function(txid) {
2014-04-24 16:56:36 -07:00
self.log('BITCOND txid:', txid); //TODO
2014-04-18 15:28:28 -07:00
if (txid) {
2014-04-21 03:30:46 -07:00
self.txProposals.setSent(ntxid, txid);
self.sendTxProposals(null, ntxid);
self.store();
2014-04-18 15:28:28 -07:00
}
2014-04-20 17:53:54 -07:00
return cb(txid);
2014-04-18 15:28:28 -07:00
});
};
2014-04-15 11:25:55 -07:00
Wallet.prototype.addSeenToTxProposals = function() {
2014-04-24 16:56:36 -07:00
var ret = false;
var myId = this.getMyCopayerId();
2014-04-16 13:50:10 -07:00
2014-04-24 16:56:36 -07:00
for (var k in this.txProposals.txps) {
2014-04-18 15:28:28 -07:00
var txp = this.txProposals.txps[k];
2014-04-20 16:24:24 -07:00
if (!txp.seenBy[myId]) {
txp.seenBy[myId] = Date.now();
2014-04-15 11:25:55 -07:00
ret = true;
}
2014-04-18 15:28:28 -07:00
}
2014-04-15 11:25:55 -07:00
return ret;
};
2014-04-30 15:50:13 -07:00
// TODO: remove this method and use getAddressesInfo everywhere
2014-05-01 06:07:30 -07:00
Wallet.prototype.getAddresses = function(opts) {
return this.publicKeyRing.getAddresses(opts);
2014-04-15 11:50:22 -07:00
};
2014-05-01 06:07:30 -07:00
Wallet.prototype.getAddressesStr = function(opts) {
return this.getAddresses(opts).map(function(a) {
2014-04-30 08:25:33 -07:00
return a.toString();
2014-04-15 14:23:35 -07:00
});
};
2014-05-01 07:04:21 -07:00
Wallet.prototype.getAddressesInfo = function(opts) {
return this.publicKeyRing.getAddressesInfo(opts);
2014-04-30 15:50:13 -07:00
};
2014-05-01 07:04:21 -07:00
Wallet.prototype.addressIsOwn = function(addrStr, opts) {
var addrList = this.getAddressesStr(opts);
2014-04-18 07:19:39 -07:00
var l = addrList.length;
var ret = false;
2014-04-24 16:56:36 -07:00
for (var i = 0; i < l; i++) {
2014-04-18 07:19:39 -07:00
if (addrList[i] === addrStr) {
2014-04-24 16:56:36 -07:00
ret = true;
2014-04-18 07:19:39 -07:00
break;
}
}
return ret;
};
2014-05-15 13:43:41 -07:00
Wallet.prototype.getBalance = function(cb) {
2014-04-18 09:20:35 -07:00
var balance = 0;
2014-05-15 13:43:41 -07:00
var safeBalance = 0;
2014-04-18 09:20:35 -07:00
var balanceByAddr = {};
var COIN = bitcore.util.COIN;
2014-05-15 13:43:41 -07:00
this.getUnspent(function(safeUnspent, unspent) {
for (var i = 0; i < unspent.length; i++) {
var u = unspent[i];
2014-04-18 09:20:35 -07:00
var amt = u.amount * COIN;
2014-04-30 08:25:33 -07:00
balance += amt;
2014-04-24 16:56:36 -07:00
balanceByAddr[u.address] = (balanceByAddr[u.address] || 0) + amt;
2014-04-17 07:51:56 -07:00
}
2014-04-30 08:25:33 -07:00
// we multiply and divide by COIN to avoid rounding errors when adding
2014-04-24 16:56:36 -07:00
for (var a in balanceByAddr) {
balanceByAddr[a] = balanceByAddr[a] / COIN;
2014-04-21 08:00:14 -07:00
}
2014-04-30 08:25:33 -07:00
balance = balance / COIN;
2014-04-15 14:23:35 -07:00
2014-05-15 13:43:41 -07:00
for (var i = 0; i < safeUnspent.length; i++) {
var u = safeUnspent[i];
var amt = u.amount * COIN;
safeBalance += amt;
}
safeBalance = safeBalance / COIN;
return cb(balance, balanceByAddr, safeBalance);
2014-04-17 12:42:27 -07:00
});
2014-04-15 14:23:35 -07:00
};
2014-05-15 13:43:41 -07:00
Wallet.prototype.getUnspent = function(cb) {
2014-04-21 08:37:32 -07:00
var self = this;
this.blockchain.getUnspent(this.getAddressesStr(), function(unspentList) {
2014-05-15 13:43:41 -07:00
var safeUnspendList = [];
2014-04-22 22:01:54 -07:00
var maxRejectCount = self.totalCopayers - self.requiredCopayers;
var uu = self.txProposals.getUsedUnspent(maxRejectCount);
2014-04-21 08:37:32 -07:00
2014-04-24 16:56:36 -07:00
for (var i in unspentList) {
if (uu.indexOf(unspentList[i].txid) === -1)
2014-05-15 13:43:41 -07:00
safeUnspendList.push(unspentList[i]);
2014-04-21 08:37:32 -07:00
}
2014-05-15 13:43:41 -07:00
return cb(safeUnspendList, unspentList);
2014-04-21 08:37:32 -07:00
});
};
2014-04-16 13:50:10 -07:00
Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
var self = this;
if (typeof opts === 'function') {
cb = opts;
opts = {};
}
opts = opts || {};
if (typeof opts.spendUnconfirmed === 'undefined') {
opts.spendUnconfirmed = this.spendUnconfirmed;
}
2014-04-21 08:37:32 -07:00
2014-05-15 13:43:41 -07:00
self.getUnspent(function(safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, safeUnspent, opts);
2014-05-15 12:39:22 -07:00
if (ntxid) {
self.sendPublicKeyRing();
2014-05-15 12:39:22 -07:00
self.sendTxProposals(null, ntxid);
2014-04-22 18:07:18 -07:00
self.store();
self.emit('txProposalsUpdated');
2014-04-22 18:07:18 -07:00
}
2014-04-16 13:50:10 -07:00
return cb();
});
};
Wallet.prototype.createTxSync = function(toAddress, amountSatStr, utxos, opts) {
2014-04-24 16:56:36 -07:00
var pkr = this.publicKeyRing;
2014-04-15 14:23:35 -07:00
var priv = this.privateKey;
opts = opts || {};
var amountSat = bitcore.bignum(amountSatStr);
2014-04-24 16:56:36 -07:00
if (!pkr.isComplete()) {
2014-04-15 14:23:35 -07:00
throw new Error('publicKeyRing is not complete');
}
if (!opts.remainderOut) {
2014-04-24 16:56:36 -07:00
opts.remainderOut = {
address: this._doGenerateAddress(true).toString()
2014-04-24 16:56:36 -07:00
};
2014-04-21 07:28:25 -07:00
}
2014-04-15 14:23:35 -07:00
var b = new Builder(opts)
.setUnspent(utxos)
.setHashToScriptMap(pkr.getRedeemScriptMap())
2014-04-24 16:56:36 -07:00
.setOutputs([{
address: toAddress,
amountSat: amountSat
}]);
2014-04-15 14:23:35 -07:00
2014-04-24 16:56:36 -07:00
var signRet;
2014-04-15 14:23:35 -07:00
if (priv) {
2014-04-24 16:56:36 -07:00
b.sign(priv.getAll(pkr.addressIndex, pkr.changeAddressIndex));
2014-04-15 14:23:35 -07:00
}
2014-04-23 09:44:20 -07:00
var myId = this.getMyCopayerId();
2014-04-20 16:24:24 -07:00
var now = Date.now();
2014-04-22 22:01:54 -07:00
var me = {};
if (priv && b.signaturesAdded) me[myId] = now;
var meSeen = {};
if (priv) meSeen[myId] = now;
2014-04-20 16:24:24 -07:00
var data = {
2014-04-22 22:01:54 -07:00
signedBy: me,
2014-04-24 16:56:36 -07:00
seenBy: meSeen,
creator: myId,
2014-04-20 16:24:24 -07:00
createdTs: now,
2014-04-15 14:23:35 -07:00
builder: b,
2014-04-20 16:24:24 -07:00
};
2014-05-15 12:39:22 -07:00
return this.txProposals.add(data);
2014-04-15 14:23:35 -07:00
};
2014-04-16 16:58:57 -07:00
Wallet.prototype.disconnect = function() {
2014-04-24 19:13:55 -07:00
this.log('## DISCONNECTING');
2014-04-16 16:58:57 -07:00
this.network.disconnect();
};
2014-04-23 17:20:44 -07:00
Wallet.prototype.getNetwork = function() {
return this.network;
};
2014-04-14 14:30:08 -07:00
module.exports = require('soop')(Wallet);