| 152 | } |
| 153 | |
| 154 | RPCHelpMan getpeginaddress() |
| 155 | { |
| 156 | return RPCHelpMan{"getpeginaddress", |
| 157 | "\nReturns information needed for claimpegin to move coins to the sidechain.\n" |
| 158 | "The user should send coins from their Bitcoin wallet to the mainchain_address returned.\n", |
| 159 | {}, |
| 160 | RPCResult{ |
| 161 | RPCResult::Type::OBJ, "", "", |
| 162 | { |
| 163 | {RPCResult::Type::STR, "mainchain_address", "mainchain deposit address to send bitcoin to"}, |
| 164 | {RPCResult::Type::STR_HEX, "claim_script", "claim script committed to by the mainchain address. This may be required in `claimpegin` to retrieve pegged-in funds\n"}, |
| 165 | }, |
| 166 | }, |
| 167 | RPCExamples{ |
| 168 | HelpExampleCli("getpeginaddress", "") |
| 169 | + HelpExampleRpc("getpeginaddress", "") |
| 170 | }, |
| 171 | [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue |
| 172 | { |
| 173 | std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); |
| 174 | if (!wallet) return NullUniValue; |
| 175 | CWallet* const pwallet = wallet.get(); |
| 176 | |
| 177 | if (pwallet->chain().isInitialBlockDownload()) { |
| 178 | throw JSONRPCError(RPC_WALLET_ERROR, "This action cannot be completed during initial sync or reindexing."); |
| 179 | } |
| 180 | |
| 181 | LegacyScriptPubKeyMan* spk_man = pwallet->GetLegacyScriptPubKeyMan(); |
| 182 | if (!spk_man) { |
| 183 | throw JSONRPCError(RPC_WALLET_ERROR, "This type of wallet does not support this command"); |
| 184 | } |
| 185 | |
| 186 | if (!pwallet->IsLocked()) { |
| 187 | pwallet->TopUpKeyPool(); |
| 188 | } |
| 189 | |
| 190 | // Use native witness destination |
| 191 | CTxDestination dest; |
| 192 | bilingual_str error; |
| 193 | if (!pwallet->GetNewDestination(OutputType::BECH32, "", dest, error)) { |
| 194 | throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error.original); |
| 195 | } |
| 196 | |
| 197 | CScript dest_script = GetScriptForDestination(dest); |
| 198 | |
| 199 | // Also add raw scripts to index to recognize later. |
| 200 | spk_man->AddCScript(dest_script); |
| 201 | |
| 202 | // Get P2CH deposit address on mainchain from most recent fedpegscript. |
| 203 | const auto& fedpegscripts = GetValidFedpegScripts(pwallet->chain().getTip(), Params().GetConsensus(), true /* nextblock_validation */); |
| 204 | if (fedpegscripts.empty()) { |
| 205 | std::string message = "No valid fedpegscripts."; |
| 206 | if (!g_con_elementsmode) { |
| 207 | message += " Not running in Elements mode, check your 'chain' param."; |
| 208 | } |
| 209 | throw JSONRPCError(RPC_INTERNAL_ERROR, message); |
| 210 | } |
| 211 | CTxDestination mainchain_dest(WitnessV0ScriptHash(calculate_contract(fedpegscripts.front().second, dest_script))); |
nothing calls this directly
no test coverage detected