mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
* 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
76 lines
2.0 KiB
Go
76 lines
2.0 KiB
Go
package arksdkwasm
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"syscall/js"
|
|
|
|
walletstore "github.com/ark-network/ark-sdk/wallet/singlekey/store"
|
|
"github.com/decred/dcrd/dcrec/secp256k1/v4"
|
|
)
|
|
|
|
type walletData struct {
|
|
EncryptedPrvkey string `json:"encrypted_private_key"`
|
|
PasswordHash string `json:"password_hash"`
|
|
Pubkey string `json:"pubkey"`
|
|
}
|
|
|
|
func (d walletData) decode() *walletstore.WalletData {
|
|
encryptedPrvkey, _ := hex.DecodeString(d.EncryptedPrvkey)
|
|
passwordHash, _ := hex.DecodeString(d.PasswordHash)
|
|
buf, _ := hex.DecodeString(d.Pubkey)
|
|
pubkey, _ := secp256k1.ParsePubKey(buf)
|
|
return &walletstore.WalletData{
|
|
EncryptedPrvkey: encryptedPrvkey,
|
|
PasswordHash: passwordHash,
|
|
Pubkey: pubkey,
|
|
}
|
|
}
|
|
|
|
type walletStore struct {
|
|
store js.Value
|
|
}
|
|
|
|
func NewLocalStorageWalletStore() (walletstore.WalletStore, error) {
|
|
store := js.Global().Get("localStorage")
|
|
return &walletStore{store}, nil
|
|
}
|
|
|
|
func (s *walletStore) AddWallet(data walletstore.WalletData) error {
|
|
wd := &walletData{
|
|
EncryptedPrvkey: hex.EncodeToString(data.EncryptedPrvkey),
|
|
PasswordHash: hex.EncodeToString(data.PasswordHash),
|
|
Pubkey: hex.EncodeToString(data.Pubkey.SerializeCompressed()),
|
|
}
|
|
|
|
if err := s.writeData(wd); err != nil {
|
|
return fmt.Errorf("failed to write to file store: %s", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *walletStore) GetWallet() (*walletstore.WalletData, error) {
|
|
data := walletData{
|
|
EncryptedPrvkey: s.store.Call("getItem", "encrypted_private_key").String(),
|
|
PasswordHash: s.store.Call("getItem", "password_hash").String(),
|
|
Pubkey: s.store.Call("getItem", "pubkey").String(),
|
|
}
|
|
return data.decode(), nil
|
|
}
|
|
|
|
func (s *walletStore) writeData(data *walletData) error {
|
|
dataMap := make(map[string]string)
|
|
buf, err := json.Marshal(data)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err := json.Unmarshal(buf, &dataMap); err != nil {
|
|
return err
|
|
}
|
|
for key, value := range dataMap {
|
|
s.store.Call("setItem", key, value)
|
|
}
|
|
return nil
|
|
}
|