utils

package
v0.2.17 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2022 License: Apache-2.0 Imports: 29 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 (
	// 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"
)
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).*")
)
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 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 GetModelBoolAttribute

func GetModelBoolAttribute(model interface{}, attribute string) *bool

GetModelBoolAttribute the attribute from the model as a bool

func GetModelName

func GetModelName(model interface{}) *string

GetModelName get the name of the model via reflection

func GetModelStringAttribute

func GetModelStringAttribute(model interface{}, attribute string) *string

GetModelStringAttribute the attribute from the model as a string

func GetModelTableName

func GetModelTableName(model interface{}) *string

GetModelTableName get the db table name of the model via reflection

func GetModelType

func GetModelType(model interface{}) reflect.Type

GetModelType get the model type of the model interface via reflection

func GetModelUnset

func GetModelUnset(model interface{}) map[string]bool

GetModelUnset gets any empty values on the model and makes sure the update actually unsets those values in the database, otherwise this never happens, and we cannot unset

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 IsModelSlice

func IsModelSlice(model interface{}) bool

IsModelSlice returns true if the given interface is a slice of models

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 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 IsStas added in v0.2.14

func IsStas(lockingScript string) bool

IsStas Check whether the given string is a STAS token output

func MarshalNullString

func MarshalNullString(x NullString) graphql.Marshaler

MarshalNullString is used by graphql to marshal into a string

func MarshalNullTime

func MarshalNullTime(x NullTime) graphql.Marshaler

MarshalNullTime is used by GraphQL to marshal the value

func RandomHex

func RandomHex(n int) (string, error)

RandomHex returns a random hex string and error

func StringInSlice

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

StringInSlice check whether the string already is in the slice

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

type HTTPInterface added in v0.2.14

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

HTTPInterface is the HTTP client interface

type NullString

type NullString struct {
	sql.NullString
}

NullString wrapper around sql.NullString

func UnmarshalNullString

func UnmarshalNullString(s interface{}) (NullString, error)

UnmarshalNullString is used by graphql to unmarshal from a NullString into a string

func (NullString) IsZero

func (x NullString) IsZero() bool

IsZero method is called by bson.IsZero in Mongo for type = NullTime

func (*NullString) MarshalBSONValue

func (x *NullString) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue method is called by bson.Marshal in Mongo for type = NullString

func (*NullString) MarshalJSON

func (x *NullString) MarshalJSON() ([]byte, error)

MarshalJSON method is called by the JSON marshaller

func (*NullString) UnmarshalBSONValue

func (x *NullString) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue method is called by bson.Unmarshal in Mongo for type = NullString

func (*NullString) UnmarshalJSON

func (x *NullString) UnmarshalJSON(data []byte) error

UnmarshalJSON method is called by the JSON unmarshaller

type NullTime

type NullTime struct {
	sql.NullTime
}

NullTime wrapper around sql.NullTime

func UnmarshalNullTime

func UnmarshalNullTime(t interface{}) (NullTime, error)

UnmarshalNullTime is used by GraphQL to unmarshal the value

func (NullTime) IsZero

func (x NullTime) IsZero() bool

IsZero method is called by bson.IsZero in Mongo for type = NullTime

func (*NullTime) MarshalBSONValue

func (x *NullTime) MarshalBSONValue() (bsontype.Type, []byte, error)

MarshalBSONValue method is called by bson.Marshal in Mongo for type = NullTime

func (*NullTime) MarshalJSON

func (x *NullTime) MarshalJSON() ([]byte, error)

MarshalJSON method is called by the JSON marshaller

func (*NullTime) UnmarshalBSONValue

func (x *NullTime) UnmarshalBSONValue(t bsontype.Type, data []byte) error

UnmarshalBSONValue method is called by bson.Unmarshal in Mongo for type = NullTime

func (*NullTime) UnmarshalJSON

func (x *NullTime) UnmarshalJSON(data []byte) error

UnmarshalJSON method is called by the JSON unmarshaller

Jump to

Keyboard shortcuts

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