Skip to content

Commit 3aaad1e

Browse files
gregfromstljnsdls
andauthored
[SDK] generateWallet utility function (#2602)
Co-authored-by: Jonas Daniels <jonas.daniels@outlook.com>
1 parent 55848db commit 3aaad1e

File tree

4 files changed

+40
-5
lines changed

4 files changed

+40
-5
lines changed

.changeset/stupid-impalas-press.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": minor
3+
---
4+
5+
Adds generateAccount function for generating a random local account

packages/thirdweb/src/exports/wallets.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ export {
88
export type { Wallet, Account } from "../wallets/interfaces/wallet.js";
99
export type { ConnectionStatus } from "../wallets/manager/index.js";
1010

11+
// utils
1112
export {
1213
getWalletBalance,
1314
type GetWalletBalanceOptions,
1415
} from "../wallets/utils/getWalletBalance.js";
16+
export {
17+
generateAccount,
18+
type GenerateAccountOptions,
19+
} from "../wallets/utils/generateAccount.js";
1520

1621
// private-key
1722
export {
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import { secp256k1 } from "@noble/curves/secp256k1";
2+
import type { Account } from "../interfaces/wallet.js";
3+
import { toHex } from "../../utils/encoding/hex.js";
4+
import { privateKeyAccount } from "../private-key.js";
5+
import type { ThirdwebClient } from "../../client/client.js";
6+
7+
export type GenerateAccountOptions = {
8+
client: ThirdwebClient;
9+
};
10+
11+
/**
12+
* Generates a new account with a random private key.
13+
* @param options - The options for generating the account.
14+
* @param options.client - The Thirdweb client to use for the generated account.
15+
* @returns A Thirdweb account.
16+
* @example
17+
* ```ts
18+
* import { generateAccount } from "thirdweb/wallets";
19+
* const account = await generateAccount({ client });
20+
* ```
21+
* @walletUtils
22+
*/
23+
export async function generateAccount(
24+
options: GenerateAccountOptions,
25+
): Promise<Account> {
26+
const privateKey = toHex(secp256k1.utils.randomPrivateKey());
27+
return privateKeyAccount({ privateKey, client: options.client });
28+
}

pnpm-lock.yaml

Lines changed: 2 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)