Categorygithub.com/bitcoinschema/go-bitcoin/v2
modulepackage
2.0.5
Repository: https://github.com/bitcoinschema/go-bitcoin.git
Documentation: pkg.go.dev

# README

go-bitcoin

A library for working with Bitcoin (BSV) transactions, addresses, keys, encryption, and more.

Release Build Status Report codecov Go
Gitpod Ready-to-Code Mergify Status Sponsor Donate


Table of Contents


Installation

go-bitcoin requires a supported release of Go.

go get -u github.com/bitcoinschema/go-bitcoin/v2

If you want to install the deprecated V1:

go get -u github.com/bitcoinschema/go-bitcoin@v1

Documentation

View the generated documentation

GoDoc

Features

Package Dependencies
Library Deployment

goreleaser for easy binary or library deployment to GitHub and can be installed via: brew install goreleaser.

The .goreleaser.yml file is used to configure goreleaser.

Use make release-snap to create a snapshot version of the release, and finally make release to ship to production.

Makefile Commands

View all makefile commands

make help

List of all current commands:

all                   Runs multiple commands
clean                 Remove previous builds and any test cache data
clean-mods            Remove all the Go mod cache
coverage              Shows the test coverage
diff                  Show the git diff
generate              Runs the go generate command in the base of the repo
godocs                Sync the latest tag with GoDocs
help                  Show this help message
install               Install the application
install-go            Install the application (Using Native Go)
install-releaser      Install the GoReleaser application
lint                  Run the golangci-lint application (install if not found)
release               Full production release (creates release in GitHub)
release               Runs common.release then runs godocs
release-snap          Test the full release (build binaries)
release-test          Full production test release (everything except deploy)
replace-version       Replaces the version in HTML/JS (pre-deploy)
tag                   Generate a new tag and push (tag version=0.0.0)
tag-remove            Remove a tag if found (tag-remove version=0.0.0)
tag-update            Update an existing tag to current commit (tag-update version=0.0.0)
test                  Runs lint and ALL tests
test-ci               Runs all tests via CI (exports coverage)
test-ci-no-race       Runs all tests via CI (no race) (exports coverage)
test-ci-short         Runs unit tests via CI (exports coverage)
test-no-lint          Runs just tests
test-short            Runs vet, lint and tests (excludes integration tests)
test-unit             Runs tests and outputs coverage
uninstall             Uninstall the application (and remove files)
update-linter         Update the golangci-lint package (macOS only)
vet                   Run the Go vet application

Examples & Tests

All unit tests and examples run via GitHub Actions and uses Go version 1.18.x. View the configuration file.

Run all tests (including integration tests)

make test

Run tests (excluding integration tests)

make test-short

Benchmarks

Run the Go benchmarks:

make bench

Code Standards

Read more about this Go project's code standards.


Usage

Checkout all the examples!


Maintainers

MrZMrZ
MrZSatchmo

Contributing

View the contributing guidelines and follow the code of conduct.

How can I help?

All kinds of contributions are welcome :raised_hands:! The most basic way to show your support is to star :star2: the project, or to raise issues :speech_balloon:. You can also support this project by becoming a sponsor on GitHub :clap: or by making a bitcoin donation to ensure this journey continues indefinitely! :rocket:

Stars


License

License

# Packages

No description provided by the author

# Functions

CalculateFeeForTx will estimate a fee for the given transaction If tx is nil this will panic Rate(s) can be derived from MinerAPI (default is DefaultDataRate and DefaultStandardRate) If rate is nil it will use default rates (0.5 sat per byte) Reference: https://tncpw.co/c215a75c.
CreatePrivateKey will create a new private key (*bec.PrivateKey).
CreatePrivateKeyString will create a new private key (hex encoded).
CreateTx will create a basic transaction and return the raw transaction (*transaction.Transaction) Note: this will NOT create a change output (funds are sent to "addresses") Note: this will NOT handle fee calculation (it's assumed you have already calculated the fee) Get the raw hex version: tx.ToString() Get the tx id: tx.GetTxID().
CreateTxUsingWif will create a basic transaction and return the raw transaction (*transaction.Transaction) Note: this will NOT create a "change" address (it's assumed you have already specified an address) Note: this will NOT handle "fee" calculation (it's assumed you have already calculated the fee) Get the raw hex version: tx.ToString() Get the tx id: tx.GetTxID().
CreateTxWithChange will automatically create the change output and calculate fees Use this if you don't want to figure out fees/change for a tx USE AT YOUR OWN RISK - this will modify a "pay-to" output to accomplish auto-fees.
CreateTxWithChangeUsingWif will automatically create the change output and calculate fees Use this if you don't want to figure out fees/change for a tx USE AT YOUR OWN RISK - this will modify a "pay-to" output to accomplish auto-fees.
CreateWif will create a new WIF (*wif.WIF).
CreateWifString will create a new WIF (string).
DecryptWithPrivateKey is a wrapper to decrypt the previously encrypted information, given a corresponding private key.
DecryptWithPrivateKeyString is a convenience wrapper for DecryptWithPrivateKey().
DefaultStandardFee returns the default standard fees offered by most miners.
EncryptShared will encrypt data and provide shared keys for decryption.
EncryptSharedString will encrypt a string to a hex encoded encrypted payload, and provide shared keys for decryption.
EncryptWithPrivateKey will encrypt the data using a given private key.
EncryptWithPrivateKeyString is a convenience wrapper for EncryptWithPrivateKey().
GenerateHDKey will create a new master node for use in creating a hierarchical deterministic keychain.
GenerateHDKeyFromString will create a new master node for use in creating a hierarchical deterministic keychain from an xPrivKey string.
GenerateHDKeyPair will generate a new xPub HD master node (xPrivateKey & xPublicKey).
GenerateSharedKeyPair creates shared keys that can be used to encrypt/decrypt data that can be decrypted by yourself (privateKey) and also the owner of the given public key.
GetAddressesForPath will get the corresponding addresses for the PublicKeys at the given path m/0/x Returns 2 keys, first is internal and second is external.
GetAddressFromHDKey is a helper function to get the Address associated with a given hdKey Expects hdKey to not be nil (otherwise will panic).
GetAddressFromPrivateKey takes a bec private key and returns a Bitcoin address.
GetAddressFromPrivateKeyString takes a private key string and returns a Bitcoin address.
GetAddressFromPubKey gets a bscript.Address from a bec.PublicKey.
GetAddressFromPubKeyString is a convenience function to use a hex string pubKey.
GetAddressFromScript will take an output script and extract a standard bitcoin address.
GetAddressStringFromHDKey is a helper function to get the Address (string) associated with a given hdKey Expects hdKey to not be nil (otherwise will panic).
GetExtendedPublicKey will get the extended public key (xPub).
GetHDKeyByPath gets the corresponding HD key from a chain/num path Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout.
GetHDKeyChild gets the child hd key for a given num Note: For a hardened child, start at 0x80000000.
GetHDKeyFromExtendedPublicKey will get the hd key from an existing extended public key (xPub).
GetPrivateKeyByPath gets the key for a given derivation path (chain/num) Expects hdKey to not be nil (otherwise will panic).
GetPrivateKeyFromHDKey is a helper function to get the Private Key associated with a given hdKey Expects hdKey to not be nil (otherwise will panic).
GetPrivateKeyStringFromHDKey is a helper function to get the Private Key (string) associated with a given hdKey Expects hdKey to not be nil (otherwise will panic).
GetPublicKeyFromHDKey is a helper function to get the Public Key associated with a given hdKey Expects hdKey to not be nil (otherwise will panic).
GetPublicKeysForPath gets the PublicKeys for a given derivation path Uses the standard m/0/0 (external) and m/0/1 (internal) paths Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout.
PrivateAndPublicKeys will return both the private and public key in one method Expects a hex encoded privateKey.
PrivateKeyFromString turns a private key (hex encoded string) into an bec.PrivateKey.
PrivateKeyToWif will convert a private key to a WIF (*wif.WIF).
PrivateKeyToWifString will convert a private key to a WIF (string).
PubKeyFromPrivateKey will derive a pubKey (hex encoded) from a given private key.
PubKeyFromPrivateKeyString will derive a pubKey (hex encoded) from a given private key.
PubKeyFromSignature gets a publickey for a signature and tells you whether is was compressed.
PubKeyFromString will convert a pubKey (string) into a pubkey (*bec.PublicKey).
ScriptFromAddress will create an output P2PKH script from an address string.
SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding sigRefCompressedKey bool determines whether the signature will reference a compressed or uncompresed key Spec: https://docs.moneybutton.com/docs/bsv-message.html.
TxFromHex will return a libsv.tx from a raw hex string.
ValidA58 validates a base58 encoded bitcoin address.
VerifyMessage verifies a string and address against the provided signature and assumes Bitcoin Signed Message encoding.
VerifyMessageDER will take a message string, a public key string and a signature string (in strict DER format) and verify that the message was signed by the public key.
WifFromString will convert a WIF (string) to a WIF (*wif.WIF).
WifToPrivateKey will convert a WIF to a private key (*bec.PrivateKey).
WifToPrivateKeyString will convert a WIF to private key (string).

# Constants

DefaultExternalChain is the default external chain (for public use to accept incoming txs) Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout.
DefaultInternalChain is the default internal chain (for change, generating, other purposes...) Reference: https://en.bitcoin.it/wiki/BIP_0032#The_default_wallet_layout.
DustLimit is the minimum value for a tx that can be spent Note: this is being deprecated in the new node software (TBD).
256 bits.
512 bits.

# Variables

ErrBadCharacter is returned when a bad character is found.
ErrChangeAddressRequired is returned when a change address is required to create a tx.
ErrMissingAddress is returned when an address is missing.
ErrMissingPubKey is returned when a pubkey is missing.
ErrMissingScript is returned when a script is missing.
ErrNotVersion0 is returned when a string is not version 0.
ErrPrivateKeyMissing is returned when a private key is missing.
ErrTooLong is returned when a string is too long.
ErrUtxosRequired is returned when utxos are required to create a tx.
ErrWifMissing is returned when a wif is missing.

# Structs

PayToAddress is the pay-to-address.
Utxo is an unspent transaction output.

# Type aliases

A25 is a type for a 25 byte (not base58 encoded) bitcoin address.
OpReturnData is the op return data to include in the tx.