Skip to content

Commit

Permalink
key_io: Use Bech32M encoding and decoding for sp keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunovo committed Jul 10, 2024
1 parent f81fb83 commit 92b15d6
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 32 deletions.
8 changes: 0 additions & 8 deletions src/kernel/chainparams.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,6 @@ class CMainParams : public CChainParams {
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(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";
Expand Down Expand Up @@ -255,8 +253,6 @@ class CTestNetParams : public CChainParams {
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(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";
Expand Down Expand Up @@ -395,8 +391,6 @@ class SigNetParams : public CChainParams {
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(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";
Expand Down Expand Up @@ -527,8 +521,6 @@ class CRegTestParams : public CChainParams
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(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";
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/chainparams.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,6 @@ class CChainParams
SECRET_KEY,
EXT_PUBLIC_KEY,
EXT_SECRET_KEY,
SP_PUBLIC_KEY,
SP_SECRET_KEY,

MAX_BASE58_TYPES
};
Expand Down Expand Up @@ -120,6 +118,7 @@ class CChainParams
const std::vector<unsigned char>& 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<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
const CCheckpointData& Checkpoints() const { return checkpointData; }

Expand Down
51 changes: 29 additions & 22 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -329,48 +329,55 @@ std::string EncodeExtKey(const CExtKey& key)
SpKey DecodeSpKey(const std::string& str)
{
SpKey key;
std::vector<unsigned char> data;
const std::vector<unsigned char>& 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<unsigned char> 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<unsigned char> 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<unsigned char> data(BIP352_SPKEY_SIZE);
key.Encode(data.data());
std::vector<unsigned char> 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<unsigned char> data;
const std::vector<unsigned char>& 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<unsigned char> 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<unsigned char> 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<unsigned char> data(BIP352_SPKEY_SIZE);
key.Encode(data.data());
std::vector<unsigned char> 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;
}

Expand Down

0 comments on commit 92b15d6

Please sign in to comment.