Class: Bitcoin::Tx
- Inherits:
-
Object
- Object
- Bitcoin::Tx
- Includes:
- HexConverter, SilentPayment
- Defined in:
- lib/bitcoin/tx.rb
Overview
Transaction class
Constant Summary collapse
- MAX_STANDARD_VERSION =
2- MAX_STANDARD_TX_WEIGHT =
The maximum weight for transactions we’re willing to relay/mine
400000- MARKER =
0x00- FLAG =
0x01
Instance Attribute Summary collapse
-
#flag ⇒ Object
Returns the value of attribute flag.
-
#inputs ⇒ Object
(also: #in)
readonly
Returns the value of attribute inputs.
-
#lock_time ⇒ Object
Returns the value of attribute lock_time.
-
#marker ⇒ Object
Returns the value of attribute marker.
-
#outputs ⇒ Object
(also: #out)
readonly
Returns the value of attribute outputs.
-
#version ⇒ Object
Returns the value of attribute version.
Class Method Summary collapse
-
.create_coinbase(msg, script, rewards = 50 * 100000000) ⇒ Bitcoin::Tx
Create coinbase transaction.
- .parse_from_payload(payload, non_witness: false, strict: false) ⇒ Object
Instance Method Summary collapse
- #==(other) ⇒ Object
- #coinbase_tx? ⇒ Boolean
- #hash ⇒ Object
-
#initialize ⇒ Tx
constructor
A new instance of Tx.
-
#serialize_old_format ⇒ Object
serialize tx with old tx format.
-
#serialize_witness_format ⇒ Object
serialize tx with segwit tx format github.com/bitcoin/bips/blob/master/bip-0144.mediawiki.
-
#sighash_for_input(input_index, output_script = nil, opts: {}, hash_type: , sig_version: :base, amount: nil, skip_separator_index: 0, prevouts: []) ⇒ String
get signature hash the script code needs is the witnessScript but removing everything up to and including the last executed OP_CODESEPARATOR before the signature checking opcode being executed.
-
#size ⇒ Object
The serialized transaction size.
-
#standard? ⇒ Boolean
check this tx is standard.
- #to_h ⇒ Object
- #to_payload ⇒ Object
- #tx_hash ⇒ Object
- #txid ⇒ Object
-
#valid? ⇒ Boolean
Verify transaction validity.
-
#verify_input_sig(input_index, script_pubkey, amount: nil, flags: STANDARD_SCRIPT_VERIFY_FLAGS, prevouts: []) ⇒ Boolean
verify input signature.
-
#vsize ⇒ Object
The virtual transaction size (differs from size for witness transactions).
-
#weight ⇒ Object
calculate tx weight weight = (legacy tx payload) * 3 + (witness tx payload).
- #witness? ⇒ Boolean
-
#witness_commitment ⇒ Object
get the witness commitment of coinbase tx.
- #witness_hash ⇒ Object
- #witness_payload ⇒ Object
- #wtxid ⇒ Object
Methods included from SilentPayment
#derive_payment_points, #extract_public_key
Methods included from HexConverter
Constructor Details
#initialize ⇒ Tx
Returns a new instance of Tx.
27 28 29 30 31 32 |
# File 'lib/bitcoin/tx.rb', line 27 def initialize @inputs = [] @outputs = [] @version = 1 @lock_time = 0 end |
Instance Attribute Details
#flag ⇒ Object
Returns the value of attribute flag.
22 23 24 |
# File 'lib/bitcoin/tx.rb', line 22 def flag @flag end |
#inputs ⇒ Object (readonly) Also known as: in
Returns the value of attribute inputs.
23 24 25 |
# File 'lib/bitcoin/tx.rb', line 23 def inputs @inputs end |
#lock_time ⇒ Object
Returns the value of attribute lock_time.
25 26 27 |
# File 'lib/bitcoin/tx.rb', line 25 def lock_time @lock_time end |
#marker ⇒ Object
Returns the value of attribute marker.
21 22 23 |
# File 'lib/bitcoin/tx.rb', line 21 def marker @marker end |
#outputs ⇒ Object (readonly) Also known as: out
Returns the value of attribute outputs.
24 25 26 |
# File 'lib/bitcoin/tx.rb', line 24 def outputs @outputs end |
#version ⇒ Object
Returns the value of attribute version.
20 21 22 |
# File 'lib/bitcoin/tx.rb', line 20 def version @version end |
Class Method Details
.create_coinbase(msg, script, rewards = 50 * 100000000) ⇒ Bitcoin::Tx
Create coinbase transaction.
42 43 44 45 46 47 48 |
# File 'lib/bitcoin/tx.rb', line 42 def self.create_coinbase(msg, script, rewards = 50 * 100000000) coinbase = Tx.new script_sig = Bitcoin::Script.new << 486604799 << "04" << msg.bth coinbase.in << TxIn.new(out_point: OutPoint.create_coinbase_outpoint, script_sig: script_sig) coinbase.out << TxOut.new(value: rewards, script_pubkey: script) coinbase end |
.parse_from_payload(payload, non_witness: false, strict: false) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/bitcoin/tx.rb', line 50 def self.parse_from_payload(payload, non_witness: false, strict: false) buf = payload.is_a?(String) ? StringIO.new(payload) : payload tx = new tx.version = buf.read(4).unpack1('V') in_count = Bitcoin.unpack_var_int_from_io(buf) has_witness = false if in_count.zero? && !non_witness tx.marker = 0 tx.flag = buf.read(1).unpack1('c') if tx.flag.zero? buf.pos -= 1 else in_count = Bitcoin.unpack_var_int_from_io(buf) has_witness = true end end in_count.times do tx.inputs << TxIn.parse_from_payload(buf) end out_count = Bitcoin.unpack_var_int_from_io(buf) out_count.times do tx.outputs << TxOut.parse_from_payload(buf) end if has_witness in_count.times do |i| tx.inputs[i].script_witness = Bitcoin::ScriptWitness.parse_from_payload(buf) end end raise ArgumentError, 'Transaction has unexpected data.' if strict && (buf.pos + 4) != buf.length tx.lock_time = buf.read(4).unpack1('V') tx end |
Instance Method Details
#==(other) ⇒ Object
131 132 133 |
# File 'lib/bitcoin/tx.rb', line 131 def ==(other) to_payload == other.to_payload end |
#coinbase_tx? ⇒ Boolean
123 124 125 |
# File 'lib/bitcoin/tx.rb', line 123 def coinbase_tx? inputs.length == 1 && inputs.first.coinbase? end |
#hash ⇒ Object
88 89 90 |
# File 'lib/bitcoin/tx.rb', line 88 def hash to_hex.to_i(16) end |
#serialize_old_format ⇒ Object
serialize tx with old tx format
136 137 138 139 140 141 142 |
# File 'lib/bitcoin/tx.rb', line 136 def serialize_old_format buf = [version].pack('V') buf << Bitcoin.pack_var_int(inputs.length) << inputs.map(&:to_payload).join buf << Bitcoin.pack_var_int(outputs.length) << outputs.map(&:to_payload).join buf << [lock_time].pack('V') buf end |
#serialize_witness_format ⇒ Object
serialize tx with segwit tx format github.com/bitcoin/bips/blob/master/bip-0144.mediawiki
146 147 148 149 150 151 152 |
# File 'lib/bitcoin/tx.rb', line 146 def serialize_witness_format buf = [version, MARKER, FLAG].pack('Vcc') buf << Bitcoin.pack_var_int(inputs.length) << inputs.map(&:to_payload).join buf << Bitcoin.pack_var_int(outputs.length) << outputs.map(&:to_payload).join buf << witness_payload << [lock_time].pack('V') buf end |
#sighash_for_input(input_index, output_script = nil, opts: {}, hash_type: , sig_version: :base, amount: nil, skip_separator_index: 0, prevouts: []) ⇒ String
get signature hash the script code needs is the witnessScript but removing everything up to and including the last executed OP_CODESEPARATOR before the signature checking opcode being executed.
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 |
# File 'lib/bitcoin/tx.rb', line 211 def sighash_for_input(input_index, output_script = nil, opts: {}, hash_type: SIGHASH_TYPE[:all], sig_version: :base, amount: nil, skip_separator_index: 0, prevouts: []) raise ArgumentError, 'input_index must be specified.' unless input_index raise ArgumentError, 'does not exist input corresponding to input_index.' if input_index >= inputs.size raise ArgumentError, 'script_pubkey must be specified.' if [:base, :witness_v0].include?(sig_version) && output_script.nil? opts[:amount] = amount if amount opts[:skip_separator_index] = skip_separator_index opts[:sig_version] = sig_version opts[:script_code] = output_script opts[:prevouts] = prevouts opts[:last_code_separator_pos] ||= 0xffffffff sig_hash_gen = SigHashGenerator.load(sig_version) sig_hash_gen.generate(self, input_index, hash_type, opts) end |
#size ⇒ Object
The serialized transaction size
182 183 184 |
# File 'lib/bitcoin/tx.rb', line 182 def size to_payload.bytesize end |
#standard? ⇒ Boolean
check this tx is standard.
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
# File 'lib/bitcoin/tx.rb', line 159 def standard? return false if version > MAX_STANDARD_VERSION return false if weight > MAX_STANDARD_TX_WEIGHT inputs.each do |i| # Biggest 'standard' txin is a 15-of-15 P2SH multisig with compressed keys (remember the 520 byte limit on redeemScript size). # That works out to a (15*(33+1))+3=513 byte redeemScript, 513+1+15*(73+1)+3=1627 # bytes of scriptSig, which we round off to 1650 bytes for some minor future-proofing. # That's also enough to spend a 20-of-20 CHECKMULTISIG scriptPubKey, though such a scriptPubKey is not considered standard. return false if i.script_sig.size > 1650 return false unless i.script_sig.push_only? end data_count = 0 outputs.each do |o| return false unless o.script_pubkey.standard? data_count += 1 if o.script_pubkey.op_return? # TODO add non P2SH multisig relay(permitbaremultisig) return false if o.dust? end return false if data_count > 1 true end |
#to_h ⇒ Object
252 253 254 255 256 257 |
# File 'lib/bitcoin/tx.rb', line 252 def to_h { txid: txid, hash: witness_hash.rhex, version: version, size: size, vsize: vsize, locktime: lock_time, vin: inputs.map(&:to_h), vout: outputs.map.with_index{|tx_out, index| tx_out.to_h.merge({n: index})} } end |
#to_payload ⇒ Object
119 120 121 |
# File 'lib/bitcoin/tx.rb', line 119 def to_payload witness? ? serialize_witness_format : serialize_old_format end |
#tx_hash ⇒ Object
92 93 94 |
# File 'lib/bitcoin/tx.rb', line 92 def tx_hash Bitcoin.double_sha256(serialize_old_format).bth end |
#txid ⇒ Object
96 97 98 |
# File 'lib/bitcoin/tx.rb', line 96 def txid tx_hash.rhex end |
#valid? ⇒ Boolean
Verify transaction validity.
261 262 263 264 265 |
# File 'lib/bitcoin/tx.rb', line 261 def valid? state = Bitcoin::ValidationState.new validation = Bitcoin::Validation.new validation.check_tx(self, state) && state.valid? end |
#verify_input_sig(input_index, script_pubkey, amount: nil, flags: STANDARD_SCRIPT_VERIFY_FLAGS, prevouts: []) ⇒ Boolean
verify input signature.
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/bitcoin/tx.rb', line 234 def verify_input_sig(input_index, script_pubkey, amount: nil, flags: STANDARD_SCRIPT_VERIFY_FLAGS, prevouts: []) script_sig = inputs[input_index].script_sig has_witness = inputs[input_index].has_witness? has_witness = true if script_pubkey.witness_program? if script_pubkey.p2sh? flags << SCRIPT_VERIFY_P2SH redeem_script = Script.parse_from_payload(script_sig.chunks.last) script_pubkey = redeem_script if redeem_script.p2wpkh? end if has_witness verify_input_sig_for_witness(input_index, script_pubkey, amount, flags, prevouts) else verify_input_sig_for_legacy(input_index, script_pubkey, flags) end end |
#vsize ⇒ Object
The virtual transaction size (differs from size for witness transactions)
187 188 189 |
# File 'lib/bitcoin/tx.rb', line 187 def vsize (weight.to_f / 4).ceil end |
#weight ⇒ Object
calculate tx weight weight = (legacy tx payload) * 3 + (witness tx payload)
193 194 195 196 197 198 199 |
# File 'lib/bitcoin/tx.rb', line 193 def weight if witness? serialize_old_format.bytesize * (WITNESS_SCALE_FACTOR - 1) + serialize_witness_format.bytesize else serialize_old_format.bytesize * WITNESS_SCALE_FACTOR end end |
#witness? ⇒ Boolean
127 128 129 |
# File 'lib/bitcoin/tx.rb', line 127 def witness? inputs.any?(&:has_witness?) end |
#witness_commitment ⇒ Object
get the witness commitment of coinbase tx. if this tx does not coinbase or not have commitment, return nil.
110 111 112 113 114 115 116 117 |
# File 'lib/bitcoin/tx.rb', line 110 def witness_commitment return nil unless coinbase_tx? outputs.each do |output| commitment = output.script_pubkey.witness_commitment return commitment if commitment end nil end |
#witness_hash ⇒ Object
100 101 102 |
# File 'lib/bitcoin/tx.rb', line 100 def witness_hash Bitcoin.double_sha256(to_payload).bth end |
#witness_payload ⇒ Object
154 155 156 |
# File 'lib/bitcoin/tx.rb', line 154 def witness_payload inputs.map { |i| i.script_witness.to_payload }.join end |
#wtxid ⇒ Object
104 105 106 |
# File 'lib/bitcoin/tx.rb', line 104 def wtxid witness_hash.rhex end |