8000 Add namepsbt RPC method by domob1812 · Pull Request #362 · namecoin/namecoin-core · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add namepsbt RPC method #362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions doc/release-notes-namecoin.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
This has uses for light clients, as discussed in
[#329](https://github.com/namecoin/namecoin-core/issues/329).

- The new RPC method `namepsbt` can be used to generate name operations
based on PSBTs. It works in the same way as the existing
`namerawtransaction`.

## Version 0.19

- The mempool now allows multiple updates of a single name (in a chain of
Expand Down
180 changes: 126 additions & 54 deletions src/rpc/names.cpp
8000
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <names/common.h>
#include <names/main.h>
#include <primitives/transaction.h>
#include <psbt.h>
#include <rpc/blockchain.h>
#include <rpc/names.h>
#include <rpc/server.h>
Expand Down Expand Up @@ -799,51 +800,27 @@ name_pending (const JSONRPCRequest& request)

/* ************************************************************************** */

UniValue
namerawtransaction (const JSONRPCRequest& request)
namespace
{
RPCHelpMan ("namerawtransaction",
"\nAdds a name operation to an existing raw transaction.\n"
"\nUse createrawtransaction first to create the basic transaction, including the required inputs and outputs also for the name.\n",
{
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction hex string"},
{"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The vout of the desired name output"},
{"nameop", RPCArg::Type::OBJ, RPCArg::Optional::NO, "The name operation to create",
{
{"op", RPCArg::Type::STR, RPCArg::Optional::NO, "The operat 8000 ion to perform, can be \"name_new\", \"name_firstupdate\" and \"name_update\""},
{"name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name to operate on"},
{"value", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The new value for the name"},
{"rand", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The nonce value to use for registrations"},
},
"nameop"},
},
RPCResult {RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "hex", "Hex string of the updated transaction"},
{RPCResult::Type::STR_HEX, "rand", /* optional */ true, "If this is a name_new, the nonce used to create it"},
},
},
RPCExamples {
HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_new\",\"name\":\"my-name\")")
+ HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_firstupdate\",\"name\":\"my-name\",\"value\":\"new value\",\"rand\":\"00112233\")")
+ HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
+ HelpExampleRpc ("namerawtransaction", R"("raw tx hex", 1, "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
}
).Check (request);

RPCTypeCheck (request.params,
{UniValue::VSTR, UniValue::VNUM, UniValue::VOBJ});

CMutableTransaction mtx;
if (!DecodeHexTx (mtx, request.params[0].get_str (), true))
throw JSONRPCError (RPC_DESERIALIZATION_ERROR, "TX decode failed");
/**
* Performs the action of namerawtransaction and namepsbt on a given
* CMutableTransaction. This is used to share the code between the two
* RPC methods.
*
* If a name_new is created and a rand value chosen, it will be placed
* into the JSON output "result" already.
*/
void
PerformNameRawtx (const int nOut, const UniValue& nameOp,
CMutableTransaction& mtx, UniValue& result)
{
mtx.SetNamecoin ();

const size_t nOut = request.params[1].get_int ();
if (nOut >= mtx.vout.size ())
if (nOut < 0 || nOut >= mtx.vout.size ())
throw JSONRPCError (RPC_INVALID_PARAMETER, "vout is out of range");
auto& script = mtx.vout[nOut].scriptPubKey;

const UniValue nameOp = request.params[2].get_obj ();
RPCTypeCheckObj (nameOp,
{
{"op", UniValueType (UniValue::VSTR)},
Expand All @@ -857,8 +834,6 @@ namerawtransaction (const JSONRPCRequest& request)
operations with arbitrary hex data. */
const UniValue NO_OPTIONS(UniValue::VOBJ);

UniValue result(UniValue::VOBJ);

if (op == "name_new")
{
RPCTypeCheckObj (nameOp,
Expand All @@ -885,8 +860,7 @@ namerawtransaction (const JSONRPCRequest& request)
const valtype name
= DecodeNameFromRPCOrThrow (find_value (nameOp, "name"), NO_OPTIONS);

mtx.vout[nOut].scriptPubKey
= CNameScript::buildNameNew (mtx.vout[nOut].scriptPubKey, name, rand);
script = CNameScript::buildNameNew (script, name, rand);
result.pushKV ("rand", HexStr (rand));
}
else if (op == "name_firstupdate")
Expand All @@ -910,9 +884,7 @@ namerawtransaction (const JSONRPCRequest& request)
= DecodeValueFromRPCOrThrow (find_value (nameOp, "value"),
NO_OPTIONS);

mtx.vout[nOut].scriptPubKey
= CNameScript::buildNameFirstupdate (mtx.vout[nOut].scriptPubKey,
name, value, rand);
script = CNameScript::buildNameFirstupdate (script, name, value, rand);
}
else if (op == "name_update")
{
Expand All @@ -929,17 +901,116 @@ namerawtransaction (const JSONRPCRequest& request)
= DecodeValueFromRPCOrThrow (find_value (nameOp, "value"),
NO_OPTIONS);

mtx.vout[nOut].scriptPubKey
= CNameScript::buildNameUpdate (mtx.vout[nOut].scriptPubKey,
name, value);
script = CNameScript::buildNameUpdate (script, name, value);
}
else
throw JSONRPCError (RPC_INVALID_PARAMETER, "Invalid name operation");
}

} // anonymous namespace

UniValue
namerawtransaction (const JSONRPCRequest& request)
{
RPCHelpMan ("namerawtransaction",
"\nAdds a name operation to an existing raw transaction.\n"
"\nUse createrawtransaction first to create the basic transaction, including the required inputs and outputs also for the name.\n",
{
{"hexstring", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The transaction hex string"},
{"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The vout of the desired name output"},
{"nameop", RPCArg::Type::OBJ, RPCArg::Optional::NO, "The name operation to create",
{
{"op", RPCArg::Type::STR, RPCArg::Optional::NO, "The operation to perform, can be \"name_new\", \"name_firstupdate\" and \"name_update\""},
{"name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name to operate on"},
{"value", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The new value for the name"},
{"rand", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The nonce value to use for registrations"},
},
"nameop"},
},
RPCResult {RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "hex", "Hex string of the updated transaction"},
{RPCResult::Type::STR_HEX, "rand", /* optional */ true, "If this is a name_new, the nonce used to create it"},
},
},
RPCExamples {
HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_new\",\"name\":\"my-name\")")
+ HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_firstupdate\",\"name\":\"my-name\",\"value\":\"new value\",\"rand\":\"00112233\")")
+ HelpExampleCli ("namerawtransaction", R"("raw tx hex" 1 "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
+ HelpExampleRpc ("namerawtransaction", R"("raw tx hex", 1, "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
}
).Check (request);

RPCTypeCheck (request.params,
{UniValue::VSTR, UniValue::VNUM, UniValue::VOBJ});

CMutableTransaction mtx;
if (!DecodeHexTx (mtx, request.params[0].get_str (), true))
throw JSONRPCError (RPC_DESERIALIZATION_ERROR, "TX decode failed");

UniValue result(UniValue::VOBJ);

PerformNameRawtx (request.params[1].get_int (), request.params[2].get_obj (),
mtx, result);

result.pushKV ("hex", EncodeHexTx (CTransaction (mtx)));
return result;
}

UniValue
namepsbt (const JSONRPCRequest& request)
{
RPCHelpMan ("namepsbt",
"\nAdds a name operation to an existing PSBT.\n"
"\nUse createpsbt first to create the basic transaction, including the required inputs and outputs also for the name.\n",
{
{"psbt", RPCArg::Type::STR, RPCArg::Optional::NO, "A base64 string of a PSBT"},
{"vout", RPCArg::Type::NUM, RPCArg::Optional::NO, "The vout of the desired name output"},
{"nameop", RPCArg::Type::OBJ, RPCArg::Optional::NO, "The name operation to create",
{
{"op", RPCArg::Type::STR, RPCArg::Optional::NO, "The operation to perform, can be \"name_new\", \"name_firstupdate\" and \"name_update\""},
{"name", RPCArg::Type::STR, RPCArg::Optional::NO, "The name to operate on"},
{"value", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The new value for the name"},
{"rand", RPCArg::Type::STR, RPCArg::Optional::OMITTED, "The nonce value to use for registrations"},
},
"nameop"},
},
RPCResult {RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "psbt", "The serialised, updated PSBT"},
{RPCResult::Type::STR_HEX, "rand", /* optional */ true, "If this is a name_new, the nonce used to create it"},
},
},
RPCExamples {
HelpExampleCli ("namepsbt", R"("psbt" 1 "{\"op\":\"name_new\",\"name\":\"my-name\")")
+ HelpExampleCli ("namepsbt", R"("psbt" 1 "{\"op\":\"name_firstupdate\",\"name\":\"my-name\",\"value\":\"new value\",\"rand\":\"00112233\")")
+ HelpExampleCli ("namepsbt", R"("psbt" 1 "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
+ HelpExampleRpc ("namepsbt", R"("psbt", 1, "{\"op\":\"name_update\",\"name\":\"my-name\",\"value\":\"new value\")")
}
).Check (request);

RPCTypeCheck (request.params,
{UniValue::VSTR, UniValue::VNUM, UniValue::VOBJ});

PartiallySignedTransaction psbtx;
std::string error;
if (!DecodeBase64PSBT (psbtx, request.params[0].get_str (), error))
throw JSONRPCError (RPC_DESERIALIZATION_ERROR,
strprintf ("TX decode failed %s", error));

UniValue result(UniValue::VOBJ);

PerformNameRawtx (request.params[1].get_int (), request.params[2].get_obj (),
*psbtx.tx, result);

CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << psbtx;
const auto* data = reinterpret_cast<const unsigned char*> (ssTx.data ());
result.pushKV ("psbt", EncodeBase64 (data, ssTx.size ()));

return result;
}

/* ************************************************************************** */

UniValue
Expand Down Expand Up @@ -971,12 +1042,13 @@ void RegisterNameRPCCommands(CRPCTable &t)
static const CRPCCommand commands[] =
{ // category name actor (function) argNames
// --------------------- ------------------------ ----------------------- ----------
{ "names", "name_show", &name_show, {"name","options"} },
{ "names", "name_history", &name_history, {"name","options"} },
{ "names", "name_scan", &name_scan, {"start","count","options"} },
{ "names", "name_pending", &name_pending, {"name","options"} },
{ "names", "name_show", &name_show, {"name", "options"} },
{ "names", "name_history", &name_history, {"name", "options"} },
{ "names", "name_scan", &name_scan, {"start", "count", "options"} },
{ "names", "name_pending", &name_pending, {"name", "options"} },
{ "names", "name_checkdb", &name_checkdb, {} },
{ "rawtransactions", "namerawtransaction", &namerawtransaction, {"hexstring","vout","nameop"} },
{ "rawtransactions", "namerawtransaction", &namerawtransaction, {"hexstring", "vout", "nameop"} },
{ "rawtransactions", "namepsbt", &namepsbt, {"psbt", "vout", "nameop"} },
};

for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++)
Expand Down
4 changes: 2 additions & 2 deletions test/functional/name_ant_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ def run_test (self):
for vout in data["vout"]:
fullOuts.append ({vout["scriptPubKey"]["addresses"][0]: vout["value"]})
combined = self.nodes[1].createrawtransaction (fullIns, fullOuts)
combined = self.nodes[1].converttopsbt (combined)

nameOp = {
"op": "name_update",
"name": "x/name",
"value": "updated",
}
combined = self.nodes[1].namerawtransaction (combined, 0, nameOp)["hex"]
combined = self.nodes[1].converttopsbt (combined)
combined = self.nodes[1].namepsbt (combined, 0, nameOp)["psbt"]

# Sign and broadcast the partial tx.
sign1 = self.nodes[0].walletprocesspsbt (combined)
Expand Down
Loading
0