libada

package module
v0.0.1-rc Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2021 License: MIT Imports: 12 Imported by: 0

README

libada-go

PkgGoDev ci

golang library for Cardano network, it is used and maintained by Bitrue.

Documentation

Index

Examples

Constants

View Source
const (
	B32Prefix     = "addr"
	TestnetSuffix = "_test"
	StakePrefix   = "stake"
	Hash28Size    = 28
)
View Source
const (
	XPubSize              = 64
	ByronPubKeyTag uint64 = 0
	ByronTag       uint64 = 24
)

Variables

View Source
var ErrInvalidCredSize = errors.New("stake credential size is not 28")

Functions

func Blake2b224

func Blake2b224(raw []byte) []byte

func Sha3AndBlake2b224

func Sha3AndBlake2b224(raw []byte) []byte

func VariableNatDecode

func VariableNatDecode(bytes []byte) (uint64, int, bool)

func VariableNatEncode

func VariableNatEncode(n uint64) []byte

Types

type Address

type Address interface {
	cbor.Marshaler

	// Raw bytes for transaction outputs
	Bytes() []byte
	// Readable and humanable string
	String() string
	// Bech32 prefix, and empty for legacy address
	Prefix() string
	// address kind
	Kind() AddressKind
	// address network
	GetNetwork() Network
}

func DecodeAddress

func DecodeAddress(raw string) (res Address, err error)

DecodeAddress decode humanable address it should be invalid if you get an error for an exchange, you should also check address network and address type

Example
RawAddressList := []string{
	"addr1v9wa6entm75duchtu50mu6u6hkagdgqzaevt0cwryaw3pnca870vt",
	"Ae2tdPwUPEZCdHLQP8Nke2AogJ7VgGiHbJDV3N1mWyQxy5AhwGgihL44t8w",
	"addr1q87ywcqelrrm9zrn4f9v5te2ss2w6f0j4ca39pwhsgxnupuec8u08zuyfu64xecytwcuc8nm6xkn0xj2sqx7m7g07fqqf96uvu",
	"DdzFFzCqrht7FAf8MpryP1p8sgkmFRUnDpifnnu4ZxpBjbCTSDwJVAaDsDqrC7SLYFx8fUrDcNNsD4AMiUgg2wGywTVpcfB1F3AHrGkv",
}

for _, address := range RawAddressList {
	got := MustDecodeAddress(address).String()
	fmt.Println(got == address)
}
Output:

true
true
true
true

func DecodeRawAddress

func DecodeRawAddress(s []byte) (Address, error)

DecodeRawAddresss decodes raw address bytes it decode raw address in tx output, you may don't need it

func MustDecodeAddress

func MustDecodeAddress(raw string) Address

type AddressKind

type AddressKind byte
const (
	LegacyAddressKind AddressKind = iota
	BaseAddressKind
	PointerAddressKind
	EnterpriseAddressKind
	RewardAddressKind
)

func (AddressKind) String

func (i AddressKind) String() string

type Asset

type Asset struct {
	PolicyId [28]byte
	Name     AssetName
}

func MustParseAssetId

func MustParseAssetId(assetId string) *Asset

func NewAsset

func NewAsset(policyId [28]byte, name AssetName) *Asset

func ParseAssetId

func ParseAssetId(assetId string) (*Asset, error)

type AssetName

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

func NewAssetName

func NewAssetName(b []byte) AssetName

func (AssetName) MarshalCBOR

func (a AssetName) MarshalCBOR() ([]byte, error)

func (AssetName) String

func (a AssetName) String() string

func (*AssetName) UnmarshalCBOR

func (a *AssetName) UnmarshalCBOR(raw []byte) error

type BaseAddress

type BaseAddress struct {
	Network Network
	Payment StakeCredential
	Stake   StakeCredential
}

func (*BaseAddress) Bytes

func (b *BaseAddress) Bytes() []byte

func (*BaseAddress) GetNetwork

func (b *BaseAddress) GetNetwork() Network

func (*BaseAddress) Kind

func (b *BaseAddress) Kind() AddressKind

func (*BaseAddress) MarshalCBOR

func (b *BaseAddress) MarshalCBOR() ([]byte, error)

func (*BaseAddress) Prefix

func (b *BaseAddress) Prefix() string

func (*BaseAddress) String

func (b *BaseAddress) String() string

type BootstrapWitness

type BootstrapWitness struct {
	VKey      []byte // pubkey(32 bytes)
	Sign      []byte // ed25519 signature
	Chaincode []byte // chaincode
	Attribute []byte // byron address attributes
	// contains filtered or unexported fields
}

BootstrapWitness the witness for Byron address

func NewBootstrapWitness

func NewBootstrapWitness(addr *LegacyAddress, xpub, sig []byte) (*BootstrapWitness, error)

type EnterpriseAddress

type EnterpriseAddress struct {
	Network Network
	Payment StakeCredential
}

func NewEnterpriseAddress

func NewEnterpriseAddress(pubkey []byte, kind StakeCredentialType, network Network) *EnterpriseAddress

NewEnterpriseAddress creates Enterprise type address with public key the pubkey param can be pubkeyHash or 32 bytes pubkey or 64 bytes xpub

func NewKeyedEnterpriseAddress

func NewKeyedEnterpriseAddress(key []byte, network Network) *EnterpriseAddress

func (*EnterpriseAddress) Bytes

func (e *EnterpriseAddress) Bytes() []byte

func (*EnterpriseAddress) GetNetwork

func (e *EnterpriseAddress) GetNetwork() Network

func (*EnterpriseAddress) Kind

func (e *EnterpriseAddress) Kind() AddressKind

func (*EnterpriseAddress) MarshalCBOR

func (e *EnterpriseAddress) MarshalCBOR() ([]byte, error)

func (*EnterpriseAddress) Prefix

func (e *EnterpriseAddress) Prefix() string

func (*EnterpriseAddress) String

func (e *EnterpriseAddress) String() string

type Input

type Input struct {
	TxHash []byte // blake2b256
	Index  uint32
	// contains filtered or unexported fields
}

func MustInput

func MustInput(txid string, index uint32) *Input

func NewInput

func NewInput(txid string, index uint32) (*Input, error)

type KeysWitness

type KeysWitness struct {
	VKey []byte // 32bytes pubkey
	Sign []byte // ed25519 sign
	// contains filtered or unexported fields
}

KeysWithness the witness for Shelly address

func NewKeysWitness

func NewKeysWitness(vkey, sign []byte) *KeysWitness

type LegacyAddress

type LegacyAddress struct {
	Hashed []byte
	Attrs  LegacyAddressAttribute
	Tag    uint64 // Byron only supports PubKeyTag(0)
}

func NewSimpleLegacyAddress

func NewSimpleLegacyAddress(xpub []byte, networks ...uint32) (*LegacyAddress, error)

func (*LegacyAddress) Bytes

func (a *LegacyAddress) Bytes() []byte

func (*LegacyAddress) GetNetwork

func (a *LegacyAddress) GetNetwork() Network

func (*LegacyAddress) Kind

func (a *LegacyAddress) Kind() AddressKind

func (*LegacyAddress) MarshalCBOR

func (a *LegacyAddress) MarshalCBOR() ([]byte, error)

func (*LegacyAddress) Prefix

func (a *LegacyAddress) Prefix() string

func (*LegacyAddress) String

func (a *LegacyAddress) String() string

func (*LegacyAddress) UnmarshalCBOR

func (a *LegacyAddress) UnmarshalCBOR(data []byte) error

type LegacyAddressAttribute

type LegacyAddressAttribute struct {
	HDPayload []byte
	Network   *uint32
}

func (LegacyAddressAttribute) MarshalCBOR

func (a LegacyAddressAttribute) MarshalCBOR() ([]byte, error)

func (*LegacyAddressAttribute) UnmarshalCBOR

func (a *LegacyAddressAttribute) UnmarshalCBOR(data []byte) error

type Network

type Network byte
const (
	Testnet Network = 0
	Mainnet Network = 1
)

func (Network) Id

func (n Network) Id() uint8

func (Network) ProtocolMagic

func (n Network) ProtocolMagic() uint32

func (Network) String

func (i Network) String() string

type Output

type Output struct {
	Address Address
	Amount  uint64
	Assets  map[[28]byte]map[AssetName]uint64
}

Output transaction output

func MustOutput

func MustOutput(addr string, amount uint64) *Output

func MustOutputWithAssets

func MustOutputWithAssets(addr string, amount uint64, assetId string, tokenAmount uint64) *Output

func NewOutput

func NewOutput(addr Address, amount uint64) *Output

func NewOutputWithAsset

func NewOutputWithAsset(addr Address, amount uint64, token *Asset, tokenAmount uint64) *Output

func (*Output) AddAmount

func (o *Output) AddAmount(amount uint64) *Output

AddAmount adds ADA for Output

func (*Output) AddAsset

func (o *Output) AddAsset(asset *Asset, tokenAmount uint64) *Output

AddAsset appends asset and asset amount to Outputs

func (*Output) AddAssetMap

func (o *Output) AddAssetMap(raw map[[28]byte]map[AssetName]uint64)

AddAssetMap copys raw asset to outputs

func (*Output) MarshalCBOR

func (o *Output) MarshalCBOR() ([]byte, error)

func (*Output) MinAdaRequired

func (o *Output) MinAdaRequired() uint64

MinAdaRequired computes required ada for this output learn more at https://docs.cardano.org/native-tokens/minimum-ada-value-requirement

func (*Output) MustAddAsset

func (o *Output) MustAddAsset(assetId string, tokenAmount uint64) *Output

func (*Output) SetAmount

func (o *Output) SetAmount(amount uint64) *Output

SetAmount sets amount for Output

func (*Output) SetMinAda

func (o *Output) SetMinAda() *Output

SetMinAda sets min ada required

func (*Output) UnmarshalCBOR

func (o *Output) UnmarshalCBOR(data []byte) error

type PointerAddress

type PointerAddress struct {
	Network Network
	Payment StakeCredential
	Stake   StakePoint
}

func (*PointerAddress) Bytes

func (p *PointerAddress) Bytes() []byte

func (*PointerAddress) GetNetwork

func (p *PointerAddress) GetNetwork() Network

func (*PointerAddress) Kind

func (p *PointerAddress) Kind() AddressKind

func (*PointerAddress) MarshalCBOR

func (p *PointerAddress) MarshalCBOR() ([]byte, error)

func (*PointerAddress) Prefix

func (p *PointerAddress) Prefix() string

func (*PointerAddress) String

func (p *PointerAddress) String() string

type Reward

type Reward struct {
	Network Network
	Payment StakeCredential
}

func (*Reward) Bytes

func (r *Reward) Bytes() []byte

func (*Reward) GetNetwork

func (r *Reward) GetNetwork() Network

func (*Reward) Kind

func (r *Reward) Kind() AddressKind

func (*Reward) MarshalCBOR

func (r *Reward) MarshalCBOR() ([]byte, error)

func (*Reward) Prefix

func (r *Reward) Prefix() string

func (*Reward) String

func (r *Reward) String() string

type ScriptsWitness

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

func (*ScriptsWitness) MarshalCBOR

func (s *ScriptsWitness) MarshalCBOR() ([]byte, error)

type StakeCredential

type StakeCredential struct {
	Kind StakeCredentialType `cbor:"0,keyasint"`
	Data []byte              `cbor:"1,keyasint"`
}

func NewKeysStakeCred

func NewKeysStakeCred(d []byte) StakeCredential

type StakeCredentialType

type StakeCredentialType byte
const (
	KeyStakeCredentialType StakeCredentialType = iota
	ScriptStakeCredentialype
)

type StakePoint

type StakePoint struct {
	Slot      uint64
	TxIndex   uint64
	CertIndex uint64
}

type TransactionBody

type TransactionBody struct {
	Inputs        []*Input  `cbor:"0,keyasint"`
	Outputs       []*Output `cbor:"1,keyasint"`
	Fee           uint64    `cbor:"2,keyasint"`
	InvalidAfter  uint32    `cbor:"3,keyasint,omitempty"`
	InvalidBefore uint32    `cbor:"8,keyasint,omitempty"`
}

func (*TransactionBody) Bytes

func (b *TransactionBody) Bytes() []byte

type Tx

type Tx struct {
	Body     *TransactionBody
	Witness  *Witness
	Metadata interface{} // TODO: supports it
	// contains filtered or unexported fields
}

func NewTx

func NewTx() *Tx

NewTx creates new Tx

Example
_, rawxprv, err := bech32.Decode("xprv1wq9th6qy7ej8w03cgas2lzwvzncvwaje8ek8lps6u6mwyhtpaewap0w2f9hhrpg6tvrffcvz5ecevvhfl6vnznswss96kqyncm2drut3kfps0pv0r4q3fuy6urzs52ywdpzx55muncgf283fr378hwzwp5p27ewe")
if err != nil {
	panic(err)
}

rootprv, err := bip32.NewXPrv(rawxprv)
if err != nil {
	panic(err)
}
rootpub, rootxpub := rootprv.PublicKey(), rootprv.XPub().Bytes()
shellAddress := NewKeyedEnterpriseAddress(rootpub, Testnet)
byronAddress, err := NewSimpleLegacyAddress(rootxpub)
if err != nil {
	panic(err)
}

// ADA only transaction
{
	tx := NewTx()
	tx.AddInputs(MustInput("2558aad25ec6b0e74009f36dc60d7fec6602ce43d603e80c9edde9dd54c78eb4", 0))
	tx.AddOutputs(NewOutput(shellAddress, 9000000)).SetFee(1000000).SetInvalidAfter(832163)
	tx.AddKeyWitness(NewKeysWitness(rootpub, rootprv.Sign(tx.Hash())))

	fmt.Println(hex.EncodeToString(tx.Bytes()))
}

// With Token transaction
{
	tx := NewTx()
	tx.AddInputs(MustInput("9640acb862c4060ab61d0c77a18348bd22c76fd855dae12d09255b5cbebe44f3", 0))

	nativeAsset := MustParseAssetId("2ad8fbdc6f18d97ce61b113301f05ff6b2a241c4e0bf5d06127d30a24f4343")
	output0 := NewOutput(shellAddress, 0).AddAsset(nativeAsset, 10000).
		SetMinAda() // you can use 0 ADA at first and set min ada at last

	tx.AddOutputs(output0, NewOutput(shellAddress, 2e6)).
		SetFee(2000000).
		SetInvalidAfter(832163).
		SetInvalidBefore(822163)

	witness, err := NewBootstrapWitness(byronAddress, rootxpub, rootprv.Sign(tx.Hash()))
	if err != nil {
		panic(err)
	}
	tx.AddBootstrapWitness(witness)
	fmt.Println(tx.ID())
}
Output:

83a400818258202558aad25ec6b0e74009f36dc60d7fec6602ce43d603e80c9edde9dd54c78eb400018182581d60c5fb57853a4a5bbb13bcd494ee0facd4e1fca33720a640664df151341a00895440021a000f4240031a000cb2a3a10081825820c6e47dceb9235ad31fec01ef3e34b02839d22fb583cd28fac7ec81ebbe653a395840e94817956d902702ed6172db8edca0193b33aba511a72e1998cc18aa492be8cffaca96c8305ffbb2836c3c04c886e23302e222b99185b87b701a40a10c50660ff6
08c1e412dc65a8220f650a396ac5ca4758d8df7ff65defb790af2437b2d84df9

func (*Tx) AddBootstrapWitness

func (t *Tx) AddBootstrapWitness(k *BootstrapWitness) *Tx

AddBootstrapWitness adds witness for legacy(Byron) address

func (*Tx) AddInputs

func (t *Tx) AddInputs(i ...*Input) *Tx

AddInputs add inputs to tx

func (*Tx) AddKeyWitness

func (t *Tx) AddKeyWitness(k *KeysWitness) *Tx

AddKeyWitness adds witness for Shelly address

func (*Tx) AddOutputs

func (t *Tx) AddOutputs(o ...*Output) *Tx

AddInputs add outputs to tx

func (*Tx) Bytes

func (t *Tx) Bytes() []byte

Bytes returns raw transaction bytes, it can submmit by cardano-graphql

func (*Tx) Hash

func (t *Tx) Hash() []byte

Hash returns message to sign

func (*Tx) ID

func (t *Tx) ID() string

ID returns tx hash(AKA txid)

func (*Tx) SetFee

func (t *Tx) SetFee(fee uint64) *Tx

SetFee sets fee for tx

func (*Tx) SetInvalidAfter

func (t *Tx) SetInvalidAfter(v uint32) *Tx

SetInvalidAfter sets InvalidAfter for tx

func (*Tx) SetInvalidBefore

func (t *Tx) SetInvalidBefore(v uint32) *Tx

SetInvalidBefore sets InvalidBefore for tx

type Witness

type Witness struct {
	Keys      []*KeysWitness      `cbor:"0,keyasint,omitempty"`
	Scripts   []*ScriptsWitness   `cbor:"1,keyasint,omitempty"`
	Bootstrap []*BootstrapWitness `cbor:"2,keyasint,omitempty"`
}

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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