#1151 Add Permission chain

Closed pavelkrolevets
Coverage Reach
src/interface/TWEthereumAbiFunction.cpp src/interface/TWStoredKey.cpp src/interface/TWBitcoinScript.cpp src/interface/TWAnyAddress.cpp src/interface/TWHash.cpp src/interface/TWData.cpp src/interface/TWPrivateKey.cpp src/interface/TWEthereumAbiValue.cpp src/interface/TWHDWallet.cpp src/interface/TWPublicKey.cpp src/interface/TWCoinType.cpp src/interface/TWBitcoinAddress.cpp src/interface/TWAnySigner.cpp src/interface/TWString+Hex.cpp src/interface/TWString.cpp src/interface/TWSegwitAddress.cpp src/interface/TWEthereumAbi.cpp src/interface/TWBase58.cpp src/interface/TWAES.cpp src/interface/TWGroestlcoinAddress.cpp src/interface/TWAccount.cpp src/interface/TWFIOAccount.cpp src/interface/TWNEARAccount.cpp src/interface/TWHDVersion.cpp src/interface/TWBitcoin.cpp src/Ethereum/ABI/ParamFactory.cpp src/Ethereum/ABI/ParamNumber.h src/Ethereum/ABI/Parameters.cpp src/Ethereum/ABI/Bytes.cpp src/Ethereum/ABI/ValueEncoder.cpp src/Ethereum/ABI/Bytes.h src/Ethereum/ABI/Array.cpp src/Ethereum/ABI/ParamNumber.cpp src/Ethereum/ABI/Function.cpp src/Ethereum/ABI/Function.h src/Ethereum/ABI/Parameters.h src/Ethereum/ABI/ParamAddress.h src/Ethereum/ABI/Array.h src/Ethereum/ABI/ValueDecoder.cpp src/Ethereum/ABI/ParamBase.h src/Ethereum/ABI/ValueEncoder.h src/Ethereum/RLP.cpp src/Ethereum/Signer.cpp src/Ethereum/ContractCall.cpp src/Ethereum/RLP.h src/Ethereum/AddressChecksum.cpp src/Ethereum/Address.cpp src/Ethereum/Entry.cpp src/Ethereum/Transaction.h src/Ethereum/Entry.h src/Ethereum/Address.h src/Ethereum/Signer.h src/Bitcoin/Script.cpp src/Bitcoin/TransactionSigner.cpp src/Bitcoin/Transaction.cpp src/Bitcoin/TransactionBuilder.cpp src/Bitcoin/UnspentSelector.cpp src/Bitcoin/SegwitAddress.cpp src/Bitcoin/CashAddress.cpp src/Bitcoin/Signer.cpp src/Bitcoin/Entry.cpp src/Bitcoin/TransactionPlan.h src/Bitcoin/FeeCalculator.cpp src/Bitcoin/Script.h src/Bitcoin/TransactionBuilder.h src/Bitcoin/TransactionInput.cpp src/Bitcoin/TransactionSigner.h src/Bitcoin/Transaction.h src/Bitcoin/SegwitAddress.h src/Bitcoin/SigHashType.h src/Bitcoin/UnspentSelector.h src/Bitcoin/OutPoint.h src/Bitcoin/Address.h src/Bitcoin/TransactionInput.h src/Bitcoin/OutPoint.cpp src/Bitcoin/TransactionOutput.cpp src/Bitcoin/CashAddress.h src/Bitcoin/Entry.h src/Bitcoin/FeeCalculator.h src/Bitcoin/OpCodes.h src/Bitcoin/TransactionOutput.h src/Permission/ABI/ParamFactory.cpp src/Permission/ABI/ParamNumber.h src/Permission/ABI/Parameters.cpp src/Permission/ABI/Bytes.cpp src/Permission/ABI/ValueEncoder.cpp src/Permission/ABI/Bytes.h src/Permission/ABI/Array.cpp src/Permission/ABI/ParamNumber.cpp src/Permission/ABI/Function.cpp src/Permission/ABI/Parameters.h src/Permission/ABI/Function.h src/Permission/ABI/ParamAddress.h src/Permission/ABI/Array.h src/Permission/ABI/ValueDecoder.cpp src/Permission/ABI/ParamBase.h src/Permission/ABI/ValueEncoder.h src/Permission/RLP.cpp src/Permission/Signer.cpp src/Permission/AddressChecksum.cpp src/Permission/Address.cpp src/Permission/RLP.h src/Permission/Entry.cpp src/Permission/Transaction.h src/Permission/Entry.h src/Permission/Address.h src/Permission/Signer.h src/FIO/TransactionBuilder.cpp src/FIO/Encryption.cpp src/FIO/Action.cpp src/FIO/Address.cpp src/FIO/Action.h src/FIO/NewFundsRequest.cpp src/FIO/Actor.cpp src/FIO/Signer.cpp src/FIO/Transaction.cpp src/FIO/Entry.cpp src/FIO/Transaction.h src/FIO/Entry.h src/FIO/NewFundsRequest.h src/FIO/Address.h src/FIO/TransactionBuilder.h src/Harmony/Signer.cpp src/Harmony/Staking.h src/Harmony/Transaction.h src/Harmony/Address.h src/Harmony/Entry.cpp src/Harmony/Entry.h src/Harmony/Address.cpp src/Harmony/Signer.h src/Ontology/ParamsBuilder.cpp src/Ontology/Address.cpp src/Ontology/OngTxBuilder.cpp src/Ontology/Signer.cpp src/Ontology/Transaction.cpp src/Ontology/Ong.cpp src/Ontology/OntTxBuilder.cpp src/Ontology/Ont.cpp src/Ontology/SigData.cpp src/Ontology/Transaction.h src/Ontology/Entry.cpp src/Ontology/SigData.h src/Ontology/ParamsBuilder.h src/Ontology/Ong.h src/Ontology/Ont.h src/Ontology/Asset.h src/Ontology/Entry.h src/Ontology/Signer.h src/EOS/Transaction.cpp src/EOS/Asset.cpp src/EOS/Address.cpp src/EOS/Signer.cpp src/EOS/Action.cpp src/EOS/Name.cpp src/EOS/Serialization.h src/EOS/PackedTransaction.cpp src/EOS/Prefixes.h src/EOS/Entry.cpp src/EOS/Action.h src/EOS/Transaction.h src/EOS/Signer.h src/EOS/PackedTransaction.h src/EOS/Name.h src/EOS/Address.h src/EOS/Entry.h src/EOS/Asset.h src/TON/Cell.cpp src/TON/Address.cpp src/TON/Signer.cpp src/TON/Contract.cpp src/TON/Cell.h src/TON/Address.h src/TON/Entry.cpp src/TON/Entry.h src/NEO/Signer.cpp src/NEO/Transaction.cpp src/NEO/ReadData.cpp src/NEO/TransactionAttribute.h src/NEO/Address.cpp src/NEO/Serializable.h src/NEO/TransactionOutput.h src/NEO/CoinReference.h src/NEO/Witness.h src/NEO/Script.cpp src/NEO/MinerTransaction.h src/NEO/Entry.cpp src/NEO/ReadData.h src/NEO/Transaction.h src/NEO/Address.h src/NEO/ISerializable.h src/NEO/Entry.h src/NEO/Signer.h src/Tron/Signer.cpp src/Tron/Serialization.cpp src/Tron/Address.cpp src/Tron/Entry.cpp src/Tron/Entry.h src/Tron/Address.h src/Decred/Signer.cpp src/Decred/Transaction.cpp src/Decred/Address.cpp src/Decred/TransactionBuilder.h src/Decred/Transaction.h src/Decred/TransactionInput.cpp src/Decred/Entry.cpp src/Decred/Signer.h src/Decred/OutPoint.h src/Decred/TransactionOutput.cpp src/Decred/TransactionOutput.h src/Decred/OutPoint.cpp src/Decred/TransactionInput.h src/Decred/Entry.h src/Keystore/StoredKey.cpp src/Keystore/EncryptionParameters.cpp src/Keystore/ScryptParameters.cpp src/Keystore/Account.cpp src/Keystore/PBKDF2Parameters.cpp src/Keystore/AESParameters.cpp src/Keystore/Account.h src/Keystore/EncryptionParameters.h src/Keystore/StoredKey.h src/Keystore/ScryptParameters.h src/Keystore/AESParameters.h src/Keystore/PBKDF2Parameters.h src/Solana/Transaction.h src/Solana/Signer.cpp src/Solana/Transaction.cpp src/Solana/Address.cpp src/Solana/Entry.cpp src/Solana/Address.h src/Solana/Entry.h src/Binance/Signer.cpp src/Binance/Serialization.cpp src/Binance/Address.cpp src/Binance/Entry.cpp src/Binance/Address.h src/Binance/Entry.h src/Binance/Signer.h src/Cbor.cpp src/Cardano/AddressV3.cpp src/Cardano/AddressV2.cpp src/Cardano/Entry.cpp src/Cardano/AddressV3.h src/Cardano/Entry.h src/Cardano/AddressV2.h src/Filecoin/Address.cpp src/Filecoin/Transaction.cpp src/Filecoin/Signer.cpp src/Filecoin/Address.h src/Filecoin/Transaction.h src/Filecoin/Entry.cpp src/Filecoin/Entry.h src/Waves/Transaction.cpp src/Waves/Address.cpp src/Waves/Signer.cpp src/Waves/Entry.cpp src/Waves/BinaryCoding.h src/Waves/Transaction.h src/Waves/Entry.h src/Polkadot/Extrinsic.cpp src/Polkadot/ScaleCodec.h src/Polkadot/Extrinsic.h src/Polkadot/Signer.cpp src/Polkadot/Entry.cpp src/Polkadot/Address.h src/Polkadot/Entry.h src/Tezos/Forging.cpp src/Tezos/Signer.cpp src/Tezos/Address.cpp src/Tezos/OperationList.cpp src/Tezos/BinaryCoding.cpp src/Tezos/Entry.cpp src/Tezos/Entry.h src/Tezos/OperationList.h src/Tezos/Address.h src/HDWallet.cpp src/Zcash/Transaction.cpp src/Zcash/Signer.cpp src/Zcash/TransactionBuilder.h src/Zcash/Transaction.h src/Zcash/Entry.cpp src/Zcash/TAddress.h src/Zcash/Entry.h src/NULS/Signer.cpp src/NULS/BinaryCoding.h src/NULS/Address.cpp src/NULS/Entry.cpp src/NULS/Entry.h src/NULS/Signer.h src/Coin.cpp src/Ripple/Transaction.cpp src/Ripple/XAddress.cpp src/Ripple/Signer.cpp src/Ripple/BinaryCoding.h src/Ripple/Transaction.h src/Ripple/Address.cpp src/Ripple/Entry.cpp src/Ripple/Entry.h src/PrivateKey.cpp src/Algorand/BinaryCoding.h src/Algorand/Transaction.cpp src/Algorand/Address.cpp src/Algorand/Signer.cpp src/Algorand/Transaction.h src/Algorand/Entry.cpp src/Algorand/Entry.h src/Cosmos/Serialization.cpp src/Cosmos/Signer.cpp src/Cosmos/Entry.cpp src/Cosmos/Address.h src/Cosmos/Entry.h src/Nano/Signer.cpp src/Nano/Address.cpp src/Nano/Entry.cpp src/Nano/Entry.h src/Nano/Signer.h src/Nebulas/Transaction.cpp src/Nebulas/Signer.cpp src/Nebulas/Address.cpp src/Nebulas/Transaction.h src/Nebulas/Entry.cpp src/Nebulas/Entry.h src/Nebulas/Signer.h src/Nimiq/Address.cpp src/Nimiq/Transaction.cpp src/Nimiq/Signer.cpp src/Nimiq/Entry.cpp src/Nimiq/Signer.h src/Nimiq/Transaction.h src/Nimiq/Entry.h src/Nimiq/Address.h src/IoTeX/Staking.cpp src/IoTeX/Signer.cpp src/IoTeX/Address.h src/IoTeX/Entry.cpp src/IoTeX/Signer.h src/IoTeX/Entry.h src/IoTeX/Address.cpp src/Theta/Transaction.cpp src/Theta/Signer.cpp src/Theta/Entry.cpp src/Theta/Transaction.h src/Theta/Coins.h src/Theta/Signer.h src/Theta/Entry.h src/Stellar/Signer.cpp src/Stellar/Address.cpp src/Stellar/Entry.cpp src/Stellar/Entry.h src/Stellar/Signer.h src/Aeternity/Signer.cpp src/Aeternity/Transaction.cpp src/Aeternity/Address.cpp src/Aeternity/Transaction.h src/Aeternity/Entry.cpp src/Aeternity/Identifiers.h src/Aeternity/Entry.h src/Aeternity/Address.h src/NEAR/Serialization.cpp src/NEAR/Address.cpp src/NEAR/Signer.cpp src/NEAR/Entry.cpp src/NEAR/Account.cpp src/NEAR/Entry.h src/BinaryCoding.cpp src/PublicKey.cpp src/Icon/Signer.cpp src/Icon/Address.cpp src/Icon/Entry.cpp src/Icon/Entry.h src/Icon/Signer.h src/Zilliqa/Signer.cpp src/Zilliqa/AddressChecksum.cpp src/Zilliqa/Address.h src/Zilliqa/Entry.cpp src/Zilliqa/Entry.h src/Zilliqa/Address.cpp src/Aion/Signer.cpp src/Aion/Address.cpp src/Aion/Transaction.cpp src/Aion/Transaction.h src/Aion/RLP.h src/Aion/Entry.cpp src/Aion/Address.h src/Aion/Entry.h src/Base58.cpp src/XXHash64.h src/Bech32.cpp src/Hash.cpp src/Elrond/Serialization.cpp src/Elrond/Signer.cpp src/Elrond/Entry.cpp src/Elrond/Address.h src/Elrond/Address.cpp src/Elrond/Entry.h src/VeChain/Transaction.cpp src/VeChain/Signer.cpp src/VeChain/Entry.cpp src/VeChain/Clause.h src/VeChain/Transaction.h src/VeChain/Entry.h src/Groestlcoin/Address.cpp src/Groestlcoin/Signer.cpp src/Groestlcoin/Entry.cpp src/Groestlcoin/Transaction.h src/Groestlcoin/Address.h src/Groestlcoin/Entry.h src/DerivationPath.h src/Wanchain/Signer.cpp src/Wanchain/Entry.cpp src/Wanchain/Address.cpp src/Wanchain/Entry.h src/Wanchain/Signer.h src/Wanchain/Address.h src/Bech32Address.cpp src/Encrypt.cpp src/Hash.h src/uint256.h src/SS58Address.h src/Base58Address.h src/Base64.cpp src/BinaryCoding.h src/DerivationPath.cpp src/Result.h src/HexCoding.h src/Base32.h src/CoinEntry.h src/Crc.cpp src/Cbor.h src/Bech32.h src/Data.h src/Base58.h src/Kusama/Entry.cpp src/Kusama/Address.h src/Kusama/Entry.h src/Bech32Address.h src/PublicKey.h src/PrivateKey.h src/Data.cpp src/HDWallet.h src/Coin.h walletconsole/lib/CommandExecutor.cpp walletconsole/lib/Keys.cpp walletconsole/lib/Util.cpp walletconsole/lib/Address.cpp walletconsole/lib/Coins.cpp walletconsole/lib/Buffer.cpp walletconsole/lib/WalletConsole.cpp walletconsole/lib/Buffer.h walletconsole/lib/TonCoin.h walletconsole/lib/Coins.h walletconsole/lib/WalletConsole.h walletconsole/lib/Keys.h walletconsole/lib/Util.h walletconsole/lib/CommandExecutor.h walletconsole/lib/Address.h

No flags found

Use flags to group coverage reports by test type, project and/or folders.
Then setup custom commit statuses and notifications for each flag.

e.g., #unittest #integration

#production #enterprise

#frontend #backend

Learn more about Codecov Flags here.

Showing 30 of 62 files from the diff.

@@ -0,0 +1,66 @@
Loading
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 "ParamBase.h"
10 +
#include "ParamNumber.h"
11 +
12 +
#include <vector>
13 +
#include <string>
14 +
#include <memory>
15 +
16 +
namespace TW::Permission::ABI {
17 +
18 +
/// A set of parameters
19 +
class ParamSet {
20 +
private:
21 +
    std::vector<std::shared_ptr<ParamBase>> _params;
22 +
23 +
public:
24 +
    ParamSet() = default;
25 +
    ParamSet(const std::shared_ptr<ParamBase>& param1) { addParam(param1); }
26 +
    ParamSet(const std::vector<std::shared_ptr<ParamBase>>& params) { addParams(params); }
27 +
    ~ParamSet();
28 +
29 +
    /// Returns the index of the parameter
30 +
    int addParam(const std::shared_ptr<ParamBase>& param);
31 +
    void addParams(const std::vector<std::shared_ptr<ParamBase>>& params);
32 +
    bool getParam(int paramIndex, std::shared_ptr<ParamBase>& param_out) const;
33 +
    std::shared_ptr<ParamBase> getParamUnsafe(int paramIndex) const;
34 +
    size_t getCount() const { return _params.size(); }
35 +
    std::vector<std::shared_ptr<ParamBase>> const& getParams() const { return _params; }
36 +
    /// Return the function type signature, of the form "baz(int32,uint256)"
37 +
    std::string getType() const;
38 +
    size_t getSize() const;
39 +
    virtual void encode(Data& data) const;
40 +
    virtual bool decode(const Data& encoded, size_t& offset_inout);
41 +
42 +
private:
43 +
    size_t getHeadSize() const;
44 +
};
45 +
46 +
/// Collection of different parameters, dynamic length, "(<par1>,<par2>,...)".
47 +
class Parameters: public ParamCollection
48 +
{
49 +
private:
50 +
    ParamSet _params;
51 +
52 +
public:
53 +
    Parameters() = default;
54 +
    Parameters(const std::vector<std::shared_ptr<ParamBase>>& params) : ParamCollection(), _params(ParamSet(params)) {}
55 +
    void addParam(const std::shared_ptr<ParamBase>& param) { _params.addParam(param); }
56 +
    void addParams(const std::vector<std::shared_ptr<ParamBase>>& params) { _params.addParams(params); }
57 +
    std::shared_ptr<ParamBase> getParam(int paramIndex) const { return _params.getParamUnsafe(paramIndex); }
58 +
    virtual std::string getType() const { return _params.getType(); }
59 +
    virtual size_t getSize() const { return _params.getSize(); }
60 +
    virtual bool isDynamic() const { return true; }
61 +
    virtual size_t getCount() const { return _params.getCount(); }
62 +
    virtual void encode(Data& data) const { _params.encode(data); }
63 +
    virtual bool decode(const Data& encoded, size_t& offset_inout) { return _params.decode(encoded, offset_inout); }
64 +
};
65 +
66 +
} // namespace TW::Permission::ABI

@@ -0,0 +1,108 @@
Loading
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 +
#include "Signer.h"
8 +
#include "HexCoding.h"
9 +
#include <google/protobuf/util/json_util.h>
10 +
11 +
using namespace TW;
12 +
using namespace TW::Permission;
13 +
14 +
Proto::SigningOutput Signer::sign(const Proto::SigningInput& input) noexcept {
15 +
    auto signer = Signer(load(input.chain_id()));
16 +
    auto key = PrivateKey(Data(input.private_key().begin(), input.private_key().end()));
17 +
    auto transaction = Signer::build(input);
18 +
19 +
    signer.sign(key, transaction);
20 +
21 +
    auto output = Proto::SigningOutput();
22 +
23 +
    auto encoded = RLP::encode(transaction);
24 +
    output.set_encoded(encoded.data(), encoded.size());
25 +
26 +
    auto v = store(transaction.v);
27 +
    output.set_v(v.data(), v.size());
28 +
29 +
    auto r = store(transaction.r);
30 +
    output.set_r(r.data(), r.size());
31 +
32 +
    auto s = store(transaction.s);
33 +
    output.set_s(s.data(), s.size());
34 +
35 +
    return output;
36 +
}
37 +
38 +
std::string Signer::signJSON(const std::string& json, const Data& key) {
39 +
    auto input = Proto::SigningInput();
40 +
    google::protobuf::util::JsonStringToMessage(json, &input);
41 +
    input.set_private_key(key.data(), key.size());
42 +
    auto output = Signer::sign(input);
43 +
    return hex(output.encoded());
44 +
}
45 +
46 +
std::tuple<uint256_t, uint256_t, uint256_t> Signer::values(const uint256_t &chainID,
47 +
                                                           const Data& signature) noexcept {
48 +
    boost::multiprecision::uint256_t r, s, v;
49 +
    import_bits(r, signature.begin(), signature.begin() + 32);
50 +
    import_bits(s, signature.begin() + 32, signature.begin() + 64);
51 +
    import_bits(v, signature.begin() + 64, signature.begin() + 65);
52 +
    v += 27;
53 +
54 +
    boost::multiprecision::uint256_t newV;
55 +
    if (chainID != 0) {
56 +
        import_bits(newV, signature.begin() + 64, signature.begin() + 65);
57 +
        newV += 35 + chainID + chainID;
58 +
    } else {
59 +
        newV = v;
60 +
    }
61 +
    return std::make_tuple(r, s, newV);
62 +
}
63 +
64 +
std::tuple<uint256_t, uint256_t, uint256_t>
65 +
Signer::sign(const uint256_t &chainID, const PrivateKey &privateKey, const Data& hash) noexcept {
66 +
    auto signature = privateKey.sign(hash, TWCurveSECP256k1);
67 +
    return values(chainID, signature);
68 +
}
69 +
70 +
Transaction Signer::build(const Proto::SigningInput &input) {
71 +
    Data toAddress;
72 +
    if (!input.to_address().empty()) {
73 +
        toAddress.resize(20);
74 +
        auto address = Address(input.to_address());
75 +
        std::copy(address.bytes.begin(), address.bytes.end(), toAddress.data());
76 +
    }
77 +
    auto transaction = Transaction(
78 +
        /* nonce: */ load(input.nonce()),
79 +
        /* gasPrice: */ load(input.gas_price()),
80 +
        /* gasLimit: */ load(input.gas_limit()),
81 +
        /* to: */ toAddress,
82 +
        /* amount: */ load(input.amount()),
83 +
        /* payload: */ Data(input.payload().begin(), input.payload().end()));
84 +
    return transaction;
85 +
}
86 +
87 +
void Signer::sign(const PrivateKey &privateKey, Transaction &transaction) const noexcept {
88 +
    auto hash = this->hash(transaction);
89 +
    auto tuple = Signer::sign(chainID, privateKey, hash);
90 +
91 +
    transaction.r = std::get<0>(tuple);
92 +
    transaction.s = std::get<1>(tuple);
93 +
    transaction.v = std::get<2>(tuple);
94 +
}
95 +
96 +
Data Signer::hash(const Transaction &transaction) const noexcept {
97 +
    auto encoded = Data();
98 +
    append(encoded, RLP::encode(transaction.nonce));
99 +
    append(encoded, RLP::encode(transaction.gasPrice));
100 +
    append(encoded, RLP::encode(transaction.gasLimit));
101 +
    append(encoded, RLP::encode(transaction.to));
102 +
    append(encoded, RLP::encode(transaction.amount));
103 +
    append(encoded, RLP::encode(transaction.payload));
104 +
    append(encoded, RLP::encode(chainID));
105 +
    append(encoded, RLP::encode(0));
106 +
    append(encoded, RLP::encode(0));
107 +
    return Hash::keccak256(RLP::encodeList(encoded));
108 +
}

@@ -0,0 +1,145 @@
Loading
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 +
#include "Parameters.h"
8 +
#include "ValueEncoder.h"
9 +
10 +
#include <cassert>
11 +
#include <string>
12 +
13 +
using namespace TW::Permission::ABI;
14 +
15 +
ParamSet::~ParamSet() {
16 +
    _params.clear();
17 +
}
18 +
19 +
/// Returns the index of the parameter
20 +
int ParamSet::addParam(const std::shared_ptr<ParamBase>& param) {
21 +
    assert(param.get() != nullptr);
22 +
    if (param.get() == nullptr) {
23 +
        return -1;
24 +
    }
25 +
    _params.push_back(param);
26 +
    return static_cast<int>(_params.size() - 1);
27 +
}
28 +
29 +
void ParamSet::addParams(const std::vector<std::shared_ptr<ParamBase>>& params) {
30 +
    for (auto p : params) {
31 +
        addParam(p);
32 +
    }
33 +
}
34 +
35 +
bool ParamSet::getParam(int paramIndex, std::shared_ptr<ParamBase>& param_out) const {
36 +
    if (paramIndex >= _params.size() || paramIndex < 0) {
37 +
        return false;
38 +
    }
39 +
    param_out = _params[paramIndex];
40 +
    return true;
41 +
}
42 +
43 +
std::shared_ptr<ParamBase> ParamSet::getParamUnsafe(int paramIndex) const {
44 +
    if (_params.size() == 0) {
45 +
        // zero parameter, nothing to return.  This may cause trouble (segfault)
46 +
        return nullptr;
47 +
    }
48 +
    if (paramIndex >= _params.size() || paramIndex < 0) {
49 +
        // invalid index, return the first instead of nullptr
50 +
        return _params[0];
51 +
    }
52 +
    return _params[paramIndex];
53 +
}
54 +
55 +
/// Return the function type signature, of the form "baz(int32,uint256)"
56 +
std::string ParamSet::getType() const {
57 +
    std::string t = "(";
58 +
    int cnt = 0;
59 +
    for (auto p : _params) {
60 +
        if (cnt > 0) {
61 +
            t += ",";
62 +
        }
63 +
        t += p->getType();
64 +
        ++cnt;
65 +
    }
66 +
    t += ")";
67 +
    return t;
68 +
}
69 +
70 +
size_t ParamSet::getSize() const {
71 +
    // 2-pass encoding
72 +
    size_t s = 0;
73 +
    for (auto p: _params) {
74 +
        if (p->isDynamic() || p->getSize() > ValueEncoder::encodedIntSize) {
75 +
            // offset used
76 +
            s += 32;
77 +
        }
78 +
        s += p->getSize();
79 +
    }
80 +
    return ValueEncoder::paddedTo32(s);
81 +
}
82 +
83 +
size_t ParamSet::getHeadSize() const {
84 +
    size_t s = 0;
85 +
    for (auto p : _params) {
86 +
        if (p->isDynamic()) {
87 +
            s += 32;
88 +
        } else {
89 +
            s += p->getSize();
90 +
        }
91 +
    }
92 +
    return s;
93 +
}
94 +
95 +
void ParamSet::encode(Data& data) const {
96 +
    // 2-pass encoding
97 +
    size_t headSize = getHeadSize();
98 +
    size_t dynamicOffset = 0;
99 +
100 +
    // pass 1: small values or indices
101 +
    for (auto p : _params) {
102 +
        if (p->isDynamic() || p->getSize() > ValueEncoder::encodedIntSize) {
103 +
            // include only offset
104 +
            ValueEncoder::encodeUInt256(uint256_t(headSize + dynamicOffset), data);
105 +
            dynamicOffset += p->getSize();
106 +
        } else {
107 +
            // encode small data
108 +
            p->encode(data);
109 +
        }
110 +
    }
111 +
112 +
    // pass 2: dynamic values
113 +
    for (auto p : _params) {
114 +
        if (p->isDynamic() || p->getSize() > ValueEncoder::encodedIntSize) {
115 +
            // encode large data
116 +
            p->encode(data);
117 +
        }
118 +
    }
119 +
}
120 +
121 +
bool ParamSet::decode(const Data& encoded, size_t& offset_inout) {
122 +
    // pass 1: small values
123 +
    for (auto p : _params) {
124 +
        if (p->isDynamic()) {
125 +
            uint256_t index;
126 +
            if (!ABI::decode(encoded, index, offset_inout)) {
127 +
                return false;
128 +
            }
129 +
            // index is read but not used
130 +
        } else {
131 +
            if (!p->decode(encoded, offset_inout)) {
132 +
                return false;
133 +
            }
134 +
        }
135 +
    }
136 +
    // pass2: large values
137 +
    for (auto p : _params) {
138 +
        if (p->isDynamic()) {
139 +
            if (!p->decode(encoded, offset_inout)) {
140 +
                return false;
141 +
            }
142 +
        }
143 +
    }
144 +
    return true;
145 +
}

@@ -0,0 +1,85 @@
Loading
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 +
#include "Bytes.h"
8 +
#include "ParamNumber.h"
9 +
#include "ValueEncoder.h"
10 +
11 +
namespace TW::Permission::ABI {
12 +
13 +
void ParamByteArray::encodeBytes(const Data& bytes, Data& data) {
14 +
    ValueEncoder::encodeUInt256(uint256_t(bytes.size()), data);
15 +
16 +
    const auto count = bytes.size();
17 +
    const auto padding = ValueEncoder::padNeeded32(count);
18 +
    data.insert(data.end(), bytes.begin(), bytes.begin() + count);
19 +
    append(data, Data(padding));
20 +
}
21 +
22 +
bool ParamByteArray::decodeBytes(const Data& encoded, Data& decoded, size_t& offset_inout) {
23 +
    size_t origOffset = offset_inout;
24 +
    // read len
25 +
    uint256_t len256;
26 +
    if (!ABI::decode(encoded, len256, offset_inout)) {
27 +
        return false;
28 +
    }
29 +
    // check if length is in the size_t range
30 +
    size_t len = static_cast<size_t>(len256);
31 +
    if (len256 != uint256_t(len)) {
32 +
        return false;
33 +
    }
34 +
    // check if there is enough data
35 +
    if (encoded.size() < offset_inout + len) {
36 +
        return false;
37 +
    }
38 +
    // read data
39 +
    decoded = Data(encoded.begin() + offset_inout, encoded.begin() + offset_inout + len);
40 +
    offset_inout += len;
41 +
    // padding
42 +
    offset_inout = origOffset + ValueEncoder::paddedTo32(offset_inout - origOffset);
43 +
    return true;
44 +
}
45 +
46 +
void ParamByteArrayFix::encode(Data& data) const {
47 +
    const auto count = _bytes.size();
48 +
    const auto padding = ValueEncoder::padNeeded32(count);
49 +
    data.insert(data.end(), _bytes.begin(), _bytes.begin() + count);
50 +
    append(data, Data(padding));
51 +
}
52 +
53 +
bool ParamByteArrayFix::decodeBytesFix(const Data& encoded, size_t n, Data& decoded,
54 +
                                       size_t& offset_inout) {
55 +
    size_t origOffset = offset_inout;
56 +
    if (encoded.size() < offset_inout + n) {
57 +
        // not enough data
58 +
        return false;
59 +
    }
60 +
    if (decoded.size() < n) {
61 +
        append(decoded, Data(n - decoded.size()));
62 +
    }
63 +
    std::copy(encoded.begin() + offset_inout, encoded.begin() + (offset_inout + n),
64 +
              decoded.begin());
65 +
    offset_inout += n;
66 +
    // padding
67 +
    offset_inout = origOffset + ValueEncoder::paddedTo32(offset_inout - origOffset);
68 +
    return true;
69 +
}
70 +
71 +
void ParamString::encodeString(const std::string& decoded, Data& data) {
72 +
    auto bytes = Data(decoded.begin(), decoded.end());
73 +
    ParamByteArray::encodeBytes(bytes, data);
74 +
}
75 +
76 +
bool ParamString::decodeString(const Data& encoded, std::string& decoded, size_t& offset_inout) {
77 +
    Data decodedData;
78 +
    if (!ParamByteArray::decodeBytes(encoded, decodedData, offset_inout)) {
79 +
        return false;
80 +
    }
81 +
    decoded = std::string(decodedData.begin(), decodedData.end());
82 +
    return true;
83 +
}
84 +
85 +
} // namespace TW::Permission::ABI

@@ -0,0 +1,81 @@
Loading
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 "ParamBase.h"
10 +
#include "ValueEncoder.h"
11 +
#include "../../Data.h"
12 +
13 +
namespace TW::Permission::ABI {
14 +
15 +
/// Dynamic array of bytes "bytes"
16 +
class ParamByteArray: public ParamCollection
17 +
{
18 +
private:
19 +
    Data _bytes;
20 +
public:
21 +
    ParamByteArray() = default;
22 +
    ParamByteArray(const Data& val) : ParamCollection() { setVal(val); }
23 +
    void setVal(const Data& val) { _bytes = val; }
24 +
    const Data& getVal() const { return _bytes; }
25 +
    virtual std::string getType() const { return "bytes"; };
26 +
    virtual size_t getSize() const { return 32 + ValueEncoder::paddedTo32(_bytes.size()); }
27 +
    virtual bool isDynamic() const { return true; }
28 +
    virtual size_t getCount() const { return _bytes.size(); }
29 +
    static void encodeBytes(const Data& bytes, Data& data);
30 +
    virtual void encode(Data& data) const { encodeBytes(_bytes, data); }
31 +
    static bool decodeBytes(const Data& encoded, Data& decoded, size_t& offset_inout);
32 +
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
33 +
        return decodeBytes(encoded, _bytes, offset_inout);
34 +
    }
35 +
};
36 +
37 +
/// Fixed-size array of bytes, "bytes<N>"
38 +
class ParamByteArrayFix: public ParamCollection
39 +
{
40 +
private:
41 +
    size_t _n;
42 +
    Data _bytes;
43 +
public:
44 +
    ParamByteArrayFix(size_t n): ParamCollection(), _n(n), _bytes(Data(_n)) {}
45 +
    ParamByteArrayFix(size_t n, const Data& val): ParamCollection(), _n(n), _bytes(Data(_n)) { setVal(val); }
46 +
    void setVal(const Data& val) { _bytes = val; }
47 +
    const std::vector<uint8_t>& getVal() const { return _bytes; }
48 +
    virtual std::string getType() const { return "bytes" + std::to_string(_n); };
49 +
    virtual size_t getSize() const { return ValueEncoder::paddedTo32(_bytes.size()); }
50 +
    virtual bool isDynamic() const { return false; }
51 +
    virtual size_t getCount() const { return _bytes.size(); }
52 +
    virtual void encode(Data& data) const;
53 +
    static bool decodeBytesFix(const Data& encoded, size_t n, Data& decoded, size_t& offset_inout);
54 +
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
55 +
        return decodeBytesFix(encoded, _n, _bytes, offset_inout);
56 +
    }
57 +
};
58 +
59 +
/// Var-length string parameter
60 +
class ParamString: public ParamCollection
61 +
{
62 +
private:
63 +
    std::string _str;
64 +
public:
65 +
    ParamString() = default;
66 +
    ParamString(std::string val): ParamCollection() { setVal(val); }
67 +
    void setVal(const std::string& val) { _str = val; }
68 +
    const std::string& getVal() const { return _str; }
69 +
    virtual std::string getType() const { return "string"; };
70 +
    virtual size_t getSize() const { return 32 + ValueEncoder::paddedTo32(_str.size()); }
71 +
    virtual bool isDynamic() const { return true; }
72 +
    virtual size_t getCount() const { return _str.size(); }
73 +
    static void encodeString(const std::string& decoded, Data& data);
74 +
    virtual void encode(Data& data) const { ParamString::encodeString(_str, data); }
75 +
    static bool decodeString(const Data& encoded, std::string& decoded, size_t& offset_inout);
76 +
    virtual bool decode(const Data& encoded, size_t& offset_inout) {
77 +
        return decodeString(encoded, _str, offset_inout);
78 +
    }
79 +
};
80 +
81 +
} // namespace TW::Permission::ABI

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Click to load this diff.
Loading diff...

Learn more Showing 26 files with coverage changes found.

New file src/Permission/ABI/Parameters.h
New
Loading file...
New file src/Permission/Signer.cpp
New
Loading file...
New file src/Permission/ABI/Parameters.cpp
New
Loading file...
New file src/Permission/ABI/Bytes.cpp
New
Loading file...
New file src/Permission/Entry.cpp
New
Loading file...
New file src/Permission/ABI/Bytes.h
New
Loading file...
New file src/Permission/ABI/Array.h
New
Loading file...
New file src/Permission/ABI/ParamFactory.cpp
New
Loading file...
New file src/Permission/ABI/ValueEncoder.cpp
New
Loading file...
New file src/Permission/AddressChecksum.cpp
New
Loading file...
New file src/Permission/Transaction.h
New
Loading file...
New file src/Permission/Address.cpp
New
Loading file...
New file src/Permission/ABI/ValueDecoder.cpp
New
Loading file...
New file src/Permission/ABI/Function.h
New
Loading file...
New file src/Permission/RLP.h
New
Loading file...
New file src/Permission/ABI/ParamAddress.h
New
Loading file...
New file src/Permission/ABI/Array.cpp
New
Loading file...
New file src/Permission/Entry.h
New
Loading file...
New file src/Permission/ABI/ValueEncoder.h
New
Loading file...
New file src/Permission/ABI/Function.cpp
New
Loading file...
New file src/Permission/ABI/ParamNumber.h
New
Loading file...
New file src/Permission/Signer.h
New
Loading file...
New file src/Permission/ABI/ParamBase.h
New
Loading file...
New file src/Permission/RLP.cpp
New
Loading file...
New file src/Permission/ABI/ParamNumber.cpp
New
Loading file...
New file src/Permission/Address.h
New
Loading file...
Files Coverage
src -0.56% 94.70%
walletconsole/lib 95.09%
Project Totals (456 files) 94.71%
Loading