util

package
v0.9.1 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: ISC Imports: 13 Imported by: 34

README

util

ISC License GoDoc

Package util provides kaspa-specific convenience functions and types.

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-hash, kaspa already supports others and may well support more in the future. This package currently provides implementations for the pay-to-pubkey-hash, and pay-to-script-hash address types.

To decode/encode an address:

addrString := "kaspa:qqfgqp8l9l90zwetj84k2jcac2m8falvvyy8xjtnhd"
defaultPrefix := util.Bech32PrefixKaspa
addr, err := util.DecodeAddress(addrString, defaultPrefix)
if err != nil {
	fmt.Println(err)
	return
}
fmt.Println(addr.EncodeAddress())

Index

Examples

Constants

View Source
const (
	// SompiPerKaspa is the number of sompi in one kaspa (1 KAS).
	SompiPerKaspa = 100000000

	// MaxSompi is the maximum transaction amount allowed in sompi.
	MaxSompi = 21000000 * SompiPerKaspa
)

Variables

View Source
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 AppDataDir

func AppDataDir(appName string, roaming bool) string

AppDataDir 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 := AppDataDir("myapp", false)
 POSIX (Linux/BSD): ~/.myapp
 Mac OS: $HOME/Library/Application Support/Myapp
 Windows: %LOCALAPPDATA%\Myapp
 Plan 9: $home/myapp

func Hash160

func Hash160(buf []byte) []byte

Hash160 calculates the hash ripemd160(sha256(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 P2PKH 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), pay-to-pubkey-hash (P2PKH), 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 AddressPubKeyHash

type AddressPubKeyHash struct {
	// contains filtered or unexported fields
}

AddressPubKeyHash is an Address for a pay-to-pubkey-hash (P2PKH) transaction.

func NewAddressPubKeyHash

func NewAddressPubKeyHash(pkHash []byte, prefix Bech32Prefix) (*AddressPubKeyHash, error)

NewAddressPubKeyHash returns a new AddressPubKeyHash. pkHash mustbe 20 bytes.

func NewAddressPubKeyHashFromPublicKey

func NewAddressPubKeyHashFromPublicKey(publicKey []byte, prefix Bech32Prefix) (*AddressPubKeyHash, error)

NewAddressPubKeyHashFromPublicKey returns a new AddressPubKeyHash from given public key

func (*AddressPubKeyHash) EncodeAddress

func (a *AddressPubKeyHash) EncodeAddress() string

EncodeAddress returns the string encoding of a pay-to-pubkey-hash address. Part of the Address interface.

func (*AddressPubKeyHash) Hash160

func (a *AddressPubKeyHash) Hash160() *[ripemd160.Size]byte

Hash160 returns the underlying array of the pubkey hash. This can be useful when an array is more appropiate than a slice (for example, when used as map keys).

func (*AddressPubKeyHash) IsForPrefix

func (a *AddressPubKeyHash) IsForPrefix(prefix Bech32Prefix) bool

IsForPrefix returns whether or not the pay-to-pubkey-hash address is associated with the passed kaspa network.

func (*AddressPubKeyHash) Prefix

func (a *AddressPubKeyHash) Prefix() Bech32Prefix

Prefix returns the prefix for this address

func (*AddressPubKeyHash) ScriptAddress

func (a *AddressPubKeyHash) ScriptAddress() []byte

ScriptAddress returns the bytes to be included in a txout script to pay to a pubkey hash. Part of the Address interface.

func (*AddressPubKeyHash) String

func (a *AddressPubKeyHash) String() string

String returns a human-readable string for the pay-to-pubkey-hash 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-hash (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) Hash160

func (a *AddressScriptHash) Hash160() *[ripemd160.Size]byte

Hash160 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

func NewAmount(f float64) (Amount, error)

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.

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

func (a Amount) MulF64(f float64) Amount

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) String

func (a Amount) String() string

String is the equivalent of calling Format with AmountKAS.

func (Amount) ToKAS

func (a Amount) ToKAS() float64

ToKAS is the equivalent of calling ToUnit with AmountKAS.

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

Directories

Path Synopsis
Package bech32 provides a Go implementation of the bech32 format.
Package bech32 provides a Go implementation of the bech32 format.

Jump to

Keyboard shortcuts

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