Documentation ¶
Overview ¶
Package utils is used for generic methods and values that are used across all packages
Index ¶
- Constants
- Variables
- func Decrypt(encryptionKey, data string) (string, error)
- func DeriveAddress(hdKey *bip32.ExtendedKey, chain uint32, num uint32) (address string, err error)
- func DeriveAddresses(hdKey *bip32.ExtendedKey, num uint32) (external, internal string, err error)
- func DeriveChildKeyFromHex(hdKey *bip32.ExtendedKey, hexHash string) (*bip32.ExtendedKey, error)
- func DerivePrivateKeyFromHex(hdKey *bip32.ExtendedKey, hexString string) (*bec.PrivateKey, error)
- func DerivePublicKey(hdKey *bip32.ExtendedKey, chain uint32, num uint32) (*bec.PublicKey, error)
- func DerivePublicKeyFromHex(hdKey *bip32.ExtendedKey, hexString string) (*bec.PublicKey, error)
- func Encrypt(encryptionKey, encryptValue string) (string, error)
- func GetAddressFromScript(lockingScript string) (address string)
- func GetChildNumsFromHex(hexHash string) ([]uint32, error)
- func GetDestinationLockingScript(lockingScript string) string
- func GetDestinationType(lockingScript string) string
- func GetDestinationTypeRegex(destType string) *regexp.Regexp
- func GetInputSizeForType(inputType string) uint64
- func GetLockingScriptFromSTASLockingScript(lockingScript string) (string, error)
- func GetOutputSize(lockingScript string) uint64
- func GetTransactionIDFromHex(hex string) (string, error)
- func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey) (*bscript.Script, error)
- func Hash(data string) string
- func HashAdler32(input string) (string, error)
- func IsMetanet(lockingScript string) bool
- func IsMultiSig(lockingScript string) bool
- func IsOpReturn(lockingScript string) bool
- func IsP2PK(lockingScript string) bool
- func IsP2PKH(lockingScript string) bool
- func IsP2SH(lockingScript string) bool
- func IsRunJS(lockingScript string) bool
- func IsSensible(lockingScript string) bool
- func IsStas(lockingScript string) bool
- func LittleEndianBytes64(value uint64, resultLength uint32) []byte
- func RandomHex(n int) (string, error)
- func SafeAssign[T any](dest *T, src *T)
- func SplitOutputValues(satoshis uint64, nrOfOutputs int) ([]uint64, error)
- func StrOrBytesToString(value interface{}) (string, error)
- func StringInSlice(a string, list []string) bool
- func ToByteArray(value interface{}) ([]byte, error)
- func ValidateXPub(rawKey string) (*bip32.ExtendedKey, error)
- type FeeUnit
- type HTTPInterface
Constants ¶
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 )
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 ¶
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") )
var ErrCouldNotDetermineDestinationOutput = errors.New("could not determine token output destination")
ErrCouldNotDetermineDestinationOutput error when token output could not be determined
var ErrDeriveFailed = errors.New("derive addresses failed, missing addresses")
ErrDeriveFailed is when the address derivation failed
var ErrHDKeyNil = errors.New("hd key is nil")
ErrHDKeyNil is when the HD Key is nil
var ErrXpubInvalidLength = errors.New("xpub is an invalid length")
ErrXpubInvalidLength is when the length of the xpub does not match the desired length
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 DeriveAddress ¶
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 ¶
DerivePublicKey will derive the internal and external address from a key
func DerivePublicKeyFromHex ¶
DerivePublicKeyFromHex will derive the public key from the extended key using the hex as the derivation paths
func GetAddressFromScript ¶
GetAddressFromScript gets the destination address from the given locking script
func GetChildNumsFromHex ¶
GetChildNumsFromHex get an array of uint32 numbers from the hex string
func GetDestinationLockingScript ¶
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 ¶
GetDestinationType Get the type of output script destination
func GetDestinationTypeRegex ¶
GetDestinationTypeRegex Get the regex for destination type
func GetInputSizeForType ¶
GetInputSizeForType get an estimated size for the input based on the type
func GetLockingScriptFromSTASLockingScript ¶
GetLockingScriptFromSTASLockingScript the the destination lockingScript from a STAS token lockingScript
func GetOutputSize ¶
GetOutputSize get an estimated size for the output based on the type
func GetTransactionIDFromHex ¶
GetTransactionIDFromHex get the transaction ID from the given transaction hex
func GetUnlockingScript ¶
func GetUnlockingScript(tx *bt.Tx, inputIndex uint32, privateKey *bec.PrivateKey) (*bscript.Script, error)
GetUnlockingScript will generate an unlocking script
func HashAdler32 ¶
HashAdler32 returns computed string calculated with Adler32 function.
func IsMultiSig ¶
IsMultiSig Check whether the given string is a multi-sig locking script
func IsOpReturn ¶
IsOpReturn Check whether the given string is an op_return
func IsP2PK ¶
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 IsSensible ¶
IsSensible Check whether the given string is a Sensible token output
func LittleEndianBytes64 ¶
LittleEndianBytes64 returns a byte array in little endian from an unsigned integer of 64 bytes.
func SafeAssign ¶
func SafeAssign[T any](dest *T, src *T)
SafeAssign - Assigns value (not pointer) the src to dest if src is not nil
func SplitOutputValues ¶
SplitOutputValues splits the satoshis value randomly into nrOfOutputs pieces
func StrOrBytesToString ¶
StrOrBytesToString converts string or []byte to string or returns an error
func StringInSlice ¶
StringInSlice check whether the string already is in the slice
func ToByteArray ¶
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 ¶
LowestFee get the lowest fee from a list of fee units, if defaultValue exists and none is found, return defaultValue
func (*FeeUnit) IsLowerThan ¶
IsLowerThan compare two fee units