MCPcopy Create free account
hub / github.com/ElementsProject/elements / importprunedfunds

Function importprunedfunds

src/wallet/rpc/backup.cpp:320–374  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

318}
319
320RPCHelpMan importprunedfunds()
321{
322 return RPCHelpMan{"importprunedfunds",
323 "\nImports funds without rescan. Corresponding address or script must previously be included in wallet. Aimed towards pruned wallets. The end-user is responsible to import additional transactions that subsequently spend the imported outputs or rescan after the point in the blockchain the transaction is included.\n",
324 {
325 {"rawtransaction", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "A raw transaction in hex funding an already-existing address in wallet"},
326 {"txoutproof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The hex output from gettxoutproof that contains the transaction"},
327 },
328 RPCResult{RPCResult::Type::NONE, "", ""},
329 RPCExamples{""},
330 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
331{
332 std::shared_ptr<CWallet> const pwallet = GetWalletForJSONRPCRequest(request);
333 if (!pwallet) return NullUniValue;
334
335 CMutableTransaction tx;
336 if (!DecodeHexTx(tx, request.params[0].get_str())) {
337 throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed. Make sure the tx has at least one input.");
338 }
339 uint256 hashTx = tx.GetHash();
340
341 CDataStream ssMB(ParseHexV(request.params[1], "proof"), SER_NETWORK, PROTOCOL_VERSION);
342 CMerkleBlock merkleBlock;
343 ssMB >> merkleBlock;
344
345 //Search partial merkle tree in proof for our transaction and index in valid block
346 std::vector<uint256> vMatch;
347 std::vector<unsigned int> vIndex;
348 if (merkleBlock.txn.ExtractMatches(vMatch, vIndex) != merkleBlock.header.hashMerkleRoot) {
349 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Something wrong with merkleblock");
350 }
351
352 LOCK(pwallet->cs_wallet);
353 int height;
354 if (!pwallet->chain().findAncestorByHash(pwallet->GetLastBlockHash(), merkleBlock.header.GetHash(), FoundBlock().height(height))) {
355 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found in chain");
356 }
357
358 std::vector<uint256>::const_iterator it;
359 if ((it = std::find(vMatch.begin(), vMatch.end(), hashTx)) == vMatch.end()) {
360 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Transaction given doesn't exist in proof");
361 }
362
363 unsigned int txnIndex = vIndex[it - vMatch.begin()];
364
365 CTransactionRef tx_ref = MakeTransactionRef(tx);
366 if (pwallet->IsMine(*tx_ref)) {
367 pwallet->AddToWallet(std::move(tx_ref), TxStateConfirmed{merkleBlock.header.GetHash(), height, static_cast<int>(txnIndex)});
368 return NullUniValue;
369 }
370
371 throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "No addresses in wallet correspond to included transaction");
372},
373 };
374}
375
376RPCHelpMan removeprunedfunds()
377{

Callers

nothing calls this directly

Calls 14

DecodeHexTxFunction · 0.85
JSONRPCErrorFunction · 0.85
ParseHexVFunction · 0.85
FoundBlockClass · 0.85
findAncestorByHashMethod · 0.80
AddToWalletMethod · 0.80
MakeTransactionRefFunction · 0.50
GetHashMethod · 0.45
ExtractMatchesMethod · 0.45
heightMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected