|
|
@ -62,7 +62,43 @@ function API(opts) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
API.prototype._loadAndCheck = function(opts, cb) { |
|
|
|
API.prototype._tryToComplete = function(data, cb) { |
|
|
|
var self = this; |
|
|
|
var isCorrupted; |
|
|
|
|
|
|
|
var url = '/v1/wallets/'; |
|
|
|
self._doGetRequest(url, data, function(err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) { |
|
|
|
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString(); |
|
|
|
var fake = []; |
|
|
|
_.each(wallet.copayers, function(copayer) { |
|
|
|
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) { |
|
|
|
fake.push(copayer); |
|
|
|
} |
|
|
|
}); |
|
|
|
if (fake.length > 0) { |
|
|
|
isCorrupted = true; |
|
|
|
data.verified = 'corrupt'; |
|
|
|
} else { |
|
|
|
data.verified = 'ok'; |
|
|
|
} |
|
|
|
self.storage.save(data, function(err) { |
|
|
|
if (isCorrupted) { |
|
|
|
return cb('Some copayers in the wallet could not be verified to have known the wallet secret'); |
|
|
|
} |
|
|
|
return cb(err, data); |
|
|
|
}); |
|
|
|
} |
|
|
|
return cb(null, data); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API.prototype._loadAndCheck = function(cb) { |
|
|
|
var self = this; |
|
|
|
this.storage.load(function(err, data) { |
|
|
|
if (err || !data) { |
|
|
|
return cb(err || 'Wallet file not found.'); |
|
|
@ -74,8 +110,8 @@ API.prototype._loadAndCheck = function(opts, cb) { |
|
|
|
if (data.n > 1) { |
|
|
|
var pkrComplete = data.publicKeyRing && data.m && data.publicKeyRing.length === data.n; |
|
|
|
|
|
|
|
if (opts.requireCompletePKR && !pkrComplete) { |
|
|
|
return cb('Wallet Incomplete, cannot derive address'); |
|
|
|
if (!pkrComplete) { |
|
|
|
return self._tryToComplete(data, cb); |
|
|
|
} |
|
|
|
} |
|
|
|
return cb(null, data); |
|
|
@ -222,39 +258,12 @@ API.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
|
API.prototype.getStatus = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, data) { |
|
|
|
this._loadAndCheck(function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/wallets/'; |
|
|
|
self._doGetRequest(url, data, function(err, body) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var wallet = body; |
|
|
|
if (wallet.n > 0 && wallet.status === 'complete' && !data.verified) { |
|
|
|
var pubKey = Bitcore.PrivateKey.fromString(data.walletPrivKey).toPublicKey().toString(); |
|
|
|
var fake = []; |
|
|
|
_.each(wallet.copayers, function(copayer) { |
|
|
|
|
|
|
|
|
|
|
|
console.log('[clilib.js.224]', copayer.xPubKey, copayer.xPubKeySignature, pubKey); //TODO
|
|
|
|
if (!SignUtils.verify(copayer.xPubKey, copayer.xPubKeySignature, pubKey)) { |
|
|
|
|
|
|
|
console.log('[clilib.js.227] FAKE'); //TODO
|
|
|
|
fake.push(copayer); |
|
|
|
} |
|
|
|
}); |
|
|
|
if (fake.length > 0) { |
|
|
|
log.error('Some copayers in the wallet could not be verified to have known the wallet secret'); |
|
|
|
data.verified = 'corrupt'; |
|
|
|
} else { |
|
|
|
data.verified = 'ok'; |
|
|
|
} |
|
|
|
self.storage.save(data, function(err) { |
|
|
|
return cb(err, wallet); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(null, wallet); |
|
|
|
return cb(err, body); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
@ -270,36 +279,34 @@ API.prototype.getStatus = function(cb) { |
|
|
|
API.prototype.sendTxProposal = function(inArgs, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var args = _createProposalOpts(inArgs, data.signingPrivKey); |
|
|
|
var args = _createProposalOpts(inArgs, data.signingPrivKey); |
|
|
|
|
|
|
|
var url = '/v1/txproposals/'; |
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
var url = '/v1/txproposals/'; |
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.createAddress = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/addresses/'; |
|
|
|
self._doPostRequest(url, {}, data, function(err, address) { |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!Verifier.checkAddress(data, address)) { |
|
|
|
return cb(new ServerCompromisedError('Server sent fake address')); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(null, address); |
|
|
|
var url = '/v1/addresses/'; |
|
|
|
self._doPostRequest(url, {}, data, function(err, address) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!Verifier.checkAddress(data, address)) { |
|
|
|
return cb(new ServerCompromisedError('Server sent fake address')); |
|
|
|
} |
|
|
|
|
|
|
|
return cb(null, address); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.history = function(limit, cb) { |
|
|
@ -309,7 +316,7 @@ API.prototype.history = function(limit, cb) { |
|
|
|
API.prototype.getBalance = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, data) { |
|
|
|
this._loadAndCheck(function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/balance/'; |
|
|
|
self._doGetRequest(url, data, cb); |
|
|
@ -320,102 +327,97 @@ API.prototype.getBalance = function(cb) { |
|
|
|
API.prototype.getTxProposals = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/'; |
|
|
|
self._doGetRequest(url, data, cb); |
|
|
|
}); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/'; |
|
|
|
self._doGetRequest(url, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.signTxProposal = function(txp, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
|
|
|
|
//Derive proper key to sign, for each input
|
|
|
|
var privs = [], |
|
|
|
derived = {}; |
|
|
|
//Derive proper key to sign, for each input
|
|
|
|
var privs = [], |
|
|
|
derived = {}; |
|
|
|
|
|
|
|
var network = new Bitcore.Address(txp.toAddress).network.name; |
|
|
|
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network); |
|
|
|
var network = new Bitcore.Address(txp.toAddress).network.name; |
|
|
|
var xpriv = new Bitcore.HDPrivateKey(data.xPrivKey, network); |
|
|
|
|
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
if (!derived[i.path]) { |
|
|
|
derived[i.path] = xpriv.derive(i.path).privateKey; |
|
|
|
} |
|
|
|
privs.push(derived[i.path]); |
|
|
|
}); |
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
if (!derived[i.path]) { |
|
|
|
derived[i.path] = xpriv.derive(i.path).privateKey; |
|
|
|
} |
|
|
|
privs.push(derived[i.path]); |
|
|
|
}); |
|
|
|
|
|
|
|
var t = new Bitcore.Transaction(); |
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
t.from(i, i.publicKeys, txp.requiredSignatures); |
|
|
|
}); |
|
|
|
var t = new Bitcore.Transaction(); |
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
t.from(i, i.publicKeys, txp.requiredSignatures); |
|
|
|
}); |
|
|
|
|
|
|
|
t.to(txp.toAddress, txp.amount) |
|
|
|
.change(txp.changeAddress) |
|
|
|
.sign(privs); |
|
|
|
t.to(txp.toAddress, txp.amount) |
|
|
|
.change(txp.changeAddress) |
|
|
|
.sign(privs); |
|
|
|
|
|
|
|
var signatures = []; |
|
|
|
_.each(privs, function(p) { |
|
|
|
var s = t.getSignatures(p)[0].signature.toDER().toString('hex'); |
|
|
|
signatures.push(s); |
|
|
|
}); |
|
|
|
var signatures = []; |
|
|
|
_.each(privs, function(p) { |
|
|
|
var s = t.getSignatures(p)[0].signature.toDER().toString('hex'); |
|
|
|
signatures.push(s); |
|
|
|
}); |
|
|
|
|
|
|
|
var url = '/v1/txproposals/' + txp.id + '/signatures/'; |
|
|
|
var args = { |
|
|
|
signatures: signatures |
|
|
|
}; |
|
|
|
var url = '/v1/txproposals/' + txp.id + '/signatures/'; |
|
|
|
var args = { |
|
|
|
signatures: signatures |
|
|
|
}; |
|
|
|
|
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.rejectTxProposal = function(txp, reason, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/txproposals/' + txp.id + '/rejections/'; |
|
|
|
var args = { |
|
|
|
reason: reason || '', |
|
|
|
}; |
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
var url = '/v1/txproposals/' + txp.id + '/rejections/'; |
|
|
|
var args = { |
|
|
|
reason: reason || '', |
|
|
|
}; |
|
|
|
self._doPostRequest(url, args, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.broadcastTxProposal = function(txp, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/txproposals/' + txp.id + '/broadcast/'; |
|
|
|
self._doPostRequest(url, {}, data, cb); |
|
|
|
}); |
|
|
|
var url = '/v1/txproposals/' + txp.id + '/broadcast/'; |
|
|
|
self._doPostRequest(url, {}, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API.prototype.removeTxProposal = function(txp, cb) { |
|
|
|
var self = this; |
|
|
|
this._loadAndCheck({ |
|
|
|
requireCompletePKR: true |
|
|
|
}, function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/' + txp.id; |
|
|
|
self._doRequest('delete', url, {}, data, cb); |
|
|
|
}); |
|
|
|
this._loadAndCheck( |
|
|
|
function(err, data) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/' + txp.id; |
|
|
|
self._doRequest('delete', url, {}, data, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
module.exports = API; |
|
|
|