This repository has been archived by the owner on Sep 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
WSScript.h
164 lines (132 loc) · 5.62 KB
/
WSScript.h
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
//
// WSScript.h
// BitcoinSPV
//
// Created by Davide De Rosa on 16/06/14.
// Copyright (c) 2014 Davide De Rosa. All rights reserved.
//
// https://github.com/keeshux
//
// This file is part of BitcoinSPV.
//
// BitcoinSPV is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// BitcoinSPV is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with BitcoinSPV. If not, see <http://www.gnu.org/licenses/>.
//
#import <Foundation/Foundation.h>
#import "WSBuffer.h"
#import "WSSized.h"
//
// Script
//
// https://en.bitcoin.it/wiki/Script
//
@class WSScriptChunk;
@class WSPublicKey;
@class WSAddress;
#pragma mark -
typedef enum {
WSScriptOpcode_OP_0 = 0x00,
WSScriptOpcode_PUSHDATA1 = 0x4c,
WSScriptOpcode_PUSHDATA2 = 0x4d,
WSScriptOpcode_PUSHDATA4 = 0x4e,
WSScriptOpcode_OP_1 = 0x51,
WSScriptOpcode_OP_2 = 0x52,
WSScriptOpcode_OP_3 = 0x53,
WSScriptOpcode_OP_4 = 0x54,
WSScriptOpcode_OP_5 = 0x55,
WSScriptOpcode_OP_6 = 0x56,
WSScriptOpcode_OP_7 = 0x57,
WSScriptOpcode_OP_8 = 0x58,
WSScriptOpcode_OP_9 = 0x59,
WSScriptOpcode_OP_10 = 0x5a,
WSScriptOpcode_OP_11 = 0x5b,
WSScriptOpcode_OP_12 = 0x5c,
WSScriptOpcode_OP_13 = 0x5d,
WSScriptOpcode_OP_14 = 0x5e,
WSScriptOpcode_OP_15 = 0x5f,
WSScriptOpcode_OP_16 = 0x60,
WSScriptOpcode_OP_RETURN = 0x6a,
WSScriptOpcode_DUP = 0x76,
WSScriptOpcode_EQUAL = 0x87,
WSScriptOpcode_EQUALVERIFY = 0x88,
WSScriptOpcode_HASH160 = 0xa9,
WSScriptOpcode_CHECKSIG = 0xac,
WSScriptOpcode_CHECKSIGVERIFY = 0xad,
WSScriptOpcode_CHECKMULTISIG = 0xae,
WSScriptOpcode_CHECKMULTISIGVERIFY = 0xaf
} WSScriptOpcode;
NSString *WSScriptOpcodeString(WSScriptOpcode opcode);
// for OP_1-16 opcodes
WSScriptOpcode WSScriptOpcodeFromValue(NSInteger value);
NSInteger WSScriptOpcodeToValue(WSScriptOpcode opcode);
#pragma mark -
//
// multisig scripts (M-of-N) are described in BIP11
//
// https://github.com/bitcoin/bips/blob/master/bip-0011.mediawiki
//
@interface WSScript : NSObject <NSCopying, WSBufferEncoder, WSBufferDecoder, WSSized>
+ (instancetype)scriptWithAddress:(WSAddress *)address;
+ (instancetype)scriptWithSignature:(NSData *)signature publicKey:(WSPublicKey *)publicKey;
+ (instancetype)scriptWithSignatures:(NSArray *)signatures publicKeys:(NSArray *)publicKeys;
+ (instancetype)redeemScriptWithNumberOfSignatures:(NSUInteger)numberOfSignatures publicKeys:(NSArray *)publicKeys;
- (instancetype)initWithChunks:(NSArray *)chunks; // WSScriptChunk
- (NSArray *)chunks;
- (BOOL)isPushDataOnly;
- (BOOL)containsData:(NSData *)data;
- (BOOL)isScriptSigWithSignature:(NSData **)signature publicKey:(WSPublicKey **)publicKey;
- (BOOL)isScriptSigWithSignatures:(NSArray **)signatures publicKeys:(NSArray **)publicKeys redeemScript:(WSScript **)redeemScript;
- (BOOL)isScriptSigWithReedemNumberOfSignatures:(NSUInteger *)numberOfSignatures publicKeys:(NSArray **)publicKeys;
- (BOOL)isPay2PubKeyHash;
- (BOOL)isPay2PubKey;
- (BOOL)isPay2ScriptHash;
- (WSAddress *)standardInputAddressWithParameters:(WSParameters *)parameters; // nil if non-standard script
- (WSAddress *)standardOutputAddressWithParameters:(WSParameters *)parameters; // nil if non-standard script
- (WSAddress *)standardAddressWithParameters:(WSParameters *)parameters; // any of the above
- (WSAddress *)addressFromHashWithParameters:(WSParameters *)parameters;
- (NSData *)originalData; // nil if from chunks, non-nil if from buffer
@end
@interface WSCoinbaseScript : WSScript
+ (instancetype)scriptWithCoinbaseData:(NSData *)coinbaseData;
- (uint32_t)blockHeight;
@end
#pragma mark -
@interface WSScriptBuilder : NSObject
- (instancetype)init;
- (instancetype)initWithAddress:(WSAddress *)address; // P2PKH or P2SH
- (instancetype)initWithSignature:(NSData *)signature publicKey:(WSPublicKey *)publicKey;
- (instancetype)initWithSignatures:(NSArray *)signatures publicKeys:(NSArray *)publicKeys;
- (instancetype)initWithRedeemNumberOfSignatures:(NSUInteger)numberOfSignatures publicKeys:(NSArray *)publicKeys;
- (void)appendChunk:(WSScriptChunk *)chunk;
- (void)appendOpcode:(WSScriptOpcode)opcode;
- (void)appendPushData:(NSData *)pushData;
- (void)appendScriptForAddress:(WSAddress *)address;
- (void)appendScript:(WSScript *)script;
- (void)removeChunksWithOpcode:(WSScriptOpcode)opcode;
- (WSScript *)build;
- (WSScript *)buildWithCopy;
@end
#pragma mark -
@interface WSScriptChunk : NSObject <NSCopying, WSBufferEncoder, WSSized>
+ (instancetype)chunkWithOpcode:(WSScriptOpcode)opcode;
+ (instancetype)chunkWithOpcode:(WSScriptOpcode)opcode pushData:(NSData *)pushData;
+ (instancetype)chunkWithPushData:(NSData *)pushData;
- (BOOL)isOpcode;
- (BOOL)isPushData;
- (BOOL)isSignature;
- (WSScriptOpcode)opcode;
- (NSString *)opcodeString;
- (NSUInteger)opcodeValue; // for OP_[1-16]
- (NSData *)pushData;
- (NSUInteger)pushDataLength;
@end