diff --git a/lib/bcoin/http/rpc.js b/lib/bcoin/http/rpc.js index 15e2c648..d8d0f40b 100644 --- a/lib/bcoin/http/rpc.js +++ b/lib/bcoin/http/rpc.js @@ -1933,7 +1933,46 @@ function mergeSigs(a, b) { } RPC.prototype.fundrawtransaction = function fundrawtransaction(args, callback) { - callback(new Error('Not implemented.')); + var tx, options, changeAddress, feeRate; + + if (args.help || args.length < 1 || args.length > 2) { + return callback(new RPCError('fundrawtransaction' + + ' "hexstring" ( options )')); + } + + if (!utils.isHex(args[0])) + return callback(new RPCError('Invalid parameter.')); + + tx = bcoin.mtx.fromRaw(args[0], 'hex'); + + if (tx.outputs.length === 0) + return callback(new RPCError('TX must have at least one output.')); + + if (args.length === 2 && args[1]) { + options = args[1]; + changeAddress = options.changeAddress; + if (changeAddress) + changeAddress = bcoin.address.fromBase58(String(changeAddress)); + feeRate = options.feeRate; + if (feeRate != null) + feeRate = utils.satoshi(feeRate + ''); + } + + options = { + rate: feeRate, + changeAddress: changeAddress + }; + + this.wallet.fund(tx, options, function(err) { + if (err) + return callback(err); + + callback(null, { + hex: tx.toRaw().toString('hex'), + changepos: tx.changeIndex, + fee: +utils.btc(tx.getFee()) + }); + }); }; RPC.prototype._createRedeem = function _createRedeem(args, callback) {