Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RPC: fix several wallet RPC calls and add RPC tests #550

Merged
merged 1 commit into from
Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 22 additions & 33 deletions lib/wallet/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,19 +257,11 @@ class RPC extends RPCBase {
}

async addMultisigAddress(args, help) {
if (help || args.length < 2 || args.length > 3) {
throw new RPCError(errs.MISC_ERROR,
'addmultisigaddress nrequired ["key",...] ( "account" )');
}

// Impossible to implement in bcoin (no address book).
throw new Error('Not implemented.');
}

async addWitnessAddress(args, help) {
if (help || args.length < 1 || args.length > 1)
throw new RPCError(errs.MISC_ERROR, 'addwitnessaddress "address"');

// Unlikely to be implemented.
throw new Error('Not implemented.');
}
Expand Down Expand Up @@ -491,7 +483,7 @@ class RPC extends RPCBase {
const valid = new Validator(args);
let name = valid.str(0);

if (name === '')
if (name === '' || args.length === 0)
name = 'default';

const addr = await wallet.createReceive(name);
Expand All @@ -500,7 +492,7 @@ class RPC extends RPCBase {
}

async getRawChangeAddress(args, help) {
if (help || args.length > 1)
if (help || args.length !== 0)
throw new RPCError(errs.MISC_ERROR, 'getrawchangeaddress');

const wallet = this.wallet;
Expand Down Expand Up @@ -750,7 +742,7 @@ class RPC extends RPCBase {
}

async importWallet(args, help) {
if (help || args.length !== 1)
if (help || args.length < 1 || args.length > 2)
throw new RPCError(errs.MISC_ERROR, 'importwallet "filename" ( rescan )');

const wallet = this.wallet;
Expand Down Expand Up @@ -839,7 +831,7 @@ class RPC extends RPCBase {
}

async importPubkey(args, help) {
if (help || args.length < 1 || args.length > 4) {
if (help || args.length < 1 || args.length > 3) {
throw new RPCError(errs.MISC_ERROR,
'importpubkey "pubkey" ( "label" rescan )');
}
Expand Down Expand Up @@ -899,8 +891,6 @@ class RPC extends RPCBase {
}

async listAddressGroupings(args, help) {
if (help)
throw new RPCError(errs.MISC_ERROR, 'listaddressgroupings');
throw new Error('Not implemented.');
}

Expand Down Expand Up @@ -1033,18 +1023,18 @@ class RPC extends RPCBase {
}

async listSinceBlock(args, help) {
if (help || args.length > 3) {
throw new RPCError(errs.MISC_ERROR,
'listsinceblock ( "blockhash" target-confirmations includeWatchonly)');
}

const wallet = this.wallet;
const chainHeight = this.wdb.state.height;
const valid = new Validator(args);
const block = valid.brhash(0);
const minconf = valid.u32(1, 0);
const watchOnly = valid.bool(2, false);

if (help) {
throw new RPCError(errs.MISC_ERROR,
'listsinceblock ( "blockhash" target-confirmations includeWatchonly)');
}

if (wallet.watchOnly !== watchOnly)
return [];

Expand All @@ -1054,10 +1044,12 @@ class RPC extends RPCBase {
const entry = await this.client.getEntry(block);
if (entry)
height = entry.height;
else
throw new RPCError(errs.MISC_ERROR, 'Block not found');
}

if (height === -1)
height = this.chain.height;
height = chainHeight;

const txs = await wallet.getHistory();
const out = [];
Expand Down Expand Up @@ -1192,7 +1184,7 @@ class RPC extends RPCBase {
if (name === '')
name = 'default';

const txs = await wallet.getHistory();
const txs = await wallet.getHistory(name);

common.sortTX(txs);

Expand Down Expand Up @@ -1368,7 +1360,7 @@ class RPC extends RPCBase {
if (help || args.length < 2 || args.length > 5) {
throw new RPCError(errs.MISC_ERROR,
'sendmany "fromaccount" {"address":amount,...}'
+ ' ( minconf "comment" ["address",...] )');
+ ' ( minconf "comment" subtractfee )');
braydonf marked this conversation as resolved.
Show resolved Hide resolved
}

const wallet = this.wallet;
Expand All @@ -1382,7 +1374,7 @@ class RPC extends RPCBase {
name = 'default';

if (!sendTo)
throw new RPCError(errs.TYPE_ERROR, 'Invalid parameter.');
throw new RPCError(errs.TYPE_ERROR, 'Invalid send-to address.');

const to = new Validator(sendTo);
const uniq = new BufferSet();
Expand All @@ -1394,10 +1386,11 @@ class RPC extends RPCBase {
const hash = addr.getHash();

if (value == null)
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid parameter.');
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid amount.');

if (uniq.has(hash))
braydonf marked this conversation as resolved.
Show resolved Hide resolved
throw new RPCError(errs.INVALID_PARAMETER, 'Invalid parameter.');
throw new RPCError(errs.INVALID_PARAMETER,
'Each send-to address must be unique.');

uniq.add(hash);

Expand Down Expand Up @@ -1451,11 +1444,6 @@ class RPC extends RPCBase {
}

async setAccount(args, help) {
if (help || args.length < 1 || args.length > 2) {
throw new RPCError(errs.MISC_ERROR,
'setaccount "bitcoinaddress" "account"');
}

// Impossible to implement in bcoin:
throw new Error('Not implemented.');
}
Expand Down Expand Up @@ -1576,9 +1564,9 @@ class RPC extends RPCBase {
}

async importPrunedFunds(args, help) {
if (help || args.length < 2 || args.length > 3) {
if (help || args.length !== 2) {
throw new RPCError(errs.MISC_ERROR,
'importprunedfunds "rawtransaction" "txoutproof" ( "label" )');
'importprunedfunds "rawtransaction" "txoutproof"');
}

const valid = new Validator(args);
Expand Down Expand Up @@ -1610,7 +1598,8 @@ class RPC extends RPCBase {
};

if (!await this.wdb.addTX(tx, entry))
throw new RPCError(errs.WALLET_ERROR, 'No tracked address for TX.');
throw new RPCError(errs.WALLET_ERROR,
'Address for TX not in wallet, or TX already in wallet');

return null;
}
Expand Down
9 changes: 9 additions & 0 deletions lib/wallet/txdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1176,6 +1176,15 @@ class TXDB {
return this.locked.delete(key);
}

/**
* Unlock all coins.
*/

unlockCoins() {
for (const coin of this.getLocked())
this.unlockCoin(coin);
}

/**
* Test locked status of a single coin.
* @param {Coin|Outpoint} coin
Expand Down
8 changes: 8 additions & 0 deletions lib/wallet/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -1955,6 +1955,14 @@ class Wallet extends EventEmitter {
return this.txdb.unlockCoin(coin);
}

/**
* Unlock all locked coins.
*/

unlockCoins() {
return this.txdb.unlockCoins();
}

/**
* Test locked status of a single coin.
* @param {Coin|Outpoint} coin
Expand Down
2 changes: 1 addition & 1 deletion lib/wallet/walletdb.js
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,7 @@ class WalletDB extends EventEmitter {
async getWalletPaths(wid) {
const items = await this.db.range({
gte: layout.P.min(wid),
lte: layout.P.min(wid)
lte: layout.P.max(wid)
});

const paths = [];
Expand Down
Loading