Documentation
¶
Index ¶
- Constants
- Variables
- func CheckDecode(input string) (result []byte, version []byte, err error)
- func CheckEncode(input []byte, version []byte) string
- func DecodeAddress(addr string, defaultNet *chaincfg.Params) (btcutil.Address, error)
- func EstimateSerializeSize(inputCount int, txOuts []Output, addChangeOutput bool, inputType InputType) int
- func ExtractPkScriptAddrs(pkScript []byte, chainParams *chaincfg.Params) (btcutil.Address, error)
- func KeyToAddress(key *hd.ExtendedKey, params *chaincfg.Params) (btcutil.Address, error)
- func KeysToAddresses(params *chaincfg.Params, keys []*hd.ExtendedKey) (addrs []btcutil.Address, err error)
- func MoneyRange(v int64) bool
- func NewCoin(txid []byte, index uint32, value btc.Amount, numConfs int64, ...) coinset.Coin
- func OutpointsEqual(a, b wire.OutPoint) bool
- func PayToAddrScript(addr btcutil.Address) ([]byte, error)
- func ProduceSignature(params *chaincfg.Params, tx *Transaction, idx int, pkScript []byte, ...) ([]byte, error)
- func PushAll(scripts [][]byte) ([]byte, error)
- func Sign1(address btc.Address, creator SignatureCreator, scriptCode []byte, ...) ([]byte, bool)
- func SignN(params *chaincfg.Params, multisigdata [][]byte, creator SignatureCreator, ...) ([][]byte, bool)
- func SignStep(params *chaincfg.Params, creator SignatureCreator, scriptPubKey []byte, ...) ([][]byte, txscript.ScriptClass, bool)
- func SignatureHash(scriptCode []byte, tx *Transaction, idx int, hashType txscript.SigHashType, ...) ([]byte, error)
- func SumOutputSerializeSizes(outputs []Output) (serializeSize int)
- type AddressPubKeyHash
- type AddressScriptHash
- type Coin
- type Input
- type InputSource
- type InputType
- type JoinSplit
- type NetID
- type Output
- type SignatureCreator
- type Transaction
- func (t *Transaction) GetHeader() uint32
- func (t *Transaction) IsCoinBase() bool
- func (t *Transaction) IsEqual(other *Transaction) bool
- func (t *Transaction) MarshalBinary() ([]byte, error)
- func (t *Transaction) ReadFrom(r io.Reader) (n int64, err error)
- func (t *Transaction) Sort()
- func (t *Transaction) TxHash() chainhash.Hash
- func (t *Transaction) UnmarshalBinary(data []byte) error
- func (tx *Transaction) Validate(params *chaincfg.Params) error
- func (t *Transaction) WriteTo(w io.Writer) (n int64, err error)
- type TxStore
Constants ¶
const ( MaxBlockBaseSize = 2000000 NumJoinSplitInputs = 2 NumJoinSplitOutputs = 2 SproutVersionGroupID uint32 = 0 OverwinterFlagMask uint32 = 0x80000000 OverwinterVersionGroupID = 0x03C48270 TxExpiryHeightThreshold uint32 = 500000000 SproutMinCurrentVersion uint32 = 1 SproutMaxCurrentVersion = 2 OverwinterMinCurrentVersion = 3 OverwinterMaxCurrentVersion = 3 )
const ( // RedeemP2PKHSigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a compressed P2PKH output. // It is calculated as: // // - OP_DATA_73 // - 72 bytes DER signature + 1 byte sighash // - OP_DATA_33 // - 33 bytes serialized compressed pubkey RedeemP2PKHSigScriptSize = 1 + 73 + 1 + 33 // RedeemP2SHMultisigSigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a 2 of 3 P2SH multisig output with compressed keys. // It is calculated as: // // - OP_0 // - OP_DATA_72 // - 72 bytes DER signature // - OP_DATA_72 // - 72 bytes DER signature // - OP_PUSHDATA // - OP_2 // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP3 // - OP_CHECKMULTISIG RedeemP2SH2of3MultisigSigScriptSize = 1 + 1 + 72 + 1 + 72 + 1 + 1 + 1 + 33 + 1 + 33 + 1 + 33 + 1 + 1 // RedeemP2SH1of2MultisigSigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a 1 of 2 P2SH multisig output with compressed keys. // It is calculated as: // // - OP_0 // - OP_DATA_72 // - 72 bytes DER signature // - OP_PUSHDATA // - OP_1 // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP2 // - OP_CHECKMULTISIG RedeemP2SH1of2MultisigSigScriptSize = 1 + 1 + 72 + 1 + 1 + 1 + 33 + 1 + 33 + 1 + 1 // RedeemP2SHMultisigTimelock1SigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a compressed P2SH timelocked multisig using the timeout. // It is calculated as: // // - OP_DATA_72 // - 72 bytes DER signature // - OP_0 // - OP_PUSHDATA // - OP_IF // - OP_2 // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP3 // - OP_CHECKMULTISIG // - OP_ELSE // - OP_PUSHDATA // - 2 byte block height // - OP_CHECKSEQUENCEVERIFY // - OP_DROP // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_CHECKSIG // - OP_ENDIF RedeemP2SHMultisigTimelock1SigScriptSize = 1 + 72 + 1 + 1 + 1 + 1 + 1 + 33 + 1 + 33 + 1 + 33 + 1 + 1 + 1 + 1 + 2 + 1 + 1 + 1 + 33 + 1 + 1 // RedeemP2SHMultisigTimelock2SigScriptSize is the worst case (largest) serialize size // of a transaction input script that redeems a compressed P2SH timelocked multisig without using the timeout. // It is calculated as: // // - OP_0 // - OP_DATA_72 // - 72 bytes DER signature // - OP_DATA_72 // - 72 bytes DER signature // - OP_1 // - OP_PUSHDATA // - OP_IF // - OP_2 // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP3 // - OP_CHECKMULTISIG // - OP_ELSE // - OP_PUSHDATA // - 2 byte block height // - OP_CHECKSEQUENCEVERIFY // - OP_DROP // - OP_DATA_33 // - 33 bytes serialized compressed pubkey // - OP_CHECKSIG // - OP_ENDIF RedeemP2SHMultisigTimelock2SigScriptSize = 1 + 1 + 72 + +1 + 72 + 1 + 1 + 1 + 1 + 1 + 33 + 1 + 33 + 1 + 33 + 1 + 1 + 1 + 1 + 2 + 1 + 1 + 1 + 33 + 1 + 1 // P2PKHPkScriptSize is the size of a transaction output script that // pays to a compressed pubkey hash. It is calculated as: // // - OP_DUP // - OP_HASH160 // - OP_DATA_20 // - 20 bytes pubkey hash // - OP_EQUALVERIFY // - OP_CHECKSIG P2PKHPkScriptSize = 1 + 1 + 1 + 20 + 1 + 1 // RedeemP2PKHInputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed P2PKH output. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 1 byte script len // - signature script // - 4 bytes sequence RedeemP2PKHInputSize = 32 + 4 + 1 + RedeemP2PKHSigScriptSize + 4 // RedeemP2SH2of3MultisigInputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed P2SH 2 of 3 multisig output. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 1 byte script len // - 4 bytes sequence /// - witness discounted signature script RedeemP2SH2of3MultisigInputSize = 32 + 4 + 1 + 4 + (RedeemP2SH2of3MultisigSigScriptSize / 4) // RedeemP2SH1of2MultisigInputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed P2SH 2 of 3 multisig output. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 1 byte script len // - 4 bytes sequence /// - witness discounted signature script RedeemP2SH1of2MultisigInputSize = 32 + 4 + 1 + 4 + (RedeemP2SH1of2MultisigSigScriptSize / 4) // RedeemP2SHMultisigTimelock1InputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed p2sh timelocked multig output with using the timeout. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 1 byte script len // - 4 bytes sequence /// - witness discounted signature script RedeemP2SHMultisigTimelock1InputSize = 32 + 4 + 1 + 4 + (RedeemP2SHMultisigTimelock1SigScriptSize / 4) // RedeemP2SHMultisigTimelock2InputSize is the worst case (largest) serialize size of a // transaction input redeeming a compressed P2SH timelocked multisig output without using the timeout. It is // calculated as: // // - 32 bytes previous tx // - 4 bytes output index // - 1 byte script len // - 4 bytes sequence /// - witness discounted signature script RedeemP2SHMultisigTimelock2InputSize = 32 + 4 + 1 + 4 + (RedeemP2SHMultisigTimelock2SigScriptSize / 4) // P2PKHOutputSize is the serialize size of a transaction output with a // P2PKH output script. It is calculated as: // // - 8 bytes output value // - 1 byte compact int encoding value 25 // - 25 bytes P2PKH output script P2PKHOutputSize = 8 + 1 + P2PKHPkScriptSize )
Worst case script and input/output size estimates.
const NotAnInput int = -1
Special case nIn for signing JoinSplits.
Variables ¶
var ( // ErrChecksumMismatch describes an error where decoding failed due // to a bad checksum. ErrChecksumMismatch = errors.New("checksum mismatch") // ErrUnknownAddressType describes an error where an address can not // decoded as a specific address type due to the string encoding // begining with an identifier byte unknown to any standard or // registered (via chaincfg.Register) network. ErrUnknownAddressType = errors.New("unknown address type") // ErrAddressCollision describes an error where an address can not // be uniquely determined as either a pay-to-pubkey-hash or // pay-to-script-hash address since the leading identifier is used for // describing both address kinds, but for different networks. Rather // than assuming or defaulting to one or the other, this error is // returned and the caller must decide how to decode the address. ErrAddressCollision = errors.New("address collision") // ErrInvalidFormat describes an error where decoding failed due to invalid version ErrInvalidFormat = errors.New("invalid format: version and/or checksum bytes missing") NetIDs = map[string]NetID{ chaincfg.MainNetParams.Name: NetID{[]byte{0x1c, 0xb8}, []byte{0x1c, 0xbd}, []byte{0x16, 0x9a}}, chaincfg.TestNet3Params.Name: NetID{[]byte{0x1d, 0x25}, []byte{0x1c, 0xba}, []byte{0x16, 0xb6}}, chaincfg.RegressionNetParams.Name: NetID{[]byte{0x1d, 0x25}, []byte{0x1c, 0xba}, []byte{0x16, 0xb6}}, } )
var ( PrevoutsHashPersonalization = []byte("ZcashPrevoutHash") SequenceHashPersonalization = []byte("ZcashSequencHash") OutputsHashPersonalization = []byte("ZcashOutputsHash") JoinSplitsHashPersonalization = []byte("ZcashJSplitsHash") )
var ( ErrOverwinterTxVersionTooLow = fmt.Errorf("overwinter transaction version too low") ErrUnknownTxVersionGroupID = fmt.Errorf("transaction has unknown version group id") ErrTxExpiryHeightIsTooHigh = fmt.Errorf("transaction expiry height is too high") ErrTxVersionTooLow = fmt.Errorf("transaction version too low") ErrTxVersionTooHigh = fmt.Errorf("transaction version too high") ErrNoTxInputs = fmt.Errorf("transaction has no inputs") ErrNoTxOutputs = fmt.Errorf("transaction has no outputs") ErrDuplicateTxInputs = fmt.Errorf("transaction contains duplicate inputs") ErrDuplicateTxNullifiers = fmt.Errorf("transaction contains duplicate nullifiers") ErrPrevOutIsNull = fmt.Errorf("transaction input refers to null previous output") ErrCoinBaseTxHasJoinSplits = fmt.Errorf("coinbase transaction has joinsplits") ErrCoinBaseTxHasOutputs = fmt.Errorf("coinbase transaction has outputs") )
Functions ¶
func CheckDecode ¶
CheckDecode decodes a string that was encoded with CheckEncode and verifies the checksum.
func CheckEncode ¶
CheckEncode prepends a version byte and appends a four byte checksum.
func DecodeAddress ¶
DecodeAddress decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type.
The zcash network the address is associated with is extracted if possible.
func EstimateSerializeSize ¶
func EstimateSerializeSize(inputCount int, txOuts []Output, addChangeOutput bool, inputType InputType) int
EstimateSerializeSize is reused from spvwallet and modified to be less btc-specific
EstimateSerializeSize returns a worst case serialize size estimate for a signed transaction that spends inputCount number of compressed P2PKH outputs and contains each transaction output from txOuts. The estimated size is incremented for an additional P2PKH change output if addChangeOutput is true.
TODO: Include joinsplits in the size estimate
func ExtractPkScriptAddrs ¶
ExtractPkScriptAddrs returns the type of script, addresses and required signatures associated with the passed PkScript. Note that it only works for 'standard' transaction script types. Any data such as public keys which are invalid are omitted from the results.
func KeyToAddress ¶
func KeysToAddresses ¶
func MoneyRange ¶
func OutpointsEqual ¶
func PayToAddrScript ¶
PayToAddrScript creates a new script to pay a transaction output to a the specified address.
func ProduceSignature ¶
func ProduceSignature( params *chaincfg.Params, tx *Transaction, idx int, pkScript []byte, hashType txscript.SigHashType, kdb txscript.KeyDB, sdb txscript.ScriptDB, previousScript []byte, ) ([]byte, error)
TODO: Support pre-overwinter v2 joinsplit transactions here (maybe)
func SignStep ¶
func SignStep(params *chaincfg.Params, creator SignatureCreator, scriptPubKey []byte, consensusBranchId uint32) ([][]byte, txscript.ScriptClass, bool)
*
- Sign scriptPubKey using signature made with creator.
- Signatures are returned in scriptSigRet (or returns false if scriptPubKey can't be signed),
- unless scriptClass is txscript.ScriptHashTy, in which case scriptSigRet is the redemption script.
- Returns false if scriptPubKey could not be completely satisfied.
func SignatureHash ¶
func SignatureHash(scriptCode []byte, tx *Transaction, idx int, hashType txscript.SigHashType, consensusBranchId uint32) ([]byte, error)
func SumOutputSerializeSizes ¶
SumOutputSerializeSizes is reused from spvwallet and modified to be less btc-specific
SumOutputSerializeSizes sums up the serialized size of the supplied outputs.
Types ¶
type AddressPubKeyHash ¶
type AddressPubKeyHash struct {
// contains filtered or unexported fields
}
AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.
func NewAddressPubKeyHash ¶
func NewAddressPubKeyHash(pkHash []byte, net *chaincfg.Params) (*AddressPubKeyHash, error)
NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.
func (*AddressPubKeyHash) EncodeAddress ¶
func (a *AddressPubKeyHash) EncodeAddress() string
EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.
func (*AddressPubKeyHash) Hash160 ¶
func (a *AddressPubKeyHash) Hash160() *[ripemd160.Size]byte
Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).
func (*AddressPubKeyHash) IsForNet ¶
func (a *AddressPubKeyHash) IsForNet(net *chaincfg.Params) bool
IsForNet returns whether or not the pay-to-pubkey-hash address is associated with the passed zcash network.
func (*AddressPubKeyHash) ScriptAddress ¶
func (a *AddressPubKeyHash) ScriptAddress() []byte
ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.
func (*AddressPubKeyHash) String ¶
func (a *AddressPubKeyHash) String() string
String returns a human-readable string for the pay-to-pubkey-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
type AddressScriptHash ¶
type AddressScriptHash struct {
// contains filtered or unexported fields
}
AddressScriptHash is an Address for a pay-to-script-hash (P2SH) transaction.
func NewAddressScriptHash ¶
func NewAddressScriptHash(serializedScript []byte, net *chaincfg.Params) (*AddressScriptHash, error)
NewAddressScriptHash returns a new AddressScriptHash.
func NewAddressScriptHashFromHash ¶
func NewAddressScriptHashFromHash(scriptHash []byte, net *chaincfg.Params) (*AddressScriptHash, error)
NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.
func (*AddressScriptHash) EncodeAddress ¶
func (a *AddressScriptHash) EncodeAddress() string
EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.
func (*AddressScriptHash) Hash160 ¶
func (a *AddressScriptHash) Hash160() *[ripemd160.Size]byte
Hash160 returns the underlying array of the script hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).
func (*AddressScriptHash) IsForNet ¶
func (a *AddressScriptHash) IsForNet(net *chaincfg.Params) bool
IsForNet returns whether or not the pay-to-script-hash address is associated with the passed zcash network.
func (*AddressScriptHash) ScriptAddress ¶
func (a *AddressScriptHash) ScriptAddress() []byte
ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.
func (*AddressScriptHash) String ¶
func (a *AddressScriptHash) String() string
String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
type Coin ¶
type InputSource ¶
type JoinSplit ¶
type JoinSplit struct { // A value v_{pub}^{old} that the JoinSplit transfer removes from the // transparent value pool. VPubOld uint64 // A value v_{pub}^{new} that the JoinSplit transfer inserts into the // transparent value pool. VPubNew uint64 // A merkle root of the note commitment tree at some block height in the // past, or the merkle root produced by a previous JoinSplit transfer in this // transaction. // // JoinSplits are always anchored to a root in the note commitment tree at // some point in the blockchain history or in the history of the current // transaction. Anchor [32]byte // A sequence of nullifiers of the input notes $nf$_{1..N^{old}}^{old} // // Nullifiers are used to prevent double-spends. They are derived from the // secrets placed in the note and the secret spend-authority key known by the // spender. Nullifiers [NumJoinSplitInputs][32]byte // A sequence of note commitments for the output notes $cm$_{1..N^{new}}^{new} // // Note commitments are introduced into the commitment tree, blinding the // public about the values and destinations involved in the JoinSplit. The // presence of a commitment in the note commitment tree is required to spend // it. Commitments [NumJoinSplitOutputs][32]byte // A Curve25519 public key epk. EphemeralKey [32]byte // A 256-bit seed that must be chosen independently at random for each // JoinSplit description. RandomSeed [32]byte // A sequence of message authentication tags h_{1..N^{old}} that bind h^{Sig} // to each a_{sk} of the JoinSplit description. // // The verification of the JoinSplit requires these MACs to be provided as an // input. Macs [NumJoinSplitInputs][32]byte // An encoding of the zero-knowledge proof \pi_{ZKJoinSplit} // // This is a zk-SNARK which ensures that this JoinSplit is valid. Proof [296]byte // A sequence of ciphertext components for the encrypted output notes, // C_{1..N^{new}}^{enc} // // These contain trapdoors, values and other information that the recipient // needs, including a memo field. It is encrypted using the scheme // implemented in crypto/NoteEncryption.cpp Ciphertexts [NumJoinSplitOutputs][601]byte }
type Output ¶
func (*Output) SerializeSize ¶
SerializeSize returns the number of bytes it would take to serialize the the transaction output.
type SignatureCreator ¶
type SignatureCreator interface { CreateSig(address btc.Address, scriptCode []byte, consensusBranchId uint32) ([]byte, bool) txscript.KeyDB txscript.ScriptDB }
func TransactionSignatureCreator ¶
func TransactionSignatureCreator(kdb txscript.KeyDB, sdb txscript.ScriptDB, tx *Transaction, idx int, hashType txscript.SigHashType) SignatureCreator
type Transaction ¶
type Transaction struct { IsOverwinter bool Version uint32 VersionGroupID uint32 Inputs []Input Outputs []Output LockTime uint32 ExpiryHeight uint32 ValueBalance int64 TemporaryUnknownValue uint16 JoinSplits []JoinSplit JoinSplitPubKey [32]byte JoinSplitSignature [64]byte }
func NewUnsignedTransaction ¶
func NewUnsignedTransaction(outputs []Output, feePerKb btc.Amount, fetchInputs InputSource, fetchChange txauthor.ChangeSource, isOverwinter bool) (*Transaction, error)
NewUnsignedTransaction is reused from spvwallet and modified to be less btc-specific
func (*Transaction) GetHeader ¶
func (t *Transaction) GetHeader() uint32
func (*Transaction) IsCoinBase ¶
func (t *Transaction) IsCoinBase() bool
IsCoinBase determines whether or not a transaction is a coinbase. A coinbase is a special transaction created by miners that has no inputs. This is represented in the block chain by a transaction with a single input that has a previous output transaction index set to the maximum value along with a zero hash.
func (*Transaction) IsEqual ¶
func (t *Transaction) IsEqual(other *Transaction) bool
func (*Transaction) MarshalBinary ¶
func (t *Transaction) MarshalBinary() ([]byte, error)
func (*Transaction) Sort ¶
func (t *Transaction) Sort()
Sort in-place sorts the inputs and outputs for BIP 69
func (*Transaction) TxHash ¶
func (t *Transaction) TxHash() chainhash.Hash
TxHash generates the Hash for the transaction.
func (*Transaction) UnmarshalBinary ¶
func (t *Transaction) UnmarshalBinary(data []byte) error
type TxStore ¶
type TxStore struct { wallet.Datastore // contains filtered or unexported fields }
func NewTxStore ¶
func (*TxStore) CheckDoubleSpends ¶
func (ts *TxStore) CheckDoubleSpends(argTx *Transaction) ([]*chainhash.Hash, error)
GetDoubleSpends takes a transaction and compares it with all transactions in the db. It returns a slice of all txids in the db which are double spent by the received tx.
func (*TxStore) PopulateAdrs ¶
PopulateAdrs just puts a bunch of adrs in ram; it doesn't touch the DB