From 8292712a8692f3e638b12dd38bf811561beabf8d Mon Sep 17 00:00:00 2001 From: Jianping Wu Date: Tue, 6 Nov 2018 15:46:56 -0800 Subject: [PATCH] Base58 encoding for namespace (prefix 2, to be fixed to N) Segwit P2SH-P2WPKH. redeemScript = 0x0014{20-byte keyhash} scriptPubKey = OP_HASH160 hash160(redeemScript) OP_EQUAL --- src/script/standard.cpp | 2 +- src/wallet/rpckeva.cpp | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/src/script/standard.cpp b/src/script/standard.cpp index de0f022af..5ec7f88b5 100644 --- a/src/script/standard.cpp +++ b/src/script/standard.cpp @@ -70,7 +70,7 @@ bool Solver(const CScript& scriptPubKey, txnouttype& typeRet, std::vector witnessprogram; - if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) { + if (script1.IsWitnessProgram(witnessversion, witnessprogram)) { if (witnessversion == 0 && witnessprogram.size() == 20) { typeRet = TX_WITNESS_V0_KEYHASH; vSolutionsRet.push_back(witnessprogram); diff --git a/src/wallet/rpckeva.cpp b/src/wallet/rpckeva.cpp index 74d65d5ee..07805f853 100644 --- a/src/wallet/rpckeva.cpp +++ b/src/wallet/rpckeva.cpp @@ -21,7 +21,7 @@ #include -const unsigned char NAMESPACE_PREFIX = 0x35; // N +const unsigned char NAMESPACE_PREFIX = 21; // 2 in base58 /* ************************************************************************** */ @@ -72,13 +72,17 @@ UniValue keva_namespace(const JSONRPCRequest& request) CKeyID keyId = pubKey.GetID(); // The namespace name is: Hash160(Hash160(keyId) || displayName) + // JWU TODO: double check this! How can node verify this!!!??? valtype toHash = ToByteVector(Hash160(ToByteVector(keyId))); toHash.insert(toHash.end(), displayName.begin(), displayName.end()); valtype namespaceHashVal = ToByteVector(Hash160(toHash)); - namespaceHashVal.insert(namespaceHashVal.begin(), NAMESPACE_PREFIX); // Append N - const std::string namespaceHash = EncodeBase64(ValtypeToString(namespaceHashVal)); + namespaceHashVal.insert(namespaceHashVal.begin(), NAMESPACE_PREFIX); + const std::string namespaceHash = EncodeBase58(namespaceHashVal); - const CScript addrName = GetScriptForDestination(keyId); + //const CScript addrName = GetScriptForDestination(keyId); + CScript redeemScript = GetScriptForDestination(WitnessV0KeyHash(keyId)); + CScriptID scriptHash = CScriptID(redeemScript); + CScript addrName = GetScriptForDestination(scriptHash); const CScript newScript = CKevaScript::buildKevaNamespace(addrName, namespaceHashVal, displayName); CCoinControl coinControl; @@ -158,7 +162,7 @@ UniValue keva_list_namespaces(const JSONRPCRequest& request) } const valtype nameSpace = kevaOp.getOpNamespace(); - const std::string nameSpaceStr = EncodeBase64(ValtypeToString(nameSpace)); + const std::string nameSpaceStr = EncodeBase58(nameSpace); const CBlockIndex* pindex; const int depth = tx.GetDepthInMainChain(pindex); if (depth <= 0) { @@ -212,8 +216,11 @@ UniValue keva_put(const JSONRPCRequest& request) ObserveSafeMode (); - const std::string namespaceStr = request.params[0].get_str (); - const valtype nameSpace = ValtypeFromString(DecodeBase64(namespaceStr)); + const std::string namespaceStr = request.params[0].get_str(); + valtype nameSpace; + if (!DecodeBase58(namespaceStr, nameSpace)) { + throw JSONRPCError (RPC_INVALID_PARAMETER, "failed to decode namespace"); + } if (nameSpace.size () > MAX_NAMESPACE_LENGTH) throw JSONRPCError (RPC_INVALID_PARAMETER, "the namespace is too long"); @@ -307,7 +314,10 @@ UniValue keva_get(const JSONRPCRequest& request) ObserveSafeMode (); const std::string namespaceStr = request.params[0].get_str (); - const valtype nameSpace = ValtypeFromString(DecodeBase64(namespaceStr)); + valtype nameSpace; + if (!DecodeBase58(namespaceStr, nameSpace)) { + throw JSONRPCError (RPC_INVALID_PARAMETER, "failed to decode namespace"); + } if (nameSpace.size () > MAX_NAMESPACE_LENGTH) throw JSONRPCError (RPC_INVALID_PARAMETER, "the namespace is too long");