forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCoin.h
173 lines (128 loc) · 6.2 KB
/
Coin.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
// Copyright © 2017-2020 Trust Wallet.
//
// 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 "Data.h"
#include "Hash.h"
#include "DerivationPath.h"
#include "PrivateKey.h"
#include "PublicKey.h"
#include "uint256.h"
#include "CoinEntry.h"
#include <TrustWalletCore/TWBlockchain.h>
#include <TrustWalletCore/TWCoinType.h>
#include <TrustWalletCore/TWCurve.h>
#include <TrustWalletCore/TWHDVersion.h>
#include <TrustWalletCore/TWPurpose.h>
#include <TrustWalletCore/TWDerivation.h>
#include <string>
#include <vector>
namespace TW {
// Return the set of supported coin types.
std::vector<TWCoinType> getCoinTypes();
/// Validates an address for a particular coin.
bool validateAddress(TWCoinType coin, const std::string& address);
/// Validates an address for a particular coin.
bool validateAddress(TWCoinType coin, const std::string& address, const PrefixVariant& prefix);
/// Validates and normalizes an address for a particular coin.
std::string normalizeAddress(TWCoinType coin, const std::string& address);
std::string normalizeAddress(TWCoinType coin, const std::string& address, const PrefixVariant& prefix);
/// Returns the blockchain for a coin type.
TWBlockchain blockchain(TWCoinType coin);
/// Returns the purpose for a coin type.
TWPurpose purpose(TWCoinType coin);
/// Returns the curve that should be used for a coin type.
TWCurve curve(TWCoinType coin);
/// Returns the default xpub HD version that should be used for a coin type.
TWHDVersion xpubVersion(TWCoinType coin);
/// Returns the default xprv HD version that should be used for a coin type.
TWHDVersion xprvVersion(TWCoinType coin);
/// Returns the xpub HD version for a TWDerivation.
TWHDVersion xpubVersionDerivation(TWCoinType coin, TWDerivation derivation);
/// Returns the xprv HD version for a TWDerivation.
TWHDVersion xprvVersionDerivation(TWCoinType coin, TWDerivation derivation);
/// Returns the default derivation path for a particular coin.
DerivationPath derivationPath(TWCoinType coin);
/// Returns an alternative derivation path for a particular coin, TWDerivationDefault for default.
DerivationPath derivationPath(TWCoinType coin, TWDerivation derivation);
/// Returns the string name of a derivation for a particular coin.
const char* derivationName(TWCoinType coin, TWDerivation derivation);
/// Returns the public key type for a particular coin.
enum TWPublicKeyType publicKeyType(TWCoinType coin);
/// Derives the address for a particular coin from the private key.
std::string deriveAddress(TWCoinType coin, const PrivateKey& privateKey);
/// Derives the address for a particular coin from the private key, with given derivation.
std::string deriveAddress(TWCoinType coin, const PrivateKey& privateKey, TWDerivation derivation);
/// Derives the address for a particular coin from the public key, with given derivation.
std::string deriveAddress(TWCoinType coin, const PublicKey& publicKey, TWDerivation derivation = TWDerivationDefault, const std::string& hrp = "");
/// Derives the address for a particular coin from the public key, with given derivation and explicit addressPrefix.
std::string deriveAddress(TWCoinType coin, const PublicKey& publicKey, const PrefixVariant& addressPrefix, TWDerivation derivation = TWDerivationDefault);
/// Returns the binary representation of a string address
Data addressToData(TWCoinType coin, const std::string& address);
/// Hasher for deriving the extended public key
Hash::Hasher publicKeyHasher(TWCoinType coin);
/// Hasher to use for base 58 checksums in keys (extended private, public)
Hash::Hasher base58Hasher(TWCoinType coin);
/// Hasher used inside address generation (hash of public key)
Hash::Hasher addressHasher(TWCoinType coin);
/// Returns static prefix for a coin type.
byte staticPrefix(TWCoinType coin);
/// Returns P2PKH prefix for a coin type.
byte p2pkhPrefix(TWCoinType coin);
/// Returns P2SH prefix for a coin type.
byte p2shPrefix(TWCoinType coin);
/// Returns human readable part for a coin type.
enum TWHRP hrp(TWCoinType coin);
/// Returns the ss58 prefix of a coin type.
std::uint32_t ss58Prefix(TWCoinType coin);
/// Returns chain ID.
const char* chainId(TWCoinType coin);
// Note: use output parameter to avoid unneeded copies
void anyCoinSign(TWCoinType coinType, const Data& dataIn, Data& dataOut);
uint32_t slip44Id(TWCoinType coin);
std::string anySignJSON(TWCoinType coinType, const std::string& json, const Data& key);
bool supportsJSONSigning(TWCoinType coinType);
void anyCoinPlan(TWCoinType coinType, const Data& dataIn, Data& dataOut);
Data anyCoinPreImageHashes(TWCoinType coinType, const Data& txInputData);
void anyCoinCompileWithSignatures(TWCoinType coinType, const Data& txInputData, const std::vector<Data>& signatures, const std::vector<PublicKey>& publicKeys, Data& txOutputOut);
Data anyCoinBuildTransactionInput(TWCoinType coinType, const std::string& from, const std::string& to, const uint256_t& amount, const std::string& asset, const std::string& memo, const std::string& chainId);
// Describes a derivation: path + optional format + optional name
struct Derivation {
TWDerivation name = TWDerivationDefault;
const char* path = "";
const char* nameString = "";
TWHDVersion xpubVersion = TWHDVersionNone;
TWHDVersion xprvVersion = TWHDVersionNone;
};
// Contains only simple types.
struct CoinInfo {
const char* id;
const char* name;
TWBlockchain blockchain;
TWPurpose purpose;
TWCurve curve;
std::vector<Derivation> derivation;
TWPublicKeyType publicKeyType;
byte staticPrefix;
byte p2pkhPrefix;
byte p2shPrefix;
TWHRP hrp;
const char* chainId;
Hash::Hasher publicKeyHasher;
Hash::Hasher base58Hasher;
Hash::Hasher addressHasher;
const char* symbol;
int decimals;
const char* explorerTransactionUrl;
const char* explorerAccountUrl;
uint32_t slip44;
std::uint32_t ss58Prefix;
// returns default derivation
const Derivation defaultDerivation() const {
return (derivation.size() > 0) ? derivation[0] : Derivation();
}
const Derivation derivationByName(TWDerivation name) const;
};
} // namespace TW