Skip to content

Commit 267428a

Browse files
Eunovojosibake
authored andcommitted
key_io: Use Bech32M encoding and decoding for sp keys (#2)
1 parent 8376dea commit 267428a

File tree

3 files changed

+30
-32
lines changed

3 files changed

+30
-32
lines changed

src/kernel/chainparams.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -147,8 +147,6 @@ class CMainParams : public CChainParams {
147147
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,128);
148148
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x88, 0xB2, 0x1E};
149149
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x88, 0xAD, 0xE4};
150-
base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe
151-
base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb
152150

153151
bech32_hrp = "bc";
154152
silent_payment_hrp = "sp";
@@ -257,8 +255,6 @@ class CTestNetParams : public CChainParams {
257255
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
258256
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
259257
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
260-
base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe
261-
base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb
262258

263259
bech32_hrp = "tb";
264260
silent_payment_hrp = "tsp";
@@ -398,8 +394,6 @@ class SigNetParams : public CChainParams {
398394
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
399395
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
400396
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
401-
base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe
402-
base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb
403397

404398
bech32_hrp = "tb";
405399
silent_payment_hrp = "tsp";
@@ -537,8 +531,6 @@ class CRegTestParams : public CChainParams
537531
base58Prefixes[SECRET_KEY] = std::vector<unsigned char>(1,239);
538532
base58Prefixes[EXT_PUBLIC_KEY] = {0x04, 0x35, 0x87, 0xCF};
539533
base58Prefixes[EXT_SECRET_KEY] = {0x04, 0x35, 0x83, 0x94};
540-
base58Prefixes[SP_PUBLIC_KEY] = {0x22, 0x48, 0x2F, 0xFE}; //0x22482ffe
541-
base58Prefixes[SP_SECRET_KEY] = {0x07, 0xC4, 0x5A, 0xB9, 0xEB}; //0x07c45ab9eb
542534

543535
bech32_hrp = "bcrt";
544536
silent_payment_hrp = "sprt";

src/kernel/chainparams.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,6 @@ class CChainParams
8686
SECRET_KEY,
8787
EXT_PUBLIC_KEY,
8888
EXT_SECRET_KEY,
89-
SP_PUBLIC_KEY,
90-
SP_SECRET_KEY,
9189

9290
MAX_BASE58_TYPES
9391
};
@@ -120,6 +118,7 @@ class CChainParams
120118
const std::vector<unsigned char>& Base58Prefix(Base58Type type) const { return base58Prefixes[type]; }
121119
const std::string& Bech32HRP() const { return bech32_hrp; }
122120
const std::string& SilentPaymentHRP() const { return silent_payment_hrp; }
121+
const std::string SilentPaymentKeyHRP(bool is_public = true) const { return silent_payment_hrp + (is_public ? "pub" : "prv"); }
123122
const std::vector<uint8_t>& FixedSeeds() const { return vFixedSeeds; }
124123
const CCheckpointData& Checkpoints() const { return checkpointData; }
125124

src/key_io.cpp

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -329,48 +329,55 @@ std::string EncodeExtKey(const CExtKey& key)
329329
SpKey DecodeSpKey(const std::string& str)
330330
{
331331
SpKey key;
332-
std::vector<unsigned char> data;
333-
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::SP_SECRET_KEY);
334-
if (DecodeBase58Check(str, data, prefix.size() + BIP352_SPKEY_SIZE)) {
335-
if (data.size() == BIP352_SPKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
336-
key.Decode(data.data() + prefix.size());
337-
}
332+
auto result = bech32::Decode(str, bech32::CharLimit::SILENT_PAYMENTS);
333+
bool isValid = result.encoding == bech32::Encoding::BECH32M && result.hrp == Params().SilentPaymentKeyHRP(false);
334+
std::vector<unsigned char> data_out = {};
335+
data_out.reserve(BIP352_SPKEY_SIZE);
336+
isValid &= ConvertBits<5, 8, false>([&](unsigned char c) { data_out.push_back(c); }, result.data.begin(), result.data.end());
337+
if (isValid) {
338+
key.Decode(data_out.data());
338339
}
339340
return key;
340341
}
341342

342343
std::string EncodeSpKey(const SpKey& key)
343344
{
344-
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SP_SECRET_KEY);
345-
size_t size = data.size();
346-
data.resize(size + BIP352_SPKEY_SIZE);
347-
key.Encode(data.data() + size);
348-
std::string ret = EncodeBase58Check(data);
345+
std::vector<unsigned char> data(BIP352_SPKEY_SIZE);
346+
key.Encode(data.data());
347+
std::vector<unsigned char> data_out = {};
348+
data_out.reserve(((BIP352_SPKEY_SIZE * 8) / 5)+1);
349+
ConvertBits<8, 5, true>([&](unsigned char c) { data_out.push_back(c); }, data.begin(), data.end());
350+
auto ret = bech32::Encode(bech32::Encoding::BECH32M, Params().SilentPaymentKeyHRP(false), data_out);
349351
memory_cleanse(data.data(), data.size());
352+
memory_cleanse(data_out.data(), data_out.size());
350353
return ret;
351354
}
352355

353356
SpPubKey DecodeSpPubKey(const std::string& str)
354357
{
355358
SpPubKey key;
356-
std::vector<unsigned char> data;
357-
const std::vector<unsigned char>& prefix = Params().Base58Prefix(CChainParams::SP_PUBLIC_KEY);
358-
if (DecodeBase58Check(str, data, prefix.size() + BIP352_SPKEY_SIZE)) {
359-
if (data.size() == BIP352_SPKEY_SIZE + prefix.size() && std::equal(prefix.begin(), prefix.end(), data.begin())) {
360-
key.Decode(data.data() + prefix.size());
361-
}
359+
auto result = bech32::Decode(str, bech32::CharLimit::SILENT_PAYMENTS);
360+
bool isValid = result.encoding == bech32::Encoding::BECH32M &&
361+
result.hrp == Params().SilentPaymentKeyHRP();
362+
std::vector<unsigned char> data_out = {};
363+
data_out.reserve(BIP352_SPKEY_SIZE);
364+
isValid &= ConvertBits<5, 8, false>([&](unsigned char c) { data_out.push_back(c); }, result.data.begin(), result.data.end());
365+
if (isValid) {
366+
key.Decode(data_out.data());
362367
}
363368
return key;
364369
}
365370

366371
std::string EncodeSpPubKey(const SpPubKey& key)
367372
{
368-
std::vector<unsigned char> data = Params().Base58Prefix(CChainParams::SP_PUBLIC_KEY);
369-
size_t size = data.size();
370-
data.resize(size + BIP352_SPKEY_SIZE);
371-
key.Encode(data.data() + size);
372-
std::string ret = EncodeBase58Check(data);
373+
std::vector<unsigned char> data(BIP352_SPKEY_SIZE);
374+
key.Encode(data.data());
375+
std::vector<unsigned char> data_out = {};
376+
data_out.reserve(((BIP352_SPKEY_SIZE * 8) / 5)+1);
377+
ConvertBits<8, 5, true>([&](unsigned char c) { data_out.push_back(c); }, data.begin(), data.end());
378+
auto ret = bech32::Encode(bech32::Encoding::BECH32M, Params().SilentPaymentKeyHRP(), data_out);
373379
memory_cleanse(data.data(), data.size());
380+
memory_cleanse(data_out.data(), data_out.size());
374381
return ret;
375382
}
376383

0 commit comments

Comments
 (0)