btcspv

package
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 4, 2020 License: LGPL-3.0 Imports: 11 Imported by: 8

Documentation

Index

Constants

View Source
const ZeroBytesError = "Attempting to encode empty bytestring. " +
	"Hint: your payload may not be properly initialized"

ZeroBytesError is the error returned when attempting to encode an empty bytestring

Variables

This section is empty.

Functions

func BytesToBigUint

func BytesToBigUint(b []byte) sdk.Uint

BytesToBigUint converts a bytestring of up to 32 bytes to a cosmos sdk uint

func BytesToUint

func BytesToUint(b []byte) uint

BytesToUint converts 1, 2, 3, or 4-byte numbers to uints

func CalculateDifficulty

func CalculateDifficulty(target sdk.Uint) sdk.Uint

CalculateDifficulty calculates difficulty from the difficulty 1 target and current target Difficulty 1 is 0x1d00ffff on mainnet and testnet Difficulty 1 is a 256 bit number encoded as a 3-byte mantissa and 1 byte exponent

func DecodeIfHex added in v1.1.0

func DecodeIfHex(s string) []byte

DecodeIfHex decodes a hex string into a byte array

func DetermineInputLength

func DetermineInputLength(input []byte) (uint64, error)

DetermineInputLength gets the length of an input by parsing the scriptSigLength

func DetermineOutputLength

func DetermineOutputLength(output []byte) (uint64, error)

DetermineOutputLength returns the length of an output

func DetermineVarIntDataLength

func DetermineVarIntDataLength(flag uint8) uint8

DetermineVarIntDataLength extracts the payload length of a Bitcoin VarInt

func EncodeP2PKH added in v1.1.0

func EncodeP2PKH(pkh []byte) (string, error)

EncodeP2PKH turns a pubkey hash into an address

func EncodeP2SH added in v1.1.0

func EncodeP2SH(sh []byte) (string, error)

EncodeP2SH turns a scripthash into an address

func EncodeP2WPKH added in v1.1.0

func EncodeP2WPKH(pkh []byte) (string, error)

EncodeP2WPKH turns a pubkey hash into an address

func EncodeP2WSH added in v1.1.0

func EncodeP2WSH(sh Hash256Digest) (string, error)

EncodeP2WSH turns a scripthash into an address

func ExtractDifficulty

func ExtractDifficulty(header RawHeader) sdk.Uint

ExtractDifficulty calculates the difficulty of a header

func ExtractHash

func ExtractHash(output []byte) ([]byte, error)

ExtractHash extracts the hash from the output script Returns the hash committed to by the pk_script

func ExtractInputAtIndex

func ExtractInputAtIndex(vin []byte, index uint) ([]byte, error)

ExtractInputAtIndex parses the input vector and returns the vin at a specified index

func ExtractOpReturnData

func ExtractOpReturnData(output []byte) ([]byte, error)

ExtractOpReturnData returns the value from the output in a tx Value is an 8byte little endian number

func ExtractOutpoint

func ExtractOutpoint(input []byte) []byte

ExtractOutpoint returns the outpoint from the in input in a tx The outpoint is a 32 bit tx id with 4 byte index

func ExtractOutputAtIndex

func ExtractOutputAtIndex(vout []byte, index uint) ([]byte, error)

ExtractOutputAtIndex returns the output at a given index in the TxIns vector

func ExtractOutputScriptLen

func ExtractOutputScriptLen(output []byte) uint

ExtractOutputScriptLen extracts the output script length

func ExtractScriptSig

func ExtractScriptSig(input []byte) ([]byte, error)

ExtractScriptSig extracts the VarInt-prepended scriptSig from the input in a tx

func ExtractScriptSigLen

func ExtractScriptSigLen(input []byte) (uint64, uint64, error)

ExtractScriptSigLen determines the length of a scriptSig in an input

func ExtractSequenceLELegacy

func ExtractSequenceLELegacy(input []byte) ([]byte, error)

ExtractSequenceLELegacy returns the LE sequence in from a tx input The sequence is a 4 byte little-endian number.

func ExtractSequenceLEWitness

func ExtractSequenceLEWitness(input []byte) []byte

ExtractSequenceLEWitness extracts the LE sequence bytes from a witness input

func ExtractSequenceLegacy

func ExtractSequenceLegacy(input []byte) (uint32, error)

ExtractSequenceLegacy returns the integer sequence in from a tx input

func ExtractSequenceWitness

func ExtractSequenceWitness(input []byte) uint32

ExtractSequenceWitness extracts the sequence integer from a witness input

func ExtractTarget

func ExtractTarget(header RawHeader) sdk.Uint

ExtractTarget returns the target from a given block hedaer

func ExtractTimestamp

func ExtractTimestamp(header RawHeader) uint

ExtractTimestamp returns the timestamp from a block header as a uint Time is not 100% reliable

func ExtractTimestampLE

func ExtractTimestampLE(header RawHeader) []byte

ExtractTimestampLE returns the timestamp from a block header It returns the timestamp as a little endian []byte Time is not 100% reliable

func ExtractTxIndex

func ExtractTxIndex(input []byte) uint

ExtractTxIndex extracts the tx input index from the input in a tx

func ExtractTxIndexLE

func ExtractTxIndexLE(input []byte) []byte

ExtractTxIndexLE extracts the LE tx input index from the input in a tx Returns the tx index as a little endian []byte

func ExtractValue

func ExtractValue(output []byte) uint

ExtractValue extracts the value from the output in a tx

func ExtractValueLE

func ExtractValueLE(output []byte) []byte

ExtractValueLE extracts the value in from the output in a tx Returns a little endian []byte of the output value

func IsLegacyInput

func IsLegacyInput(input []byte) bool

IsLegacyInput determines whether an input is legacy

func LastBytes

func LastBytes(in []byte, num int) []byte

LastBytes returns the last num in from a byte array

func ParseVarInt added in v1.3.0

func ParseVarInt(b []byte) (uint64, uint64, error)

ParseVarInt parses the length and value of a VarInt payload

func Prove

func Prove(txid Hash256Digest, merkleRoot Hash256Digest, intermediateNodes []byte, index uint) bool

Prove checks the validity of a merkle proof

func RetargetAlgorithm

func RetargetAlgorithm(
	previousTarget sdk.Uint,
	firstTimestamp uint,
	secondTimestamp uint) sdk.Uint

RetargetAlgorithm performs Bitcoin consensus retargets

func ReverseEndianness

func ReverseEndianness(b []byte) []byte

ReverseEndianness takes in a byte slice and returns a reversed endian byte slice.

func Strip0xPrefix added in v1.4.0

func Strip0xPrefix(s string) string

Strip0xPrefix removes the 0x prefix from a hex string

func ValidateHeaderChain

func ValidateHeaderChain(headers []byte) (sdk.Uint, error)

ValidateHeaderChain checks validity of header chain

func ValidateHeaderPrevHash

func ValidateHeaderPrevHash(header RawHeader, prevHeaderDigest Hash256Digest) bool

ValidateHeaderPrevHash checks validity of header chain

func ValidateHeaderWork

func ValidateHeaderWork(digest Hash256Digest, target sdk.Uint) bool

ValidateHeaderWork checks validity of header work

func ValidateVin

func ValidateVin(vin []byte) bool

ValidateVin checks that the vin passed up is properly formatted

func ValidateVout

func ValidateVout(vout []byte) bool

ValidateVout checks that the vin passed up is properly formatted

func VerifyHash256Merkle

func VerifyHash256Merkle(proof []byte, index uint) bool

VerifyHash256Merkle checks a merkle inclusion proof's validity. Note that `index` is not a reliable indicator of location within a block.

Types

type BitcoinHeader added in v1.1.0

type BitcoinHeader struct {
	Raw        RawHeader     `json:"raw"`
	Hash       Hash256Digest `json:"hash"`
	Height     uint32        `json:"height"`
	PrevHash   Hash256Digest `json:"prevhash"`
	MerkleRoot Hash256Digest `json:"merkle_root"`
}

BitcoinHeader is a parsed Bitcoin header, values are LE

func HeaderFromHex added in v1.2.0

func HeaderFromHex(s string, height uint32) (BitcoinHeader, error)

HeaderFromHex buidls a BitcoinHeader from a hex string and height

func HeaderFromRaw added in v1.2.0

func HeaderFromRaw(raw RawHeader, height uint32) BitcoinHeader

HeaderFromRaw builds a BitcoinHeader from a raw bytestring and height

func (BitcoinHeader) Validate added in v1.1.0

func (b BitcoinHeader) Validate() (bool, error)

Validate checks validity of all the elements in a BitcoinHeader

type Hash160Digest added in v1.2.0

type Hash160Digest [20]byte

Hash160Digest is a 20-byte ripemd160+sha2 hash

func Hash160

func Hash160(in []byte) Hash160Digest

Hash160 takes a byte slice and returns a hashed byte slice.

func NewHash160Digest added in v1.2.0

func NewHash160Digest(b []byte) (Hash160Digest, error)

NewHash160Digest instantiates a Hash160Digest from a byte slice

func (Hash160Digest) MarshalJSON added in v1.4.0

func (h Hash160Digest) MarshalJSON() ([]byte, error)

MarshalJSON marashalls 32 byte digests as 0x-prepended hex

func (*Hash160Digest) UnmarshalJSON added in v1.4.0

func (h *Hash160Digest) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshalls 32 byte digests

type Hash256Digest added in v1.1.0

type Hash256Digest [32]byte

Hash256Digest is a 32-byte double-sha2 hash

func CalculateTxID

func CalculateTxID(version, vin, vout, locktime []byte) Hash256Digest

CalculateTxID hashes transaction to get txid

func ExtractInputTxIDLE

func ExtractInputTxIDLE(input []byte) Hash256Digest

ExtractInputTxIDLE returns the LE tx input index from the input in a tx

func ExtractMerkleRootLE

func ExtractMerkleRootLE(header RawHeader) Hash256Digest

ExtractMerkleRootLE returns the transaction merkle root from a given block header The returned merkle root is little-endian

func ExtractPrevBlockHashLE

func ExtractPrevBlockHashLE(header RawHeader) Hash256Digest

ExtractPrevBlockHashLE returns the previous block's hash from a block header Returns the hash as a little endian []byte

func Hash256

func Hash256(in []byte) Hash256Digest

Hash256 implements bitcoin's hash256 (double sha2)

func Hash256MerkleStep added in v1.4.0

func Hash256MerkleStep(a []byte, b []byte) Hash256Digest

Hash256MerkleStep concatenates and hashes two inputs for merkle proving

func NewHash256Digest added in v1.2.0

func NewHash256Digest(b []byte) (Hash256Digest, error)

NewHash256Digest instantiates a Hash256Digest from a byte slice

func ReverseHash256Endianness added in v1.2.0

func ReverseHash256Endianness(h Hash256Digest) Hash256Digest

ReverseHash256Endianness returns a new digest with opposite byteorder

func (Hash256Digest) MarshalJSON added in v1.1.0

func (h Hash256Digest) MarshalJSON() ([]byte, error)

MarshalJSON marashalls 32 byte digests as 0x-prepended hex

func (*Hash256Digest) UnmarshalJSON added in v1.1.0

func (h *Hash256Digest) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshalls 32 byte digests

type HexBytes added in v1.1.0

type HexBytes []byte

HexBytes is a type alias to make JSON hex ser/deser easier

func (HexBytes) MarshalJSON added in v1.1.0

func (h HexBytes) MarshalJSON() ([]byte, error)

MarshalJSON marashalls bytestrings as 0x-prepended hex

func (*HexBytes) UnmarshalJSON added in v1.1.0

func (h *HexBytes) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshalls 32 byte digests

type RawHeader added in v1.1.0

type RawHeader [80]byte

RawHeader is an 80-byte raw header

func NewRawHeader added in v1.2.0

func NewRawHeader(b []byte) (RawHeader, error)

NewRawHeader instantiates a RawHeader from a byte slice

func (RawHeader) MarshalJSON added in v1.1.0

func (h RawHeader) MarshalJSON() ([]byte, error)

MarshalJSON marashalls 32 byte digests as 0x-prepended hex

func (*RawHeader) UnmarshalJSON added in v1.1.0

func (h *RawHeader) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshalls 32 byte digests

type SPVProof added in v1.1.0

type SPVProof struct {
	Version           HexBytes      `json:"version"`
	Vin               HexBytes      `json:"vin"`
	Vout              HexBytes      `json:"vout"`
	Locktime          HexBytes      `json:"locktime"`
	TxID              Hash256Digest `json:"tx_id"`
	Index             uint32        `json:"index"`
	ConfirmingHeader  BitcoinHeader `json:"confirming_header"`
	IntermediateNodes HexBytes      `json:"intermediate_nodes"`
}

SPVProof is the base struct for an SPV proof

func (SPVProof) Validate added in v1.1.0

func (s SPVProof) Validate() (bool, error)

Validate checks validity of all the elements in an SPVProof

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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