|
|
@ -43,8 +43,8 @@ function _signRequest(url, args, privKey) { |
|
|
|
return SignUtils.sign(message, privKey); |
|
|
|
}; |
|
|
|
|
|
|
|
function _createXPrivKey() { |
|
|
|
return new Bitcore.HDPrivateKey().toString(); |
|
|
|
function _createXPrivKey(network) { |
|
|
|
return new Bitcore.HDPrivateKey(network).toString(); |
|
|
|
}; |
|
|
|
|
|
|
|
function ClientLib(opts) { |
|
|
@ -86,16 +86,16 @@ ClientLib.prototype._loadAndCheck = function() { |
|
|
|
|
|
|
|
ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { |
|
|
|
var self = this; |
|
|
|
network = network || 'livenet'; |
|
|
|
|
|
|
|
var data = this._load(); |
|
|
|
if (data) return cb('File ' + this.filename + ' already contains a wallet'); |
|
|
|
|
|
|
|
// Generate wallet key pair to verify copayers
|
|
|
|
var privKey = new Bitcore.PrivateKey(); |
|
|
|
var privKey = new Bitcore.PrivateKey(null, network); |
|
|
|
var pubKey = privKey.toPublicKey(); |
|
|
|
|
|
|
|
data = { |
|
|
|
xPrivKey: _createXPrivKey(), |
|
|
|
m: m, |
|
|
|
n: n, |
|
|
|
walletPrivKey: privKey.toString(), |
|
|
@ -106,7 +106,7 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo |
|
|
|
m: m, |
|
|
|
n: n, |
|
|
|
pubKey: pubKey.toString(), |
|
|
|
network: network || 'livenet', |
|
|
|
network: network, |
|
|
|
}; |
|
|
|
|
|
|
|
request({ |
|
|
@ -122,11 +122,10 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo |
|
|
|
} |
|
|
|
|
|
|
|
var walletId = body.walletId; |
|
|
|
var secret = walletId + ':' + privKey.toString(); |
|
|
|
var secret = walletId + ':' + privKey.toString() + ':' + (network ? 'T' : 'L'); |
|
|
|
data.secret = secret; |
|
|
|
|
|
|
|
self._save(data); |
|
|
|
|
|
|
|
self._joinWallet(data, secret, copayerName, function(err) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -135,18 +134,21 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { |
|
|
|
ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { |
|
|
|
var self = this; |
|
|
|
data = data || {}; |
|
|
|
|
|
|
|
var secretSplit = secret.split(':'); |
|
|
|
var walletId = secretSplit[0]; |
|
|
|
|
|
|
|
var walletPrivKey = Bitcore.PrivateKey.fromString(secretSplit[1]); |
|
|
|
var network = secretSplit[2] == 'T' ? 'testnet' : 'livenet'; |
|
|
|
data.xPrivKey = _createXPrivKey(network); |
|
|
|
|
|
|
|
var xPubKey = new Bitcore.HDPublicKey(data.xPrivKey); |
|
|
|
var xPubKeySignature = SignUtils.sign(xPubKey.toString(), walletPrivKey); |
|
|
|
|
|
|
|
var signingPrivKey = (new Bitcore.HDPrivateKey(data.xPrivKey)).derive('m/1/0').privateKey; |
|
|
|
|
|
|
|
var args = { |
|
|
|
walletId: walletId, |
|
|
|
name: copayerName, |
|
|
@ -185,10 +187,6 @@ ClientLib.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
|
var data = this._load(); |
|
|
|
if (data) return cb('File ' + this.filename + ' already contains a wallet'); |
|
|
|
|
|
|
|
data = { |
|
|
|
xPrivKey: _createXPrivKey(), |
|
|
|
}; |
|
|
|
|
|
|
|
self._joinWallet(data, secret, copayerName, cb); |
|
|
|
}; |
|
|
|
|
|
|
@ -410,7 +408,9 @@ ClientLib.prototype.sign = function(txp, cb) { |
|
|
|
//Derive proper key to sign, for each input
|
|
|
|
var privs = [], |
|
|
|
derived = {}; |
|
|
|
var xpriv = new Bitcore.HDPrivateKey(data.xprivKey); |
|
|
|
|
|
|
|
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]) { |
|
|
@ -419,8 +419,6 @@ ClientLib.prototype.sign = function(txp, cb) { |
|
|
|
privs.push(derived[i.path]); |
|
|
|
}); |
|
|
|
|
|
|
|
console.log('[clientlib.js.419:privs:]',privs); //TODO
|
|
|
|
|
|
|
|
var t = new Bitcore.Transaction(); |
|
|
|
_.each(txp.inputs, function(i) { |
|
|
|
t.from(i, i.publicKeys, txp.requiredSignatures); |
|
|
@ -435,8 +433,28 @@ console.log('[clientlib.js.419:privs:]',privs); //TODO |
|
|
|
var s = t.getSignatures(p)[0].signature.toDER().toString('hex'); |
|
|
|
signatures.push(s); |
|
|
|
}); |
|
|
|
console.log('[clientlib.js.436:signatures:]',signatures); //TODO
|
|
|
|
//
|
|
|
|
|
|
|
|
var url = '/v1//'; |
|
|
|
var signature = _signRequest(url, args, data.signingPrivKey); |
|
|
|
request({ |
|
|
|
headers: { |
|
|
|
'x-identity': data.copayerId, |
|
|
|
'x-signature': signature, |
|
|
|
}, |
|
|
|
method: 'post', |
|
|
|
url: _getUrl(url), |
|
|
|
body: args, |
|
|
|
json: true, |
|
|
|
}, function(err, res, body) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (res.statusCode != 200) { |
|
|
|
_parseError(body); |
|
|
|
return cb('Request error'); |
|
|
|
} |
|
|
|
return cb(null, body); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
return signatures; |
|
|
|
}; |
|
|
|
|
|
|
|