-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add bdk wallet create function (#81)
- Loading branch information
1 parent
9a25b08
commit a0f21b9
Showing
12 changed files
with
173 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Wallet 指令文件 | ||
|
||
(English version)(Work In Progress) | ||
|
||
## 目錄 | ||
|
||
- [Wallet](#wallet) | ||
|
||
## Wallet | ||
|
||
### `bdk wallet create` | ||
|
||
Description: 產生 Wallet 的相關資訊 | ||
|
||
| Options | Type | Description | Required | Default | | ||
| --------------------- | :-----: | ------------------------------ | :------: | ------- | | ||
| --help | boolean | Show help | | | | ||
| --version | boolean | Show version number | | | | ||
| -i, --interactive | boolean | 是否使用 Cathay BDK 互動式問答 | | | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# 使用範例 | ||
(English version)(Work In Progress) | ||
|
||
## 目錄 | ||
- [建立 Wallet](#建立-wallet) | ||
|
||
## 建立 Wallet | ||
|
||
```bash | ||
# 輸入指令,啟動 Wallet 互動式介面 | ||
bdk wallet create -i | ||
``` | ||
|
||
選擇欲建立的 Wallet 類型(預設為 ethereum),確認輸入後即可獲得 `wallet address`、`private key`,請自行妥善保管,若不使用互動模式,則預設為建立 `Ethereum Wallet`。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import prompts from 'prompts' | ||
import { Argv, Arguments } from 'yargs' | ||
import Wallet from '../service/wallet' | ||
import { onCancel } from '../../util/error' | ||
import { WalletCreateType, WalletType } from '../model/type/wallet.type' | ||
import ora from 'ora' | ||
|
||
export const command = 'create' | ||
|
||
export const desc = '產生 Wallet 的相關資訊' | ||
|
||
interface OptType { | ||
interactive: boolean | ||
} | ||
|
||
export const builder = (yargs: Argv<OptType>) => { | ||
return yargs | ||
.example('bdk wallet create --interactive', 'Cathay BDK 互動式問答') | ||
.option('interactive', { type: 'boolean', description: '是否使用 Cathay BDK 互動式問答', alias: 'i' }) | ||
} | ||
|
||
export const handler = async (argv: Arguments<OptType>) => { | ||
const wallet = new Wallet() | ||
|
||
let type: WalletType | ||
|
||
if (argv.interactive) { | ||
const walletOption = [ | ||
{ title: 'ethereum', value: WalletType.ETHEREUM }, | ||
] | ||
|
||
const { walletType } = await prompts([{ | ||
type: 'select', | ||
name: 'walletType', | ||
message: 'What type of wallet you want to create?', | ||
choices: walletOption, | ||
}], { onCancel }) | ||
type = walletType | ||
} else { | ||
type = WalletType.ETHEREUM | ||
} | ||
|
||
const walletCreateConfig: WalletCreateType = { | ||
type: type, | ||
} | ||
|
||
const spinner = ora('Wallet Create ...').start() | ||
const result = await wallet.create(walletCreateConfig) | ||
spinner.succeed(`Wallet Create Result:\n ${result}`) | ||
spinner.succeed('Wallet Create Successfully!') | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum WalletType { | ||
ETHEREUM = 'ethereum' | ||
} | ||
/** | ||
* @requires ethereum - [string] ethereum | ||
*/ | ||
export interface WalletCreateType { | ||
type: WalletType | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
|
||
import { ethers } from 'ethers' | ||
import { WalletCreateType, WalletType } from '../model/type/wallet.type' | ||
|
||
export default class Wallet { | ||
/** | ||
* @description 建立 quorum network | ||
*/ | ||
public create (walletCreateConfig: WalletCreateType) { | ||
const { address, privateKey } = this.createWalletAddress(walletCreateConfig.type) | ||
const walletAddress = address | ||
const result = `🔑 Your ${walletCreateConfig.type} wallet address: 0x${walletAddress}\n 🔑 Wallet private key: ${privateKey}` | ||
return result | ||
} | ||
|
||
/** @ignore */ | ||
public createWalletAddress (type: WalletType) { | ||
let nodekey: ethers.Wallet | ||
let privateKey: string | ||
let publicKey: string | ||
let address: string | ||
|
||
switch (type) { | ||
case WalletType.ETHEREUM: | ||
nodekey = ethers.Wallet.createRandom() | ||
privateKey = nodekey.privateKey.replace(/^0x/, '') | ||
publicKey = nodekey.publicKey.replace(/^0x04/, '') | ||
address = nodekey.address.replace(/^0x/, '').toLowerCase() | ||
break | ||
} | ||
|
||
return { privateKey, publicKey, address } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Argv } from 'yargs' | ||
|
||
export const command = 'wallet' | ||
|
||
export const desc = '管理 Wallet 的指令' | ||
|
||
export const builder = (yargs: Argv) => { | ||
return yargs.commandDir('../wallet/command').demandCommand() | ||
} | ||
|
||
export const handler = {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* global describe, it */ | ||
import assert from 'assert' | ||
import Wallet from '../../../src/wallet/service/wallet' | ||
import { WalletCreateType, WalletType } from '../../../src/wallet/model/type/wallet.type' | ||
|
||
// write a test for the wallet class | ||
describe('Wallet.Create', function () { | ||
this.timeout(1000) | ||
const wallet = new Wallet() | ||
|
||
// create a new ethereum wallet | ||
describe('Wallet.Create', () => { | ||
it('should create a ethereum wallet with address and private key', () => { | ||
const WalletCreateType: WalletCreateType = { | ||
type: WalletType.ETHEREUM, | ||
} | ||
const result = wallet.create(WalletCreateType) | ||
|
||
assert(result.length > 0, 'Wallet create error') | ||
}) | ||
}) | ||
}) |