Documentation
¶
Overview ¶
Package bc is a bitcoin blockchain library bc = block chain.
Index ¶
- Variables
- func BuildCoinbase(c1 []byte, c2 []byte, extraNonce1 string, extraNonce2 string) []byte
- func BuildMerkleRoot(txids []string) (string, error)
- func BuildMerkleRootFromCoinbase(coinbaseHash []byte, merkleBranches []string) []byte
- func BuildMerkleTreeStore(txids []string) ([]string, error)
- func BuildMerkleTreeStoreChainHash(txids []*chainhash.Hash) []*chainhash.Hash
- func BytesFromStringReverse(s string) []byte
- func Decode32Byte(hexStr string) ([32]byte, error)
- func DifficultyFromBits(bits []byte) (float64, error)
- func DifficultyToHashrate(coin string, diff uint64, targetSeconds float64) float64
- func Equals(b1 []byte, b2 []byte) bool
- func ExpandTargetFrom(bits string) (string, error)
- func ExpandTargetFromAsInt(bits string) (*big.Int, error)
- func ExtractMerkleRootFromBlockHeader(header string) (string, error)
- func GetCoinbaseParts(height uint32, coinbaseValue uint64, defaultWitnessCommitment string, ...) (coinbase1 []byte, coinbase2 []byte, err error)
- func GetMerkleBranches(template []string) []string
- func HumanHash(val float64) string
- func MerkleRootFromBranches(txHash string, txIndex int, branches []string) (string, error)
- func MerkleTreeParent(leftNode, rightNode []byte) []byte
- func MerkleTreeParentBytes(l *chainhash.Hash, r *chainhash.Hash) *chainhash.Hash
- func MerkleTreeParentStr(leftNode, rightNode string) (string, error)
- func ReverseHexString(hex string) string
- func Sha256Sha256(digest []byte) []byte
- func SortByteArrays(src [][]byte) [][]byte
- func StringFromBytesReverse(h []byte) string
- func TxsToTxIDs(txs []string) ([]string, error)
- func UInt32ToBytes(num uint32) []byte
- type BUMP
- func NewBUMPFromBytes(bytes []byte) (*BUMP, error)
- func NewBUMPFromJSON(jsonStr string) (*BUMP, error)
- func NewBUMPFromMerkleTreeAndIndex(blockHeight uint64, merkleTree []*chainhash.Hash, txIndex uint64) (*BUMP, error)
- func NewBUMPFromStr(str string) (*BUMP, error)
- func NewBUMPFromStream(bytes []byte) (*BUMP, int, error)
- type Block
- type BlockHeader
- func (bh *BlockHeader) BitsStr() string
- func (bh *BlockHeader) Bytes() []byte
- func (bh *BlockHeader) HashMerkleRootStr() string
- func (bh *BlockHeader) HashPrevBlockStr() string
- func (bh *BlockHeader) MarshalJSON() ([]byte, error)
- func (bh *BlockHeader) String() string
- func (bh *BlockHeader) UnmarshalJSON(b []byte) error
- func (bh *BlockHeader) Valid() bool
- type BlockHeaderChain
- type MapiCallback
- type MerklePath
- type MerkleProof
Constants ¶
This section is empty.
Variables ¶
var ( // ErrHeaderNotFound can be returned if the blockHash isn't found on the network. ErrHeaderNotFound = errors.New("header with not found") // ErrNotOnLongestChain indicates the blockhash is present but isn't on the longest current chain. ErrNotOnLongestChain = errors.New("header exists but is not on the longest chain") )
Functions ¶
func BuildCoinbase ¶
BuildCoinbase recombines the different parts of the coinbase transaction. See https://arxiv.org/pdf/1703.06545.pdf section 2.2 for more info.
func BuildMerkleRoot ¶
BuildMerkleRoot builds the Merkle Root from a list of transactions.
func BuildMerkleRootFromCoinbase ¶
BuildMerkleRootFromCoinbase builds the merkle root of the block from the coinbase transaction hash (txid) and the merkle branches needed to work up the merkle tree and returns the merkle root byte array.
func BuildMerkleTreeStore ¶
BuildMerkleTreeStore creates a merkle tree from a slice of transaction IDs, stores it using a linear array, and returns a slice of the backing array. A linear array was chosen as opposed to an actual tree structure since it uses about half as much memory. The following describes a merkle tree and how it is stored in a linear array.
A merkle tree is a tree in which every non-leaf node is the hash of its children nodes. A diagram depicting how this works for bitcoin transactions where h(x) is a double sha256 follows:
root = h1234 = h(h12 + h34) / \ h12 = h(h1 + h2) h34 = h(h3 + h4) / \ / \ h1 = h(tx1) h2 = h(tx2) h3 = h(tx3) h4 = h(tx4)
The above stored as a linear array is as follows:
[h1 h2 h3 h4 h12 h34 root]
As the above shows, the merkle root is always the last element in the array.
The number of inputs is not always a power of two which results in a balanced tree structure as above. In that case, parent nodes with no children are also zero and parent nodes with only a single left node are calculated by concatenating the left node with itself before hashing. Since this function uses nodes that are pointers to the hashes, empty nodes will be nil.
The additional bool parameter indicates if we are generating the merkle tree using witness transaction id's rather than regular transaction id's. This also presents an additional case wherein the wtxid of the coinbase transaction is the zeroHash.
based off of bsvd: https://github.com/bitcoinsv/bsvd/blob/4c29707f717300d3eb92352081c3b0fec556881b/blockchain/merkle.go#L74
func BuildMerkleTreeStoreChainHash ¶
BuildMerkleTreeStoreChainHash has the same functionality as BuildMerkleTreeStore but uses chainhash as a type to avoid string conversions.
func BytesFromStringReverse ¶
BytesFromStringReverse decodes a hex string into a byte slice and reverses it.
func Decode32Byte ¶
Decode32Byte decodes a hex string into a 32-byte array.
func DifficultyFromBits ¶
DifficultyFromBits returns the mining difficulty from the nBits field in the block header.
func DifficultyToHashrate ¶
DifficultyToHashrate takes a specific coin ticker, it's difficulty, and target and computes the estimated hashrate on that specific coin (or chain).
func ExpandTargetFrom ¶
ExpandTargetFrom comment.
func ExpandTargetFromAsInt ¶
ExpandTargetFromAsInt comment.
func ExtractMerkleRootFromBlockHeader ¶
ExtractMerkleRootFromBlockHeader will take an 80 byte Bitcoin block header hex string and return the Merkle Root from it.
func GetCoinbaseParts ¶
func GetCoinbaseParts(height uint32, coinbaseValue uint64, defaultWitnessCommitment string, coinbaseText string, walletAddress string, minerIDBytes []byte, ) (coinbase1 []byte, coinbase2 []byte, err error)
GetCoinbaseParts returns the two split coinbase parts from coinbase metadata. See https://arxiv.org/pdf/1703.06545.pdf section 2.2 for more info.
func GetMerkleBranches ¶
GetMerkleBranches comment.
func MerkleRootFromBranches ¶
MerkleRootFromBranches returns a Merkle root given a transaction hash (txid), the index in which it is positioned in the Merkle tree, and the branches needed along the way (a Merkle path).
func MerkleTreeParent ¶
MerkleTreeParent returns the Merkle Tree parent of two Merkle Tree children.
func MerkleTreeParentBytes ¶
MerkleTreeParentBytes returns the Merkle Tree parent of two Merkle Tree children. The expectation is that the bytes are not reversed.
func MerkleTreeParentStr ¶
MerkleTreeParentStr returns the Merkle Tree parent of two Merkle Tree children using hex strings instead of just bytes.
func ReverseHexString ¶
ReverseHexString reverses the hex string (little endian/big endian). This is used when computing merkle trees in Bitcoin, for example.
func Sha256Sha256 ¶
Sha256Sha256 calculates the double sha256 hash of a byte slice.
func StringFromBytesReverse ¶
StringFromBytesReverse reverses a byte slice and encodes it as a hex string.
func TxsToTxIDs ¶
TxsToTxIDs takes an array of transactions and returns instead an array of their corresponding transaction IDs.
func UInt32ToBytes ¶
UInt32ToBytes converts an uint32 into an array of bytes.
Types ¶
type BUMP ¶
type BUMP struct { BlockHeight uint64 `json:"blockHeight"` Path [][]leaf `json:"path"` }
BUMP data model json format according to BRC-74.
func NewBUMPFromBytes ¶
NewBUMPFromBytes creates a new BUMP from a byte slice.
func NewBUMPFromJSON ¶
NewBUMPFromJSON creates a BUMP from a JSON string.
func NewBUMPFromMerkleTreeAndIndex ¶
func NewBUMPFromMerkleTreeAndIndex(blockHeight uint64, merkleTree []*chainhash.Hash, txIndex uint64) (*BUMP, error)
NewBUMPFromMerkleTreeAndIndex with a merkle tree we calculate the merkle path for a given transaction.
func NewBUMPFromStr ¶
NewBUMPFromStr creates a BUMP from hex string.
func NewBUMPFromStream ¶
NewBUMPFromStream takes an array of bytes and contracts a BUMP from it, returning the BUMP and the bytes used. Despite the name, this is not reading a stream in the true sense: it is a byte slice that contains many BUMPs one after another.
func (*BUMP) Bytes ¶
Bytes encodes a BUMP as a slice of bytes. BUMP Binary Format according to BRC-74 https://brc.dev/74
func (*BUMP) CalculateRootGivenTxid ¶
CalculateRootGivenTxid calculates the root of the Merkle tree given a txid.
type Block ¶
type Block struct { BlockHeader *BlockHeader Txs []*bt.Tx }
A Block in the Bitcoin blockchain.
func NewBlockFromBytes ¶
NewBlockFromBytes will encode a block header byte slice into the bitcoin block header structure.
See https://btcinformation.org/en/developer-reference#serialized-blocks
func NewBlockFromStr ¶
NewBlockFromStr will encode a block header hash into the bitcoin block header structure.
See https://btcinformation.org/en/developer-reference#serialized-blocks
func (*Block) Bytes ¶
Bytes will decode a bitcoin block struct into a byte slice.
See https://btcinformation.org/en/developer-reference#serialized-blocks
type BlockHeader ¶
type BlockHeader struct { Version uint32 `json:"version"` Time uint32 `json:"time"` Nonce uint32 `json:"nonce"` HashPrevBlock []byte `json:"hashPrevBlock"` HashMerkleRoot []byte `json:"merkleRoot"` Bits []byte `json:"bits"` }
A BlockHeader in the Bitcoin blockchain.
func NewBlockHeaderFromBytes ¶
func NewBlockHeaderFromBytes(headerBytes []byte) (*BlockHeader, error)
NewBlockHeaderFromBytes will encode a block header byte slice into the bitcoin block header structure.
func NewBlockHeaderFromStr ¶
func NewBlockHeaderFromStr(headerStr string) (*BlockHeader, error)
NewBlockHeaderFromStr will encode a block header hash into the bitcoin block header structure.
func (*BlockHeader) BitsStr ¶
func (bh *BlockHeader) BitsStr() string
BitsStr returns the Block Header encoded as hex string.
func (*BlockHeader) Bytes ¶
func (bh *BlockHeader) Bytes() []byte
Bytes will decode a bitcoin block header struct into a byte slice.
func (*BlockHeader) HashMerkleRootStr ¶
func (bh *BlockHeader) HashMerkleRootStr() string
HashMerkleRootStr returns the Block Header encoded as hex string.
func (*BlockHeader) HashPrevBlockStr ¶
func (bh *BlockHeader) HashPrevBlockStr() string
HashPrevBlockStr returns the Block Header encoded as hex string.
func (*BlockHeader) MarshalJSON ¶
func (bh *BlockHeader) MarshalJSON() ([]byte, error)
MarshalJSON marshals the receiving bc.BlockHeader into a JSON []byte.
func (*BlockHeader) String ¶
func (bh *BlockHeader) String() string
String returns the Block Header encoded as hex string.
func (*BlockHeader) UnmarshalJSON ¶
func (bh *BlockHeader) UnmarshalJSON(b []byte) error
UnmarshalJSON unmarshal a JSON []byte into the receiving bc.BlockHeader.
func (*BlockHeader) Valid ¶
func (bh *BlockHeader) Valid() bool
Valid checks whether a blockheader satisfies the proof-of-work claimed in Bits. Wwe check whether its Hash256 read as a little endian number is less than the Bits written in expanded form.
type BlockHeaderChain ¶
type BlockHeaderChain interface {
BlockHeader(ctx context.Context, blockHash string) (*BlockHeader, error)
}
A BlockHeaderChain is a generic interface used to map things in the block header chain (chain of block headers). For example, it is used to get a block Header from a bitcoin block hash if it exists in the longest block header chain.
Errors can be returned if the header isn't found or is on a stale chain, you may also use the ErrHeaderNotFound & ErrNotOnLongestChain sentinel errors when implementing the interface.
type MapiCallback ¶
type MapiCallback struct { CallbackPayload string `json:"callbackPayload"` APIVersion string `json:"apiVersion"` Timestamp string `json:"timestamp"` MinerID string `json:"minerId"` BlockHash string `json:"blockHash"` BlockHeight uint64 `json:"blockHeight"` CallbackTxID string `json:"callbackTxId"` CallbackReason string `json:"callbackReason"` }
MapiCallback is the body contents posted to the provided callback url from Merchant API.
func NewMapiCallbackFromBytes ¶
func NewMapiCallbackFromBytes(b []byte) (*MapiCallback, error)
NewMapiCallbackFromBytes is a glorified JSON unmarshaller, but might be more sophisticated in the future.
func (*MapiCallback) Bytes ¶
func (mcb *MapiCallback) Bytes() ([]byte, error)
Bytes convert the MapiCallback struct into a binary format. We are not going to parse anything out but rather take the whole JSON object as a binary blob. The reason behind this approach is that the mapi server signs the whole callback, so if a single byte is out of place, the signature will be invalid.
type MerklePath ¶
MerklePath data model json format according to BRC-58.
func GetTxMerklePath ¶
func GetTxMerklePath(txIndex int, merkleTree []string) *MerklePath
GetTxMerklePath with a merkle tree we calculate the merkle path for a given transaction.
func NewMerklePathFromBytes ¶
func NewMerklePathFromBytes(bytes []byte) (*MerklePath, error)
NewMerklePathFromBytes creates a new MerklePath from a byte slice.
func NewMerklePathFromStr ¶
func NewMerklePathFromStr(str string) (*MerklePath, error)
NewMerklePathFromStr creates a MerklePath from hex string.
func (*MerklePath) Bytes ¶
func (mp *MerklePath) Bytes() ([]byte, error)
Bytes encodes a MerklePath as a slice of bytes. MerklePath Binary Format according to BRC-71 https://brc.dev/71
func (*MerklePath) CalculateRoot ¶
func (mp *MerklePath) CalculateRoot(txid string) (string, error)
CalculateRoot calculates the merkle root from a transaction ID and a MerklePath.
func (*MerklePath) String ¶
func (mp *MerklePath) String() (string, error)
String encodes a MerklePath as a hex string.
type MerkleProof ¶
type MerkleProof struct { Index uint64 `json:"index"` TxOrID string `json:"txOrId"` Target string `json:"target"` Nodes []string `json:"nodes"` TargetType string `json:"targetType,omitempty"` ProofType string `json:"proofType,omitempty"` Composite bool `json:"composite,omitempty"` }
A MerkleProof is a structure that proves the inclusion of a Bitcoin transaction in a block.
func (*MerkleProof) Bytes ¶
func (mp *MerkleProof) Bytes() ([]byte, error)
Bytes convert the JSON Merkle Proof into byte encoding.
Check the following encoding:
flags: byte, index: varint, txLength: varint, //omitted if flag bit 0 == 0 as it's a fixed length transaction ID txOrId: byte[32 or variable length], target: byte[32 or 80], //determined by flag bits 1 and 2 nodeCount: varint, nodes: node[]
Source Files
¶
Directories
¶
Path | Synopsis |
---|---|
Package main is the main package for the examples
|
Package main is the main package for the examples |
Package spv is a simple payment verification library for Bitcoin SV.
|
Package spv is a simple payment verification library for Bitcoin SV. |
testing
|
|
data
Package data contains test data for the spv package.
|
Package data contains test data for the spv package. |