Skip to content

Commit

Permalink
chore: Allow Specified Network ID on Mnemonic Phrase Wallet Import (#363
Browse files Browse the repository at this point in the history
)
  • Loading branch information
John-peterson-coinbase authored Jan 17, 2025
1 parent 87e76c3 commit 4477a1e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/coinbase/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,12 +143,16 @@ export class Wallet {
* Allows for the loading of an existing CDP wallet into CDP.
* - If MnemonicSeedPhrase: Must contain a valid BIP-39 mnemonic phrase (12, 15, 18, 21, or 24 words).
* Allows for the import of an external wallet into CDP as a 1-of-1 wallet.
* @param networkId - the ID of the blockchain network. Defaults to 'base-sepolia'.
* @returns A Promise that resolves to the loaded Wallet instance
* @throws {ArgumentError} If the data format is invalid.
* @throws {ArgumentError} If the seed is not provided.
* @throws {ArgumentError} If the mnemonic seed phrase is invalid.
*/
public static async import(data: WalletData | MnemonicSeedPhrase): Promise<Wallet> {
public static async import(
data: WalletData | MnemonicSeedPhrase,
networkId: string = Coinbase.networks.BaseSepolia,
): Promise<Wallet> {
// Check if data is a mnemonic seed phrase object
if (isMnemonicSeedPhrase(data)) {
// Handle mnemonic seed phrase object import
Expand All @@ -168,7 +172,7 @@ export class Wallet {
// Create wallet using the provided seed
const wallet = await Wallet.createWithSeed({
seed: seed,
networkId: Coinbase.networks.BaseSepolia,
networkId,
});

// Ensure the wallet is created
Expand Down
12 changes: 12 additions & 0 deletions src/tests/wallet_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,18 @@ describe("Wallet Class", () => {
expect(Coinbase.apiClients.address!.listAddresses).toHaveBeenCalledTimes(1);
});

it("successfully imports a wallet from a valid 24-word mnemonic on base-mainnet", async () => {
const wallet = await Wallet.import(
{ mnemonicPhrase: validMnemonic },
Coinbase.networks.BaseMainnet,
);
expect(wallet).toBeInstanceOf(Wallet);
expect(wallet.getNetworkId()).toEqual(Coinbase.networks.BaseMainnet);
expect(Coinbase.apiClients.wallet!.createWallet).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.address!.createAddress).toHaveBeenCalledTimes(1);
expect(Coinbase.apiClients.address!.listAddresses).toHaveBeenCalledTimes(1);
});

it("throws an error when mnemonic is empty", async () => {
await expect(Wallet.import({ mnemonicPhrase: "" })).rejects.toThrow(
"BIP-39 mnemonic seed phrase must be provided",
Expand Down

0 comments on commit 4477a1e

Please sign in to comment.