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 "AddressV2.h"
10
#include "Data.h"
11
#include "../PublicKey.h"
12

13
#include <string>
14
#include <optional>
15

16
namespace TW::Cardano {
17

18
/// A Cardano-Shelley address, V3 or V2.
19 1
class AddressV3 {
20
  public:
21
    enum Discrimination: uint8_t {
22
        Discrim_Production = 0,
23
        Discrim_Test = 1,
24
    };
25

26
    enum Kind: uint8_t {
27
        Kind_Sentinel_Low = 2,
28
        Kind_Single = 3,
29
        Kind_Group = 4,
30
        Kind_Account = 5,
31
        Kind_Multisig = 6,
32
        Kind_Sentinel_High = 7,
33
    };
34

35
    Discrimination discrimination;
36

37
    Kind kind;
38

39
    /// key1: spending key or account key
40
    Data key1;
41

42
    /// group key (in case of Group address, empty otherwise)
43
    Data groupKey;
44

45
    /// Used in case of legacy address (V2)
46
    std::optional<AddressV2> legacyAddressV2;
47

48
    /// Determines whether a string makes a valid address.
49
    static bool isValid(const std::string& addr);
50

51
    /// Create a single spending key address
52
    static AddressV3 createSingle(Discrimination discrimination_in, const TW::Data& spendingKey);
53
    /// Create a group address
54
    static AddressV3 createGroup(Discrimination discrimination_in, const TW::Data& spendingKey, const TW::Data& groupKey);
55
    /// Create an account address
56
    static AddressV3 createAccount(Discrimination discrimination_in, const TW::Data& accountKey);
57

58
    /// Initializes a Cardano address with a string representation.  Throws if invalid.
59
    explicit AddressV3(const std::string& addr);
60

61
    /// Initializes a Cardano address from an extended public key.
62
    explicit AddressV3(const PublicKey& publicKey);
63

64
    /// Initializes a Cardano address from packed data (see data())
65
    explicit AddressV3(const Data& data);
66

67
    /// Copy constructor
68
    AddressV3(const AddressV3& other);
69

70
    void operator=(const AddressV3& other);
71

72
    /// Returns the Bech string representation of the address, with default HRP.
73
    std::string string() const;
74
    /// Returns the Bech string representation of the address, with given HRP.
75
    std::string string(const std::string& hrp) const;
76
    /// Returns the internal Base32 string representation of the address.
77
    std::string stringBase32() const;
78

79
    /// Check validity and parse elements of a string address.  Used internally by isValid and ctor.
80
    static bool parseAndCheckV3(const std::string& addr, Discrimination& discrimination, Kind& kind, TW::Data& key1, TW::Data& key2);
81

82
    /// Return the binary data representation (keys appended, internal format)
83
    Data data() const;
84

85
private:
86 1
    AddressV3() : discrimination(Discrim_Production), kind(Kind_Single) {}
87
};
88

89
inline bool operator==(const AddressV3& lhs, const AddressV3& rhs) {
90
    return lhs.discrimination == rhs.discrimination && lhs.kind == rhs.kind && lhs.key1 == rhs.key1 && lhs.groupKey == rhs.groupKey;
91
}
92

93
} // namespace TW::Cardano
94

95
/// Wrapper for C interface.
96
struct TWCardanoAddress {
97
    TW::Cardano::AddressV3 impl;
98
};

Read our documentation on viewing source code .

Loading