Skip to content

Commit

Permalink
wip: try to expose an in-memory SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
tiero committed Aug 24, 2024
1 parent 1b9660e commit 8e298b9
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 33 deletions.
2 changes: 1 addition & 1 deletion pkg/client-sdk/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ GO_VERSION := $(shell go version | cut -d' ' -f3)
build-wasm:
@mkdir -p $(BUILD_DIR)
@echo "Version: $(VERSION)"
@GOOS=js GOARCH=wasm GO111MODULE=on go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(BUILD_DIR)/ark-sdk.wasm $(WASM_DIR)/main.go
@GOOS=js GOARCH=wasm GO111MODULE=on go build -ldflags="-X 'main.Version=$(VERSION)'" -o $(BUILD_DIR)/ark-sdk.wasm $(WASM_DIR)/*.go
48 changes: 48 additions & 0 deletions pkg/client-sdk/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// index.js

const fs = require('fs').promises;
const path = require('path');
const dns = require('dns');

// Set Google's DNS server
dns.setServers(['8.8.8.8']);

// Load the Go WASM runtime
require('./wasm_exec');

async function loadWasm() {
const go = new Go();
const wasmBuffer = await fs.readFile(path.join(__dirname, 'build/ark-sdk.wasm'));
const result = await WebAssembly.instantiate(wasmBuffer, go.importObject);
go.run(result.instance);
}

async function main() {
try {
await loadWasm();
console.log("ARK SDK WASM module loaded successfully");

// Set the network
const network = await global.getNetwork();
console.log("Network:", network);
// Call the Init function
try {
const result = await global.init(
"singlekey", // Wallet type
"grpc", // Client type
"https://asp-signet.arklabs.to", // ASP URL (replace with actual URL)
"abandon abandon abandon", // Seed (replace with actual seed)
"sercet", // Password (replace with actual password)
"signet" // Chain (either "bitcoin" or "liquid")
);
console.log("Init function called successfully", result);
} catch (error) {
console.error("Error calling Init function:", error);
}

} catch (error) {
console.error("Error loading WASM module:", error);
}
}

main().catch(console.error);
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build js && wasm
// +build js,wasm

package browser
package main

import (
"context"
Expand All @@ -14,6 +14,10 @@ import (
"github.com/ark-network/ark/pkg/client-sdk/wallet"
singlekeywallet "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey"
walletstore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store"
walletfilestore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store/file"
walletmemstore "github.com/ark-network/ark/pkg/client-sdk/wallet/singlekey/store/inmemory"

browser "github.com/ark-network/ark/pkg/client-sdk/wasm/browser"
)

var (
Expand Down Expand Up @@ -115,9 +119,15 @@ func NewCovenantlessClient(
select {}
}

func getWalletStore(storeType string) (walletstore.WalletStore, error) {
if storeType == LocalStorageStore {
return NewLocalStorageWalletStore()
func getWalletStore(storeType string, datadir string) (walletstore.WalletStore, error) {
if storeType == browser.LocalStorageStore {
return browser.NewLocalStorageWalletStore()
}
if storeType == store.FileStore {
return walletfilestore.NewWalletStore(datadir)
}
if storeType == store.InMemoryStore {
return walletmemstore.NewWalletStore()
}
// TODO: Support IndexDB store
return nil, fmt.Errorf("unknown wallet store type")
Expand All @@ -126,7 +136,7 @@ func getWalletStore(storeType string) (walletstore.WalletStore, error) {
func getSingleKeyWallet(
configStore store.ConfigStore, network string,
) (wallet.WalletService, error) {
walletStore, err := getWalletStore(configStore.GetType())
walletStore, err := getWalletStore(configStore.GetType(), configStore.GetDatadir())
if err != nil {
return nil, err
}
Expand Down
33 changes: 11 additions & 22 deletions pkg/client-sdk/wasm/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,25 @@ package main

import (
"context"
inmemorystore "github.com/ark-network/ark/pkg/client-sdk/store/inmemory"
"github.com/ark-network/ark/pkg/client-sdk/wasm/browser"
"syscall/js"

inmemory "github.com/ark-network/ark/pkg/client-sdk/store/inmemory"
)

func main() {
c := make(chan struct{}, 0)
var (
ctx = context.Background()
)
store, err := browser.NewLocalStorageStore()

storeSvc, err := inmemory.NewConfigStore()
if err != nil {
browser.ConsoleError(err)
js.Global().Get("console").Call("error", err.Error())
return
}
if store != nil {
if err := browser.NewCovenantlessClient(ctx, store); err != nil {
browser.ConsoleError(err)
return
}
} else {
storeSvc, err := inmemorystore.NewConfigStore()
if err != nil {
browser.ConsoleError(err)
return
}
if err := browser.NewCovenantlessClient(ctx, storeSvc); err != nil {
browser.ConsoleError(err)
return
}

if err := NewCovenantlessClient(context.Background(), storeSvc); err != nil {
js.Global().Get("console").Call("error", err.Error())
return
}

println("ARK SDK WebAssembly module initialized")
<-c
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//go:build js && wasm
// +build js,wasm

package browser
package main

import (
"context"
Expand Down Expand Up @@ -33,17 +33,17 @@ func LogWrapper() js.Func {
func InitWrapper() js.Func {
return JSPromise(func(args []js.Value) (interface{}, error) {
if len(args) != 6 {
return nil, errors.New("invalid number of args")
return nil, errors.New("init: expected 6 arguments, got " + fmt.Sprint(len(args)))
}
chain := args[5].String()
if chain != "bitcoin" && chain != "liquid" {
return nil, errors.New("invalid chain, select either 'bitcoin' or 'liquid'")
if chain != "bitcoin" && chain != "liquid" && chain != "signet" && chain != "testnet" && chain != "regtest" && chain != "liquidtestnet" && chain != "liquidregtest" {
return nil, errors.New("invalid chain, select either 'bitcoin', 'liquid', 'signet', 'testnet', 'regtest', 'liquidtestnet', or 'liquidregtest'")
}

var walletSvc wallet.WalletService
switch args[0].String() {
case arksdk.SingleKeyWallet:
walletStore, err := getWalletStore(configStore.GetType())
walletStore, err := getWalletStore(configStore.GetType(), configStore.GetDatadir())
if err != nil {
return nil, fmt.Errorf("failed to init wallet store: %s", err)
}
Expand Down

0 comments on commit 8e298b9

Please sign in to comment.