Skip to content

Commit

Permalink
tweakfedpegscript: Optional fedpeg arg
Browse files Browse the repository at this point in the history
  • Loading branch information
instagibbs committed Oct 8, 2019
1 parent ee14d88 commit 97e46f9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/rpc/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,13 @@ static UniValue echo(const JSONRPCRequest& request)

UniValue tweakfedpegscript(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() != 1)
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
throw std::runtime_error(
RPCHelpMan{"tweakfedpegscript",
"\nReturns a tweaked fedpegscript.\n",
{
{"claim_script", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "Script to tweak the fedpegscript with. For example obtained as a result of getpeginaddress."},
{"fedpegscript", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED_NAMED_ARG, "Fedpegscript to be used with the claim_script. By default this is the current fedpegscript."},
},
RPCResult{
"{\n"
Expand All @@ -651,11 +652,21 @@ UniValue tweakfedpegscript(const JSONRPCRequest& request)
throw JSONRPCError(RPC_TYPE_ERROR, "the first argument must be a hex string");
}

CScript fedpegscript = GetValidFedpegScripts(chainActive.Tip(), Params().GetConsensus(), true /* nextblock_validation */).front().second;

if (!request.params[1].isNull()) {
if (IsHex(request.params[1].get_str())) {
std::vector<unsigned char> fedpeg_byte = ParseHex(request.params[1].get_str());
fedpegscript = CScript(fedpeg_byte.begin(), fedpeg_byte.end());
} else {
throw JSONRPCError(RPC_TYPE_ERROR, "fedpegscript must be a hex string");
}
}

std::vector<unsigned char> scriptData = ParseHex(request.params[0].get_str());
CScript claim_script = CScript(scriptData.begin(), scriptData.end());

const auto& fedpegscripts = GetValidFedpegScripts(chainActive.Tip(), Params().GetConsensus(), true /* nextblock_validation */);
CScript tweaked_script = calculate_contract(fedpegscripts.front().second, claim_script);
CScript tweaked_script = calculate_contract(fedpegscript, claim_script);
CTxDestination parent_addr(ScriptHash(GetScriptForWitness(tweaked_script)));

UniValue ret(UniValue::VOBJ);
Expand Down Expand Up @@ -846,7 +857,7 @@ static const CRPCCommand commands[] =
{ "util", "signmessagewithprivkey", &signmessagewithprivkey, {"privkey","message"} },
// ELEMENTS:
{ "util", "getpakinfo", &getpakinfo, {}},
{ "util", "tweakfedpegscript", &tweakfedpegscript, {"claim_script"} },
{ "util", "tweakfedpegscript", &tweakfedpegscript, {"claim_script", "fedpegscript"} },
{ "util", "createblindedaddress", &createblindedaddress, {"address", "blinding_key"}},
{ "util", "dumpassetlabels", &dumpassetlabels, {}},
{ "hidden", "calcfastmerkleroot", &calcfastmerkleroot, {"leaves"} },
Expand Down
6 changes: 6 additions & 0 deletions test/functional/rpc_tweakfedpeg.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,11 @@ def run_test(self):
dyna_fed_tweaked = "75745c87635b2103258857ed88024e5296436f851337451d3d68522fbb21dc5a047b965cd2f790932103ab43d77e8706a799cee3c14678574bccaca98c101217369cee987073cd2546492102ee5c8dfdf742bcc2b08386a6a3ec4b6087d39a6fd7e6f0fdeb82ecc30b11d0342102b104b6a469d8b3ccf0a671198e5f7d7010dbf2eb5587733b168f6e0d3be75760210207fc37d4877529fb0d63b1de352689627400a43f18717fa182fe0eea283bdd7921025d8724d82c61708458bc1a08ec9a7fcaf0ab6747ccf0a7d378faea07a261ab3e2102af6859d48d0a4518a4811f12adb974bbd051931e3f96f0b6ae16142c43ae6fd12102458215967f7977effc21b964bd92d870ae7eca98f343801031639150139e63ed2102cdef66bf4b5d26d0be22cabd5761970cfa84be905dc0137ad08ab634f104cb9e210297fb764f808c126f46ce0444124213bdc6a18f2904e75c9144f53bed4c19376e2103c631e77d14a5eb2fc61496ee3e9ea19d1e56e7e65a07aaa786f25ffc2921efde210270addb9011a4b41987b0848a8dc52e5442964fe5188c6a4c9320fbb9016390772103bf9aa75444d0013c46dcf10d70831b75b46a25d901fb7c6360beb2f6cac9c503210278f303dbaad1410a26d99e4dca8925fae4c90532a156c29e8ab1abf5ccaa663d210394cc0983add2dc60aa988c9faedebdc5463106184d72dd430ef24d215daf8b935f6702c00fb275522102892d66841804e5ad236f9e012bd8f1082857c9355f83aa4cd64dbe19e487d50821032d090e7b1d5047a3ac626de4b2a18e210501734895c5b7717412dac0234dd97b21026a693f0278011a09d56c04cf6b62ce8253c92a5f03a5010e6633b497ce3989085368ae"
assert_equal(self.nodes[2].tweakfedpegscript(claim_script)["script"], dyna_fed_tweaked)

# Optional fedpegscript arg (same arg as active script!)
assert_equal(self.nodes[2].tweakfedpegscript(claim_script, DYNAFED_SCRIPT)["script"], dyna_fed_tweaked)

# Older script should result in same as before
assert_equal(self.nodes[2].tweakfedpegscript(claim_script, LIQUID_SCRIPT)["script"], liquid_tweaked)

if __name__ == '__main__':
TweakFedpegTest().main()

0 comments on commit 97e46f9

Please sign in to comment.