Files
ark/pkg/client-sdk/wasm/ark_sdk_wasm.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

100 lines
2.7 KiB
Go

package arksdkwasm
import (
"context"
"fmt"
"strings"
"syscall/js"
arksdk "github.com/ark-network/ark-sdk"
"github.com/ark-network/ark-sdk/store"
"github.com/ark-network/ark-sdk/wallet"
liquidwallet "github.com/ark-network/ark-sdk/wallet/singlekey/liquid"
walletstore "github.com/ark-network/ark-sdk/wallet/singlekey/store"
)
var (
arkSdkClient arksdk.ArkClient
configStore store.ConfigStore
)
func init() {
js.Global().Set("init", InitWrapper())
js.Global().Set("unlock", UnlockWrapper())
js.Global().Set("lock", LockWrapper())
js.Global().Set("balance", BalanceWrapper())
js.Global().Set("onboard", OnboardWrapper())
js.Global().Set("receive", ReceiveWrapper())
js.Global().Set("sendOnChain", SendOnChainWrapper())
js.Global().Set("sendOffChain", SendOffChainWrapper())
js.Global().Set("unilateralRedeem", UnilateralRedeemWrapper())
js.Global().Set("collaborativeRedeem", CollaborativeRedeemWrapper())
js.Global().Set("log", LogWrapper())
js.Global().Set("getAspUrl", GetAspUrlWrapper())
js.Global().Set("getAspPubKeyHex", GetAspPubkeyWrapper())
js.Global().Set("getWalletType", GetWalletTypeWrapper())
js.Global().Set("getClientType", GetClientTypeWrapper())
js.Global().Set("getNetwork", GetNetworkWrapper())
js.Global().Set("getRoundLifetime", GetRoundLifetimeWrapper())
js.Global().Set("getUnilateralExitDelay", GetUnilateralExitDelayWrapper())
js.Global().Set("getMinRelayFee", GetMinRelayFeeWrapper())
}
func New(
ctx context.Context, storeSvc store.ConfigStore,
) error {
var err error
data, err := storeSvc.GetData(ctx)
if err != nil {
return err
}
if data == nil {
arkSdkClient, err = arksdk.New(storeSvc)
} else {
var walletSvc wallet.WalletService
switch data.WalletType {
case arksdk.SingleKeyWallet:
walletSvc, err = getSingleKeyWallet(storeSvc, data.Network.Name)
if err != nil {
return err
}
// TODO: Support HD wallet
default:
return fmt.Errorf("unknown wallet type")
}
arkSdkClient, err = arksdk.LoadWithWallet(storeSvc, walletSvc)
}
if err != nil {
js.Global().Get("console").Call("error", err.Error())
return err
}
configStore = storeSvc
select {}
}
func getWalletStore(storeType string) (walletstore.WalletStore, error) {
if storeType == LocalStorageStore {
return NewLocalStorageWalletStore()
}
// TODO: Support IndexDB store
return nil, fmt.Errorf("unknown wallet store type")
}
func getSingleKeyWallet(
configStore store.ConfigStore, network string,
) (wallet.WalletService, error) {
walletStore, err := getWalletStore(configStore.GetType())
if err != nil {
return nil, err
}
if strings.Contains(network, "liquid") {
return liquidwallet.NewWalletService(configStore, walletStore)
}
// TODO: Support bitcoin wallet
return nil, fmt.Errorf("network %s not supported yet", network)
}