From d391782cd9d726634b64ebdbdc1fa0c7ea7f56c1 Mon Sep 17 00:00:00 2001 From: Gregory Sanders Date: Mon, 17 Dec 2018 17:15:27 -0500 Subject: [PATCH] Add getwalletpakinfo RPC call --- src/wallet/rpcdump.cpp | 50 ++++++++++++++++++++++++++++++++++++++++ src/wallet/rpcwallet.cpp | 2 ++ 2 files changed, 52 insertions(+) diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 5800e75827..bf9a03f50b 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -1275,3 +1275,53 @@ UniValue importmulti(const JSONRPCRequest& mainRequest) return response; } + +extern CTxDestination DeriveBitcoinOfflineAddress(const CExtPubKey& xpub, const uint32_t counter); + +UniValue getwalletpakinfo(const JSONRPCRequest& request) +{ + std::shared_ptr const wallet = GetWalletForJSONRPCRequest(request); + CWallet* const pwallet = wallet.get(); + + if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { + return NullUniValue; + } + + if (request.fHelp || request.params.size() != 0) + throw std::runtime_error( + "getwalletpakinfo\n" + "\nReturns relevant pegout authorization key (PAK) information about this wallet. Throws an error if initpegoutwallet` has not been invoked on this wallet.\n" + "\nResult:\n" + "{\n" + "\"derivation_path\" (string) The next index to be used by the wallet for `sendtomainchain`.\n" + "\"bitcoin_xpub\" (string) The Bitcoin xpubkey loaded in the wallet for pegouts.\n" + "\"liquid_pak\" (string) Pubkey in hex corresponding to the Liquid PAK loaded in the wallet for pegouts.\n" + "\"liquid_pak_address\" (string) The corresponding address for `liquid_pak`. Useful for `dumpprivkey` for wallet backup or transfer.\n" + "\"address_lookahead\"(array) The three next Bitcoin addresses the wallet will use for `sendtomainchain` based on the internal counter.\n" + "}\n" + ); + + LOCK2(cs_main, pwallet->cs_wallet); + + if (pwallet->offline_counter == -1) { + throw JSONRPCError(RPC_MISC_ERROR, "This wallet has not been initialized for PAK-enforced peg-outs."); + } + + UniValue ret(UniValue::VOBJ); + std::stringstream ss; + ss << pwallet->offline_counter; + ret.push_back(Pair("derivation_path", "/0/"+ss.str())); + + CExtPubKey& xpub = pwallet->offline_xpub; + + ret.pushKV("bitcoin_xpub", EncodeExtPubKey(xpub)); + ret.pushKV("liquid_pak", HexStr(pwallet->online_key)); + ret.pushKV("liquid_pak_address", EncodeDestination((pwallet->online_key.GetID()))); + + UniValue address_list(UniValue::VARR); + for (unsigned int i = 0; i < 3; i++) { + address_list.push_back(EncodeParentDestination(DeriveBitcoinOfflineAddress(xpub, pwallet->offline_counter+i))); + } + ret.push_back(Pair("address_lookahead", address_list)); + return ret; +} diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 9eed789078..639fb14507 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5648,6 +5648,7 @@ extern UniValue importprunedfunds(const JSONRPCRequest& request); extern UniValue removeprunedfunds(const JSONRPCRequest& request); extern UniValue importmulti(const JSONRPCRequest& request); extern UniValue rescanblockchain(const JSONRPCRequest& request); +extern UniValue getwalletpakinfo(const JSONRPCRequest& request); static const CRPCCommand commands[] = { // category name actor (function) argNames @@ -5708,6 +5709,7 @@ static const CRPCCommand commands[] = { "wallet", "createrawpegin", &createrawpegin, {"bitcoin_tx", "txoutproof", "claim_script"} }, { "wallet", "sendtomainchain", &sendtomainchain, {"address", "amount", "subtractfeefromamount"} }, { "wallet", "initpegoutwallet", &initpegoutwallet, {"bitcoin_xpub", "bip32_counter", "liquid_pak"} }, + { "wallet", "getwalletpakinfo", &getwalletpakinfo, {} }, /** Account functions (deprecated) */