bitcoin

package module
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2020 License: MIT Imports: 15 Imported by: 0

README

Go-Bitcoin

A small collection of utility functions for working with Bitcoin (BSV) transactions.

Features

  • Sign / Verify Bitcoin Message
  • Private key (string) to Address (string)
  • Private key (string) to Private key (bsvec.PrivateKey)

Dependencies

  • github.com/bitcoinsv/bsvd/
  • github.com/bitcoinsv/bsvutil/
  • github.com/itchyny/base58-go
  • github.com/piotrnar/gocoin/lib/secp256k1

Notes

Adding more to this library as needed. PRs and suggestions welcome.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Address

func Address(publicKey *bsvec.PublicKey) (*bsvutil.LegacyAddressPubKeyHash, error)

Address gets a bsvutil.LegacyAddressPubKeyHash

func AddressFromPrivateKey

func AddressFromPrivateKey(privateKey string) (string, error)

AddressFromPrivateKey takes a private key string and returns a Bitcoin address

func HexDecode

func HexDecode(str string) []byte

HexDecode returns a decoded hex string without handling errors todo: why ignore the error? (@mrz)

func PrivateKey

func PrivateKey(privKey string) (ecdsaPrivKey *bsvec.PrivateKey)

PrivateKey turns a private key string into an bsvec.PrivateKey

func SignMessage

func SignMessage(privKey string, message string) string

SignMessage signs a string with the provided private key using Bitcoin Signed Message encoding

func ValidA58

func ValidA58(a58 []byte) (bool, error)

ValidA58 validates a base58 encoded bitcoin address. An address is valid if it can be decoded into a 25 byte address, the version number is 0, and the checksum validates. Return value ok will be true for valid addresses. If ok is false, the address is invalid and the error value may indicate why.

Example

ExampleValidA58 example using ValidA58()

valid, err := ValidA58([]byte("1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2"))
if err != nil {
	fmt.Printf("error occurred: %s", err.Error())
	return
} else if !valid {
	fmt.Printf("address is not valid: %s", "1KCEAmVS6FFggtc7W9as7sEENvjt7DqMi2")
	return
} else {
	fmt.Printf("address is valid!")
}
Output:

address is valid!

func VerifyMessage

func VerifyMessage(address, signature, data string) error

VerifyMessage verifies a string and address against the provided signature and assumes Bitcoin Signed Message encoding

Types

type A25

type A25 [25]byte

A25 is a type for a 25 byte (not base58 encoded) bitcoin address.

func (*A25) ComputeChecksum

func (a *A25) ComputeChecksum() (c [4]byte)

ComputeChecksum returns a four byte checksum computed from the first 21 bytes of the address. The embedded checksum is not updated.

func (*A25) EmbeddedChecksum

func (a *A25) EmbeddedChecksum() (c [4]byte)

EmbeddedChecksum returns the 4 checksum bytes of a A25 address

func (*A25) Set58

func (a *A25) Set58(s []byte) error

Set58 takes a base58 encoded address and decodes it into the receiver. Errors are returned if the argument is not valid base58 or if the decoded value does not fit in the 25 byte address. The address is not otherwise checked for validity.

func (*A25) Version

func (a *A25) Version() byte

Version returns the version byte of a A25 address

Jump to

Keyboard shortcuts

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