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,177 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Key
4
+ ========
5
+
6
+ Class BTC::Key encapsulates a pair of public and private keys (or a public key only)
7
+ and provides methods to sign messages and verify signatures.
8
+
9
+ **Private key** is a 256-bit integer encoded in a 32-byte big-endian binary string (always padded with zeros from the left).
10
+ Private key is used to sign messages. Private key can be `nil` for *public-only* BTC::Key instances.
11
+
12
+ **Public key** is a 33-byte or 65-byte binary string encoding coordinates of a point on
13
+ the elliptic curve [secp256k1](https://en.bitcoin.it/wiki/Secp256k1).
14
+ Public key is used to verify signatures.
15
+
16
+ Public key can be **compressed** (33 bytes) or **uncompressed** (65 bytes).
17
+ Compressed public key contains only an X coordinate of a point and recovers Y coordinate with extra arithmetic operations.
18
+ Uncompressed public key contains both X and Y coordinates. Both versions are equally capable of verifying signatures, but
19
+ resulting [addresses](p2pkh.md) are different (because hashes are different). [BIP32](keychain.md) standard uses only compressed public key for compactness and consistency.
20
+
21
+ If the BTC::Key instance contains only a public key, it can only verify signatures, but cannot create them.
22
+
23
+ Class Methods
24
+ -------------
25
+
26
+ #### validate\_private\_key\_range(*private_key*)
27
+
28
+ Returns `true` if data representing a private key is within a valid range for elliptic curve *secp256k1*.
29
+
30
+ #### validate\_public\_key(*public_key*)
31
+
32
+ Returns `true` if the raw binary public key is valid and well-formed.
33
+ Accepts both compressed and uncompressed public keys.
34
+ Logs detailed info using [BTC::Diagnostics](diagnostics.md).
35
+
36
+ #### validate\_script\_signature(*data*, verify\_lower\_s: true)
37
+
38
+ Returns `true` if the [script signature](signature.md) is valid.
39
+
40
+ If `verify_lower_s` is `true` (it is by default), then this method also verifies if the signature is canonical.
41
+
42
+ #### validate\_and\_normalize\_script\_signature(*script_sig*)
43
+
44
+ Validates [script signature](signature.md) and normalizes it if needed.
45
+
46
+ Returns `nil` if script signature is not valid (e.g. DER encoding is invalid, or [hash type](signature.md) is invalid).
47
+
48
+ Returns `script_sig` if it is a valid, canonical signature.
49
+
50
+ Returns a normalized ("lower s") version of `script_sig` if it is non-canonical.
51
+
52
+ #### normalized_signature(*signature*)
53
+
54
+ Returns a `sig` or a normalized version of `sig`.
55
+ Assumes signature is a valid plain DER-encoded ECDSA signature, otherwise raises `BTCError`.
56
+
57
+ Initializers
58
+ ------------
59
+
60
+ #### random(public\_key\_compressed: *true|false*, network: *BTC::Network*)
61
+
62
+ Returns a new `BTC::Key` instance with a random `private_key`.
63
+
64
+ If `public_key_compressed` is not specified, `true` is used.
65
+
66
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
67
+
68
+
69
+ #### new(private\_key: *String*, public\_key\_compressed: *true|false*, network: *BTC::Network*)
70
+
71
+ Returns a new `BTC::Key` instance with a given binary `private_key`.
72
+
73
+ If `private_key` is not within a valid range, raises `FormatError`.
74
+
75
+ If `public_key_compressed` is not specified, `true` is used.
76
+
77
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
78
+
79
+
80
+ #### new(public_key: *String*, network: *BTC::Network*)
81
+
82
+ Returns a new `BTC::Key` instance with a given binary `public_key`.
83
+ This instance does not contain a private key and can only be used to verify signatures.
84
+
85
+ If `public_key` is not a valid compressed or uncompressed public key, raises `FormatError`.
86
+
87
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
88
+
89
+
90
+ #### new(wif: *String*)
91
+
92
+ Returns a new `BTC::Key` instance with a private key decoded from [WIF](wif.md) string.
93
+
94
+ If `wif` is not a valid WIF-encoded string, raises `FormatError`.
95
+
96
+ Public key compression and `network` attributes are inherited from WIF.
97
+
98
+
99
+ Instance Methods
100
+ ----------------
101
+
102
+ #### ecdsa_signature(*hash*)
103
+
104
+ Returns a binary DER-encoded ECDSA signature for a given binary string `hash` (typically 32-byte long).
105
+ Signature is normalized and uses deterministic nonce `k` based on private key and the `hash`.
106
+
107
+ Raises `ArgumentError` if receiver's private key is `nil`.
108
+
109
+ #### verify\_ecdsa\_signature(*signature*, *hash*)
110
+
111
+ Returns `true` if the binary DER-encoded `signature` for a given binary string `hash` is correctly verified
112
+ against the public key of the receiver.
113
+
114
+ #### public\_key\_compressed
115
+
116
+ Returns `true` or `false` depending on whether public key is compressed.
117
+
118
+ #### compressed_key
119
+
120
+ Returns a new `BTC::Key` instance with `public_key_compressed` set to `true`.
121
+
122
+ #### uncompressed_key
123
+
124
+ Returns a new `BTC::Key` instance with `public_key_compressed` set to `false`.
125
+
126
+ #### private_key
127
+
128
+ Returns a raw 32-byte private key or `nil` if it is a public-only key.
129
+
130
+ #### public_key
131
+
132
+ Returns a raw 33- or 65-byte public key depending on `public_key_compressed` value.
133
+
134
+ #### compressed\_public\_key
135
+
136
+ Returns a raw compressed 33-byte public key.
137
+
138
+ #### uncompressed\_public\_key
139
+
140
+ Returns a raw uncompressed 65-byte public key.
141
+
142
+ #### network
143
+
144
+ Returns a [BTC::Network](network.md) instance used by `address` method (mainnet or testnet).
145
+
146
+ #### address(network: *BTC::Network*)
147
+
148
+ Returns a [BTC::PublicKeyAddress](p2pkh.md) instance with a given `network`.
149
+
150
+ If `network` is not specified, `self.network` is used.
151
+
152
+ #### to_wif(network: *BTC::Network*)
153
+
154
+ Returns `private_key` encoded in [WIF](wif.md).
155
+
156
+ Returns nil if `private_key` is nil.
157
+
158
+ If `network` is not specified, `self.network` is used.
159
+
160
+ #### dup
161
+
162
+ Returns a new `BTC::Key` instance with the same `private_key`, `public_key` and `network`.
163
+
164
+ #### ==
165
+
166
+ Returns `true` if both instances have equal private and public keys.
167
+
168
+
169
+
170
+
171
+
172
+
173
+
174
+
175
+
176
+
177
+
@@ -0,0 +1,180 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Keychain
4
+ =============
5
+
6
+ BTC::Keychain is an implementation of BIP32 "[Hierarchical Deterministic Wallets](https://github.com/bitcoin/bips/blob/master/bip-0032.mediawiki)" (HD Wallets).
7
+
8
+ Keychain encapsulates either a pair of "extended" keys (private and public), or only a public extended key.
9
+ **Extended key** means the key (private or public) is accompanied by an extra 256 bits of entropy
10
+ called **chain code** and some metadata about its position in a tree of keys (depth, parent fingerprint, index).
11
+
12
+ Keychain allows to derive [keys](key.md) using an unsigned 32-bit integer called **index**.
13
+
14
+ Keychain has two modes of operation:
15
+
16
+ 1. **Normal derivation** which allows to derive public keys separately from the private ones.
17
+ 2. **Hardened derivation** which derives keys from the private keychain.
18
+
19
+ Derivation can be treated as a single key or as a new branch of keychains.
20
+ BIP43 and BIP44 propose a way for wallets to organize streams of keys using Keychain.
21
+
22
+ Examples
23
+ --------
24
+
25
+ Create a keychain with a seed, derive keychains and keys:
26
+
27
+ ```ruby
28
+ keychain = Keychain.new(seed: "secret seed")
29
+ keychain.derived_keychain(0).xpub # => "xpub68YTawrm7..."
30
+ keychain.derived_key(0).address.to_s # => "1HosjTCbRD9og..."
31
+ ```
32
+
33
+ You can derive keys using hardened method (`hardened` is `false` by default):
34
+
35
+ ```ruby
36
+ keychain.derived_keychain(0, hardened: true).xpub # => "xpub68YTawruT..."
37
+ keychain.derived_key(0, hardened: true).address.to_s # => "14ELmCNCnku1Y..."
38
+ ```
39
+
40
+ You can specify entire paths:
41
+
42
+ ```ruby
43
+ keychain.derived_key("44'/0'/0'/0/0").address.to_s # => "1fY2x5v63a..."
44
+ ```
45
+
46
+ Initializers
47
+ ------------
48
+
49
+ #### new(seed: *String*, network: *BTC::Network*)
50
+
51
+ Returns a new `BTC::Keychain` instance initialized with a binary string `seed`.
52
+
53
+ To generate `seed`, you may use [BTC::Data.random_data](data.md#random_data)(16).
54
+
55
+ If `network` is not specified, [BTC::Network.default](network.md#default) is used.
56
+
57
+ #### new(extended_key: *String*)
58
+
59
+ Returns a new `BTC::Keychain` instance initialized with a given extended public or private key (`"xpub..."` or `"xprv..."`).
60
+
61
+ `network` attribute is set based on the encoding of the extended key. If `"tpub..."` or `"tprv..."` is used, `Network.testnet` is assigned.
62
+
63
+ #### new(xpub: *String*)
64
+
65
+ An alias to `new(extended_key: ...)`.
66
+
67
+ #### new(xprv: *String*)
68
+
69
+ An alias to `new(extended_key: ...)`.
70
+
71
+
72
+ Instance Methods
73
+ ----------------
74
+
75
+ #### key
76
+
77
+ Instance of [BTC::Key](key.md) that is a "head" of this keychain.
78
+ If the keychain is public-only, `key` does not have a private component.
79
+
80
+ #### chain_code
81
+
82
+ A 32-byte binary "chain code" string used as an additional entropy in derivation procedure.
83
+
84
+ #### extended\_public\_key
85
+
86
+ A [Base58Check](base58.md)-encoded extended public key.
87
+ Starts with `xpub` for mainnet and `tpub` for testnet.
88
+
89
+ #### xpub
90
+
91
+ An alias to `extended_public_key`.
92
+
93
+ #### extended\_private\_key
94
+
95
+ A [Base58Check](base58.md)-encoded extended private key.
96
+ Starts with `xprv` for mainnet and `tprv` for testnet.
97
+
98
+ #### xprv
99
+
100
+ An alias to `extended_private_key`.
101
+
102
+ #### identifier
103
+
104
+ A 160-bit binary identifier (aka "hash") of the keychain.
105
+ Equals `RIPEMD160(SHA256(key.public_key))`
106
+
107
+ #### fingerprint
108
+
109
+ 32-bit unsigned integer extracted from `identifier`.
110
+
111
+ #### parent_fingerprint
112
+
113
+ A fingerprint of the parent keychain. Returns 0 for the master (root) keychain.
114
+
115
+ #### index
116
+
117
+ Index of this keychain (uint32) in the parent keychain. Returns 0 for master (root) keychain.
118
+
119
+ #### depth
120
+
121
+ Depth in the hierarchy (uint32). Returns 0 for master (root) keychain.
122
+
123
+ #### network
124
+
125
+ Returns a [BTC::Network](network.md) instance used to format extended public and private keys.
126
+
127
+ #### private?
128
+
129
+ Returns `true` if this keychain contains a private component (can derive both public and private keys).
130
+
131
+ #### public?
132
+
133
+ Returns `true` if this keychain does not contain a private component (can only derive public keys).
134
+
135
+ #### hardened?
136
+
137
+ Returns `true` if this keychain was derived using *hardened* method from its parent.
138
+ For master (root) keychain returns `false`.
139
+
140
+ #### public_keychain
141
+
142
+ Returns a public-only copy of the keychain.
143
+
144
+ #### derived_keychain(*index*, hardened: *false | true*)
145
+
146
+ Returns a derived keychain with a given `index`.
147
+
148
+ Raises `ArgumentError` if `index` is below zero or higher than 0x7fffffff.
149
+
150
+ If `hardened` is `true`, uses private key for hardened derivation.
151
+ If this is a public-only keychain, raises `BTCError`. Default value is `false`.
152
+
153
+ #### derived_keychain(*path*)
154
+
155
+ Returns a derived keychain with a given string `path`.
156
+ Path is specified as a sequence of integers separated by a forward slash `/`.
157
+ Integers can be suffixed with `'` to specify hardened derivation.
158
+ Path can be optionally prefixed with `m/`.
159
+
160
+ Raises `ArgumentError` if `path` is not a well-formed derivation path.
161
+
162
+ The following are equivalent:
163
+
164
+ ```ruby
165
+ k.derived_keychain(0) == k.derived_keychain("m/0")
166
+ k.derived_keychain(1) == k.derived_keychain("1")
167
+ k.derived_keychain(2, hardened: true) == k.derived_keychain("2'")
168
+ k.derived_keychain(3).derived_keychain(4) == k.derived_keychain("3/4")
169
+ k.derived_keychain(5, hardened: true).derived_keychain(6) == k.derived_keychain("m/5'/6")
170
+ ```
171
+
172
+ #### derived_key(*index*, hardened: *false | true*)
173
+
174
+ Equivalent to `derived_keychain(...).key`
175
+
176
+ #### derived_key(*path*)
177
+
178
+ Equivalent to `derived_keychain(path).key`
179
+
180
+
@@ -0,0 +1,75 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Network
4
+ ============
5
+
6
+ Network object specifies one of Bitcoin networks: `mainnet` or `testnet`.
7
+ Network affects formatting of [addresses](address.md), private keys in [WIF](wif.md) format and
8
+ extended private and public keys in [BIP32 Keychains](keychain.md).
9
+
10
+ You typically use network by asking a specific object whether it belongs to mainnet or testnet:
11
+
12
+ ```ruby
13
+ Address.parse("1A39uDWJkaPT2o5qr5dVBdvhZtNXeSNXM4").network.mainnet? # => true
14
+ Address.parse("mipcBbFg9gMiCh81Kj8tqqdgoZub1ZJRfn").network.testnet? # => true
15
+ WIF.parse("L3p8oAcQTtuokSCRHQ7i4MhjWc9zornvpJLfmg62sYpLRJF9woSu").network.testnet? # => false
16
+ Keychain.new(xpub: "tpubD6NzVbkrYhZ4YFb9G...").network.mainnet? # => false
17
+ ```
18
+
19
+ Class Methods
20
+ -------------
21
+
22
+ #### default
23
+
24
+ Current default network used when creating a [BTC::Key](key.md) or [BTC::Keychain](keychain.md) without specifying network explicitly.
25
+ By default equals `BTC::Network.mainnet`.
26
+
27
+ You may use this to validate user input:
28
+
29
+ ```ruby
30
+ address = Address.parse(user_entered_address)
31
+ if address.network != Network.default
32
+ raise "Entered address is not compatible with the network used by application!"
33
+ end
34
+ ```
35
+
36
+ #### default=(network)
37
+
38
+ Sets the default network to be used for all new objects that do not explicitly specify network.
39
+ This setting does not affect already created objects that have some network specified.
40
+
41
+ #### mainnet
42
+
43
+ Returns a singleton object representing the "main" Bitcoin network.
44
+
45
+ #### testnet
46
+
47
+ Returns a singleton object representing the "testnet3" Bitcoin network.
48
+
49
+
50
+ Instance Methods
51
+ ----------------
52
+
53
+ #### name
54
+
55
+ Returns a name of the network (`"mainnet"` or `"testnet"`).
56
+
57
+ #### mainnet?
58
+
59
+ Returns `true` if this network is mainnet.
60
+
61
+ #### testnet?
62
+
63
+ Returns `true` if this network is testnet3.
64
+
65
+ #### genesis_block
66
+
67
+ Returns an instance of `BTC::Block` containing the genesis block for this network.
68
+
69
+ #### genesis\_block\_header
70
+
71
+ Returns an instance of `BTC::BlockHeader` containing a header of the genesis block for this network.
72
+
73
+ #### max_target
74
+
75
+ Returns a maximum target for this network (as native Bignum type). See also [proof of work conversion methods](proof_of_work.md).
@@ -0,0 +1,220 @@
1
+ [Index](index.md)
2
+
3
+ BTC::Opcode
4
+ ===========
5
+
6
+ **Operator** is a basic building block of the [script](script.md).
7
+ It is a one-byte command (*opcode*) that performs a certain action during script evaluation.
8
+ Module `BTC::Opcode` defines all available opcodes and conversion methods.
9
+
10
+ There are two kinds of opcodes:
11
+
12
+ * **Pushdata** — an opcode that pushes arbitrary data on stack.
13
+ All opcodes with value less or equal `OP_PUSHDATA4` are *pushdata* opcodes (this includes `OP_0`, but does not include `OP_1`, `OP_2` etc.)
14
+ * **Operation** — an opcode that performs some operation. This includes integer-pushing opcodes starting with `OP_1`.
15
+
16
+ Module Functions
17
+ ----------------
18
+
19
+ #### name\_for\_opcode(opcode)
20
+
21
+ Returns a name for a given opcode value. For unknwon opcode returns `"OP_UNKNOWN"`
22
+
23
+ ```ruby
24
+ Opcode.name_for_opcode(OP_VERIFY) # => "OP_VERIFY"
25
+ ```
26
+
27
+ #### opcode\_for\_name(name)
28
+
29
+ Returns an opcode value for a given name. For unknwon names returns `OP_INVALIDOPCODE` (0xFF).
30
+
31
+ ```ruby
32
+ Opcode.opcode_for_name("OP_VERIFY") # => OP_VERIFY
33
+ ```
34
+
35
+ #### opcode\_for\_small\_integer(int)
36
+
37
+ Returns an opcode corresponding to a small integer (-1, 0, 1, ... 16).
38
+ For invalid integer returns `OP_INVALIDOPCODE`.
39
+
40
+ ```ruby
41
+ Opcode.opcode_for_small_integer(-1) # => OP_1NEGATE
42
+ ```
43
+
44
+ #### small\_integer\_from\_opcode(opcode)
45
+
46
+ Returns a small integer (-1, 0, 1, ... 16) corresponding to a given opcode.
47
+ For invalid integer returns `nil`.
48
+
49
+ ```ruby
50
+ Opcode.small_integer_from_opcode(OP_16) # => 16
51
+ ```
52
+
53
+
54
+ Operators
55
+ ---------
56
+
57
+ ### 1. Operators pushing data on stack
58
+
59
+ Name | Value | Description
60
+ :---------------|:------------|:-----------------------------------
61
+ OP_FALSE | 0x00 | Pushes byte `0` on the stack.
62
+ OP_0 | 0x00 | Pushes byte `0` on the stack.
63
+ OP_PUSHDATA*N* | 0x01..0x4b | Any opcode with value < `OP_PUSHDATA1` is a length of the string to be pushed on the stack. So opcode 0x01 is followed by 1 byte of data, 0x09 by 9 bytes and so on up to 0x4b (75 bytes).
64
+ OP_PUSHDATA1 | 0x4c | Followed by a 1-byte length of the string to push (allows pushing 0..255 bytes).
65
+ OP_PUSHDATA2 | 0x4d | Followed by a 2-byte length of the string to push (allows pushing 0..65535 bytes).
66
+ OP_PUSHDATA4 | 0x4e | Followed by a 4-byte length of the string to push (allows pushing 0..4294967295 bytes).
67
+ OP_1NEGATE | 0x4f | Pushes number `-1` on the stack.
68
+ OP_RESERVED | 0x50 | Not assigned. If executed, transaction is invalid.
69
+ OP_TRUE | 0x51 | Pushes number `1` on the stack.
70
+ OP_1 | 0x51 | Pushes number `1` on the stack.
71
+ OP_2 | 0x52 | Pushes number `2` on the stack.
72
+ OP_3 | 0x53 | Pushes number `3` on the stack.
73
+ OP_4 | 0x54 | Pushes number `4` on the stack.
74
+ OP_5 | 0x55 | Pushes number `5` on the stack.
75
+ OP_6 | 0x56 | Pushes number `6` on the stack.
76
+ OP_7 | 0x57 | Pushes number `7` on the stack.
77
+ OP_8 | 0x58 | Pushes number `8` on the stack.
78
+ OP_9 | 0x59 | Pushes number `9` on the stack.
79
+ OP_10 | 0x5a | Pushes number `10` on the stack.
80
+ OP_11 | 0x5b | Pushes number `11` on the stack.
81
+ OP_12 | 0x5c | Pushes number `12` on the stack.
82
+ OP_13 | 0x5d | Pushes number `13` on the stack.
83
+ OP_14 | 0x5e | Pushes number `14` on the stack.
84
+ OP_15 | 0x5f | Pushes number `15` on the stack.
85
+ OP_16 | 0x60 | Pushes number `16` on the stack.
86
+
87
+
88
+ ### 2. Control Flow Operators
89
+
90
+ Bitcoin executes all operators from `OP_IF` to `OP_ENDIF` even inside "non-executed" branch (to keep track of nesting).
91
+ Since `OP_VERIF` and `OP_VERNOTIF` are not assigned, even inside a non-executed branch they will fall in "default:" switch case
92
+ and cause the script to fail. Some other operators like `OP_VER` can be present inside non-executed branch because they'll be skipped.
93
+
94
+ Name | Value | Description
95
+ :---------------|:------------|:-----------------------------------
96
+ OP_NOP | 0x61 | Does nothing.
97
+ OP_VER | 0x62 | Not assigned. If executed, transaction is invalid.
98
+ OP_IF | 0x63 | If the top stack value is not 0, the statements are executed. The top stack value is removed.
99
+ OP_NOTIF | 0x64 | If the top stack value is 0, the statements are executed. The top stack value is removed.
100
+ OP_VERIF | 0x65 | Not assigned. Script is invalid with that opcode (even if inside non-executed branch).
101
+ OP_VERNOTIF | 0x66 | Not assigned. Script is invalid with that opcode (even if inside non-executed branch).
102
+ OP_ELSE | 0x67 | Executes code if the previous `OP_IF` or `OP_NOTIF` was not executed.
103
+ OP_ENDIF | 0x68 | Finishes if/else block.
104
+ OP_VERIFY | 0x69 | Removes item from the stack if it's not 0x00 or 0x80 (negative zero). Otherwise, marks script as invalid.
105
+ OP_RETURN | 0x6a | Marks transaction as invalid.
106
+
107
+ ### 3. Stack Operators
108
+
109
+ Name | Value | Description
110
+ :---------------|:------------|:-----------------------------------
111
+ OP_TOALTSTACK | 0x6b | Moves item from the stack to altstack.
112
+ OP_FROMALTSTACK | 0x6c | Moves item from the altstack to stack.
113
+ OP_2DROP | 0x6d | Removes top 2 items from stack. Fails if less than 2 items are available.
114
+ OP_2DUP | 0x6e | (a b → a b a b)
115
+ OP_3DUP | 0x6f | (a b c → a b c a b c)
116
+ OP_2OVER | 0x70 | (a b c d → a b c d a b)
117
+ OP_2ROT | 0x71 | (a b c d e f → c d e f a b)
118
+ OP_2SWAP | 0x72 | (a b c d → c d a b)
119
+ OP_IFDUP | 0x73 | Duplicates the top value only if it's not zero or negative zero (0x80).
120
+ OP_DEPTH | 0x74 | Adds size of the stack as a signed little-endian integer on stack.
121
+ OP_DROP | 0x75 | Removes the top value on stack.
122
+ OP_DUP | 0x76 | Duplicates the top value.
123
+ OP_NIP | 0x77 | Removes the value below the top one. Fails if less than 2 items are available.
124
+ OP_OVER | 0x78 | (a b → a b a)
125
+ OP_PICK | 0x79 | (x(n) ... x2 x1 x0 n → x(n) ... x2 x1 x0 x(n))
126
+ OP_ROLL | 0x7a | (x(n) ... x2 x1 x0 n → x(n-1) ... x2 x1 x0 x(n))
127
+ OP_ROT | 0x7b | (a b c → b c a)
128
+ OP_SWAP | 0x7c | (a b → b a)
129
+ OP_TUCK | 0x7d | (a b → b a b)
130
+
131
+
132
+ ### 4. Splice Operators
133
+
134
+ Name | Value | Description
135
+ :---------------|:------------|:-----------------------------------
136
+ OP_CAT | 0x7e | Disabled opcode. If executed, transaction is invalid.
137
+ OP_SUBSTR | 0x7f | Disabled opcode. If executed, transaction is invalid.
138
+ OP_LEFT | 0x80 | Disabled opcode. If executed, transaction is invalid.
139
+ OP_RIGHT | 0x81 | Disabled opcode. If executed, transaction is invalid.
140
+ OP_SIZE | 0x82 | Adds byte length of the top item as a signed little-endian integer.
141
+
142
+
143
+ ### 5. Logic Operators
144
+
145
+ Name | Value | Description
146
+ :---------------|:------------|:-----------------------------------
147
+ OP_INVERT | 0x83 | Disabled opcode. If executed, transaction is invalid.
148
+ OP_AND | 0x84 | Disabled opcode. If executed, transaction is invalid.
149
+ OP_OR | 0x85 | Disabled opcode. If executed, transaction is invalid.
150
+ OP_XOR | 0x86 | Disabled opcode. If executed, transaction is invalid.
151
+ OP_EQUAL | 0x87 | Last two items are removed from the stack and compared. Result (`true` or `false`) is pushed to the stack.
152
+ OP\_EQUALVERIFY | 0x88 | Same as `OP_EQUAL`, but removes the result from the stack if it's true or marks script as invalid.
153
+ OP_RESERVED1 | 0x89 | Disabled opcode. If executed, transaction is invalid.
154
+ OP_RESERVED2 | 0x8a | Disabled opcode. If executed, transaction is invalid.
155
+
156
+ ### 6. Numeric Operators
157
+
158
+ Name | Value | Description
159
+ :---------------------|:------|:-----------------------------------
160
+ OP_1ADD | 0x8b | Adds 1 to last item, pops it from stack and pushes result.
161
+ OP_1SUB | 0x8c | Substracts 1 to last item, pops it from stack and pushes result.
162
+ OP_2MUL | 0x8d | Disabled opcode. If executed, transaction is invalid.
163
+ OP_2DIV | 0x8e | Disabled opcode. If executed, transaction is invalid.
164
+ OP_NEGATE | 0x8f | Negates the number, pops it from stack and pushes result.
165
+ OP_ABS | 0x90 | Replaces number with its absolute value
166
+ OP_NOT | 0x91 | Replaces number with True if it's zero, False otherwise.
167
+ OP_0NOTEQUAL | 0x92 | Replaces number with True if it's not zero, False otherwise.
168
+ OP_ADD | 0x93 | (x y → x+y)
169
+ OP_SUB | 0x94 | (x y → x-y)
170
+ OP_MUL | 0x95 | Disabled opcode. If executed, transaction is invalid.
171
+ OP_DIV | 0x96 | Disabled opcode. If executed, transaction is invalid.
172
+ OP_MOD | 0x97 | Disabled opcode. If executed, transaction is invalid.
173
+ OP_LSHIFT | 0x98 | Disabled opcode. If executed, transaction is invalid.
174
+ OP_RSHIFT | 0x99 | Disabled opcode. If executed, transaction is invalid.
175
+ OP_BOOLAND | 0x9a | (x y → x and y)
176
+ OP_BOOLOR | 0x9b | (x y → x or y)
177
+ OP_NUMEQUAL | 0x9c | (x y → x == y)
178
+ OP_NUMEQUALVERIFY | 0x9d | Same as `OP_NUMEQUAL OP_VERIFY`.
179
+ OP_NUMNOTEQUAL | 0x9e | (x y → x ≠ y)
180
+ OP_LESSTHAN | 0x9f | (x y → x < y)
181
+ OP_GREATERTHAN | 0xa0 | (x y → x > y)
182
+ OP_LESSTHANOREQUAL | 0xa1 | (x y → x ≤ y)
183
+ OP_GREATERTHANOREQUAL | 0xa2 | (x y → x ≥ y)
184
+ OP_MIN | 0xa3 | (x y → min(x,y))
185
+ OP_MAX | 0xa4 | (x y → max(x,y))
186
+ OP_WITHIN | 0xa5 | (x min max → min ≤ x < max)
187
+
188
+ ### 7. Crypto Operators
189
+
190
+
191
+ Name | Value | Description
192
+ :----------------------|:------------|:-----------------------------------
193
+ OP_RIPEMD160 | 0xa6 | Replaces top value with its [RIPEMD-160](http://en.wikipedia.org/wiki/RIPEMD) hash.
194
+ OP_SHA1 | 0xa7 | Replaces top value with its [SHA-1](http://en.wikipedia.org/wiki/SHA-1) hash.
195
+ OP_SHA256 | 0xa8 | Replaces top value with its [SHA-256](http://en.wikipedia.org/wiki/SHA-2) hash.
196
+ OP_HASH160 | 0xa9 | Replaces top value with the result of `RIPEMD-160(SHA-256(string)`.
197
+ OP_HASH256 | 0xaa | Replaces top value with the result of `SHA-256(SHA-256(string))`.
198
+ OP_CODESEPARATOR | 0xab | This opcode is obsolete and does effectively nothing.
199
+ OP_CHECKSIG | 0xac | (*signature* *pubkey* → true/false) Verifies transaction signature (with [hashtype](signature.md) byte) with a given public key.
200
+ OP_CHECKSIGVERIFY | 0xad | Same as `OP_CHECKSIG OP_VERIFY`
201
+ OP_CHECKMULTISIG | 0xae | (OP\_0 *signatures* *M* *pubkeys* *N* → true/false) Verifies transaction signatures (each must have a [hashtype](signature.md) byte) against the given public keys. Signatures must be provided in the same order as corresponding public keys.
202
+ OP_CHECKMULTISIGVERIFY | 0xaf | Same as `OP_CHECKMULTISIG OP_VERIFY`
203
+
204
+
205
+ ### 8. Expansion Opcodes
206
+
207
+ Name | Value | Description
208
+ :----------------------|:------------|:-----------------------------------
209
+ OP_NOP1 | 0xb0 | Does nothing.
210
+ OP_NOP2 | 0xb1 | Does nothing.
211
+ OP_NOP3 | 0xb2 | Does nothing.
212
+ OP_NOP4 | 0xb3 | Does nothing.
213
+ OP_NOP5 | 0xb4 | Does nothing.
214
+ OP_NOP6 | 0xb5 | Does nothing.
215
+ OP_NOP7 | 0xb6 | Does nothing.
216
+ OP_NOP8 | 0xb7 | Does nothing.
217
+ OP_NOP9 | 0xb8 | Does nothing.
218
+ OP_NOP10 | 0xb9 | Does nothing.
219
+ OP_INVALIDOPCODE | 0xff | Invalid opcode. If executed, transaction is invalid.
220
+
@@ -0,0 +1,7 @@
1
+ [Index](index.md)
2
+
3
+ BTC::OpenSSL
4
+ ===============
5
+
6
+ A module containing FFI bindings to native OpenSSL functions.
7
+ Implements low-level primitives to create ECDSA signatures, transform public keys, perform arithmetic with big integers.