In this section we will make a wallet loader that uses a storage service. The storage service will be used to save and load the wallet state.
We will show two different ways to achieve this.
import { WalletService, Environments } from "@peersyst/ckb-wallet-sdk";
import { mainnetConnection, testnetConnection } from "./network-connections";
import { saveWalletState, loadWalletInfo } from "./storage-service";
export function loadWallet(walletId: number): WalletService {
const { mnemonic, walletState, environment } = loadWalletInfo(walletId);
if (environment === Environments.Mainnet) {
const wallet = new WalletService(mainnetConnection, mnemonic, walletState);
return wallet;
}
const wallet = new WalletService(testnetConnection, mnemonic, walletState);
return wallet;
}
export function saveWallet(walletId: number, wallet: WalletService): void {
const walletState = wallet.getWalletState();
saveWalletState(walletId, walletState);
}
import { WalletService, Environments, WalletState, Logger } from "@peersyst/ckb-wallet-sdk";
import { mainnetConnection, testnetConnection } from "./network-connections";
import { saveWalletState, loadWalletInfo } from "./storage-service";
export function loadWallet(walletId: number): WalletService {
const { mnemonic, walletState, environment } = loadWalletInfo(walletId);
const onSync = (walletFState: WalletState) => {
Logger.log("New wallet state");
saveWalletState(walletId, walletFState);
};
const onSyncStarts = () => {
Logger.log("Synchronization started");
};
if (environment === Environments.Mainnet) {
const wallet = new WalletService(mainnetConnection, mnemonic, walletState, onSync, onSyncStarts);
return wallet;
}
const wallet = new WalletService(testnetConnection, mnemonic, walletState, onSync, onSyncStarts);
return wallet;
}