diff --git a/include/dogecoin/wallet.h b/include/dogecoin/wallet.h index a3c31bdef..cb979b6e1 100644 --- a/include/dogecoin/wallet.h +++ b/include/dogecoin/wallet.h @@ -131,7 +131,7 @@ LIBDOGECOIN_API dogecoin_bool dogecoin_wallet_flush(dogecoin_wallet* wallet); LIBDOGECOIN_API void dogecoin_wallet_set_master_key_copy(dogecoin_wallet* wallet, const dogecoin_hdnode* master_xpub); /** derives the next child hdnode and derives an address (memory is owned by the wallet) */ -LIBDOGECOIN_API dogecoin_wallet_addr* dogecoin_wallet_next_addr(dogecoin_wallet* wallet); +LIBDOGECOIN_API void dogecoin_wallet_next_addr(dogecoin_wallet_addr* waddr, dogecoin_wallet* wallet); LIBDOGECOIN_API dogecoin_wallet_addr* dogecoin_wallet_next_bip44_addr(dogecoin_wallet* wallet); LIBDOGECOIN_API dogecoin_bool dogecoin_p2pkh_address_to_wallet_pubkeyhash(const char* address_in, dogecoin_wallet_addr* addr, dogecoin_wallet* wallet); diff --git a/src/cli/spvnode.c b/src/cli/spvnode.c index d39b57185..f74a7da28 100644 --- a/src/cli/spvnode.c +++ b/src/cli/spvnode.c @@ -315,7 +315,7 @@ dogecoin_wallet* dogecoin_wallet_init(const dogecoin_chainparams* chain, const c // TODO } - dogecoin_wallet_addr* waddr; + dogecoin_wallet_addr* waddr = dogecoin_calloc(1, sizeof(*waddr)); if (address != NULL) { char delim[] = " "; @@ -347,7 +347,7 @@ dogecoin_wallet* dogecoin_wallet_init(const dogecoin_chainparams* chain, const c } #else else if (wallet->waddr_vector->len == 0) { - waddr = dogecoin_wallet_next_addr(wallet); + dogecoin_wallet_next_addr(waddr, wallet); } #endif diff --git a/src/wallet.c b/src/wallet.c index e5e02719f..2912a5acf 100644 --- a/src/wallet.c +++ b/src/wallet.c @@ -670,13 +670,12 @@ void dogecoin_wallet_set_master_key_copy(dogecoin_wallet* wallet, const dogecoin dogecoin_file_commit(wallet->dbfile); } -dogecoin_wallet_addr* dogecoin_wallet_next_addr(dogecoin_wallet* wallet) +void dogecoin_wallet_next_addr(dogecoin_wallet_addr *waddr, dogecoin_wallet* wallet) { if (!wallet || !wallet->masterkey) - return NULL; + return; //for now, only m/k is possible - dogecoin_wallet_addr *waddr = dogecoin_wallet_addr_new(); dogecoin_hdnode *hdnode = dogecoin_hdnode_copy(wallet->masterkey); dogecoin_hdnode_public_ckd(hdnode, wallet->next_childindex); dogecoin_hdnode_get_hash160(hdnode, waddr->pubkeyhash); @@ -732,7 +731,7 @@ dogecoin_wallet_addr* dogecoin_wallet_next_addr(dogecoin_wallet* wallet) //increase the in-memory counter (cache) wallet->next_childindex++; - return waddr; + return; } dogecoin_wallet_addr* dogecoin_wallet_next_bip44_addr(dogecoin_wallet* wallet) diff --git a/test/wallet_tests.c b/test/wallet_tests.c index 8a52d1797..509632485 100644 --- a/test/wallet_tests.c +++ b/test/wallet_tests.c @@ -202,17 +202,19 @@ void test_wallet_basics() u_assert_int_eq(suc, 1); dogecoin_wallet_set_master_key_copy(wallet, &node); - dogecoin_wallet_addr *wallet_addr = dogecoin_wallet_next_addr(wallet); + dogecoin_wallet_addr *wallet_addr = dogecoin_calloc(1, sizeof(*wallet_addr)); + dogecoin_wallet_next_addr(wallet_addr, wallet); u_assert_int_eq(wallet_addr->childindex, 0); dogecoin_wallet_new(wallet, &dogecoin_chainparams_main); u_assert_int_eq(dogecoin_wallet_load(wallet, wallettmpfile, &error, &created), true); - dogecoin_wallet_addr *wallet_addr2 = dogecoin_wallet_next_addr(wallet); + dogecoin_wallet_addr *wallet_addr2 = dogecoin_calloc(1, sizeof(*wallet_addr2)); + dogecoin_wallet_next_addr(wallet_addr2, wallet); u_assert_int_eq(wallet_addr2->childindex, 1); //force to regenerate child 1 wallet->next_childindex = 1; - wallet_addr = dogecoin_wallet_next_addr(wallet); + dogecoin_wallet_next_addr(wallet_addr, wallet); //now it should be equal u_assert_mem_eq(wallet_addr->pubkeyhash, wallet_addr2->pubkeyhash, sizeof(uint160));