diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index b07e431a..1daeda9c 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -769,6 +769,7 @@ RPC.prototype.getblock = function getblock(args, callback) { }; RPC.prototype.txToJSON = function txToJSON(tx) { + var self = this; return { txid: tx.txid, hash: tx.wtxid, @@ -800,17 +801,17 @@ RPC.prototype.txToJSON = function txToJSON(tx) { return { value: +utils.btc(output.value), n: i, - scriptPubKey: scriptToJSON(output.script, true) + scriptPubKey: self._scriptToJSON(output.script, true) }; }), blockhash: tx.block || null, - confirmations: tx.getConfirmations(), + confirmations: tx.getConfirmations(this.chain.height), time: tx.ts, blocktime: tx.ts }; }; -function scriptToJSON(script, hex) { +RPC.prototype._scriptToJSON = function scriptToJSON(script, hex) { var out = {}; var type, address; @@ -826,10 +827,10 @@ function scriptToJSON(script, hex) { address = script.getAddress(); - out.addresses = address ? [address.toBase58()] : []; + out.addresses = address ? [address.toBase58(this.network)] : []; return out; -} +}; RPC.prototype.getblockhash = function getblockhash(args, callback) { var height; @@ -1097,7 +1098,7 @@ RPC.prototype.mempoolToJSON = function mempoolToJSON(verbose, callback) { descendantcount: 0, descendantsize: entry.size, descendantfees: entry.fees, - depends: tx.getPrevout().map(utils.revHex) + depends: [] }; next(); @@ -1147,9 +1148,9 @@ RPC.prototype.gettxout = function gettxout(args, callback) { callback(null, { bestblock: utils.revHex(self.chain.tip.hash), - confirmations: coin.getConfirmations(), + confirmations: coin.getConfirmations(self.chain.height), value: +utils.btc(coin.value), - scriptPubKey: scriptToJSON(coin.script, true), + scriptPubKey: self._scriptToJSON(coin.script, true), version: coin.version, coinbase: coin.coinbase }); @@ -1834,7 +1835,7 @@ RPC.prototype.createrawtransaction = function createrawtransaction(args, callbac } address = bcoin.address.fromBase58(key); - b58 = address.toBase58(); + b58 = address.toBase58(this.network); if (addrs[b58]) return callback(new RPCError('Duplicate address')); @@ -1881,8 +1882,8 @@ RPC.prototype.decodescript = function decodescript(args, callback) { hash = utils.hash160(script.toRaw()); address = bcoin.address.fromHash(hash, bcoin.script.types.SCRIPTHASH); - script = scriptToJSON(script); - script.p2sh = address.toBase58(); + script = this._scriptToJSON(script); + script.p2sh = address.toBase58(this.network); callback(null, script); }; @@ -2212,6 +2213,8 @@ RPC.prototype._createRedeem = function _createRedeem(args, callback) { /* Utility functions */ RPC.prototype.createmultisig = function createmultisig(args, callback) { + var self = this; + if (args.help || args.length < 2 || args.length > 2) return callback(new RPCError('createmultisig nrequired ["key",...]')); @@ -2220,7 +2223,7 @@ RPC.prototype.createmultisig = function createmultisig(args, callback) { return callback(err); callback(null, { - address: script.getAddress().toBase58(), + address: script.getAddress().toBase58(self.network), redeemScript: script.toJSON() }); }); @@ -2258,12 +2261,13 @@ RPC.prototype.createwitnessaddress = function createwitnessaddress(args, callbac program = this._scriptForWitness(script); callback(null, { - address: program.getAddress().toBase58(), + address: program.getAddress().toBase58(this.network), witnessScript: program.toJSON() }); }; RPC.prototype.validateaddress = function validateaddress(args, callback) { + var self = this; var b58, address, json; if (args.help || args.length !== 1) @@ -2285,7 +2289,7 @@ RPC.prototype.validateaddress = function validateaddress(args, callback) { json = { isvalid: true, - address: address.toBase58(), + address: address.toBase58(self.network), scriptPubKey: address.toScript().toJSON(), ismine: ring ? true : false, iswatchonly: false @@ -2902,6 +2906,7 @@ RPC.prototype.getreceivedbyaddress = function getreceivedbyaddress(args, callbac }; RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { + var self = this; var receive, member, json; this.walletdb.tx.getMap(tx, function(err, map) { @@ -2917,7 +2922,7 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { json = { amount: +utils.btc(tx.getOutputValue()), - confirmations: tx.getConfirmations(), + confirmations: tx.getConfirmations(self.chain.height), blockhash: tx.block ? utils.revHex(tx.block) : null, blockindex: tx.index, blocktime: tx.ts, @@ -2928,7 +2933,7 @@ RPC.prototype._toWalletTX = function _toWalletTX(tx, callback) { 'bip125-replaceable': 'no', details: [{ account: member.name, - address: member.paths[0].address.toBase58(), + address: member.paths[0].address.toBase58(self.network), category: receive ? 'receive' : 'send', amount: +utils.btc(member.value), label: member.name, @@ -3163,7 +3168,7 @@ RPC.prototype.listsinceblock = function listsinceblock(args, callback) { if (tx.height < height) return next(); - if (tx.getConfirmations() < conf) + if (tx.getConfirmations(self.chain.height) < conf) return next(); if (!highest || tx.height > highest) @@ -3191,6 +3196,7 @@ RPC.prototype.listsinceblock = function listsinceblock(args, callback) { }; RPC.prototype._toListTX = function _toListTX(tx, callback) { + var self = this; var receive, member, json; this.walletdb.tx.getMap(tx, function(err, map) { @@ -3206,12 +3212,12 @@ RPC.prototype._toListTX = function _toListTX(tx, callback) { json = { account: member.name, - address: member.paths[0].address.toBase58(), + address: member.paths[0].address.toBase58(self.network), category: receive ? 'receive' : 'send', amount: +utils.btc(member.value), label: member.name, vout: 0, - confirmations: tx.getConfirmations(), + confirmations: tx.getConfirmations(self.chain.height), blockhash: tx.block ? utils.revHex(tx.block) : null, blockindex: tx.index, blocktime: tx.ts, @@ -3344,7 +3350,7 @@ RPC.prototype.listunspent = function listunspent(args, callback) { out.push({ txid: utils.revHex(coin.hash), vout: coin.index, - address: address ? address.toBase58() : null, + address: address ? address.toBase58(self.network) : null, account: ring ? ring.name : undefined, redeemScript: ring && ring.script ? ring.script.toJSON()