transaction

package
v1.0.44 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 3, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

View Source
const (
	SighashAll          = 0x00000001
	SighashNone         = 0x00000002
	SighashSingle       = 0x00000003
	SighashForkID       = 0x00000040
	SighashAnyoneCanPay = 0x00000080
	SighashAllForkID    = (0x00000001 | 0x00000040)
)

Signature constants

Variables

This section is empty.

Functions

func GetSighashForInput

func GetSighashForInput(transaction *BitcoinTransaction, sighashType uint32, inputNumber uint32) string

GetSighashForInput function

Types

type BitcoinTransaction

type BitcoinTransaction struct {
	Bytes    []byte
	Version  uint32
	Witness  bool
	Inputs   []*Input
	Outputs  []*Output
	Locktime uint32
}

A BitcoinTransaction wraps a bitcoin transaction

func New

func New() *BitcoinTransaction

New creates a new (version 1) BitcoinTransaction.

func NewFromBytes

func NewFromBytes(bytes []byte) *BitcoinTransaction

NewFromBytes takes an array of bytes and constructs a BitcoinTransaction.

func NewFromBytesWithUsed

func NewFromBytesWithUsed(bytes []byte) (*BitcoinTransaction, int)

NewFromBytesWithUsed takes an array of bytes and constructs a BitcoinTransaction and returns the offset (length of tx).

func NewFromString

func NewFromString(str string) (*BitcoinTransaction, error)

NewFromString takes a hex string representation of a bitcoin transaction and returns a BitcoinTransaction object.

func (*BitcoinTransaction) AddInput

func (bt *BitcoinTransaction) AddInput(input *Input)

AddInput adds a new input to the transaction.

func (*BitcoinTransaction) AddOutput

func (bt *BitcoinTransaction) AddOutput(output *Output)

AddOutput adds a new output to the transaction.

func (*BitcoinTransaction) AddUTXO

func (bt *BitcoinTransaction) AddUTXO(txID string, vout uint32, script string, satoshis uint64) error

AddUTXO function

func (*BitcoinTransaction) ApplySignatures

func (bt *BitcoinTransaction) ApplySignatures(signingPayload *SigningPayload, sigType uint32) error

ApplySignatures applies the signatures passed in through SigningPayload parameter to the transaction inputs The signing payload from the signing service should contain a signing item for each of the tx inputs. If the TX input does not belong to us, its signature will be blank unless its owner has already signed it. If the signing payload contains a signature for a given input, we apply that to the tx regardless of whether we own it or not.

func (*BitcoinTransaction) ApplySignaturesWithoutP2PKHCheck

func (bt *BitcoinTransaction) ApplySignaturesWithoutP2PKHCheck(signingPayload *SigningPayload, sigType uint32) error

ApplySignaturesWithoutP2PKHCheck applies signatures without checking if the input previous script equals to a P2PKH script matching the private key (see func SignWithoutP2PKHCheck below)

func (*BitcoinTransaction) GetInputs

func (bt *BitcoinTransaction) GetInputs() []*Input

GetInputs returns an array of all inputs in the transaction.

func (*BitcoinTransaction) GetOutputs

func (bt *BitcoinTransaction) GetOutputs() []*Output

GetOutputs returns an array of all outputs in the transaction.

func (*BitcoinTransaction) GetSighashPayload

func (bt *BitcoinTransaction) GetSighashPayload(sigType uint32) (*SigningPayload, error)

GetSighashPayload assembles a payload of sighases for this TX, to be submitted to signing service.

func (*BitcoinTransaction) GetTxID

func (bt *BitcoinTransaction) GetTxID() string

GetTxID returns the transaction ID of the transaction (which is also the transaction hash).

func (*BitcoinTransaction) HasWitnessData

func (bt *BitcoinTransaction) HasWitnessData() bool

HasWitnessData returns true if the optional Witness flag == 0001

func (*BitcoinTransaction) Hex

func (bt *BitcoinTransaction) Hex() []byte

Hex encodes the transaction into a hex byte array. See https://chainquery.com/bitcoin-cli/decoderawtransaction

func (*BitcoinTransaction) HexWithClearedInputs

func (bt *BitcoinTransaction) HexWithClearedInputs(index int, scriptPubKey []byte) []byte

HexWithClearedInputs encodes the transaction into a hex byte array but clears its inputs first. This is used when signing transactions.

func (*BitcoinTransaction) InputCount

func (bt *BitcoinTransaction) InputCount() int

InputCount returns the number of transaction inputs.

func (*BitcoinTransaction) IsCoinbase

func (bt *BitcoinTransaction) IsCoinbase() bool

IsCoinbase determines if this transaction is a coinbase by seeing if any of the inputs have no inputs.

func (*BitcoinTransaction) OutputCount

func (bt *BitcoinTransaction) OutputCount() int

OutputCount returns the number of transaction inputs.

func (*BitcoinTransaction) PayTo

func (bt *BitcoinTransaction) PayTo(address string, amount uint64) error

PayTo function

func (*BitcoinTransaction) Sign

func (bt *BitcoinTransaction) Sign(privateKey *btcec.PrivateKey, sigType uint32) error

Sign the transaction Normally we'd expect the signing service to do this, but we include this for testing purposes

func (*BitcoinTransaction) SignWithoutP2PKHCheck

func (bt *BitcoinTransaction) SignWithoutP2PKHCheck(privateKey *btcec.PrivateKey, sigType uint32) error

SignWithoutP2PKHCheck signs the transaction without checking if the input previous script equals to a P2PKH script matching the private key

type Input

type Input struct {
	PreviousTxHash     [32]byte
	PreviousTxOutIndex uint32
	PreviousTxSatoshis uint64
	PreviousTxScript   *Script
	SigScript          *Script
	SequenceNumber     uint32
}

Input is a representation of a transaction input

func NewInput

func NewInput() *Input

NewInput creates a new Input object with a finalised sequence number.

func NewInputFromBytes

func NewInputFromBytes(bytes []byte) (*Input, int)

NewInputFromBytes returns a transaction input from the bytes provided.

func (*Input) Hex

func (i *Input) Hex(clear bool) []byte

Hex encodes the Input into a hex byte array.

func (*Input) String

func (i *Input) String() string

type Output

type Output struct {
	Value  uint64
	Script []byte
}

Output is a representation of a transaction output

func NewOutput

func NewOutput() *Output

NewOutput creates a new Output object.

func NewOutputForPublicKeyHash

func NewOutputForPublicKeyHash(publicKeyHash string, satoshis uint64) (*Output, error)

NewOutputForPublicKeyHash makes an output to a PKH with a value.

func NewOutputFromBytes

func NewOutputFromBytes(bytes []byte) (*Output, int)

NewOutputFromBytes returns a transaction Output from the bytes provided

func NewOutputOpReturn

func NewOutputOpReturn(data []byte) (*Output, error)

NewOutputOpReturn creates a new Output with OP_FALSE OP_RETURN and then the data passed in encoded as hex.

func NewOutputOpReturnPush

func NewOutputOpReturnPush(data [][]byte) (*Output, error)

NewOutputOpReturnPush creates a new Output with OP_FALSE OP_RETURN and then uses OP_PUSHDATA format to encode the multiple byte arrays passed in.

func (*Output) GetOutputScript

func (o *Output) GetOutputScript() []byte

GetOutputScript returns the script of the output

func (*Output) Hex

func (o *Output) Hex() []byte

Hex encodes the Output into a hex byte array.

func (*Output) String

func (o *Output) String() string

type RedeemScript

type RedeemScript struct {
	SignaturesRequired int
	PublicKeys         [][]byte
	Signatures         [][]byte
}

RedeemScript contains the metadata used when creating an unlocking script (SigScript) for a multisig output.

func NewRedeemScript

func NewRedeemScript(signaturesRequired int) (*RedeemScript, error)

NewRedeemScript creates a new RedeemScript with minimum signature threshold needed.

func NewRedeemScriptFromElectrum

func NewRedeemScriptFromElectrum(script string) (*RedeemScript, error)

NewRedeemScriptFromElectrum TODO:

func (*RedeemScript) AddPublicKey

func (rs *RedeemScript) AddPublicKey(pkey string, derivationPath []uint32) error

AddPublicKey appends a public key to the RedeemScript.

type Script

type Script []byte

Script type

func NewScript

func NewScript() *Script

NewScript creates a new Script.

func NewScriptFromBytes

func NewScriptFromBytes(b []byte) *Script

NewScriptFromBytes wraps a byte slice with the Script type.

func NewScriptFromString

func NewScriptFromString(s string) *Script

NewScriptFromString creates a new script from a hex encoded string.

func (*Script) AppendPushDataToScript

func (s *Script) AppendPushDataToScript(d []byte)

AppendPushDataToScript take data bytes and appends the length (see VarInt) and the data to the script.

func (*Script) GetPublicKeyHash

func (s *Script) GetPublicKeyHash() ([]byte, error)

GetPublicKeyHash returns a public key hash byte array if the script is a P2PKH script

func (*Script) IsMultisigOut

func (s *Script) IsMultisigOut() bool

IsMultisigOut returns true if this is a multisig output script.

func (*Script) IsPublicKeyHashOut

func (s *Script) IsPublicKeyHashOut() bool

IsPublicKeyHashOut returns true if this is a pay to pubkey hash output script.

func (*Script) IsPublicKeyOut

func (s *Script) IsPublicKeyOut() bool

IsPublicKeyOut returns true if this is a public key output script.

func (*Script) IsScriptHashOut

func (s *Script) IsScriptHashOut() bool

IsScriptHashOut returns true if this is a p2sh output script.

func (*Script) ToString

func (s *Script) ToString() string

ToString returns hex string of script.

type SigningItem

type SigningItem struct {
	PublicKeyHash string `json:"publicKeyHash"`
	SigHash       string `json:"sigHash"`
	PublicKey     string `json:"publicKey,omitempty"`
	Signature     string `json:"signature,omitempty"`
}

SigningItem contains the metadata neeeded to sign a transaction.

type SigningPayload

type SigningPayload []*SigningItem

SigningPayload type

func NewSigningPayload

func NewSigningPayload() *SigningPayload

NewSigningPayload creates a new SigningPayload.

func NewSigningPayloadFromTx

func NewSigningPayloadFromTx(bt *BitcoinTransaction, sigType uint32) (*SigningPayload, error)

NewSigningPayloadFromTx creates a new SigningPayload from a BitcoinTransaction and a SIGHASH type.

func (*SigningPayload) AddItem

func (sp *SigningPayload) AddItem(publicKeyHash string, sigHash string)

AddItem appends a new SigningItem to the SigningPayload array.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL