wallet: create set_subaddress_lookahead wallet rpc endpoint

This commit is contained in:
benevanoff 2023-11-03 18:40:57 -05:00 committed by nahuhh
parent b987676d63
commit 6f36037116
5 changed files with 63 additions and 1 deletions

View file

@ -704,6 +704,18 @@ namespace tools
res.index = *index;
return true;
}
bool wallet_rpc_server::on_set_subaddr_lookahead(const wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::request& req, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::response& res, epee::json_rpc::error& er, const connection_context *ctx)
{
if (!m_wallet) return not_open(er);
try {
m_wallet->set_subaddress_lookahead(req.major_idx, req.minor_idx);
}
catch (const std::exception& e) {
handle_rpc_exception(std::current_exception(), er, WALLET_RPC_ERROR_CODE_UNKNOWN_ERROR);
return false;
}
return true;
}
//------------------------------------------------------------------------------------------------------------------------------
bool wallet_rpc_server::on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx)
{

View file

@ -71,6 +71,7 @@ namespace tools
MAP_JON_RPC_WE("get_balance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE)
MAP_JON_RPC_WE("get_address", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS)
MAP_JON_RPC_WE("get_address_index", on_getaddress_index, wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX)
MAP_JON_RPC_WE("set_subaddress_lookahead", on_set_subaddr_lookahead, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD)
MAP_JON_RPC_WE("getbalance", on_getbalance, wallet_rpc::COMMAND_RPC_GET_BALANCE)
MAP_JON_RPC_WE("getaddress", on_getaddress, wallet_rpc::COMMAND_RPC_GET_ADDRESS)
MAP_JON_RPC_WE("create_address", on_create_address, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS)
@ -172,6 +173,7 @@ namespace tools
bool on_getbalance(const wallet_rpc::COMMAND_RPC_GET_BALANCE::request& req, wallet_rpc::COMMAND_RPC_GET_BALANCE::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_getaddress(const wallet_rpc::COMMAND_RPC_GET_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_getaddress_index(const wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX::request& req, wallet_rpc::COMMAND_RPC_GET_ADDRESS_INDEX::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_set_subaddr_lookahead(const wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::request& req, wallet_rpc::COMMAND_RPC_SET_SUBADDR_LOOKAHEAD::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_create_address(const wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_CREATE_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_label_address(const wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::request& req, wallet_rpc::COMMAND_RPC_LABEL_ADDRESS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);
bool on_get_accounts(const wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::request& req, wallet_rpc::COMMAND_RPC_GET_ACCOUNTS::response& res, epee::json_rpc::error& er, const connection_context *ctx = NULL);

View file

@ -47,7 +47,7 @@
// advance which version they will stop working with
// Don't go over 32767 for any of these
#define WALLET_RPC_VERSION_MAJOR 1
#define WALLET_RPC_VERSION_MINOR 28
#define WALLET_RPC_VERSION_MINOR 29
#define MAKE_WALLET_RPC_VERSION(major,minor) (((major)<<16)|(minor))
#define WALLET_RPC_VERSION MAKE_WALLET_RPC_VERSION(WALLET_RPC_VERSION_MAJOR, WALLET_RPC_VERSION_MINOR)
namespace tools
@ -182,6 +182,27 @@ namespace wallet_rpc
typedef epee::misc_utils::struct_init<response_t> response;
};
struct COMMAND_RPC_SET_SUBADDR_LOOKAHEAD
{
struct request_t
{
uint32_t major_idx;
uint32_t minor_idx;
BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(major_idx)
KV_SERIALIZE(minor_idx)
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<request_t> request;
struct response_t
{
BEGIN_KV_SERIALIZE_MAP()
END_KV_SERIALIZE_MAP()
};
typedef epee::misc_utils::struct_init<response_t> response;
};
struct COMMAND_RPC_CREATE_ADDRESS
{
struct request_t

View file

@ -46,6 +46,7 @@ class WalletTest():
self.check_keys()
self.create_subaddresses()
self.tags()
self.update_lookahead()
self.attributes()
self.open_close()
self.languages()
@ -163,6 +164,20 @@ class WalletTest():
res = wallet.label_account(0, "main")
def update_lookahead(self):
print('Updating subaddress lookahead')
wallet = Wallet()
address_0_999 = '8BQKgTSSqJjP14AKnZUBwnXWj46MuNmLvHfPTpmry52DbfNjjHVvHUk4mczU8nj8yZ57zBhksTJ8kM5xKeJXw55kCMVqyG7' # this is the address for address 999 of the main account in the test wallet
try: # assert address_1_999 is not in the current pubkey table
wallet.get_address_index(address_0_999)
except Exception as e:
assert str(e) == "{'error': {'code': -2, 'message': \"Address doesn't belong to the wallet\"}, 'id': '0', 'jsonrpc': '2.0'}"
# update the lookahead and assert the high index address is now in the table
wallet.set_subaddress_lookahead(50, 1000)
r = wallet.get_address_index(address_0_999)
assert r['index']['major'] == 0
assert r['index']['minor'] == 999
def tags(self):
print('Testing tags')
wallet = Wallet()

View file

@ -334,6 +334,18 @@ class Wallet(object):
}
return self.rpc.send_json_rpc_request(generate_from_keys)
def set_subaddress_lookahead(self, major_idx: int, minor_idx: int):
lookahead = {
'method': 'set_subaddress_lookahead',
'jsonrpc': '2.0',
'params' : {
'major_idx': major_idx,
'minor_idx': minor_idx
},
'id': '0'
}
return self.rpc.send_json_rpc_request(lookahead)
def open_wallet(self, filename, password='', autosave_current = True):
open_wallet = {
'method': 'open_wallet',