copay/js/models/core/Wallet.js

1580 lines
40 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-24 10:40:03 -07:00
var preconditions = require('preconditions').singleton();
var parseBitcoinURI = require('./HDPath').parseBitcoinURI;
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-07-04 07:40:07 -07:00
var Address = bitcore.Address;
var PayPro = bitcore.PayPro;
var Transaction = bitcore.Transaction;
2014-04-10 13:57:41 -07:00
2014-07-29 07:23:58 -07:00
var HDParams = require('./HDParams');
2014-06-17 10:28:26 -07:00
var PublicKeyRing = require('./PublicKeyRing');
var TxProposal = require('./TxProposal');
2014-07-31 21:09:46 -07:00
var TxProposals = require('./TxProposals');
2014-06-17 10:28:26 -07:00
var PrivateKey = require('./PrivateKey');
2014-07-08 08:34:49 -07:00
var copayConfig = require('../../../config');
2014-06-17 10:28:26 -07:00
if (typeof angular !== 'undefined') {
var $http = angular.bootstrap().get('$http');
}
var $http = function $http(options, callback) {
if (typeof options === 'string') {
options = { uri: options };
}
options.method = options.method || 'GET';
options.headers = options.headers || {};
var ret = {
success: function(cb) {
this._success = cb;
return this;
},
error: function(cb) {
this._error = cb;
return this;
},
_success: function() {
;
},
_error: function(_, err) {
throw err;
}
};
var method = (options.method || 'GET').toUpperCase();
var uri = options.uri || options.url;
var req = options;
req.headers = req.headers || {};
req.body = req.body || {};
if (typeof XMLHttpRequest !== 'undefined') {
var xhr = new XMLHttpRequest();
xhr.open(method, uri, true);
Object.keys(options.headers).forEach(function(key) {
var val = options.headers[key];
if (key === 'Content-Length') return;
if (key === 'Content-Transfer-Encoding') return;
xhr.setRequestHeader(key, val);
});
// For older browsers:
// xhr.overrideMimeType('text/plain; charset=x-user-defined');
// Newer browsers:
xhr.responseType = 'arraybuffer';
xhr.onload = function(event) {
var response = xhr.response;
var buf = new Uint8Array(response);
// return callback(null, xhr, buf);
var headers = {};
(xhr.getAllResponseHeaders() || '').replace(
/(?:\r?\n|^)([^:\r\n]+): *([^\r\n]+)/g,
function($0, $1, $2) {
headers[$1.toLowerCase()] = $2;
}
);
return ret._success(buf, xhr.status, headers, options);
};
xhr.onerror = function(event) {
return ret._error(null, event, null, options);
};
if (options.body) {
xhr.send(options.body);
} else {
xhr.send(null);
}
return ret;
}
// require('request')(options, callback);
return ret;
}
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];
});
if (copayConfig.forceNetwork && this.getNetworkName() !== copayConfig.networkName)
throw new Error('Network forced to ' + copayConfig.networkName +
2014-07-30 17:20:08 -07:00
' and tried to create a Wallet with network ' + this.getNetworkName());
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
2014-08-12 12:26:15 -07:00
this.ignoreLock = opts.ignoreLock;
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 || {};
this.publicKey = this.privateKey.publicHex;
//network nonces are 8 byte buffers, representing a big endian number
//one nonce for oneself, and then one nonce for each copayer
this.network.setHexNonce(opts.networkNonce);
this.network.setHexNonces(opts.networkNonces);
2014-04-14 13:42:10 -07:00
}
2014-04-10 13:57:41 -07:00
2014-08-03 18:34:47 -07:00
Wallet.builderOpts = {
2014-07-30 17:20:08 -07:00
lockTime: null,
signhash: bitcore.Transaction.SIGNHASH_ALL,
fee: null,
feeSat: null,
};
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
}
};
2014-08-12 12:26:15 -07:00
Wallet.prototype.getLock = function() {
return this.storage.getLock(this.id);
};
2014-08-12 12:26:15 -07:00
Wallet.prototype.setLock = function() {
return this.storage.setLock(this.id);
};
2014-08-12 12:26:15 -07:00
Wallet.prototype.unlock = function() {
this.storage.removeLock(this.id);
};
2014-08-12 12:26:15 -07:00
Wallet.prototype.checkAndLock = function() {
if (this.getLock()) {
return true;
}
this.setLock();
return false;
};
Wallet.prototype._handleIndexes = function(senderId, data, isInbound) {
this.log('RECV INDEXES:', data);
2014-07-29 07:23:58 -07:00
var inIndexes = HDParams.fromList(data.indexes);
var hasChanged = this.publicKeyRing.mergeIndexes(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-08-13 07:16:07 -07:00
this.log('## WALLET ERROR', e);
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
2014-08-03 19:57:23 -07:00
Wallet.prototype._processProposalEvents = function(senderId, m) {
var ev;
if (m) {
if (m.new) {
2014-08-03 18:34:47 -07:00
ev = {
type: 'new',
cid: senderId
}
2014-08-05 12:25:02 -07:00
} else if (m.newCopayer) {
ev = {
2014-08-03 19:57:23 -07:00
type: 'signed',
2014-08-05 12:25:02 -07:00
cid: m.newCopayer
2014-08-03 19:57:23 -07:00
};
}
} else {
2014-08-03 18:34:47 -07:00
ev = {
type: 'corrupt',
cId: senderId,
};
}
2014-08-03 19:57:23 -07:00
2014-08-03 18:34:47 -07:00
if (ev)
this.emit('txProposalEvent', ev);
};
/* OTDO
events.push({
type: 'signed',
cId: k,
txId: ntxid
});
*/
2014-08-04 08:43:21 -07:00
Wallet.prototype._getKeyMap = function(txp) {
2014-08-04 13:12:53 -07:00
preconditions.checkArgument(txp);
2014-08-03 18:34:47 -07:00
2014-08-04 13:12:53 -07:00
var keyMap = this.publicKeyRing.copayersForPubkeys(txp._inputSignatures[0], txp.inputChainPaths);
2014-08-04 08:43:21 -07:00
var inSig = JSON.stringify(txp._inputSignatures[0].sort());
if (JSON.stringify(Object.keys(keyMap).sort()) !== inSig) {
throw new Error('inputSignatures dont match know copayers pubkeys');
}
2014-08-03 18:34:47 -07:00
var keyMapStr = JSON.stringify(keyMap);
// All inputs must be signed with the same copayers
2014-08-04 08:43:21 -07:00
for (var i in txp._inputSignatures) {
2014-08-03 18:34:47 -07:00
if (!i) continue;
2014-08-04 08:43:21 -07:00
var inSigX = JSON.stringify(txp._inputSignatures[i].sort());
if (inSigX !== inSig)
throw new Error('found inputs with different signatures:');
}
2014-08-04 08:43:21 -07:00
return keyMap;
};
2014-08-05 12:25:02 -07:00
Wallet.prototype._checkSentTx = function(ntxid, cb) {
var txp = this.txProposals.get(ntxid);
var tx = txp.builder.build();
this.blockchain.checkSentTx(tx, function(err, txid) {
var ret = false;
if (txid) {
txp.setSent(txid);
ret = txid;
}
return cb(ret);
});
};
Wallet.prototype._handleTxProposal = function(senderId, data) {
2014-08-05 12:25:02 -07:00
var self = this;
this.log('RECV TXPROPOSAL: ', data);
2014-08-03 18:34:47 -07:00
var m;
2014-07-30 17:20:08 -07:00
try {
2014-08-05 12:25:02 -07:00
m = this.txProposals.merge(data.txProposal, Wallet.builderOpts);
var keyMap = this._getKeyMap(m.txp);
ret.newCopayer = m.txp.setCopayers(senderId, keyMap);
2014-08-03 18:34:47 -07:00
2014-07-30 17:20:08 -07:00
} catch (e) {
2014-08-04 13:12:53 -07:00
this.log('Corrupt TX proposal received from:', senderId, e);
}
2014-08-03 18:34:47 -07:00
if (m) {
2014-08-05 12:25:02 -07:00
if (m.hasChanged) {
this.sendSeen(m.ntxid);
var tx = m.txp.builder.build();
if (tx.isComplete()) {
this._checkSentTx(m.ntxid, function(ret) {
if (ret) {
self.emit('txProposalsUpdated');
self.store();
}
});
} else {
this.sendTxProposal(m.ntxid);
}
}
2014-08-03 18:34:47 -07:00
this.emit('txProposalsUpdated');
this.store();
}
this._processProposalEvents(senderId, m);
};
2014-05-15 13:43:41 -07:00
Wallet.prototype._handleReject = function(senderId, data, isInbound) {
2014-08-04 08:43:21 -07:00
preconditions.checkState(data.ntxid);
this.log('RECV REJECT:', data);
2014-08-03 18:34:47 -07:00
2014-08-05 12:25:02 -07:00
var txp = this.txProposals.get(data.ntxid);
2014-08-03 18:34:47 -07:00
if (!txp)
2014-08-04 08:43:21 -07:00
throw new Error('Received Reject for an unknown TX from:' + senderId);
2014-08-03 18:34:47 -07:00
if (txp.signedBy[senderId])
throw new Error('Received Reject for an already signed TX from:' + senderId);
txp.setRejected(senderId);
this.store();
2014-08-03 18:34:47 -07:00
this.emit('txProposalsUpdated');
this.emit('txProposalEvent', {
type: 'rejected',
cId: senderId,
txId: data.ntxid,
});
};
Wallet.prototype._handleSeen = function(senderId, data, isInbound) {
2014-08-04 08:43:21 -07:00
preconditions.checkState(data.ntxid);
this.log('RECV SEEN:', data);
2014-08-04 08:43:21 -07:00
2014-08-05 12:25:02 -07:00
var txp = this.txProposals.get(data.ntxid);
2014-08-04 08:43:21 -07:00
txp.setSeen(senderId);
this.store();
2014-08-03 18:34:47 -07:00
this.emit('txProposalsUpdated');
this.emit('txProposalEvent', {
type: 'seen',
cId: senderId,
txId: data.ntxid,
});
2014-04-14 13:42:10 -07:00
};
2014-06-18 16:18:13 -07:00
Wallet.prototype._handleAddressBook = function(senderId, data, isInbound) {
2014-08-04 08:43:21 -07:00
preconditions.checkState(data.addressBook);
2014-06-18 16:18:13 -07:00
this.log('RECV ADDRESSBOOK:', data);
var rcv = data.addressBook;
var hasChange;
for (var key in rcv) {
2014-06-18 16:18:13 -07:00
if (!this.addressBook[key]) {
2014-07-07 16:01:50 -07:00
var isVerified = this.verifyAddressbookEntry(rcv[key], senderId, key);
if (isVerified) {
this.addressBook[key] = rcv[key];
hasChange = true;
}
2014-06-18 16:18:13 -07:00
}
}
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);
2014-08-13 07:16:07 -07:00
this.log('badMessage FROM:', senderId);
return;
}
2014-08-13 07:16:07 -07:00
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);
2014-07-30 17:20:08 -07:00
break;
2014-04-20 12:16:09 -07:00
case 'walletReady':
this.sendPublicKeyRing(senderId);
2014-07-30 17:20:08 -07:00
this.sendAddressBook(senderId);
this.sendAllTxProposals(senderId); // send old txps
break;
2014-04-16 13:50:10 -07:00
case 'publicKeyRing':
this._handlePublicKeyRing(senderId, data, isInbound);
2014-07-30 17:20:08 -07:00
break;
case 'reject':
this._handleReject(senderId, data, isInbound);
2014-08-03 18:34:47 -07:00
break;
case 'seen':
2014-08-03 18:34:47 -07:00
this._handleSeen(senderId, data, isInbound);
break;
case 'txProposal':
this._handleTxProposal(senderId, data, isInbound);
2014-07-30 17:20:08 -07:00
break;
case 'indexes':
this._handleIndexes(senderId, data, isInbound);
2014-07-30 17:20:08 -07:00
break;
2014-06-18 16:18:13 -07:00
case 'addressbook':
this._handleAddressBook(senderId, data, isInbound);
2014-07-30 17:20:08 -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) {
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
};
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-07-03 07:37:26 -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-08-12 12:26:15 -07:00
if (this.checkAndLock() && !this.ignoreLock) {
this.emit('locked');
return;
}
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,
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());
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');
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,
copayerId: cid,
2014-07-07 13:00:26 -07:00
nick: this.publicKeyRing.nicknameForCopayer(cid),
index: i,
2014-05-01 05:41:18 -07:00
});
}
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 networkNonce = this.network.getHexNonce();
var networkNonces = this.network.getHexNonces();
2014-04-17 14:02:20 -07:00
var walletObj = {
opts: optsObj,
networkNonce: networkNonce, //yours
networkNonces: networkNonces, //copayers
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,
2014-06-25 13:14:12 -07:00
addressBook: this.addressBook,
2014-04-17 14:02:20 -07:00
};
return walletObj;
2014-04-15 06:22:50 -07:00
};
// fromObj => from a trusted source
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-25 13:14:12 -07:00
2014-06-17 10:28:26 -07:00
opts.publicKeyRing = PublicKeyRing.fromObj(o.publicKeyRing);
2014-07-31 21:09:46 -07:00
opts.txProposals = TxProposals.fromObj(o.txProposals, Wallet.builderOpts);
2014-06-17 10:28:26 -07:00
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();
return this.storage.export(walletObj);
};
Wallet.prototype.send = function(recipients, obj) {
this.network.send(recipients, obj);
};
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-08-04 13:12:53 -07:00
this.log('### SENDING txProposal ' + ntxid + ' TO:', recipients || 'All', this.txProposals);
this.send(recipients, {
type: 'txProposal',
2014-08-05 12:25:02 -07:00
txProposal: this.txProposals.get(ntxid).toObjTrim(),
walletId: this.id,
});
2014-04-15 08:17:28 -07:00
};
Wallet.prototype.sendSeen = function(ntxid) {
preconditions.checkArgument(ntxid);
this.log('### SENDING seen: ' + ntxid + ' TO: All');
this.send(null, {
type: 'seen',
ntxid: ntxid,
walletId: this.id,
});
};
Wallet.prototype.sendReject = function(ntxid) {
preconditions.checkArgument(ntxid);
this.log('### SENDING reject: ' + ntxid + ' TO: All');
this.send(null, {
type: 'reject',
ntxid: ntxid,
walletId: this.id,
});
};
2014-04-20 12:16:09 -07:00
Wallet.prototype.sendWalletReady = function(recipients) {
this.log('### SENDING WalletReady TO:', recipients);
this.send(recipients, {
2014-04-24 16:56:36 -07:00
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
this.send(recipients, {
2014-04-24 16:56:36 -07:00
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
this.send(recipients, {
2014-04-24 16:56:36 -07:00
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) {
2014-07-29 07:23:58 -07:00
var indexes = HDParams.serialize(this.publicKeyRing.indexes);
this.log('### INDEXES TO:', recipients || 'All', indexes);
this.send(recipients, {
type: 'indexes',
indexes: indexes,
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.send(recipients, {
2014-06-18 16:18:13 -07:00
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, this.publicKey);
};
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-07-31 21:09:46 -07:00
Wallet.prototype.getTxProposals = function() {
2014-04-15 11:25:55 -07:00
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
if (txp.readonly && !txp.finallyRejected && !txp.sentTs) {} else {
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-08-05 12:25:02 -07:00
var txp = this.txProposals.reject(ntxid, this.getMyCopayerId());
this.sendReject(ntxid);
this.store();
this.emit('txProposalsUpdated');
2014-04-18 15:28:28 -07:00
};
2014-04-16 13:50:10 -07:00
Wallet.prototype.sign = function(ntxid, cb) {
preconditions.checkState(typeof this.getMyCopayerId() !== 'undefined');
2014-04-18 15:28:28 -07:00
var self = this;
setTimeout(function() {
var myId = self.getMyCopayerId();
2014-08-05 12:25:02 -07:00
var txp = self.txProposals.get(ntxid);
// if (!txp || txp.rejectedBy[myId] || txp.signedBy[myId]) {
// if (cb) cb(false);
// }
//
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 = txp.countSignatures();
b.sign(keys);
2014-04-18 15:28:28 -07:00
var ret = false;
if (txp.countSignatures() > 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-18 15:28:28 -07:00
2014-08-05 12:25:02 -07:00
Wallet.prototype.sendTx = function(ntxid, cb) {
var txp = this.txProposals.get(ntxid);
if (txp.merchant) {
return this.sendPaymentTx(ntxid, cb);
}
2014-04-18 15:28:28 -07:00
var tx = txp.builder.build();
2014-08-05 12:25:02 -07:00
if (!tx.isComplete())
throw new Error('Tx is not complete. Can not broadcast');
2014-06-09 13:52:08 -07:00
this.log('Broadcasting Transaction');
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-08-05 12:25:02 -07:00
self.txProposals.get(ntxid).setSent(txid);
2014-06-18 06:09:40 -07:00
self.sendTxProposal(ntxid);
self.store();
2014-08-05 12:25:02 -07:00
return cb(txid);
} else {
self.log('Sent failed. Checking is the TX was sent already');
self._checkSentTx(ntxid, function(txid) {
if (txid)
self.store();
return cb(txid);
});
2014-04-18 15:28:28 -07:00
}
});
};
2014-04-15 11:25:55 -07:00
Wallet.prototype.createPaymentTx = function(options, cb) {
var self = this;
if (typeof options === 'string') {
options = { uri: options };
}
if (options.uri.indexOf('bitcoin:') === 0) {
options.uri = parseBitcoinURI(options.uri).merchant;
if (!options.uri) {
return cb(new Error('No URI.'));
}
}
return $http({
method: options.method || 'POST',
url: options.uri || options.url,
headers: {
'Accept': PayPro.PAYMENT_REQUEST_CONTENT_TYPE
+ ', ' + PayPro.PAYMENT_ACK_CONTENT_TYPE,
'Content-Type': 'application/octet-stream',
'Content-Length': 0
},
responseType: 'arraybuffer'
})
.success(function(data, status, headers, config) {
data = PayPro.PaymentRequest.decode(data);
var pr = new PayPro();
pr = pr.makePaymentRequest(data);
return self.receivePaymentRequest(options, pr, cb);
})
.error(function(data, status, headers, config) {
return cb(new Error('Status: ' + JSON.stringify(status)));
});
};
Wallet.prototype.receivePaymentRequest = function(options, pr, cb) {
var self = this;
var ver = pr.get('payment_details_version');
var pki_type = pr.get('pki_type');
var pki_data = pr.get('pki_data');
var details = pr.get('serialized_payment_details');
var sig = pr.get('signature');
var certs = PayPro.X509Certificates.decode(pki_data);
certs = certs.certificate;
// XXX Temporary fix for tests
if (!PayPro.RootCerts) {
PayPro.RootCerts = {
getTrusted: function() {}
};
}
var trusted = certs.map(function(cert) {
var der = cert.toString('hex');
var pem = PayPro.prototype._DERtoPEM(der, 'CERTIFICATE');
return PayPro.RootCerts.getTrusted(pem);
});
if (!trusted.length) {
if (typeof SSL_UNTRUSTED === 'undefined') {
return cb(new Error('Not a trusted certificate.'));
}
}
// Verify Signature
var verified = pr.verify();
if (!verified) {
return cb(new Error('Server sent a bad signature.'));
}
var ca = trusted[0];
details = PayPro.PaymentDetails.decode(details);
var pd = new PayPro();
pd = pd.makePaymentDetails(details);
var network = pd.get('network');
var outputs = pd.get('outputs');
var time = pd.get('time');
var expires = pd.get('expires');
var memo = pd.get('memo');
var payment_url = pd.get('payment_url');
var merchant_data = pd.get('merchant_data');
var merchantData = {
pr: {
payment_details_version: ver,
pki_type: pki_type,
pki_data: certs,
pd: {
network: network,
outputs: outputs.map(function(output) {
return {
amount: output.get('amount'),
script: {
offset: output.get('script').offset,
limit: output.get('script').limit,
buffer: output.get('script').buffer.toString('hex')
}
};
}),
time: time,
expires: expires,
memo: memo,
payment_url: payment_url,
merchant_data: merchant_data.toString('hex')
},
signature: sig,
ca: ca,
2014-07-30 13:35:57 -07:00
},
total: bignum('0').toString(10)
};
return this.getUnspent(function(err, unspent) {
var ntxid = self.createPaymentTxSync(options, merchantData, unspent);
if (ntxid) {
self.sendIndexes();
self.sendTxProposal(ntxid);
self.store();
self.emit('txProposalsUpdated');
}
self.log('You are currently on this BTC network:');
self.log(network);
self.log('The server sent you a message:');
self.log(memo);
return cb(ntxid, ca);
});
};
Wallet.prototype.sendPaymentTx = function(ntxid, options, cb) {
var self = this;
if (!cb) {
cb = options;
options = {};
}
var txp = this.txProposals.txps[ntxid];
if (!txp) return;
var tx = txp.builder.build();
if (!tx.isComplete()) return;
this.log('Sending Transaction');
var refund_outputs = [];
options.refund_to = options.refund_to
|| self.publicKeyRing.getPubKeys(0, false, this.getMyCopayerId())[0];
if (options.refund_to) {
2014-07-30 13:31:57 -07:00
var total = bignum('0');
for (var i = 0; i < tx.outs.length - 1; i++) {
2014-07-30 13:31:57 -07:00
total = total.add(bignum.fromBuffer(tx.outs[i].v));
}
var rpo = new PayPro();
rpo = rpo.makeOutput();
2014-07-30 13:31:57 -07:00
rpo.set('amount', +total.toString(10));
rpo.set('script',
Buffer.concat([
new Buffer([
118, // OP_DUP
169, // OP_HASH160
76, // OP_PUSHDATA1
20, // number of bytes
]),
options.refund_to,
new Buffer([
136, // OP_EQUALVERIFY
172 // OP_CHECKSIG
])
])
);
refund_outputs.push(rpo.message);
}
// We send this to the serve after receiving a PaymentRequest
var pay = new PayPro();
pay = pay.makePayment();
var merchant_data = txp.merchant.pr.pd.merchant_data;
if (typeof merchant_data === 'string') {
merchant_data = new Buffer(merchant_data, 'hex');
}
pay.set('merchant_data', merchant_data);
pay.set('transactions', [tx.serialize()]);
pay.set('refund_to', refund_outputs);
options.memo = options.memo || options.comment
|| 'Hi server, I would like to give you some money.';
pay.set('memo', txp.merchant.pr.pd.memo);
return $http({
method: 'POST',
url: txp.merchant.pr.pd.payment_url,
headers: {
// BIP-71
'Accept': PayPro.PAYMENT_REQUEST_CONTENT_TYPE
+ ', ' + PayPro.PAYMENT_ACK_CONTENT_TYPE,
'Content-Type': PayPro.PAYMENT_CONTENT_TYPE,
'Content-Length': pay.length + '',
'Content-Transfer-Encoding': 'binary'
},
body: pay.serialize(),
responseType: 'arraybuffer'
})
.success(function(data, status, headers, config) {
data = PayPro.PaymentACK.decode(data);
var ack = new PayPro();
ack = ack.makePaymentACK(data);
return self.receivePaymentRequestACK(tx, txp, ack, cb);
})
.error(function(data, status, headers, config) {
return cb(new Error('Status: ' + JSON.stringify(status)));
});
};
Wallet.prototype.receivePaymentRequestACK = function(tx, txp, ack, cb) {
var self = this;
var payment = ack.get('payment');
var memo = ack.get('memo');
this.log('Our payment was acknowledged!');
this.log('Message from Merchant: %s', memo);
payment = PayPro.Payment.decode(payment);
var pay = new PayPro();
payment = pay.makePayment(payment);
var tx = payment.message.transactions[0];
if (tx.buffer) {
tx.buffer = tx.buffer.slice(tx.offset, tx.limit);
var ptx = new bitcore.Transaction();
ptx.parse(tx.buffer);
tx = ptx;
}
var txid = tx.getHash().toString('hex');
2014-07-29 11:52:59 -07:00
return cb(txid, txp.merchant.pr.ca);
};
Wallet.prototype.createPaymentTxSync = function(options, merchantData, unspent) {
var self = this;
var priv = this.privateKey;
var pkr = this.publicKeyRing;
// preconditions.checkArgument(new Address(toAddress).network().name === this.getNetworkName());
preconditions.checkState(pkr.isComplete());
if (options.memo) preconditions.checkArgument(options.memo.length <= 100);
var opts = {
remainderOut: {
address: this._doGenerateAddress(true).toString()
}
};
var outs = [];
merchantData.pr.pd.outputs.forEach(function(output) {
outs.push({
address: self.getAddressesStr()[0]
|| 'mfWxJ45yp2SFn7UciZyNpvDKrzbhyfKrY8', // dummy address (testnet 0 * hash160)
amountSatStr: '0' // dummy amount
});
});
var b = new Builder(opts)
.setUnspent(unspent)
.setOutputs(outs);
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
b = b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
if (priv) {
var keys = priv.getForPaths(inputChainPaths);
var signed = b.sign(keys);
}
2014-07-30 13:35:57 -07:00
if (typeof merchantData.total === 'string') {
merchantData.total = bignum(merchantData.total, 10);
}
merchantData.pr.pd.outputs.forEach(function(output, i) {
var amount = output.get
? output.get('amount')
: output.amount;
var script = output.get
? output.get('script')
: {
offset: output.script.offset,
limit: output.script.limit,
buffer: new Buffer(output.script.buffer, 'hex')
};
var v = new Buffer(8);
v[0] = (amount.low >> 0) & 0xff;
v[1] = (amount.low >> 8) & 0xff;
v[2] = (amount.low >> 16) & 0xff;
v[3] = (amount.low >> 24) & 0xff;
v[4] = (amount.high >> 0) & 0xff;
v[5] = (amount.high >> 8) & 0xff;
v[6] = (amount.high >> 16) & 0xff;
v[7] = (amount.high >> 24) & 0xff;
var s = script.buffer.slice(script.offset, script.limit);
b.tx.outs[i].v = v;
b.tx.outs[i].s = s;
2014-07-30 13:31:57 -07:00
merchantData.total = merchantData.total.add(bignum.fromBuffer(v));
});
2014-07-30 13:31:57 -07:00
merchantData.total = merchantData.total.toString(10);
this.log('');
this.log('Created transaction:');
this.log(b.tx.getStandardizedObject());
this.log('');
var myId = this.getMyCopayerId();
var now = Date.now();
var me = {};
var tx = b.build();
if (priv && tx.countInputSignatures(0)) me[myId] = now;
var meSeen = {};
if (priv) meSeen[myId] = now;
var data = {
inputChainPaths: inputChainPaths,
signedBy: me,
seenBy: meSeen,
creator: myId,
createdTs: now,
builder: b,
comment: options.memo,
merchant: merchantData
};
var ntxid = this.txProposals.add(data);
return ntxid;
};
Wallet.prototype.addSeenToTxProposals = function() {
var ret = false;
var myId = this.getMyCopayerId();
for (var k in this.txProposals.txps) {
var txp = this.txProposals.txps[k];
if (!txp.seenBy[myId]) {
txp.seenBy[myId] = Date.now();
ret = true;
}
}
return ret;
};
2014-04-15 11:25:55 -07:00
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, this.publicKey);
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];
var name = u.txid + ',' + u.vout;
if (!uu[name] && (self.spendUnconfirmed || u.confirmations >= 1))
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 amountSatStr === 'function') {
var cb = amountSatStr;
var merchant = toAddress;
return this.createPaymentTx({ uri: merchant }, cb);
}
if (typeof comment === 'function') {
var cb = comment;
var merchant = toAddress;
var comment = amountSatStr;
return this.createPaymentTx({ uri: merchant, memo: comment }, cb);
}
2014-04-16 13:50:10 -07:00
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-08-03 19:57:23 -07:00
preconditions.checkArgument(new Address(toAddress).network().name === this.getNetworkName(), 'networkname mismatch');
preconditions.checkState(pkr.isComplete(), 'pubkey ring incomplete');
2014-08-05 12:25:02 -07:00
preconditions.checkState(priv, 'no private key');
if (comment) preconditions.checkArgument(comment.length <= 100);
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
2014-08-03 18:34:47 -07:00
for (var k in Wallet.builderOpts) {
opts[k] = Wallet.builderOpts[k];
}
2014-04-15 14:23:35 -07:00
var b = new Builder(opts)
2014-07-30 17:20:08 -07:00
.setUnspent(utxos)
.setOutputs([{
address: toAddress,
amountSatStr: amountSatStr,
}]);
2014-04-15 14:23:35 -07:00
2014-05-30 11:07:52 -07:00
var selectedUtxos = b.getSelectedUnspent();
var inputChainPaths = selectedUtxos.map(function(utxo) {
return pkr.pathForAddress(utxo.address);
});
2014-06-24 09:17:22 -07:00
b = b.setHashToScriptMap(pkr.getRedeemScriptMap(inputChainPaths));
var keys = priv.getForPaths(inputChainPaths);
var signed = b.sign(keys);
2014-04-23 09:44:20 -07:00
var myId = this.getMyCopayerId();
2014-04-20 16:24:24 -07:00
var now = Date.now();
var tx = b.build();
2014-08-03 18:34:47 -07:00
if (!tx.countInputSignatures(0))
throw new Error('Could not sign generated tx');
var me = {};
me[myId] = now;
2014-04-22 22:01:54 -07:00
var meSeen = {};
if (priv) meSeen[myId] = now;
2014-04-20 16:24:24 -07:00
var ntxid = this.txProposals.add(new TxProposal({
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-05-30 11:07:52 -07:00
return ntxid;
2014-04-15 14:23:35 -07:00
};
Wallet.prototype.updateIndexes = function(callback) {
var self = this;
self.log('Updating indexes...');
var tasks = this.publicKeyRing.indexes.map(function(index) {
return function(callback) {
self.updateIndex(index, callback);
};
});
async.parallel(tasks, function(err) {
if (err) callback(err);
self.log('Indexes updated');
self.emit('publicKeyRingUpdated');
self.store();
callback();
});
}
Wallet.prototype.updateIndex = function(index, callback) {
var self = this;
2014-07-07 08:26:42 -07:00
var SCANN_WINDOW = 20;
self.indexDiscovery(index.changeIndex, true, index.cosigner, SCANN_WINDOW, function(err, changeIndex) {
if (err) return callback(err);
if (changeIndex != -1)
index.changeIndex = changeIndex + 1;
2014-07-07 08:26:42 -07:00
self.indexDiscovery(index.receiveIndex, false, index.cosigner, SCANN_WINDOW, function(err, receiveIndex) {
if (err) return callback(err);
if (receiveIndex != -1)
index.receiveIndex = receiveIndex + 1;
callback();
});
});
}
Wallet.prototype.deriveAddresses = function(index, amout, isChange, cosigner) {
var ret = new Array(amout);
for (var i = 0; i < amout; i++) {
ret[i] = this.publicKeyRing.getAddress(index + i, isChange, cosigner).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, cosigner, gap, cb) {
var scanIndex = start;
var lastActive = -1;
var hasActivity = false;
var self = this;
async.doWhilst(
function _do(next) {
2014-07-30 17:20:08 -07:00
// Optimize window to minimize the derivations.
var scanWindow = (lastActive == -1) ? gap : gap - (scanIndex - lastActive) + 1;
var addresses = self.deriveAddresses(scanIndex, scanWindow, change, cosigner);
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-08-12 12:26:15 -07:00
this.unlock();
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 copayerId = this.getMyCopayerId();
var ts = Date.now();
var payload = {
address: key,
label: label,
copayerId: copayerId,
createdTs: ts
};
var newEntry = {
hidden: false,
createdTs: ts,
copayerId: copayerId,
label: label,
signature: this.signJson(payload)
2014-06-18 16:18:13 -07:00
};
this.addressBook[key] = newEntry;
2014-06-18 16:18:13 -07:00
this.sendAddressBook();
2014-06-17 21:00:32 -07:00
this.store();
};
2014-07-07 16:01:50 -07:00
Wallet.prototype.verifyAddressbookEntry = function(rcvEntry, senderId, key) {
2014-07-07 13:38:17 -07:00
if (!key) throw new Error('Keys are required');
2014-07-07 16:01:50 -07:00
var signature = rcvEntry.signature;
var payload = {
address: key,
2014-07-07 16:01:50 -07:00
label: rcvEntry.label,
copayerId: rcvEntry.copayerId,
createdTs: rcvEntry.createdTs
};
2014-07-07 16:01:50 -07:00
return this.verifySignedJson(senderId, payload, signature);
}
Wallet.prototype.toggleAddressBookEntry = function(key) {
if (!key) throw new Error('Key is required');
this.addressBook[key].hidden = !this.addressBook[key].hidden;
this.store();
2014-06-17 21:00:32 -07:00
};
Wallet.prototype.isShared = function() {
return this.totalCopayers > 1;
}
2014-06-25 13:14:12 -07:00
Wallet.prototype.isReady = function() {
var ret = this.publicKeyRing.isComplete() && this.publicKeyRing.isFullyBackup();
2014-06-25 13:14:12 -07:00
return ret;
};
Wallet.prototype.setBackupReady = function() {
this.publicKeyRing.setBackupReady();
this.sendPublicKeyRing();
this.store();
2014-06-25 13:14:12 -07:00
};
Wallet.prototype.signJson = function(payload) {
var key = new bitcore.Key();
key.private = new Buffer(this.getMyCopayerIdPriv(), 'hex');
key.regenerateSync();
var sign = bitcore.Message.sign(JSON.stringify(payload), key);
return sign.toString('hex');
}
2014-07-07 13:38:17 -07:00
Wallet.prototype.verifySignedJson = function(senderId, payload, signature) {
var pubkey = new Buffer(senderId, 'hex');
var sign = new Buffer(signature, 'hex');
var v = bitcore.Message.verifyWithPubKey(pubkey, JSON.stringify(payload), sign);
return v;
}
2014-04-14 14:30:08 -07:00
module.exports = require('soop')(Wallet);