tx

package
v0.0.0-...-942cd0b Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2021 License: MIT Imports: 13 Imported by: 0

Documentation

Overview

Implementation of Bitcoin transaction. Reference: https://en.bitcoin.it/wiki/Transaction

Index

Constants

View Source
const (
	// Sighash Types
	SighashAll = uint32(iota + 1)
	SighashNone
	SighashSingle
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Tx

type Tx struct {
	Version  uint32
	TxIns    []TxIn
	TxOuts   []TxOut
	LockTime uint32

	// Utility, not transmitted over the wire.
	TestNet bool
	SegWit  bool
	// contains filtered or unexported fields
}

func Fetch

func Fetch(txId string, testnet, fresh bool) (Tx, error)

func (*Tx) Fee

func (t *Tx) Fee() (int, error)

Fee returns the fee of this transaction in satoshi

func (*Tx) Id

func (t *Tx) Id() (string, error)

func (*Tx) Marshal

func (t *Tx) Marshal() ([]byte, error)

Marshal converts Tx t to []byte.

Legacy format:

[Version][NumIns][TxIns][NumOuts][TxOuts][LockTime]

SegWit format:

[Version][Marker][Flag][NumIns][TxIns][NumOuts][TxOuts][Witness][LockTime]

func (*Tx) SighashBip143

func (t *Tx) SighashBip143(index int, scriptPubKey script.Script, value uint64) ([32]byte, error)

SighashBip143 returns the message that needs to get signed for the input with the index. Fixes the O(n^2) hashing problem. ref: https://github.com/bitcoin/bips/blob/master/bip-0143.mediawiki#Specification

func (*Tx) SighashLegacy

func (t *Tx) SighashLegacy(index int) ([32]byte, error)

SighashLegacy returns the message that needs to get signed for the input with the index.

func (*Tx) SignInput

func (t *Tx) SignInput(index int, priv *ecdsa.PrivateKey) (bool, error)

SignInput signs the input with the index using the private key TODO: make SegWit compatible

func (*Tx) Unmarshal

func (t *Tx) Unmarshal(r io.Reader) (*Tx, error)

Unmarshal parses a Tx from the Reader r.

Legacy format:

[Version][NumIns][TxIns][NumOuts][TxOuts][LockTime]

SegWit format:

[Version][Marker][Flag][NumIns][TxIns][NumOuts][TxOuts][Witness][LockTime]

func (*Tx) Verify

func (t *Tx) Verify() (bool, error)

Verify returns whether this transaction is valid

func (*Tx) VerifyInput

func (t *Tx) VerifyInput(index int) (bool, error)

VerifyInput returns whether the input has a valid signature

type TxIn

type TxIn struct {
	PrevTxId  [32]byte      // prev transaction ID: hash256 of prev tx contents
	PrevIndex uint32        // UTXO output index in the prev transaction
	ScriptSig script.Script // unlocking script
	Sequence  uint32        // originally intended for "high frequency trades", with locktime

	// Utility, not transmitted over the wire.
	TestNet bool // whether this tx is on testnet or mainnet

	// SegWit specific
	Witness [][]byte // stack elements
}

func (*TxIn) Marshal

func (in *TxIn) Marshal() ([]byte, error)

func (*TxIn) ScriptPubKey

func (in *TxIn) ScriptPubKey() (script.Script, error)

ScriptPubKey returns the ScriptPubKey of the UTXO from the previous transaction

func (*TxIn) Unmarshal

func (in *TxIn) Unmarshal(r io.Reader) *TxIn

func (*TxIn) Value

func (in *TxIn) Value() (uint64, error)

Value returns the Amount of the UTXO from the previous transaction

type TxOut

type TxOut struct {
	Amount       uint64        // in units of satoshi (1e-8 of a bitcoin)
	ScriptPubKey script.Script // locking script
}

func (*TxOut) Marshal

func (out *TxOut) Marshal() ([]byte, error)

func (*TxOut) Unmarshal

func (out *TxOut) Unmarshal(r io.Reader) *TxOut

Jump to

Keyboard shortcuts

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