import { storeJson } from '../fileModels/storeJson' import { actionGroups } from '../actionGroups' import { i18n } from '../i18n' import { sdk } from '../sdk' import { dataDir, SPACED_CHAIN } from '../utils' const BACKUP_DIR = `${dataDir}/${SPACED_CHAIN}/wallets_backup` const BACKUP_PATH = `${BACKUP_DIR}/default.json` export const exportWallet = sdk.Action.withoutInput( // id 'export-wallet', // metadata async ({ effects }) => ({ name: i18n('Export Wallet'), description: i18n( 'Export the `default` spaces wallet to /data/mainnet/wallets_backup/default.json and surface the JSON below.', ), warning: null, allowedStatuses: 'only-running', group: actionGroups.spaced, visibility: 'enabled', }), // run async ({ effects }) => { const spacedAuth = await storeJson.read((s) => s.spacedAuth).once() if (!spacedAuth) { return { version: '1', title: i18n('Failure'), message: i18n('Could not export wallet: ${error}', { error: 'spacedAuth missing from store.json', }), result: null, } } const mounts = sdk.Mounts.of().mountVolume({ volumeId: 'main', subpath: null, mountpoint: dataDir, readonly: false, }) const cmd = [ `mkdir -p ${BACKUP_DIR}`, `/root/.cargo/bin/space-cli --chain ${SPACED_CHAIN} --rpc-user ${spacedAuth.username} --rpc-password ${spacedAuth.password} exportwallet ${BACKUP_PATH}`, ].join(' && ') const result = await sdk.SubContainer.withTemp( effects, { imageId: 'spaces' }, mounts, 'spaces-export-wallet', async (subc) => { const exportRes = await subc.exec(['bash', '-c', cmd], { user: 'root' }) if (exportRes.exitCode !== 0) { return { ok: false as const, stderr: (exportRes.stderr ?? '').toString(), exitCode: exportRes.exitCode, } } const catRes = await subc.exec(['cat', BACKUP_PATH], { user: 'root' }) if (catRes.exitCode !== 0) { return { ok: false as const, stderr: (catRes.stderr ?? '').toString(), exitCode: catRes.exitCode, } } return { ok: true as const, body: (catRes.stdout ?? '').toString() } }, ) if (!result.ok) { return { version: '1', title: i18n('Failure'), message: i18n('Could not export wallet: ${error}', { error: result.stderr || `exit ${result.exitCode}`, }), result: null, } } return { version: '1', title: i18n('Wallet exported.'), message: i18n( 'A copy of this JSON is saved inside the container at /data/mainnet/wallets_backup/default.json.', ), result: { type: 'single', name: i18n('Wallet JSON'), description: null, value: result.body.trim(), masked: true, copyable: true, qr: false, }, } }, )