(args: CommonArgs<{ filename?: string; readonly?: boolean }>)
| 24 | } |
| 25 | |
| 26 | export async function exportWallet(args: CommonArgs<{ filename?: string; readonly?: boolean }>) { |
| 27 | const { wallet, opts } = args; |
| 28 | if (opts.command) { |
| 29 | Object.assign(opts, command(args)); |
| 30 | } |
| 31 | const replaceTilde = str => str.startsWith('~') ? str.replace('~', os.homedir()) : str; |
| 32 | |
| 33 | const readOnly = opts.readonly ?? (wallet.isReadOnly() || await prompt.confirm({ |
| 34 | message: 'Export as read-only (cannot send funds)?', |
| 35 | initialValue: false |
| 36 | })); |
| 37 | if (prompt.isCancel(readOnly)) { |
| 38 | throw new UserCancelled(); |
| 39 | } |
| 40 | |
| 41 | const filename = opts.filename || await prompt.text({ |
| 42 | message: 'Enter filename to export to:', |
| 43 | initialValue: `~/${wallet.name}${readOnly ? '-nokey' : ''}-export.json`, |
| 44 | validate: (value) => { |
| 45 | value = value.trim(); |
| 46 | if (!value) return 'Filename is required'; |
| 47 | value = replaceTilde(value); |
| 48 | |
| 49 | try { |
| 50 | let _path = ''; |
| 51 | for (const part of value.split('/').slice(0, -1)) { |
| 52 | _path = path.join(_path, part); |
| 53 | if (fs.existsSync(_path)) { |
| 54 | fs.accessSync(_path, fs.constants.W_OK); |
| 55 | } else { |
| 56 | break; // stop at the first non-existing directory |
| 57 | } |
| 58 | } |
| 59 | } catch (err) { |
| 60 | return 'Cannot write to the specified file path: ' + err.message; |
| 61 | } |
| 62 | return; // valid value |
| 63 | } |
| 64 | }); |
| 65 | if (prompt.isCancel(filename)) { |
| 66 | throw new UserCancelled(); |
| 67 | } |
| 68 | |
| 69 | const exportPassword = await getPassword('File encryption password:', { hidden: false, minLength: 6 }); |
| 70 | |
| 71 | await wallet.export({ |
| 72 | filename: replaceTilde(filename), |
| 73 | exportPassword, |
| 74 | readOnly |
| 75 | }); |
| 76 | |
| 77 | prompt.log.success('Exported to ' + filename); |
| 78 | }; |
no test coverage detected