Documentation ¶
Overview ¶
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 https://en.bitcoin.it/wiki/Script for full specification.
Index ¶
- Constants
- Variables
- func CheckPublicKeyIsValid(publicKey []byte) error
- func Hash160(data []byte) ([]byte, error)
- func NewMOfNRedeemScript(m int, n int, publicKeys [][]byte) ([]byte, error)
- func NewP2PKHScriptPubKey(publicKeyHash []byte) ([]byte, error)
- func NewP2SHScriptPubKey(redeemScriptHash []byte) ([]byte, error)
- func NewPrivateKey() []byte
- func NewPublicKey(privateKey []byte) ([]byte, error)
- func NewRandomBytes(size int) ([]byte, error)
- func NewRawTransaction(inputTxHash string, satoshis int, scriptSig []byte, scriptPubKey []byte) ([]byte, error)
- func NewSignature(rawTransaction []byte, privateKey []byte) ([]byte, error)
Constants ¶
const ( OP_1 = 81 + iota OP_2 //82 OP_3 //83 OP_4 //.. OP_5 OP_6 OP_7 OP_8 OP_9 OP_10 OP_11 OP_12 OP_13 OP_14 //.. OP_15 //95 OP_16 //96 )
OP_1 through OP_16
const ( OP_0 = 0 OP_PUSHDATA1 = 76 OP_PUSHDATA2 = 77 OP_DUP = 118 OP_EQUAL = 135 OP_EQUALVERIFY = 136 OP_HASH160 = 169 OP_CHECKSIG = 172 OP_CHECKMULTISIG = 174 )
OP codes other than OP_1 through OP_16, used in P2SH Multisig transanctions.
Variables ¶
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.
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.**
Functions ¶
func CheckPublicKeyIsValid ¶
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 ¶
Hash160 performs the same operations as OP_HASH160 in Bitcoin Script It hashes the given data first with SHA256, then RIPEMD160
func NewMOfNRedeemScript ¶
NewMOfNRedeemScript creates a M-of-N Multisig redeem script given m, n and n public keys
func NewP2PKHScriptPubKey ¶
NewP2PKHScriptPubKey creates a scriptPubKey for a P2PKH transaction given the destination public key hash
func NewP2SHScriptPubKey ¶
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 ¶
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 ¶
NewRandomBytes generates pseudorandom bytes of length size. Cryptographically secure to the limits of crypto/rand package.
Types ¶
This section is empty.