wallet: Implement `z_getnewaccount`

Closes zcash/zcash#5178.
This commit is contained in:
Jack Grigg 2022-01-13 18:45:40 +00:00
parent 6118432712
commit fe384eeb1f
4 changed files with 47 additions and 9 deletions

View File

@ -64,6 +64,7 @@ BASE_SCRIPTS= [
'p2p-fullblocktest.py',
# vv Tests less than 30s vv
'wallet_1941.py',
'wallet_accounts.py',
'wallet_addresses.py',
'wallet_anchorfork.py',
'wallet_changeindicator.py',

31
qa/rpc-tests/wallet_accounts.py Executable file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env python3
# Copyright (c) 2022 The Zcash developers
# Distributed under the MIT software license, see the accompanying
# file COPYING or https://www.opensource.org/licenses/mit-license.php .
from test_framework.test_framework import BitcoinTestFramework
from test_framework.util import (
assert_equal,
start_nodes,
)
# Test wallet accounts behaviour
class WalletAccountsTest(BitcoinTestFramework):
def setup_nodes(self):
return start_nodes(self.num_nodes, self.options.tmpdir, [[
'-experimentalfeatures',
'-orchardwallet',
]] * self.num_nodes)
def run_test(self):
# With a new wallet, the first account will be 0.
account0 = self.nodes[0].z_getnewaccount()
assert_equal(account0['account'], 0)
# The next account will be 1.
account1 = self.nodes[0].z_getnewaccount()
assert_equal(account1['account'], 1)
if __name__ == '__main__':
WalletAccountsTest().main()

View File

@ -16,6 +16,7 @@
#include "proof_verifier.h"
#include "rpc/server.h"
#include "timedata.h"
#include "tinyformat.h"
#include "transaction_builder.h"
#include "util.h"
#include "util/match.h"
@ -3016,18 +3017,15 @@ UniValue z_getnewaccount(const UniValue& params, bool fHelp)
if (fHelp || params.size() > 0)
throw runtime_error(
"z_getnewaccount\n"
"\nPrepares and returns a new account, and its corresponding default address.\n"
"\nPrepares and returns a new account.\n"
"\nAccounts are numbered starting from zero; this RPC method selects the next"
"\navailable sequential account number within the UA-compatible HD seed phrase.\n"
"\nThe account will be prepared with spending keys for the best and second-best"
"\nshielded pools, and the transparent pool.\n"
"\nEach new account is a separate group of funds within the wallet, and adds an"
"\nadditional performance cost to wallet scanning. If you want a new address"
"\nfor an existing account, use the z_getaddressforaccount RPC method.\n"
"\nadditional performance cost to wallet scanning.\n"
"\nUse the z_getaddressforaccount RPC method to obtain addresses for an account.\n"
"\nResult:\n"
"{\n"
" \"account\": n, (numeric) the new account number\n"
" \"unifiedaddress\" (string) The default address for this account\n"
"}\n"
"\nExamples:\n"
+ HelpExampleCli("z_getnewaccount", "")
@ -3037,10 +3035,17 @@ UniValue z_getnewaccount(const UniValue& params, bool fHelp)
if (!fExperimentalOrchardWallet) {
throw JSONRPCError(RPC_WALLET_ENCRYPTION_FAILED, "Error: the Orchard wallet experimental extensions are disabled.");
}
int64_t account = 999999; // TODO placeholder
LOCK(pwalletMain->cs_wallet);
EnsureWalletIsUnlocked();
// Generate the new account.
auto skNew = pwalletMain->GenerateNewUnifiedSpendingKey();
const auto& account = skNew.second;
UniValue result(UniValue::VOBJ);
result.pushKV("account", account);
result.pushKV("unifiedaddress", "TODO");
result.pushKV("account", (uint64_t)account);
return result;
}

View File

@ -101,6 +101,7 @@ public:
}
void IncrementAccountCounter() {
// TODO: We should check for overflow somewhere and handle it.
accountCounter += 1;
}