1
// Copyright © 2017-2020 Trust Wallet.
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
#pragma once
8

9
#include "../PublicKey.h"
10

11
#include <string>
12

13
namespace TW::TON {
14

15
using WorkchainId_t = int32_t;
16

17
/// Workchain ID, currently supported: masterchain: -1, basic workchain: 0
18
class Workchain {
19
  public:
20
    static constexpr WorkchainId_t MasterChainId = -1;
21
    static constexpr WorkchainId_t BasicChainId = 0;
22
    static constexpr WorkchainId_t InvalidChainId = 0x80000000;
23

24
    // The default workchain ID
25 1
    static WorkchainId_t defaultChain() { return BasicChainId; }
26
    static bool isValid(WorkchainId_t workchainId_in);
27
};
28

29
/// TON smart contract address, also account address
30 1
class Address {
31
  public:
32
    WorkchainId_t workchainId;
33

34
    static const uint8_t AddressLength = 32;
35

36
    // Address: 256 bits (for chains -1 and 0)
37
    TW::Data addrBytes;
38

39 1
    bool isBounceable{true};
40 1
    bool isTestOnly{false};
41

42
    /// Initializes a TON address with a string representation, either raw or user friendly
43
    explicit Address(const std::string& address);
44

45
    /// Initializes a TON address with a public key.  WorkchainId is optional, Basic chain by default.
46
    explicit Address(const PublicKey& publicKey, WorkchainId_t workchain = Workchain::defaultChain());
47

48
    /// Determines whether a string makes a valid address, in any format
49
    static bool isValid(const std::string& address, WorkchainId_t workchain = Workchain::defaultChain());
50

51
    /// Returns a string representation of the address (user friendly format)
52
    std::string string() const;
53

54
    /// Returns a string representation of the address, raw format
55
    std::string stringRaw() const;
56

57
  private:
58
    /// Empty constructor
59 1
    Address() = default;
60

61
    static bool parseAddress(const std::string& addressStr_in, Address& addr_inout);
62
    static bool parseRawAddress(const std::string& addressStr_in, Address& addr_inout);
63
    // Accepts user-friendly base64 format, also accepts Base64Url format
64
    static bool parseUserAddress(const std::string& addressStr_in, Address& addr_inout);
65
};
66

67
inline bool operator==(const Address& lhs, const Address& rhs) {
68
    return (lhs.workchainId == rhs.workchainId && lhs.addrBytes == rhs.addrBytes &&
69
            lhs.isBounceable == rhs.isBounceable && lhs.isTestOnly == rhs.isTestOnly);
70
}
71

72
} // namespace TW::TON

Read our documentation on viewing source code .

Loading