-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Filecoin support #811
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Filecoin support #811
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // Copyright © 2017-2020 Trust. | ||
| // | ||
| // This file is part of Trust. The full Trust copyright notice, including | ||
| // terms governing use, modification, and redistribution, is contained in the | ||
| // file LICENSE at the root of the source code distribution tree. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "TWBase.h" | ||
| #include "TWData.h" | ||
| #include "TWFilecoinProto.h" | ||
|
|
||
| TW_EXTERN_C_BEGIN | ||
|
|
||
| /// Helper class to sign Filecoin transactions. | ||
| TW_EXPORT_CLASS | ||
| struct TWFilecoinSigner; | ||
|
|
||
| /// Signs a transaction. | ||
| TW_EXPORT_STATIC_METHOD | ||
| TW_Filecoin_Proto_SigningOutput TWFilecoinSignerSign(TW_Filecoin_Proto_SigningInput input); | ||
|
|
||
| TW_EXTERN_C_END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,178 @@ | ||
| // Copyright © 2017-2020 Trust. | ||
| // | ||
| // This file is part of Trust. The full Trust copyright notice, including | ||
| // terms governing use, modification, and redistribution, is contained in the | ||
| // file LICENSE at the root of the source code distribution tree. | ||
|
|
||
| #include "Address.h" | ||
|
|
||
| #include "../Base32.h" | ||
| #include "../Data.h" | ||
|
|
||
| using namespace TW; | ||
| using namespace TW::Filecoin; | ||
|
|
||
| static const char BASE32_ALPHABET_FILECOIN[] = "abcdefghijklmnopqrstuvwxyz234567"; | ||
| static constexpr size_t checksumSize = 4; | ||
|
|
||
| bool Address::isValid(const Data& data) { | ||
| if (data.size() < 2) { | ||
| return false; | ||
| } | ||
| Type type = getType(data[0]); | ||
| if (type == Type::Invalid) { | ||
| return false; | ||
| } else if (type == Type::ID) { | ||
| // Verify varuint encoding | ||
| if (data.size() > 11) { | ||
| return false; | ||
| } | ||
| if (data.size() == 11 && data[10] > 0x01) { | ||
| return false; | ||
| } | ||
| int i; | ||
| for (i = 1; i < data.size(); i++) { | ||
| if ((data[i] & 0x80) == 0) { | ||
| break; | ||
| } | ||
| } | ||
| return i == data.size() - 1; | ||
| } else { | ||
| return data.size() == (1 + Address::payloadSize(type)); | ||
| } | ||
| } | ||
|
|
||
| static bool isValidID(const std::string& string) { | ||
| if (string.length() > 22) | ||
| return false; | ||
| for (int i = 2; i < string.length(); i++) { | ||
| if (string[i] < '0' || string[i] > '9') { | ||
| return false; | ||
| } | ||
| } | ||
| try { | ||
| size_t chars; | ||
| [[maybe_unused]] uint64_t id = std::stoull(string.substr(2), &chars); | ||
| return chars > 0; | ||
| } catch (...) { | ||
| return false; | ||
| } | ||
| } | ||
|
|
||
| static bool isValidBase32(const std::string& string, Address::Type type) { | ||
| // Check if valid Base32. | ||
| uint8_t size = Address::payloadSize(type); | ||
| Data decoded; | ||
| if (!Base32::decode(string.substr(2), decoded, BASE32_ALPHABET_FILECOIN)) { | ||
| return false; | ||
| } | ||
|
|
||
| // Check size | ||
| if (decoded.size() != size + checksumSize) { | ||
| return false; | ||
| } | ||
|
|
||
| // Extract raw address. | ||
| Data address; | ||
| address.push_back(static_cast<uint8_t>(type)); | ||
| address.insert(address.end(), decoded.data(), decoded.data() + size); | ||
|
|
||
| // Verify checksum. | ||
| Data should_sum = Hash::blake2b(address, checksumSize); | ||
| return std::memcmp(should_sum.data(), decoded.data() + size, checksumSize) == 0; | ||
| } | ||
|
|
||
| bool Address::isValid(const std::string& string) { | ||
| if (string.length() < 3) { | ||
| return false; | ||
| } | ||
| // Only main net addresses supported. | ||
| if (string[0] != PREFIX) { | ||
| return false; | ||
| } | ||
| // Get address type. | ||
| auto type = parseType(string[1]); | ||
| if (type == Type::Invalid) { | ||
| return false; | ||
| } | ||
|
|
||
| // ID addresses are special, they are just numbers. | ||
| return type == Type::ID ? isValidID(string) : isValidBase32(string, type); | ||
| } | ||
|
|
||
| Address::Address(const std::string& string) { | ||
| if (!isValid(string)) | ||
| throw std::invalid_argument("Invalid address data"); | ||
|
|
||
| Type type = parseType(string[1]); | ||
| // First byte is type | ||
| bytes.push_back(static_cast<uint8_t>(type)); | ||
| if (type == Type::ID) { | ||
| uint64_t id = std::stoull(string.substr(2)); | ||
| while (id >= 0x80) { | ||
| bytes.push_back(((uint8_t)id) | 0x80); | ||
| id >>= 7; | ||
| } | ||
| bytes.push_back((uint8_t)id); | ||
| return; | ||
| } | ||
|
|
||
| Data decoded; | ||
| if (!Base32::decode(string.substr(2), decoded, BASE32_ALPHABET_FILECOIN)) | ||
| throw std::invalid_argument("Invalid address data"); | ||
| uint8_t payloadSize = Address::payloadSize(type); | ||
|
|
||
| bytes.insert(bytes.end(), decoded.data(), decoded.data() + payloadSize); | ||
riptl marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Address::Address(const Data& data) { | ||
| if (!isValid(data)) { | ||
| throw std::invalid_argument("Invalid address data"); | ||
| } | ||
| bytes = data; | ||
| } | ||
|
|
||
| Address::Address(const PublicKey& publicKey) { | ||
| bytes.push_back(static_cast<uint8_t>(Type::SECP256K1)); | ||
| Data hash = Hash::blake2b(publicKey.bytes, payloadSize(Type::SECP256K1)); | ||
| bytes.insert(bytes.end(), hash.begin(), hash.end()); | ||
| } | ||
|
|
||
| std::string Address::string() const { | ||
| std::string s; | ||
| // Main net address prefix | ||
| s.push_back(PREFIX); | ||
| // Address type prefix | ||
| s.push_back(typeAscii(type())); | ||
|
|
||
| if (type() == Type::ID) { | ||
| uint64_t id = 0; | ||
| unsigned shift = 0; | ||
| for (int i = 1; i < bytes.size(); i++) { | ||
| if (bytes[i] < 0x80) { | ||
| id |= bytes[i] << shift; | ||
| break; | ||
| } else { | ||
| id |= ((uint64_t)(bytes[i] & 0x7F)) << shift; | ||
| shift += 7; | ||
| } | ||
| } | ||
| s.append(std::to_string(id)); | ||
| return s; | ||
| } | ||
|
|
||
| uint8_t payloadSize = Address::payloadSize(type()); | ||
| // Base32 encoded body | ||
| Data toEncode(payloadSize + checksumSize); | ||
| // Copy address payload without prefix | ||
| std::copy(bytes.data() + 1, bytes.data() + payloadSize + 1, toEncode.data()); | ||
| // Append Blake2b checksum | ||
| Data bytesVec; | ||
| bytesVec.assign(std::begin(bytes), std::end(bytes)); | ||
| Data sum = Hash::blake2b(bytesVec, checksumSize); | ||
| assert(sum.size() == checksumSize); | ||
| std::copy(sum.begin(), sum.end(), toEncode.data() + payloadSize); | ||
| s.append(Base32::encode(toEncode, BASE32_ALPHABET_FILECOIN)); | ||
|
|
||
| return s; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| // Copyright © 2017-2020 Trust. | ||
| // | ||
| // This file is part of Trust. The full Trust copyright notice, including | ||
| // terms governing use, modification, and redistribution, is contained in the | ||
| // file LICENSE at the root of the source code distribution tree. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include "../PublicKey.h" | ||
|
|
||
| #include <array> | ||
| #include <cstdint> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace TW::Filecoin { | ||
|
|
||
| class Address { | ||
| public: | ||
| enum class Type : uint8_t { | ||
| ID = 0, | ||
| SECP256K1 = 1, | ||
| ACTOR = 2, | ||
| BLS = 3, | ||
| Invalid, | ||
| }; | ||
|
|
||
| /// Address data with address type prefix. | ||
| Data bytes; | ||
|
|
||
| /// Determines whether a collection of bytes makes a valid address. | ||
| static bool isValid(const Data& data); | ||
|
|
||
| /// Determines whether a string makes a valid encoded address. | ||
| static bool isValid(const std::string& string); | ||
|
|
||
| /// Initializes an address with a string representation. | ||
| explicit Address(const std::string& string); | ||
|
|
||
| /// Initializes an address with a collection of bytes. | ||
| explicit Address(const Data& data); | ||
|
|
||
| /// Initializes an address with a secp256k1 public key. | ||
| explicit Address(const PublicKey& publicKey); | ||
|
|
||
| /// Returns a string representation of the address. | ||
| [[nodiscard]] std::string string() const; | ||
|
|
||
| /// Returns the type of an address. | ||
| Type type() const { return getType(bytes[0]); } | ||
|
|
||
| /// Address prefix | ||
| static constexpr char PREFIX = 'f'; | ||
|
|
||
| public: | ||
| /// Attempts to get the type by number. | ||
| static Type getType(uint8_t raw) { | ||
| switch (raw) { | ||
| case 0: | ||
| return Type::ID; | ||
| case 1: | ||
| return Type::SECP256K1; | ||
| case 2: | ||
| return Type::ACTOR; | ||
| case 3: | ||
| return Type::BLS; | ||
| default: | ||
| return Type::Invalid; | ||
| } | ||
| } | ||
|
|
||
| /// Attempts to get the type by ASCII. | ||
| static Type parseType(char c) { | ||
| if (c >= '0' && c <= '3') { | ||
| return static_cast<Type>(c - '0'); | ||
| } else { | ||
| return Type::Invalid; | ||
| } | ||
| } | ||
|
|
||
| /// Returns ASCII character of type | ||
| static char typeAscii(Type t) { return '0' + static_cast<char>(t); } | ||
|
|
||
| // Returns the payload size (excluding any prefixes) of an address type. | ||
| // If the payload size is undefined/variable (e.g. ID) | ||
| // or the type is unknown, it returns zero. | ||
| static uint8_t payloadSize(Type t) { | ||
| switch (t) { | ||
| case Type::SECP256K1: | ||
| case Type::ACTOR: | ||
| return 20; | ||
| case Type::BLS: | ||
| return 48; | ||
| default: | ||
| return 0; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| inline bool operator==(const Address& lhs, const Address& rhs) { | ||
| return lhs.bytes == rhs.bytes; | ||
| } | ||
|
|
||
| } // namespace TW::Filecoin |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.