From dc698d2f44d6ff11c39eb8fd101e989de1e2965c Mon Sep 17 00:00:00 2001 From: pacoyang Date: Tue, 23 Jan 2024 12:42:41 +0800 Subject: [PATCH 1/2] feat: add support for mnemonic --- src/lib/PhatBaseCommand.ts | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/src/lib/PhatBaseCommand.ts b/src/lib/PhatBaseCommand.ts index ac69579..b19fbf9 100644 --- a/src/lib/PhatBaseCommand.ts +++ b/src/lib/PhatBaseCommand.ts @@ -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, @@ -49,6 +49,7 @@ export interface ParsedFlags { readonly accountFilePath: string readonly accountPassword: string readonly privateKey: string + readonly mnemonic: string readonly coreSettings: string readonly pruntimeUrl: string readonly externalAccountId: string @@ -138,23 +139,28 @@ 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'], }), endpoint: Flags.string({ description: 'Phala Blockchain RPC endpoint', @@ -518,6 +524,20 @@ 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!) + 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, From 6e3a14ff8f7d77cd9b56938535babf387d88ec93 Mon Sep 17 00:00:00 2001 From: pacoyang Date: Tue, 23 Jan 2024 12:55:10 +0800 Subject: [PATCH 2/2] feat: add support for address index selection --- src/lib/PhatBaseCommand.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/lib/PhatBaseCommand.ts b/src/lib/PhatBaseCommand.ts index b19fbf9..9116f1d 100644 --- a/src/lib/PhatBaseCommand.ts +++ b/src/lib/PhatBaseCommand.ts @@ -50,6 +50,7 @@ export interface ParsedFlags { readonly accountPassword: string readonly privateKey: string readonly mnemonic: string + readonly addressIndex: number readonly coreSettings: string readonly pruntimeUrl: string readonly externalAccountId: string @@ -162,6 +163,12 @@ export default abstract class PhatBaseCommand extends BaseCommand { 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', required: false, @@ -529,7 +536,9 @@ export default abstract class PhatBaseCommand extends BaseCommand { this.action.fail('The current connected chain does not support EVM wallets.') this.exit(1) } - const account = mnemonicToAccount(this.parsedFlags.mnemonic || process.env.MNEMONIC!) + const account = mnemonicToAccount(this.parsedFlags.mnemonic || process.env.MNEMONIC!, { + addressIndex: this.parsedFlags.addressIndex, + }) const client = createWalletClient({ account, chain: mainnet,