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 support mnemonic #29

Merged
merged 2 commits into from
Jan 23, 2024
Merged
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
39 changes: 34 additions & 5 deletions src/lib/PhatBaseCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import type { Result, Vec, u64, u8, Text, Bool, Struct } from '@polkadot/types'
import type { AccountId, ChainType, Hash } from '@polkadot/types/interfaces'
import { createPublicClient, createWalletClient, http } from 'viem'
import { mainnet } from 'viem/chains'
import { privateKeyToAccount } from 'viem/accounts'
import { privateKeyToAccount, mnemonicToAccount } from 'viem/accounts'

import {
MAX_BUILD_SIZE,
Expand All @@ -49,6 +49,8 @@ export interface ParsedFlags {
readonly accountFilePath: string
readonly accountPassword: string
readonly privateKey: string
readonly mnemonic: string
readonly addressIndex: number
readonly coreSettings: string
readonly pruntimeUrl: string
readonly externalAccountId: string
Expand Down Expand Up @@ -138,23 +140,34 @@ export default abstract class PhatBaseCommand extends BaseCommand {
char: 'a',
required: false,
description: 'Path to polkadot account JSON file',
exclusive: ['suri', 'privateKey'],
exclusive: ['suri', 'privateKey', 'mnemonic'],
}),
accountPassword: Flags.string({
char: 'p',
required: false,
description: 'Polkadot account password',
exclusive: ['suri', 'privateKey'],
exclusive: ['suri', 'privateKey', 'mnemonic'],
}),
suri: Flags.string({
required: false,
description: 'Substrate uri',
exclusive: ['accountFilePath', 'privateKey'],
exclusive: ['accountFilePath', 'privateKey', 'mnemonic'],
}),
privateKey: Flags.string({
description: 'EVM account private key',
required: false,
exclusive: ['suri', 'accountFilePath'],
exclusive: ['suri', 'accountFilePath', 'mnemonic'],
}),
mnemonic: Flags.string({
description: 'EVM account mnemonic',
required: false,
exclusive: ['suri', 'accountFilePath', 'privateKey'],
}),
addressIndex: Flags.integer({
description: 'EVM account address index',
required: false,
default: 0,
exclusive: ['suri', 'accountFilePath', 'privateKey'],
}),
endpoint: Flags.string({
description: 'Phala Blockchain RPC endpoint',
Expand Down Expand Up @@ -518,6 +531,22 @@ export default abstract class PhatBaseCommand extends BaseCommand {
const provider = await EvmAccountMappingProvider.create(apiPromise, client, account)
return provider
}
if (this.parsedFlags.mnemonic || (process.env.MNEMONIC && !this.parsedFlags.suri && !this.parsedFlags.accountFilePath)) {
if (!apiPromise.consts?.evmAccountMapping?.eip712Name) {
this.action.fail('The current connected chain does not support EVM wallets.')
this.exit(1)
}
const account = mnemonicToAccount(this.parsedFlags.mnemonic || process.env.MNEMONIC!, {
addressIndex: this.parsedFlags.addressIndex,
})
const client = createWalletClient({
account,
chain: mainnet,
transport: http()
})
const provider = await EvmAccountMappingProvider.create(apiPromise, client, account)
return provider
}
const pair = await this.getDecodedPair({
suri: this.parsedFlags.suri || process.env.POLKADOT_WALLET_SURI,
accountFilePath: this.parsedFlags.accountFilePath || process.env.POLKADOT_WALLET_ACCOUNT_FILE,
Expand Down
Loading