From d8e940f30135f771ea1a0683d5967711a69b28d0 Mon Sep 17 00:00:00 2001 From: David Burkett Date: Sat, 29 Jan 2022 14:28:57 -0500 Subject: [PATCH] MWEB: Add StealthAddress to CTxDestination and add pegin and hogex script consts & logic --- src/chainparams.cpp | 3 +++ src/chainparams.h | 2 ++ src/key_io.cpp | 23 ++++++++++++++++++++++ src/rpc/blockchain.cpp | 2 +- src/rpc/util.cpp | 14 ++++++++++++++ src/script/script.cpp | 35 ++++++++++++++++++++++++++++++++++ src/script/script.h | 16 ++++++++++++++++ src/script/standard.cpp | 35 ++++++++++++++++++++++++++++++++++ src/script/standard.h | 10 +++++++++- src/test/script_tests.cpp | 15 ++++++++++++++- src/wallet/rpcwallet.cpp | 8 ++++++++ src/wallet/scriptpubkeyman.cpp | 2 ++ 12 files changed, 162 insertions(+), 3 deletions(-) diff --git a/src/chainparams.cpp b/src/chainparams.cpp index e2f54cb5e..00575d2f8 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -132,6 +132,7 @@ public: base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4}; bech32_hrp = "ltc"; + mweb_hrp = "ltcmweb"; vFixedSeeds = std::vector(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main)); @@ -236,6 +237,7 @@ public: base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; bech32_hrp = "tltc"; + mweb_hrp = "tmweb"; vFixedSeeds = std::vector(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test)); @@ -341,6 +343,7 @@ public: base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; bech32_hrp = "rltc"; + mweb_hrp = "tmweb"; } /** diff --git a/src/chainparams.h b/src/chainparams.h index 6b45c8af7..a48fbd853 100644 --- a/src/chainparams.h +++ b/src/chainparams.h @@ -89,6 +89,7 @@ public: const std::vector& DNSSeeds() const { return vSeeds; } const std::vector& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } const std::string& Bech32HRP() const { return bech32_hrp; } + const std::string& MWEB_HRP() const { return mweb_hrp; } const std::vector& FixedSeeds() const { return vFixedSeeds; } const CCheckpointData& Checkpoints() const { return checkpointData; } const ChainTxData& TxData() const { return chainTxData; } @@ -104,6 +105,7 @@ protected: std::vector vSeeds; std::vector base58Prefixes[MAX_BASE58_TYPES]; std::string bech32_hrp; + std::string mweb_hrp; std::string strNetworkID; CBlock genesis; std::vector vFixedSeeds; diff --git a/src/key_io.cpp b/src/key_io.cpp index 007e89557..1fb8160e6 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -66,6 +66,16 @@ public: return bech32::Encode(bech32::Encoding::BECH32M, m_params.Bech32HRP(), data); } + std::string operator()(const StealthAddress& id) const + { + std::vector serialized = id.Serialized(); + + std::vector converted = {0}; + ConvertBits<8, 5, true>([&](unsigned char c) { converted.push_back(c); }, serialized.begin(), serialized.end()); + + return bech32::Encode(bech32::Encoding::BECH32, m_params.MWEB_HRP(), converted); + } + std::string operator()(const CNoDestination& no) const { return {}; } }; @@ -138,6 +148,19 @@ CTxDestination DecodeDestination(const std::string& str, const CChainParams& par return unk; } } + + auto decoded = bech32::Decode(str, true); + if (decoded.encoding == bech32::Encoding::BECH32 && decoded.hrp == params.MWEB_HRP()) { + std::vector converted; + converted.reserve(((decoded.data.size() - 1) * 5) / 8); + + if (ConvertBits<5, 8, false>([&](unsigned char c) { converted.push_back(c); }, decoded.data.begin() + 1, decoded.data.end())) { + if (converted.size() == 66) { + return StealthAddress(BigInt<33>(converted.data()), BigInt<33>(converted.data() + 33)); + } + } + } + return CNoDestination(); } } // namespace diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 9bc139b0d..9407fc3a5 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -154,7 +154,7 @@ UniValue blockToJSON(const CBlock& block, const CBlockIndex* tip, const CBlockIn const CBlockIndex* pnext; int confirmations = ComputeNextBlockAndDepth(tip, blockindex, pnext); result.pushKV("confirmations", confirmations); - result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS)); + result.pushKV("strippedsize", (int)::GetSerializeSize(block, PROTOCOL_VERSION | SERIALIZE_TRANSACTION_NO_WITNESS | SERIALIZE_NO_MWEB)); result.pushKV("size", (int)::GetSerializeSize(block, PROTOCOL_VERSION)); result.pushKV("weight", (int)::GetBlockWeight(block)); result.pushKV("height", blockindex->nHeight); diff --git a/src/rpc/util.cpp b/src/rpc/util.cpp index b8609e3e7..dd999362d 100644 --- a/src/rpc/util.cpp +++ b/src/rpc/util.cpp @@ -224,6 +224,7 @@ public: UniValue obj(UniValue::VOBJ); obj.pushKV("isscript", false); obj.pushKV("iswitness", false); + obj.pushKV("ismweb", false); return obj; } @@ -232,6 +233,7 @@ public: UniValue obj(UniValue::VOBJ); obj.pushKV("isscript", true); obj.pushKV("iswitness", false); + obj.pushKV("ismweb", false); return obj; } @@ -242,6 +244,7 @@ public: obj.pushKV("iswitness", true); obj.pushKV("witness_version", 0); obj.pushKV("witness_program", HexStr(id)); + obj.pushKV("ismweb", false); return obj; } @@ -252,6 +255,7 @@ public: obj.pushKV("iswitness", true); obj.pushKV("witness_version", 0); obj.pushKV("witness_program", HexStr(id)); + obj.pushKV("ismweb", false); return obj; } @@ -261,6 +265,16 @@ public: obj.pushKV("iswitness", true); obj.pushKV("witness_version", (int)id.version); obj.pushKV("witness_program", HexStr(Span(id.program, id.length))); + obj.pushKV("ismweb", false); + return obj; + } + + UniValue operator()(const StealthAddress& id) const + { + UniValue obj(UniValue::VOBJ); + obj.pushKV("isscript", false); + obj.pushKV("iswitness", false); + obj.pushKV("ismweb", true); return obj; } }; diff --git a/src/script/script.cpp b/src/script/script.cpp index f31472e42..a787c3738 100644 --- a/src/script/script.cpp +++ b/src/script/script.cpp @@ -5,6 +5,7 @@ #include