diff --git a/src/kernel/chainparams.cpp b/src/kernel/chainparams.cpp index 9740c7c7cf398a..0a9b2e9a77e26e 100644 --- a/src/kernel/chainparams.cpp +++ b/src/kernel/chainparams.cpp @@ -146,8 +146,6 @@ class CMainParams : public CChainParams { base58Prefixes[SECRET_KEY] = std::vector(1,128); base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E}; base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4}; - base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe - base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb bech32_hrp = "bc"; silent_payment_hrp = "sp"; @@ -255,8 +253,6 @@ class CTestNetParams : public CChainParams { base58Prefixes[SECRET_KEY] = std::vector(1,239); base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; - base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe - base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb bech32_hrp = "tb"; silent_payment_hrp = "tsp"; @@ -395,8 +391,6 @@ class SigNetParams : public CChainParams { base58Prefixes[SECRET_KEY] = std::vector(1,239); base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; - base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe - base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb bech32_hrp = "tb"; silent_payment_hrp = "tsp"; @@ -527,8 +521,6 @@ class CRegTestParams : public CChainParams base58Prefixes[SECRET_KEY] = std::vector(1,239); base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF}; base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94}; - base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe - base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb bech32_hrp = "bcrt"; silent_payment_hrp = "sprt"; diff --git a/src/kernel/chainparams.h b/src/kernel/chainparams.h index 5cc6c5979b73ad..49f9165dadeb76 100644 --- a/src/kernel/chainparams.h +++ b/src/kernel/chainparams.h @@ -86,8 +86,6 @@ class CChainParams SECRET_KEY, EXT_PUBLIC_KEY, EXT_SECRET_KEY, - SP_PUBLIC_KEY, - SP_SECRET_KEY, MAX_BASE58_TYPES }; @@ -120,6 +118,7 @@ class CChainParams const std::vector& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; } const std::string& Bech32HRP() const { return bech32_hrp; } const std::string& SilentPaymentHRP() const { return silent_payment_hrp; } + const std::string SilentPaymentKeyHRP(bool is_public = true) const { return silent_payment_hrp + (is_public ? "pub" : "prv"); } const std::vector& FixedSeeds() const { return vFixedSeeds; } const CCheckpointData& Checkpoints() const { return checkpointData; } diff --git a/src/key_io.cpp b/src/key_io.cpp index 3775c2d5436a05..fc5c6ec2cb39c2 100644 --- a/src/key_io.cpp +++ b/src/key_io.cpp @@ -329,48 +329,55 @@ std::string EncodeExtKey(const CExtKey& key) SpKey DecodeSpKey(const std::string& str) { SpKey key; - std::vector data; - const std::vector& prefix = Params().Base58Prefix(CChainParams::SP_SECRET_KEY); - if (DecodeBase58Check(str, data, prefix.size() + BIP352_SPKEY_SIZE)) { - if (data.size() == BIP352_SPKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) { - key.Decode(data.data() + prefix.size()); - } + auto result = bech32::Decode(str, bech32::CharLimit::SILENT_PAYMENTS); + bool isValid = result.encoding == bech32::Encoding::BECH32M && result.hrp == Params().SilentPaymentKeyHRP(false); + std::vector data_out = {}; + data_out.reserve(BIP352_SPKEY_SIZE); + isValid &= ConvertBits<5, 8, false>([&](unsigned char c) { data_out.push_back(c); }, result.data.begin(), result.data.end()); + if (isValid) { + key.Decode(data_out.data()); } return key; } std::string EncodeSpKey(const SpKey& key) { - std::vector data = Params().Base58Prefix(CChainParams::SP_SECRET_KEY); - size_t size = data.size(); - data.resize(size + BIP352_SPKEY_SIZE); - key.Encode(data.data() + size); - std::string ret = EncodeBase58Check(data); + std::vector data(BIP352_SPKEY_SIZE); + key.Encode(data.data()); + std::vector data_out = {}; + data_out.reserve(((BIP352_SPKEY_SIZE * 8) / 5)+1); + ConvertBits<8, 5, true>([&](unsigned char c) { data_out.push_back(c); }, data.begin(), data.end()); + auto ret = bech32::Encode(bech32::Encoding::BECH32M, Params().SilentPaymentKeyHRP(false), data_out); memory_cleanse(data.data(), data.size()); + memory_cleanse(data_out.data(), data_out.size()); return ret; } SpPubKey DecodeSpPubKey(const std::string& str) { SpPubKey key; - std::vector data; - const std::vector& prefix = Params().Base58Prefix(CChainParams::SP_PUBLIC_KEY); - if (DecodeBase58Check(str, data, prefix.size() + BIP352_SPKEY_SIZE)) { - if (data.size() == BIP352_SPKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) { - key.Decode(data.data() + prefix.size()); - } + auto result = bech32::Decode(str, bech32::CharLimit::SILENT_PAYMENTS); + bool isValid = result.encoding == bech32::Encoding::BECH32M && + result.hrp == Params().SilentPaymentKeyHRP(); + std::vector data_out = {}; + data_out.reserve(BIP352_SPKEY_SIZE); + isValid &= ConvertBits<5, 8, false>([&](unsigned char c) { data_out.push_back(c); }, result.data.begin(), result.data.end()); + if (isValid) { + key.Decode(data_out.data()); } return key; } std::string EncodeSpPubKey(const SpPubKey& key) { - std::vector data = Params().Base58Prefix(CChainParams::SP_PUBLIC_KEY); - size_t size = data.size(); - data.resize(size + BIP352_SPKEY_SIZE); - key.Encode(data.data() + size); - std::string ret = EncodeBase58Check(data); + std::vector data(BIP352_SPKEY_SIZE); + key.Encode(data.data()); + std::vector data_out = {}; + data_out.reserve(((BIP352_SPKEY_SIZE * 8) / 5)+1); + ConvertBits<8, 5, true>([&](unsigned char c) { data_out.push_back(c); }, data.begin(), data.end()); + auto ret = bech32::Encode(bech32::Encoding::BECH32M, Params().SilentPaymentKeyHRP(), data_out); memory_cleanse(data.data(), data.size()); + memory_cleanse(data_out.data(), data_out.size()); return ret; }