|
| 1 | +// Copyright © 2017-2020 Trust. |
| 2 | +// |
| 3 | +// This file is part of Trust. The full Trust copyright notice, including |
| 4 | +// terms governing use, modification, and redistribution, is contained in the |
| 5 | +// file LICENSE at the root of the source code distribution tree. |
| 6 | + |
| 7 | +#include "Address.h" |
| 8 | + |
| 9 | +#include "../Base32.h" |
| 10 | +#include "../Data.h" |
| 11 | + |
| 12 | +using namespace TW; |
| 13 | +using namespace TW::Filecoin; |
| 14 | + |
| 15 | +static const char BASE32_ALPHABET_FILECOIN[] = "abcdefghijklmnopqrstuvwxyz234567"; |
| 16 | +static constexpr size_t checksumSize = 4; |
| 17 | + |
| 18 | +bool Address::isValid(const Data& data) { |
| 19 | + if (data.size() < 2) { |
| 20 | + return false; |
| 21 | + } |
| 22 | + Type type = getType(data[0]); |
| 23 | + if (type == Type::Invalid) { |
| 24 | + return false; |
| 25 | + } else if (type == Type::ID) { |
| 26 | + // Verify varuint encoding |
| 27 | + if (data.size() > 11) { |
| 28 | + return false; |
| 29 | + } |
| 30 | + if (data.size() == 11 && data[10] > 0x01) { |
| 31 | + return false; |
| 32 | + } |
| 33 | + int i; |
| 34 | + for (i = 1; i < data.size(); i++) { |
| 35 | + if ((data[i] & 0x80) == 0) { |
| 36 | + break; |
| 37 | + } |
| 38 | + } |
| 39 | + return i == data.size() - 1; |
| 40 | + } else { |
| 41 | + return data.size() == (1 + Address::payloadSize(type)); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +static bool isValidID(const std::string& string) { |
| 46 | + if (string.length() > 22) |
| 47 | + return false; |
| 48 | + for (int i = 2; i < string.length(); i++) { |
| 49 | + if (string[i] < '0' || string[i] > '9') { |
| 50 | + return false; |
| 51 | + } |
| 52 | + } |
| 53 | + try { |
| 54 | + size_t chars; |
| 55 | + [[maybe_unused]] uint64_t id = std::stoull(string.substr(2), &chars); |
| 56 | + return chars > 0; |
| 57 | + } catch (...) { |
| 58 | + return false; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +static bool isValidBase32(const std::string& string, Address::Type type) { |
| 63 | + // Check if valid Base32. |
| 64 | + uint8_t size = Address::payloadSize(type); |
| 65 | + Data decoded; |
| 66 | + if (!Base32::decode(string.substr(2), decoded, BASE32_ALPHABET_FILECOIN)) { |
| 67 | + return false; |
| 68 | + } |
| 69 | + |
| 70 | + // Check size |
| 71 | + if (decoded.size() != size + checksumSize) { |
| 72 | + return false; |
| 73 | + } |
| 74 | + |
| 75 | + // Extract raw address. |
| 76 | + Data address; |
| 77 | + address.push_back(static_cast<uint8_t>(type)); |
| 78 | + address.insert(address.end(), decoded.data(), decoded.data() + size); |
| 79 | + |
| 80 | + // Verify checksum. |
| 81 | + Data should_sum = Hash::blake2b(address, checksumSize); |
| 82 | + return std::memcmp(should_sum.data(), decoded.data() + size, checksumSize) == 0; |
| 83 | +} |
| 84 | + |
| 85 | +bool Address::isValid(const std::string& string) { |
| 86 | + if (string.length() < 3) { |
| 87 | + return false; |
| 88 | + } |
| 89 | + // Only main net addresses supported. |
| 90 | + if (string[0] != PREFIX) { |
| 91 | + return false; |
| 92 | + } |
| 93 | + // Get address type. |
| 94 | + auto type = parseType(string[1]); |
| 95 | + if (type == Type::Invalid) { |
| 96 | + return false; |
| 97 | + } |
| 98 | + |
| 99 | + // ID addresses are special, they are just numbers. |
| 100 | + return type == Type::ID ? isValidID(string) : isValidBase32(string, type); |
| 101 | +} |
| 102 | + |
| 103 | +Address::Address(const std::string& string) { |
| 104 | + if (!isValid(string)) |
| 105 | + throw std::invalid_argument("Invalid address data"); |
| 106 | + |
| 107 | + Type type = parseType(string[1]); |
| 108 | + // First byte is type |
| 109 | + bytes.push_back(static_cast<uint8_t>(type)); |
| 110 | + if (type == Type::ID) { |
| 111 | + uint64_t id = std::stoull(string.substr(2)); |
| 112 | + while (id >= 0x80) { |
| 113 | + bytes.push_back(((uint8_t)id) | 0x80); |
| 114 | + id >>= 7; |
| 115 | + } |
| 116 | + bytes.push_back((uint8_t)id); |
| 117 | + return; |
| 118 | + } |
| 119 | + |
| 120 | + Data decoded; |
| 121 | + if (!Base32::decode(string.substr(2), decoded, BASE32_ALPHABET_FILECOIN)) |
| 122 | + throw std::invalid_argument("Invalid address data"); |
| 123 | + uint8_t payloadSize = Address::payloadSize(type); |
| 124 | + |
| 125 | + bytes.insert(bytes.end(), decoded.data(), decoded.data() + payloadSize); |
| 126 | +} |
| 127 | + |
| 128 | +Address::Address(const Data& data) { |
| 129 | + if (!isValid(data)) { |
| 130 | + throw std::invalid_argument("Invalid address data"); |
| 131 | + } |
| 132 | + bytes = data; |
| 133 | +} |
| 134 | + |
| 135 | +Address::Address(const PublicKey& publicKey) { |
| 136 | + bytes.push_back(static_cast<uint8_t>(Type::SECP256K1)); |
| 137 | + Data hash = Hash::blake2b(publicKey.bytes, payloadSize(Type::SECP256K1)); |
| 138 | + bytes.insert(bytes.end(), hash.begin(), hash.end()); |
| 139 | +} |
| 140 | + |
| 141 | +std::string Address::string() const { |
| 142 | + std::string s; |
| 143 | + // Main net address prefix |
| 144 | + s.push_back(PREFIX); |
| 145 | + // Address type prefix |
| 146 | + s.push_back(typeAscii(type())); |
| 147 | + |
| 148 | + if (type() == Type::ID) { |
| 149 | + uint64_t id = 0; |
| 150 | + unsigned shift = 0; |
| 151 | + for (int i = 1; i < bytes.size(); i++) { |
| 152 | + if (bytes[i] < 0x80) { |
| 153 | + id |= bytes[i] << shift; |
| 154 | + break; |
| 155 | + } else { |
| 156 | + id |= ((uint64_t)(bytes[i] & 0x7F)) << shift; |
| 157 | + shift += 7; |
| 158 | + } |
| 159 | + } |
| 160 | + s.append(std::to_string(id)); |
| 161 | + return s; |
| 162 | + } |
| 163 | + |
| 164 | + uint8_t payloadSize = Address::payloadSize(type()); |
| 165 | + // Base32 encoded body |
| 166 | + Data toEncode(payloadSize + checksumSize); |
| 167 | + // Copy address payload without prefix |
| 168 | + std::copy(bytes.data() + 1, bytes.data() + payloadSize + 1, toEncode.data()); |
| 169 | + // Append Blake2b checksum |
| 170 | + Data bytesVec; |
| 171 | + bytesVec.assign(std::begin(bytes), std::end(bytes)); |
| 172 | + Data sum = Hash::blake2b(bytesVec, checksumSize); |
| 173 | + assert(sum.size() == checksumSize); |
| 174 | + std::copy(sum.begin(), sum.end(), toEncode.data() + payloadSize); |
| 175 | + s.append(Base32::encode(toEncode, BASE32_ALPHABET_FILECOIN)); |
| 176 | + |
| 177 | + return s; |
| 178 | +} |
0 commit comments