Skip to content

Commit

Permalink
Merge pull request qtumproject#218 from qtumproject/earlz/rpc-hexaddress
Browse files Browse the repository at this point in the history
Add RPC commands gethexaddress and fromhexaddress
  • Loading branch information
qtum-neil authored Jun 29, 2017
2 parents a633d64 + 2a061fc commit ed22562
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions doc/sparknet-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,9 @@ Qtum supports all of the RPC commands supported by Bitcoin Core, but also includ
* `listcontracts` - This will output a list of currently deployed contract addresses with their respective balance. This RPC call may change or be removed in the future.
* `reservebalance` - This will reserve a set amount of coins so that they do not participate in staking. If you reserve as many or more coins than are in your wallet, then you will not participate at all in staking and block creation for the network.
* `getstakinginfo` - This will show some info about the current node's staking status, including network difficulty and expected time (in seconds) until staking a new block.
* `gethexaddress` - This will convert a standard Base58 pubkeyhash address to a hex address for use in smart contracts
* `fromhexaddress` - this will convert a hex address used in smart contracts to a standard Base58 pubkeyhash address


# New Qtum Command Line Arguments

Expand Down
5 changes: 5 additions & 0 deletions src/base58.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ bool CBitcoinAddress::IsScript() const
return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::SCRIPT_ADDRESS);
}

bool CBitcoinAddress::IsPubKeyHash() const
{
return IsValid() && vchVersion == Params().Base58Prefix(CChainParams::PUBKEY_ADDRESS);
}

void CBitcoinSecret::SetKey(const CKey& vchSecret)
{
assert(vchSecret.IsValid());
Expand Down
1 change: 1 addition & 0 deletions src/base58.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class CBitcoinAddress : public CBase58Data {
CTxDestination Get() const;
bool GetKeyID(CKeyID &keyID) const;
bool IsScript() const;
bool IsPubKeyHash() const;
};

/**
Expand Down
57 changes: 57 additions & 0 deletions src/rpc/rawtransaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,61 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, UniValue& entry)
}
}


UniValue gethexaddress(const JSONRPCRequest& request) {
if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
throw runtime_error(
"gethexaddress \"address\"\n"

"\nConverts a base58 pubkeyhash address to a hex address for use in smart contracts.\n"

"\nArguments:\n"
"1. \"address\" (string, required) The base58 address\n"

"\nResult:\n"
"\"hexaddress\" (string) The raw hex pubkeyhash address for use in smart contracts\n"

"\nExamples:\n"
+ HelpExampleCli("gethexaddress", "\"address\"")
+ HelpExampleRpc("gethexaddress", "\"address\"")
);

CBitcoinAddress address(request.params[0].get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Qtum address");

if(!address.IsPubKeyHash())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Only pubkeyhash addresses are supported");

return boost::get<CKeyID>(address.Get()).GetHex();
}

UniValue fromhexaddress(const JSONRPCRequest& request) {
if (request.fHelp || request.params.size() < 1 || request.params.size() > 1)
throw runtime_error(
"fromhexaddress \"hexaddress\"\n"

"\nConverts a raw hex address to a base58 pubkeyhash address\n"

"\nArguments:\n"
"1. \"hexaddress\" (string, required) The raw hex address\n"

"\nResult:\n"
"\"address\" (string) The base58 pubkeyhash address\n"

"\nExamples:\n"
+ HelpExampleCli("fromhexaddress", "\"hexaddress\"")
+ HelpExampleRpc("fromhexaddress", "\"hexaddress\"")
);
if (request.params[0].get_str().size() != 40)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid pubkeyhash hex size (should be 40 hex characters)");
CKeyID raw;
raw.SetHex(request.params[0].get_str());
CBitcoinAddress address(raw);

return address.ToString();
}

UniValue getrawtransaction(const JSONRPCRequest& request)
{
if (request.fHelp || request.params.size() < 1 || request.params.size() > 2)
Expand Down Expand Up @@ -937,6 +992,8 @@ static const CRPCCommand commands[] =
{ "rawtransactions", "decodescript", &decodescript, true, {"hexstring"} },
{ "rawtransactions", "sendrawtransaction", &sendrawtransaction, false, {"hexstring","allowhighfees"} },
{ "rawtransactions", "signrawtransaction", &signrawtransaction, false, {"hexstring","prevtxs","privkeys","sighashtype"} }, /* uses wallet if enabled */
{ "rawtransactions", "gethexaddress", &gethexaddress, true, {"address",} },
{ "rawtransactions", "fromhexaddress", &fromhexaddress, true, {"hexaddress",} },

{ "blockchain", "gettxoutproof", &gettxoutproof, true, {"txids", "blockhash"} },
{ "blockchain", "verifytxoutproof", &verifytxoutproof, true, {"proof"} },
Expand Down

0 comments on commit ed22562

Please sign in to comment.