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 "Transaction.h"
10
#include "Address.h"
11
#include "../proto/FIO.pb.h"
12

13
#include "../Data.h"
14
#include "../PrivateKey.h"
15

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

19
namespace TW::FIO {
20

21
/// Represents current parameters of the FIO blockchain
22
/// @chanId: Constant chainId (32 bytes), obtained from get_info API
23
/// @headBlockNumber: The last block number, obtained from get_info API
24
/// @refBlockPrefix: Block prefix of last block, obtained from get_block API
25 1
class ChainParams {
26
public:
27
    Data chainId;
28
    uint64_t headBlockNumber;
29
    uint64_t refBlockPrefix;
30
};
31

32
class TransactionBuilder {
33
public:
34
    static constexpr auto ContractAddress = "fio.address";
35
    static constexpr auto ContractToken = "fio.token";
36
    static constexpr auto ContractPayRequest = "fio.reqobt";
37
    static constexpr auto AuthrizationActive = "active";
38
    static const auto ExpirySeconds = 3600;
39

40
    /// Generic transaction signer: Build a signed transaction, in Json, from the specific SigningInput messages.
41
    static std::string sign(Proto::SigningInput in);
42

43
    /// Create a signed RegisterFioAddress transaction, returned as json string (double quote delimited), suitable for register_fio_address RPC call
44
    /// @address The owners' FIO address. Ex.: "FIO6m1fMdTpRkRBnedvYshXCxLFiC5suRU8KDfx8xxtXp2hntxpnf"
45
    /// @privateKey The private key matching the address, needed for signing.
46
    /// @fioName The FIO name to be registered. Ex.: "mario@trust"
47
    /// @chainParams Current parameters from the FIO chain, must be obtained recently using get_info and get_block calls.
48
    /// @fee Max fee to spend, can be obtained using get_fee API.
49
    /// @walletTpId The FIO name of the originating wallet (project-wide constant)
50
    /// @expiryTime Expiry for this message, can be 0, then it is taken from current time with default expiry
51
    static std::string createRegisterFioAddress(const Address& address, const PrivateKey& privateKey, 
52
        const std::string& fioName,
53
        const ChainParams& chainParams, uint64_t fee, const std::string& walletTpId, uint32_t expiryTime);
54

55
    /// Create a signed AddAddress transaction, returned as json string (double quote delimited), suitable for add_pub_address RPC call
56
    /// @address The owners' FIO address
57
    /// @privateKey The private key matching the address, needed for signing.
58
    /// @fioName The FIO name already registered to the owner. Ex.: "dmitry@trust"
59
    /// @addressess List of public addresses to be registered, ex. {{"BTC", "bc1qv...7v"},{"BNB", "bnb1ts3...9s"}}
60
    /// @chainParams Current parameters from the FIO chain, must be obtained recently using get_info and get_block calls.
61
    /// @fee Max fee to spend, can be obtained using get_fee API.
62
    /// @walletTpId The FIO name of the originating wallet (project-wide constant)
63
    /// @expiryTime Expiry for this message, can be 0, then it is taken from current time with default expiry
64
    /// Note: fee is usually 0 for add_pub_address.
65
    static std::string createAddPubAddress(const Address& address, const PrivateKey& privateKey, const std::string& fioName,
66
        const std::vector<std::pair<std::string, std::string>>& pubAddresses,
67
        const ChainParams& chainParams, uint64_t fee, const std::string& walletTpId, uint32_t expiryTime);
68

69
    /// Create a signed TransferTokens transaction, returned as json string (double quote delimited), suitable for transfer_tokens_pub_key RPC call
70
    /// @address The owners' FIO address
71
    /// @privateKey The private key matching the address, needed for signing.
72
    /// @payeePublicKey FIO address of the payee. Ex.: "FIO6m1fMdTpRkRBnedvYshXCxLFiC5suRU8KDfx8xxtXp2hntxpnf"
73
    /// @amount Amount of FIO coins to be transferred.
74
    /// @chainParams Current parameters from the FIO chain, must be obtained recently using get_info and get_block calls.
75
    /// @fee Max fee to spend, can be obtained using get_fee API.
76
    /// @walletTpId The FIO name of the originating wallet (project-wide constant)
77
    /// @expiryTime Expiry for this message, can be 0, then it is taken from current time with default expiry
78
    static std::string createTransfer(const Address& address, const PrivateKey& privateKey, 
79
        const std::string& payeePublicKey, uint64_t amount,
80
        const ChainParams& chainParams, uint64_t fee, const std::string& walletTpId, uint32_t expiryTime);
81

82
    /// Create a signed RenewFioAddress transaction, returned as json string (double quote delimited), suitable for register_fio_address RPC call
83
    /// @address The owners' FIO address. Ex.: "FIO6m1fMdTpRkRBnedvYshXCxLFiC5suRU8KDfx8xxtXp2hntxpnf"
84
    /// @privateKey The private key matching the address, needed for signing.
85
    /// @fioName The FIO name to be renewed. Ex.: "alice@trust"
86
    /// @chainParams Current parameters from the FIO chain, must be obtained recently using get_info and get_block calls.
87
    /// @fee Max fee to spend, can be obtained using get_fee API.
88
    /// @walletTpId The FIO name of the originating wallet (project-wide constant)
89
    /// @expiryTime Expiry for this message, can be 0, then it is taken from current time with default expiry
90
    static std::string createRenewFioAddress(const Address& address, const PrivateKey& privateKey, 
91
        const std::string& fioName,
92
        const ChainParams& chainParams, uint64_t fee, const std::string& walletTpId, uint32_t expiryTime);
93

94
    /// Create a signed NewFundsRequest transaction, returned as json string (double quote delimited), suitable for new_funds_request RPC call
95
    /// @address The owners' FIO address. Ex.: "FIO6m1fMdTpRkRBnedvYshXCxLFiC5suRU8KDfx8xxtXp2hntxpnf"
96
    /// @privateKey The private key matching the address, needed for signing.
97
    /// @payerFioName The FIO name of the requested payer. Ex.: "alice@trust"
98
    /// @payerFioAddress The FIO address (not name) of the payer, owner of payerFioName.
99
    /// @payeeFioName Own FIO name.  Ex.: "bob@trust"
100
    /// @payeePublicAddress Public addressed of the payee, on the mentioned blockchain.
101
    /// @amount Amount requested (as string)
102
    /// @coinSymbol Token code of the amount requested
103
    /// @memo Memo free text.  Optional, may be empty.
104
    /// @hash Hash.  Optional, may be empty.
105
    /// @offlineUrl Attached offline URL.  Optional, may be empty.
106
    /// @chainParams Current parameters from the FIO chain, must be obtained recently using get_info and get_block calls.
107
    /// @fee Max fee to spend, can be obtained using get_fee API.
108
    /// @walletTpId The FIO name of the originating wallet (project-wide constant)
109
    /// @expiryTime Expiry for this message, can be 0, then it is taken from current time with default expiry
110
    /// @iv Optional initial values for encryption, should be empty.  Can be set for testability (16 bytes).
111
    /// Note: as of FIO testnet v0.9.0, this transaction is not accepted; see https://github.com/trustwallet/wallet-core/issues/850
112
    static std::string createNewFundsRequest(const Address& address, const PrivateKey& privateKey,
113
        const std::string& payerFioName, const std::string& payerFioAddress, const std::string& payeeFioName, const std::string& payeePublicAddress,
114
        const std::string& amount, const std::string& coinSymbol, const std::string& memo, const std::string& hash, const std::string& offlineUrl,
115
        const ChainParams& chainParams, uint64_t fee, const std::string& walletTpId, uint32_t expiryTime,
116
        const Data& iv);
117

118
    /// Used internally.  Creates signatures and json with transaction.
119
    static std::string signAdnBuildTx(const Data& chainId, const Data& packedTx, const PrivateKey& privateKey);
120

121
    /// Used internally.  If expiry is 0, fill it based on current time.  Return true if value has been changed.
122
    static bool expirySetDefaultIfNeeded(uint32_t& expiryTime);
123
};
124

125
} // namespace TW::FIO

Read our documentation on viewing source code .

Loading