Documentation ¶
Overview ¶
Package util provides kaspa-specific convenience functions and types.
Block Overview ¶
A Block defines a kaspa block that provides easier and more efficient manipulation of raw blocks. It also memoizes hashes for the block and its transactions on their first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.
Tx Overview ¶
A Tx defines a kaspa transaction that provides more efficient manipulation of raw transactions. It memoizes the hash for the transaction on its first access so subsequent accesses don't have to repeat the relatively expensive hashing operations.
Address Overview ¶
The Address interface provides an abstraction for a kaspa address. While the most common type is a pay-to-pubkey, kaspa already supports others and may well support more in the future. This package currently provides implementations for the pay-to-pubkey, and pay-to-script-hash address types.
To decode/encode an address:
addrString := "kaspa:qqj9fg59mptxkr9j0y53j5mwurcmda5mtza9n6v9pm9uj8h0wgk6uma5pvumr" defaultPrefix := util.Bech32PrefixKaspa addr, err := util.DecodeAddress(addrString, defaultPrefix) if err != nil { fmt.Println(err) return } fmt.Println(addr.EncodeAddress())
Index ¶
- Constants
- Variables
- func AppDir(appName string, roaming bool) string
- func HashBlake2b(buf []byte) []byte
- type Address
- type AddressPublicKey
- type AddressPublicKeyECDSA
- type AddressScriptHash
- func (a *AddressScriptHash) EncodeAddress() string
- func (a *AddressScriptHash) HashBlake2b() *[blake2b.Size256]byte
- func (a *AddressScriptHash) IsForPrefix(prefix Bech32Prefix) bool
- func (a *AddressScriptHash) Prefix() Bech32Prefix
- func (a *AddressScriptHash) ScriptAddress() []byte
- func (a *AddressScriptHash) String() string
- type Amount
- type AmountUnit
- type Bech32Prefix
Examples ¶
Constants ¶
const PublicKeySize = 32
PublicKeySize is the public key size for a schnorr public key
const PublicKeySizeECDSA = 33
PublicKeySizeECDSA is the public key size for an ECDSA public key
Variables ¶
var ( // ErrUnknownAddressType describes an error where an address can not // decoded as a specific address type due to the string encoding // begining with an identifier byte unknown to any standard or // registered (via dagconfig.Register) network. ErrUnknownAddressType = errors.New("unknown address type") )
Functions ¶
func AppDir ¶ added in v0.10.0
AppDir returns an operating system specific directory to be used for storing application data for an application.
The appName parameter is the name of the application the data directory is being requested for. This function will prepend a period to the appName for POSIX style operating systems since that is standard practice. An empty appName or one with a single dot is treated as requesting the current directory so only "." will be returned. Further, the first character of appName will be made lowercase for POSIX style operating systems and uppercase for Mac and Windows since that is standard practice.
The roaming parameter only applies to Windows where it specifies the roaming application data profile (%APPDATA%) should be used instead of the local one (%LOCALAPPDATA%) that is used by default.
Example results:
dir := AppDir("myapp", false) POSIX (Linux/BSD): ~/.myapp Mac OS: $HOME/Library/Application Support/Myapp Windows: %LOCALAPPDATA%\Myapp Plan 9: $home/myapp
func HashBlake2b ¶ added in v0.10.0
HashBlake2b calculates the hash blake2b(b).
Types ¶
type Address ¶
type Address interface { // String returns the string encoding of the transaction output // destination. // // Please note that String differs subtly from EncodeAddress: String // will return the value as a string without any conversion, while // EncodeAddress may convert destination types (for example, // converting pubkeys to P2PK addresses) before encoding as a // payment address string. String() string // EncodeAddress returns the string encoding of the payment address // associated with the Address value. See the comment on String // for how this method differs from String. EncodeAddress() string // ScriptAddress returns the raw bytes of the address to be used // when inserting the address into a txout's script. ScriptAddress() []byte // Prefix returns the prefix for this address Prefix() Bech32Prefix // IsForPrefix returns whether or not the address is associated with the // passed kaspa network. IsForPrefix(prefix Bech32Prefix) bool }
Address is an interface type for any type of destination a transaction output may spend to. This includes pay-to-pubkey (P2PK) and pay-to-script-hash (P2SH). Address is designed to be generic enough that other kinds of addresses may be added in the future without changing the decoding and encoding API.
func DecodeAddress ¶
func DecodeAddress(addr string, expectedPrefix Bech32Prefix) (Address, error)
DecodeAddress decodes the string encoding of an address and returns the Address if addr is a valid encoding for a known address type.
If any expectedPrefix except Bech32PrefixUnknown is passed, it is compared to the prefix extracted from the address, and if the two do not match - an error is returned
type AddressPublicKey ¶ added in v0.10.0
type AddressPublicKey struct {
// contains filtered or unexported fields
}
AddressPublicKey is an Address for a pay-to-pubkey (P2PK) transaction.
func NewAddressPublicKey ¶ added in v0.10.0
func NewAddressPublicKey(publicKey []byte, prefix Bech32Prefix) (*AddressPublicKey, error)
NewAddressPublicKey returns a new AddressPublicKey. publicKey must be 32 bytes.
func (*AddressPublicKey) EncodeAddress ¶ added in v0.10.0
func (a *AddressPublicKey) EncodeAddress() string
EncodeAddress returns the string encoding of a pay-to-pubkey address. Part of the Address interface.
func (*AddressPublicKey) IsForPrefix ¶ added in v0.10.0
func (a *AddressPublicKey) IsForPrefix(prefix Bech32Prefix) bool
IsForPrefix returns whether or not the pay-to-pubkey address is associated with the passed kaspa network.
func (*AddressPublicKey) Prefix ¶ added in v0.10.0
func (a *AddressPublicKey) Prefix() Bech32Prefix
Prefix returns the prefix for this address
func (*AddressPublicKey) ScriptAddress ¶ added in v0.10.0
func (a *AddressPublicKey) ScriptAddress() []byte
ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey. Part of the Address interface.
func (*AddressPublicKey) String ¶ added in v0.10.0
func (a *AddressPublicKey) String() string
String returns a human-readable string for the pay-to-pubkey address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
type AddressPublicKeyECDSA ¶ added in v0.10.0
type AddressPublicKeyECDSA struct {
// contains filtered or unexported fields
}
AddressPublicKeyECDSA is an Address for a pay-to-pubkey (P2PK) ECDSA transaction.
func NewAddressPublicKeyECDSA ¶ added in v0.10.0
func NewAddressPublicKeyECDSA(publicKey []byte, prefix Bech32Prefix) (*AddressPublicKeyECDSA, error)
NewAddressPublicKeyECDSA returns a new AddressPublicKeyECDSA. publicKey must be 33 bytes.
func (*AddressPublicKeyECDSA) EncodeAddress ¶ added in v0.10.0
func (a *AddressPublicKeyECDSA) EncodeAddress() string
EncodeAddress returns the string encoding of a pay-to-pubkey address. Part of the Address interface.
func (*AddressPublicKeyECDSA) IsForPrefix ¶ added in v0.10.0
func (a *AddressPublicKeyECDSA) IsForPrefix(prefix Bech32Prefix) bool
IsForPrefix returns whether or not the pay-to-pubkey address is associated with the passed kaspa network.
func (*AddressPublicKeyECDSA) Prefix ¶ added in v0.10.0
func (a *AddressPublicKeyECDSA) Prefix() Bech32Prefix
Prefix returns the prefix for this address
func (*AddressPublicKeyECDSA) ScriptAddress ¶ added in v0.10.0
func (a *AddressPublicKeyECDSA) ScriptAddress() []byte
ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey. Part of the Address interface.
func (*AddressPublicKeyECDSA) String ¶ added in v0.10.0
func (a *AddressPublicKeyECDSA) String() string
String returns a human-readable string for the pay-to-pubkey address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
type AddressScriptHash ¶
type AddressScriptHash struct {
// contains filtered or unexported fields
}
AddressScriptHash is an Address for a pay-to-script-publicKey (P2SH) transaction.
func NewAddressScriptHash ¶
func NewAddressScriptHash(serializedScript []byte, prefix Bech32Prefix) (*AddressScriptHash, error)
NewAddressScriptHash returns a new AddressScriptHash.
func NewAddressScriptHashFromHash ¶
func NewAddressScriptHashFromHash(scriptHash []byte, prefix Bech32Prefix) (*AddressScriptHash, error)
NewAddressScriptHashFromHash returns a new AddressScriptHash. scriptHash must be 20 bytes.
func (*AddressScriptHash) EncodeAddress ¶
func (a *AddressScriptHash) EncodeAddress() string
EncodeAddress returns the string encoding of a pay-to-script-hash address. Part of the Address interface.
func (*AddressScriptHash) HashBlake2b ¶ added in v0.10.0
func (a *AddressScriptHash) HashBlake2b() *[blake2b.Size256]byte
HashBlake2b returns the underlying array of the script hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).
func (*AddressScriptHash) IsForPrefix ¶
func (a *AddressScriptHash) IsForPrefix(prefix Bech32Prefix) bool
IsForPrefix returns whether or not the pay-to-script-hash address is associated with the passed kaspa network.
func (*AddressScriptHash) Prefix ¶
func (a *AddressScriptHash) Prefix() Bech32Prefix
Prefix returns the prefix for this address
func (*AddressScriptHash) ScriptAddress ¶
func (a *AddressScriptHash) ScriptAddress() []byte
ScriptAddress returns the bytes to be included in a txout script to pay to a script hash. Part of the Address interface.
func (*AddressScriptHash) String ¶
func (a *AddressScriptHash) String() string
String returns a human-readable string for the pay-to-script-hash address. This is equivalent to calling EncodeAddress, but is provided so the type can be used as a fmt.Stringer.
type Amount ¶
type Amount uint64
Amount represents the base kaspa monetary unit (colloquially referred to as a `Sompi'). A single Amount is equal to 1e-8 of a kaspa.
Example ¶
package main import ( "fmt" "github.com/kaspanet/kaspad/util" ) func main() { a := util.Amount(0) fmt.Println("Zero Sompi:", a) a = util.Amount(1e8) fmt.Println("100,000,000 Sompi:", a) a = util.Amount(1e5) fmt.Println("100,000 Sompi:", a) }
Output: Zero Sompi: 0 KAS 100,000,000 Sompi: 1 KAS 100,000 Sompi: 0.001 KAS
Example (UnitConversions) ¶
package main import ( "fmt" "github.com/kaspanet/kaspad/util" ) func main() { amount := util.Amount(44433322211100) fmt.Println("Sompi to kKAS:", amount.Format(util.AmountKiloKAS)) fmt.Println("Sompi to KAS:", amount) fmt.Println("Sompi to MilliKAS:", amount.Format(util.AmountMilliKAS)) fmt.Println("Sompi to MicroKAS:", amount.Format(util.AmountMicroKAS)) fmt.Println("Sompi to Sompi:", amount.Format(util.AmountSompi)) }
Output: Sompi to kKAS: 444.333222111 kKAS Sompi to KAS: 444333.222111 KAS Sompi to MilliKAS: 444333222.111 mKAS Sompi to MicroKAS: 444333222111 μKAS Sompi to Sompi: 44433322211100 Sompi
func NewAmount ¶
NewAmount creates an Amount from a floating point value representing some value in kaspa. NewAmount errors if f is NaN or +-Infinity, but does not check that the amount is within the total amount of kaspa producible as f may not refer to an amount at a single moment in time.
NewAmount is for specifically for converting KAS to Sompi. For creating a new Amount with an int64 value which denotes a quantity of Sompi, do a simple type conversion from type int64 to Amount. TODO: Refactor NewAmount. When amounts are more than 1e9 KAS, the precision can be higher than one sompi (1e9 and 1e9+1e-8 will result as the same number)
Example ¶
package main import ( "fmt" "math" "github.com/kaspanet/kaspad/util" ) func main() { amountOne, err := util.NewAmount(1) if err != nil { fmt.Println(err) return } fmt.Println(amountOne) //Output 1 amountFraction, err := util.NewAmount(0.01234567) if err != nil { fmt.Println(err) return } fmt.Println(amountFraction) //Output 2 amountZero, err := util.NewAmount(0) if err != nil { fmt.Println(err) return } fmt.Println(amountZero) //Output 3 amountNaN, err := util.NewAmount(math.NaN()) if err != nil { fmt.Println(err) return } fmt.Println(amountNaN) //Output 4 }
Output: 1 KAS 0.01234567 KAS 0 KAS invalid kaspa amount
func (Amount) Format ¶
func (a Amount) Format(u AmountUnit) string
Format formats a monetary amount counted in kaspa base units as a string for a given unit. The conversion will succeed for any unit, however, known units will be formated with an appended label describing the units with SI notation, or "Sompi" for the base unit.
func (Amount) MulF64 ¶
MulF64 multiplies an Amount by a floating point value. While this is not an operation that must typically be done by a full node or wallet, it is useful for services that build on top of kaspa (for example, calculating a fee by multiplying by a percentage).
func (Amount) ToUnit ¶
func (a Amount) ToUnit(u AmountUnit) float64
ToUnit converts a monetary amount counted in kaspa base units to a floating point value representing an amount of kaspa.
type AmountUnit ¶
type AmountUnit int
AmountUnit describes a method of converting an Amount to something other than the base unit of a kaspa. The value of the AmountUnit is the exponent component of the decadic multiple to convert from an amount in kaspa to an amount counted in units.
const ( AmountMegaKAS AmountUnit = 6 AmountKiloKAS AmountUnit = 3 AmountKAS AmountUnit = 0 AmountMilliKAS AmountUnit = -3 AmountMicroKAS AmountUnit = -6 AmountSompi AmountUnit = -8 )
These constants define various units used when describing a kaspa monetary amount.
func (AmountUnit) String ¶
func (u AmountUnit) String() string
String returns the unit as a string. For recognized units, the SI prefix is used, or "Sompi" for the base unit. For all unrecognized units, "1eN KAS" is returned, where N is the AmountUnit.
type Bech32Prefix ¶
type Bech32Prefix int
Bech32Prefix is the human-readable prefix for a Bech32 address.
const ( // Unknown/Erroneous prefix Bech32PrefixUnknown Bech32Prefix = iota // Prefix for the main network. Bech32PrefixKaspa // Prefix for the dev network. Bech32PrefixKaspaDev // Prefix for the test network. Bech32PrefixKaspaTest // Prefix for the simulation network. Bech32PrefixKaspaSim )
Constants that define Bech32 address prefixes. Every network is assigned a unique prefix.
func ParsePrefix ¶
func ParsePrefix(prefixString string) (Bech32Prefix, error)
ParsePrefix attempts to parse a Bech32 address prefix.
func (Bech32Prefix) String ¶
func (prefix Bech32Prefix) String() string
Converts from Bech32 address prefixes to their string values