cardano

package module
v0.0.0-...-ef514fe Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: GPL-3.0 Imports: 11 Imported by: 0

README

cardano-go

PkgGoDev ci

cardano-go is a library for creating go applicactions that interact with the Cardano Blockchain. [WIP]

Check out our Catalyst proposal: (https://cardano.ideascale.com/c/idea/416383)

Installation

$ go get github.com/echovl/cardano-go

Usage

Get protocol parameters
package main

import (
	"fmt"

	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/blockfrost"
)

func main() {
	node := blockfrost.NewNode(cardano.Mainnet, "project-id")

	pparams, err := node.ProtocolParams()
	if err != nil {
		panic(err)
	}

	fmt.Println(pparams)
}
Create simple transaction
package main

import (
	"fmt"

	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/crypto"
)

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	sender, err := cardano.NewAddress("addr")
	if err != nil {
		panic(err)
	}
	receiver, err := cardano.NewAddress("addr")
	if err != nil {
		panic(err)
	}
	sk, err := crypto.NewPrvKey("addr_sk")
	if err != nil {
		panic(err)
	}
	txHash, err := cardano.NewHash32("txhash")
	if err != nil {
		panic(err)
	}

	txInput := cardano.NewTxInput(txHash, 0, cardano.NewValue(20e6))
	txOut := cardano.NewTxOutput(receiver, cardano.NewValue(10e6))

	txBuilder.AddInputs(txInput)
	txBuilder.AddOutputs(txOut)
	txBuilder.SetTTL(100000)
	txBuilder.AddChangeIfNeeded(sender)
	txBuilder.Sign(sk)

	tx, err := txBuilder.Build()
	if err != nil {
		panic(err)
	}

	fmt.Println(tx.Hex())
}
Set fee and change output
package main

import (
	"github.com/echovl/cardano-go"
)

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	changeAddr, err := cardano.NewAddress("addr")
	if err != nil {
		panic(err)
	}

	txBuilder.AddChangeIfNeeded(changeAddr)
}
Submit transaction
package main

import (
	"fmt"

	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/blockfrost"
)

func main() {
	node := blockfrost.NewNode(cardano.Mainnet, "project-id")

	txHash, err := node.SubmitTx(&cardano.Tx{})
	if err != nil {
		panic(err)
	}

	fmt.Println(txHash)
}
Add transaction metadata
package main

import "github.com/echovl/cardano-go"

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	txBuilder.AddAuxiliaryData(&cardano.AuxiliaryData{
		Metadata: cardano.Metadata{
			0: map[string]interface{}{
				"hello": "cardano-go",
			},
		},
	})
}
Stake key registration
package main

import (
	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/crypto"
)

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	stakeKey, err := crypto.NewPrvKey("stake_sk")
	if err != nil {
		panic(err)
	}

	stakeRegCert, err := cardano.NewStakeRegistrationCertificate(stakeKey.PubKey())
	if err != nil {
		panic(err)
	}

	txBuilder.AddCertificate(stakeRegCert)
}
Stake delegation
package main

import (
	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/crypto"
)

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	stakeKey, err := crypto.NewPrvKey("stake_sk")
	if err != nil {
		panic(err)
	}

	poolKeyHash, err := cardano.NewHash28("2d6765748cc86efe862f5abeb0c0271f91d368d300123ecedc078ef2")
    if err != nil {
        panic(err)
    }
	stakeDelCert, err := cardano.NewStakeDelegationCertificate(stakeKey.PubKey(), poolKeyHash)
	if err != nil {
		panic(err)
	}

	txBuilder.AddCertificate(stakeDelCert)
}
Mint native tokens
package main

import (
	"math/big"

	"github.com/echovl/cardano-go"
	"github.com/echovl/cardano-go/crypto"
)

func main() {
	txBuilder := cardano.NewTxBuilder(&cardano.ProtocolParams{})

	policyKey, err := crypto.NewPrvKey("policy_sk")
	if err != nil {
		panic(err)
	}
	policyScript, err := cardano.NewScriptPubKey(policyKey.PubKey())
	if err != nil {
		panic(err)
	}
	policyID, err := cardano.NewPolicyID(policyScript)
	if err != nil {
		panic(err)
	}

	newAsset := cardano.NewMint().
		Set(
			policyID,
			cardano.NewMintAssets().
				Set(cardano.NewAssetName("cardanogo"), big.NewInt(1e9)),
		)

	addr, err := cardano.NewAddress("addr")
	if err != nil {
		panic(err)
	}

	txHashIn, err := cardano.NewHash32("txhash")
	if err != nil {
		panic(err)
	}

	txBuilder.AddInputs(
		cardano.NewTxInput(txHashIn, 0, cardano.NewValue(10e6)),
	)
	txBuilder.AddOutputs(
		cardano.NewTxOutput(addr, cardano.NewValueWithAssets(10e6, newAsset.MultiAsset())),
	)

	txBuilder.AddNativeScript(policyScript)
	txBuilder.Mint(newAsset)
}

cwallet

This package provides a cli cwallet for wallet management. It supports:

  • hierarchical deterministic wallets
  • fetching balances
  • transfering ADA
Configuration

cwallet uses blockfrost as a Node provider. It requires a config file in $XDG_CONFIG_HOME/cwallet.yml. Example:

blockfrost_project_id: "project-id"
Installation
$ git clone github.com/echovl/cardano-go
$ make && sudo make install
Usage

Wallet creation:

$ cwallet new-wallet jhon
mnemonic: various find knee churn bicycle current midnight visit artist help soon flower venture wasp problem

List wallet address:

$ cwallet list-address jhon
PATH                      ADDRESS
m/1852'/1815'/0'/0/0      addr1vxzfs9dj365gcdmv6dwj7auewf624ghwrtduecu37hrxsyst8gvu2

Send ADA:

$ cwallet transfer echo addr1vxzfs9dj365gcdmv6dwj7auewf624ghwrtduecu37hrxsyst8gvu2 2000000
fd3a7d6e9742fd9ddba2bd1740fa994f5c93a4f59bf88dc5f81d8d7413c5b3a9

Documentation

Overview

Package cardano-go is a library for the Cardano Blockchain.

Getting started

The best way to get started working with cardano-go is to use `go get` to add the library.

 go get github.com/echovl/cardano-go
	go get github.com/echovl/cardano-go/node
	go get github.com/echovl/cardano-go/tx

Hello cardano-go

This example shows how you can use cardano-go to query UTXOs.

package main

import (
    "fmt"

    "github.com/echovl/cardano-go/node/blockfrost"
    "github.com/echovl/cardano-go/types"
)

func main() {
    addr, err := types.NewAddress("addr1v9c7flddz5nqj448t78h5mdgau8p9ee24m7n62g2s48akkcjzfhw3")
    if err != nil {
        panic(err)
    }

    node := blockfrost.NewNode(types.Mainnet, "project-id")

    utxos, err := node.UTXOs(addr)
    if err != nil {
        panic(err)
    }

    fmt.Println(utxos)
}

Index

Constants

View Source
const (
	SingleHostAddr RelayType = 0
	SingleHostName           = 1
	MultiHostName            = 2
)
View Source
const (
	ProtocolMagic = 1097911063
)

Variables

This section is empty.

Functions

func Blake224Hash

func Blake224Hash(b []byte) ([]byte, error)

Types

type AddrKeyHash

type AddrKeyHash = Hash28

type Address

type Address struct {
	Network Network
	Type    AddressType
	Pointer Pointer

	Payment StakeCredential
	Stake   StakeCredential
}

Address represents a Cardano address.

func NewAddress

func NewAddress(bech string) (Address, error)

NewAddress creates an Address from a bech32 encoded string.

func NewAddressFromBytes

func NewAddressFromBytes(bytes []byte) (Address, error)

NewAddressFromBytes creates an Address from bytes.

func NewBaseAddress

func NewBaseAddress(network Network, payment StakeCredential, stake StakeCredential) (Address, error)

NewBaseAddress returns a new Base Address.

func NewEnterpriseAddress

func NewEnterpriseAddress(network Network, payment StakeCredential) (Address, error)

NewEnterpriseAddress returns a new Enterprise Address.

func NewPointerAddress

func NewPointerAddress(network Network, payment StakeCredential, ptr Pointer) (Address, error)

NewPointerAddress returns a new Pointer Address.

func (*Address) Bech32

func (addr *Address) Bech32() string

Bech32 returns the Address encoded as bech32.

func (*Address) Bytes

func (addr *Address) Bytes() []byte

Bytes returns the CBOR encoding of the Address as bytes.

func (*Address) MarshalCBOR

func (addr *Address) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

func (Address) String

func (addr Address) String() string

String returns the Address encoded as bech32.

func (*Address) UnmarshalCBOR

func (addr *Address) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type AddressType

type AddressType byte
const (
	Base       AddressType = 0x00
	Ptr        AddressType = 0x04
	Enterprise AddressType = 0x06
)

type AssetName

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

AssetName represents an Asset name.

func NewAssetName

func NewAssetName(name string) AssetName

NewAssetName returns a new AssetName.

func (*AssetName) Bytes

func (an *AssetName) Bytes() []byte

Bytes returns the underlying name bytes.

func (AssetName) String

func (an AssetName) String() string

String implements Stringer.

type Assets

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

Assets repressents a set of Cardano Native Tokens.

func NewAssets

func NewAssets() *Assets

NewAssets returns a new empty Assets.

func (*Assets) Get

func (a *Assets) Get(name AssetName) BigNum

Get returns the value of a given Asset in Assets.

func (*Assets) Keys

func (a *Assets) Keys() []AssetName

Keys returns all the AssetNames stored in Assets.

func (*Assets) MarshalCBOR

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

MarshalCBOR implements cbor.Marshaler

func (*Assets) Set

func (a *Assets) Set(name AssetName, val BigNum) *Assets

Set sets the value of a given Asset in Assets.

func (*Assets) UnmarshalCBOR

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

UnmarshalCBOR implements cbor.Unmarshaler.

type AuxiliaryData

type AuxiliaryData struct {
	Metadata      Metadata    `cbor:"0,keyasint,omitempty"`
	NativeScripts interface{} `cbor:"1,keyasint,omitempty"`
	PlutusScripts interface{} `cbor:"2,keyasint,omitempty"`
}

AuxiliaryData is the auxiliary data in the transaction.

func (*AuxiliaryData) MarshalCBOR

func (d *AuxiliaryData) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler

func (*AuxiliaryData) UnmarshalCBOR

func (d *AuxiliaryData) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler

type BigNum

type BigNum uint64

type Certificate

type Certificate struct {
	Type CertificateType

	// Common fields
	StakeCredential StakeCredential
	PoolKeyHash     PoolKeyHash
	VrfKeyHash      Hash32

	// Pool related fields
	Operator      PoolKeyHash
	Pledge        Coin
	Margin        UnitInterval
	RewardAccount Address
	Owners        []AddrKeyHash
	Relays        []Relay
	PoolMetadata  *PoolMetadata // or null
	Epoch         uint64

	// Genesis fields
	GenesisHash         Hash28
	GenesisDelegateHash Hash28
}

Certificate is a Cardano certificate.

func NewStakeDelegationCertificate

func NewStakeDelegationCertificate(stakeKey crypto.PubKey, poolKeyHash Hash28) (Certificate, error)

NewStakeDelegationCertificate creates a Stake Delegation Certificate.

func NewStakeDeregistrationCertificate

func NewStakeDeregistrationCertificate(stakeKey crypto.PubKey) (Certificate, error)

NewStakeDeregistrationCertificate creates a Stake Deregistration Certificate.

func NewStakeRegistrationCertificate

func NewStakeRegistrationCertificate(stakeKey crypto.PubKey) (Certificate, error)

NewStakeRegistrationCertificate creates a Stake Registration Certificate.

func (*Certificate) MarshalCBOR

func (c *Certificate) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

func (*Certificate) UnmarshalCBOR

func (c *Certificate) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type CertificateType

type CertificateType uint
const (
	StakeRegistration CertificateType = iota
	StakeDeregistration
	StakeDelegation
	PoolRegistration
	PoolRetirement
	GenesisKeyDelegation
	MoveInstantaneousRewards
)

type Coin

type Coin BigNum

Coin represents the Cardano Native Token, in Lovelace.

type Hash28

type Hash28 []byte

func NewHash28

func NewHash28(h string) (Hash28, error)

NewHash28 returns a new Hash28 from a hex encoded string.

func (Hash28) String

func (h Hash28) String() string

String returns the hex encoding representation of a Hash28.

type Hash32

type Hash32 []byte

func NewHash32

func NewHash32(h string) (Hash32, error)

NewHash32 returns a new Hash32 from a hex encoded string.

func (Hash32) String

func (h Hash32) String() string

String returns the hex encoding representation of a Hash32

type Metadata

type Metadata map[uint]interface{}

Metadata represents the transaction metadata.

type Mint

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

Mint is a bundle of MintAssets indexed by Policy.

func NewMint

func NewMint() *Mint

NewMint returns a new empty Mint.

func (*Mint) Get

func (m *Mint) Get(policyID PolicyID) *MintAssets

Get returns the MintAssets of a given Policy in Mint.

func (*Mint) Keys

func (m *Mint) Keys() []PolicyID

Keys returns all the Policies stored in Mint.

func (*Mint) MarshalCBOR

func (ma *Mint) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler

func (*Mint) MultiAsset

func (m *Mint) MultiAsset() *MultiAsset

MultiAsset returns a new MultiAsset created from Mint.

func (*Mint) Set

func (m *Mint) Set(policyID PolicyID, assets *MintAssets) *Mint

Set sets the MintAssets of a given Policy in Mint.

func (*Mint) UnmarshalCBOR

func (ma *Mint) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type MintAssets

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

MintAssets represents a set of Cardano Native Tokens to be minted.

func NewMintAssets

func NewMintAssets() *MintAssets

NewMintAssets returns a new empty MintAssets.

func (*MintAssets) Get

func (a *MintAssets) Get(name AssetName) *big.Int

Get returns the value of a given Asset in MintAssets.

func (*MintAssets) Keys

func (a *MintAssets) Keys() []AssetName

Keys returns all the AssetNames stored in MintAssets.

func (*MintAssets) MarshalCBOR

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

MarshalCBOR implements cbor.Marshaler

func (*MintAssets) Set

func (a *MintAssets) Set(name AssetName, val *big.Int) *MintAssets

Set sets the value of a given Asset in MintAssets.

func (*MintAssets) UnmarshalCBOR

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

UnmarshalCBOR implements cbor.Unmarshaler.

type MultiAsset

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

MultiAsset is a bundle of Assets indexed by Policy.

func NewMultiAsset

func NewMultiAsset() *MultiAsset

NewMultiAsset returns a new empty MultiAsset.

func (*MultiAsset) Get

func (ma *MultiAsset) Get(policyID PolicyID) *Assets

Get returns the Assets of a given Policy in MultiAsset.

func (*MultiAsset) Keys

func (ma *MultiAsset) Keys() []PolicyID

Keys returns all the Policies stored in MultiAsset.

func (*MultiAsset) MarshalCBOR

func (ma *MultiAsset) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler

func (*MultiAsset) Set

func (ma *MultiAsset) Set(policyID PolicyID, assets *Assets) *MultiAsset

Set sets the Assets of a given Policy in MultiAsset.

func (MultiAsset) String

func (ma MultiAsset) String() string

String implements Stringer.

func (*MultiAsset) UnmarshalCBOR

func (ma *MultiAsset) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type NativeScript

type NativeScript struct {
	Type          NativeScriptType
	KeyHash       AddrKeyHash
	N             uint64
	Scripts       []NativeScript
	IntervalValue uint64
}

NativeScript is a Cardano Native Script.

func NewScriptPubKey

func NewScriptPubKey(publicKey crypto.PubKey) (NativeScript, error)

NewScriptPubKey returns a new Script PubKey.

func (*NativeScript) Bytes

func (ns *NativeScript) Bytes() ([]byte, error)

Bytes returns the CBOR encoding of the script as bytes.

func (*NativeScript) Hash

func (ns *NativeScript) Hash() (Hash28, error)

Hash returns the script hash using blake2b224.

func (*NativeScript) MarshalCBOR

func (ns *NativeScript) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

func (*NativeScript) UnmarshalCBOR

func (ns *NativeScript) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type NativeScriptType

type NativeScriptType uint64
const (
	ScriptPubKey NativeScriptType = iota
	ScriptAll
	ScriptAny
	ScriptNofK
	ScriptInvalidBefore
	ScriptInvalidAfter
)

type Network

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

func (Network) String

func (n Network) String() string

String implements Stringer.

type Node

type Node interface {
	// UTxOs returns a list of unspent transaction outputs for a given address
	UTxOs(Address) ([]UTxO, error)

	// Tip returns the node's current tip
	Tip() (*NodeTip, error)

	// SubmitTx submits a transaction to the node using cbor encoding
	SubmitTx(*Tx) (*Hash32, error)

	// ProtocolParams returns the Node's Protocol Parameters
	ProtocolParams() (*ProtocolParams, error)

	// Network returns the node's current network type
	Network() Network
}

Node is the interface required for a Cardano backend/node. A backend/node is used to interact with the Cardano Blockchain, sending transactions and fetching state.

type NodeTip

type NodeTip struct {
	Block uint64
	Epoch uint64
	Slot  uint64
}

type Pointer

type Pointer struct {
	Slot      uint64
	TxIndex   uint64
	CertIndex uint64
}

Pointer is the location of the Stake Registration Certificate in the blockchain.

type PolicyID

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

PolicyID is the native token policy id.

func NewPolicyID

func NewPolicyID(script NativeScript) (PolicyID, error)

NewPolicyID returns a new PolicyID using a native script.

func NewPolicyIDFromHash

func NewPolicyIDFromHash(scriptHash Hash28) PolicyID

NewPolicyIDFromHash returns a new PolicyID using a script hash.

func (*PolicyID) Bytes

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

Bytes returns underlying script hash.

func (*PolicyID) String

func (p *PolicyID) String() string

String implements Stringer.

type PoolKeyHash

type PoolKeyHash = Hash28

type PoolMetadata

type PoolMetadata struct {
	URL  string
	Hash Hash32
	// contains filtered or unexported fields
}

PoolMetadata represents the metadata used for a pool registration.

type ProtocolParams

type ProtocolParams struct {
	MinFeeA              Coin
	MinFeeB              Coin
	MaxBlockBodySize     uint
	MaxTxSize            uint
	MaxBlockHeaderSize   uint
	KeyDeposit           Coin
	PoolDeposit          Coin
	MaxEpoch             uint
	NOpt                 uint
	PoolPledgeInfluence  Rational
	ExpansionRate        UnitInterval
	TreasuryGrowthRate   UnitInterval
	D                    UnitInterval
	ExtraEntropy         []byte
	ProtocolVersion      ProtocolVersion
	MinPoolCost          Coin
	CoinsPerUTXOWord     Coin
	CostModels           interface{}
	ExecutionCosts       interface{}
	MaxTxExUnits         interface{}
	MaxBlockTxExUnits    interface{}
	MaxValueSize         uint
	CollateralPercentage uint
	MaxCollateralInputs  uint
}

ProtocolParams is a Cardano Protocol Parameters.

type ProtocolVersion

type ProtocolVersion struct {
	Major uint
	Minor uint
	// contains filtered or unexported fields
}

ProtocolVersion is the protocol version number.

type Rational

type Rational struct {
	P uint64
	Q uint64
	// contains filtered or unexported fields
}

func (*Rational) MarshalCBOR

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

MarshalCBOR implements cbor.Marshaler

func (*Rational) UnmarshalCBOR

func (r *Rational) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler

type Relay

type Relay struct {
	Type    RelayType
	Port    Uint64
	Ipv4    []byte
	Ipv6    []byte
	DNSName string
}

func (*Relay) MarshalCBOR

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

MarshalCBOR implements cbor.Marshaler.

func (*Relay) UnmarshalCBOR

func (r *Relay) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type RelayType

type RelayType uint64

type ScriptHashNamespace

type ScriptHashNamespace uint8
const (
	NativeScriptNamespace ScriptHashNamespace = iota
	PlutusScriptNamespace
)

type SigIndex

type SigIndex struct {
	Index     int
	Signature []byte
}

type StakeCredential

type StakeCredential struct {
	Type       StakeCredentialType
	KeyHash    AddrKeyHash
	ScriptHash Hash28
}

StakeCredential is a Cardano credential.

func NewKeyCredential

func NewKeyCredential(publicKey crypto.PubKey) (StakeCredential, error)

NewKeyCredential creates a Key Credential.

func NewScriptCredential

func NewScriptCredential(script []byte) (StakeCredential, error)

NewKeyCredential creates a Script Credential.

func (*StakeCredential) Equal

func (s *StakeCredential) Equal(rhs StakeCredential) bool

Equal returns true if the credentials are equal.

func (*StakeCredential) Hash

func (s *StakeCredential) Hash() Hash28

func (*StakeCredential) MarshalCBOR

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

MarshalCBOR implements cbor.Marshaler.

func (*StakeCredential) UnmarshalCBOR

func (s *StakeCredential) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type StakeCredentialType

type StakeCredentialType uint64
const (
	KeyCredential StakeCredentialType = iota
	ScriptCredential
)

type String

type String *string

func NewString

func NewString(s string) String

type Tx

type Tx struct {
	Body          TxBody
	WitnessSet    WitnessSet
	IsValid       bool
	AuxiliaryData *AuxiliaryData // or null
	// contains filtered or unexported fields
}

Tx is a Cardano transaction.

func (*Tx) Bytes

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

Bytes returns the CBOR encoding of the transaction as bytes.

func (*Tx) ConfigureWitnessSet

func (tx *Tx) ConfigureWitnessSet(witnesses []VKeyWitness)

func (*Tx) Hash

func (tx *Tx) Hash() (Hash32, error)

Hash returns the transaction body hash using blake2b.

func (Tx) Hex

func (tx Tx) Hex() string

Hex returns the CBOR encoding of the transaction as hex.

func (*Tx) MarshalCBOR

func (tx *Tx) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

func (*Tx) UnmarshalCBOR

func (tx *Tx) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type TxBody

type TxBody struct {
	Inputs  []*TxInput  `cbor:"0,keyasint"`
	Outputs []*TxOutput `cbor:"1,keyasint"`
	Fee     Coin        `cbor:"2,keyasint"`

	// Optionals
	TTL                   Uint64        `cbor:"3,keyasint,omitempty"`
	Certificates          []Certificate `cbor:"4,keyasint,omitempty"`
	Withdrawals           interface{}   `cbor:"5,keyasint,omitempty"` // unsupported
	Update                interface{}   `cbor:"6,keyasint,omitempty"` // unsupported
	AuxiliaryDataHash     *Hash32       `cbor:"7,keyasint,omitempty"`
	ValidityIntervalStart Uint64        `cbor:"8,keyasint,omitempty"`
	Mint                  *Mint         `cbor:"9,keyasint,omitempty"`
	ScriptDataHash        *Hash32       `cbor:"10,keyasint,omitempty"`
	Collateral            []TxInput     `cbor:"11,keyasint,omitempty"`
	RequiredSigners       []AddrKeyHash `cbor:"12,keyasint,omitempty"`
	NetworkID             Uint64        `cbor:"13,keyasint,omitempty"`
}

func (*TxBody) Hash

func (body *TxBody) Hash() (Hash32, error)

Hash returns the transaction body hash using blake2b256.

type TxBuilder

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

TxBuilder is a transaction builder.

func NewTxBuilder

func NewTxBuilder(protocol *ProtocolParams) *TxBuilder

NewTxBuilder returns a new instance of TxBuilder.

func (*TxBuilder) AddAuxiliaryData

func (tb *TxBuilder) AddAuxiliaryData(data *AuxiliaryData)

AddAuxiliaryData adds auxiliary data to the transaction.

func (*TxBuilder) AddCertificate

func (tb *TxBuilder) AddCertificate(cert Certificate)

AddCertificate adds a certificate to the transaction.

func (*TxBuilder) AddChangeIfNeeded

func (tb *TxBuilder) AddChangeIfNeeded(changeAddr Address)

AddChangeIfNeeded instructs the builder to calculate the required fee for the transaction and to add an aditional output for the change if there is any.

func (*TxBuilder) AddInputs

func (tb *TxBuilder) AddInputs(inputs ...*TxInput)

AddInputs adds inputs to the transaction.

func (*TxBuilder) AddNativeScript

func (tb *TxBuilder) AddNativeScript(script NativeScript)

AddNativeScript adds a native script to the transaction.

func (*TxBuilder) AddOutputs

func (tb *TxBuilder) AddOutputs(outputs ...*TxOutput)

AddOutputs adds outputs to the transaction.

func (*TxBuilder) Build

func (tb *TxBuilder) Build() (*Tx, error)

Build returns a new transaction using the inputs, outputs and keys provided.

func (*TxBuilder) BuildWithoutSigning

func (tb *TxBuilder) BuildWithoutSigning() (*Tx, error)

func (*TxBuilder) MinCoinsForTxOut

func (tb *TxBuilder) MinCoinsForTxOut(txOut *TxOutput) Coin

MinCoinsForTxOut computes the minimal amount of coins required for a given transaction output. More info could be found in <https://github.com/input-output-hk/cardano-ledger/blob/master/doc/explanations/min-utxo-alonzo.rst>

func (*TxBuilder) MinFee

func (tb *TxBuilder) MinFee() (Coin, error)

MinFee computes the minimal fee required for the transaction. This assumes that the inputs-outputs are defined and signing keys are present.

func (*TxBuilder) Mint

func (tb *TxBuilder) Mint(asset *Mint)

Mint adds a new multiasset to mint.

func (*TxBuilder) Reset

func (tb *TxBuilder) Reset()

Reset resets the builder to its initial state.

func (*TxBuilder) SetFee

func (tb *TxBuilder) SetFee(fee Coin)

SetFee sets the transactions's fee.

func (*TxBuilder) SetSignatures

func (tb *TxBuilder) SetSignatures(txHash Hash32, signatures []SigIndex)

func (*TxBuilder) SetTTL

func (tb *TxBuilder) SetTTL(ttl uint64)

SetTtl sets the transaction's time to live.

func (*TxBuilder) Sign

func (tb *TxBuilder) Sign(privateKeys ...crypto.PrvKey)

Sign adds signing keys to create signatures for the witness set.

type TxInput

type TxInput struct {
	TxHash Hash32
	Index  uint64
	Amount *Value `cbor:"-"`
	// contains filtered or unexported fields
}

TxInput is the transaction input.

func NewTxInput

func NewTxInput(txHash Hash32, index uint, amount *Value) *TxInput

NewTxInput creates a new instance of TxInput

func (TxInput) String

func (t TxInput) String() string

String implements stringer.

type TxOutput

type TxOutput struct {
	Address Address
	Amount  *Value
	// contains filtered or unexported fields
}

TxInput is the transaction output.

func NewTxOutput

func NewTxOutput(addr Address, amount *Value) *TxOutput

NewTxOutput creates a new instance of TxOutput

func (TxOutput) String

func (t TxOutput) String() string

type UTxO

type UTxO struct {
	TxHash  Hash32
	Spender Address
	Amount  *Value
	Index   uint64
}

UTxO is a Cardano Unspent Transaction Output.

type Uint64

type Uint64 *uint64

func NewUint64

func NewUint64(u uint64) Uint64

type UnitInterval

type UnitInterval = Rational

type VKeyWitness

type VKeyWitness struct {
	VKey      crypto.PubKey // ed25519 public key
	Signature []byte        // ed25519 signature
	// contains filtered or unexported fields
}

VKeyWitness is a witnesss that uses verification keys.

type Value

type Value struct {
	Coin       Coin
	MultiAsset *MultiAsset
}

Value is a bundle of transferable Cardano Native Tokens.

func NewValue

func NewValue(coin Coin) *Value

NewValue returns a new only-coin Value.

func NewValueWithAssets

func NewValueWithAssets(coin Coin, assets *MultiAsset) *Value

NewValueWithAssets returns a new MultiAsset Value.

func (*Value) Add

func (v *Value) Add(rhs *Value) *Value

Add computes the addition of two Values and returns the result.

func (*Value) Cmp

func (v *Value) Cmp(rhs *Value) int

Compares two Values and returns

-1 if v < rhs
 0 if v == rhs
 1 if v > rhs
 2 if not comparable

func (*Value) IsZero

func (v *Value) IsZero() bool

IsZero returns true if the Value is zero.

func (*Value) MarshalCBOR

func (v *Value) MarshalCBOR() ([]byte, error)

MarshalCBOR implements cbor.Marshaler.

func (*Value) OnlyCoin

func (v *Value) OnlyCoin() bool

OnlyCoin returns true if the Value only holds coins.

func (*Value) Sub

func (v *Value) Sub(rhs *Value) *Value

Sub computes the substracion of two Values and returns the result.

func (*Value) UnmarshalCBOR

func (v *Value) UnmarshalCBOR(data []byte) error

UnmarshalCBOR implements cbor.Unmarshaler.

type Witness

type Witness struct {
	PubKey    crypto.PubKey
	Signature []byte
}

type WitnessSet

type WitnessSet struct {
	VKeyWitnessSet []VKeyWitness  `cbor:"0,keyasint,omitempty"`
	Scripts        []NativeScript `cbor:"1,keyasint,omitempty"`
}

WitnessSet represents the witnesses of the transaction.

Directories

Path Synopsis
cli
cmd
internal
bech32
https://github.com/decred/dcrd/tree/master/bech32
https://github.com/decred/dcrd/tree/master/bech32
cbor
Package cbor is a modern CBOR codec (RFC 8949 & RFC 7049) with CBOR tags, Go struct tags (toarray/keyasint/omitempty), Core Deterministic Encoding, CTAP2, Canonical CBOR, float64->32->16, and duplicate map key detection.
Package cbor is a modern CBOR codec (RFC 8949 & RFC 7049) with CBOR tags, Go struct tags (toarray/keyasint/omitempty), Core Deterministic Encoding, CTAP2, Canonical CBOR, float64->32->16, and duplicate map key detection.

Jump to

Keyboard shortcuts

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