utils

package
v0.14.0 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2024 License: Apache-2.0 Imports: 23 Imported by: 3

Documentation

Overview

Package utils is used for generic methods and values that are used across all packages

Index

Constants

View Source
const (
	// ScriptTypePubKey alias from bscript
	ScriptTypePubKey = bscript2.ScriptTypePubKey

	// ScriptTypePubKeyHash alias from bscript
	ScriptTypePubKeyHash = bscript2.ScriptTypePubKeyHash

	// ScriptTypeNullData alias from bscript
	ScriptTypeNullData = bscript2.ScriptTypeNullData

	// ScriptTypeMultiSig alias from bscript
	ScriptTypeMultiSig = bscript2.ScriptTypeMultiSig

	// ScriptTypeNonStandard alias from bscript
	ScriptTypeNonStandard = bscript2.ScriptTypeNonStandard

	// ScriptHashType is the type for the deprecated script hash
	ScriptHashType = "scripthash"

	// ScriptMetanet is the type for a metanet transaction
	ScriptMetanet = "metanet"

	// ScriptTypeTokenStas is the type for a STAS output
	ScriptTypeTokenStas = "token_stas"

	// ScriptTypeTokenSensible is the type for a Sensible output
	ScriptTypeTokenSensible = "token_sensible" // 73656e7369626c65
)
View Source
const (
	// XpubKeyLength is the length of an xPub string key
	XpubKeyLength = 111

	// ChainInternal internal chain num
	ChainInternal = uint32(1)

	// ChainExternal external chain num
	ChainExternal = uint32(0)

	// MaxInt32 max integer for int32
	MaxInt32 = int64(1<<(32-1) - 1)
)

Variables

View Source
var (
	// HashPuzzleRegexpString regex string of a hash puzzle
	HashPuzzleRegexpString = `a914[\da-f]{40}8876a9[\da-f]{40}88ac`

	// HashPuzzleRegexp regex string of a hash puzzle
	HashPuzzleRegexp = `^a914[\da-f]{40}8876a9[\da-f]{40}88ac`

	// RunRegexpString regex string of run protocol in op_return
	RunRegexpString = `006a0372756e`

	// RunRegexp regex of run protocol in op_return
	RunRegexp = `^006a0372756e`

	// P2PKHRegexpString regex string of P2PKH
	P2PKHRegexpString = `76a914[\da-f]{40}88ac`

	// P2PKHRegexp OP_DUP OP_HASH160 [pubkey hash] OP_EQUALVERIFY OP_CHECKSIG
	P2PKHRegexp, _ = regexp.Compile(`^` + P2PKHRegexpString + `$`) // exact match

	// P2PKHSubstringRegexp substring of OP_DUP OP_HASH160 [pubkey hash] OP_EQUALVERIFY OP_CHECKSIG
	P2PKHSubstringRegexp, _ = regexp.Compile(P2PKHRegexpString)

	// P2SHRegexp OP_HASH160 [hash] OP_EQUAL
	P2SHRegexp, _ = regexp.Compile(`^a914[\da-f]{40}87$`)

	// P2SHSubstringRegexp substring of OP_HASH160 [hash] OP_EQUAL
	P2SHSubstringRegexp, _ = regexp.Compile(`a914[\da-f]{40}87`)

	// MetanetRegexp OP_FALSE OP_RETURN 1635018093
	MetanetRegexp, _ = regexp.Compile("^006a046d65746142")

	// MetanetSubstringRegexp substring of OP_FALSE OP_RETURN 1635018093
	MetanetSubstringRegexp, _ = regexp.Compile("006a046d65746142")

	// OpReturnRegexp OP_FALSE OP_RETURN
	OpReturnRegexp, _ = regexp.Compile("^006a")

	// OpReturnSubstringRegexp substring of OP_FALSE OP_RETURN
	OpReturnSubstringRegexp, _ = regexp.Compile("006a")

	// StasRegexp OP_DUP OP_HASH160 [pubkey hash] OP_EQUALVERIFY OP_CHECKSIG OP_VERIFY ...
	StasRegexp, _ = regexp.Compile("^76a914[0-9a-f]{40}88ac6976aa607f5f7f7c5e7f7c5d7f7c5c7f7c5b7f7c5a7f7c597f7c587f7c577f7c567f7c557f7c547f7c537f7c527f7c517f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7c5f7f7c5e7f7c5d7f7c5c7f7c5b7f7c5a7f7c597f7c587f7c577f7c567f7c557f7c547f7c537f7c527f7c517f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e01007e818b21414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff007d976e7c5296a06394677768827601249301307c7e23022079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798027e7c7e7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e01417e21038ff83d8cf12121491609c4939dc11c4aa35503508fe432dc5a5c1905608b9218ad547f7701207f01207f7701247f517f7801007e8102fd00a063546752687f7801007e817f727e7b01177f777b557a766471567a577a786354807e7e676d68aa880067765158a569765187645294567a5379587a7e7e78637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6867567a6876aa587a7d54807e577a597a5a7a786354807e6f7e7eaa727c7e676d6e7eaa7c687b7eaa587a7d877663516752687c72879b69537a647500687c7b547f77517f7853a0916901247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f77788c6301247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f777852946301247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f77686877517f7c52797d8b9f7c53a09b91697c76638c7c587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f777c6876638c7c587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f777c6863587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f7768587f517f7801007e817602fc00a06302fd00a063546752687f7801007e81727e7b7b687f75537f7c0376a9148801147f775379645579887567726881766968789263556753687a76026c057f7701147f8263517f7c766301007e817f7c6775006877686b537992635379528763547a6b547a6b677c6b567a6b537a7c717c71716868547a587f7c81547a557964936755795187637c686b687c547f7701207f75748c7a7669765880748c7a76567a876457790376a9147e7c7e557967041976a9147c7e0288ac687e7e5579636c766976748c7a9d58807e6c0376a9147e748c7a7e6c7e7e676c766b8263828c007c80517e846864745aa0637c748c7a76697d937b7b58807e56790376a9147e748c7a7e55797e7e6868686c567a5187637500678263828c007c80517e846868647459a0637c748c7a76697d937b7b58807e55790376a9147e748c7a7e55797e7e687459a0637c748c7a76697d937b7b58807e55790376a9147e748c7a7e55797e7e68687c537a9d547963557958807e041976a91455797e0288ac7e7e68aa87726d77776a14[0-9a-fA-F]{40}(0100|0101).*$")

	// StasSubstringRegexp substring of OP_DUP OP_HASH160 [pubkey hash] OP_EQUALVERIFY OP_CHECKSIG OP_VERIFY ...
	StasSubstringRegexp, _ = regexp.Compile("76a914[0-9a-f]{40}88ac6976aa607f5f7f7c5e7f7c5d7f7c5c7f7c5b7f7c5a7f7c597f7c587f7c577f7c567f7c557f7c547f7c537f7c527f7c517f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7c5f7f7c5e7f7c5d7f7c5c7f7c5b7f7c5a7f7c597f7c587f7c577f7c567f7c557f7c547f7c537f7c527f7c517f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e01007e818b21414136d08c5ed2bf3ba048afe6dcaebafeffffffffffffffffffffffffffffff007d976e7c5296a06394677768827601249301307c7e23022079be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798027e7c7e7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c8276638c687f7c7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e7e01417e21038ff83d8cf12121491609c4939dc11c4aa35503508fe432dc5a5c1905608b9218ad547f7701207f01207f7701247f517f7801007e8102fd00a063546752687f7801007e817f727e7b01177f777b557a766471567a577a786354807e7e676d68aa880067765158a569765187645294567a5379587a7e7e78637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6878637c8c7c53797e577a7e6867567a6876aa587a7d54807e577a597a5a7a786354807e6f7e7eaa727c7e676d6e7eaa7c687b7eaa587a7d877663516752687c72879b69537a647500687c7b547f77517f7853a0916901247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f77788c6301247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f777852946301247f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e816854937f77686877517f7c52797d8b9f7c53a09b91697c76638c7c587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f777c6876638c7c587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f777c6863587f77517f7c01007e817602fc00a06302fd00a063546752687f7c01007e81687f7768587f517f7801007e817602fc00a06302fd00a063546752687f7801007e81727e7b7b687f75537f7c0376a9148801147f775379645579887567726881766968789263556753687a76026c057f7701147f8263517f7c766301007e817f7c6775006877686b537992635379528763547a6b547a6b677c6b567a6b537a7c717c71716868547a587f7c81547a557964936755795187637c686b687c547f7701207f75748c7a7669765880748c7a76567a876457790376a9147e7c7e557967041976a9147c7e0288ac687e7e5579636c766976748c7a9d58807e6c0376a9147e748c7a7e6c7e7e676c766b8263828c007c80517e846864745aa0637c748c7a76697d937b7b58807e56790376a9147e748c7a7e55797e7e6868686c567a5187637500678263828c007c80517e846868647459a0637c748c7a76697d937b7b58807e55790376a9147e748c7a7e55797e7e687459a0637c748c7a76697d937b7b58807e55790376a9147e748c7a7e55797e7e68687c537a9d547963557958807e041976a91455797e0288ac7e7e68aa87726d77776a14[0-9a-fA-F]{40}(0100|0101).*")

	// SensibleRegexp OP_FALSE OP_RETURN 115 101 110 115 105 98 108 101
	// TODO this detects sensible tokens, but is very broad. Make more refined
	SensibleRegexp, _ = regexp.Compile("73656e7369626c65")

	// SensibleSubstringRegexp substring of OP_FALSE OP_RETURN 115 101 110 115 105 98 108 101
	SensibleSubstringRegexp, _ = regexp.Compile("73656e7369626c65")
)
View Source
var ErrCouldNotDetermineDestinationOutput = errors.New("could not determine token output destination")

ErrCouldNotDetermineDestinationOutput error when token output could not be determined

View Source
var ErrDeriveFailed = errors.New("derive addresses failed, missing addresses")

ErrDeriveFailed is when the address derivation failed

View Source
var ErrHDKeyNil = errors.New("hd key is nil")

ErrHDKeyNil is when the HD Key is nil

View Source
var ErrXpubInvalidLength = errors.New("xpub is an invalid length")

ErrXpubInvalidLength is when the length of the xpub does not match the desired length

View Source
var ErrXpubNoMatch = errors.New("xpub key does not match raw key")

ErrXpubNoMatch is when the derived xpub key does not match the key given

Functions

func Decrypt added in v0.2.14

func Decrypt(encryptionKey, data string) (string, error)

Decrypt will take the data and decrypt using a char(64) key

func DeriveAddress added in v0.2.22

func DeriveAddress(hdKey *bip32.ExtendedKey, chain uint32, num uint32) (address string, err error)

DeriveAddress will derive the given address from a key

func DeriveAddresses

func DeriveAddresses(hdKey *bip32.ExtendedKey, num uint32) (external, internal string, err error)

DeriveAddresses will derive the internal and external address from a key

func DeriveChildKeyFromHex

func DeriveChildKeyFromHex(hdKey *bip32.ExtendedKey, hexHash string) (*bip32.ExtendedKey, error)

DeriveChildKeyFromHex derive the child extended key from the hex string

func DerivePrivateKeyFromHex

func DerivePrivateKeyFromHex(hdKey *bip32.ExtendedKey, hexString string) (*bec.PrivateKey, error)

DerivePrivateKeyFromHex will derive the private key from the extended key using the hex as the derivation paths

func DerivePublicKey

func DerivePublicKey(hdKey *bip32.ExtendedKey, chain uint32, num uint32) (*bec.PublicKey, error)

DerivePublicKey will derive the internal and external address from a key

func DerivePublicKeyFromHex

func DerivePublicKeyFromHex(hdKey *bip32.ExtendedKey, hexString string) (*bec.PublicKey, error)

DerivePublicKeyFromHex will derive the public key from the extended key using the hex as the derivation paths

func DownloadAndUnzipFile added in v0.2.14

func DownloadAndUnzipFile(ctx context.Context, client HTTPInterface, file *os.File, URL string) error

DownloadAndUnzipFile download the zip file from the URL and put it's content in the file

func Encrypt added in v0.2.14

func Encrypt(encryptionKey, encryptValue string) (string, error)

Encrypt will encrypt the value using the encryption key

func GetAddressFromScript added in v0.2.14

func GetAddressFromScript(lockingScript string) (address string)

GetAddressFromScript gets the destination address from the given locking script

func GetChildNumsFromHex

func GetChildNumsFromHex(hexHash string) ([]uint32, error)

GetChildNumsFromHex get an array of uint32 numbers from the hex string

func GetDestinationLockingScript added in v0.2.14

func GetDestinationLockingScript(lockingScript string) string

GetDestinationLockingScript gets the destination locking script from non-standard locking scripts that reference a destination (hashed pubkey) - this can be used to determine destination for tokens (like STAS, run etc.)

func GetDestinationType

func GetDestinationType(lockingScript string) string

GetDestinationType Get the type of output script destination

func GetDestinationTypeRegex added in v0.2.14

func GetDestinationTypeRegex(destType string) *regexp.Regexp

GetDestinationTypeRegex Get the regex for destination type

func GetInputSizeForType

func GetInputSizeForType(inputType string) uint64

GetInputSizeForType get an estimated size for the input based on the type

func GetLockingScriptFromSTASLockingScript added in v0.2.14

func GetLockingScriptFromSTASLockingScript(lockingScript string) (string, error)

GetLockingScriptFromSTASLockingScript the the destination lockingScript from a STAS token lockingScript

func GetOutputSize

func GetOutputSize(lockingScript string) uint64

GetOutputSize get an estimated size for the output based on the type

func GetTransactionIDFromHex

func GetTransactionIDFromHex(hex string) (string, error)

GetTransactionIDFromHex get the transaction ID from the given transaction hex

func GetUnlockingScript added in v0.2.14

func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey) (*bscript.Script, error)

GetUnlockingScript will generate an unlocking script

func Hash

func Hash(data string) string

Hash will generate a hash of the given string (used for xPub:hash)

func IsMetanet

func IsMetanet(lockingScript string) bool

IsMetanet Check whether the given string is a metanet output

func IsMultiSig

func IsMultiSig(lockingScript string) bool

IsMultiSig Check whether the given string is a multi-sig locking script

func IsOpReturn

func IsOpReturn(lockingScript string) bool

IsOpReturn Check whether the given string is an op_return

func IsP2PK added in v0.2.33

func IsP2PK(lockingScript string) bool

IsP2PK Check whether the given string is a p2pk output This is the original destination type that was used in the first blocks by Satoshi

func IsP2PKH

func IsP2PKH(lockingScript string) bool

IsP2PKH Check whether the given string is a p2pkh output

func IsP2SH

func IsP2SH(lockingScript string) bool

IsP2SH Check whether the given string is a p2shHex output

func IsRunJS added in v0.2.31

func IsRunJS(lockingScript string) bool

IsRunJS Check whether the given string is a Run JS output

func IsSensible added in v0.4.10

func IsSensible(lockingScript string) bool

IsSensible Check whether the given string is a Sensible token output

func IsStas added in v0.2.14

func IsStas(lockingScript string) bool

IsStas Check whether the given string is a STAS token output

func LittleEndianBytes64 added in v0.5.13

func LittleEndianBytes64(value uint64, resultLength uint32) []byte

LittleEndianBytes64 returns a byte array in little endian from an unsigned integer of 64 bytes.

func RandomHex

func RandomHex(n int) (string, error)

RandomHex returns a random hex string and error

func SafeAssign added in v0.11.0

func SafeAssign[T any](dest *T, src *T)

SafeAssign - Assigns value (not pointer) the src to dest if src is not nil

func SplitOutputValues added in v0.2.32

func SplitOutputValues(satoshis uint64, nrOfOutputs int) ([]uint64, error)

SplitOutputValues splits the satoshis value randomly into nrOfOutputs pieces

func StrOrBytesToString added in v0.14.0

func StrOrBytesToString(value interface{}) (string, error)

StrOrBytesToString converts string or []byte to string or returns an error

func StringInSlice

func StringInSlice(a string, list []string) bool

StringInSlice check whether the string already is in the slice

func ToByteArray added in v0.14.0

func ToByteArray(value interface{}) ([]byte, error)

ToByteArray converts string or []byte to byte array or returns an error

func ValidateXPub

func ValidateXPub(rawKey string) (*bip32.ExtendedKey, error)

ValidateXPub will check the xPub key for length & validation

Types

type FeeUnit

type FeeUnit bt.FeeUnit

FeeUnit fee unit imported from go-bt/v2

func LowestFee added in v0.13.0

func LowestFee(feeUnits []FeeUnit, defaultValue *FeeUnit) *FeeUnit

LowestFee get the lowest fee from a list of fee units, if defaultValue exists and none is found, return defaultValue

func ValidFees added in v0.13.0

func ValidFees(feeUnits []FeeUnit) []FeeUnit

ValidFees filters out invalid fees from a list of fee units

func (*FeeUnit) IsLowerThan added in v0.13.0

func (f *FeeUnit) IsLowerThan(other *FeeUnit) bool

IsLowerThan compare two fee units

func (*FeeUnit) IsValid added in v0.13.0

func (f *FeeUnit) IsValid() bool

IsValid returns true if the Bytes in fee are greater than 0

func (*FeeUnit) IsZero added in v0.13.0

func (f *FeeUnit) IsZero() bool

IsZero returns true if the fee unit suggest no fees (free)

func (*FeeUnit) String added in v0.13.0

func (f *FeeUnit) String() string

String returns the fee unit as a string

type HTTPInterface added in v0.2.14

type HTTPInterface interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPInterface is the HTTP client interface

Jump to

Keyboard shortcuts

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