fix conflicts
This commit is contained in:
commit
2c0b733025
24 changed files with 3745 additions and 193 deletions
|
|
@ -29,15 +29,6 @@ function _asyncForEach(array, fn, callback) {
|
|||
}
|
||||
};
|
||||
|
||||
Insight.prototype.getBalance = function(unspent) {
|
||||
var balance = 0;
|
||||
for(var i=0;i<unspent.length; i++) {
|
||||
balance = balance + unspent[i].amount;
|
||||
}
|
||||
|
||||
return balance;
|
||||
};
|
||||
|
||||
Insight.prototype.listUnspent = function(addresses, cb) {
|
||||
var self = this;
|
||||
|
||||
|
|
|
|||
|
|
@ -166,10 +166,7 @@ PublicKeyRing.prototype.getAddress = function (index, isChange) {
|
|||
this._checkIndexRange(index, isChange);
|
||||
|
||||
var script = this.getRedeemScript(index,isChange);
|
||||
var hash = coinUtil.sha256ripe160(script.getBuffer());
|
||||
var version = this.network.P2SHVersion;
|
||||
var addr = new Address(version, hash);
|
||||
return addr;
|
||||
return Address.fromScript(script, this.network.name);
|
||||
};
|
||||
|
||||
PublicKeyRing.prototype.getScriptPubKeyHex = function (index, isChange) {
|
||||
|
|
@ -185,10 +182,11 @@ PublicKeyRing.prototype.generateAddress = function(isChange) {
|
|||
|
||||
var ret =
|
||||
this.getAddress(isChange ? this.changeAddressIndex : this.addressIndex, isChange);
|
||||
if (isChange)
|
||||
if (isChange) {
|
||||
this.changeAddressIndex++;
|
||||
else
|
||||
} else {
|
||||
this.addressIndex++;
|
||||
}
|
||||
|
||||
return ret;
|
||||
|
||||
|
|
|
|||
|
|
@ -96,11 +96,12 @@ Wallet.prototype._handleTxProposals = function(senderId, data, isInbound) {
|
|||
|
||||
Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
||||
|
||||
if (this.id !== data.walletId)
|
||||
throw new Error('wrong message received: Bad wallet ID');
|
||||
|
||||
if (this.id !== data.walletId) {
|
||||
this.emit('badMessage',senderId);
|
||||
this.log('badMessage FROM:', senderId); //TODO
|
||||
return;
|
||||
}
|
||||
this.log('[Wallet.js.98]' , data.type); //TODO
|
||||
|
||||
switch(data.type) {
|
||||
case 'publicKeyRing':
|
||||
this._handlePublicKeyRing(senderId, data, isInbound);
|
||||
|
|
@ -108,18 +109,17 @@ Wallet.prototype._handleData = function(senderId, data, isInbound) {
|
|||
case 'txProposals':
|
||||
this._handleTxProposals(senderId, data, isInbound);
|
||||
break;
|
||||
case 'abort':
|
||||
this.emit('abort');
|
||||
break;
|
||||
}
|
||||
};
|
||||
|
||||
Wallet.prototype._handleNetworkChange = function(newPeer) {
|
||||
if (!newPeer) return;
|
||||
this.log('#### Setting new PEER:', newPeer);
|
||||
this.sendWalletId(newPeer);
|
||||
this.sendPublicKeyRing(newPeer);
|
||||
this.sendTxProposals(newPeer);
|
||||
if (newPeer) {
|
||||
this.log('#### Setting new PEER:', newPeer);
|
||||
this.sendWalletId(newPeer);
|
||||
this.sendPublicKeyRing(newPeer);
|
||||
this.sendTxProposals(newPeer);
|
||||
}
|
||||
this.emit('refresh');
|
||||
};
|
||||
|
||||
Wallet.prototype._optsToObj = function () {
|
||||
|
|
@ -136,10 +136,17 @@ Wallet.prototype._optsToObj = function () {
|
|||
Wallet.prototype.netStart = function() {
|
||||
var self = this;
|
||||
var net = this.network;
|
||||
net.removeAllListeners();
|
||||
net.on('networkChange', self._handleNetworkChange.bind(self) );
|
||||
net.on('data', self._handleData.bind(self) );
|
||||
net.on('open', function() {}); // TODO
|
||||
net.on('close', function() {}); // TODO
|
||||
net.on('openError', function() {
|
||||
this.log('[Wallet.js.132:openError:] GOT openError'); //TODO
|
||||
self.emit('openError');
|
||||
});
|
||||
net.on('close', function() {
|
||||
self.emit('close');
|
||||
});
|
||||
net.start(function(peerId) {
|
||||
self.emit('created');
|
||||
});
|
||||
|
|
@ -211,6 +218,7 @@ Wallet.prototype.sendPublicKeyRing = function(recipients) {
|
|||
this.emit('publicKeyRingUpdated', this.publicKeyRing);
|
||||
};
|
||||
|
||||
|
||||
Wallet.prototype.generateAddress = function() {
|
||||
var addr = this.publicKeyRing.generateAddress();
|
||||
this.store();
|
||||
|
|
@ -303,10 +311,34 @@ Wallet.prototype.getAddressesStr = function() {
|
|||
return ret;
|
||||
};
|
||||
|
||||
Wallet.prototype.getTotalBalance = function(cb) {
|
||||
this.getBalance(this.getAddressesStr(), function(balance) {
|
||||
return cb(balance);
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.getBalance = function(addrs, cb) {
|
||||
var balance = 0;
|
||||
this.listUnspent(addrs, function(utxos) {
|
||||
for(var i=0;i<utxos.length; i++) {
|
||||
balance = balance + utxos[i].amount;
|
||||
}
|
||||
if (balance) {
|
||||
if (balance === parseInt(balance)) {
|
||||
balance = balance;
|
||||
}
|
||||
else {
|
||||
balance = balance.toFixed(8);
|
||||
}
|
||||
}
|
||||
return cb(balance);
|
||||
});
|
||||
};
|
||||
|
||||
Wallet.prototype.listUnspent = function(cb) {
|
||||
this.blockchain.listUnspent(this.getAddressesStr(), cb);
|
||||
Wallet.prototype.listUnspent = function(addrs, cb) {
|
||||
this.blockchain.listUnspent(addrs, function(utxos) {
|
||||
return cb(utxos);
|
||||
});
|
||||
};
|
||||
|
||||
|
||||
|
|
@ -326,7 +358,7 @@ Wallet.prototype.createTx = function(toAddress, amountSatStr, opts, cb) {
|
|||
opts.remainderOut={ address: this.publicKeyRing.generateAddress(true).toString()};
|
||||
}
|
||||
|
||||
self.listUnspent(function(utxos) {
|
||||
self.listUnspent(self.getAddressesStr(), function(utxos) {
|
||||
// TODO check enough funds, etc.
|
||||
self.createTxSync(toAddress, amountSatStr, utxos, opts);
|
||||
self.store();
|
||||
|
|
|
|||
|
|
@ -148,12 +148,14 @@ WalletFactory.prototype.openRemote = function(peedId) {
|
|||
opts.storage = this.storage;
|
||||
opts.network = this.network;
|
||||
opts.blockchain = this.blockchain;
|
||||
opts.spendUnconfirmed = opts.spendUnconfirmed || this.walletDefaults.spendUnconfirmed;
|
||||
|
||||
opts.spendUnconfirmed = typeof opts.spendUnconfirmed === undefined
|
||||
?this.walletDefaults.spendUnconfirmed : opts.spendUnconfirmed;
|
||||
|
||||
opts.requiredCopayers = requiredCopayers;
|
||||
opts.totalCopayers = totalCopayers;
|
||||
var w = new Wallet(opts);
|
||||
w.store();
|
||||
this.addWalletId(w.id);
|
||||
return w;
|
||||
};
|
||||
|
||||
|
|
|
|||
|
|
@ -16,11 +16,18 @@ var EventEmitter= imports.EventEmitter || require('events').EventEmitter;
|
|||
*/
|
||||
|
||||
function Network(opts) {
|
||||
var self = this;
|
||||
opts = opts || {};
|
||||
this.peerId = opts.peerId;
|
||||
this.apiKey = opts.apiKey || 'lwjd5qra8257b9';
|
||||
this.debug = opts.debug || 3;
|
||||
this.maxPeers = opts.maxPeers || 5;
|
||||
this.opts = { key: opts.key };
|
||||
|
||||
// For using your own peerJs server
|
||||
['port', 'host', 'path', 'debug'].forEach(function(k) {
|
||||
if (opts[k]) self.opts[k]=opts[k];
|
||||
});
|
||||
this.connectedPeers = [];
|
||||
}
|
||||
|
||||
|
|
@ -61,14 +68,9 @@ Network._arrayRemove = function(el, array) {
|
|||
return array;
|
||||
};
|
||||
|
||||
// DEBUG
|
||||
Network.prototype._showConnectedPeers = function() {
|
||||
// console.log("### CONNECTED PEERS", this.connectedPeers);
|
||||
};
|
||||
|
||||
Network.prototype._onClose = function(peerId) {
|
||||
this.connectedPeers = Network._arrayRemove(peerId, this.connectedPeers);
|
||||
this._notify();
|
||||
this._notifyNetworkChange();
|
||||
};
|
||||
|
||||
Network.prototype._connectToPeers = function(peerIds) {
|
||||
|
|
@ -97,7 +99,7 @@ Network.prototype._onData = function(data, isInbound) {
|
|||
switch(obj.data.type) {
|
||||
case 'peerList':
|
||||
this._connectToPeers(obj.data.peers);
|
||||
this._notify();
|
||||
this._notifyNetworkChange();
|
||||
break;
|
||||
case 'disconnect':
|
||||
this._onClose(obj.sender);
|
||||
|
|
@ -133,6 +135,13 @@ Network.prototype._addPeer = function(peerId, isInbound) {
|
|||
}
|
||||
};
|
||||
|
||||
Network.prototype._checkAnyPeer = function() {
|
||||
if (!this.connectedPeers.length) {
|
||||
console.log('EMIT openError: no more peers, not even you!');
|
||||
this.emit('openError');
|
||||
}
|
||||
}
|
||||
|
||||
Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
||||
|
||||
var self=this;
|
||||
|
|
@ -144,7 +153,7 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
|||
dataConn.peer, isInbound);
|
||||
|
||||
self._addPeer(dataConn.peer, isInbound);
|
||||
self._notify( isInbound ? dataConn.peer : null);
|
||||
self._notifyNetworkChange( isInbound ? dataConn.peer : null);
|
||||
this.emit('open');
|
||||
}
|
||||
});
|
||||
|
|
@ -155,19 +164,19 @@ Network.prototype._setupConnectionHandlers = function(dataConn, isInbound) {
|
|||
|
||||
dataConn.on('error', function(e) {
|
||||
console.log('### DATA ERROR',e ); //TODO
|
||||
self.emit('dataError');
|
||||
});
|
||||
|
||||
dataConn.on('close', function() {
|
||||
if (self.closing) return;
|
||||
self.closing=1;
|
||||
console.log('### CLOSE RECV FROM:', dataConn.peer);
|
||||
self._onClose(dataConn.peer);
|
||||
this.emit('close');
|
||||
self._checkAnyPeer();
|
||||
});
|
||||
};
|
||||
|
||||
Network.prototype._notify = function(newPeer) {
|
||||
this._showConnectedPeers();
|
||||
Network.prototype._notifyNetworkChange = function(newPeer) {
|
||||
console.log('[WebRTC.js.164:_notifyNetworkChange:]', newPeer); //TODO
|
||||
this.emit('networkChange', newPeer);
|
||||
};
|
||||
|
||||
|
|
@ -178,7 +187,7 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
p.on('open', function(peerId) {
|
||||
self.peerId = peerId;
|
||||
self.connectedPeers = [peerId];
|
||||
self._notify();
|
||||
self._notifyNetworkChange();
|
||||
return openCallback(peerId);
|
||||
});
|
||||
|
||||
|
|
@ -187,7 +196,7 @@ Network.prototype._setupPeerHandlers = function(openCallback) {
|
|||
self.peer.disconnect();
|
||||
self.peer.destroy();
|
||||
self.peer = null;
|
||||
this.emit('abort');
|
||||
self._checkAnyPeer();
|
||||
});
|
||||
|
||||
p.on('connection', function(dataConn) {
|
||||
|
|
@ -210,11 +219,7 @@ Network.prototype.start = function(openCallback) {
|
|||
// Start PeerJS Peer
|
||||
if (this.peer) return openCallback(); // This is for connectTo-> peer is started before
|
||||
|
||||
this.peer = new Peer(this.peerId, {
|
||||
key: this.apiKey, // TODO: we need our own PeerServer KEY (http://peerjs.com/peerserver)
|
||||
debug: this.debug,
|
||||
});
|
||||
|
||||
this.peer = new Peer(this.peerId, this.opts);
|
||||
this._setupPeerHandlers(openCallback);
|
||||
};
|
||||
|
||||
|
|
@ -239,6 +244,7 @@ Network.prototype._sendToOne = function(peerId, data, cb) {
|
|||
|
||||
Network.prototype.send = function(peerIds, data, cb) {
|
||||
var self=this;
|
||||
console.log('[WebRTC.js.242] SENDING ', data.type); //TODO
|
||||
if (!peerIds) {
|
||||
peerIds = this.connectedPeers;
|
||||
data.isBroadcast = 1;
|
||||
|
|
@ -248,6 +254,7 @@ Network.prototype.send = function(peerIds, data, cb) {
|
|||
var l = peerIds.length;
|
||||
var i = 0;
|
||||
peerIds.forEach(function(peerId) {
|
||||
console.log('[WebRTC.js.258:peerId:]',peerId); //TODO
|
||||
self._sendToOne(peerId, data, function () {
|
||||
if (++i === l && typeof cb === 'function') cb();
|
||||
});
|
||||
|
|
@ -263,7 +270,6 @@ Network.prototype.connectTo = function(peerId) {
|
|||
console.log('### STARTING TO CONNECT TO:' + peerId );
|
||||
|
||||
var dataConn = this.peer.connect(peerId, {
|
||||
// label: 'wallet',
|
||||
serialization: 'none',
|
||||
reliable: true,
|
||||
metadata: { message: 'hi copayer!' }
|
||||
|
|
@ -276,7 +282,6 @@ Network.prototype.connectTo = function(peerId) {
|
|||
Network.prototype.disconnect = function(cb) {
|
||||
var self = this;
|
||||
self.closing = 1;
|
||||
|
||||
this.send(null, { type: 'disconnect' }, function() {
|
||||
self.connectedPeers = [];
|
||||
self.peerId = null;
|
||||
|
|
|
|||
|
|
@ -54,13 +54,17 @@ Storage.prototype.remove = function(walletId, k) {
|
|||
|
||||
Storage.prototype.getWalletIds = function() {
|
||||
var walletIds = [];
|
||||
var uniq = {};
|
||||
|
||||
for (var i = 0; i < localStorage.length; i++) {
|
||||
var key = localStorage.key(i);
|
||||
var split = key.split('::');
|
||||
if (split.length == 2) {
|
||||
var walletId = split[0];
|
||||
walletIds.push(walletId);
|
||||
if (typeof uniq[walletId] === 'undefined' ) {
|
||||
walletIds.push(walletId);
|
||||
uniq[walletId] = 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue