@dag-kit/kit 1.0.2 → 1.0.4-alpha.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. package/dist/index.cjs +564 -0
  2. package/dist/index.cjs.map +1 -0
  3. package/dist/index.d.cts +209 -0
  4. package/dist/index.d.ts +209 -0
  5. package/dist/index.js +544 -0
  6. package/dist/index.js.map +1 -0
  7. package/package.json +16 -11
  8. package/src/clients/actions/contract.ts +1 -1
  9. package/src/clients/actions/example.ts +252 -252
  10. package/src/clients/actions/example2.ts +60 -60
  11. package/src/clients/actions/index.ts +145 -0
  12. package/src/clients/actions/main.ts +34 -17
  13. package/src/clients/actions/test.ts +210 -210
  14. package/src/clients/types.ts +2 -1
  15. package/src/exports/index.ts +7 -4
  16. package/src/exports/public-types.ts +6 -0
  17. package/src/index.ts +17 -0
  18. package/src/signers/PrivateKeySigner.ts +41 -0
  19. package/src/signers/PrivySigner.ts +93 -0
  20. package/src/signers/types.ts +45 -0
  21. package/src/version.ts +1 -1
  22. package/dist/esm/clients/actions/contract.js +0 -42
  23. package/dist/esm/clients/actions/example.js +0 -211
  24. package/dist/esm/clients/actions/example2.js +0 -50
  25. package/dist/esm/clients/actions/index.js +0 -1
  26. package/dist/esm/clients/actions/main.js +0 -415
  27. package/dist/esm/clients/actions/test.js +0 -1
  28. package/dist/esm/clients/actions/testPaymasterService.js +0 -88
  29. package/dist/esm/clients/chains.js +0 -19
  30. package/dist/esm/clients/types.js +0 -1
  31. package/dist/esm/exports/index.js +0 -2
  32. package/dist/esm/version.js +0 -3
  33. package/dist/types/clients/actions/contract.d.ts +0 -12
  34. package/dist/types/clients/actions/contract.d.ts.map +0 -1
  35. package/dist/types/clients/actions/example.d.ts +0 -2
  36. package/dist/types/clients/actions/example.d.ts.map +0 -1
  37. package/dist/types/clients/actions/example2.d.ts +0 -2
  38. package/dist/types/clients/actions/example2.d.ts.map +0 -1
  39. package/dist/types/clients/actions/index.d.ts +0 -1
  40. package/dist/types/clients/actions/index.d.ts.map +0 -1
  41. package/dist/types/clients/actions/main.d.ts +0 -40
  42. package/dist/types/clients/actions/main.d.ts.map +0 -1
  43. package/dist/types/clients/actions/test.d.ts +0 -1
  44. package/dist/types/clients/actions/test.d.ts.map +0 -1
  45. package/dist/types/clients/actions/testPaymasterService.d.ts +0 -2
  46. package/dist/types/clients/actions/testPaymasterService.d.ts.map +0 -1
  47. package/dist/types/clients/chains.d.ts +0 -81
  48. package/dist/types/clients/chains.d.ts.map +0 -1
  49. package/dist/types/clients/types.d.ts +0 -31
  50. package/dist/types/clients/types.d.ts.map +0 -1
  51. package/dist/types/exports/index.d.ts +0 -4
  52. package/dist/types/exports/index.d.ts.map +0 -1
  53. package/dist/types/version.d.ts +0 -2
  54. package/dist/types/version.d.ts.map +0 -1
@@ -1,210 +1,210 @@
1
- import "dotenv/config";
2
- import { toSimpleSmartAccount } from "permissionless/accounts";
3
- import {
4
- createPublicClient,
5
- createWalletClient,
6
- http,
7
- parseEther,
8
- encodeFunctionData,
9
- defineChain,
10
- } from "viem";
11
- import { privateKeyToAccount } from "viem/accounts";
12
- import { createPimlicoClient } from "permissionless/clients/pimlico";
13
- import { entryPoint06Address } from "viem/account-abstraction";
14
- import { createSmartAccountClient } from "permissionless";
15
- import { abi } from "./contract";
16
-
17
- // Configuration
18
- const adminKey =
19
- "0x5810098e367422376897bb2645c5ada5850a99aeec0505a58d38853ebd7f9f31";
20
- const FACTORY_ADDRESS = "0x8FaB6DF00085eb05D5F2C1FA46a6E539587ae3f3"; // Your factory
21
- const TARGET_ADDRESS = "0x7fd5385efcB7B2898933288948a9496CDc0fA8ee"; // Your desired address
22
-
23
- // BlockDAG Awakening Testnet
24
- const awakening = defineChain({
25
- id: 1043,
26
- name: "Awakening Testnet",
27
- nativeCurrency: { decimals: 18, name: "Dag", symbol: "DAG" },
28
- rpcUrls: { default: { http: ["https://relay.awakening.bdagscan.com"] } },
29
- blockExplorers: {
30
- default: { name: "Explorer", url: "https://awakening.bdagscan.com/" },
31
- },
32
- });
33
-
34
- const localBundler = "http://0.0.0.0:3000/";
35
-
36
- console.log("\nšŸ” Using Your Existing Address");
37
- console.log("=================================\n");
38
-
39
- const publicClient = createPublicClient({
40
- chain: awakening,
41
- transport: http(awakening.rpcUrls.default.http[0]),
42
- });
43
-
44
- const walletClient = createWalletClient({
45
- account: privateKeyToAccount(adminKey),
46
- chain: awakening,
47
- transport: http(awakening.rpcUrls.default.http[0]),
48
- });
49
-
50
- console.log(`Target Address: ${TARGET_ADDRESS}`);
51
- console.log(`Owner: ${privateKeyToAccount(adminKey).address}`);
52
- console.log(`Factory: ${FACTORY_ADDRESS}\n`);
53
-
54
- // Step 1: Create account using the specific address
55
- // Since the account is already deployed, we just need to reference it
56
- const account = await toSimpleSmartAccount({
57
- client: publicClient,
58
- owner: privateKeyToAccount(adminKey),
59
- factoryAddress: FACTORY_ADDRESS,
60
- entryPoint: {
61
- address: entryPoint06Address,
62
- version: "0.6",
63
- },
64
- address: TARGET_ADDRESS, // Use the existing address directly
65
- });
66
-
67
- console.log("=== Smart Account Information ===");
68
- console.log(`Address: ${account.address}`);
69
- console.log(
70
- `Explorer: https://awakening.bdagscan.com/address/${account.address}`
71
- );
72
- console.log("āœ… Using your existing account\n");
73
-
74
- // Check if deployed
75
- const code = await publicClient.getCode({
76
- address: account.address,
77
- });
78
-
79
- const isDeployed = code && code !== "0x";
80
- console.log(
81
- `Deployment Status: ${isDeployed ? "āœ… Deployed" : "āš ļø Not deployed"}`
82
- );
83
-
84
- // Check balance
85
- const balance = await publicClient.getBalance({
86
- address: account.address,
87
- });
88
-
89
- const balanceInDAG = Number(balance) / 1e18;
90
- console.log(`Balance: ${balanceInDAG} DAG`);
91
-
92
- if (!isDeployed) {
93
- console.log("\nāš ļø Account not deployed yet.");
94
- console.log("The account should already be deployed based on the explorer.");
95
- console.log("If you need to deploy it, do so manually first.\n");
96
- }
97
-
98
- // Step 3: Test bundler connection
99
- console.log("=== Testing Bundler ===");
100
- try {
101
- const dagClient = createPimlicoClient({
102
- transport: http(localBundler),
103
- entryPoint: {
104
- address: entryPoint06Address,
105
- version: "0.6",
106
- },
107
- });
108
-
109
- const gasPrice = await dagClient.getUserOperationGasPrice();
110
- console.log("āœ… Bundler responding");
111
- console.log(`Gas Price: ${gasPrice.fast.maxFeePerGas} wei\n`);
112
- } catch (error: any) {
113
- console.error("āŒ Bundler not responding:", error.message);
114
- console.log("\nšŸ’” Start Alto bundler:");
115
- console.log(`./alto \\
116
- --entrypoints "${entryPoint06Address}" \\
117
- --executor-private-keys "${adminKey}" \\
118
- --utility-private-key "${adminKey}" \\
119
- --rpc-url "https://relay.awakening.bdagscan.com" \\
120
- --port 3000 \\
121
- --safe-mode false \\
122
- --dangerous-skip-user-operation-validation true`);
123
- process.exit(1);
124
- }
125
-
126
- // Step 4: Send test UserOperation
127
- console.log("=== Sending Test UserOperation ===");
128
-
129
- const smartAccountClient = createSmartAccountClient({
130
- bundlerTransport: http(localBundler),
131
- chain: awakening,
132
- account,
133
- });
134
-
135
- const cd = encodeFunctionData({
136
- abi,
137
- functionName: "decrement",
138
- args: [],
139
- });
140
-
141
- try {
142
- // Simple test: send 0 value to self
143
- const txHash = await smartAccountClient.sendTransaction({
144
- to: "0x692e69cA1Fe89eF72ca94B0E3a32A92835501a08",
145
- value: 0n,
146
- data: cd,
147
- maxFeePerGas: 50000000000n, // 50 gwei
148
- maxPriorityFeePerGas: 50000000000n, // 50 gwei
149
- callGasLimit: 150000n,
150
- verificationGasLimit: 500000n,
151
- preVerificationGas: 100000n,
152
- });
153
-
154
- console.log(`\nāœ… Success!`);
155
- console.log(`UserOperation Hash: ${txHash}`);
156
- console.log(`Explorer: https://awakening.bdagscan.com/tx/${txHash}`);
157
-
158
- console.log("\nā³ Waiting for confirmation...");
159
- await new Promise((resolve) => setTimeout(resolve, 5000));
160
-
161
- const newBalance = await publicClient.getBalance({
162
- address: account.address,
163
- });
164
- const newBalanceInDAG = Number(newBalance) / 1e18;
165
- console.log(`\nFinal Balance: ${newBalanceInDAG} DAG`);
166
- console.log(`Gas Cost: ${balanceInDAG - newBalanceInDAG} DAG`);
167
-
168
- console.log("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
169
- console.log("šŸŽ‰ Success! Your account is ready for reuse.");
170
- console.log(` Address: ${TARGET_ADDRESS}`);
171
- console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
172
-
173
- const result = await publicClient.readContract({
174
- address: "0x692e69cA1Fe89eF72ca94B0E3a32A92835501a08",
175
- abi: abi,
176
- functionName: "getNum",
177
- });
178
-
179
- console.log("Result is given as", result);
180
- } catch (error: any) {
181
- console.error("\nāŒ Transaction failed:");
182
- console.error(error.message);
183
-
184
- if (error.message?.includes("Missing or invalid parameters")) {
185
- console.log("\nāš ļø This is the known Awakening RPC issue.");
186
- console.log("The RPC cannot properly handle eth_call for UserOperations.");
187
- console.log(
188
- "\nYour setup is correct - the issue is with Awakening's RPC implementation."
189
- );
190
- }
191
-
192
- if (error.details) {
193
- console.log("\nDetails:", error.details);
194
- }
195
- }
196
-
197
- // Instructions for reuse
198
- console.log("\nšŸ’¾ To reuse this address in future scripts:");
199
- console.log(`
200
- const account = await toSimpleSmartAccount({
201
- client: publicClient,
202
- owner: privateKeyToAccount(adminKey),
203
- factoryAddress: "${FACTORY_ADDRESS}",
204
- entryPoint: {
205
- address: entryPoint06Address,
206
- version: "0.6",
207
- },
208
- address: "${TARGET_ADDRESS}", // Your existing account
209
- });
210
- `);
1
+ // import "dotenv/config";
2
+ // import { toSimpleSmartAccount } from "permissionless/accounts";
3
+ // import {
4
+ // createPublicClient,
5
+ // createWalletClient,
6
+ // http,
7
+ // parseEther,
8
+ // encodeFunctionData,
9
+ // defineChain,
10
+ // } from "viem";
11
+ // import { privateKeyToAccount } from "viem/accounts";
12
+ // import { createPimlicoClient } from "permissionless/clients/pimlico";
13
+ // import { entryPoint06Address } from "viem/account-abstraction";
14
+ // import { createSmartAccountClient } from "permissionless";
15
+ // import { abi } from "./contract";
16
+
17
+ // // Configuration
18
+ // const adminKey =
19
+ // "0x5810098e367422376897bb2645c5ada5850a99aeec0505a58d38853ebd7f9f31";
20
+ // const FACTORY_ADDRESS = "0x8FaB6DF00085eb05D5F2C1FA46a6E539587ae3f3"; // Your factory
21
+ // const TARGET_ADDRESS = "0x7fd5385efcB7B2898933288948a9496CDc0fA8ee"; // Your desired address
22
+
23
+ // // BlockDAG Awakening Testnet
24
+ // const awakening = defineChain({
25
+ // id: 1043,
26
+ // name: "Awakening Testnet",
27
+ // nativeCurrency: { decimals: 18, name: "Dag", symbol: "DAG" },
28
+ // rpcUrls: { default: { http: ["https://relay.awakening.bdagscan.com"] } },
29
+ // blockExplorers: {
30
+ // default: { name: "Explorer", url: "https://awakening.bdagscan.com/" },
31
+ // },
32
+ // });
33
+
34
+ // const localBundler = "http://0.0.0.0:3000/";
35
+
36
+ // console.log("\nšŸ” Using Your Existing Address");
37
+ // console.log("=================================\n");
38
+
39
+ // const publicClient = createPublicClient({
40
+ // chain: awakening,
41
+ // transport: http(awakening.rpcUrls.default.http[0]),
42
+ // });
43
+
44
+ // const walletClient = createWalletClient({
45
+ // account: privateKeyToAccount(adminKey),
46
+ // chain: awakening,
47
+ // transport: http(awakening.rpcUrls.default.http[0]),
48
+ // });
49
+
50
+ // console.log(`Target Address: ${TARGET_ADDRESS}`);
51
+ // console.log(`Owner: ${privateKeyToAccount(adminKey).address}`);
52
+ // console.log(`Factory: ${FACTORY_ADDRESS}\n`);
53
+
54
+ // // Step 1: Create account using the specific address
55
+ // // Since the account is already deployed, we just need to reference it
56
+ // const account = await toSimpleSmartAccount({
57
+ // client: publicClient,
58
+ // owner: privateKeyToAccount(adminKey),
59
+ // factoryAddress: FACTORY_ADDRESS,
60
+ // entryPoint: {
61
+ // address: entryPoint06Address,
62
+ // version: "0.6",
63
+ // },
64
+ // address: TARGET_ADDRESS, // Use the existing address directly
65
+ // });
66
+
67
+ // console.log("=== Smart Account Information ===");
68
+ // console.log(`Address: ${account.address}`);
69
+ // console.log(
70
+ // `Explorer: https://awakening.bdagscan.com/address/${account.address}`
71
+ // );
72
+ // console.log("āœ… Using your existing account\n");
73
+
74
+ // // Check if deployed
75
+ // const code = await publicClient.getCode({
76
+ // address: account.address,
77
+ // });
78
+
79
+ // const isDeployed = code && code !== "0x";
80
+ // console.log(
81
+ // `Deployment Status: ${isDeployed ? "āœ… Deployed" : "āš ļø Not deployed"}`
82
+ // );
83
+
84
+ // // Check balance
85
+ // const balance = await publicClient.getBalance({
86
+ // address: account.address,
87
+ // });
88
+
89
+ // const balanceInDAG = Number(balance) / 1e18;
90
+ // console.log(`Balance: ${balanceInDAG} DAG`);
91
+
92
+ // if (!isDeployed) {
93
+ // console.log("\nāš ļø Account not deployed yet.");
94
+ // console.log("The account should already be deployed based on the explorer.");
95
+ // console.log("If you need to deploy it, do so manually first.\n");
96
+ // }
97
+
98
+ // // Step 3: Test bundler connection
99
+ // console.log("=== Testing Bundler ===");
100
+ // try {
101
+ // const dagClient = createPimlicoClient({
102
+ // transport: http(localBundler),
103
+ // entryPoint: {
104
+ // address: entryPoint06Address,
105
+ // version: "0.6",
106
+ // },
107
+ // });
108
+
109
+ // const gasPrice = await dagClient.getUserOperationGasPrice();
110
+ // console.log("āœ… Bundler responding");
111
+ // console.log(`Gas Price: ${gasPrice.fast.maxFeePerGas} wei\n`);
112
+ // } catch (error: any) {
113
+ // console.error("āŒ Bundler not responding:", error.message);
114
+ // console.log("\nšŸ’” Start Alto bundler:");
115
+ // console.log(`./alto \\
116
+ // --entrypoints "${entryPoint06Address}" \\
117
+ // --executor-private-keys "${adminKey}" \\
118
+ // --utility-private-key "${adminKey}" \\
119
+ // --rpc-url "https://relay.awakening.bdagscan.com" \\
120
+ // --port 3000 \\
121
+ // --safe-mode false \\
122
+ // --dangerous-skip-user-operation-validation true`);
123
+ // process.exit(1);
124
+ // }
125
+
126
+ // // Step 4: Send test UserOperation
127
+ // console.log("=== Sending Test UserOperation ===");
128
+
129
+ // const smartAccountClient = createSmartAccountClient({
130
+ // bundlerTransport: http(localBundler),
131
+ // chain: awakening,
132
+ // account,
133
+ // });
134
+
135
+ // const cd = encodeFunctionData({
136
+ // abi,
137
+ // functionName: "decrement",
138
+ // args: [],
139
+ // });
140
+
141
+ // try {
142
+ // // Simple test: send 0 value to self
143
+ // const txHash = await smartAccountClient.sendTransaction({
144
+ // to: "0x692e69cA1Fe89eF72ca94B0E3a32A92835501a08",
145
+ // value: 0n,
146
+ // data: cd,
147
+ // maxFeePerGas: 50000000000n, // 50 gwei
148
+ // maxPriorityFeePerGas: 50000000000n, // 50 gwei
149
+ // callGasLimit: 150000n,
150
+ // verificationGasLimit: 500000n,
151
+ // preVerificationGas: 100000n,
152
+ // });
153
+
154
+ // console.log(`\nāœ… Success!`);
155
+ // console.log(`UserOperation Hash: ${txHash}`);
156
+ // console.log(`Explorer: https://awakening.bdagscan.com/tx/${txHash}`);
157
+
158
+ // console.log("\nā³ Waiting for confirmation...");
159
+ // await new Promise((resolve) => setTimeout(resolve, 5000));
160
+
161
+ // const newBalance = await publicClient.getBalance({
162
+ // address: account.address,
163
+ // });
164
+ // const newBalanceInDAG = Number(newBalance) / 1e18;
165
+ // console.log(`\nFinal Balance: ${newBalanceInDAG} DAG`);
166
+ // console.log(`Gas Cost: ${balanceInDAG - newBalanceInDAG} DAG`);
167
+
168
+ // console.log("\n━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
169
+ // console.log("šŸŽ‰ Success! Your account is ready for reuse.");
170
+ // console.log(` Address: ${TARGET_ADDRESS}`);
171
+ // console.log("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\n");
172
+
173
+ // const result = await publicClient.readContract({
174
+ // address: "0x692e69cA1Fe89eF72ca94B0E3a32A92835501a08",
175
+ // abi: abi,
176
+ // functionName: "getNum",
177
+ // });
178
+
179
+ // console.log("Result is given as", result);
180
+ // } catch (error: any) {
181
+ // console.error("\nāŒ Transaction failed:");
182
+ // console.error(error.message);
183
+
184
+ // if (error.message?.includes("Missing or invalid parameters")) {
185
+ // console.log("\nāš ļø This is the known Awakening RPC issue.");
186
+ // console.log("The RPC cannot properly handle eth_call for UserOperations.");
187
+ // console.log(
188
+ // "\nYour setup is correct - the issue is with Awakening's RPC implementation."
189
+ // );
190
+ // }
191
+
192
+ // if (error.details) {
193
+ // console.log("\nDetails:", error.details);
194
+ // }
195
+ // }
196
+
197
+ // // Instructions for reuse
198
+ // console.log("\nšŸ’¾ To reuse this address in future scripts:");
199
+ // console.log(`
200
+ // const account = await toSimpleSmartAccount({
201
+ // client: publicClient,
202
+ // owner: privateKeyToAccount(adminKey),
203
+ // factoryAddress: "${FACTORY_ADDRESS}",
204
+ // entryPoint: {
205
+ // address: entryPoint06Address,
206
+ // version: "0.6",
207
+ // },
208
+ // address: "${TARGET_ADDRESS}", // Your existing account
209
+ // });
210
+ // `);
@@ -10,6 +10,7 @@ import {
10
10
  encodeFunctionData,
11
11
  parseEther,
12
12
  } from "viem";
13
+ import { ISigner } from "../signers/types.js";
13
14
 
14
15
  export interface DagAAConfig {
15
16
  chain: Chain;
@@ -21,7 +22,7 @@ export interface DagAAConfig {
21
22
  }
22
23
 
23
24
  export interface SmartAccountConfig {
24
- owner: `0x${string}`;
25
+ signer: ISigner;
25
26
  accountAddress?: Address;
26
27
  }
27
28
 
@@ -1,10 +1,13 @@
1
- export { createDagAAClient, parseDAG } from "../clients/actions/main";
1
+ export { createDagAAClient, parseDAG } from "../clients/actions/main.js";
2
+ export { awakening, arbitrumSep } from "../clients/chains.js";
3
+ export { DagAAClient } from "../clients/actions/main.js";
2
4
 
3
- export {
5
+ export type {
4
6
  DagAAConfig,
5
7
  SmartAccountConfig,
6
8
  SendUserOperationParams,
7
9
  UserOperationReceipt,
8
- } from "../clients/types";
10
+ } from "./public-types.js";
9
11
 
10
- export { awakening, arbitrumSep } from "../clients/chains";
12
+ export { PrivySigner } from "../signers/PrivySigner.js";
13
+ export { PrivateKeySigner } from "../signers/PrivateKeySigner.js";
@@ -0,0 +1,6 @@
1
+ export type {
2
+ DagAAConfig,
3
+ SmartAccountConfig,
4
+ SendUserOperationParams,
5
+ UserOperationReceipt,
6
+ } from "../clients/types.js";
package/src/index.ts ADDED
@@ -0,0 +1,17 @@
1
+ export { createDagAAClient, parseDAG } from "./clients/actions/main.js";
2
+ export { awakening, arbitrumSep } from "./clients/chains.js";
3
+ export { DagAAClient } from "./clients/actions/main.js";
4
+
5
+ export type {
6
+ DagAAConfig,
7
+ SmartAccountConfig,
8
+ SendUserOperationParams,
9
+ UserOperationReceipt,
10
+ } from "./exports/public-types.js";
11
+
12
+ export { PrivySigner } from "./signers/PrivySigner.js";
13
+ export { PrivateKeySigner } from "./signers/PrivateKeySigner.js";
14
+
15
+ // Re-export commonly used viem types for convenience
16
+ export type { Address, Hash, Chain } from "viem";
17
+ export type { ISigner } from "./signers/types.js";
@@ -0,0 +1,41 @@
1
+ // src/signers/PrivateKeySigner.ts
2
+ import {
3
+ createWalletClient,
4
+ http,
5
+ type Chain,
6
+ type Account,
7
+ type WalletClient,
8
+ LocalAccount,
9
+ } from "viem";
10
+ import { privateKeyToAccount } from "viem/accounts";
11
+ import { ISigner } from "./types.js";
12
+
13
+ export class PrivateKeySigner implements ISigner {
14
+ private account: LocalAccount;
15
+ private walletClient;
16
+
17
+ constructor(privateKey: `0x${string}`, chain: Chain, rpcUrl: string) {
18
+ this.account = privateKeyToAccount(privateKey);
19
+ this.walletClient = createWalletClient({
20
+ account: this.account,
21
+ chain,
22
+ transport: http(rpcUrl),
23
+ });
24
+ }
25
+
26
+ getAccount(): LocalAccount {
27
+ return this.account;
28
+ }
29
+
30
+ getWalletClient(): WalletClient {
31
+ return this.walletClient;
32
+ }
33
+
34
+ getAddress(): `0x${string}` {
35
+ return this.account.address;
36
+ }
37
+
38
+ isReady(): boolean {
39
+ return true;
40
+ }
41
+ }
@@ -0,0 +1,93 @@
1
+ // src/signers/PrivySigner.ts
2
+ import {
3
+ type Account,
4
+ type WalletClient,
5
+ createWalletClient,
6
+ custom,
7
+ type Chain,
8
+ LocalAccount,
9
+ } from "viem";
10
+ import { ISigner } from "./types.js";
11
+
12
+ /**
13
+ * Privy Signer - Works with Privy's embedded wallets
14
+ *
15
+ * Usage in React:
16
+ * ```tsx
17
+ * import { useWallets } from '@privy-io/react-auth';
18
+ *
19
+ * const { wallets } = useWallets();
20
+ * const embeddedWallet = wallets.find(w => w.walletClientType === 'privy');
21
+ *
22
+ * const signer = new PrivySigner(embeddedWallet, chain);
23
+ * ```
24
+ */
25
+ export class PrivySigner implements ISigner {
26
+ private privyWallet: any; // Privy wallet object
27
+ private chain: Chain;
28
+ private cachedAddress?: `0x${string}`;
29
+ private cachedWalletClient;
30
+
31
+ constructor(privyWallet: any, chain: Chain) {
32
+ if (!privyWallet) {
33
+ throw new Error("Privy wallet is required");
34
+ }
35
+ this.privyWallet = privyWallet;
36
+ this.chain = chain;
37
+ }
38
+
39
+ async getAccount(): Promise<LocalAccount> {
40
+ const provider = await this.privyWallet.getEthereumProvider();
41
+
42
+ if (!provider) {
43
+ throw new Error("No Ethereum provider found in Privy wallet");
44
+ }
45
+
46
+ // Create a custom account from the provider
47
+ return {
48
+ address: this.privyWallet.address as `0x${string}`,
49
+ type: "json-rpc",
50
+ // @ts-ignore - Privy provider compatible with viem
51
+ source: "privateKey",
52
+ } as unknown as LocalAccount;
53
+ }
54
+
55
+ async getWalletClient(): Promise<WalletClient> {
56
+ if (this.cachedWalletClient) {
57
+ return this.cachedWalletClient;
58
+ }
59
+
60
+ const provider = await this.privyWallet.getEthereumProvider();
61
+
62
+ if (!provider) {
63
+ throw new Error("No Ethereum provider found in Privy wallet");
64
+ }
65
+
66
+ // Create wallet client with Privy's provider
67
+ this.cachedWalletClient = createWalletClient({
68
+ account: await this.getAccount(),
69
+ chain: this.chain,
70
+ transport: custom(provider),
71
+ });
72
+
73
+ return this.cachedWalletClient;
74
+ }
75
+
76
+ async getAddress(): Promise<`0x${string}`> {
77
+ if (this.cachedAddress) {
78
+ return this.cachedAddress;
79
+ }
80
+
81
+ this.cachedAddress = this.privyWallet.address as `0x${string}`;
82
+ return this.cachedAddress;
83
+ }
84
+
85
+ async isReady(): Promise<boolean> {
86
+ try {
87
+ const provider = await this.privyWallet.getEthereumProvider();
88
+ return !!provider && !!this.privyWallet.address;
89
+ } catch {
90
+ return false;
91
+ }
92
+ }
93
+ }
@@ -0,0 +1,45 @@
1
+ // src/signers/types.ts
2
+ import { Account, LocalAccount, WalletClient } from "viem";
3
+
4
+ /**
5
+ * Common interface for all signers
6
+ * This allows users to plug in different signing methods
7
+ */
8
+ export interface ISigner {
9
+ /**
10
+ * Get the viem Account object for signing
11
+ */
12
+ getAccount(): LocalAccount | Promise<LocalAccount>;
13
+
14
+ /**
15
+ * Get the wallet client for transactions
16
+ */
17
+ getWalletClient(): WalletClient | Promise<WalletClient>;
18
+
19
+ /**
20
+ * Get the signer's address
21
+ */
22
+ getAddress(): `0x${string}` | Promise<`0x${string}`>;
23
+
24
+ /**
25
+ * Check if signer is ready/connected
26
+ */
27
+ isReady(): boolean | Promise<boolean>;
28
+ }
29
+
30
+ /**
31
+ * Configuration for different signer types
32
+ */
33
+ export type SignerConfig =
34
+ | {
35
+ type: "privateKey";
36
+ privateKey: `0x${string}`;
37
+ }
38
+ | {
39
+ type: "privy";
40
+ // Privy handles its own state, no config needed
41
+ }
42
+ | {
43
+ type: "custom";
44
+ signer: ISigner;
45
+ };