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 <TrustWalletCore/TWCoinType.h>
10

11
#include "Data.h"
12
#include "PublicKey.h"
13
#include "PrivateKey.h"
14

15
#include <string>
16
#include <vector>
17

18
namespace TW {
19

20
/// Interface for coin-specific entry, used to dispatch calls to coins
21
/// Implement this for all coins.
22
class CoinEntry {
23
public:
24
    // Report the coin types this implementation is responsible of
25
    virtual const std::vector<TWCoinType> coinTypes() const = 0;
26
    virtual bool validateAddress(TWCoinType coin, const std::string& address, TW::byte p2pkh, TW::byte p2sh, const char* hrp) const = 0;
27
    // normalizeAddress is optional, it may leave this default, no-change implementation
28 1
    virtual std::string normalizeAddress(TWCoinType coin, const std::string& address) const { return address; }
29
    virtual std::string deriveAddress(TWCoinType coin, const PublicKey& publicKey, TW::byte p2pkh, const char* hrp) const = 0;
30
    // Signing
31
    virtual void sign(TWCoinType coin, const Data& dataIn, Data& dataOut) const = 0;
32 1
    virtual bool supportsJSONSigning() const { return false; }
33
    // It is optional, Signing JSON input with private key
34 0
    virtual std::string signJSON(TWCoinType coin, const std::string& json, const Data& key) const { return ""; }
35
    // Sign and encode broadcastable raw transaction
36 1
    virtual void encodeRawTx(TWCoinType coin, const Data& dataIn, Data& dataOut) const { return; }
37 1
    virtual void decodeRawTx(TWCoinType coin, const Data& dataIn, Data& dataOut) const { return; }
38
    // Planning, for UTXO chains, in preparation for signing
39
    // It is optional, only UTXO chains need it, default impl. leaves empty result.
40 1
    virtual void plan(TWCoinType coin, const Data& dataIn, Data& dataOut) const { return; }
41
};
42

43
// In each coin's Entry.cpp the specific types of the coin are used, this template enforces the Signer implement:
44
// static Proto::SigningOutput sign(const Proto::SigningInput& input) noexcept;
45
// Note: use output parameter to avoid unneeded copies
46
template <typename Signer, typename Input>
47 1
void signTemplate(const Data& dataIn, Data& dataOut) {
48 1
    auto input = Input();
49 1
    input.ParseFromArray(dataIn.data(), (int)dataIn.size());
50 1
    auto serializedOut = Signer::sign(input).SerializeAsString();
51 1
    dataOut.insert(dataOut.end(), serializedOut.begin(), serializedOut.end());
52
}
53

54
template <typename Signer, typename Input>
55 1
void encodeTemplate(const Data& dataIn, Data& dataOut) {
56 1
    auto input = Input();
57 1
    input.ParseFromArray(dataIn.data(), (int)dataIn.size());
58 1
    auto encoded = Signer::sign(input).encoded();
59 1
    dataOut.insert(dataOut.end(), encoded.begin(), encoded.end());
60
}
61

62
// Note: use output parameter to avoid unneeded copies
63
template <typename Planner, typename Input>
64 1
void planTemplate(const Data& dataIn, Data& dataOut) {
65 1
    auto input = Input();
66 1
    input.ParseFromArray(dataIn.data(), (int)dataIn.size());
67 1
    auto serializedOut = Planner::plan(input).SerializeAsString();
68 1
    dataOut.insert(dataOut.end(), serializedOut.begin(), serializedOut.end());
69
}
70

71
} // namespace TW

Read our documentation on viewing source code .

Loading