forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSigner.h
More file actions
89 lines (71 loc) · 2.65 KB
/
Copy pathSigner.h
File metadata and controls
89 lines (71 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
// Copyright © 2017-2020 Trust Wallet.
//
// This file is part of Trust. The full Trust copyright notice, including
// terms governing use, modification, and redistribution, is contained in the
// file LICENSE at the root of the source code distribution tree.
#pragma once
#include "Transaction.h"
#include "TransactionBuilder.h"
#include "TransactionInput.h"
#include "../Bitcoin/Amount.h"
#include "../Bitcoin/Script.h"
#include "../Bitcoin/TransactionPlan.h"
#include "../Hash.h"
#include "../PrivateKey.h"
#include "../Result.h"
#include "../proto/Bitcoin.pb.h"
#include "../proto/Decred.pb.h"
#include <memory>
#include <string>
#include <vector>
namespace TW::Decred {
/// Helper class that performs Decred transaction signing.
class Signer {
private:
/// Private key and redeem script provider for signing.
Bitcoin::Proto::SigningInput input;
public:
/// Transaction plan.
Bitcoin::TransactionPlan plan;
/// Transaction being signed.
Transaction transaction;
private:
/// List of signed inputs.
std::vector<TransactionInput> signedInputs;
public:
/// Initializes a transaction signer.
Signer() = default;
/// Initializes a transaction signer with signing input.
explicit Signer(Bitcoin::Proto::SigningInput&& input)
: input(input), plan(TransactionBuilder::plan(input)) {
transaction = TransactionBuilder::build(plan, input.to_address(), input.change_address());
}
/// Initializes a transaction signer with signing input, a transaction, and
/// a hash type.
Signer(Bitcoin::Proto::SigningInput&& input, const Bitcoin::TransactionPlan& plan)
: input(input), plan(plan) {
transaction = TransactionBuilder::build(plan, input.to_address(), input.change_address());
}
/// Signs the transaction.
///
/// \returns the signed transaction or an error.
Result<Transaction> sign();
/// Signs a particular input.
///
/// \returns the signed transaction script.
Result<Bitcoin::Script> sign(Bitcoin::Script script, size_t index);
private:
Result<std::vector<Data>> signStep(Bitcoin::Script script, size_t index);
Data createSignature(const Transaction& transaction, const Bitcoin::Script& script,
const Data& key, size_t index);
Data pushAll(const std::vector<Data>& results);
/// Returns the private key for the given public key hash.
Data keyForPublicKeyHash(const Data& hash) const;
/// Returns the redeem script for the given script hash.
Data scriptForScriptHash(const Data& hash) const;
};
} // namespace TW::Decred
/// Wrapper for C interface.
struct TWDecredSigner {
TW::Decred::Signer impl;
};