import { storeJson } from '../fileModels/storeJson' import { i18n } from '../i18n' import { sdk } from '../sdk' import { BITCOIN_RPC_USER, BITCOIND_PACKAGE_ID, randomPassword, } from '../utils' export const setBitcoinRpc = sdk.Action.withoutInput( // id 'set-bitcoin-rpc', // metadata async ({ effects }) => ({ name: i18n('Set up Bitcoin RPC'), description: i18n('Re-run the bitcoind RPC credential setup for Spaces'), warning: null, allowedStatuses: 'any', group: null, visibility: 'enabled', }), // run async ({ effects }) => { const existing = await storeJson.read((s) => s.btcAuth).once() const username = existing?.username ?? BITCOIN_RPC_USER const password = existing?.password ?? randomPassword() try { await effects.action.run({ packageId: BITCOIND_PACKAGE_ID, actionId: 'generate-rpc-dependent', input: { username, password }, }) } catch (err) { return { version: '1', title: i18n('Failure'), message: i18n('Could not call bitcoind. Is Bitcoin installed and running?'), result: null, } } if (!existing) { await storeJson.merge( effects, { btcAuth: { username, password } }, { allowWriteAfterConst: true }, ) } return { version: '1', title: i18n('Success'), message: i18n('Bitcoin RPC credentials have been re-sent to bitcoind.'), result: null, } }, )