Skip to content

Latest commit

 

History

History
213 lines (125 loc) · 5.8 KB

transaction.md

File metadata and controls

213 lines (125 loc) · 5.8 KB

Index

BTC::Transaction

Transaction (aka "tx") is an object that represents transfer of bitcoins from one or more inputs to one or more outputs. Use BTC::Transaction class to inspect transactions or create them transactions manually. To build transaction we recommend using BTC::TransactionBuilder, which takes care of a lot of difficulties and exposes easy to use, yet powerful enough API.

Transactions are stored within blocks that form the block chain.

The first transaction in a block is called coinbase transaction. It has no inputs and the outputs contain collected mining fees and the mining reward (25 BTC per block as of March 2015).

Constants

CURRENT_VERSION

Current version for all transactions. Equals 1.

DEFAULT_FEE_RATE

Default mining fee rate in satoshis per 1000 bytes. Equals 10000.

Initializers

new(hex: String)

Returns a new transaction initialized with a hex-encoded string in wire format. Raises ArgumentError if transaction is incomplete or incorrectly encoded.

new(data: String)

Returns a new transaction initialized with a binary string in wire format. Raises ArgumentError if transaction is incomplete or incorrectly encoded.

new(stream: IO)

Returns a new transaction initialized with data in wire format read from a given stream. Raises ArgumentError if transaction is incomplete or incorrectly encoded.

new(attributes)

Returns a new transaction with named attributes. All attributes are optional and have appropriate default values.

Transaction.new(
  version: 1,
  inputs: [...],
  outputs: [...],
  lock_time: 0,
  block_height: 319238,
  ...
)

Attributes

version

Version of the transaction. Default value is BTC::Transaction::CURRENT_VERSION.

inputs

List of TransactionInput objects. See also add_input and remove_all_inputs.

outputs

List of TransactionOutput objects. See also add_output and remove_all_outputs.

lock_time

Lock time makes transaction not spendable until a designated time in the future. Contains either a block height or a unix timestamp. If this value is below LOCKTIME_THRESHOLD, then it is treated as a block height. Default value is 0.

Optional Attributes

These are not derived from transaction binary data, but set from some other source.

block_hash

Binary hash of the block at which transaction was included. If not confirmed or not available, equals nil.

Read-write. Default value is nil.

block_id

Hex big-endian hash of the block at which transaction was included. See Hash↔ID Conversion. If not confirmed or not available, equals nil.

Read-write. Default value is nil.

block_height

Height of the block at which transaction was included. If not confirmed equals -1. Note: block_height might not be provided by some APIs while confirmations may be.

Read-write. Default value is nil.

block_time

Time of the block at which tx was included (Time instance or nil).

Read-write. Default value is nil.

confirmations

Number of confirmations for this transaction (depth in the blockchan). Value 0 stands for unconfirmed transaction (stored in mempool).

Read-write. Default value is nil.

fee

If available, returns mining fee paid by this transaction. If set, inputs_amount is updated as (outputs_amount + fee).

Read-write. Default value is nil.

inputs_amount

If available, returns total amount of all inputs. If set, fee is updated as (inputs_amount - outputs_amount).

Read-write. Default value is nil.

outputs_amount

Total amount on all outputs (not including fees). Always available because outputs contain their amounts.

Read-only.

Instance Methods

transaction_hash

32-byte transaction hash identifying the transaction.

transaction_id

Hex big-endian hash of the transaction. See Hash↔ID Conversion.

data

Binary representation of the transaction in wire format (aka payload).

dictionary

Dictionary representation of transaction ready to be encoded in JSON, PropertyList etc.

coinbase?

Returns true if this transaction generates new coins.

signature_hash(input_index: Integer, output_script: BTC::Script, hash_type: Integer)

Returns a binary hash for signing a transaction (see BTC::Key#ecdsa_signature). You should specify an input index, output script of the previous transaction for that input, and an optional hash type (default is SIGHASH_ALL).

to_s

Returns a hex representation of the transaction data.

to_hex

Returns a hex representation of the transaction data.

dup

Returns a complete copy of a transaction (each input and output is also copied via dup).

==

Returns true if both transactions have equal binary representation.

add_input(input)

Adds a BTC::TransactionInput instance to a list of inputs. After being added, input will have its transaction attribute set to the receiver.

Returns self.

add_output(output)

Adds a BTC::TransactionOutput instance to a list of outputs. After being added, output will have its transaction attribute set to the receiver.

Returns self.

remove_all_inputs

Removes all inputs from the transaction and returns self.

remove_all_outputs

Removes all outputs from the transaction and returns self.