btcruby 0.0.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (117) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +18 -0
  3. data/.travis.yml +7 -0
  4. data/FAQ.md +7 -0
  5. data/Gemfile +3 -0
  6. data/Gemfile.lock +18 -0
  7. data/HOWTO.md +17 -0
  8. data/LICENSE +19 -0
  9. data/README.md +59 -0
  10. data/Rakefile +6 -0
  11. data/TODO.txt +40 -0
  12. data/bin/console +19 -0
  13. data/btcruby.gemspec +20 -0
  14. data/documentation/address.md +73 -0
  15. data/documentation/base58.md +52 -0
  16. data/documentation/block.md +127 -0
  17. data/documentation/block_header.md +120 -0
  18. data/documentation/constants.md +88 -0
  19. data/documentation/data.md +54 -0
  20. data/documentation/diagnostics.md +90 -0
  21. data/documentation/extensions.md +76 -0
  22. data/documentation/hash_functions.md +58 -0
  23. data/documentation/hash_id.md +22 -0
  24. data/documentation/index.md +230 -0
  25. data/documentation/key.md +177 -0
  26. data/documentation/keychain.md +180 -0
  27. data/documentation/network.md +75 -0
  28. data/documentation/opcode.md +220 -0
  29. data/documentation/openssl.md +7 -0
  30. data/documentation/p2pkh.md +71 -0
  31. data/documentation/p2sh.md +64 -0
  32. data/documentation/proof_of_work.md +84 -0
  33. data/documentation/script.md +280 -0
  34. data/documentation/signature.md +71 -0
  35. data/documentation/transaction.md +213 -0
  36. data/documentation/transaction_builder.md +188 -0
  37. data/documentation/transaction_input.md +133 -0
  38. data/documentation/transaction_output.md +130 -0
  39. data/documentation/wif.md +72 -0
  40. data/documentation/wire_format.md +70 -0
  41. data/lib/btcruby/address.rb +296 -0
  42. data/lib/btcruby/base58.rb +108 -0
  43. data/lib/btcruby/big_number.rb +47 -0
  44. data/lib/btcruby/block.rb +170 -0
  45. data/lib/btcruby/block_header.rb +231 -0
  46. data/lib/btcruby/constants.rb +59 -0
  47. data/lib/btcruby/currency_formatter.rb +64 -0
  48. data/lib/btcruby/data.rb +98 -0
  49. data/lib/btcruby/diagnostics.rb +92 -0
  50. data/lib/btcruby/errors.rb +8 -0
  51. data/lib/btcruby/extensions.rb +65 -0
  52. data/lib/btcruby/hash_functions.rb +54 -0
  53. data/lib/btcruby/hash_id.rb +18 -0
  54. data/lib/btcruby/key.rb +517 -0
  55. data/lib/btcruby/keychain.rb +464 -0
  56. data/lib/btcruby/network.rb +73 -0
  57. data/lib/btcruby/opcode.rb +197 -0
  58. data/lib/btcruby/open_assets/asset.rb +35 -0
  59. data/lib/btcruby/open_assets/asset_address.rb +49 -0
  60. data/lib/btcruby/open_assets/asset_definition.rb +75 -0
  61. data/lib/btcruby/open_assets/asset_id.rb +24 -0
  62. data/lib/btcruby/open_assets/asset_marker.rb +94 -0
  63. data/lib/btcruby/open_assets/asset_processor.rb +377 -0
  64. data/lib/btcruby/open_assets/asset_transaction.rb +184 -0
  65. data/lib/btcruby/open_assets/asset_transaction_builder/errors.rb +15 -0
  66. data/lib/btcruby/open_assets/asset_transaction_builder/provider.rb +32 -0
  67. data/lib/btcruby/open_assets/asset_transaction_builder/result.rb +47 -0
  68. data/lib/btcruby/open_assets/asset_transaction_builder.rb +418 -0
  69. data/lib/btcruby/open_assets/asset_transaction_input.rb +64 -0
  70. data/lib/btcruby/open_assets/asset_transaction_output.rb +140 -0
  71. data/lib/btcruby/open_assets.rb +26 -0
  72. data/lib/btcruby/openssl.rb +536 -0
  73. data/lib/btcruby/proof_of_work.rb +110 -0
  74. data/lib/btcruby/safety.rb +26 -0
  75. data/lib/btcruby/script.rb +733 -0
  76. data/lib/btcruby/signature_hashtype.rb +37 -0
  77. data/lib/btcruby/transaction.rb +511 -0
  78. data/lib/btcruby/transaction_builder/errors.rb +15 -0
  79. data/lib/btcruby/transaction_builder/provider.rb +54 -0
  80. data/lib/btcruby/transaction_builder/result.rb +73 -0
  81. data/lib/btcruby/transaction_builder/signer.rb +28 -0
  82. data/lib/btcruby/transaction_builder.rb +520 -0
  83. data/lib/btcruby/transaction_input.rb +298 -0
  84. data/lib/btcruby/transaction_outpoint.rb +30 -0
  85. data/lib/btcruby/transaction_output.rb +315 -0
  86. data/lib/btcruby/version.rb +3 -0
  87. data/lib/btcruby/wif.rb +118 -0
  88. data/lib/btcruby/wire_format.rb +362 -0
  89. data/lib/btcruby.rb +44 -2
  90. data/sample_code/creating_a_p2sh_multisig_address.rb +21 -0
  91. data/sample_code/creating_a_transaction_manually.rb +44 -0
  92. data/sample_code/generating_an_address.rb +20 -0
  93. data/sample_code/using_transaction_builder.rb +49 -0
  94. data/spec/address_spec.rb +206 -0
  95. data/spec/all.rb +6 -0
  96. data/spec/base58_spec.rb +83 -0
  97. data/spec/block_header_spec.rb +18 -0
  98. data/spec/block_spec.rb +18 -0
  99. data/spec/currency_formatter_spec.rb +46 -0
  100. data/spec/data_spec.rb +50 -0
  101. data/spec/diagnostics_spec.rb +41 -0
  102. data/spec/key_spec.rb +205 -0
  103. data/spec/keychain_spec.rb +261 -0
  104. data/spec/network_spec.rb +48 -0
  105. data/spec/open_assets/asset_address_spec.rb +33 -0
  106. data/spec/open_assets/asset_id_spec.rb +15 -0
  107. data/spec/open_assets/asset_marker_spec.rb +47 -0
  108. data/spec/open_assets/asset_processor_spec.rb +567 -0
  109. data/spec/open_assets/asset_transaction_builder_spec.rb +273 -0
  110. data/spec/open_assets/asset_transaction_spec.rb +70 -0
  111. data/spec/proof_of_work_spec.rb +53 -0
  112. data/spec/script_spec.rb +66 -0
  113. data/spec/spec_helper.rb +8 -0
  114. data/spec/transaction_builder_spec.rb +338 -0
  115. data/spec/transaction_spec.rb +162 -0
  116. data/spec/wire_format_spec.rb +283 -0
  117. metadata +141 -7
@@ -0,0 +1,71 @@
1
+ [Index](index.md)
2
+
3
+ BTC::PublicKeyAddress
4
+ =====================
5
+
6
+ A subclass of [BTC::Address](address.md) that represents an address based on the public key (pay-to-public-key-hash, P2PKH).
7
+
8
+ Initializers
9
+ ------------
10
+
11
+ #### new(string: *String*)
12
+
13
+ Returns a new address by parsing a [Base58Check](base58.md)-encoded string that must represent a valid P2PKH address.
14
+
15
+ #### new(hash: *String*, network: *BTC::Network*)
16
+
17
+ Returns a new address with a 20-byte binary string `hash`.
18
+
19
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
20
+
21
+ #### new(public_key: *String*, network: *BTC::Network*)
22
+
23
+ Returns a new address with a hash of the binary string `public_key`.
24
+
25
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
26
+
27
+ #### new(key: *BTC::Key*, network: *BTC::Network*)
28
+
29
+ Returns a new address with a hash of the `key.public_key`.
30
+
31
+ If `network` is not specified, `key.network` is used.
32
+
33
+
34
+ Instance Methods
35
+ ----------------
36
+
37
+ #### hash
38
+
39
+ Returns a 20-byte binary hash stored within the address.
40
+
41
+ #### to_s
42
+
43
+ Returns string representation of the address in [Base58Check](base58.md) encoding.
44
+
45
+ #### network
46
+
47
+ Returns a [BTC::Network](network.md) instance based on the version prefix of the address (mainnet or testnet).
48
+
49
+ #### version
50
+
51
+ Returns an integer value of the one-byte prefix of the address. 0 for mainnet, 111 for testnet.
52
+
53
+ #### public_address
54
+
55
+ Returns `self`.
56
+
57
+ #### p2pkh?
58
+
59
+ Returns `true`.
60
+
61
+ #### p2sh?
62
+
63
+ Returns `false`.
64
+
65
+ #### script
66
+
67
+ Returns [BTC::Script](script.md) instance that can be used in the [transaction output](transaction_output.md) to send bitcoins to this address.
68
+
69
+
70
+
71
+
@@ -0,0 +1,64 @@
1
+ [Index](index.md)
2
+
3
+ BTC::ScriptHashAddress
4
+ =====================
5
+
6
+ A subclass of [BTC::Address](address.md) that represents an address based on the redeem [script](script.md) (pay-to-script-hash, P2SH).
7
+
8
+ Initializers
9
+ ------------
10
+
11
+ #### new(string: *String*)
12
+
13
+ Returns a new address by parsing a [Base58Check](base58.md)-encoded string that must represent a valid P2SH address.
14
+
15
+ #### new(hash: *String*, network: *BTC::Network*)
16
+
17
+ Returns a new address with a 20-byte binary `hash`.
18
+
19
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
20
+
21
+ #### new(redeem_script: *BTC::Script*, network: *BTC::Network*)
22
+
23
+ Returns a new address with a hash of the [BTC::Script](script.md) (so-called “redeem script”).
24
+
25
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
26
+
27
+
28
+ Instance Methods
29
+ ----------------
30
+
31
+ #### hash
32
+
33
+ Returns a 20-byte binary hash stored within the address.
34
+
35
+ #### to_s
36
+
37
+ Returns string representation of the address in [Base58Check](base58.md) encoding.
38
+
39
+ #### network
40
+
41
+ Returns a [BTC::Network](network.md) instance based on the version prefix of the address (mainnet or testnet).
42
+
43
+ #### version
44
+
45
+ Returns an integer value of the one-byte prefix of the address. 5 for mainnet, 196 for testnet.
46
+
47
+ #### public_address
48
+
49
+ Returns `self`.
50
+
51
+ #### p2pkh?
52
+
53
+ Returns `false`.
54
+
55
+ #### p2sh?
56
+
57
+ Returns `true`.
58
+
59
+ #### script
60
+
61
+ Returns [BTC::Script](script.md) instance that can be used in the [transaction output](transaction_output.md) to send bitcoins to this address.
62
+
63
+ Note: do not confuse this with the “redeem script” that is compressed within the address's *hash*.
64
+
@@ -0,0 +1,84 @@
1
+ [Index](index.md)
2
+
3
+ BTC::ProofOfWork
4
+ ================
5
+
6
+ ProofOfWork module defines functions to convert between different kinds of difficulty and proof of work representations.
7
+
8
+ * **Target** is big unsigned integer derived from 256-bit hash (interpreted as little-endian integer).
9
+ Hash of a valid block should be below target.
10
+ * **Bits** is a "compact" representation of a target as uint32.
11
+ * **difficulty** is a floating point multiple of the minimum difficulty.
12
+ Difficulty = 2 means the block is 2x more difficult than the minimal difficulty.
13
+
14
+ You can use all functions as methods on `ProofOfWork` object.
15
+
16
+ Constants
17
+ ---------
18
+
19
+ #### MAX\_TARGET\_MAINNET
20
+
21
+ Defines the minimum difficulty for the proof of work on mainnet blockchain. See also [Network#max_target](network.md#max_target).
22
+
23
+ Equals `0x00000000ffff0000000000000000000000000000000000000000000000000000`.
24
+
25
+ #### MAX\_TARGET\_TESTNET
26
+
27
+ Defines the minimum difficulty for the proof of work on testnet blockchain. See also [Network#max_target](network.md#max_target).
28
+
29
+ Equals `0x00000007fff80000000000000000000000000000000000000000000000000000`.
30
+
31
+
32
+ Module Functions
33
+ ----------------
34
+
35
+ #### bits\_from\_target(*target*)
36
+
37
+ Converts a 256-bit integer (*Bignum*) to 32-bit compact representation (*Fixnum*).
38
+
39
+ ```ruby
40
+ >> ProofOfWork.bits_from_target(0x00000000ffff0000000000000000000000000000000000000000000000000000)
41
+ => 0x1d00ffff
42
+ ```
43
+
44
+ #### target\_from\_bits(*bits*)
45
+
46
+ Converts a 32-bit compact representation of the target to a 256-bit integer (*Bignum*).
47
+
48
+ ```ruby
49
+ >> ProofOfWork.target_from_bits(0x1d00ffff)
50
+ => 0x00000000ffff0000000000000000000000000000000000000000000000000000
51
+ ```
52
+
53
+ #### bits\_from\_difficulty(*difficulty*, max\_target: MAX\_TARGET\_MAINNET)
54
+
55
+ Computes bits from difficulty. Could be inaccurate since difficulty is a limited-precision floating-point number.
56
+
57
+ If not specified, `max_target` equals `MAX_TARGET_MAINNET`.
58
+
59
+ #### difficulty\_from\_bits(*bits*, max\_target: MAX\_TARGET\_MAINNET)
60
+
61
+ Computes difficulty from bits.
62
+
63
+ If not specified, `max_target` equals `MAX_TARGET_MAINNET`.
64
+
65
+ #### target\_from\_difficulty(*difficulty*, max\_target: MAX\_TARGET\_MAINNET)
66
+
67
+ Computes target from difficulty. Could be inaccurate since difficulty is a limited-precision floating-point number.
68
+
69
+ If not specified, `max_target` equals `MAX_TARGET_MAINNET`.
70
+
71
+ #### difficulty\_from\_target(*target*, max\_target: MAX\_TARGET\_MAINNET)
72
+
73
+ Compute relative difficulty from a given target.
74
+ E.g. returns 2.5 if target is 2.5 times harder to reach than the `max_target`.
75
+
76
+ If not specified, `max_target` equals `MAX_TARGET_MAINNET`.
77
+
78
+ #### hash\_from\_target(*target*)
79
+
80
+ Converts target integer to a binary little-endian 32-byte string padded with zero bytes.
81
+
82
+ #### target\_from\_hash(*hash*)
83
+
84
+ Converts 32-byte binary string to a target integer (`hash` is treated as a little-endian integer).
@@ -0,0 +1,280 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Script
4
+ ===========
5
+
6
+ Script is a string of [opcodes](opcode.md) specifying conditions that allow moving bitcoins from one transaction to another.
7
+
8
+ Script can be used in two contexts:
9
+
10
+ * In [transaction outputs](transaction_output.md), as a predicate (e.g. “only an owner of this key is allowed to spend funds”).
11
+ Such script is called simply a **script** or a **scriptPubKey**.
12
+ * In [transaction inputs](transaction_input.md), as an input to a script from a corresponding output
13
+ (e.g. “here is a signature made with the key mentioned in the output”).
14
+ Such script is called a **signature script** or a **scriptSig**.
15
+
16
+ Typically an output script contains interesting opcodes while the input script contains only static data, without any opcodes
17
+ (because any operations in the input script can be always pre-computed).
18
+
19
+ In case of [P2SH](p2sh.md) payments, the output script contains only a hash of an actual predicate script.
20
+ The input script then contains both the static data and a corresponding predicate script (called "redemption script").
21
+
22
+
23
+ Initializers
24
+ ------------
25
+
26
+ #### new()
27
+
28
+ Returns a new empty `BTC::Script` instance. Use `<<` or `+` operators to add operators to the script.
29
+
30
+ ```ruby
31
+ >> Script.new << OP_9 << "some pushdata operation" << OP_VERIFY
32
+ => OP_9 736f6d65207075736864617461206f7065726174696f6e OP_VERIFY
33
+ ```
34
+
35
+ #### new(hex: *String*)
36
+
37
+ Returns a new `BTC::Script` instance deserialized from a given hex-encoded string.
38
+
39
+ ```ruby
40
+ >> Script.new(hex: "76a914f2b27f7f9e519a6e77228a603c5c9a8434946a2288ac")
41
+ => OP_DUP OP_HASH160 f2b27f7f9e519a6e77228a603c5c9a8434946a22 OP_EQUALVERIFY OP_CHECKSIG
42
+ ```
43
+
44
+ #### new(data: *String*)
45
+
46
+ Returns a new `BTC::Script` instance deserialized from a given binary string.
47
+
48
+ ```ruby
49
+ >> Script.new(data: "76a914f2b27f7f9e519a6e77228a603c5c9a8434946a2288ac".from_hex)
50
+ => OP_DUP OP_HASH160 f2b27f7f9e519a6e77228a603c5c9a8434946a22 OP_EQUALVERIFY OP_CHECKSIG
51
+ ```
52
+
53
+ #### new(op\_return: *String* or *Array of Strings*)
54
+
55
+ Returns a new `BTC::Script` instance containing `OP_RETURN` opcode followed by one or more *pushdata* binary strings.
56
+
57
+ ```ruby
58
+ >> Script.new(op_return: "correct horse battery staple")
59
+ => OP_RETURN 636f727265637420686f727365206261747465727920737461706c65
60
+
61
+ >> Script.new(op_return: ["correct horse battery", "battery staple correct"])
62
+ => OP_RETURN 636f727265... 6261747465...
63
+ ```
64
+
65
+ #### new(public\_keys: *Array*, signatures\_required: *Integer*)
66
+
67
+ Returns a new `BTC::Script` instance containing an `OP_CHECKMULTISIG` opcode with the provided public keys and a required number of signatures:
68
+
69
+ ```ruby
70
+ >> a, b, c = [1,2,3].map{ Key.random.public_key }
71
+ >> Script.new(public_keys: [a, b, c], signatures_required: 2)
72
+ => OP_2 0357f93e... 025f3ed5... 021a90c5... OP_3 OP_CHECKMULTISIG
73
+ ```
74
+
75
+
76
+ Instance Methods
77
+ ----------------
78
+
79
+ #### data
80
+
81
+ Returns a serialized binary form of the script.
82
+
83
+ #### standard?
84
+
85
+ Returns `true` if this script is of standard kind. Valid non-standard scripts are allowed,
86
+ but are not relayed by nodes with default configuration and may take longer to be included in a block.
87
+
88
+ As of March 2015, scripts returning `true` from the following methods are considered standard:
89
+
90
+ * `public_key_hash_script?`
91
+ * `script_hash_script?`
92
+ * `standard_multisig_script?`
93
+ * `public_key_script?`
94
+ * `standard_op_return_script?`
95
+
96
+ #### data_only?
97
+
98
+ Returns `true` if the script consists of *pushdata* operations only (uncluding small integer opcodes: `OP_0`, `OP_1` etc).
99
+ This is used to verify input script for [P2SH](p2sh.md) output.
100
+
101
+ #### to_s
102
+
103
+ Returns a human-readable representation of script. E.g. instead of raw binary `76a914f2b2...` returns `"OP_DUP OP_HASH160 f2b2..."`.
104
+
105
+ #### to_a
106
+
107
+ Returns an array of [opcodes](opcode.md) (integers) and *pushdatas* (strings). `OP_0` is encoded as an empty string.
108
+
109
+ #### to_hex
110
+
111
+ Returns a raw binary representation of script (see `data`) in hex encoding.
112
+
113
+ #### dup
114
+
115
+ Returns a complete copy of a script.
116
+
117
+ #### ==
118
+
119
+ Returns true if both scripts can be serialized in the same binary string.
120
+
121
+
122
+
123
+ ### Regular Scripts
124
+
125
+
126
+ #### public\_key\_script?
127
+
128
+ Returns `true` if the script is of form `<pubkey> OP_CHECKSIG` (rarely used pay-to-pubkey script).
129
+
130
+ #### public_key
131
+
132
+ Returns a raw public key if this script is `public_key_script?`.
133
+
134
+ #### public\_key\_hash\_script?
135
+
136
+ Returns `true` if this script is a [P2PKH](p2pkh.md) script (`OP_DUP OP_HASH160 <20-byte hash> OP_EQUALVERIFY OP_CHECKSIG`).
137
+ This is the most used kind of script that corresponds to a [pay-to-pubkey-hash](p2pkh.md) address.
138
+
139
+ #### public\_key\_hash
140
+
141
+ Returns a 20-byte hash of the public key if this script is `public_key_hash_script?`.
142
+
143
+ #### script\_hash\_script?
144
+
145
+ Returns `true` if this script is a [P2SH](p2sh.md) script (`OP_HASH160 <20-byte hash> OP_EQUAL`).
146
+
147
+ #### script_hash
148
+
149
+ Returns a 20-byte hash of the script if this script is `script_hash_script?`.
150
+
151
+
152
+
153
+ ### Null Data (OP_RETURN) Scripts
154
+
155
+ #### op\_return\_script?
156
+
157
+ Returns `true` if this script contains `OP_RETURN` opcode followed by pushdata.
158
+
159
+ #### standard\_op\_return\_script?
160
+
161
+ Returns `true` if this script contains `OP_RETURN` opcode followed by one pushdata string with a length of 40 bytes or less.
162
+
163
+ #### op\_return\_data
164
+
165
+ Returns the first *pushdata* string if this script is `op_return_script?`.
166
+
167
+ #### op\_return\_items
168
+
169
+ Returns all *pushdata* strings if this script is `op_return_script?`.
170
+
171
+
172
+
173
+
174
+ ### Multisig Scripts
175
+
176
+ #### multisig\_script?
177
+
178
+ Returns `true` if this script is a valid multisig script of form `<M> <public keys> <N> OP_CHECKMULTISIG` where:
179
+
180
+ * Both N and M are greater than zero
181
+ * N is greater or equal M
182
+ * N equals the number of public keys.
183
+
184
+ Both *N* and *M* can be encoded as small integer [opcodes](opcode.md) (`OP_1` to `OP_16`) or as *pushdatas* containing little-endian integers.
185
+
186
+ #### standard\_multisig\_script?
187
+
188
+ Returns `true` if this script is a multisig script with additional constraints:
189
+
190
+ * Both *N* and *M* are encoded as small integer opcodes (`OP_1`, `OP_2` etc.)
191
+ * Both *N* and *M* are not greater than 15.
192
+
193
+ Note: this applies to multisig scripts used as *redeem scripts* within [P2SH](p2sh.md).
194
+ Standard bare multisig scripts (not wrapped in P2SH) must have *N* no greater than 3.
195
+
196
+ #### multisig\_public\_keys
197
+
198
+ Returns an array of raw public keys if this script is `multisig_script?`.
199
+
200
+ #### multisig\_signatures\_required
201
+
202
+ Returns a number of required signatures if this script is `multisig_script?`.
203
+
204
+
205
+
206
+ ### Conversion
207
+
208
+ #### standard_address(network: *BTC::Network*)
209
+
210
+ Returns [BTC::PublicKeyAddress](p2pkh.md) or [BTC::ScriptHashAddress](p2sh.md) if
211
+ the script is a standard output script for these addresses.
212
+ Returns `nil` for all other scripts.
213
+
214
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
215
+
216
+ #### p2sh_script
217
+
218
+ Returns a [P2SH](p2sh.md) script that wraps the receiver: `OP_HASH160 #{script.data.hash160} OP_EQUAL`.
219
+
220
+ #### simulated\_signature\_script(strict: true|false)
221
+
222
+ Returns a dummy *signature script* of the same length and structure as the intended *signature script* for the receiver.
223
+ Only a few standard script types are supported.
224
+
225
+ Set `strict` to `false` to allow imprecise guess for P2SH script (2-of-3 multisig). Default is `true`.
226
+
227
+ Returns `nil` if could not determine a matching script.
228
+
229
+
230
+
231
+
232
+ ### Modification
233
+
234
+
235
+ #### append_opcode(opcode)
236
+
237
+ Appends a non-pushdata opcode to the script.
238
+
239
+ #### append_pushdata(string, opcode: *Integer*)
240
+
241
+ Appends a pushdata opcode with binary `string` in the most compact encoding.
242
+
243
+ Optional `opcode` may be equal to `OP_PUSHDATA1`, `OP_PUSHDATA2`, or `OP_PUSHDATA4` to specify a non-compact encoding.
244
+
245
+ Raises `ArgumentError` if opcode does not represent a given string length.
246
+
247
+ #### delete_opcode(opcode)
248
+
249
+ Removes all occurences of the `opcode`. Typically used by verification engine to remove `OP_CODESEPARATOR`.
250
+
251
+ #### delete_pushdata(string)
252
+
253
+ Removes all occurences of *pushdata* opcodes containing `string`.
254
+
255
+ #### append_script(script)
256
+
257
+ Appends a `BTC::Script` instance to receiver.
258
+
259
+ #### <<(*script*)
260
+
261
+ Appends `BTC::Script` instance to receiver. Same as `append_script(script)`.
262
+
263
+ #### <<(*Integer*)
264
+
265
+ Appends an opcode specified by the *Integer*. Same as `append_opcode(integer)`.
266
+
267
+ #### <<(*String*)
268
+
269
+ Appends a *pushdata* opcode with a given string. Same as `append_pushdata(string)`.
270
+
271
+ #### <<(*Array*)
272
+
273
+ Appends an array of `BTC::Script` instances, opcode and strings. Array may contain nested arrays.
274
+
275
+ #### +
276
+
277
+ Returns a copy of the receiver with appended object using `<<`. It is defined as `self.dup << argument`.
278
+
279
+
280
+
@@ -0,0 +1,71 @@
1
+ [Index](index.md)
2
+
3
+ Bitcoin Signatures
4
+ ==================
5
+
6
+ Bitcoin uses ECDSA signatures that prove that an owner of a certain [private key](key.md#private_key)
7
+ acknowledges a certain message (usually a [transaction](transaction.md)).
8
+
9
+ One or more signatures are used in a [transaction input](transaction_input.md) to prove that
10
+ the owner of bitcoins locked in a corresponding [output](transaction_output.md) allows spending them in this specific transaction.
11
+
12
+ Within Bitcoin transactions, signature contains an extra byte (*hash type*) that specifies how exactly the transaction must be transformed before being signed.
13
+ To differentiate between regular signatures and signatures with a hash type, we use the following terms:
14
+
15
+ **Signature** is a DER-encoded ECDSA signature.
16
+
17
+ **Script Signature** is a DER-encoded ECDSA signature with an appended *hashtype* byte.
18
+
19
+ Hash Types
20
+ ----------
21
+
22
+ Hash type determines how [OP_CHECKSIG](opcode.md#op_checksig) hashes the [transaction](transaction.md)
23
+ to verify the signature in a [transaction input](transaction_output.md).
24
+ Depending on hash type, transaction is modified in some way before its hash is computed.
25
+ Hash type is 1 byte appended to a signature in a [transaction input](transaction_input.md).
26
+
27
+ First three types are mutually exclusive (tested using `hashtype & 0x1F`).
28
+ `SIGHASH_ANYONECANPAY` type can be combined together with any of the first three types.
29
+
30
+ #### BTC::SIGHASH_ALL = 0x01
31
+
32
+ Default. Signs all inputs and outputs.
33
+ Other inputs have scripts and sequences zeroed out, current input has its script
34
+ replaced by the previous transaction's output script (or, in case of [P2SH](p2sh.md),
35
+ by the signatures and the redemption script).
36
+
37
+ If `(hashtype & SIGHASH_OUTPUT_MASK)` is not `SIGHASH_NONE` or `SIGHASH_SINGLE`, then `SIGHASH_ALL` is used.
38
+
39
+ #### BTC::SIGHASH_NONE = 0x02
40
+
41
+ All outputs are removed. "I don't care where it goes as long as others pay".
42
+ Note: this is not safe when used with `SIGHASH_ANYONECANPAY`, because then anyone who relays the transaction
43
+ can pick your input and use in his own transaction.
44
+ It's also not safe if all inputs are `SIGHASH_NONE` as well (or it's the only input).
45
+
46
+ #### BTC::SIGHASH_SINGLE = 0x03
47
+
48
+ Hash only the output with the same index as the current input.
49
+ Preceding outputs are "nullified", other outputs are removed.
50
+ Special case: if there is no matching output, hash is equal
51
+ `0000000000000000000000000000000000000000000000000000000000000001` (32 bytes)
52
+
53
+ #### BTC::SIGHASH\_OUTPUT\_MASK = 0x1F
54
+
55
+ This mask is used to determine one of the first types independently from `SIGHASH_ANYONECANPAY` option:
56
+
57
+ ```
58
+ if (hashtype & SIGHASH_OUTPUT_MASK) == SIGHASH_NONE
59
+ blank all outputs
60
+ end
61
+ ```
62
+
63
+ #### BTC::SIGHASH_ANYONECANPAY = 0x80
64
+
65
+ Removes all inputs except for current txin before hashing.
66
+ This allows to sign the transaction outputs without knowing who and how adds other inputs.
67
+ E.g. a crowdfunding transaction with 100 BTC output can be signed independently by any number of people
68
+ and will become valid only when someone combines all inputs in a single transaction to make it valid.
69
+ Can be used together with any of the above types.
70
+
71
+