@charterlabs/rhinestone-sdk 0.0.1

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 (68) hide show
  1. package/README.md +208 -0
  2. package/dist/accounts/index.d.ts +27 -0
  3. package/dist/accounts/index.d.ts.map +1 -0
  4. package/dist/accounts/index.js +289 -0
  5. package/dist/accounts/nexus.d.ts +20 -0
  6. package/dist/accounts/nexus.d.ts.map +1 -0
  7. package/dist/accounts/nexus.js +236 -0
  8. package/dist/accounts/safe.d.ts +17 -0
  9. package/dist/accounts/safe.d.ts.map +1 -0
  10. package/dist/accounts/safe.js +187 -0
  11. package/dist/accounts/utils.d.ts +27 -0
  12. package/dist/accounts/utils.d.ts.map +1 -0
  13. package/dist/accounts/utils.js +181 -0
  14. package/dist/execution/index.d.ts +33 -0
  15. package/dist/execution/index.d.ts.map +1 -0
  16. package/dist/execution/index.js +258 -0
  17. package/dist/execution/smart-session.d.ts +13 -0
  18. package/dist/execution/smart-session.d.ts.map +1 -0
  19. package/dist/execution/smart-session.js +85 -0
  20. package/dist/index.d.ts +34 -0
  21. package/dist/index.d.ts.map +1 -0
  22. package/dist/index.js +67 -0
  23. package/dist/modules/abi/smart-sessions.d.ts +42 -0
  24. package/dist/modules/abi/smart-sessions.d.ts.map +1 -0
  25. package/dist/modules/abi/smart-sessions.js +131 -0
  26. package/dist/modules/common.d.ts +16 -0
  27. package/dist/modules/common.d.ts.map +1 -0
  28. package/dist/modules/common.js +11 -0
  29. package/dist/modules/index.d.ts +33 -0
  30. package/dist/modules/index.d.ts.map +1 -0
  31. package/dist/modules/index.js +153 -0
  32. package/dist/modules/omni-account.d.ts +9 -0
  33. package/dist/modules/omni-account.d.ts.map +1 -0
  34. package/dist/modules/omni-account.js +15 -0
  35. package/dist/modules/validators/core.d.ts +23 -0
  36. package/dist/modules/validators/core.d.ts.map +1 -0
  37. package/dist/modules/validators/core.js +118 -0
  38. package/dist/modules/validators/index.d.ts +4 -0
  39. package/dist/modules/validators/index.d.ts.map +1 -0
  40. package/dist/modules/validators/index.js +17 -0
  41. package/dist/modules/validators/smart-sessions.d.ts +29 -0
  42. package/dist/modules/validators/smart-sessions.d.ts.map +1 -0
  43. package/dist/modules/validators/smart-sessions.js +426 -0
  44. package/dist/orchestrator/client.d.ts +29 -0
  45. package/dist/orchestrator/client.d.ts.map +1 -0
  46. package/dist/orchestrator/client.js +250 -0
  47. package/dist/orchestrator/consts.d.ts +5 -0
  48. package/dist/orchestrator/consts.d.ts.map +1 -0
  49. package/dist/orchestrator/consts.js +9 -0
  50. package/dist/orchestrator/error.d.ts +18 -0
  51. package/dist/orchestrator/error.d.ts.map +1 -0
  52. package/dist/orchestrator/error.js +33 -0
  53. package/dist/orchestrator/index.d.ts +11 -0
  54. package/dist/orchestrator/index.d.ts.map +1 -0
  55. package/dist/orchestrator/index.js +40 -0
  56. package/dist/orchestrator/registry.d.ts +17 -0
  57. package/dist/orchestrator/registry.d.ts.map +1 -0
  58. package/dist/orchestrator/registry.js +358 -0
  59. package/dist/orchestrator/types.d.ts +221 -0
  60. package/dist/orchestrator/types.d.ts.map +1 -0
  61. package/dist/orchestrator/types.js +19 -0
  62. package/dist/orchestrator/utils.d.ts +29 -0
  63. package/dist/orchestrator/utils.d.ts.map +1 -0
  64. package/dist/orchestrator/utils.js +316 -0
  65. package/dist/types.d.ts +113 -0
  66. package/dist/types.d.ts.map +1 -0
  67. package/dist/types.js +2 -0
  68. package/package.json +54 -0
@@ -0,0 +1,236 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDeployArgs = getDeployArgs;
4
+ exports.getSmartAccount = getSmartAccount;
5
+ exports.getSessionSmartAccount = getSessionSmartAccount;
6
+ exports.get7702SmartAccount = get7702SmartAccount;
7
+ exports.get7702InitCalls = get7702InitCalls;
8
+ const viem_1 = require("viem");
9
+ const account_abstraction_1 = require("viem/account-abstraction");
10
+ const modules_1 = require("../modules");
11
+ const validators_1 = require("../modules/validators");
12
+ const utils_1 = require("./utils");
13
+ const NEXUS_IMPLEMENTATION_ADDRESS = '0x000000004f43c49e93c970e84001853a70923b03';
14
+ const NEXUS_FACTORY_ADDRESS = '0x000000001D1D5004a02bAfAb9de2D6CE5b7B13de';
15
+ const NEXUS_BOOTSTRAP_ADDRESS = '0x00000000D3254452a909E4eeD47455Af7E27C289';
16
+ const K1_MEE_VALIDATOR_ADDRESS = '0x00000000d12897ddadc2044614a9677b191a2d95';
17
+ function getDeployArgs(config) {
18
+ const salt = (0, viem_1.keccak256)('0x');
19
+ const moduleSetup = (0, modules_1.getSetup)(config);
20
+ const initData = (0, viem_1.encodeAbiParameters)([{ type: 'address' }, { type: 'bytes' }], [
21
+ NEXUS_BOOTSTRAP_ADDRESS,
22
+ (0, viem_1.encodeFunctionData)({
23
+ abi: (0, viem_1.parseAbi)([
24
+ 'struct BootstrapConfig {address module;bytes initData;}',
25
+ 'struct BootstrapPreValidationHookConfig {uint256 hookType;address module;bytes data;}',
26
+ 'struct RegistryConfig {address registry;address[] attesters;uint8 threshold;}',
27
+ 'function initNexus(BootstrapConfig[] calldata validators,BootstrapConfig[] calldata executors,BootstrapConfig calldata hook,BootstrapConfig[] calldata fallbacks,BootstrapPreValidationHookConfig[] calldata preValidationHooks,RegistryConfig registryConfig) external',
28
+ ]),
29
+ functionName: 'initNexus',
30
+ args: [
31
+ moduleSetup.validators.map((v) => ({
32
+ module: v.address,
33
+ initData: v.initData,
34
+ })),
35
+ moduleSetup.executors.map((e) => ({
36
+ module: e.address,
37
+ initData: e.initData,
38
+ })),
39
+ {
40
+ module: viem_1.zeroAddress,
41
+ initData: '0x',
42
+ },
43
+ moduleSetup.fallbacks.map((f) => ({
44
+ module: f.address,
45
+ initData: f.initData,
46
+ })),
47
+ [],
48
+ {
49
+ registry: moduleSetup.registry,
50
+ attesters: moduleSetup.attesters,
51
+ threshold: moduleSetup.threshold,
52
+ },
53
+ ],
54
+ }),
55
+ ]);
56
+ const factoryData = (0, viem_1.encodeFunctionData)({
57
+ abi: (0, viem_1.parseAbi)(['function createAccount(bytes,bytes32)']),
58
+ functionName: 'createAccount',
59
+ args: [initData, salt],
60
+ });
61
+ const creationCode = '0x60806040526102aa803803806100148161018c565b92833981016040828203126101885781516001600160a01b03811692909190838303610188576020810151906001600160401b03821161018857019281601f8501121561018857835161006e610069826101c5565b61018c565b9481865260208601936020838301011161018857815f926020809301865e8601015260017f90b772c2cb8a51aa7a8a65fc23543c6d022d5b3f8e2b92eed79fba7eef8293005d823b15610176577f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc80546001600160a01b031916821790557fbc7cd75a20ee27fd9adebab32041f755214dbc6bffa90cc0225b39da2e5c2d3b5f80a282511561015e575f8091610146945190845af43d15610156573d91610137610069846101c5565b9283523d5f602085013e6101e0565b505b604051606b908161023f8239f35b6060916101e0565b50505034156101485763b398979f60e01b5f5260045ffd5b634c9c8ce360e01b5f5260045260245ffd5b5f80fd5b6040519190601f01601f191682016001600160401b038111838210176101b157604052565b634e487b7160e01b5f52604160045260245ffd5b6001600160401b0381116101b157601f01601f191660200190565b9061020457508051156101f557805190602001fd5b63d6bda27560e01b5f5260045ffd5b81511580610235575b610215575090565b639996b31560e01b5f9081526001600160a01b0391909116600452602490fd5b50803b1561020d56fe60806040523615605c575f8073ffffffffffffffffffffffffffffffffffffffff7f360894a13ba1a3210667c828492db98dca3e2076cc3735a920a3ca505d382bbc5416368280378136915af43d5f803e156058573d5ff35b3d5ffd5b00fea164736f6c634300081b000a';
62
+ const initializationCallData = (0, viem_1.encodeFunctionData)({
63
+ abi: (0, viem_1.parseAbi)(['function initializeAccount(bytes)']),
64
+ functionName: 'initializeAccount',
65
+ args: [initData],
66
+ });
67
+ const accountInitData = (0, viem_1.encodeAbiParameters)([
68
+ {
69
+ name: 'address',
70
+ type: 'address',
71
+ },
72
+ {
73
+ name: 'calldata',
74
+ type: 'bytes',
75
+ },
76
+ ], [NEXUS_IMPLEMENTATION_ADDRESS, initializationCallData]);
77
+ const hashedInitcode = (0, viem_1.keccak256)((0, viem_1.concat)([creationCode, accountInitData]));
78
+ return {
79
+ factory: NEXUS_FACTORY_ADDRESS,
80
+ factoryData,
81
+ salt,
82
+ hashedInitcode,
83
+ implementation: NEXUS_IMPLEMENTATION_ADDRESS,
84
+ initializationCallData,
85
+ };
86
+ }
87
+ async function getSmartAccount(client, address, owners, validatorAddress, sign) {
88
+ return getBaseSmartAccount(address, client, validatorAddress, async () => {
89
+ return (0, validators_1.getMockSignature)(owners);
90
+ }, sign);
91
+ }
92
+ async function getSessionSmartAccount(client, address, session, validatorAddress, sign) {
93
+ return await getBaseSmartAccount(address, client, validatorAddress, async () => {
94
+ const dummyOpSignature = (0, validators_1.getMockSignature)(session.owners);
95
+ return (0, validators_1.encodeSmartSessionSignature)(validators_1.SMART_SESSION_MODE_USE, (0, validators_1.getPermissionId)(session), dummyOpSignature);
96
+ }, async (hash) => {
97
+ const signature = await sign(hash);
98
+ return (0, validators_1.encodeSmartSessionSignature)(validators_1.SMART_SESSION_MODE_USE, (0, validators_1.getPermissionId)(session), signature);
99
+ });
100
+ }
101
+ async function get7702SmartAccount(account, client) {
102
+ return await getBaseSmartAccount(account.address, client, viem_1.zeroAddress, async () => {
103
+ const dynamicPart = K1_MEE_VALIDATOR_ADDRESS.substring(2).padEnd(40, '0');
104
+ return `0x0000000000000000000000000000000000000000000000000000000000000040000000000000000000000000${dynamicPart}000000000000000000000000000000000000000000000000000000000000004181d4b4981670cb18f99f0b4a66446df1bf5b204d24cfcb659bf38ba27a4359b5711649ec2423c5e1247245eba2964679b6a1dbb85c992ae40b9b00c6935b02ff1b00000000000000000000000000000000000000000000000000000000000000`;
105
+ }, async (hash) => {
106
+ if (!account.signMessage) {
107
+ throw new Error('Sign message not supported by account');
108
+ }
109
+ return await account.signMessage({
110
+ message: { raw: hash },
111
+ });
112
+ });
113
+ }
114
+ async function getBaseSmartAccount(address, client, nonceValidatorAddress, getStubSignature, signUserOperation) {
115
+ return await (0, account_abstraction_1.toSmartAccount)({
116
+ client,
117
+ entryPoint: {
118
+ abi: account_abstraction_1.entryPoint07Abi,
119
+ address: account_abstraction_1.entryPoint07Address,
120
+ version: '0.7',
121
+ },
122
+ async decodeCalls() {
123
+ throw new Error('Not implemented');
124
+ },
125
+ async encodeCalls(calls) {
126
+ return (0, utils_1.encode7579Calls)({
127
+ mode: {
128
+ type: calls.length > 1 ? 'batchcall' : 'call',
129
+ revertOnError: false,
130
+ selector: '0x',
131
+ context: '0x',
132
+ },
133
+ callData: calls,
134
+ });
135
+ },
136
+ async getAddress() {
137
+ return address;
138
+ },
139
+ async getFactoryArgs() {
140
+ return {};
141
+ },
142
+ async getNonce(args) {
143
+ const TIMESTAMP_ADJUSTMENT = 16777215n; // max value for size 3
144
+ const defaultedKey = (args?.key ?? 0n) % TIMESTAMP_ADJUSTMENT;
145
+ const defaultedValidationMode = '0x00';
146
+ const key = (0, viem_1.concat)([
147
+ (0, viem_1.toHex)(defaultedKey, { size: 3 }),
148
+ defaultedValidationMode,
149
+ nonceValidatorAddress,
150
+ ]);
151
+ return (0, utils_1.getAccountNonce)(client, {
152
+ address,
153
+ entryPointAddress: account_abstraction_1.entryPoint07Address,
154
+ key: BigInt(key),
155
+ });
156
+ },
157
+ async getStubSignature() {
158
+ return getStubSignature();
159
+ },
160
+ async signMessage() {
161
+ throw new Error('Not implemented');
162
+ },
163
+ async signTypedData() {
164
+ throw new Error('Not implemented');
165
+ },
166
+ async signUserOperation(parameters) {
167
+ const { chainId = client.chain?.id, ...userOperation } = parameters;
168
+ if (!chainId)
169
+ throw new Error('Chain id not found');
170
+ const hash = (0, account_abstraction_1.getUserOperationHash)({
171
+ userOperation: {
172
+ ...userOperation,
173
+ sender: userOperation.sender ?? (await this.getAddress()),
174
+ signature: '0x',
175
+ },
176
+ entryPointAddress: account_abstraction_1.entryPoint07Address,
177
+ entryPointVersion: '0.7',
178
+ chainId: chainId,
179
+ });
180
+ return await signUserOperation(hash);
181
+ },
182
+ });
183
+ }
184
+ function get7702InitCalls(config) {
185
+ const eoa = config.eoa;
186
+ if (!eoa) {
187
+ throw new Error('EIP-7702 accounts must have an EOA account');
188
+ }
189
+ const moduleSetup = (0, modules_1.getSetup)(config);
190
+ return [
191
+ {
192
+ to: eoa.address,
193
+ data: (0, viem_1.encodeFunctionData)({
194
+ abi: (0, viem_1.parseAbi)([
195
+ 'function setRegistry(address newRegistry, address[] calldata attesters, uint8 threshold)',
196
+ ]),
197
+ functionName: 'setRegistry',
198
+ args: [
199
+ moduleSetup.registry,
200
+ moduleSetup.attesters,
201
+ moduleSetup.threshold,
202
+ ],
203
+ }),
204
+ },
205
+ ...moduleSetup.validators.map((validator) => ({
206
+ to: eoa.address,
207
+ data: (0, viem_1.encodeFunctionData)({
208
+ abi: (0, viem_1.parseAbi)([
209
+ 'function installModule(uint256 moduleTypeId, address module, bytes calldata initData)',
210
+ ]),
211
+ functionName: 'installModule',
212
+ args: [validator.type, validator.address, validator.initData],
213
+ }),
214
+ })),
215
+ ...moduleSetup.executors.map((executor) => ({
216
+ to: eoa.address,
217
+ data: (0, viem_1.encodeFunctionData)({
218
+ abi: (0, viem_1.parseAbi)([
219
+ 'function installModule(uint256 moduleTypeId, address module, bytes calldata initData)',
220
+ ]),
221
+ functionName: 'installModule',
222
+ args: [executor.type, executor.address, executor.initData],
223
+ }),
224
+ })),
225
+ ...moduleSetup.fallbacks.map((fallback) => ({
226
+ to: eoa.address,
227
+ data: (0, viem_1.encodeFunctionData)({
228
+ abi: (0, viem_1.parseAbi)([
229
+ 'function installModule(uint256 moduleTypeId, address module, bytes calldata initData)',
230
+ ]),
231
+ functionName: 'installModule',
232
+ args: [fallback.type, fallback.address, fallback.initData],
233
+ }),
234
+ })),
235
+ ];
236
+ }
@@ -0,0 +1,17 @@
1
+ import { type Abi, type Address, type Hex, type PublicClient } from 'viem';
2
+ import { type SmartAccount, type SmartAccountImplementation } from 'viem/account-abstraction';
3
+ import type { OwnerSet, RhinestoneAccountConfig, Session } from '../types';
4
+ declare function getDeployArgs(config: RhinestoneAccountConfig): {
5
+ factory: `0x${string}`;
6
+ factoryData: `0x${string}`;
7
+ salt: `0x${string}`;
8
+ hashedInitcode: `0x${string}`;
9
+ implementation: `0x${string}`;
10
+ initializationCallData: null;
11
+ };
12
+ declare function getSmartAccount(client: PublicClient, address: Address, owners: OwnerSet, validatorAddress: Address, sign: (hash: Hex) => Promise<Hex>): Promise<SmartAccount<SmartAccountImplementation<Abi, "0.7">>>;
13
+ declare function getSessionSmartAccount(client: PublicClient, address: Address, session: Session, validatorAddress: Address, sign: (hash: Hex) => Promise<Hex>): Promise<SmartAccount<SmartAccountImplementation<Abi, "0.7">>>;
14
+ declare function get7702SmartAccount(): never;
15
+ declare function get7702InitCalls(): never;
16
+ export { getDeployArgs, getSmartAccount, getSessionSmartAccount, get7702InitCalls, get7702SmartAccount, };
17
+ //# sourceMappingURL=safe.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"safe.d.ts","sourceRoot":"","sources":["../../accounts/safe.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,KAAK,GAAG,EACR,KAAK,OAAO,EAIZ,KAAK,GAAG,EAER,KAAK,YAAY,EAGlB,MAAM,MAAM,CAAA;AACb,OAAO,EAIL,KAAK,YAAY,EACjB,KAAK,0BAA0B,EAEhC,MAAM,0BAA0B,CAAA;AASjC,OAAO,KAAK,EAAE,QAAQ,EAAE,uBAAuB,EAAE,OAAO,EAAE,MAAM,UAAU,CAAA;AAgB1E,iBAAS,aAAa,CAAC,MAAM,EAAE,uBAAuB;;;;;;;EA0ErD;AAED,iBAAe,eAAe,CAC5B,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,OAAO,EAChB,MAAM,EAAE,QAAQ,EAChB,gBAAgB,EAAE,OAAO,EACzB,IAAI,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,iEAWlC;AAED,iBAAe,sBAAsB,CACnC,MAAM,EAAE,YAAY,EACpB,OAAO,EAAE,OAAO,EAChB,OAAO,EAAE,OAAO,EAChB,gBAAgB,EAAE,OAAO,EACzB,IAAI,EAAE,CAAC,IAAI,EAAE,GAAG,KAAK,OAAO,CAAC,GAAG,CAAC,iEAuBlC;AA0ED,iBAAS,mBAAmB,IAAI,KAAK,CAEpC;AAED,iBAAS,gBAAgB,IAAI,KAAK,CAEjC;AAsBD,OAAO,EACL,aAAa,EACb,eAAe,EACf,sBAAsB,EACtB,gBAAgB,EAChB,mBAAmB,GACpB,CAAA"}
@@ -0,0 +1,187 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getDeployArgs = getDeployArgs;
4
+ exports.getSmartAccount = getSmartAccount;
5
+ exports.getSessionSmartAccount = getSessionSmartAccount;
6
+ exports.get7702InitCalls = get7702InitCalls;
7
+ exports.get7702SmartAccount = get7702SmartAccount;
8
+ const viem_1 = require("viem");
9
+ const account_abstraction_1 = require("viem/account-abstraction");
10
+ const modules_1 = require("../modules");
11
+ const validators_1 = require("../modules/validators");
12
+ const utils_1 = require("./utils");
13
+ const SAFE_7579_LAUNCHPAD_ADDRESS = '0x7579011aB74c46090561ea277Ba79D510c6C00ff';
14
+ const SAFE_7579_ADAPTER_ADDRESS = '0x7579ee8307284f293b1927136486880611f20002';
15
+ const SAFE_SINGLETON_ADDRESS = '0x29fcb43b46531bca003ddc8fcb67ffe91900c762';
16
+ const SAFE_PROXY_FACTORY_ADDRESS = '0x4e1dcf7ad4e460cfd30791ccc4f9c8a4f820ec67';
17
+ const NO_SAFE_OWNER_ADDRESS = '0xbabe99e62d8bcbd3acf5ccbcfcd4f64fe75e5e72';
18
+ function getDeployArgs(config) {
19
+ {
20
+ const owners = getOwners(config);
21
+ const threshold = getThreshold(config);
22
+ const moduleSetup = (0, modules_1.getSetup)(config);
23
+ const initData = (0, viem_1.encodeFunctionData)({
24
+ abi: (0, viem_1.parseAbi)([
25
+ 'function setup(address[] calldata _owners,uint256 _threshold,address to,bytes calldata data,address fallbackHandler,address paymentToken,uint256 payment, address paymentReceiver) external',
26
+ ]),
27
+ functionName: 'setup',
28
+ args: [
29
+ owners,
30
+ threshold,
31
+ SAFE_7579_LAUNCHPAD_ADDRESS,
32
+ (0, viem_1.encodeFunctionData)({
33
+ abi: (0, viem_1.parseAbi)([
34
+ 'struct ModuleInit {address module;bytes initData;}',
35
+ 'function addSafe7579(address safe7579,ModuleInit[] calldata validators,ModuleInit[] calldata executors,ModuleInit[] calldata fallbacks, ModuleInit[] calldata hooks,address[] calldata attesters,uint8 threshold) external',
36
+ ]),
37
+ functionName: 'addSafe7579',
38
+ args: [
39
+ SAFE_7579_ADAPTER_ADDRESS,
40
+ moduleSetup.validators.map((v) => ({
41
+ module: v.address,
42
+ initData: v.initData,
43
+ })),
44
+ moduleSetup.executors.map((e) => ({
45
+ module: e.address,
46
+ initData: e.initData,
47
+ })),
48
+ moduleSetup.fallbacks.map((f) => ({
49
+ module: f.address,
50
+ initData: f.initData,
51
+ })),
52
+ moduleSetup.hooks.map((h) => ({
53
+ module: h.address,
54
+ initData: h.initData,
55
+ })),
56
+ moduleSetup.attesters,
57
+ moduleSetup.threshold,
58
+ ],
59
+ }),
60
+ SAFE_7579_ADAPTER_ADDRESS,
61
+ viem_1.zeroAddress,
62
+ BigInt(0),
63
+ viem_1.zeroAddress,
64
+ ],
65
+ });
66
+ const saltNonce = 0n;
67
+ const factoryData = (0, viem_1.encodeFunctionData)({
68
+ abi: (0, viem_1.parseAbi)([
69
+ 'function createProxyWithNonce(address singleton,bytes calldata initializer,uint256 saltNonce) external payable returns (address)',
70
+ ]),
71
+ functionName: 'createProxyWithNonce',
72
+ args: [SAFE_SINGLETON_ADDRESS, initData, saltNonce],
73
+ });
74
+ const salt = (0, viem_1.keccak256)((0, viem_1.encodePacked)(['bytes32', 'uint256'], [(0, viem_1.keccak256)(initData), saltNonce]));
75
+ const hashedInitcode = '0xe298282cefe913ab5d282047161268a8222e4bd4ed106300c547894bbefd31ee';
76
+ return {
77
+ factory: SAFE_PROXY_FACTORY_ADDRESS,
78
+ factoryData,
79
+ salt,
80
+ hashedInitcode,
81
+ implementation: SAFE_SINGLETON_ADDRESS,
82
+ initializationCallData: null,
83
+ };
84
+ }
85
+ }
86
+ async function getSmartAccount(client, address, owners, validatorAddress, sign) {
87
+ return getBaseSmartAccount(address, client, validatorAddress, async () => {
88
+ return (0, validators_1.getMockSignature)(owners);
89
+ }, sign);
90
+ }
91
+ async function getSessionSmartAccount(client, address, session, validatorAddress, sign) {
92
+ return await getBaseSmartAccount(address, client, validatorAddress, async () => {
93
+ const dummyOpSignature = (0, validators_1.getMockSignature)(session.owners);
94
+ return (0, validators_1.encodeSmartSessionSignature)(validators_1.SMART_SESSION_MODE_USE, (0, validators_1.getPermissionId)(session), dummyOpSignature);
95
+ }, async (hash) => {
96
+ const signature = await sign(hash);
97
+ return (0, validators_1.encodeSmartSessionSignature)(validators_1.SMART_SESSION_MODE_USE, (0, validators_1.getPermissionId)(session), signature);
98
+ });
99
+ }
100
+ async function getBaseSmartAccount(address, client, validatorAddress, getStubSignature, signUserOperation) {
101
+ return await (0, account_abstraction_1.toSmartAccount)({
102
+ client,
103
+ entryPoint: {
104
+ abi: account_abstraction_1.entryPoint07Abi,
105
+ address: account_abstraction_1.entryPoint07Address,
106
+ version: '0.7',
107
+ },
108
+ async decodeCalls() {
109
+ throw new Error('Not implemented');
110
+ },
111
+ async encodeCalls(calls) {
112
+ return (0, utils_1.encode7579Calls)({
113
+ mode: {
114
+ type: calls.length > 1 ? 'batchcall' : 'call',
115
+ revertOnError: false,
116
+ selector: '0x',
117
+ context: '0x',
118
+ },
119
+ callData: calls,
120
+ });
121
+ },
122
+ async getAddress() {
123
+ return address;
124
+ },
125
+ async getFactoryArgs() {
126
+ return {};
127
+ },
128
+ async getNonce() {
129
+ const key = (0, viem_1.concat)([validatorAddress, '0x00000000']);
130
+ const nonce = await (0, utils_1.getAccountNonce)(client, {
131
+ address,
132
+ entryPointAddress: account_abstraction_1.entryPoint07Address,
133
+ key: BigInt(key),
134
+ });
135
+ return nonce;
136
+ },
137
+ async getStubSignature() {
138
+ return getStubSignature();
139
+ },
140
+ async signMessage() {
141
+ throw new Error('Not implemented');
142
+ },
143
+ async signTypedData() {
144
+ throw new Error('Not implemented');
145
+ },
146
+ async signUserOperation(parameters) {
147
+ const { chainId = client.chain?.id, ...userOperation } = parameters;
148
+ if (!chainId)
149
+ throw new Error('Chain id not found');
150
+ const hash = (0, account_abstraction_1.getUserOperationHash)({
151
+ userOperation: {
152
+ ...userOperation,
153
+ sender: userOperation.sender ?? (await this.getAddress()),
154
+ signature: '0x',
155
+ },
156
+ entryPointAddress: account_abstraction_1.entryPoint07Address,
157
+ entryPointVersion: '0.7',
158
+ chainId: chainId,
159
+ });
160
+ return await signUserOperation(hash);
161
+ },
162
+ });
163
+ }
164
+ function get7702SmartAccount() {
165
+ throw new Error('EIP-7702 is not supported for Safe accounts');
166
+ }
167
+ function get7702InitCalls() {
168
+ throw new Error('EIP-7702 is not supported for Safe accounts');
169
+ }
170
+ function getOwners(config) {
171
+ const ownerSet = config.owners;
172
+ switch (ownerSet.type) {
173
+ case 'ecdsa':
174
+ return ownerSet.accounts.map((account) => account.address);
175
+ case 'passkey':
176
+ return [NO_SAFE_OWNER_ADDRESS];
177
+ }
178
+ }
179
+ function getThreshold(config) {
180
+ const ownerSet = config.owners;
181
+ switch (ownerSet.type) {
182
+ case 'ecdsa':
183
+ return ownerSet.threshold ? BigInt(ownerSet.threshold) : 1n;
184
+ case 'passkey':
185
+ return 1n;
186
+ }
187
+ }
@@ -0,0 +1,27 @@
1
+ import type { Address, Client, Hex } from 'viem';
2
+ import type { RhinestoneAccountConfig } from '../types';
3
+ type CallType = 'call' | 'delegatecall' | 'batchcall';
4
+ interface ExecutionMode<callType extends CallType> {
5
+ type: callType;
6
+ revertOnError?: boolean;
7
+ selector?: Hex;
8
+ context?: Hex;
9
+ }
10
+ interface EncodeCallDataParams<callType extends CallType> {
11
+ mode: ExecutionMode<callType>;
12
+ callData: readonly {
13
+ to: Address;
14
+ value?: bigint | undefined;
15
+ data?: Hex | undefined;
16
+ }[];
17
+ }
18
+ interface GetAccountNonceParams {
19
+ address: Address;
20
+ entryPointAddress: Address;
21
+ key?: bigint;
22
+ }
23
+ declare function encode7579Calls<callType extends CallType>({ mode, callData, }: EncodeCallDataParams<callType>): Hex;
24
+ declare function getAccountNonce(client: Client, args: GetAccountNonceParams): Promise<bigint>;
25
+ declare function getBundlerClient(config: RhinestoneAccountConfig, client: Client): import("viem/account-abstraction").BundlerClient<import("viem").HttpTransport<undefined, false>, undefined, undefined, Client, undefined>;
26
+ export { encode7579Calls, getAccountNonce, getBundlerClient };
27
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../accounts/utils.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,MAAM,CAAA;AAchD,OAAO,KAAK,EAAiB,uBAAuB,EAAE,MAAM,UAAU,CAAA;AAEtE,KAAK,QAAQ,GAAG,MAAM,GAAG,cAAc,GAAG,WAAW,CAAA;AAErD,UAAU,aAAa,CAAC,QAAQ,SAAS,QAAQ;IAC/C,IAAI,EAAE,QAAQ,CAAA;IACd,aAAa,CAAC,EAAE,OAAO,CAAA;IACvB,QAAQ,CAAC,EAAE,GAAG,CAAA;IACd,OAAO,CAAC,EAAE,GAAG,CAAA;CACd;AAED,UAAU,oBAAoB,CAAC,QAAQ,SAAS,QAAQ;IACtD,IAAI,EAAE,aAAa,CAAC,QAAQ,CAAC,CAAA;IAC7B,QAAQ,EAAE,SAAS;QACjB,EAAE,EAAE,OAAO,CAAA;QACX,KAAK,CAAC,EAAE,MAAM,GAAG,SAAS,CAAA;QAC1B,IAAI,CAAC,EAAE,GAAG,GAAG,SAAS,CAAA;KACvB,EAAE,CAAA;CACJ;AAED,UAAU,qBAAqB;IAC7B,OAAO,EAAE,OAAO,CAAA;IAChB,iBAAiB,EAAE,OAAO,CAAA;IAC1B,GAAG,CAAC,EAAE,MAAM,CAAA;CACb;AAkDD,iBAAS,eAAe,CAAC,QAAQ,SAAS,QAAQ,EAAE,EAClD,IAAI,EACJ,QAAQ,GACT,EAAE,oBAAoB,CAAC,QAAQ,CAAC,GAAG,GAAG,CAuFtC;AAED,iBAAe,eAAe,CAC5B,MAAM,EAAE,MAAM,EACd,IAAI,EAAE,qBAAqB,GAC1B,OAAO,CAAC,MAAM,CAAC,CAmCjB;AAED,iBAAS,gBAAgB,CAAC,MAAM,EAAE,uBAAuB,EAAE,MAAM,EAAE,MAAM,6IAyBxE;AAyBD,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,gBAAgB,EAAE,CAAA"}
@@ -0,0 +1,181 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.encode7579Calls = encode7579Calls;
4
+ exports.getAccountNonce = getAccountNonce;
5
+ exports.getBundlerClient = getBundlerClient;
6
+ const viem_1 = require("viem");
7
+ const account_abstraction_1 = require("viem/account-abstraction");
8
+ const actions_1 = require("viem/actions");
9
+ const utils_1 = require("viem/utils");
10
+ function parseCallType(callType) {
11
+ switch (callType) {
12
+ case 'call':
13
+ return '0x00';
14
+ case 'batchcall':
15
+ return '0x01';
16
+ case 'delegatecall':
17
+ return '0xff';
18
+ }
19
+ }
20
+ function encodeExecutionMode({ type, revertOnError, selector, context, }) {
21
+ return (0, viem_1.encodePacked)(['bytes1', 'bytes1', 'bytes4', 'bytes4', 'bytes22'], [
22
+ (0, viem_1.toHex)((0, viem_1.toBytes)(parseCallType(type), { size: 1 })),
23
+ (0, viem_1.toHex)((0, viem_1.toBytes)(revertOnError ? '0x01' : '0x00', { size: 1 })),
24
+ (0, viem_1.toHex)((0, viem_1.toBytes)('0x0', { size: 4 })),
25
+ (0, viem_1.toHex)((0, viem_1.toBytes)(selector ?? '0x', { size: 4 })),
26
+ (0, viem_1.toHex)((0, viem_1.toBytes)(context ?? '0x', { size: 22 })),
27
+ ]);
28
+ }
29
+ function encode7579Calls({ mode, callData, }) {
30
+ if (callData.length > 1 && mode?.type !== 'batchcall') {
31
+ throw new Error(`mode ${JSON.stringify(mode)} does not supported for batchcall calldata`);
32
+ }
33
+ const executeAbi = [
34
+ {
35
+ type: 'function',
36
+ name: 'execute',
37
+ inputs: [
38
+ {
39
+ name: 'execMode',
40
+ type: 'bytes32',
41
+ internalType: 'ExecMode',
42
+ },
43
+ {
44
+ name: 'executionCalldata',
45
+ type: 'bytes',
46
+ internalType: 'bytes',
47
+ },
48
+ ],
49
+ outputs: [],
50
+ stateMutability: 'payable',
51
+ },
52
+ ];
53
+ if (callData.length > 1) {
54
+ return (0, viem_1.encodeFunctionData)({
55
+ abi: executeAbi,
56
+ functionName: 'execute',
57
+ args: [
58
+ encodeExecutionMode(mode),
59
+ (0, viem_1.encodeAbiParameters)([
60
+ {
61
+ name: 'executionBatch',
62
+ type: 'tuple[]',
63
+ components: [
64
+ {
65
+ name: 'target',
66
+ type: 'address',
67
+ },
68
+ {
69
+ name: 'value',
70
+ type: 'uint256',
71
+ },
72
+ {
73
+ name: 'callData',
74
+ type: 'bytes',
75
+ },
76
+ ],
77
+ },
78
+ ], [
79
+ callData.map((arg) => {
80
+ return {
81
+ target: arg.to,
82
+ value: arg.value ?? 0n,
83
+ callData: arg.data ?? '0x',
84
+ };
85
+ }),
86
+ ]),
87
+ ],
88
+ });
89
+ }
90
+ const call = callData.length === 0 ? undefined : callData[0];
91
+ if (!call) {
92
+ throw new Error('No calls to encode');
93
+ }
94
+ return (0, viem_1.encodeFunctionData)({
95
+ abi: executeAbi,
96
+ functionName: 'execute',
97
+ args: [
98
+ encodeExecutionMode(mode),
99
+ (0, viem_1.concatHex)([
100
+ call.to,
101
+ (0, viem_1.toHex)(call.value ?? 0n, { size: 32 }),
102
+ call.data ?? '0x',
103
+ ]),
104
+ ],
105
+ });
106
+ }
107
+ async function getAccountNonce(client, args) {
108
+ const { address, entryPointAddress, key = 0n } = args;
109
+ return await (0, utils_1.getAction)(client, actions_1.readContract, 'readContract')({
110
+ address: entryPointAddress,
111
+ abi: [
112
+ {
113
+ inputs: [
114
+ {
115
+ name: 'sender',
116
+ type: 'address',
117
+ },
118
+ {
119
+ name: 'key',
120
+ type: 'uint192',
121
+ },
122
+ ],
123
+ name: 'getNonce',
124
+ outputs: [
125
+ {
126
+ name: 'nonce',
127
+ type: 'uint256',
128
+ },
129
+ ],
130
+ stateMutability: 'view',
131
+ type: 'function',
132
+ },
133
+ ],
134
+ functionName: 'getNonce',
135
+ args: [address, key],
136
+ });
137
+ }
138
+ function getBundlerClient(config, client) {
139
+ function getBundlerEndpoint(config, chainId) {
140
+ switch (config.type) {
141
+ case 'pimlico':
142
+ return `https://api.pimlico.io/v2/${chainId}/rpc?apikey=${config.apiKey}`;
143
+ }
144
+ }
145
+ const { bundler } = config;
146
+ const chainId = client.chain?.id;
147
+ if (!chainId) {
148
+ throw new Error('Chain id is required');
149
+ }
150
+ const endpoint = bundler
151
+ ? getBundlerEndpoint(bundler, chainId)
152
+ : `https://public.pimlico.io/v2/${chainId}/rpc`;
153
+ return (0, account_abstraction_1.createBundlerClient)({
154
+ client,
155
+ transport: (0, viem_1.http)(endpoint),
156
+ paymaster: true,
157
+ userOperation: {
158
+ estimateFeesPerGas: () => getGasPriceEstimate(endpoint),
159
+ },
160
+ });
161
+ }
162
+ async function getGasPriceEstimate(bundlerUrl) {
163
+ const response = await fetch(bundlerUrl, {
164
+ method: 'POST',
165
+ headers: {
166
+ 'Content-Type': 'application/json',
167
+ },
168
+ body: JSON.stringify({
169
+ jsonrpc: '2.0',
170
+ id: Date.now(),
171
+ // TODO do not rely on vendor-specific methods
172
+ method: 'pimlico_getUserOperationGasPrice',
173
+ params: [],
174
+ }),
175
+ });
176
+ const json = (await response.json());
177
+ return {
178
+ maxFeePerGas: BigInt(json.result.fast.maxFeePerGas),
179
+ maxPriorityFeePerGas: BigInt(json.result.fast.maxPriorityFeePerGas),
180
+ };
181
+ }