primary

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 26, 2022 License: BSD-3-Clause Imports: 16 Imported by: 0

Documentation

Index

Examples

Constants

View Source
const (
	MainnetAPIURI = "http://archiver.switzerlandnorth.cloudapp.azure.com:9650"
	FujiAPIURI    = "http://api.djtx-test.network"
	LocalAPIURI   = "http://localhost:9650"
)

Variables

This section is empty.

Functions

func AddAllUTXOs

func AddAllUTXOs(
	ctx context.Context,
	utxos UTXOs,
	client UTXOClient,
	codec codec.Manager,
	sourceChainID ids.ID,
	destinationChainID ids.ID,
	addrs []string,
) error

AddAllUTXOs fetches all the UTXOs referenced by [addresses] that were sent from [sourceChainID] to [destinationChainID] from the [client]. It then uses codec to parse the returned UTXOs and it adds them into [utxos]. If [ctx] expires, then the returned error will be immediately reported.

func FormatAddresses

func FormatAddresses(chain, hrp string, addrSet ids.ShortSet) ([]string, error)

FormatAddresses returns the string format of the provided address set for the requested chain and hrp. This is useful to use with the API clients to support address queries.

Types

type ChainUTXOs

type ChainUTXOs interface {
	AddUTXO(ctx context.Context, destinationChainID ids.ID, utxo *djtx.UTXO) error
	RemoveUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) error

	UTXOs(ctx context.Context, sourceChainID ids.ID) ([]*djtx.UTXO, error)
	GetUTXO(ctx context.Context, sourceChainID, utxoID ids.ID) (*djtx.UTXO, error)
}

func NewChainUTXOs

func NewChainUTXOs(chainID ids.ID, utxos UTXOs) ChainUTXOs

type UTXOClient

type UTXOClient interface {
	GetAtomicUTXOs(
		ctx context.Context,
		addrs []string,
		sourceChain string,
		limit uint32,
		startAddress,
		startUTXOID string,
	) ([][]byte, api.Index, error)
}

type UTXOs

type UTXOs interface {
	AddUTXO(ctx context.Context, sourceChainID, destinationChainID ids.ID, utxo *djtx.UTXO) error
	RemoveUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) error

	UTXOs(ctx context.Context, sourceChainID, destinationChainID ids.ID) ([]*djtx.UTXO, error)
	GetUTXO(ctx context.Context, sourceChainID, destinationChainID, utxoID ids.ID) (*djtx.UTXO, error)
}

func NewUTXOs

func NewUTXOs() UTXOs

type Wallet

type Wallet interface {
	P() p.Wallet
	X() x.Wallet
}

Wallet provides chain wallets for the primary network.

Example
ctx := context.Background()
kc := secp256k1fx.NewKeychain(genesis.EWOQKey)

// NewWallet fetches the available UTXOs owned by [kc] on the network that
// [LocalAPIURI] is hosting.
walletSyncStartTime := time.Now()
wallet, err := NewWallet(ctx, LocalAPIURI, kc)
if err != nil {
	fmt.Printf("failed to initialize wallet with: %s\n", err)
	return
}
fmt.Printf("synced wallet in %s\n", time.Since(walletSyncStartTime))

// Get the P-chain and the X-chain wallets
pWallet := wallet.P()
xWallet := wallet.X()

// Pull out useful constants to use when issuing transactions.
xChainID := xWallet.BlockchainID()
djtxAssetID := xWallet.DJTXAssetID()
owner := &secp256k1fx.OutputOwners{
	Threshold: 1,
	Addrs: []ids.ShortID{
		genesis.EWOQKey.PublicKey().Address(),
	},
}

// Send 100 schmeckles to the P-chain.
exportStartTime := time.Now()
exportTxID, err := xWallet.IssueExportTx(
	constants.PlatformChainID,
	[]*djtx.TransferableOutput{
		{
			Asset: djtx.Asset{
				ID: djtxAssetID,
			},
			Out: &secp256k1fx.TransferOutput{
				Amt:          100 * units.Schmeckle,
				OutputOwners: *owner,
			},
		},
	},
)
if err != nil {
	fmt.Printf("failed to issue X->P export transaction with: %s\n", err)
	return
}
fmt.Printf("issued X->P export %s in %s\n", exportTxID, time.Since(exportStartTime))

// Import the 100 schmeckles from the X-chain into the P-chain.
importStartTime := time.Now()
importTxID, err := pWallet.IssueImportTx(xChainID, owner)
if err != nil {
	fmt.Printf("failed to issue X->P import transaction with: %s\n", err)
	return
}
fmt.Printf("issued X->P import %s in %s\n", importTxID, time.Since(importStartTime))
Output:

func NewWallet

func NewWallet(ctx context.Context, uri string, kc *secp256k1fx.Keychain) (Wallet, error)

NewWallet returns a wallet that supports issuing transactions to the chains living in the primary network.

On creation, the wallet attaches to the provided [uri] and fetches all UTXOs that reference any of the keys contained in [kc]. If the UTXOs are modified through an external issuance process, such as another instance of the wallet, the UTXOs may become out of sync.

The wallet manages all UTXOs locally, and performs all tx signing locally.

func NewWalletWithOptions

func NewWalletWithOptions(w Wallet, options ...common.Option) Wallet

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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