|
1 | 1 | import { getRandomValues } from "node:crypto";
|
| 2 | +import type { Signer } from "@xmtp/node-sdk"; |
2 | 3 | import { fromString, toString } from "uint8arrays";
|
3 |
| -import { toBytes } from "viem"; |
| 4 | +import { createWalletClient, http, toBytes } from "viem"; |
4 | 5 | import { privateKeyToAccount } from "viem/accounts";
|
| 6 | +import { sepolia } from "viem/chains"; |
5 | 7 |
|
6 |
| -/** |
7 |
| - * Create a signer from a private key |
8 |
| - * @param privateKey - The private key of the account |
9 |
| - * @returns The signer |
10 |
| - */ |
11 |
| -export const createSigner = (privateKey: `0x${string}`) => { |
12 |
| - /* Convert the private key to an account */ |
13 |
| - const account = privateKeyToAccount(privateKey); |
14 |
| - /* Return the signer */ |
| 8 | +interface User { |
| 9 | + key: string; |
| 10 | + account: ReturnType<typeof privateKeyToAccount>; |
| 11 | + wallet: ReturnType<typeof createWalletClient>; |
| 12 | +} |
| 13 | + |
| 14 | +export const createUser = (key: string): User => { |
| 15 | + const account = privateKeyToAccount(key as `0x${string}`); |
| 16 | + return { |
| 17 | + key, |
| 18 | + account, |
| 19 | + wallet: createWalletClient({ |
| 20 | + account, |
| 21 | + chain: sepolia, |
| 22 | + transport: http(), |
| 23 | + }), |
| 24 | + }; |
| 25 | +}; |
| 26 | + |
| 27 | +export const createSigner = (key: string): Signer => { |
| 28 | + const user = createUser(key); |
15 | 29 | return {
|
16 |
| - getAddress: () => account.address, |
| 30 | + walletType: "EOA", |
| 31 | + getAddress: () => user.account.address, |
17 | 32 | signMessage: async (message: string) => {
|
18 |
| - const signature = await account.signMessage({ |
| 33 | + const signature = await user.wallet.signMessage({ |
19 | 34 | message,
|
| 35 | + account: user.account, |
20 | 36 | });
|
21 | 37 | return toBytes(signature);
|
22 | 38 | },
|
|
0 commit comments