|
|
@ -6,6 +6,7 @@ var util = require('util'); |
|
|
|
var async = require('async'); |
|
|
|
var log = require('npmlog'); |
|
|
|
var request = require('request') |
|
|
|
var events = require('events'); |
|
|
|
log.debug = log.verbose; |
|
|
|
|
|
|
|
var Bitcore = require('bitcore') |
|
|
@ -112,6 +113,7 @@ function API(opts) { |
|
|
|
this.request = request || opts.request; |
|
|
|
this.baseUrl = opts.baseUrl || BASE_URL; |
|
|
|
this.basePath = this.baseUrl.replace(/http.?:\/\/[a-zA-Z0-9:-]*\//, '/'); |
|
|
|
this.noPasswdAccess = opts.noPasswdAccess || 'full'; |
|
|
|
if (this.verbose) { |
|
|
|
log.level = 'debug'; |
|
|
|
} else { |
|
|
@ -119,6 +121,8 @@ function API(opts) { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
util.inherits(API, events.EventEmitter); |
|
|
|
|
|
|
|
API.prototype._tryToCompleteFromServer = function(wcd, cb) { |
|
|
|
|
|
|
|
if (!wcd.walletPrivKey) |
|
|
@ -142,7 +146,7 @@ API.prototype._tryToCompleteFromServer = function(wcd, cb) { |
|
|
|
|
|
|
|
wcd.publicKeyRing = _.pluck(wallet.copayers, 'xPubKey') |
|
|
|
|
|
|
|
self.storage.save(wcd, function(err) { |
|
|
|
self.save(wcd, function(err) { |
|
|
|
return cb(err, wcd); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -162,7 +166,7 @@ API.prototype._tryToCompleteFromData = function(wcd, toComplete, cb) { |
|
|
|
return cb(ex); |
|
|
|
} |
|
|
|
|
|
|
|
this.storage.save(wcd, function(err) { |
|
|
|
this.save(wcd, function(err) { |
|
|
|
return cb(err, wcd); |
|
|
|
}); |
|
|
|
}; |
|
|
@ -177,15 +181,82 @@ API.prototype._tryToComplete = function(opts, wcd, cb) { |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
// access: 'full' > 'readwrite' > readonly'
|
|
|
|
API.prototype._processWcdAfterRead = function(rawData, requiredAccess, cb) { |
|
|
|
var WU = WalletUtils; |
|
|
|
requiredAccess = requiredAccess || 'full'; |
|
|
|
|
|
|
|
if (!rawData) |
|
|
|
return cb(null, rawData); |
|
|
|
|
|
|
|
var requiredAccessLevel = WU.accessNameToLevel(requiredAccess); |
|
|
|
|
|
|
|
var access = WU.accessFromData(rawData); |
|
|
|
var accessLevel = WU.accessNameToLevel(access); |
|
|
|
|
|
|
|
// Is the data available?
|
|
|
|
if (requiredAccessLevel <= accessLevel) |
|
|
|
return cb(null, rawData); |
|
|
|
|
|
|
|
// Has any encrypted info?
|
|
|
|
if (!rawData.enc) |
|
|
|
return cb('NOTAUTH'); |
|
|
|
|
|
|
|
// Decrypt it and try again
|
|
|
|
this.emit('needPassword', function(password) { |
|
|
|
if (!password) return cb('No password'); |
|
|
|
|
|
|
|
try { |
|
|
|
rawData = WU.decryptWallet(rawData, password); |
|
|
|
} catch (e) {}; |
|
|
|
|
|
|
|
if (!rawData) |
|
|
|
return cb('NOTAUTH'); |
|
|
|
|
|
|
|
access = WU.accessFromData(rawData); |
|
|
|
accessLevel = WU.accessNameToLevel(access); |
|
|
|
|
|
|
|
// Is the data available?
|
|
|
|
if (requiredAccessLevel <= accessLevel) |
|
|
|
return cb(null, rawData); |
|
|
|
|
|
|
|
return cb('NOTAUTH'); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
API.prototype.setNopasswdAccess = function(noPasswdAccess) { |
|
|
|
if (!_.contains(['none', 'readonly', 'readwrite', 'full'], noPasswdAccess)) |
|
|
|
throw new Error('Bad nopasswd access:' + noPasswdAccess); |
|
|
|
|
|
|
|
this.noPasswdAccess = noPasswdAccess; |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype._processWcdBeforeWrite = function(wcd, cb) { |
|
|
|
var self = this; |
|
|
|
// Is any encrypted?
|
|
|
|
if (this.noPasswdAccess == 'full') { |
|
|
|
return cb(null, wcd); |
|
|
|
} else { |
|
|
|
this.emit('needPassword', function(password) { |
|
|
|
if (!password) return cb('No password given'); |
|
|
|
var ewcd = WalletUtils.encryptWallet(wcd, self.noPasswdAccess, password); |
|
|
|
return cb(null, ewcd); |
|
|
|
}); |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
API.prototype._load = function(cb) { |
|
|
|
API.prototype._load = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
$.shouldBeFunction(cb); |
|
|
|
|
|
|
|
this.storage.load(function(err, wcd) { |
|
|
|
if (err || !wcd) { |
|
|
|
this.storage.load(function(err, rawdata) { |
|
|
|
if (err || !rawdata) { |
|
|
|
return cb(err || 'wcd file not found.'); |
|
|
|
} |
|
|
|
return cb(null, wcd); |
|
|
|
self._processWcdAfterRead(rawdata, opts.requiredAccess, cb); |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
@ -198,7 +269,7 @@ API.prototype._load = function(cb) { |
|
|
|
API.prototype._loadAndCheck = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._load(function(err, wcd) { |
|
|
|
this._load(opts, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!wcd.n || (wcd.n > 1 && wcd.publicKeyRing.length != wcd.n)) { |
|
|
@ -275,6 +346,18 @@ API.prototype._doJoinWallet = function(walletId, walletPrivKey, xPubKey, copayer |
|
|
|
}); |
|
|
|
}; |
|
|
|
|
|
|
|
API.prototype.save = function(inWcd, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
self._processWcdBeforeWrite(inWcd, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
self.storage.save(wcd, function(err) { |
|
|
|
return cb(err, null); |
|
|
|
}); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
API.prototype.generateKey = function(network, cb) { |
|
|
|
var self = this; |
|
|
|
network = network || 'livenet'; |
|
|
@ -286,7 +369,8 @@ API.prototype.generateKey = function(network, cb) { |
|
|
|
return cb(self.storage.getName() + ' already contains a wallet'); |
|
|
|
|
|
|
|
var wcd = _initWcd(network); |
|
|
|
self.storage.save(wcd, function(err) { |
|
|
|
|
|
|
|
self.save(wcd, function(err) { |
|
|
|
return cb(err, null); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -327,7 +411,7 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb |
|
|
|
self._doJoinWallet(walletId, walletPrivKey, wcd.publicKeyRing[0], copayerName, |
|
|
|
function(err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
self.storage.save(wcd, function(err) { |
|
|
|
self.save(wcd, function(err) { |
|
|
|
return cb(err, n > 1 ? secret : null); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -338,7 +422,9 @@ API.prototype.createWallet = function(walletName, copayerName, m, n, network, cb |
|
|
|
|
|
|
|
API.prototype.reCreateWallet = function(walletName, cb) { |
|
|
|
var self = this; |
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly', |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var walletPrivKey = new Bitcore.PrivateKey(); |
|
|
@ -386,7 +472,7 @@ API.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
|
function(err, joinedWallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
_addWalletToWcd(wcd, secretData.walletPrivKey, joinedWallet.m, joinedWallet.n); |
|
|
|
self.storage.save(wcd, cb); |
|
|
|
self.save(wcd, cb); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}; |
|
|
@ -394,7 +480,9 @@ API.prototype.joinWallet = function(secret, copayerName, cb) { |
|
|
|
API.prototype.getStatus = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._load(function(err, wcd) { |
|
|
|
this._load({ |
|
|
|
requiredAccess: 'readonly' |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/wallets/'; |
|
|
@ -419,7 +507,9 @@ API.prototype.sendTxProposal = function(opts, cb) { |
|
|
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly', |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!wcd.rwPrivKey) |
|
|
@ -442,7 +532,9 @@ API.prototype.sendTxProposal = function(opts, cb) { |
|
|
|
API.prototype.createAddress = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readwrite', |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/addresses/'; |
|
|
@ -464,7 +556,9 @@ API.prototype.createAddress = function(cb) { |
|
|
|
API.prototype.getMainAddresses = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly', |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
var url = '/v1/addresses/'; |
|
|
@ -486,7 +580,9 @@ API.prototype.getMainAddresses = function(opts, cb) { |
|
|
|
API.prototype.getBalance = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly', |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/balance/'; |
|
|
|
self._doGetRequest(url, wcd, cb); |
|
|
@ -505,7 +601,9 @@ API.prototype.export = function(opts, cb) { |
|
|
|
opts = opts || {}; |
|
|
|
var access = opts.access || 'full'; |
|
|
|
|
|
|
|
this._load(function(err, wcd) { |
|
|
|
this._load({ |
|
|
|
requiredAccess: access, |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
var v = []; |
|
|
|
|
|
|
@ -570,7 +668,7 @@ API.prototype.import = function(str, cb) { |
|
|
|
return cb('Invalid source wallet'); |
|
|
|
|
|
|
|
wcd.network = wcd.publicKeyRing[0].substr(0, 4) == 'tpub' ? 'testnet' : 'livenet'; |
|
|
|
self.storage.save(wcd, function(err) { |
|
|
|
self.save(wcd, function(err) { |
|
|
|
return cb(err, WalletUtils.accessFromData(wcd)); |
|
|
|
}); |
|
|
|
}); |
|
|
@ -584,6 +682,7 @@ API.prototype.parseTxProposals = function(txData, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly', |
|
|
|
toComplete: txData.toComplete |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
@ -614,7 +713,9 @@ API.prototype.parseTxProposals = function(txData, cb) { |
|
|
|
API.prototype.getTxProposals = function(opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly' |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/'; |
|
|
|
self._doGetRequest(url, wcd, function(err, txps) { |
|
|
@ -678,7 +779,9 @@ API.prototype.getSignatures = function(txp, cb) { |
|
|
|
$.checkArgument(txp.creatorId); |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'full' |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!Verifier.checkTxProposal(wcd, txp)) { |
|
|
@ -692,7 +795,9 @@ API.prototype.getSignatures = function(txp, cb) { |
|
|
|
API.prototype.getEncryptedWalletData = function(cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readonly' |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
var toComplete = JSON.stringify(_.pick(wcd, WALLET_AIRGAPPED_TOCOMPLETE)); |
|
|
|
return cb(null, _encryptMessage(toComplete, WalletUtils.privateKeyToAESKey(wcd.roPrivKey))); |
|
|
@ -706,7 +811,9 @@ API.prototype.signTxProposal = function(txp, cb) { |
|
|
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, function(err, wcd) { |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: txp.signatures ? 'readwrite' : 'full' |
|
|
|
}, function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|
if (!Verifier.checkTxProposal(wcd, txp)) { |
|
|
@ -729,7 +836,9 @@ API.prototype.rejectTxProposal = function(txp, reason, cb) { |
|
|
|
|
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readwrite' |
|
|
|
}, |
|
|
|
function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -744,7 +853,9 @@ API.prototype.rejectTxProposal = function(txp, reason, cb) { |
|
|
|
API.prototype.broadcastTxProposal = function(txp, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
this._loadAndCheck({}, |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readwrite' |
|
|
|
}, |
|
|
|
function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -757,7 +868,9 @@ API.prototype.broadcastTxProposal = function(txp, cb) { |
|
|
|
|
|
|
|
API.prototype.removeTxProposal = function(txp, cb) { |
|
|
|
var self = this; |
|
|
|
this._loadAndCheck({}, |
|
|
|
this._loadAndCheck({ |
|
|
|
requiredAccess: 'readwrite' |
|
|
|
}, |
|
|
|
function(err, wcd) { |
|
|
|
if (err) return cb(err); |
|
|
|
var url = '/v1/txproposals/' + txp.id; |
|
|
|