Add CBOR metadata indexes, local API and coordinated auto mining
This commit is contained in:
1 parent
0405452ab2
commit
aa39db3879
14 files changed
+599
-54
No files matched your search
+15
-3
@@ -1,18 +1,30 @@
|
||||
import { minerPreference } from './miner.js';
|
||||
import { LocalChain } from './adapters/chain.js';
|
||||
import { TestWallet, 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() };
|
||||
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) };
|
||||
return { wallet: data.wallet ? TestWallet.load(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(),
|
||||
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) {
|
||||
try { localStorage.setItem(STORAGE_KEY, encodeLab(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 } = {}) {
|
||||
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 };
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user