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 "../Bitcoin/TransactionPlan.h"
11
#include "../Bitcoin/TransactionBuilder.h"
12
#include "../proto/Bitcoin.pb.h"
13
#include "../proto/Decred.pb.h"
14

15
#include <TrustWalletCore/TWCoinType.h>
16

17
#include <algorithm>
18

19
namespace TW::Decred {
20

21
struct TransactionBuilder {
22
    /// Plans a transaction by selecting UTXOs and calculating fees.
23 1
    static Bitcoin::TransactionPlan plan(const Bitcoin::Proto::SigningInput& input) {
24 1
        return Bitcoin::TransactionBuilder::plan(input);
25
    }
26

27
    /// Builds a transaction by selecting UTXOs and calculating fees.
28 1
    static Transaction build(const Bitcoin::TransactionPlan& plan, const std::string& toAddress,
29
                             const std::string& changeAddress) {
30 1
        auto coin = TWCoinTypeDecred;                                 
31 1
        auto lockingScriptTo = Bitcoin::Script::lockScriptForAddress(toAddress, coin);
32 1
        if (lockingScriptTo.empty()) {
33 1
            return {};
34
        }
35

36 1
        Transaction tx;
37 1
        tx.outputs.emplace_back(TransactionOutput(plan.amount, /* version: */ 0, lockingScriptTo));
38

39 1
        if (plan.change > 0) {
40 1
            auto lockingScriptChange = Bitcoin::Script::lockScriptForAddress(changeAddress, coin);
41 1
            tx.outputs.emplace_back(
42 1
                TransactionOutput(plan.change, /* version: */ 0, lockingScriptChange));
43
        }
44

45 1
        const auto emptyScript = Bitcoin::Script();
46 1
        for (auto& utxo : plan.utxos) {
47 1
            auto input = TransactionInput();
48 1
            input.previousOutput = utxo.out_point();
49 1
            input.sequence = utxo.out_point().sequence();
50 1
            tx.inputs.push_back(std::move(input));
51
        }
52

53 1
        return tx;
54
    }
55
};
56

57
} // namespace TW::Decred

Read our documentation on viewing source code .

Loading