Persist extension wallet descriptors
This commit is contained in:
1 parent
276a945f83
commit
b623b27939
1 file changed
+7
-8
+7
-8
@@ -1,16 +1,16 @@
|
||||
import { minerPreference } from './miner.js';
|
||||
import { LocalChain } from './adapters/chain.js';
|
||||
import { TestWallet, NETWORK } from './adapters/wallet.js';
|
||||
import { loadWalletBackup, NETWORK } from './adapters/wallet.js';
|
||||
export const STORAGE_KEY = 'super-testnet:lab:v1';
|
||||
export function decodeLab(raw) {
|
||||
if (!raw) return { wallet: null, chain: new LocalChain(), miner: minerPreference() };
|
||||
if (!raw) return { wallet:null, chain:new LocalChain(), miner:minerPreference() };
|
||||
const data = JSON.parse(raw);
|
||||
if (data.kind !== 'test-lab' || data.network !== NETWORK || data.version !== 1) throw new Error('Unsupported lab backup.');
|
||||
return { wallet: data.wallet ? TestWallet.load(data.wallet) : null, chain: new LocalChain(data.chain), miner: minerPreference(data.miner) };
|
||||
return { wallet:data.wallet ? loadWalletBackup(data.wallet) : null, chain:new LocalChain(data.chain), miner:minerPreference(data.miner) };
|
||||
}
|
||||
export const encodeLab = lab => JSON.stringify({
|
||||
kind: 'test-lab', network: NETWORK, version: 1,
|
||||
wallet: lab.wallet?.backup() ?? null, chain: lab.chain.snapshot(), miner: minerPreference(lab.miner),
|
||||
kind:'test-lab', network:NETWORK, version:1,
|
||||
wallet:lab.wallet?.backup() ?? null, chain:lab.chain.snapshot(), miner:minerPreference(lab.miner),
|
||||
});
|
||||
export const readLab = () => decodeLab(localStorage.getItem(STORAGE_KEY));
|
||||
export function saveLab(lab) {
|
||||
@@ -18,13 +18,12 @@ export function saveLab(lab) {
|
||||
catch { throw new Error('Browser storage is full or unavailable. No changes were saved. Export a lab backup before clearing storage.'); }
|
||||
}
|
||||
|
||||
// Shared by manual mining, auto mining, and all other lab mutations.
|
||||
export async function mutateLab(action, { locks = navigator.locks, read = readLab, save = saveLab } = {}) {
|
||||
export async function mutateLab(action, { locks=navigator.locks, read=readLab, save=saveLab } = {}) {
|
||||
if (!locks) throw new Error('This app needs a secure-context browser with Web Locks. Use localhost or HTTPS.');
|
||||
return locks.request(STORAGE_KEY, async () => {
|
||||
const candidate = read();
|
||||
const changed = await action(candidate);
|
||||
if (changed !== false) save(candidate);
|
||||
return { lab: candidate, changed: changed !== false };
|
||||
return { lab:candidate, changed:changed !== false };
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user