NAME
CBitcoin::Transaction - A wrapper for transactions.
- dl_load_flags
-
Nothing to see here.
- new
-
use CBitcoin; use CBitcoin::Transaction; $CBitcoin::network_bytes = CBitcoin::TESTNET; $CBitcoin::chain = CBitcoin::CHAIN_LEGACY; my $tx = CBitcoin::Transaction->new( 'inputs' => \@txinputs, 'outputs' => \@txoutputs );
- deserialize
-
my $tx = CBitcoin::Transaction->deserialize($serialized_tx,\@scriptPubs,\@amounts); die "failed to parse" unless defined $tx;
If deserializing a raw transaction, please provide @scriptPubs that correspond with the inputs. If you are on the UAHF chain, then you need to also provide the amounts (in satoshi) that correspond to all inputs.
- lockTime
-
Returns the lockTime on the transaction.
- version
- hash
-
Returns the double sha256 hash of the transaction, which also functions a sort of transaction id. However, please do not use this as an id when accounting for real money.
- hash_type $tx-hash_type(SIGHASH_ALL) >
-
This hash is unrelated to the hash() subroutine. This one is related to what parts of the transaction do signatures correspond to. There is no need to mess with this subroutine unless you know what you are doing.
- flag_type
-
Similar to hash_type(), this sorts out what flags are used when evaluating input scripts. This is used in validate_sigs().
Handling Inputs/Outputs
Inputs contain the contract language that determines who has the right to claim the funds stored on the input balance. The outputs contain the addresses and amounts being spent. The addresses are typically references to the contract, not the contract itself. The contract (in P2SH scripts) are referred to as redeem scripts.
- $tx-add_redeem_script($input_index,$script)>
-
For p2sh transaction inputs, you need the redeem script in order to claim the funds. Use this subroutine to add the redeem script to the corresponding transaction input. The redeem script must be in human readable format (deserialized), not serialized binary format.
- add_output($tx_out)
-
Do not use this subroutine. All outputs have to be supplied in the constructor.
- randomize()
-
To preserve privacy, randomize outputs so people cannot gues which output is change and which is sending money. Do not use this subroutine directly.
- numOfInputs
-
Returns the number of inputs in the transaction.
- $tx-input(2)>
-
Returns the transaction input corresponding to the index number in the argument.
- numOfOutputs
-
Returns the number of transaction outputs.
- $tx-output(2) >
-
Returns the transaction output corresponding to the index number in the argument.
- $tx-serialize($raw_bool,$flush_bool)>
-
Raw means there are no script sigs. Flush means to force reserialization (includes script sigs).
To get a serialized raw transaction: my $rawtx = $tx->serialize(1);
Do not use this subroutine to get a fully signed transaction.
- validate
-
https://en.bitcoin.it/w/images/en/7/70/Bitcoin_OpCheckSig_InDetail.png
This subroutine is obsolete.
- $tx-validate_sigs($txdata) >
-
https://en.bitcoin.it/w/images/en/7/70/Bitcoin_OpCheckSig_InDetail.png
SCRIPT_VERIFY_NONE -> 1 SCRIPT_VERIFY_STRICTENC -> 2 SCRIPT_VERIFY_P2SH -> 3 SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC -> 4
Validate a serialized transaction. The $txdata is a product of signing a transaction. We need to deserialize the transaction first and add in the script pubs (the contract language) for the transaction inputs.
- $tx-assemble_multisig_p2sh($i,$n,$txraw,@keys)>
-
The keys are in binary form, as is the transaction data. The following is an example of how to use it.
# see CBitcoin::CBHD; my $root = CBitcoin::CBHD->generate(); # set the chain $CBitcoin::chain = CBitcoin::CHAIN_UAHF; my @keys = ( $root->deriveChild(1,1),$root->deriveChild(1,2) ); # the prevout hash. the prevout index is below. # got these from a block explorer, but we have to reverse the bytes my @hashes = ( '6105e342232a9e67e4fa4ee0651eb8efd146dc0d7d346c788f45d8ad591c4577' ); # multisig_p2sh_script($m,$n,@pubksy) my $multisig_input = CBitcoin::Script::multisig_p2sh_script(2,2, $root->deriveChild(1,1)->publickey(), $root->deriveChild(1,2)->publickey() ); my $p2sh_input = CBitcoin::Script::script_to_p2sh($multisig_input); my @ins = ( # input amount = 0.01394 BTC CBitcoin::TransactionInput->new({ 'prevOutHash' => pack('H*',join('',reverse($hashes[0] =~ m/([[:xdigit:]]{2})/g) ) ) ,'prevOutIndex' => 1 ,'script' => $p2sh_input ,'input_amount' => int(0.01394 * 100_000_000) }), ); my $balance = int( (0.01394) * 100_000_000); my $fee = int(0.0001 * 100_000_000); my @outs = ( CBitcoin::TransactionOutput->new({ 'script' => CBitcoin::Script::address_to_script($root->deriveChild(1,3)->address()) ,'value' => ($balance - $fee) }) ); my $tx = CBitcoin::Transaction->new({ 'inputs' => \@ins, 'outputs' => \@outs }); # need the redeem script in order to do the signature. $tx->add_redeem_script(0,$multisig_input); my $txdata = $tx->assemble_multisig_p2sh( 0 ,2 # total number of pub keys ,undef ,$root->deriveChild(1,1),$root->deriveChild(1,2) ); ok($tx->validate_sigs($txdata),'good tx with multisig uahf');
- my $txdata = $tx-assemble_p2pkh($i,$key)>
-
With a CBHD $key, sign a transaction input. The serialized transaction including the signature is returned. This subroutine is for pay to public key hash scripts.
- my $txdata = $tx-assemble_p2p($i,$key)>
-
With a CBHD $key, sign a transaction input. The serialized transaction including the signature is returned. This subroutine is for pay to public key scripts. P2P scripts are usually found in coinbase transactions. P2PKH is normally.
- push_data($data)-$adddata >
- txfee($size)
-
The default is 70 satoshis/byte.
$CBitcoin::Transaction::tx_fee = 70;
SYNOPSIS
use CBitcoin;
use CBitcoin::Transaction;
AUTHOR
Joel De Jesus, <dejesus.joel at e-flamingo.jp>
BUGS
Please report any bugs or feature requests to https://github.com/favioflamingo/libcbitcoin-perl. I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.
SUPPORT
You can find documentation for this module with the perldoc command.
perldoc CBitcoin::Transaction
You can also look for information at: https://github.com/favioflamingo/libcbitcoin-perl
ACKNOWLEDGEMENTS
LICENSE AND COPYRIGHT
Copyright 2014-2017 Joel De Jesus.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
9 POD Errors
The following errors were encountered while parsing the POD:
- Around line 56:
'=item' outside of any '=over'
- Around line 85:
Unknown directive: =header1
- Around line 326:
You forgot a '=back' before '=head2'
- Around line 330:
'=item' outside of any '=over'
- Around line 417:
Unknown directive: =header2
- Around line 537:
Unknown directive: =header2
- Around line 723:
Unknown directive: =header2
- Around line 774:
You forgot a '=back' before '=head1'
- Around line 803:
You forgot a '=back' before '=head1'