copay/js/models/core/Wallet.js

846 lines
22 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-06-17 10:28:26 -07:00
var http = require('http');
var EventEmitter = imports.EventEmitter || require('events').EventEmitter;
var async = require('async');
2014-06-18 06:09:40 -07:00
var preconditions = require('preconditions').instance();
2014-06-17 10:28:26 -07:00
2014-04-24 16:56:36 -07:00
var bitcore = require('bitcore');
2014-05-21 12:10:39 -07:00
var bignum = bitcore.Bignum;
2014-04-24 16:56:36 -07:00
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 SecureRandom = bitcore.SecureRandom;
var Base58Check = bitcore.Base58.base58Check;
2014-04-10 13:57:41 -07:00
2014-06-17 10:28:26 -07:00
var AddressIndex = require('./AddressIndex');
var PublicKeyRing = require('./PublicKeyRing');
var TxProposals = require('./TxProposals');
var PrivateKey = require('./PrivateKey');
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-06-02 13:40:29 -07:00
'publicKeyRing', 'txProposals', 'privateKey', 'version',
'reconnectDelay'
2014-04-24 16:56:36 -07:00
].forEach(function(k) {
2014-05-21 07:10:19 -07:00
if (typeof opts[k] === 'undefined')
throw new Error('missing required option for Wallet: ' + k);
2014-04-16 13:50:10 -07:00
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
// Renew token every 24hs
if (opts.tokenTime && new Date().getTime() - opts.tokenTime < 86400000) {
this.token = opts.token;
this.tokenTime = opts.tokenTime;
}
2014-06-17 21:00:32 -07:00
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-06-18 16:18:13 -07:00
this.addressBook = opts.addressBook || {};
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() {
2014-05-01 17:48:17 -07:00
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
}
};
Wallet.prototype._handleIndexes = function(senderId, data, isInbound) {
this.log('RECV INDEXES:', data);
2014-06-17 10:28:26 -07:00
var inIndexes = AddressIndex.fromObj(data.indexes);
var hasChanged = this.publicKeyRing.indexes.merge(inIndexes);
if (hasChanged) {
this.emit('publicKeyRingUpdated');
this.store();
}
};
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
2014-06-17 10:28:26 -07:00
var inPKR = PublicKeyRing.fromObj(data.publicKeyRing);
2014-05-27 13:05:43 -07:00
var wasIncomplete = !this.publicKeyRing.isComplete();
var hasChanged;
2014-06-13 07:30:48 -07:00
try {
hasChanged = this.publicKeyRing.merge(inPKR, true);
2014-06-13 07:30:48 -07:00
} catch (e) {
2014-06-12 10:27:53 -07:00
this.log('## WALLET ERROR', e); //TODO
this.emit('connectionError', e.message);
return;
}
2014-04-18 14:25:51 -07:00
2014-04-24 16:56:36 -07:00
if (hasChanged) {
2014-05-27 13:05:43 -07:00
if (wasIncomplete) {
this.sendPublicKeyRing();
}
2014-04-24 19:13:55 -07:00
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
}
this.emit('publicKeyRingUpdated');
this.store();
2014-04-16 13:50:10 -07:00
}
2014-04-14 13:42:10 -07:00
};
2014-04-14 13:17:56 -07:00
2014-04-14 14:30:08 -07:00
Wallet.prototype._handleTxProposal = function(senderId, data) {
this.log('RECV TXPROPOSAL:', data);
2014-04-14 13:17:56 -07:00
var inTxp = TxProposals.TxProposal.fromObj(data.txProposal);
2014-06-17 12:42:49 -07:00
var mergeInfo = this.txProposals.merge(inTxp);
var added = this.addSeenToTxProposals();
this.emit('txProposalsUpdated');
this.store();
2014-05-15 13:43:41 -07:00
for (var i = 0; i < mergeInfo.events.length; i++) {
this.emit('txProposalEvent', mergeInfo.events[i]);
}
2014-04-14 13:42:10 -07:00
};
2014-06-18 16:18:13 -07:00
Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
this.log('RECV ADDRESSBOOK:', data);
var rcv = data.addressBook;
var hasChange;
for(var key in rcv) {
if (!this.addressBook[key]) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
else {
if (rcv[key].createdTs > this.addressBook[key].createdTs) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
}
}
if (hasChange) {
this.emit('addressBookUpdated');
this.store();
}
};
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
2014-06-13 07:30:48 -07:00
if (data.type !== 'walletId' && 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-06-18 16:18:13 -07:00
this.sendAddressBook(senderId);
2014-06-18 06:09:40 -07:00
this.sendAllTxProposals(senderId); // send old txps
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;
case 'txProposal':
this._handleTxProposal(senderId, data, isInbound);
2014-04-24 16:56:36 -07:00
break;
case 'indexes':
this._handleIndexes(senderId, data, isInbound);
break;
2014-06-18 16:18:13 -07:00
case 'addressbook':
this._handleAddressBook(senderId, data, isInbound);
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) {
2014-06-09 16:08:12 -07:00
this.currentDelay = null;
2014-05-09 10:44:05 -07:00
this.emit('disconnect', peerID);
2014-05-09 10:35:57 -07:00
};
2014-04-20 12:16:09 -07:00
2014-06-09 14:01:15 -07:00
Wallet.prototype.getNetworkName = function() {
return this.publicKeyRing.network.name;
};
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-05-14 17:02:01 -07:00
version: this.version,
2014-04-17 14:02:20 -07:00
};
2014-06-03 07:54:07 -07:00
if (this.token) {
obj.token = this.token;
obj.tokenTime = new Date().getTime();
}
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() {
2014-06-19 11:03:31 -07:00
return this.getCopayerId(0); //copayer id is hex of a public key
2014-04-18 10:40:16 -07:00
};
Wallet.prototype.getMyCopayerIdPriv = function() {
2014-06-19 11:03:31 -07:00
return this.privateKey.getIdPriv(); //copayer idpriv is hex of a private key
};
2014-04-30 08:58:40 -07:00
Wallet.prototype.getSecret = function() {
var pubkeybuf = new Buffer(this.getMyCopayerId(), 'hex');
var str = Base58Check.encode(pubkeybuf);
2014-04-30 08:58:40 -07:00
return str;
};
Wallet.decodeSecret = function(secretB) {
var secret = Base58Check.decode(secretB);
var pubKeyBuf = secret.slice(0, 33);
2014-04-30 08:58:40 -07:00
return {
pubKey: pubKeyBuf.toString('hex')
2014-04-30 08:58:40 -07:00
}
};
2014-04-24 19:13:55 -07:00
Wallet.prototype._lockIncomming = function() {
this.network.lockIncommingConnections(this.publicKeyRing.getAllCopayerIds());
};
2014-06-19 11:03:31 -07:00
Wallet.prototype.netStart = function(callback) {
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('close', function() {
self.emit('close');
});
net.on('serverError', function(msg) {
self.emit('serverError', msg);
});
2014-04-20 08:41:28 -07:00
2014-04-23 09:44:20 -07:00
var myId = self.getMyCopayerId();
var myIdPriv = self.getMyCopayerIdPriv();
2014-04-24 16:56:36 -07:00
var startOpts = {
2014-04-23 18:43:17 -07:00
copayerId: myId,
privkey: myIdPriv,
token: self.token,
maxPeers: self.totalCopayers
2014-04-20 08:41:28 -07:00
};
2014-04-24 19:13:55 -07:00
if (this.publicKeyRing.isComplete()) {
this._lockIncomming();
}
2014-06-09 07:41:13 -07:00
2014-04-24 16:56:36 -07:00
net.start(startOpts, function() {
self.emit('ready', net.getPeer());
self.token = net.peer.options.token;
2014-06-03 07:54:07 -07:00
setTimeout(function() {
self.emit('publicKeyRingUpdated', true);
2014-06-02 13:40:29 -07:00
self.scheduleConnect();
self.emit('txProposalsUpdated');
self.store();
2014-06-03 07:54:07 -07:00
}, 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-06-02 13:40:29 -07:00
Wallet.prototype.scheduleConnect = function() {
var self = this;
2014-06-03 07:54:07 -07:00
if (self.network.isOnline()) {
self.connectToAll();
2014-06-13 07:30:48 -07:00
self.currentDelay = self.currentDelay * 2 || self.reconnectDelay;
2014-06-09 16:08:12 -07:00
setTimeout(self.scheduleConnect.bind(self), self.currentDelay);
2014-06-03 07:54:07 -07:00
}
2014-06-02 13:40:29 -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(),
2014-06-17 21:00:32 -07:00
privateKey: this.privateKey ? this.privateKey.toObj() : undefined,
addressBook: this.addressBook
2014-04-17 14:02:20 -07:00
};
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));
2014-06-17 21:00:32 -07:00
opts.addressBook = o.addressBook;
2014-06-17 10:28:26 -07:00
opts.publicKeyRing = PublicKeyRing.fromObj(o.publicKeyRing);
opts.txProposals = TxProposals.fromObj(o.txProposals);
opts.privateKey = 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();
delete walletObj.opts.token;
delete walletObj.opts.tokenTime;
2014-05-01 14:32:22 -07:00
return this.storage.export(walletObj);
};
2014-06-18 06:09:40 -07:00
Wallet.prototype.sendAllTxProposals = function(recipients) {
var ntxids = this.txProposals.getNtxids();
for (var i in ntxids) {
var ntxid = ntxids[i];
this.sendTxProposal(ntxid, recipients);
2014-05-15 12:39:22 -07:00
}
2014-04-15 08:17:28 -07:00
};
2014-06-18 06:09:40 -07:00
Wallet.prototype.sendTxProposal = function(ntxid, recipients) {
preconditions.checkArgument(ntxid);
2014-06-18 06:21:26 -07:00
preconditions.checkState(this.txProposals.txps[ntxid]);
2014-06-18 06:09:40 -07:00
this.log('### SENDING txProposal '+ntxid+' TO:', recipients || 'All', this.txProposals);
this.network.send(recipients, {
type: 'txProposal',
2014-06-18 06:09:40 -07:00
txProposal: this.txProposals.txps[ntxid].toObj(),
walletId: this.id,
});
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,
opts: this._optsToObj(),
networkName: this.getNetworkName(),
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-06-09 06:30:37 -07:00
var publicKeyRing = this.publicKeyRing.toObj();
delete publicKeyRing.publicKeysCache; // exclude publicKeysCache from network obj
2014-04-15 08:17:28 -07:00
2014-04-24 16:56:36 -07:00
this.network.send(recipients, {
type: 'publicKeyRing',
2014-06-09 06:30:37 -07:00
publicKeyRing: publicKeyRing,
2014-04-15 08:17:28 -07:00
walletId: this.id,
});
};
Wallet.prototype.sendIndexes = function(recipients) {
this.log('### INDEXES TO:', recipients || 'All', this.publicKeyRing.indexes.toObj());
this.network.send(recipients, {
type: 'indexes',
indexes: this.publicKeyRing.indexes.toObj(),
walletId: this.id,
});
};
2014-04-15 08:17:28 -07:00
2014-06-18 16:18:13 -07:00
Wallet.prototype.sendAddressBook = function(recipients) {
this.log('### SENDING addressBook TO:', recipients || 'All', this.addressBook);
this.network.send(recipients, {
type: 'addressbook',
addressBook: this.addressBook,
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);
this.sendIndexes();
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-06-18 06:21:26 -07:00
for (var ntxid in this.txProposals.txps) {
var txp = this.txProposals.getTxProposal(ntxid, copayers);
2014-06-17 10:31:15 -07:00
txp.signedByUs = txp.signedBy[this.getMyCopayerId()] ? true : false;
txp.rejectedByUs = txp.rejectedBy[this.getMyCopayerId()] ? true : false;
if (this.totalCopayers - txp.rejectCount < this.requiredCopayers) {
txp.finallyRejected = true;
}
2014-04-21 03:27:45 -07:00
2014-06-17 10:31:15 -07:00
ret.push(txp);
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-06-04 12:28:37 -07:00
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
2014-06-13 07:30:48 -07:00
throw new Error('Invalid transaction to reject: ' + ntxid);
2014-06-04 12:28:37 -07:00
}
2014-04-22 22:01:54 -07:00
txp.rejectedBy[myId] = Date.now();
2014-06-18 06:09:40 -07:00
this.sendTxProposal(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];
2014-05-23 13:22:24 -07:00
if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
if (cb) cb(false);
}
2014-04-16 13:50:10 -07:00
var pkr = self.publicKeyRing;
2014-05-30 11:07:52 -07:00
var keys = self.privateKey.getForPaths(txp.inputChainPaths);
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();
2014-06-18 06:09:40 -07:00
self.sendTxProposal(ntxid);
self.store();
self.emit('txProposalsUpdated');
ret = true;
}
if (cb) return cb(ret);
2014-06-03 07:54:07 -07:00
}, 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;
2014-06-09 13:52:08 -07:00
this.log('Broadcasting Transaction');
2014-04-18 15:28:28 -07:00
2014-05-23 13:22:24 -07:00
var scriptSig = tx.ins[0].getScript();
var size = scriptSig.serialize().length;
2014-04-18 15:28:28 -07:00
var txHex = tx.serialize().toString('hex');
2014-06-09 13:52:08 -07:00
this.log('Raw transaction: ', txHex);
2014-04-18 15:28:28 -07:00
var self = this;
this.blockchain.sendRawTransaction(txHex, function(txid) {
2014-06-09 13:52:08 -07:00
self.log('BITCOIND txid:', txid);
2014-04-18 15:28:28 -07:00
if (txid) {
2014-04-21 03:30:46 -07:00
self.txProposals.setSent(ntxid, txid);
2014-06-18 06:09:40 -07:00
self.sendTxProposal(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;
};
//retunrs values in SATOSHIs
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 = {};
2014-06-12 13:42:26 -07:00
var COIN = coinUtil.COIN;
2014-05-15 13:43:41 -07:00
this.getUnspent(function(err, safeUnspent, unspent) {
if (err) {
return cb(err);
}
2014-05-15 13:43:41 -07:00
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
2014-06-12 13:42:26 -07:00
// we multiply and divide by BIT to avoid rounding errors when adding
2014-04-24 16:56:36 -07:00
for (var a in balanceByAddr) {
balanceByAddr[a] = parseInt(balanceByAddr[a].toFixed(0));
2014-04-21 08:00:14 -07:00
}
balance = parseInt(balance.toFixed(0));
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 = parseInt(safeBalance.toFixed(0));
return cb(null, 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(err, unspentList) {
if (err) {
return cb(err);
}
2014-04-21 08:37:32 -07:00
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) {
2014-06-13 07:30:48 -07:00
var u = unspentList[i];
if (!uu[u.txid + ',' + u.vout])
2014-06-05 10:55:19 -07:00
safeUnspendList.push(u);
2014-04-21 08:37:32 -07:00
}
return cb(null, safeUnspendList, unspentList);
2014-04-21 08:37:32 -07:00
});
};
Wallet.prototype.createTx = function(toAddress, amountSatStr, comment, opts, cb) {
2014-04-16 13:50:10 -07:00
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
this.getUnspent(function(err, safeUnspent) {
var ntxid = self.createTxSync(toAddress, amountSatStr, comment, safeUnspent, opts);
2014-05-15 12:39:22 -07:00
if (ntxid) {
self.sendIndexes();
2014-06-18 06:09:40 -07:00
self.sendTxProposal(ntxid);
2014-04-22 18:07:18 -07:00
self.store();
self.emit('txProposalsUpdated');
2014-04-22 18:07:18 -07:00
}
2014-05-21 11:14:48 -07:00
return cb(ntxid);
2014-04-16 13:50:10 -07:00
});
};
Wallet.prototype.createTxSync = function(toAddress, amountSatStr, comment, 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 || {};
2014-05-21 12:10:39 -07:00
var amountSat = bignum(amountSatStr);
2014-04-15 14:23:35 -07:00
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 (comment && comment.length > 100) {
throw new Error("comment can't be longer that 100 characters");
}
2014-04-15 14:23:35 -07:00
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-05-30 11:07:52 -07:00
var selectedUtxos = b.getSelectedUnspent();
2014-06-03 07:54:07 -07:00
2014-05-30 11:07:52 -07:00
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
2014-04-15 14:23:35 -07:00
if (priv) {
2014-05-30 11:07:52 -07:00
var keys = priv.getForPaths(inputChainPaths);
var signed = b.sign(keys);
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-05-30 11:07:52 -07:00
inputChainPaths: inputChainPaths,
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,
comment: comment
2014-04-20 16:24:24 -07:00
};
2014-05-30 11:07:52 -07:00
var ntxid = this.txProposals.add(data);
return ntxid;
2014-04-15 14:23:35 -07:00
};
Wallet.prototype.updateIndexes = function(callback) {
var self = this;
var start = self.publicKeyRing.indexes.changeIndex;
self.indexDiscovery(start, true, 20, function(err, changeIndex) {
if (err) return callback(err);
if (changeIndex != -1)
self.publicKeyRing.indexes.changeIndex = changeIndex + 1;
start = self.publicKeyRing.indexes.receiveIndex;
self.indexDiscovery(start, false, 20, function(err, receiveIndex) {
if (err) return callback(err);
if (receiveIndex != -1)
self.publicKeyRing.indexes.receiveIndex = receiveIndex + 1;
self.emit('publicKeyRingUpdated');
self.store();
callback();
});
});
}
Wallet.prototype.deriveAddresses = function(index, amout, isChange) {
var ret = new Array(amout);
for(var i = 0; i < amout; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange).toString();
}
return ret;
}
// This function scans the publicKeyRing branch starting at index @start and reports the index with last activity,
// using a scan window of @gap. The argument @change defines the branch to scan: internal or external.
// Returns -1 if no activity is found in range.
Wallet.prototype.indexDiscovery = function(start, change, gap, cb) {
var scanIndex = start;
var lastActive = -1;
var hasActivity = false;
var self = this;
async.doWhilst(
function _do(next) {
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change);
self.blockchain.checkActivity(addresses, function(err, actives){
if (err) throw err;
// Check for new activities in the newlly scanned addresses
var recentActive = actives.reduce(function(r, e, i) {
return e ? scanIndex + i : r;
}, lastActive);
hasActivity = lastActive != recentActive;
lastActive = recentActive;
scanIndex += scanWindow;
next();
});
},
function _while() { return hasActivity; },
function _finnaly(err) {
if (err) return cb(err);
cb(null, lastActive);
}
);
}
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-06-18 16:18:13 -07:00
Wallet.prototype._checkAddressBook = function(key) {
if (this.addressBook[key] && this.addressBook[key].copayerId != -1) {
throw new Error('This address already exists in your Address Book: ' + address);
2014-06-17 21:00:32 -07:00
}
};
2014-06-18 16:18:13 -07:00
Wallet.prototype.setAddressBook = function(key, label) {
this._checkAddressBook(key);
var addressbook = {
createdTs: Date.now(),
copayerId: this.getMyCopayerId(),
label: label
};
this.addressBook[key] = addressbook;
this.sendAddressBook();
2014-06-17 21:00:32 -07:00
this.store();
};
2014-06-18 16:18:13 -07:00
Wallet.prototype.deleteAddressBook = function(key) {
if (key) {
this.addressBook[key].copayerId = -1;
this.addressBook[key].createdTs = Date.now();
this.sendAddressBook();
2014-06-17 21:00:32 -07:00
this.store();
}
};
2014-04-14 14:30:08 -07:00
module.exports = require('soop')(Wallet);