|
|
@ -5,11 +5,13 @@ var $ = require('preconditions').singleton(); |
|
|
|
var async = require('async'); |
|
|
|
var log = require('npmlog'); |
|
|
|
log.debug = log.verbose; |
|
|
|
|
|
|
|
var Bitcore = require('bitcore'); |
|
|
|
var PublicKey = Bitcore.PublicKey; |
|
|
|
var HDPublicKey = Bitcore.HDPublicKey; |
|
|
|
var Explorers = require('bitcore-explorers'); |
|
|
|
|
|
|
|
var utils = require('./utils'); |
|
|
|
var Lock = require('./lock'); |
|
|
|
var Storage = require('./storage'); |
|
|
|
var SignUtils = require('./signutils'); |
|
|
@ -45,6 +47,11 @@ function CopayServer(opts) { |
|
|
|
CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
var self = this, pubKey; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['id', 'name', 'm', 'n', 'pubKey']); |
|
|
|
if (!Wallet.verifyCopayerLimits(opts.m, opts.n)) return cb('Incorrect m or n value'); |
|
|
|
var network = opts.network || 'livenet'; |
|
|
|
if (network != 'livenet' && network != 'testnet') return cb('Invalid network'); |
|
|
|
|
|
|
|
try { |
|
|
|
pubKey = new PublicKey.fromString(opts.pubKey); |
|
|
|
} catch (e) { |
|
|
@ -60,7 +67,7 @@ CopayServer.prototype.createWallet = function (opts, cb) { |
|
|
|
name: opts.name, |
|
|
|
m: opts.m, |
|
|
|
n: opts.n, |
|
|
|
network: opts.network || 'livenet', |
|
|
|
network: network, |
|
|
|
pubKey: pubKey, |
|
|
|
}); |
|
|
|
|
|
|
@ -119,6 +126,8 @@ CopayServer.prototype._verifySignature = function (text, signature, pubKey) { |
|
|
|
CopayServer.prototype.joinWallet = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'id', 'name', 'xPubKey', 'xPubKeySignature']); |
|
|
|
|
|
|
|
self._runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
@ -164,6 +173,8 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
|
CopayServer.prototype.createAddress = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'isChange']); |
|
|
|
|
|
|
|
self._runLocked(opts.walletId, cb, function (cb) { |
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
@ -195,6 +206,8 @@ CopayServer.prototype._doCreateAddress = function (pkr, index, isChange) { |
|
|
|
CopayServer.prototype.verifyMessageSignature = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'copayerId', 'message', 'signature']); |
|
|
|
|
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -275,6 +288,8 @@ CopayServer.prototype._getUtxos = function (opts, cb) { |
|
|
|
CopayServer.prototype.getBalance = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, 'walletId'); |
|
|
|
|
|
|
|
self._getUtxos({ walletId: opts.walletId }, function (err, utxos) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -326,6 +341,8 @@ CopayServer.prototype._selectUtxos = function (txp, utxos) { |
|
|
|
CopayServer.prototype.createTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'copayerId', 'toAddress', 'amount', 'message']); |
|
|
|
|
|
|
|
self.getWallet({ id: opts.walletId }, function (err, wallet) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
@ -373,6 +390,8 @@ CopayServer.prototype._broadcastTx = function (rawTx, cb) { |
|
|
|
CopayServer.prototype.signTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId', 'signature']); |
|
|
|
|
|
|
|
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
@ -406,10 +425,13 @@ CopayServer.prototype.signTx = function (opts, cb) { |
|
|
|
* @param {string} opts.walletId - The wallet id. |
|
|
|
* @param {string} opts.copayerId - The wallet id. |
|
|
|
* @param {string} opts.txProposalId - The identifier of the transaction. |
|
|
|
* @param {string} [opts.reason] - A message to other copayers explaining the rejection. |
|
|
|
*/ |
|
|
|
CopayServer.prototype.rejectTx = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, ['walletId', 'copayerId', 'txProposalId']); |
|
|
|
|
|
|
|
self.fetchTx(opts.walletId, opts.txProposalId, function (err, txp) { |
|
|
|
if (err) return cb(err); |
|
|
|
if (!txp) return cb('Transaction proposal not found'); |
|
|
@ -436,6 +458,8 @@ CopayServer.prototype.rejectTx = function (opts, cb) { |
|
|
|
CopayServer.prototype.getPendingTxs = function (opts, cb) { |
|
|
|
var self = this; |
|
|
|
|
|
|
|
utils.checkRequired(opts, 'walletId'); |
|
|
|
|
|
|
|
self.storage.fetchTxs(opts.walletId, function (err, txps) { |
|
|
|
if (err) return cb(err); |
|
|
|
|
|
|
|