Changeset View
Changeset View
Standalone View
Standalone View
src/rpc/avalanche.cpp
Show First 20 Lines • Show All 293 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
static UniValue delegateavalancheproof(const Config &config, | static UniValue delegateavalancheproof(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"delegateavalancheproof", | "delegateavalancheproof", | ||||
"Delegate the avalanche proof to another public key.\n", | "Delegate the avalanche proof to another public key.\n", | ||||
{ | { | ||||
{"proof", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | {"limitedproofid", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | ||||
"The proof to be delegated."}, | "The limited id of the proof to be delegated."}, | ||||
{"privatekey", RPCArg::Type::STR, RPCArg::Optional::NO, | {"privatekey", RPCArg::Type::STR, RPCArg::Optional::NO, | ||||
"The private key in base58-encoding. Must match the proof master " | "The private key in base58-encoding. Must match the proof master " | ||||
"public key or the upper level parent delegation public key if " | "public key or the upper level parent delegation public key if " | ||||
" supplied."}, | " supplied."}, | ||||
{"publickey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | {"publickey", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, | ||||
"The public key to delegate the proof to."}, | "The public key to delegate the proof to."}, | ||||
{"delegation", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, | {"delegation", RPCArg::Type::STR_HEX, RPCArg::Optional::OMITTED, | ||||
"A string that is the serialized, hex-encoded delegation for the " | "A string that is the serialized, hex-encoded delegation for the " | ||||
"proof and which is a parent for the delegation to build."}, | "proof and which is a parent for the delegation to build."}, | ||||
}, | }, | ||||
RPCResult{RPCResult::Type::STR_HEX, "delegation", | RPCResult{RPCResult::Type::STR_HEX, "delegation", | ||||
"A string that is a serialized, hex-encoded delegation."}, | "A string that is a serialized, hex-encoded delegation."}, | ||||
RPCExamples{HelpExampleRpc("delegateavalancheproof", | RPCExamples{ | ||||
"\"<proof>\" \"<privkey>\" \"<pubkey>\"")}, | HelpExampleRpc("delegateavalancheproof", | ||||
"\"<limitedproofid>\" \"<privkey>\" \"<pubkey>\"")}, | |||||
} | } | ||||
.Check(request); | .Check(request); | ||||
RPCTypeCheck(request.params, | RPCTypeCheck(request.params, | ||||
{UniValue::VSTR, UniValue::VSTR, UniValue::VSTR}); | {UniValue::VSTR, UniValue::VSTR, UniValue::VSTR}); | ||||
if (!g_avalanche) { | if (!g_avalanche) { | ||||
throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized"); | throw JSONRPCError(RPC_INTERNAL_ERROR, "Avalanche is not initialized"); | ||||
} | } | ||||
avalanche::Proof proof; | avalanche::LimitedProofId limitedProofId{ | ||||
bilingual_str error; | ParseHashV(request.params[0], "limitedproofid")}; | ||||
if (!avalanche::Proof::FromHex(proof, request.params[0].get_str(), error)) { | |||||
throw JSONRPCError(RPC_INVALID_PARAMETER, error.original); | |||||
} | |||||
avalanche::ProofValidationState proofState; | |||||
if (!proof.verify(proofState)) { | |||||
throw JSONRPCError(RPC_INVALID_PARAMETER, "The proof is invalid"); | |||||
} | |||||
const CKey privkey = DecodeSecret(request.params[1].get_str()); | const CKey privkey = DecodeSecret(request.params[1].get_str()); | ||||
if (!privkey.IsValid()) { | if (!privkey.IsValid()) { | ||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, | ||||
"The private key is invalid"); | "The private key is invalid"); | ||||
} | } | ||||
const CPubKey pubkey = ParsePubKey(request.params[2]); | const CPubKey pubkey = ParsePubKey(request.params[2]); | ||||
avalanche::DelegationBuilder dgb(proof); | |||||
CPubKey auth; | CPubKey auth; | ||||
std::unique_ptr<avalanche::DelegationBuilder> dgb; | |||||
deadalnix: I don't think this needs to be a unique pointer. | |||||
if (request.params.size() >= 4 && !request.params[3].isNull()) { | if (request.params.size() >= 4 && !request.params[3].isNull()) { | ||||
avalanche::Delegation dg; | avalanche::Delegation dg; | ||||
CDataStream ss(ParseHexV(request.params[3], "delegation"), SER_NETWORK, | CDataStream ss(ParseHexV(request.params[3], "delegation"), SER_NETWORK, | ||||
PROTOCOL_VERSION); | PROTOCOL_VERSION); | ||||
ss >> dg; | ss >> dg; | ||||
if (dg.getProofId() != proof.getId()) { | if (dg.getProofId() != | ||||
limitedProofId.computeProofId(dg.getProofMaster())) { | |||||
throw JSONRPCError( | throw JSONRPCError( | ||||
RPC_INVALID_PARAMETER, | RPC_INVALID_PARAMETER, | ||||
"The supplied delegation does not match the proof"); | "The supplied delegation does not match the proof"); | ||||
} | } | ||||
avalanche::DelegationState dgState; | avalanche::DelegationState dgState; | ||||
if (!dg.verify(dgState, auth)) { | if (!dg.verify(dgState, auth)) { | ||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError(RPC_INVALID_PARAMETER, | ||||
"The supplied delegation is not valid"); | "The supplied delegation is not valid"); | ||||
} | } | ||||
if (!dgb.importDelegation(dg)) { | if (privkey.GetPubKey() != auth) { | ||||
throw JSONRPCError( | |||||
RPC_INVALID_PARAMETER, | |||||
"The supplied private key does not match the delegation"); | |||||
} | |||||
dgb.reset(new avalanche::DelegationBuilder{limitedProofId, | |||||
dg.getProofMaster()}); | |||||
if (!dgb->importDelegation(dg)) { | |||||
throw JSONRPCError(RPC_INVALID_PARAMETER, | throw JSONRPCError(RPC_INVALID_PARAMETER, | ||||
"Failed to import the delegation"); | "Failed to import the delegation"); | ||||
} | } | ||||
} else { | } else { | ||||
auth = proof.getMaster(); | auth = privkey.GetPubKey(); | ||||
} | dgb.reset(new avalanche::DelegationBuilder{limitedProofId, auth}); | ||||
if (privkey.GetPubKey() != auth) { | |||||
throw JSONRPCError( | |||||
RPC_INVALID_PARAMETER, | |||||
"The private key does not match the proof or the delegation"); | |||||
} | } | ||||
if (!dgb.addLevel(privkey, pubkey)) { | if (!dgb->addLevel(privkey, pubkey)) { | ||||
throw JSONRPCError(RPC_MISC_ERROR, "Unable to build the delegation"); | throw JSONRPCError(RPC_MISC_ERROR, "Unable to build the delegation"); | ||||
} | } | ||||
CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | CDataStream ss(SER_NETWORK, PROTOCOL_VERSION); | ||||
ss << dgb.build(); | ss << dgb->build(); | ||||
return HexStr(ss); | return HexStr(ss); | ||||
} | } | ||||
static UniValue getavalanchepeerinfo(const Config &config, | static UniValue getavalanchepeerinfo(const Config &config, | ||||
const JSONRPCRequest &request) { | const JSONRPCRequest &request) { | ||||
RPCHelpMan{ | RPCHelpMan{ | ||||
"getavalanchepeerinfo", | "getavalanchepeerinfo", | ||||
"Returns data about each connected avalanche peer as a json array of " | "Returns data about each connected avalanche peer as a json array of " | ||||
▲ Show 20 Lines • Show All 114 Lines • Show Last 20 Lines |
I don't think this needs to be a unique pointer.