-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase58.cpp
More file actions
48 lines (39 loc) · 1.13 KB
/
base58.cpp
File metadata and controls
48 lines (39 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
// Bitcoin's Base58 Alphabet (No 0, O, I, or l)
const char* ALPHABET = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
std::string encodeBase58(std::vector<unsigned char> data) {
std::vector<int> digits(1, 0);
for (unsigned char byte : data) {
int carry = byte;
for (size_t i = 0; i < digits.size(); ++i) {
carry += digits[i] << 8;
digits[i] = carry % 58;
carry /= 58;
}
while (carry) {
digits.push_back(carry % 58);
carry /= 58;
}
}
// Add leading '1's for each leading zero byte
std::string result = "";
for (unsigned char byte : data) {
if (byte == 0) result += ALPHABET[0];
else break;
}
// Convert digits to alphabet characters
for (int i = digits.size() - 1; i >= 0; --i) {
result += ALPHABET[digits[i]];
}
return result;
}
int main() {
// Example: A mock 20-byte public key hash
std::vector<unsigned char> mockPayload = {0x00, 0x62, 0xe9, 0x07, 0xb1, 0x5c, 0xbf, 0x27, 0xd5, 0x42};
std::string address = encodeBase58(mockPayload);
std::cout << "Encoded Base58: " << address << std::endl;
return 0;
}