Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,6 @@ class CoinAddressDerivationTests {
KAVA -> assertEquals("kava1drpa0x9ptz0fql3frv562rcrhj2nstuz3pas87", address)
CARDANO -> assertEquals("addr1snpa4z7ntyfszv7ckquprdw75w4qjqh0qmya9jtkpxxlzxghlqyvv7l0yjamh8fxraw06p3ua8sj2g2gv98v4849s43t9g2999kquuu5egnprk", address)
NEO -> assertEquals("AT6w7PJvwPcSqHvtbNBY2aHPDv12eW5Uuf", address)
FILECOIN -> assertEquals("f1zzykebxldfcakj5wdb5n3n7priul522fnmjzori", address)
}
}
23 changes: 23 additions & 0 deletions coins.json
Original file line number Diff line number Diff line change
Expand Up @@ -1330,5 +1330,28 @@
"clientPublic": "http://seed1.ngd.network:10332",
"clientDocs": "https://neo.org/eco"
}
},
{
"id": "filecoin",
"name": "Filecoin",
"symbol": "FIL",
"decimals": 18,
"blockchain": "Filecoin",
"derivationPath": "m/44'/461'/0'/0/0",
"curve": "secp256k1",
"publicKeyType": "secp256k1Extended",
"explorer": {
"url": "https://filscan.io",
"txPath": "/#/message/detail?cid=",
"accountPath": "/#/address/detail?address=",
"sampleTx": "bafy2bzacecbm3ofxjjzcl2rg32ninphza34mm3ijr55zjsamwfqmz4ib63mqe",
"sampleAccount": "t1nbb73vhk5dtmnsgeaetbo76daepqjtrfoccn74i"
},
"info": {
"url": "https://filecoin.io/",
"client": "https://github.com/filecoin-project/lotus",
"clientPublic": "",
"clientDocs": "https://docs.lotu.sh"
}
}
]
1 change: 1 addition & 0 deletions docs/coins.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ This list is generated from [./coins.json](../coins.json)
| 434 | Kusama | KSM | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/kusama/info/logo.png" width="32" /> | <https://kusama.network> |
| 457 | Aeternity | AE | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/aeternity/info/logo.png" width="32" /> | <https://aeternity.com> |
| 459 | Kava | KAVA | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/kava/info/logo.png" width="32" /> | <https://kava.io> |
| 461 | Filecoin | FIL | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/filecoin/info/logo.png" width="32" /> | <https://filecoin.io/> |
| 500 | Theta | THETA | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/theta/info/logo.png" width="32" /> | <https://www.thetatoken.org> |
| 501 | Solana | SOL | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/solana/info/logo.png" width="32" /> | <https://solana.com> |
| 714 | Binance | BNB | <img src="https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/binance/info/logo.png" width="32" /> | <https://binance.org> |
Expand Down
1 change: 1 addition & 0 deletions include/TrustWalletCore/TWBlockchain.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ enum TWBlockchain {
TWBlockchainPolkadot = 29,
TWBlockchainCardano = 30,
TWBlockchainNEO = 31,
TWBlockchainFilecoin = 32,
};

TW_EXTERN_C_END
1 change: 1 addition & 0 deletions include/TrustWalletCore/TWCoinType.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ enum TWCoinType {
TWCoinTypeAlgorand = 283,
TWCoinTypeKusama = 434,
TWCoinTypePolkadot = 354,
TWCoinTypeFilecoin = 461,
};

/// Returns the blockchain for a coin type.
Expand Down
23 changes: 23 additions & 0 deletions include/TrustWalletCore/TWFilecoinSigner.h
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
7 changes: 7 additions & 0 deletions src/Coin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include "EOS/Address.h"
#include "Ethereum/Address.h"
#include "FIO/Address.h"
#include "Filecoin/Address.h"
#include "Groestlcoin/Address.h"
#include "Harmony/Address.h"
#include "Icon/Address.h"
Expand Down Expand Up @@ -190,6 +191,9 @@ bool TW::validateAddress(TWCoinType coin, const std::string& string) {

case TWCoinTypeNEO:
return NEO::Address::isValid(string);

case TWCoinTypeFilecoin:
return Filecoin::Address::isValid(string);
}
}

Expand Down Expand Up @@ -361,6 +365,9 @@ std::string TW::deriveAddress(TWCoinType coin, const PublicKey& publicKey) {

case TWCoinTypeNEO:
return NEO::Address(publicKey).string();

case TWCoinTypeFilecoin:
return Filecoin::Address(publicKey).string();
}
}

Expand Down
178 changes: 178 additions & 0 deletions src/Filecoin/Address.cpp
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);
}

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;
}
104 changes: 104 additions & 0 deletions src/Filecoin/Address.h
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
Loading