Files
ark/pkg/client-sdk/example/alice_to_bob.go
Pietralberto Mazza 89df461623 Update client sdk (#207)
* Add bitcoin networks

* Refactor client

* Refactor explorer

* Refactor store

* Refactor wallet

* Refactor sdk client

* Refactor wasm & Update examples

* Move common util funcs to internal/utils

* Move to constants for service types

* Add unit tests

* Parallelize tests

* Lint

* Add job to gh action

* go mod tidy

* Fixes

* Fixes

* Fix compose file

* Fixes

* Fixes after review:
* Drop factory pattern
* Drop password from ark client methods
* Make singlekey wallet manage store and wallet store instead of defining WalletStore as extension of Store
* Move constants to arksdk module
* Drop config and expect directory store and wallet as ark client factory args

* Fix

* Add constants for bitcoin/liquid explorer

* Fix test

* Fix wasm

* Rename client.Client to client.ASPClient

* Rename store.Store to store.ConfigStore

* Rename wallet.Wallet to wallet.WalletService

* Renamings

* Lint

* Fixes

* Move everything to internal/utils & move ComputeVtxoTaprootScript to common

* Go mod tidy
2024-07-30 16:08:23 +02:00

235 lines
5.1 KiB
Go

package main
import (
"context"
"fmt"
"io"
"os/exec"
"strings"
"sync"
"time"
arksdk "github.com/ark-network/ark-sdk"
inmemorystore "github.com/ark-network/ark-sdk/store/inmemory"
log "github.com/sirupsen/logrus"
)
var (
aspUrl = "localhost:8080"
clientType = arksdk.GrpcClient
password = "password"
walletType = arksdk.SingleKeyWallet
)
func main() {
ctx := context.Background()
log.Info("alice is setting up her ark wallet...")
aliceArkClient, err := setupArkClient()
if err != nil {
log.Fatal(err)
}
if err := aliceArkClient.Unlock(ctx, password); err != nil {
log.Fatal(err)
}
//nolint:all
defer aliceArkClient.Lock(ctx, password)
log.Info("alice is acquiring onchain funds...")
_, aliceOnchainAddr, err := aliceArkClient.Receive(ctx)
if err != nil {
log.Fatal(err)
}
if _, err := runCommand("nigiri", "faucet", "--liquid", aliceOnchainAddr); err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
onboardAmount := uint64(20000)
log.Infof("alice is onboarding with %d sats offchain...", onboardAmount)
txid, err := aliceArkClient.Onboard(ctx, onboardAmount)
if err != nil {
log.Fatal(err)
}
if err := generateBlock(); err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
log.Infof("alice onboarded with tx: %s", txid)
aliceBalance, err := aliceArkClient.Balance(ctx, false)
if err != nil {
log.Fatal(err)
}
log.Infof("alice onchain balance: %d", aliceBalance.OnchainBalance.SpendableAmount)
log.Infof("alice offchain balance: %d", aliceBalance.OffchainBalance.Total)
fmt.Println("")
log.Info("bob is setting up his ark wallet...")
bobArkClient, err := setupArkClient()
if err != nil {
log.Fatal(err)
}
if err := bobArkClient.Unlock(ctx, password); err != nil {
log.Fatal(err)
}
//nolint:all
defer bobArkClient.Lock(ctx, password)
bobOffchainAddr, _, err := bobArkClient.Receive(ctx)
if err != nil {
log.Fatal(err)
}
bobBalance, err := bobArkClient.Balance(ctx, false)
if err != nil {
log.Fatal(err)
}
log.Infof("bob onchain balance: %d", bobBalance.OnchainBalance.SpendableAmount)
log.Infof("bob offchain balance: %d", bobBalance.OffchainBalance.Total)
amount := uint64(1000)
receivers := []arksdk.Receiver{
{
To: bobOffchainAddr,
Amount: amount,
},
}
fmt.Println("")
log.Infof("alice is sending %d sats to bob offchain...", amount)
txid, err = aliceArkClient.SendOffChain(ctx, false, receivers)
if err != nil {
log.Fatal(err)
}
log.Infof("payment completed in round tx: %s", txid)
if err := generateBlock(); err != nil {
log.Fatal(err)
}
time.Sleep(5 * time.Second)
aliceBalance, err = aliceArkClient.Balance(ctx, false)
if err != nil {
log.Fatal(err)
}
fmt.Println("")
log.Infof("alice onchain balance: %d", aliceBalance.OnchainBalance.SpendableAmount)
log.Infof("alice offchain balance: %d", aliceBalance.OffchainBalance.Total)
bobBalance, err = bobArkClient.Balance(ctx, false)
if err != nil {
log.Fatal(err)
}
log.Infof("bob onchain balance: %d", bobBalance.OnchainBalance.SpendableAmount)
log.Infof("bob offchain balance: %d", bobBalance.OffchainBalance.Total)
}
func setupArkClient() (arksdk.ArkClient, error) {
storeSvc, err := inmemorystore.NewConfigStore()
if err != nil {
return nil, fmt.Errorf("failed to setup store: %s", err)
}
client, err := arksdk.New(storeSvc)
if err != nil {
return nil, fmt.Errorf("failed to setup ark client: %s", err)
}
if err := client.Init(context.Background(), arksdk.InitArgs{
WalletType: walletType,
ClientType: clientType,
AspUrl: aspUrl,
Password: password,
}); err != nil {
return nil, fmt.Errorf("failed to initialize wallet: %s", err)
}
return client, nil
}
func runCommand(name string, arg ...string) (string, error) {
errb := new(strings.Builder)
cmd := newCommand(name, arg...)
stdout, err := cmd.StdoutPipe()
if err != nil {
return "", err
}
stderr, err := cmd.StderrPipe()
if err != nil {
return "", err
}
if err := cmd.Start(); err != nil {
return "", err
}
output := new(strings.Builder)
errorb := new(strings.Builder)
var wg sync.WaitGroup
wg.Add(2)
go func() {
defer wg.Done()
if _, err := io.Copy(output, stdout); err != nil {
fmt.Fprintf(errb, "error reading stdout: %s", err)
}
}()
go func() {
defer wg.Done()
if _, err := io.Copy(errorb, stderr); err != nil {
fmt.Fprintf(errb, "error reading stderr: %s", err)
}
}()
wg.Wait()
if err := cmd.Wait(); err != nil {
if errMsg := errorb.String(); len(errMsg) > 0 {
return "", fmt.Errorf(errMsg)
}
if outMsg := output.String(); len(outMsg) > 0 {
return "", fmt.Errorf(outMsg)
}
return "", err
}
if errMsg := errb.String(); len(errMsg) > 0 {
return "", fmt.Errorf(errMsg)
}
return strings.Trim(output.String(), "\n"), nil
}
func newCommand(name string, arg ...string) *exec.Cmd {
cmd := exec.Command(name, arg...)
return cmd
}
func generateBlock() error {
if _, err := runCommand("nigiri", "rpc", "--liquid", "generatetoaddress", "1", "el1qqwk722tghgkgmh3r2ph4d2apwj0dy9xnzlenzklx8jg3z299fpaw56trre9gpk6wmw0u4qycajqeva3t7lzp7wnacvwxha59r"); err != nil {
return err
}
time.Sleep(6 * time.Second)
return nil
}