forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathScript.h
More file actions
121 lines (89 loc) · 3.88 KB
/
Copy pathScript.h
File metadata and controls
121 lines (89 loc) · 3.88 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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// 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 "../Data.h"
#include <TrustWalletCore/TWBitcoinOpCodes.h>
#include <TrustWalletCore/TWCoinType.h>
#include <string>
#include <vector>
namespace TW::Bitcoin {
class Script {
public:
/// Script raw bytes.
Data bytes;
/// Initializes an empty script.
Script() = default;
/// Initializes a script with a collection of raw bytes.
template <typename It>
Script(It begin, It end) : bytes(begin, end) {}
/// Initializaes a script with a collection of raw bytes by moving.
explicit Script(Data&& bytes) : bytes(bytes) {}
/// Whether the script is empty.
bool empty() const { return bytes.empty(); }
/// Returns the script's script hash.
Data hash() const;
/// Determines whether this is a pay-to-script-hash (P2SH) script.
bool isPayToScriptHash() const;
/// Determines whether this is a pay-to-witness-script-hash (P2WSH) script.
bool isPayToWitnessScriptHash() const;
/// Determines whether this is a witness programm script.
bool isWitnessProgram() const;
/// Matches the script to a pay-to-public-key (P2PK) script.
bool matchPayToPubkey(Data& publicKey) const;
/// Matches the script to a pay-to-public-key-hash (P2PKH).
bool matchPayToPubkeyHash(Data& keyHash) const;
/// Matches the script to a pay-to-script-hash (P2SH).
bool matchPayToScriptHash(Data& scriptHash) const;
/// Matches the script to a pay-to-witness-public-key-hash (P2WPKH).
bool matchPayToWitnessPublicKeyHash(Data& keyHash) const;
/// Matches the script to a pay-to-witness-script-hash (P2WSH).
bool matchPayToWitnessScriptHash(Data& scriptHash) const;
/// Matches the script to a multisig script.
bool matchMultisig(std::vector<Data>& publicKeys, int& required) const;
/// Builds a pay-to-public-key-hash (P2PKH) script from a public key hash.
static Script buildPayToPublicKeyHash(const Data& hash);
/// Builds a pay-to-script-hash (P2SH) script from a script hash.
static Script buildPayToScriptHash(const Data& scriptHash);
/// Builds a pay-to-witness-program script, P2WSH or P2WPKH.
static Script buildPayToWitnessProgram(const Data& program);
/// Builds a pay-to-witness-public-key-hash (P2WPKH) script from a public
/// key hash.
static Script buildPayToWitnessPubkeyHash(const Data& hash);
/// Builds a pay-to-witness-script-hash (P2WSH) script from a script hash.
static Script buildPayToWitnessScriptHash(const Data& scriptHash);
/// Builds a pay-to-public-key-hash (P2PKH) script appropriate for the given
/// address.
static Script buildForAddress(const std::string& address, enum TWCoinType coin);
/// Encodes the script.
void encode(Data& data) const;
/// Encodes a small integer
static uint8_t encodeNumber(int n) {
assert(n >= 0 && n <= 16);
if (n == 0) {
return OP_0;
}
return OP_1 + uint8_t(n - 1);
}
private:
/// Extracts a single opcode at the given index including its operand.
///
/// \param index [in/out] index where the operation starts, on return the
/// index of the next operation. \param opcode [out] the opcode. \param
/// operand [out] the opcode's operand. \returns whether an opcode was
/// available.
bool getScriptOp(size_t& index, uint8_t& opcode, Data& operand) const;
};
inline bool operator==(const Script& lhs, const Script& rhs) {
return lhs.bytes == rhs.bytes;
}
inline bool operator!=(const Script& lhs, const Script& rhs) {
return !(lhs == rhs);
}
} // namespace TW::Bitcoin
/// Wrapper for C interface.
struct TWBitcoinScript {
TW::Bitcoin::Script impl;
};