|
@ -9,7 +9,7 @@ log.level = 'debug'; |
|
|
var fs = require('fs') |
|
|
var fs = require('fs') |
|
|
|
|
|
|
|
|
var Bitcore = require('bitcore') |
|
|
var Bitcore = require('bitcore') |
|
|
var SignUtils = require('./signutils'); |
|
|
var SignUtils = require('../signutils'); |
|
|
|
|
|
|
|
|
var BASE_URL = 'http://localhost:3001/copay/api'; |
|
|
var BASE_URL = 'http://localhost:3001/copay/api'; |
|
|
|
|
|
|
|
@ -47,24 +47,24 @@ function _createXPrivKey(network) { |
|
|
return new Bitcore.HDPrivateKey(network).toString(); |
|
|
return new Bitcore.HDPrivateKey(network).toString(); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
function ClientLib(opts) { |
|
|
function API(opts) { |
|
|
if (!opts.filename) { |
|
|
if (!opts.filename) { |
|
|
throw new Error('Please set the config filename'); |
|
|
throw new Error('Please set the config filename'); |
|
|
} |
|
|
} |
|
|
this.filename = opts.filename; |
|
|
this.filename = opts.filename; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype._save = function(data) { |
|
|
API.prototype._save = function(data) { |
|
|
fs.writeFileSync(this.filename, JSON.stringify(data)); |
|
|
fs.writeFileSync(this.filename, JSON.stringify(data)); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype._load = function() { |
|
|
API.prototype._load = function() { |
|
|
try { |
|
|
try { |
|
|
return JSON.parse(fs.readFileSync(this.filename)); |
|
|
return JSON.parse(fs.readFileSync(this.filename)); |
|
|
} catch (ex) {} |
|
|
} catch (ex) {} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype._loadAndCheck = function() { |
|
|
API.prototype._loadAndCheck = function() { |
|
|
var data = this._load(); |
|
|
var data = this._load(); |
|
|
if (!data) { |
|
|
if (!data) { |
|
|
log.error('Wallet file not found.'); |
|
|
log.error('Wallet file not found.'); |
|
@ -107,17 +107,17 @@ ClientLib.prototype._doRequest = function(method, url, args, data, cb) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClientLib.prototype._doPostRequest = function(url, args, data, cb) { |
|
|
API.prototype._doPostRequest = function(url, args, data, cb) { |
|
|
return this._doRequest('post', url, args, data, cb); |
|
|
return this._doRequest('post', url, args, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype._doGetRequest = function(url, data, cb) { |
|
|
API.prototype._doGetRequest = function(url, data, cb) { |
|
|
return this._doRequest('get', url, {}, data, cb); |
|
|
return this._doRequest('get', url, {}, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { |
|
|
API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
network = network || 'livenet'; |
|
|
network = network || 'livenet'; |
|
|
if (!_.contains(['testnet', 'livenet'], network)) |
|
|
if (!_.contains(['testnet', 'livenet'], network)) |
|
@ -146,6 +146,8 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo |
|
|
var url = '/v1/wallets/'; |
|
|
var url = '/v1/wallets/'; |
|
|
|
|
|
|
|
|
this._doPostRequest(url, args, data, function(err, body) { |
|
|
this._doPostRequest(url, args, data, function(err, body) { |
|
|
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
|
var walletId = body.walletId; |
|
|
var walletId = body.walletId; |
|
|
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L'); |
|
|
var secret = walletId + ':' + privKey.toString() + ':' + (network == 'testnet' ? 'T' : 'L'); |
|
|
data.secret = secret; |
|
|
data.secret = secret; |
|
@ -159,7 +161,7 @@ ClientLib.prototype.createWallet = function(walletName, copayerName, m, n, netwo |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { |
|
|
API.prototype._joinWallet = function(data, secret, copayerName, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
data = data || {}; |
|
|
data = data || {}; |
|
|
|
|
|
|
|
@ -196,7 +198,7 @@ ClientLib.prototype._joinWallet = function(data, secret, copayerName, cb) { |
|
|
}); |
|
|
}); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
API.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._load(); |
|
|
var data = this._load(); |
|
@ -205,7 +207,7 @@ ClientLib.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
self._joinWallet(data, secret, copayerName, cb); |
|
|
self._joinWallet(data, secret, copayerName, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.status = function(cb) { |
|
|
API.prototype.getStatus = function(cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -249,7 +251,7 @@ ClientLib.prototype.status = function(cb) { |
|
|
* @param inArgs.amount |
|
|
* @param inArgs.amount |
|
|
* @param inArgs.message |
|
|
* @param inArgs.message |
|
|
*/ |
|
|
*/ |
|
|
ClientLib.prototype.send = function(inArgs, cb) { |
|
|
API.prototype.sendTxProposal = function(inArgs, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -260,7 +262,7 @@ ClientLib.prototype.send = function(inArgs, cb) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// Get addresses
|
|
|
// Get addresses
|
|
|
ClientLib.prototype.addresses = function(cb) { |
|
|
API.prototype.getAddresses = function(cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -272,7 +274,7 @@ ClientLib.prototype.addresses = function(cb) { |
|
|
|
|
|
|
|
|
// Creates a new address
|
|
|
// Creates a new address
|
|
|
// TODO: verify derivation!!
|
|
|
// TODO: verify derivation!!
|
|
|
ClientLib.prototype.address = function(cb) { |
|
|
API.prototype.createAddress = function(cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -281,11 +283,11 @@ ClientLib.prototype.address = function(cb) { |
|
|
this._doPostRequest(url, {}, data, cb); |
|
|
this._doPostRequest(url, {}, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.history = function(limit, cb) { |
|
|
API.prototype.history = function(limit, cb) { |
|
|
|
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.balance = function(cb) { |
|
|
API.prototype.getBalance = function(cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -295,7 +297,7 @@ ClientLib.prototype.balance = function(cb) { |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ClientLib.prototype.txProposals = function(opts, cb) { |
|
|
API.prototype.getTxProposals = function(opts, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
|
|
|
|
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
@ -304,7 +306,7 @@ ClientLib.prototype.txProposals = function(opts, cb) { |
|
|
this._doGetRequest(url, data, cb); |
|
|
this._doGetRequest(url, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.sign = function(txp, cb) { |
|
|
API.prototype.signTxProposal = function(txp, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
|
|
|
|
|
|
@ -346,7 +348,7 @@ ClientLib.prototype.sign = function(txp, cb) { |
|
|
this._doPostRequest(url, args, data, cb); |
|
|
this._doPostRequest(url, args, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
ClientLib.prototype.reject = function(txp, reason, cb) { |
|
|
API.prototype.rejectTxProposal = function(txp, reason, cb) { |
|
|
var self = this; |
|
|
var self = this; |
|
|
var data = this._loadAndCheck(); |
|
|
var data = this._loadAndCheck(); |
|
|
|
|
|
|
|
@ -357,4 +359,4 @@ ClientLib.prototype.reject = function(txp, reason, cb) { |
|
|
this._doPostRequest(url, args, data, cb); |
|
|
this._doPostRequest(url, args, data, cb); |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
module.exports = ClientLib; |
|
|
module.exports = API; |