rawtx

package module
Version: v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2022 License: BSD-3-Clause Imports: 10 Imported by: 1

README

rawtx

A Golang module that helps you answer questions about raw bitcoin transactions, their inputs, outputs and scripts.

Transactions

Transactions can be deserialized from a hex string or from a wire.MsgTx. Based on that following questions can be answered.

Input and Output type

Following is answered:

spends? pays to?
P2PK
P2PKH
P2SH-P2WPKH can't tell
from the rawtx
P2WPKH_V0
P2MS
P2SH
P2SH-P2WSH can't tell
from the rawtx
P2WSH_V0
OP_RETURN can't spend an
OP_RETURN output

Additionally you might ask:

Bitcoin Script

There is a BitcoinScript parser for byte slices build in. A ParsedBitcoinScript is a slice of ParsedOpCodes. It has a String() method which displays the OP codes in a bitcoin developer readable format.

bs1 := BitcoinScript{0x6e, 0x87, 0x91, 0x69, 0xa7, 0x7c, 0xa7, 0x87}
pbs1 := bs1.Parse()
fmt.Println(pbs1.String())
// -> OP_2DUP OP_EQUAL OP_NOT OP_VERIFY OP_SHA1 OP_SWAP OP_SHA1 OP_EQUAL
bs2 = BitcoinScript{byte(OpRETURN), byte(OpDATA2), byte(OpCHECKLOCKTIMEVERIFY), byte(OpDATA12)}
pbs2 = bs2.Parse()
fmt.Println(pbs2.String())
// -> OP_RETURN OP_DATA_2(b10c)

The actual OpCode behind the ParsedOpCode can, but doesn't have to push data. You can check if a ParsedOpCode is

Running tests

Either normal test suit with coverage report in percent.

$ make test
...
PASS
coverage: 100.0% of statements
ok      github.com/0xB10C/rawtx 0.028s

Or with detailed coverage HTML report opened in your browser.

$ make cover
...
PASS
coverage: 100.0% of statements
ok      github.com/0xB10C/rawtx 0.028s
go tool cover -html=count.out

Discalimer

Do not use in consensus related code!

License

rawTx is licensed under a BSD 3-Clause License.

Documentation

Overview

Package rawtx helps you answer questions about raw bitcoin transactions, their inputs, outputs and scripts. More information https://github.com/0xB10C/rawtx

Index

Constants

View Source
const DERMarker = 0x30

DERMarker represents the compound maker used in the DER encoding

View Source
const DERValueMarker = 0x02

DERValueMarker represents the value (integer) maker used in the DER encoding

View Source
const TAPROOT_ANNEX_INDICATOR = 0x50
View Source
const TAPROOT_LEAF_MASK = 0xfe
View Source
const TAPROOT_LEAF_TAPSCRIPT = 0xc0

Variables

View Source
var OpCodeStringMap = map[OpCode]string{}/* 256 elements not displayed */

OpCodeStringMap maps op codes to their developer readable names

Functions

func HexDecodeRawTxString

func HexDecodeRawTxString(rawTx string) (hexDecodedTx []byte, err error)

HexDecodeRawTxString hex decodes a rawTx string and returns it as byte slice.

Types

type BitcoinScript

type BitcoinScript []byte

BitcoinScript represents a bitcoin script as a byte slice A script can for example be the scriptPubKey or the scriptSig

func (BitcoinScript) IsMultisigScript

func (s BitcoinScript) IsMultisigScript() (isMultisig bool, numRequiredSigs int, numPossiblePubKeys int)

IsMultisigScript checks if a passed BitcoinScript is multisig. Supported are P2SH, P2SH-P2WSH and P2WSH redeem scripts.

func (BitcoinScript) Parse added in v1.1.0

func (s BitcoinScript) Parse() (parsed ParsedBitcoinScript)

Parse parses the BitcoinScript and returns a ParsedBitcoinScript. It expects a script with correctly formatted data pushes, or it might return ParsedOpCodes with shorter than expected data for pushes-past-script-end.

type ECDSASignature added in v1.2.0

type ECDSASignature struct {
	// contains filtered or unexported fields
}

ECDSASignature hold the raw byte fields of a deserialized ECDSA signature However, these fields might e.g. still be padded with zero values.

func DeserializeECDSASignature added in v1.2.0

func DeserializeECDSASignature(sigBytes []byte, requireStrictDER bool) (sig ECDSASignature, ok bool)

DeserializeECDSASignature tries to deserialize a ECDSA signature. The caller can choose strict DER encoding is required. If the function returns ok=true then the ECDSASignature object is filled with the r and S values. The caller is expected to pass a signature with the SigHash flag removed. https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki#der-encoding-reference

func (*ECDSASignature) HasLowR added in v1.2.0

func (sig *ECDSASignature) HasLowR() bool

HasLowR checks if a ECDSASignature has a low r value as implemented in https://github.com/bitcoin/bitcoin/pull/13666

func (*ECDSASignature) HasLowS added in v1.2.0

func (sig *ECDSASignature) HasLowS() bool

HasLowS checks if a ECDSASignature has a low S value as defined in https://github.com/bitcoin/bips/blob/master/bip-0146.mediawiki#low_s

type Input

type Input struct {
	Outpoint  Outpoint
	ScriptSig BitcoinScript
	Sequence  uint32
	Witness   ParsedBitcoinScript
	// contains filtered or unexported fields
}

Input represents a bitcoin transaction input as a struct.

func (*Input) FromWireTxIn

func (in *Input) FromWireTxIn(txIn *wire.TxIn)

FromWireTxIn populates an Input struct with values from a wire.TxIn.

func (*Input) GetNestedP2WSHRedeemScript

func (in *Input) GetNestedP2WSHRedeemScript() (redeemScript BitcoinScript)

GetNestedP2WSHRedeemScript checks if the input spend is a Nested-P2WSH input and then returns the redeemScript. The returned redeemScript is empty if the input does not spend a Nested-P2WSH input.

func (*Input) GetP2SHRedeemScript

func (in *Input) GetP2SHRedeemScript() (redeemScript BitcoinScript)

GetP2SHRedeemScript checks if the input spend is a P2SH input and then returns the redeemScript. The returned redeemScript is empty if the input does not spend a P2SH input.

func (*Input) GetP2WSHRedeemScript

func (in *Input) GetP2WSHRedeemScript() (redeemScript BitcoinScript)

GetP2WSHRedeemScript checks if the input spend is a P2WSH input and then returns the redeemScript. The returned redeemScript is empty if the input does not spend a P2WSH input.

func (*Input) GetType

func (in *Input) GetType() InputType

GetType retruns the input type as a InputType

func (*Input) HasWitness

func (in *Input) HasWitness() bool

HasWitness returns a boolean indicating if an input has a witness

func (*Input) InputStats added in v1.2.0

func (input *Input) InputStats() *InputStats

InputStats returns a populated *InputStats struct for the input

func (*Input) IsCoinbase added in v1.1.0

func (in *Input) IsCoinbase() bool

IsCoinbase checks if an input is a coinbase input.

func (*Input) IsCoinbaseWithWitness added in v1.2.0

func (in *Input) IsCoinbaseWithWitness() bool

IsCoinbaseWithWitness checks if an input is a coinbase input by checking the previous- output-index to be equal to 0xffffffff and then checking the previous-tx-hash to be all zero and then checks if the coinbase has a witness.

func (*Input) IsCoinbaseWithoutWitness added in v1.2.0

func (in *Input) IsCoinbaseWithoutWitness() bool

IsCoinbaseWithoutWitness checks if an input is a coinbase input by checking the previous- output-index to be equal to 0xffffffff and then checking the previous-tx-hash to be all zero and then checks if the coinbase has a witness.

func (*Input) IsLNUniliteralClosing

func (in *Input) IsLNUniliteralClosing() bool

IsLNUniliteralClosing checks if the input spend is a lightning network unilateral close.

  OP_IF
	  pubKey
  OP_ELSE
	  OP_DATA_X (i.e. CSV time) OP_CHECKSEQUENCEVERIFY OP_DROP pubKey
  OP_ENDIF
  OP_CHECKSIG

func (*Input) SpendsMultisig

func (in *Input) SpendsMultisig() bool

SpendsMultisig checks if the input spend is a multisig input. Checked are P2MS, P2SH, P2SH-P2WSH and P2WSH inputs.

func (*Input) SpendsNativeSegWit

func (in *Input) SpendsNativeSegWit() bool

SpendsNativeSegWit checks if the input spend is a native SegWit input. A native SegWit input has a witness but an empty scriptSig.

func (*Input) SpendsNestedP2WPKH

func (in *Input) SpendsNestedP2WPKH() bool

SpendsNestedP2WPKH checks if the input spend is a nested P2WPKH input. A nested P2WPKH input has a witness and the scriptSig looks like OP_DATA_22(OP_0 OP_DATA_20(20 byte hash))

func (*Input) SpendsNestedP2WSH

func (in *Input) SpendsNestedP2WSH() bool

SpendsNestedP2WSH checks if the input spend is a nested P2WSH input. A nested P2WSH input has a witness and the scriptSig looks like OP_DATA_34(OP_0 OP_DATA_32(32 byte hash))

func (*Input) SpendsNestedSegWit

func (in *Input) SpendsNestedSegWit() bool

SpendsNestedSegWit checks if the input spend is a nested SegWit input. A nested SegWit input has an **not** empty scriptSig and a witness.

func (*Input) SpendsP2MS

func (in *Input) SpendsP2MS() bool

SpendsP2MS checks if an input spends a P2MS input. A P2MS has no witness items and a scriptSig as follows: OP_0 <signature> [<signature>] [<signature>] (where [] means optional)

func (*Input) SpendsP2PK

func (in *Input) SpendsP2PK() bool

SpendsP2PK checks if the input spend is a P2PK input. A P2PK input only contains the signature in the scriptSig. <signature>

func (*Input) SpendsP2PKH

func (in *Input) SpendsP2PKH() (spendsP2PKH bool)

SpendsP2PKH checks if the input spend is a P2PKH input. <signature> <pubkey>

func (*Input) SpendsP2PKHWithIsCompressed

func (in *Input) SpendsP2PKHWithIsCompressed() (spendsP2PKH bool, isCompressedPubKey bool)

SpendsP2PKHWithIsCompressed checks if the input spend is a P2PKH input. Additionally it returns a boolean indicating if the revealed pubkey is compressed. <signature> <pubkey>

func (*Input) SpendsP2SH

func (in *Input) SpendsP2SH() (spendsP2SH bool)

SpendsP2SH checks if the input spend is a P2SH input. A P2SH input has a redeemscript push at the end of the scriptSig, which is neither a signature or a pubkey.

func (*Input) SpendsP2TR added in v1.3.0

func (in *Input) SpendsP2TR() bool

func (*Input) SpendsP2TRKeyPath added in v1.3.0

func (in *Input) SpendsP2TRKeyPath() bool

func (*Input) SpendsP2TRScriptPath added in v1.3.0

func (in *Input) SpendsP2TRScriptPath() bool

func (*Input) SpendsP2WPKH

func (in *Input) SpendsP2WPKH() bool

SpendsP2WPKH checks if an input is spending a P2WPKH input. A P2WPKH input has a empty scriptSig, but contains exactly two items in the witness: [signature, pubkey]

func (*Input) SpendsP2WSH

func (in *Input) SpendsP2WSH() bool

SpendsP2WSH checks if an input spends a P2WSH input. Native SegWit inputs that aren't P2WPKH or P2TR are likely P2WSH.

type InputStats added in v1.2.0

type InputStats struct {
	Type                   InputType
	TypeString             string
	Sequence               uint32
	IsSpendingSegWit       bool
	IsSpendingNativeSegWit bool
	IsSpendingNestedSegWit bool
	IsLNUniliteralClosing  bool
	IsSpendingMultisig     bool
	MultiSigM              int
	MultiSigN              int
	SigStats               []*SignatureStats
	PubKeyStats            []*PubKeyStats
	OpCodes                []OpCode
}

InputStats contains stats about a transaction input

type InputType

type InputType int

InputType defines the input type

const (
	InP2PK InputType = iota + 1
	InP2PKH
	InP2SH_P2WPKH
	InP2WPKH
	InP2MS
	InP2SH
	InP2SH_P2WSH
	InP2WSH
	InP2TRKP // key path
	InP2TRSP // script path
	InCOINBASE
	InCOINBASE_WITNESS
	InUNKNOWN
)

Possible types a input can be

func (InputType) String

func (it InputType) String() string

type LocktimeStats added in v1.2.0

type LocktimeStats struct {
	Locktime      uint32
	IsEnforced    bool
	IsBlockHeight bool
	IsTimestamp   bool
}

LocktimeStats contains stats about the transaction locktime

type OpCode

type OpCode byte

OpCode represents a Bitcoin operation code

const (
	Op0                   OpCode = 0x00
	OpDATA1               OpCode = 0x01
	OpDATA2               OpCode = 0x02
	OpDATA3               OpCode = 0x03
	OpDATA4               OpCode = 0x04
	OpDATA5               OpCode = 0x05
	OpDATA6               OpCode = 0x06
	OpDATA7               OpCode = 0x07
	OpDATA8               OpCode = 0x08
	OpDATA9               OpCode = 0x09
	OpDATA10              OpCode = 0x0a
	OpDATA11              OpCode = 0x0b
	OpDATA12              OpCode = 0x0c
	OpDATA13              OpCode = 0x0d
	OpDATA14              OpCode = 0x0e
	OpDATA15              OpCode = 0x0f
	OpDATA16              OpCode = 0x10
	OpDATA17              OpCode = 0x11
	OpDATA18              OpCode = 0x12
	OpDATA19              OpCode = 0x13
	OpDATA20              OpCode = 0x14
	OpDATA21              OpCode = 0x15
	OpDATA22              OpCode = 0x16
	OpDATA23              OpCode = 0x17
	OpDATA24              OpCode = 0x18
	OpDATA25              OpCode = 0x19
	OpDATA26              OpCode = 0x1a
	OpDATA27              OpCode = 0x1b
	OpDATA28              OpCode = 0x1c
	OpDATA29              OpCode = 0x1d
	OpDATA30              OpCode = 0x1e
	OpDATA31              OpCode = 0x1f
	OpDATA32              OpCode = 0x20
	OpDATA33              OpCode = 0x21
	OpDATA34              OpCode = 0x22
	OpDATA35              OpCode = 0x23
	OpDATA36              OpCode = 0x24
	OpDATA37              OpCode = 0x25
	OpDATA38              OpCode = 0x26
	OpDATA39              OpCode = 0x27
	OpDATA40              OpCode = 0x28
	OpDATA41              OpCode = 0x29
	OpDATA42              OpCode = 0x2a
	OpDATA43              OpCode = 0x2b
	OpDATA44              OpCode = 0x2c
	OpDATA45              OpCode = 0x2d
	OpDATA46              OpCode = 0x2e
	OpDATA47              OpCode = 0x2f
	OpDATA48              OpCode = 0x30
	OpDATA49              OpCode = 0x31
	OpDATA50              OpCode = 0x32
	OpDATA51              OpCode = 0x33
	OpDATA52              OpCode = 0x34
	OpDATA53              OpCode = 0x35
	OpDATA54              OpCode = 0x36
	OpDATA55              OpCode = 0x37
	OpDATA56              OpCode = 0x38
	OpDATA57              OpCode = 0x39
	OpDATA58              OpCode = 0x3a
	OpDATA59              OpCode = 0x3b
	OpDATA60              OpCode = 0x3c
	OpDATA61              OpCode = 0x3d
	OpDATA62              OpCode = 0x3e
	OpDATA63              OpCode = 0x3f
	OpDATA64              OpCode = 0x40
	OpDATA65              OpCode = 0x41
	OpDATA66              OpCode = 0x42
	OpDATA67              OpCode = 0x43
	OpDATA68              OpCode = 0x44
	OpDATA69              OpCode = 0x45
	OpDATA70              OpCode = 0x46
	OpDATA71              OpCode = 0x47
	OpDATA72              OpCode = 0x48
	OpDATA73              OpCode = 0x49
	OpDATA74              OpCode = 0x4a
	OpDATA75              OpCode = 0x4b
	OpPUSHDATA1           OpCode = 0x4c
	OpPUSHDATA2           OpCode = 0x4d
	OpPUSHDATA4           OpCode = 0x4e
	Op1NEGATE             OpCode = 0x4f
	OpRESERVED            OpCode = 0x50
	Op1                   OpCode = 0x51
	OpTRUE                OpCode = 0x51
	Op2                   OpCode = 0x52
	Op3                   OpCode = 0x53
	Op4                   OpCode = 0x54
	Op5                   OpCode = 0x55
	Op6                   OpCode = 0x56
	Op7                   OpCode = 0x57
	Op8                   OpCode = 0x58
	Op9                   OpCode = 0x59
	Op10                  OpCode = 0x5a
	Op11                  OpCode = 0x5b
	Op12                  OpCode = 0x5c
	Op13                  OpCode = 0x5d
	Op14                  OpCode = 0x5e
	Op15                  OpCode = 0x5f
	Op16                  OpCode = 0x60
	OpNOP                 OpCode = 0x61
	OpVER                 OpCode = 0x62
	OpIF                  OpCode = 0x63
	OpNOTIF               OpCode = 0x64
	OpVERIF               OpCode = 0x65
	OpVERNOTIF            OpCode = 0x66
	OpELSE                OpCode = 0x67
	OpENDIF               OpCode = 0x68
	OpVERIFY              OpCode = 0x69
	OpRETURN              OpCode = 0x6a
	OpTOALTSTACK          OpCode = 0x6b
	OpFROMALTSTACK        OpCode = 0x6c
	Op2DROP               OpCode = 0x6d
	Op2DUP                OpCode = 0x6e
	Op3DUP                OpCode = 0x6f
	Op2OVER               OpCode = 0x70
	Op2ROT                OpCode = 0x71
	Op2SWAP               OpCode = 0x72
	OpIFDUP               OpCode = 0x73
	OpDEPTH               OpCode = 0x74
	OpDROP                OpCode = 0x75
	OpDUP                 OpCode = 0x76
	OpNIP                 OpCode = 0x77
	OpOVER                OpCode = 0x78
	OpPICK                OpCode = 0x79
	OpROLL                OpCode = 0x7a
	OpROT                 OpCode = 0x7b
	OpSWAP                OpCode = 0x7c
	OpTUCK                OpCode = 0x7d
	OpCAT                 OpCode = 0x7e
	OpSUBSTR              OpCode = 0x7f
	OpLEFT                OpCode = 0x80
	OpRIGHT               OpCode = 0x81
	OpSIZE                OpCode = 0x82
	OpINVERT              OpCode = 0x83
	OpAND                 OpCode = 0x84
	OpOR                  OpCode = 0x85
	OpXOR                 OpCode = 0x86
	OpEQUAL               OpCode = 0x87
	OpEQUALVERIFY         OpCode = 0x88
	OpRESERVED1           OpCode = 0x89
	OpRESERVED2           OpCode = 0x8a
	Op1ADD                OpCode = 0x8b
	Op1SUB                OpCode = 0x8c
	Op2MUL                OpCode = 0x8d
	Op2DIV                OpCode = 0x8e
	OpNEGATE              OpCode = 0x8f
	OpABS                 OpCode = 0x90
	OpNOT                 OpCode = 0x91
	Op0NOTEQUAL           OpCode = 0x92
	OpADD                 OpCode = 0x93
	OpSUB                 OpCode = 0x94
	OpMUL                 OpCode = 0x95
	OpDIV                 OpCode = 0x96
	OpMOD                 OpCode = 0x97
	OpLSHIFT              OpCode = 0x98
	OpRSHIFT              OpCode = 0x99
	OpBOOLAND             OpCode = 0x9a
	OpBOOLOR              OpCode = 0x9b
	OpNUMEQUAL            OpCode = 0x9c
	OpNUMEQUALVERIFY      OpCode = 0x9d
	OpNUMNOTEQUAL         OpCode = 0x9e
	OpLESSTHAN            OpCode = 0x9f
	OpGREATERTHAN         OpCode = 0xa0
	OpLESSTHANOREQUAL     OpCode = 0xa1
	OpGREATERTHANOREQUAL  OpCode = 0xa2
	OpMIN                 OpCode = 0xa3
	OpMAX                 OpCode = 0xa4
	OpWITHIN              OpCode = 0xa5
	OpRIPEMD160           OpCode = 0xa6
	OpSHA1                OpCode = 0xa7
	OpSHA256              OpCode = 0xa8
	OpHASH160             OpCode = 0xa9
	OpHASH256             OpCode = 0xaa
	OpCODESEPARATOR       OpCode = 0xab
	OpCHECKSIG            OpCode = 0xac
	OpCHECKSIGVERIFY      OpCode = 0xad
	OpCHECKMULTISIG       OpCode = 0xae
	OpCHECKMULTISIGVERIFY OpCode = 0xaf
	OpNOP1                OpCode = 0xb0
	OpCHECKLOCKTIMEVERIFY OpCode = 0xb1
	OpCHECKSEQUENCEVERIFY OpCode = 0xb2
	OpNOP4                OpCode = 0xb3
	OpNOP5                OpCode = 0xb4
	OpNOP6                OpCode = 0xb5
	OpNOP7                OpCode = 0xb6
	OpNOP8                OpCode = 0xb7
	OpNOP9                OpCode = 0xb8
	OpNOP10               OpCode = 0xb9
	OpUNKNOWN186          OpCode = 0xba
	OpUNKNOWN187          OpCode = 0xbb
	OpUNKNOWN188          OpCode = 0xbc
	OpUNKNOWN189          OpCode = 0xbd
	OpUNKNOWN190          OpCode = 0xbe
	OpUNKNOWN191          OpCode = 0xbf
	OpUNKNOWN192          OpCode = 0xc0
	OpUNKNOWN193          OpCode = 0xc1
	OpUNKNOWN194          OpCode = 0xc2
	OpUNKNOWN195          OpCode = 0xc3
	OpUNKNOWN196          OpCode = 0xc4
	OpUNKNOWN197          OpCode = 0xc5
	OpUNKNOWN198          OpCode = 0xc6
	OpUNKNOWN199          OpCode = 0xc7
	OpUNKNOWN200          OpCode = 0xc8
	OpUNKNOWN201          OpCode = 0xc9
	OpUNKNOWN202          OpCode = 0xca
	OpUNKNOWN203          OpCode = 0xcb
	OpUNKNOWN204          OpCode = 0xcc
	OpUNKNOWN205          OpCode = 0xcd
	OpUNKNOWN206          OpCode = 0xce
	OpUNKNOWN207          OpCode = 0xcf
	OpUNKNOWN208          OpCode = 0xd0
	OpUNKNOWN209          OpCode = 0xd1
	OpUNKNOWN210          OpCode = 0xd2
	OpUNKNOWN211          OpCode = 0xd3
	OpUNKNOWN212          OpCode = 0xd4
	OpUNKNOWN213          OpCode = 0xd5
	OpUNKNOWN214          OpCode = 0xd6
	OpUNKNOWN215          OpCode = 0xd7
	OpUNKNOWN216          OpCode = 0xd8
	OpUNKNOWN217          OpCode = 0xd9
	OpUNKNOWN218          OpCode = 0xda
	OpUNKNOWN219          OpCode = 0xdb
	OpUNKNOWN220          OpCode = 0xdc
	OpUNKNOWN221          OpCode = 0xdd
	OpUNKNOWN222          OpCode = 0xde
	OpUNKNOWN223          OpCode = 0xdf
	OpUNKNOWN224          OpCode = 0xe0
	OpUNKNOWN225          OpCode = 0xe1
	OpUNKNOWN226          OpCode = 0xe2
	OpUNKNOWN227          OpCode = 0xe3
	OpUNKNOWN228          OpCode = 0xe4
	OpUNKNOWN229          OpCode = 0xe5
	OpUNKNOWN230          OpCode = 0xe6
	OpUNKNOWN231          OpCode = 0xe7
	OpUNKNOWN232          OpCode = 0xe8
	OpUNKNOWN233          OpCode = 0xe9
	OpUNKNOWN234          OpCode = 0xea
	OpUNKNOWN235          OpCode = 0xeb
	OpUNKNOWN236          OpCode = 0xec
	OpUNKNOWN237          OpCode = 0xed
	OpUNKNOWN238          OpCode = 0xee
	OpUNKNOWN239          OpCode = 0xef
	OpUNKNOWN240          OpCode = 0xf0
	OpUNKNOWN241          OpCode = 0xf1
	OpUNKNOWN242          OpCode = 0xf2
	OpUNKNOWN243          OpCode = 0xf3
	OpUNKNOWN244          OpCode = 0xf4
	OpUNKNOWN245          OpCode = 0xf5
	OpUNKNOWN246          OpCode = 0xf6
	OpUNKNOWN247          OpCode = 0xf7
	OpUNKNOWN248          OpCode = 0xf8
	OpUNKNOWN249          OpCode = 0xf9
	OpSMALLINTEGER        OpCode = 0xfa
	OpPUBKEYS             OpCode = 0xfb
	OpUNKNOWN252          OpCode = 0xfc
	OpPUBKEYHASH          OpCode = 0xfd
	OpPUBKEY              OpCode = 0xfe
	OpINVALIDOPCODE       OpCode = 0xff
)

Bitcoin OP Codes. Mainly copied and adopted from https://github.com/btcsuite/btcd/blob/master/txscript/opcode.go

func GetDataPushOpCodeForLength added in v1.2.0

func GetDataPushOpCodeForLength(length int) OpCode

GetDataPushOpCodeForLength returns the optimal DataPush OpCode for the given data length OpINVALIDOPCODE is returned for zero, negative or bigger than 0xffffffff inputs.

func (OpCode) IsDataPushOpCode added in v1.1.0

func (opCode OpCode) IsDataPushOpCode() bool

IsDataPushOpCode indicates if a opCode pushes data to the stack

type Outpoint

type Outpoint struct {
	PrevTxHash  [32]byte
	OutputIndex uint32
}

Outpoint represents a bitcoin transaction input's previous outpoint as a struct.

func (*Outpoint) FromWireOutpoint

func (outpoint *Outpoint) FromWireOutpoint(wireOutpoint *wire.OutPoint)

FromWireOutpoint populates an Outpoint struct with values from a wire.OutPoint.

type Output

type Output struct {
	Value        int64
	ScriptPubKey BitcoinScript
	// contains filtered or unexported fields
}

Output represents a bitcoin transaction output as a struct.

func (*Output) FromWireTxOut

func (out *Output) FromWireTxOut(txOut *wire.TxOut)

FromWireTxOut populates an Output struct with values from a wire.TxOut.

func (*Output) GetOPReturnData

func (out *Output) GetOPReturnData() (bool, ParsedOpCode)

GetOPReturnData returns a ParsedOpCode struct, which includes the Op Code and the data pushed by OP_RETURN. An OP_RETURN scriptPubKey looks like:

OP_RETURN <SomeDataPush> <OP_RETURN data>

func (*Output) GetType

func (out *Output) GetType() OutputType

GetType retruns the output type as a OutputType

func (*Output) IsOPReturnOutput

func (out *Output) IsOPReturnOutput() (is bool)

IsOPReturnOutput returns if an Output is an OP_RETURN output An OP_RETURN scriptPubKey looks like:

OP_RETURN <SomeDataPush> <OP_RETURN data>

func (*Output) IsP2MSOutput

func (out *Output) IsP2MSOutput() (is bool, m int, n int)

IsP2MSOutput returns a boolean indicating if a output is a P2MS output A P2MS 1-of-2 scriptPubKey looks like:

OP_1 PubKey PubKey OP_2 OP_CHECKMULTISIG

func (*Output) IsP2PKHOutput

func (out *Output) IsP2PKHOutput() bool

IsP2PKHOutput returns a boolean indicating if a output is a P2PKH output A P2PKH scriptPubKey looks like:

OP_DUP OP_HASH160 OP_DATA_20(20 byte pubKeyHash) OP_EQUALVERIFY OP_CHECKSIG
OP_DUP OP_HASH160 OP_DATA_20(                  ) OP_EQUALVERIFY OP_CHECKSIG

func (*Output) IsP2PKOutput

func (out *Output) IsP2PKOutput() bool

IsP2PKOutput returns a boolean indicating if a output is a P2PK output A P2PK output looks like:

PubKey OP_CHECKSIG

func (*Output) IsP2SHOutput

func (out *Output) IsP2SHOutput() bool

IsP2SHOutput returns a boolean indicating if a output is a P2SH output A P2SH scriptPubKey looks like:

OP_HASH160 OP_DATA_20(20 byte hash) OP_EQUAL

func (*Output) IsP2TROutput added in v1.3.0

func (out *Output) IsP2TROutput() bool

IsP2TROutput returns a boolean indicating if a output is a P2TR output A P2TR output looks like:

OP_1 OP_PUSH32 <schnorr_public_key>

func (*Output) IsP2WPKHV0Output

func (out *Output) IsP2WPKHV0Output() bool

IsP2WPKHV0Output returns a boolean indicating if a output is a P2WPKH output with witness program 0 A P2WPKH V0 output looks like:

OP_0 OP_DATA_20(20 byte hash) (where the leading OP_0 indicates witness program 0)

func (*Output) IsP2WSHV0Output

func (out *Output) IsP2WSHV0Output() bool

IsP2WSHV0Output returns a boolean indicating if a output is a P2WSH output with witness program 0 A P2WSH V0 output looks like:

OP_0 (as witness program 0) OP_DATA_32(32 byte hash)

func (*Output) OutputStats added in v1.2.0

func (out *Output) OutputStats() *OutputStats

OutputStats returns a populated *OutputStats struct for an output

type OutputStats added in v1.2.0

type OutputStats struct {
	Type         OutputType
	TypeString   string
	Amount       int64
	OpReturnData []byte
	PubKeyStats  []*PubKeyStats // P2MS outputs have pubkeys
	OpCodes      []OpCode
}

OutputStats contains stats about an output

type OutputType

type OutputType int

OutputType defines the input type

const (
	OutP2PK OutputType = iota + 1
	OutP2PKH
	OutP2WPKH
	OutP2MS
	OutP2SH
	OutP2WSH
	OutP2TR
	OutOPRETURN
	OutUNKNOWN
)

Possible types a output can be

func (OutputType) String

func (ot OutputType) String() string

type ParsedBitcoinScript

type ParsedBitcoinScript []ParsedOpCode

ParsedBitcoinScript is a parsed BitcoinScript

func (ParsedBitcoinScript) String

func (pbs ParsedBitcoinScript) String() (s string)

type ParsedOpCode

type ParsedOpCode struct {
	OpCode     OpCode
	PushedData []byte
}

ParsedOpCode respresents a OP Code as a part of a parsed BitcoinScript. TODO: rename ito ParsedBitcoinScriptElement or something similar?

func (ParsedOpCode) GetSigHash

func (poc ParsedOpCode) GetSigHash() (sighash byte)

GetSigHash returns the SIGHASH of a signature. If the passed ParsedOpCode does not push a Signature the returned SIGHASH is 0.

func (ParsedOpCode) IsCompressedECDSAPubKey added in v1.3.0

func (poc ParsedOpCode) IsCompressedECDSAPubKey() bool

IsCompressedECDSAPubKey checks a two byte slices if they could represent a compressed public key. <pubkey length> <|pubkey|>

func (ParsedOpCode) IsECDSAPubKey added in v1.3.0

func (poc ParsedOpCode) IsECDSAPubKey() bool

IsECDSAPubKey checks a two byte slices if they could represent a public key.

func (ParsedOpCode) IsECDSAPubKeyWithIsCompressed added in v1.3.0

func (poc ParsedOpCode) IsECDSAPubKeyWithIsCompressed() (isPubKey bool, isCompressed bool)

IsECDSAPubKeyWithIsCompressed checks a two byte slices if they could represent a pubkey and if that pubkey is compressed.

func (*ParsedOpCode) IsECDSASignature added in v1.2.0

func (poc *ParsedOpCode) IsECDSASignature(requireStrictDER bool) bool

IsECDSASignature checks a ParsedOpCode if it could represent a ECDSA signature.

func (*ParsedOpCode) IsECDSASignatureInStrictDER added in v1.2.0

func (poc *ParsedOpCode) IsECDSASignatureInStrictDER() bool

IsECDSASignatureInStrictDER checks a ParsedOpCode if it represents a DER-encoded signature.

func (*ParsedOpCode) IsSchnorrSignature added in v1.3.0

func (poc *ParsedOpCode) IsSchnorrSignature() bool

IsSchnorrSignature checks a ParsedOpCode if it could represent a Schnorr signature.

func (*ParsedOpCode) IsSignature

func (poc *ParsedOpCode) IsSignature() bool

IsSignature checks a ParsedOpCode if it could represent a signature.

func (ParsedOpCode) IsUncompressedECDSAPubKey added in v1.3.0

func (poc ParsedOpCode) IsUncompressedECDSAPubKey() bool

IsUncompressedECDSAPubKey checks a two byte slices if they could represent a uncompressed public key. <pubkey length> <|pubkey|>

func (*ParsedOpCode) PubKeyStats added in v1.2.0

func (sigOpCode *ParsedOpCode) PubKeyStats() *PubKeyStats

PubKeyStats returns a populated *PubKeyStats struct for the PubKey The caller must make sure that the *ParsedOpCode is a pubkey.

func (*ParsedOpCode) SignatureStats added in v1.2.0

func (sigOpCode *ParsedOpCode) SignatureStats() *SignatureStats

SignatureStats returns a populated *SignatureStats struct for the signature The caller must make sure that the *ParsedOpCode is a signature.

func (*ParsedOpCode) String

func (poc *ParsedOpCode) String() string

type PubKeyStats added in v1.2.0

type PubKeyStats struct {
	IsCompressed bool
}

PubKeyStats contains stats about a PubKey

type SignatureStats added in v1.2.0

type SignatureStats struct {
	Length      int
	IsECDSA     bool
	IsStrictDER bool
	HasLowR     bool
	HasLowS     bool
	SigHash     byte
}

SignatureStats contains stats about a signature

type TestMultisigType added in v1.2.0

type TestMultisigType struct {
	// contains filtered or unexported fields
}

TestMultisigType stores an expected value which is compared to a function result in a unit test

type TestOpReturnData added in v1.2.0

type TestOpReturnData struct {
	// contains filtered or unexported fields
}

TestOpReturnData stores an expected value which is compared to a function result in a unit test

type TestTransaction added in v1.2.0

type TestTransaction struct {
	Note                     string
	RawTx                    string
	Size                     int
	VSize                    int
	Weight                   int
	CompressedPubKey         []bool
	MultisigType             []TestMultisigType
	InputTypes               []InputType
	OutputTypes              []OutputType
	OpReturnData             []TestOpReturnData
	Locktime                 uint32
	OutputSum                int64
	IsExplicitlySignalingRBF bool
	IsBIP69Compliant         bool
	IsSpendingMultisig       bool
	IsSpendingTaproot        bool
	IsLNUniliteralClosing    bool
	P2MSType                 []TestMultisigType
}

TestTransaction is used in the unit tests to

func GetTestTransactions added in v1.2.0

func GetTestTransactions() []TestTransaction

GetTestTransactions returns an array of TestTransactions

type Tx

type Tx struct {
	Hash       []byte
	HashString string
	Version    int32
	Inputs     []Input
	Outputs    []Output
	Locktime   uint32
	// contains filtered or unexported fields
}

Tx represents a bitcoin transaction as a struct.

func DeserializeRawTxBytes

func DeserializeRawTxBytes(rawTx []byte) (tx Tx, err error)

DeserializeRawTxBytes returns a wire.MsgTx for a hex decoded rawTx as byte slice. If the rawTx is can't be deserialized an error is returned.

func StringToTx

func StringToTx(rawTx string) (Tx, error)

StringToTx returns a wire.MsgTx for a raw transaction hex string

func (*Tx) FromWireMsgTx

func (tx *Tx) FromWireMsgTx(wireTx *wire.MsgTx)

FromWireMsgTx populates a Tx struct with values from a wire.MsgTx.

func (*Tx) GetLocktime

func (tx *Tx) GetLocktime() uint32

GetLocktime returns the locktime of the transaction

func (*Tx) GetNumInputs

func (tx *Tx) GetNumInputs() int

GetNumInputs returns the number of inputs the transaction has

func (*Tx) GetNumOutputs

func (tx *Tx) GetNumOutputs() int

GetNumOutputs returns the number of outputs the transaction has

func (*Tx) GetOutputSum

func (tx *Tx) GetOutputSum() (sumOutputValues int64)

GetOutputSum returns the sum of all output values of the transaction in satoshi

func (*Tx) GetSizeWithWitness

func (tx *Tx) GetSizeWithWitness() int

GetSizeWithWitness returns the transaction size **without** the witness stripped (size in bytes)

func (*Tx) GetSizeWithoutWitness

func (tx *Tx) GetSizeWithoutWitness() int

GetSizeWithoutWitness returns the transaction size **with** the witness stripped (vsize in vbyte)

func (*Tx) HasOPReturnOutput

func (tx *Tx) HasOPReturnOutput() bool

HasOPReturnOutput returns a boolean indicating if the transaction has a OP_RETURN output

func (*Tx) HasP2MSOutput

func (tx *Tx) HasP2MSOutput() bool

HasP2MSOutput returns a boolean indicating if the transaction has a P2MS output

func (*Tx) HasP2PKHOutput

func (tx *Tx) HasP2PKHOutput() bool

HasP2PKHOutput returns a boolean indicating if the transaction has a P2PKH output

func (*Tx) HasP2PKOutput

func (tx *Tx) HasP2PKOutput() bool

HasP2PKOutput returns a boolean indicating if the transaction has a P2PK output

func (*Tx) HasP2SHOutput

func (tx *Tx) HasP2SHOutput() bool

HasP2SHOutput returns a boolean indicating if the transaction has a P2SH output

func (*Tx) HasP2WPKHOutput

func (tx *Tx) HasP2WPKHOutput() bool

HasP2WPKHOutput returns a boolean indicating if the transaction has a P2WPKH output

func (*Tx) HasP2WSHOutput

func (tx *Tx) HasP2WSHOutput() bool

HasP2WSHOutput returns a boolean indicating if the transaction has a P2WSH output

func (*Tx) IsBIP69Compliant

func (tx *Tx) IsBIP69Compliant() bool

IsBIP69Compliant returns a boolean indicating if the transaction is BIP 69 compliant

func (*Tx) IsCoinbase

func (tx *Tx) IsCoinbase() bool

IsCoinbase returns a boolean indicating if a the transaction is a coinbase transaction.

func (*Tx) IsExplicitlyRBFSignaling

func (tx *Tx) IsExplicitlyRBFSignaling() bool

IsExplicitlyRBFSignaling returns a boolean indicating if the transaction is explicitly signaling ReplaceByFee The transaction might still be implicitly able to be replaced by fee by e.g. a parent transaction signaling RBG

func (*Tx) IsSpendingMultisig

func (tx *Tx) IsSpendingMultisig() bool

IsSpendingMultisig returns a boolean indicating if the transaction spends a multisig input

func (*Tx) IsSpendingNativeSegWit

func (tx *Tx) IsSpendingNativeSegWit() bool

IsSpendingNativeSegWit returns a boolean indicating if the transaction spends native SegWit

func (*Tx) IsSpendingNestedSegWit

func (tx *Tx) IsSpendingNestedSegWit() bool

IsSpendingNestedSegWit returns a boolean indicating if the transaction spends nested SegWit

func (*Tx) IsSpendingSegWit

func (tx *Tx) IsSpendingSegWit() bool

IsSpendingSegWit returns a boolean indicating if a transaction spends SegWit inputs

func (*Tx) IsSpendingTaproot added in v1.3.2

func (tx *Tx) IsSpendingTaproot() bool

IsSpendingTaproot returns a boolean indicating if the transaction spends a taproot input

func (*Tx) LocktimeStats added in v1.2.0

func (tx *Tx) LocktimeStats() *LocktimeStats

LocktimeStats returns a populated *LocktimeStats struct for the transaction

func (*Tx) Stats added in v1.2.0

func (tx *Tx) Stats() *TxStats

Stats returns a *TxStats for the transaction

type TxStats added in v1.2.0

type TxStats struct {
	TxID                     []byte
	TxIDString               string
	Version                  int32
	Payments                 uint32
	OutAmount                int64
	VSize                    int
	Size                     int
	IsCoinbase               bool
	IsSpendingSegWit         bool
	IsSpendingTaproot        bool
	IsSpendingNativeSegWit   bool
	IsSpendingNestedSegWit   bool
	IsBIP69Compliant         bool
	IsExplicitlyRBFSignaling bool
	Locktime                 *LocktimeStats
	InStats                  []*InputStats
	OutStats                 []*OutputStats
}

TxStats contains stats about a transaction.

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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