btcruby 0.0.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +18 -0
- data/.travis.yml +7 -0
- data/FAQ.md +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +18 -0
- data/HOWTO.md +17 -0
- data/LICENSE +19 -0
- data/README.md +59 -0
- data/Rakefile +6 -0
- data/TODO.txt +40 -0
- data/bin/console +19 -0
- data/btcruby.gemspec +20 -0
- data/documentation/address.md +73 -0
- data/documentation/base58.md +52 -0
- data/documentation/block.md +127 -0
- data/documentation/block_header.md +120 -0
- data/documentation/constants.md +88 -0
- data/documentation/data.md +54 -0
- data/documentation/diagnostics.md +90 -0
- data/documentation/extensions.md +76 -0
- data/documentation/hash_functions.md +58 -0
- data/documentation/hash_id.md +22 -0
- data/documentation/index.md +230 -0
- data/documentation/key.md +177 -0
- data/documentation/keychain.md +180 -0
- data/documentation/network.md +75 -0
- data/documentation/opcode.md +220 -0
- data/documentation/openssl.md +7 -0
- data/documentation/p2pkh.md +71 -0
- data/documentation/p2sh.md +64 -0
- data/documentation/proof_of_work.md +84 -0
- data/documentation/script.md +280 -0
- data/documentation/signature.md +71 -0
- data/documentation/transaction.md +213 -0
- data/documentation/transaction_builder.md +188 -0
- data/documentation/transaction_input.md +133 -0
- data/documentation/transaction_output.md +130 -0
- data/documentation/wif.md +72 -0
- data/documentation/wire_format.md +70 -0
- data/lib/btcruby/address.rb +296 -0
- data/lib/btcruby/base58.rb +108 -0
- data/lib/btcruby/big_number.rb +47 -0
- data/lib/btcruby/block.rb +170 -0
- data/lib/btcruby/block_header.rb +231 -0
- data/lib/btcruby/constants.rb +59 -0
- data/lib/btcruby/currency_formatter.rb +64 -0
- data/lib/btcruby/data.rb +98 -0
- data/lib/btcruby/diagnostics.rb +92 -0
- data/lib/btcruby/errors.rb +8 -0
- data/lib/btcruby/extensions.rb +65 -0
- data/lib/btcruby/hash_functions.rb +54 -0
- data/lib/btcruby/hash_id.rb +18 -0
- data/lib/btcruby/key.rb +517 -0
- data/lib/btcruby/keychain.rb +464 -0
- data/lib/btcruby/network.rb +73 -0
- data/lib/btcruby/opcode.rb +197 -0
- data/lib/btcruby/open_assets/asset.rb +35 -0
- data/lib/btcruby/open_assets/asset_address.rb +49 -0
- data/lib/btcruby/open_assets/asset_definition.rb +75 -0
- data/lib/btcruby/open_assets/asset_id.rb +24 -0
- data/lib/btcruby/open_assets/asset_marker.rb +94 -0
- data/lib/btcruby/open_assets/asset_processor.rb +377 -0
- data/lib/btcruby/open_assets/asset_transaction.rb +184 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/errors.rb +15 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/provider.rb +32 -0
- data/lib/btcruby/open_assets/asset_transaction_builder/result.rb +47 -0
- data/lib/btcruby/open_assets/asset_transaction_builder.rb +418 -0
- data/lib/btcruby/open_assets/asset_transaction_input.rb +64 -0
- data/lib/btcruby/open_assets/asset_transaction_output.rb +140 -0
- data/lib/btcruby/open_assets.rb +26 -0
- data/lib/btcruby/openssl.rb +536 -0
- data/lib/btcruby/proof_of_work.rb +110 -0
- data/lib/btcruby/safety.rb +26 -0
- data/lib/btcruby/script.rb +733 -0
- data/lib/btcruby/signature_hashtype.rb +37 -0
- data/lib/btcruby/transaction.rb +511 -0
- data/lib/btcruby/transaction_builder/errors.rb +15 -0
- data/lib/btcruby/transaction_builder/provider.rb +54 -0
- data/lib/btcruby/transaction_builder/result.rb +73 -0
- data/lib/btcruby/transaction_builder/signer.rb +28 -0
- data/lib/btcruby/transaction_builder.rb +520 -0
- data/lib/btcruby/transaction_input.rb +298 -0
- data/lib/btcruby/transaction_outpoint.rb +30 -0
- data/lib/btcruby/transaction_output.rb +315 -0
- data/lib/btcruby/version.rb +3 -0
- data/lib/btcruby/wif.rb +118 -0
- data/lib/btcruby/wire_format.rb +362 -0
- data/lib/btcruby.rb +44 -2
- data/sample_code/creating_a_p2sh_multisig_address.rb +21 -0
- data/sample_code/creating_a_transaction_manually.rb +44 -0
- data/sample_code/generating_an_address.rb +20 -0
- data/sample_code/using_transaction_builder.rb +49 -0
- data/spec/address_spec.rb +206 -0
- data/spec/all.rb +6 -0
- data/spec/base58_spec.rb +83 -0
- data/spec/block_header_spec.rb +18 -0
- data/spec/block_spec.rb +18 -0
- data/spec/currency_formatter_spec.rb +46 -0
- data/spec/data_spec.rb +50 -0
- data/spec/diagnostics_spec.rb +41 -0
- data/spec/key_spec.rb +205 -0
- data/spec/keychain_spec.rb +261 -0
- data/spec/network_spec.rb +48 -0
- data/spec/open_assets/asset_address_spec.rb +33 -0
- data/spec/open_assets/asset_id_spec.rb +15 -0
- data/spec/open_assets/asset_marker_spec.rb +47 -0
- data/spec/open_assets/asset_processor_spec.rb +567 -0
- data/spec/open_assets/asset_transaction_builder_spec.rb +273 -0
- data/spec/open_assets/asset_transaction_spec.rb +70 -0
- data/spec/proof_of_work_spec.rb +53 -0
- data/spec/script_spec.rb +66 -0
- data/spec/spec_helper.rb +8 -0
- data/spec/transaction_builder_spec.rb +338 -0
- data/spec/transaction_spec.rb +162 -0
- data/spec/wire_format_spec.rb +283 -0
- metadata +141 -7
@@ -0,0 +1,213 @@
|
|
1
|
+
[Index](index.md)
|
2
|
+
|
3
|
+
BTC::Transaction
|
4
|
+
================
|
5
|
+
|
6
|
+
**Transaction** (aka "tx") is an object that represents transfer of bitcoins
|
7
|
+
from one or more [inputs](transaction_input.md) to one or more [outputs](transaction_output.md).
|
8
|
+
Use `BTC::Transaction` class to inspect transactions or create them transactions manually.
|
9
|
+
To build transaction we recommend using [BTC::TransactionBuilder](transaction_builder.md),
|
10
|
+
which takes care of a lot of difficulties and exposes easy to use, yet powerful enough API.
|
11
|
+
|
12
|
+
Transactions are stored within [blocks](block.md) that form the *block chain*.
|
13
|
+
|
14
|
+
The first transaction in a [block](block.md) is called *coinbase transaction*.
|
15
|
+
It has no inputs and the outputs contain collected mining fees and the mining reward (25 BTC per block as of March 2015).
|
16
|
+
|
17
|
+
Constants
|
18
|
+
---------
|
19
|
+
|
20
|
+
#### CURRENT_VERSION
|
21
|
+
|
22
|
+
Current version for all transactions. Equals `1`.
|
23
|
+
|
24
|
+
#### DEFAULT\_FEE\_RATE
|
25
|
+
|
26
|
+
Default mining fee rate in satoshis per 1000 bytes. Equals `10000`.
|
27
|
+
|
28
|
+
|
29
|
+
Initializers
|
30
|
+
------------
|
31
|
+
|
32
|
+
#### new(hex: *String*)
|
33
|
+
|
34
|
+
Returns a new transaction initialized with a hex-encoded string in [wire format](wire_format.md).
|
35
|
+
Raises `ArgumentError` if transaction is incomplete or incorrectly encoded.
|
36
|
+
|
37
|
+
#### new(data: *String*)
|
38
|
+
|
39
|
+
Returns a new transaction initialized with a binary string in [wire format](wire_format.md).
|
40
|
+
Raises `ArgumentError` if transaction is incomplete or incorrectly encoded.
|
41
|
+
|
42
|
+
#### new(stream: *IO*)
|
43
|
+
|
44
|
+
Returns a new transaction initialized with data in [wire format](wire_format.md) read from a given stream.
|
45
|
+
Raises `ArgumentError` if transaction is incomplete or incorrectly encoded.
|
46
|
+
|
47
|
+
#### new(*attributes*)
|
48
|
+
|
49
|
+
Returns a new transaction with named [attributes](#attributes).
|
50
|
+
All attributes are optional and have appropriate default values.
|
51
|
+
|
52
|
+
```ruby
|
53
|
+
Transaction.new(
|
54
|
+
version: 1,
|
55
|
+
inputs: [...],
|
56
|
+
outputs: [...],
|
57
|
+
lock_time: 0,
|
58
|
+
block_height: 319238,
|
59
|
+
...
|
60
|
+
)
|
61
|
+
```
|
62
|
+
|
63
|
+
Attributes
|
64
|
+
----------
|
65
|
+
|
66
|
+
#### version
|
67
|
+
|
68
|
+
Version of the transaction. Default value is `BTC::Transaction::CURRENT_VERSION`.
|
69
|
+
|
70
|
+
#### inputs
|
71
|
+
|
72
|
+
List of [TransactionInput](transaction_input.md) objects. See also `add_input` and `remove_all_inputs`.
|
73
|
+
|
74
|
+
#### outputs
|
75
|
+
|
76
|
+
List of [TransactionOutput](transaction_output.md) objects. See also `add_output` and `remove_all_outputs`.
|
77
|
+
|
78
|
+
#### lock_time
|
79
|
+
|
80
|
+
Lock time makes transaction not spendable until a designated time in the future.
|
81
|
+
Contains either a block height or a unix timestamp.
|
82
|
+
If this value is below [LOCKTIME_THRESHOLD](constants.md),
|
83
|
+
then it is treated as a block height. Default value is `0`.
|
84
|
+
|
85
|
+
Optional Attributes
|
86
|
+
-------------------
|
87
|
+
|
88
|
+
These are not derived from transaction binary data, but set from some other source.
|
89
|
+
|
90
|
+
#### block_hash
|
91
|
+
|
92
|
+
Binary hash of the block at which transaction was included.
|
93
|
+
If not confirmed or not available, equals `nil`.
|
94
|
+
|
95
|
+
Read-write. Default value is `nil`.
|
96
|
+
|
97
|
+
#### block_id
|
98
|
+
|
99
|
+
Hex big-endian hash of the block at which transaction was included.
|
100
|
+
See [Hash↔ID Conversion](hash_id.md).
|
101
|
+
If not confirmed or not available, equals `nil`.
|
102
|
+
|
103
|
+
Read-write. Default value is `nil`.
|
104
|
+
|
105
|
+
#### block_height
|
106
|
+
|
107
|
+
Height of the block at which transaction was included. If not confirmed equals `-1`.
|
108
|
+
Note: `block_height` might not be provided by some APIs while `confirmations` may be.
|
109
|
+
|
110
|
+
Read-write. Default value is `nil`.
|
111
|
+
|
112
|
+
#### block_time
|
113
|
+
|
114
|
+
Time of the block at which tx was included (`Time` instance or `nil`).
|
115
|
+
|
116
|
+
Read-write. Default value is `nil`.
|
117
|
+
|
118
|
+
#### confirmations
|
119
|
+
|
120
|
+
Number of confirmations for this transaction (depth in the blockchan).
|
121
|
+
Value `0` stands for unconfirmed transaction (stored in mempool).
|
122
|
+
|
123
|
+
Read-write. Default value is `nil`.
|
124
|
+
|
125
|
+
#### fee
|
126
|
+
|
127
|
+
If available, returns mining fee paid by this transaction.
|
128
|
+
If set, `inputs_amount` is updated as (`outputs_amount` + `fee`).
|
129
|
+
|
130
|
+
Read-write. Default value is `nil`.
|
131
|
+
|
132
|
+
#### inputs_amount
|
133
|
+
|
134
|
+
If available, returns total amount of all inputs.
|
135
|
+
If set, `fee` is updated as (`inputs_amount` - `outputs_amount`).
|
136
|
+
|
137
|
+
Read-write. Default value is `nil`.
|
138
|
+
|
139
|
+
#### outputs_amount
|
140
|
+
|
141
|
+
Total amount on all outputs (not including fees).
|
142
|
+
Always available because [outputs](transaction_output.md) contain their amounts.
|
143
|
+
|
144
|
+
Read-only.
|
145
|
+
|
146
|
+
|
147
|
+
Instance Methods
|
148
|
+
----------------
|
149
|
+
|
150
|
+
#### transaction_hash
|
151
|
+
|
152
|
+
32-byte transaction hash identifying the transaction.
|
153
|
+
|
154
|
+
#### transaction_id
|
155
|
+
|
156
|
+
Hex big-endian hash of the transaction. See [Hash↔ID Conversion](hash_id.md).
|
157
|
+
|
158
|
+
#### data
|
159
|
+
|
160
|
+
Binary representation of the transaction in [wire format](wire_format.md) (aka payload).
|
161
|
+
|
162
|
+
#### dictionary
|
163
|
+
|
164
|
+
Dictionary representation of transaction ready to be encoded in JSON, PropertyList etc.
|
165
|
+
|
166
|
+
#### coinbase?
|
167
|
+
|
168
|
+
Returns `true` if this transaction generates new coins.
|
169
|
+
|
170
|
+
#### signature\_hash(input\_index: *Integer*, output\_script: *BTC::Script*, hash\_type: *Integer*)
|
171
|
+
|
172
|
+
Returns a binary hash for signing a transaction (see [BTC::Key#ecdsa_signature](key.md#ecdsa_signaturehash)).
|
173
|
+
You should specify an input index, output [script](script.md) of the previous transaction for that input,
|
174
|
+
and an optional [hash type](signature.md) (default is `SIGHASH_ALL`).
|
175
|
+
|
176
|
+
#### to_s
|
177
|
+
|
178
|
+
Returns a hex representation of the transaction `data`.
|
179
|
+
|
180
|
+
#### to_hex
|
181
|
+
|
182
|
+
Returns a hex representation of the transaction `data`.
|
183
|
+
|
184
|
+
#### dup
|
185
|
+
|
186
|
+
Returns a complete copy of a transaction (each input and output is also copied via `dup`).
|
187
|
+
|
188
|
+
#### ==
|
189
|
+
|
190
|
+
Returns `true` if both transactions have equal binary representation.
|
191
|
+
|
192
|
+
#### add_input(*input*)
|
193
|
+
|
194
|
+
Adds a [BTC::TransactionInput](transaction_input.md) instance to a list of `inputs`.
|
195
|
+
After being added, input will have its `transaction` attribute set to the receiver.
|
196
|
+
|
197
|
+
Returns `self`.
|
198
|
+
|
199
|
+
#### add_output(*output*)
|
200
|
+
|
201
|
+
Adds a [BTC::TransactionOutput](transaction_output.md) instance to a list of `outputs`.
|
202
|
+
After being added, output will have its `transaction` attribute set to the receiver.
|
203
|
+
|
204
|
+
Returns `self`.
|
205
|
+
|
206
|
+
#### remove\_all\_inputs
|
207
|
+
|
208
|
+
Removes all [inputs](transaction_input.md) from the transaction and returns `self`.
|
209
|
+
|
210
|
+
#### remove\_all\_outputs
|
211
|
+
|
212
|
+
Removes all [outputs](transaction_output.md) from the transaction and returns `self`.
|
213
|
+
|
@@ -0,0 +1,188 @@
|
|
1
|
+
[Index](index.md)
|
2
|
+
|
3
|
+
BTC::TransactionBuilder
|
4
|
+
=======================
|
5
|
+
|
6
|
+
Transaction Builder allows building complex transactions in a convenient and safe manner.
|
7
|
+
It handles logic of selecting unspent outputs, calculates mining fees and computes amount for the change output.
|
8
|
+
|
9
|
+
Example
|
10
|
+
-------
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
# 0. Create an instance of TransactionBuilder.
|
14
|
+
builder = TransactionBuilder.new
|
15
|
+
|
16
|
+
# 1. Provide a list of addresses or WIF objects to get unspent outputs from.
|
17
|
+
builder.input_addresses = [ "L1uyy5qTuGrVXrmrsvHWHgVzW9kKdrp27wBC7Vs6nZDTF2BRUVwy" ]
|
18
|
+
|
19
|
+
# 2. Use a local UTXO index or Chain.com API to fetch unspent outputs for the input addresses.
|
20
|
+
builder.unspent_outputs_provider_block = lambda do |addresses, outputs_amount, outputs_size, fee_rate|
|
21
|
+
return array of BTC::TransactionOutput instances...
|
22
|
+
end
|
23
|
+
|
24
|
+
# 3. Specify payment address and amount
|
25
|
+
builder.outputs = [
|
26
|
+
TransactionOutput.new(value: 10_000, script: Address.parse("17XBj6iFEsf8kzDMGQk5ghZipxX49VXuaV").script)
|
27
|
+
]
|
28
|
+
|
29
|
+
# 4. Specify the change address
|
30
|
+
builder.change_address = Address.parse("1CBtcGivXmHQ8ZqdPgeMfcpQNJrqTrSAcG")
|
31
|
+
|
32
|
+
# 5. Build the transaction and broadcast it.
|
33
|
+
result = builder.build
|
34
|
+
tx = result.transaction
|
35
|
+
puts tx.to_hex
|
36
|
+
|
37
|
+
# => 01000000018689302ea03ef5dd56fb7940a867f9240fa811eddeb0fa4c87ad9ff3728f5e11
|
38
|
+
# 000000006b483045022100e280f71106a84a4a1b1a2035eae70266eb53630beab2b59cc...
|
39
|
+
```
|
40
|
+
|
41
|
+
Result Attributes
|
42
|
+
-----------------
|
43
|
+
|
44
|
+
Transaction Builder's `build` method returns `BTC::TransactionBuilderResult` instance with the following attributes:
|
45
|
+
|
46
|
+
#### transaction
|
47
|
+
|
48
|
+
[BTC::Transaction](transaction.md) instance. Each input is either signed (if WIF was used)
|
49
|
+
or contains an unspent output's script as its signature_script.
|
50
|
+
|
51
|
+
Unsigned inputs are marked using `unsigned_input_indexes` attribute.
|
52
|
+
|
53
|
+
#### unsigned\_input\_indexes
|
54
|
+
|
55
|
+
List of input indexes that are not signed.
|
56
|
+
Empty list means all indexes are signed.
|
57
|
+
|
58
|
+
#### fee
|
59
|
+
|
60
|
+
Total fee for the composed transaction.
|
61
|
+
Equals `inputs_amount` - `outputs_amount`.
|
62
|
+
|
63
|
+
#### inputs_amount
|
64
|
+
|
65
|
+
Total amount on the inputs.
|
66
|
+
|
67
|
+
#### outputs_amount
|
68
|
+
|
69
|
+
Total amount on the outputs.
|
70
|
+
|
71
|
+
#### change_amount
|
72
|
+
|
73
|
+
Amount in satoshis sent to a change address.
|
74
|
+
Equals `outputs_amount` - `sent_amount`.
|
75
|
+
|
76
|
+
#### sent_amount
|
77
|
+
|
78
|
+
Amount in satoshis sent to outputs specified by the user.
|
79
|
+
Equals `outputs_amount` - `change_amount`.
|
80
|
+
|
81
|
+
|
82
|
+
Attributes
|
83
|
+
----------
|
84
|
+
|
85
|
+
#### input_addresses
|
86
|
+
|
87
|
+
Addresses from which to fetch the inputs. Could be base58-encoded addresses or [WIF](wif.md) strings, or [BTC::Address](address.md) instances.
|
88
|
+
|
89
|
+
If any address is a WIF (string or `BTC::WIF` instance), the corresponding input will be
|
90
|
+
automatically signed with its private key using `SIGHASH_ALL` [hashtype](signature.md).
|
91
|
+
|
92
|
+
Otherwise, the `signature_script` in the input will be set to the output script from unspent output and index of that input will be added to `unsigned_input_indexes`.
|
93
|
+
|
94
|
+
#### unspent_outputs
|
95
|
+
|
96
|
+
Actual available [BTC::TransactionOutputs](transaction_output.md) to spend.
|
97
|
+
If not specified, builder will fetch and remember unspent outputs
|
98
|
+
using `unspent_outputs_provider_block`.
|
99
|
+
Only necessary inputs will be selected for spending.
|
100
|
+
|
101
|
+
If [TransactionOutput#confirmations](transaction_output.md#confirmations) attribute is not `nil`, outputs are sorted
|
102
|
+
from oldest to newest, unless `keep_unspent_outputs_order` is set to `true`.
|
103
|
+
|
104
|
+
#### unspent\_outputs\_provider\_block
|
105
|
+
|
106
|
+
Data-providing block with signature lambda{|addresses, outputs_amount, outputs_size, fee_rate| [...] }
|
107
|
+
|
108
|
+
* `addresses` is a list of BTC::Address instances.
|
109
|
+
* `outputs_amount` is a total amount in satoshis to be spent in all outputs (not including change output).
|
110
|
+
* `outputs_size` is a total size of all outputs in bytes (including change output).
|
111
|
+
* `fee_rate` is a miner's fee per 1000 bytes.
|
112
|
+
|
113
|
+
Block returns an array of unspent BTC::TransactionOutput instances with non-nil #transaction_hash and #index.
|
114
|
+
|
115
|
+
Note: data provider may or may not use additional parameters as a hint
|
116
|
+
to select the best matching unspent outputs. If it takes into account these parameters,
|
117
|
+
it is responsible to provide enough unspent outputs to cover the resulting fee.
|
118
|
+
|
119
|
+
If `outputs_amount` is 0, all possible unspent outputs are expected to be returned.
|
120
|
+
|
121
|
+
#### outputs
|
122
|
+
|
123
|
+
An array of `BTC::TransactionOutput` instances determining how many coins to spend and how.
|
124
|
+
If the array is empty, all unspent outputs are spent to the change address.
|
125
|
+
|
126
|
+
#### change_address
|
127
|
+
|
128
|
+
Change address (base58-encoded string or `BTC::Address`).
|
129
|
+
Must be specified, but may not be used if change is too small.
|
130
|
+
|
131
|
+
#### fee_rate
|
132
|
+
|
133
|
+
Miner's fee per kilobyte (1000 bytes).
|
134
|
+
Default is `BTC::Transaction::DEFAULT_FEE_RATE`.
|
135
|
+
|
136
|
+
#### minimum_change
|
137
|
+
|
138
|
+
Minimum amount of change below which transaction is not composed.
|
139
|
+
If change amount is non-zero and below this value, more unspent outputs are used.
|
140
|
+
If change amount is zero, change output is not even created and this attribute is not used.
|
141
|
+
Default value equals `fee_rate`.
|
142
|
+
|
143
|
+
#### dust_change
|
144
|
+
|
145
|
+
Amount of change that can be forgone as a mining fee if there are no more
|
146
|
+
unspent outputs available. If equals zero, no amount is allowed to be forgone.
|
147
|
+
|
148
|
+
Default value equals `minimum_change`. This means builder will never fail with "insufficient funds" just because it could not
|
149
|
+
find enough unspents for big enough change. In worst case it will forgo the change
|
150
|
+
as a part of the mining fee. Set to 0 to avoid wasting a single satoshi.
|
151
|
+
|
152
|
+
#### keep\_unspent\_outputs\_order
|
153
|
+
|
154
|
+
If true, does not sort `unspent_outputs` by confirmations number.
|
155
|
+
Default is `false`, but order will be preserved if `confirmations` attribute is `nil`.
|
156
|
+
|
157
|
+
|
158
|
+
Instance Method
|
159
|
+
---------------
|
160
|
+
|
161
|
+
#### build
|
162
|
+
|
163
|
+
Attempts to build a transaction and returns an instance of `BTC::TransactionBuilderResult`.
|
164
|
+
|
165
|
+
Raises a subclass of `TransactionBuilderError` exception.
|
166
|
+
|
167
|
+
Errors
|
168
|
+
------
|
169
|
+
|
170
|
+
#### TransactionBuilderError < BTCError
|
171
|
+
|
172
|
+
A base class for all errors used by Transaction Builder.
|
173
|
+
|
174
|
+
#### TransactionBuilderMissingChangeAddressError < TransactionBuilderError
|
175
|
+
|
176
|
+
Change address is not specified.
|
177
|
+
|
178
|
+
#### TransactionBuilderMissingUnspentOutputsError < TransactionBuilderError
|
179
|
+
|
180
|
+
Unspent outputs are missing. Maybe because input_addresses are not specified.
|
181
|
+
|
182
|
+
#### TransactionBuilderInsufficientFundsError < TransactionBuilderError
|
183
|
+
|
184
|
+
Unspent outputs are not sufficient to build the transaction.
|
185
|
+
|
186
|
+
|
187
|
+
|
188
|
+
|
@@ -0,0 +1,133 @@
|
|
1
|
+
[Index](index.md)
|
2
|
+
|
3
|
+
BTC::TransactionInput
|
4
|
+
=====================
|
5
|
+
|
6
|
+
Transaction Input (aka "txin") is a part of a bitcoin [transaction](transaction.md) that
|
7
|
+
unlocks bitcoins stored in the [outputs](transaction_output.md) of the previous transactions.
|
8
|
+
|
9
|
+
Every input contains a reference to some output (transaction hash and a numeric index of the output)
|
10
|
+
and a [signature script](script.md) that typically contains [signatures](key.md) and other data
|
11
|
+
to satisfy conditions defined by the corresponding output script.
|
12
|
+
|
13
|
+
Constants
|
14
|
+
---------
|
15
|
+
|
16
|
+
#### INVALID_INDEX
|
17
|
+
|
18
|
+
Invalid index is used in *coinbase* inputs. Equals `0xFFFFFFFF` (`(unsigned int) -1`).
|
19
|
+
|
20
|
+
#### MAX_SEQUENCE
|
21
|
+
|
22
|
+
Equals `0xFFFFFFFF`.
|
23
|
+
|
24
|
+
#### ZERO_HASH256
|
25
|
+
|
26
|
+
Equals all-zero 32-byte binary string.
|
27
|
+
|
28
|
+
|
29
|
+
Initializers
|
30
|
+
------------
|
31
|
+
|
32
|
+
#### new(data: *String*)
|
33
|
+
|
34
|
+
Returns a new transaction input initialized with a binary string in [wire format](wire_format.md).
|
35
|
+
Raises `ArgumentError` if transaction input is incomplete or incorrectly encoded.
|
36
|
+
|
37
|
+
#### new(stream: *IO*)
|
38
|
+
|
39
|
+
Returns a new transaction input initialized with data in [wire format](wire_format.md) read from a given stream.
|
40
|
+
Raises `ArgumentError` if transaction input is incomplete or incorrectly encoded.
|
41
|
+
|
42
|
+
#### new(*attributes*)
|
43
|
+
|
44
|
+
Returns a new transaction input with named [attributes](#attributes).
|
45
|
+
All attributes are optional and have appropriate default values.
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
TransactionInput.new(
|
49
|
+
previous_id: "d21633ba23f70118185227be58a63527675641ad37967e2aa461559f577aec43",
|
50
|
+
previous_index: 0,
|
51
|
+
signature_script: Script.new << script_signature << pubkey,
|
52
|
+
...
|
53
|
+
)
|
54
|
+
```
|
55
|
+
|
56
|
+
Attributes
|
57
|
+
----------
|
58
|
+
|
59
|
+
#### previous_hash
|
60
|
+
|
61
|
+
Binary hash of the previous transaction.
|
62
|
+
|
63
|
+
#### previous_id
|
64
|
+
|
65
|
+
Hex big-endian hash of the previous transaction. See [Hash↔ID Conversion](hash_id.md).
|
66
|
+
|
67
|
+
#### previous_index
|
68
|
+
|
69
|
+
Index of the previous transaction's output (uint32).
|
70
|
+
Default value is `INVALID_INDEX`.
|
71
|
+
|
72
|
+
#### signature_script
|
73
|
+
|
74
|
+
[BTC::Script](script.md) instance that proves ownership of the previous transaction output.
|
75
|
+
We intentionally do not call it "script" to avoid accidental confusion with
|
76
|
+
[TransactionOutput#script](transaction_output.md#script).
|
77
|
+
|
78
|
+
For *coinase* inputs use `coinbase_data` instead.
|
79
|
+
|
80
|
+
#### coinbase_data
|
81
|
+
|
82
|
+
Binary string contained in place of `signature_script` in a coinbase input (see `coinbase?`).
|
83
|
+
|
84
|
+
Returns `nil` if it is not a coinbase input.
|
85
|
+
|
86
|
+
#### sequence
|
87
|
+
|
88
|
+
Input sequence (uint32_t). Default is maximum value 0xFFFFFFFF.
|
89
|
+
Sequence is used to update a timelocked tx stored in memory of the nodes. It is only relevant when tx lock_time > 0.
|
90
|
+
Currently, for DoS and security reasons, nodes do not store timelocked transactions making the sequence number meaningless.
|
91
|
+
|
92
|
+
|
93
|
+
Optional Attributes
|
94
|
+
-------------------
|
95
|
+
|
96
|
+
These are not derived from transaction input’s binary data, but set from some other source.
|
97
|
+
|
98
|
+
#### transaction
|
99
|
+
|
100
|
+
Optional reference to the owning transaction. It is set on [tx.add_input](transaction.md#add_inputinput) and
|
101
|
+
reset to `nil` on [tx.remove_all_inputs](transaction.md#remove_all_inputs).
|
102
|
+
|
103
|
+
Default value is `nil`.
|
104
|
+
|
105
|
+
#### transaction_output
|
106
|
+
|
107
|
+
Optional reference to an [output](transaction_output.md) that this input is spending.
|
108
|
+
|
109
|
+
#### value
|
110
|
+
|
111
|
+
Optional value in the corresponding output (in satoshis).
|
112
|
+
|
113
|
+
Default value is `transaction_output.value` or `nil`.
|
114
|
+
|
115
|
+
|
116
|
+
Instance Methods
|
117
|
+
----------------
|
118
|
+
|
119
|
+
#### data
|
120
|
+
|
121
|
+
Binary representation of the transaction input in [wire format](wire_format.md) (aka payload).
|
122
|
+
|
123
|
+
#### dictionary
|
124
|
+
|
125
|
+
Dictionary representation of transaction input ready to be encoded in JSON, PropertyList etc.
|
126
|
+
|
127
|
+
#### coinbase?
|
128
|
+
|
129
|
+
Returns `true` if this transaction input belongs to a *coinbase* transaction.
|
130
|
+
Coinbase input has no `signature_script` and its `index` equals `INVALID_INDEX`.
|
131
|
+
|
132
|
+
|
133
|
+
|
@@ -0,0 +1,130 @@
|
|
1
|
+
[Index](index.md)
|
2
|
+
|
3
|
+
BTC::TransactionOutput
|
4
|
+
======================
|
5
|
+
|
6
|
+
Transaction Output (aka "txout") is a part of a bitcoin [transaction](transaction.md) that specifies
|
7
|
+
destination of the bitcoins being transferred.
|
8
|
+
|
9
|
+
Every output has `value` (in satoshis) and `script` (that typically corresponds to an [address](address.md)).
|
10
|
+
|
11
|
+
This class is frequently used to specify **unspent outputs** (aka "unspents").
|
12
|
+
Unspent outputs typically have optional attributes `transaction_hash` and `index` to allow
|
13
|
+
creating a corresponding [input](transaction_input.md).
|
14
|
+
|
15
|
+
Initializers
|
16
|
+
------------
|
17
|
+
|
18
|
+
#### new(data: *String*)
|
19
|
+
|
20
|
+
Returns a new transaction output initialized with a binary string in [wire format](wire_format.md).
|
21
|
+
Raises `ArgumentError` if transaction output is incomplete or incorrectly encoded.
|
22
|
+
|
23
|
+
#### new(stream: *IO*)
|
24
|
+
|
25
|
+
Returns a new transaction output initialized with data in [wire format](wire_format.md) read from a given stream.
|
26
|
+
Raises `ArgumentError` if transaction output is incomplete or incorrectly encoded.
|
27
|
+
|
28
|
+
#### new(*attributes*)
|
29
|
+
|
30
|
+
Returns a new transaction output with named [attributes](#attributes).
|
31
|
+
All attributes are optional and have appropriate default values.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
TransactionOutput.new(
|
35
|
+
value: 42 * BTC::COIN,
|
36
|
+
script: Address.parse("1CBtcGivXmHQ8ZqdPgeMfcpQNJrqTrSAcG").script,
|
37
|
+
...
|
38
|
+
)
|
39
|
+
```
|
40
|
+
|
41
|
+
Attributes
|
42
|
+
----------
|
43
|
+
|
44
|
+
#### value
|
45
|
+
|
46
|
+
Amount in satoshis to be locked in this output.
|
47
|
+
|
48
|
+
#### script
|
49
|
+
|
50
|
+
An instance of [BTC::Script](script.md) that specifies conditions that allow spending bitcoins.
|
51
|
+
|
52
|
+
Typically corresponds to a recipient [address](address.md):
|
53
|
+
|
54
|
+
```ruby
|
55
|
+
Address.parse("1CBtcGivXmHQ8ZqdPgeMfcpQNJrqTrSAcG").script
|
56
|
+
```
|
57
|
+
|
58
|
+
Optional Attributes
|
59
|
+
-------------------
|
60
|
+
|
61
|
+
These are not derived from transaction outputs’s binary data, but set from some other source.
|
62
|
+
|
63
|
+
#### transaction
|
64
|
+
|
65
|
+
Reference to the owning transaction. It is set on [tx.add_output](transaction.md#add_outputoutput) and
|
66
|
+
reset to `nil` in [tx.remove_all_outputs](transaction.md#remove_all_outputs). Default value is `nil`.
|
67
|
+
|
68
|
+
#### transaction_hash
|
69
|
+
|
70
|
+
Binary hash of the containing transaction. Default value is `nil`.
|
71
|
+
|
72
|
+
#### transaction_id
|
73
|
+
|
74
|
+
Hex big-endian hash of the containing transaction. See [Hash↔ID Conversion](hash_id.md).
|
75
|
+
|
76
|
+
#### index
|
77
|
+
|
78
|
+
Index of this output in the containing transaction. Default value is `nil`.
|
79
|
+
|
80
|
+
#### block_hash
|
81
|
+
|
82
|
+
Binary hash of the block at which containing transaction was included.
|
83
|
+
If not confirmed or not available, equals `nil`.
|
84
|
+
|
85
|
+
#### block_id
|
86
|
+
|
87
|
+
Hex big-endian hash corresponding to `block_hash`. See [Hash↔ID Conversion](hash_id.md).
|
88
|
+
|
89
|
+
#### block_height
|
90
|
+
|
91
|
+
Height of the block at which containing transaction was included.
|
92
|
+
If not confirmed equals `-1`.
|
93
|
+
|
94
|
+
Note: `block_height` might not be provided by some APIs while `confirmations` may be.
|
95
|
+
Default value is derived from `transaction` if possible or equals `nil`.
|
96
|
+
|
97
|
+
#### block_time
|
98
|
+
|
99
|
+
Time of the block at which containing transaction was included (`Time` instance or `nil`).
|
100
|
+
Default value is derived from `transaction` if possible or equals `nil`.
|
101
|
+
|
102
|
+
#### confirmations
|
103
|
+
|
104
|
+
Number of confirmations. Default value is derived from `transaction` if possible or equals `nil`.
|
105
|
+
|
106
|
+
#### spent
|
107
|
+
|
108
|
+
If available, returns whether this output is spent (`true` or `false`).
|
109
|
+
Default is `nil`. See also `spent_confirmations`.
|
110
|
+
|
111
|
+
#### spent_confirmations
|
112
|
+
|
113
|
+
If the containing transaction is spent, contains number of confirmations of the spending transaction.
|
114
|
+
|
115
|
+
Returns `nil` if not available or output is not spent.
|
116
|
+
|
117
|
+
Returns `0` if spending transaction is not confirmed.
|
118
|
+
|
119
|
+
|
120
|
+
Instance Methods
|
121
|
+
----------------
|
122
|
+
|
123
|
+
#### data
|
124
|
+
|
125
|
+
Binary representation of the transaction output in [wire format](wire_format.md) (aka payload).
|
126
|
+
|
127
|
+
#### dictionary
|
128
|
+
|
129
|
+
Dictionary representation of transaction output ready to be encoded in JSON, PropertyList etc.
|
130
|
+
|