Skip to content

Commit

Permalink
refactor getpeginaddress to not call getnewaddress RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed May 1, 2019
1 parent cdb33e5 commit a9cbe55
Showing 1 changed file with 19 additions and 18 deletions.
37 changes: 19 additions & 18 deletions src/wallet/rpcwallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4503,33 +4503,34 @@ UniValue getpeginaddress(const JSONRPCRequest& request)
+ HelpExampleRpc("getpeginaddress", "")
);

//Creates new address for receiving unlocked utxos
JSONRPCRequest req;
CTxDestination address = DecodeDestination(getnewaddress(req).get_str());
if (!pwallet->IsLocked()) {
pwallet->TopUpKeyPool();
}

Witnessifier w(pwallet);
bool ret = boost::apply_visitor(w, address);
if (!ret) {
throw JSONRPCError(RPC_WALLET_ERROR, "Public key or redeemscript not known to wallet, or the key is uncompressed");
// Generate a new key that is added to wallet
CPubKey newKey;
if (!pwallet->GetKeyFromPool(newKey)) {
throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, "Error: Keypool ran out, please call keypoolrefill first");
}

pwallet->SetAddressBook(w.result, "", "receive");
// Use native witness destination
CTxDestination dest = GetDestinationForKey(newKey, OutputType::BECH32);

CScript destScript = GetScriptForDestination(address);
CScript witProg = GetScriptForWitness(destScript);
pwallet->SetAddressBook(dest, "", "receive");

CScript dest_script = GetScriptForDestination(dest);

// Also add raw scripts to index to recognize later.
pwallet->AddCScript(witProg);
pwallet->AddCScript(destScript);
pwallet->AddCScript(dest_script);

//Call contracthashtool, get deposit address on mainchain.
CTxDestination destAddr(ScriptHash(GetScriptForWitness(calculate_contract(Params().GetConsensus().fedpegScript, witProg))));
// Get P2CH deposit address on mainchain.
CTxDestination mainchain_dest(ScriptHash(GetScriptForWitness(calculate_contract(Params().GetConsensus().fedpegScript, dest_script))));

UniValue fundinginfo(UniValue::VOBJ);
UniValue ret(UniValue::VOBJ);

fundinginfo.pushKV("mainchain_address", EncodeParentDestination(destAddr));
fundinginfo.pushKV("claim_script", HexStr(witProg));
return fundinginfo;
ret.pushKV("mainchain_address", EncodeParentDestination(mainchain_dest));
ret.pushKV("claim_script", HexStr(dest_script));
return ret;
}

//! Derive BIP32 tweak from master xpub to child pubkey.
Expand Down

0 comments on commit a9cbe55

Please sign in to comment.