Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add wallet:new command #41

Merged
merged 2 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ USAGE
* [`terrain task:new [TASK]`](#terrain-tasknew-task)
* [`terrain task:run [TASK]`](#terrain-taskrun-task)
* [`terrain test CONTRACT-NAME`](#terrain-test-contract-name)
* [`terrain wallet:new`](#terrain-wallet-new)

## `terrain code:new [NAME]`

Expand Down Expand Up @@ -692,6 +693,24 @@ EXAMPLES
```

_See code: [src/commands/test.ts](https://github.com/terra-money/terrain/blob/v0.2.0/src/commands/test.ts)_

## `terrain wallet:new`

Generate a new wallet to use for signing contracts
```
USAGE
$ terrain wallet:new [--outfile <value>]

FLAGS
--index=<value> key index to use, default value is 0
--outfile=<value> absolute path to store the mnemonic key to. If omitted, output to stdout

DESCRIPTION
Generate a new wallet.
```

_See code: [src/commands/task/run.ts](./src/commands/wallet/new.ts)_

<!-- commandsstop -->


Expand Down
41 changes: 41 additions & 0 deletions src/commands/wallet/new.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { Command, flags } from '@oclif/command';
import { MnemonicKey } from '@terra-money/terra.js';
import * as fs from 'fs';

export default class WalletNew extends Command {
static description = 'Generate a new wallet.';

static flags = {
outfile: flags.string({
description: 'absolute path to store the mnemonic key to. If omitted, output to stdout',
}),
index: flags.integer({
description: 'key index to use, default value is 0',
default: 0,
}),
};

async run() {
const { flags } = this.parse(WalletNew);
this.log('Generating new terra-wallet');

const mk = new MnemonicKey({
index: flags.index,
});
if (flags.outfile) {
if (fs.existsSync(flags.outfile)) {
this.error(`outfile: '${flags.outfile}' already exists, abort`);
this.exit(1);
}

this.log(`saving mnemonic to '${flags.outfile}'`);
fs.writeFileSync(flags.outfile, mk.mnemonic);
this.exit(0);
}

this.log('mnemonic key:');
this.log(mk.mnemonic);

this.exit(0);
}
}