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 "BinaryCoding.h"
8
#include "Forging.h"
9
#include "HexCoding.h"
10
#include "OperationList.h"
11
#include "../Base58.h"
12
#include "../proto/Tezos.pb.h"
13

14
using namespace TW;
15
using namespace TW::Tezos;
16
using namespace TW::Tezos::Proto;
17

18 1
Tezos::OperationList::OperationList(const std::string& str) {
19 1
    branch = str;
20
}
21

22 1
void Tezos::OperationList::addOperation(const Operation& operation) {
23 1
    operation_list.push_back(operation);
24
}
25

26
// Forge the given branch to a hex encoded string.
27 1
Data Tezos::OperationList::forgeBranch() const {
28 1
    std::array<byte, 2> prefix = {1, 52};
29 1
    const auto decoded = Base58::bitcoin.decodeCheck(branch);
30 1
    if (decoded.size() != 34 || !std::equal(prefix.begin(), prefix.end(), decoded.begin())) {
31 0
        throw std::invalid_argument("Invalid branch for forge");
32
    }
33 1
    auto forged = Data();
34 1
    forged.insert(forged.end(), decoded.begin() + prefix.size(), decoded.end());
35 1
    return forged;
36
}
37

38 1
Data Tezos::OperationList::forge(const PrivateKey& privateKey) const {
39 1
    auto forged = forgeBranch();
40

41 1
    for (auto operation : operation_list) {
42
        // If it's REVEAL operation, inject the public key if not specified
43 1
        if (operation.kind() == Operation::REVEAL && operation.has_reveal_operation_data()) {
44 1
            auto revealOperationData = operation.mutable_reveal_operation_data();
45 1
            if (revealOperationData->public_key().empty()) {
46 1
                auto publicKey = privateKey.getPublicKey(TWPublicKeyTypeED25519);
47 1
                revealOperationData->set_public_key(publicKey.bytes.data(), publicKey.bytes.size());
48
            }
49
        }
50

51 1
        append(forged, forgeOperation(operation));
52
    }
53

54 1
    return forged;
55
}

Read our documentation on viewing source code .

Loading