Version: v0.0.0-...-98fd9ef Latest Latest

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

Go to latest
Published: Dec 20, 2014 License: MIT Imports: 13 Imported by: 0



Package btcutils contains a number of useful Bitcoin related functions originally used in the go-bitcoin-multisig project but useful in any general Bitcoin project.

Provides Bitcoin Script enum to improve code readability. See for full specification.



View Source
const (
	OP_1 = 81 + iota
	OP_2 //82
	OP_3 //83
	OP_4 //..
	OP_14 //..
	OP_15 //95
	OP_16 //96

OP_1 through OP_16

View Source
const (
	OP_0             = 0
	OP_PUSHDATA1     = 76
	OP_PUSHDATA2     = 77
	OP_DUP           = 118
	OP_EQUAL         = 135
	OP_HASH160       = 169
	OP_CHECKSIG      = 172

OP codes other than OP_1 through OP_16, used in P2SH Multisig transanctions.


View Source
var FIXED_NONCE = [...]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}

Fixed nonce value for repeatable testing. We declare var and not const because Go slices are mutable and cannot be const, but we use fixedNonce like a constant.

View Source
var SetFixedNonce bool

setFixedNonce is used for testing and debugging. It is by default false, but if set to true, then newNonce() will always return a zero-valued [32]byte{}. Allows repeatable ECDSA signatures for testing **Should never be turned on in production. Limit to use in tests only.**


func CheckPublicKeyIsValid

func CheckPublicKeyIsValid(publicKey []byte) error

CheckPublicKeyIsValid runs a couple of checks to make sure a public key looks valid. Returns an error with a helpful message or nil if key is valid.

func Hash160

func Hash160(data []byte) ([]byte, error)

Hash160 performs the same operations as OP_HASH160 in Bitcoin Script It hashes the given data first with SHA256, then RIPEMD160

func NewMOfNRedeemScript

func NewMOfNRedeemScript(m int, n int, publicKeys [][]byte) ([]byte, error)

NewMOfNRedeemScript creates a M-of-N Multisig redeem script given m, n and n public keys

func NewP2PKHScriptPubKey

func NewP2PKHScriptPubKey(publicKeyHash []byte) ([]byte, error)

NewP2PKHScriptPubKey creates a scriptPubKey for a P2PKH transaction given the destination public key hash

func NewP2SHScriptPubKey

func NewP2SHScriptPubKey(redeemScriptHash []byte) ([]byte, error)

NewP2SHScriptPubKey creates a scriptPubKey for a P2SH transaction given the redeemScript hash

func NewPrivateKey

func NewPrivateKey() []byte

NewPrivateKey generates a pseudorandom private key compatible with ECDSA. Cryptographically secure to the limits of crypto/rand package.

func NewPublicKey

func NewPublicKey(privateKey []byte) ([]byte, error)

NewPublicKey generates the public key from the private key. Unfortunately golang ecdsa package does not include a secp256k1 curve as this is fairly specific to Bitcoin. Using toxeus/go-secp256k1 which wraps the official bitcoin/c-secp256k1 with cgo.

func NewRandomBytes

func NewRandomBytes(size int) ([]byte, error)

NewRandomBytes generates pseudorandom bytes of length size. Cryptographically secure to the limits of crypto/rand package.

func NewRawTransaction

func NewRawTransaction(inputTxHash string, satoshis int, scriptSig []byte, scriptPubKey []byte) ([]byte, error)

NewRawTransaction creates a Bitcoin transaction given inputs, output satoshi amount, scriptSig and scriptPubKey

func NewSignature

func NewSignature(rawTransaction []byte, privateKey []byte) ([]byte, error)

NewSignature generates a ECDSA signature given the raw transaction and privateKey to sign with


This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
t or T : Toggle theme light dark auto
y or Y : Canonical URL