@dynamic-labs-wallet/node-evm 0.0.353 → 0.1.0-request-signing
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.
- package/index.cjs +1237 -189
- package/index.esm.js +1214 -192
- package/package.json +9 -8
- package/src/client/accountAdapter.d.ts +4 -3
- package/src/client/accountAdapter.d.ts.map +1 -1
- package/src/client/client.d.ts +110 -31
- package/src/client/client.d.ts.map +1 -1
- package/src/client/earn.client.d.ts +35 -0
- package/src/client/earn.client.d.ts.map +1 -0
- package/src/client/earn.constants.d.ts +42 -0
- package/src/client/earn.constants.d.ts.map +1 -0
- package/src/client/earn.types.d.ts +33 -0
- package/src/client/earn.types.d.ts.map +1 -0
- package/src/client/gasless.client.d.ts +54 -0
- package/src/client/gasless.client.d.ts.map +1 -0
- package/src/client/gasless.constants.d.ts +63 -0
- package/src/client/gasless.constants.d.ts.map +1 -0
- package/src/client/gasless.types.d.ts +148 -0
- package/src/client/gasless.types.d.ts.map +1 -0
- package/src/client/index.d.ts +6 -0
- package/src/client/index.d.ts.map +1 -1
- package/src/delegatedClient.d.ts +66 -9
- package/src/delegatedClient.d.ts.map +1 -1
- package/src/delegatedGasless.d.ts +78 -0
- package/src/delegatedGasless.d.ts.map +1 -0
- package/src/index.d.ts +1 -0
- package/src/index.d.ts.map +1 -1
- package/src/utils.d.ts +3 -1
- package/src/utils.d.ts.map +1 -1
- package/src/zerodev/types.d.ts +4 -3
- package/src/zerodev/types.d.ts.map +1 -1
package/index.cjs
CHANGED
|
@@ -5,6 +5,7 @@ var node = require('@dynamic-labs-wallet/node');
|
|
|
5
5
|
var viem = require('viem');
|
|
6
6
|
var utils = require('viem/utils');
|
|
7
7
|
var chains = require('viem/chains');
|
|
8
|
+
var node_crypto = require('node:crypto');
|
|
8
9
|
var client = require('@dynamic-labs-sdk/client');
|
|
9
10
|
var core = require('@dynamic-labs-sdk/client/core');
|
|
10
11
|
var viem$1 = require('@dynamic-labs-sdk/evm/viem');
|
|
@@ -35,6 +36,20 @@ const ERROR_ACCOUNT_ADDRESS_REQUIRED = 'Account address is required';
|
|
|
35
36
|
const ERROR_VERIFY_MESSAGE_SIGNATURE = 'Error verifying message signature';
|
|
36
37
|
const ERROR_SIGN_RAW_MESSAGE = 'Error signing raw message';
|
|
37
38
|
|
|
39
|
+
const mapTransactionToEvmTransaction = (transaction)=>{
|
|
40
|
+
var _transaction_to, _transaction_chainId;
|
|
41
|
+
return {
|
|
42
|
+
to: (_transaction_to = transaction.to) != null ? _transaction_to : '',
|
|
43
|
+
data: transaction.data,
|
|
44
|
+
gas: transaction.gas ? `0x${transaction.gas.toString(16)}` : undefined,
|
|
45
|
+
gasPrice: transaction.gasPrice ? `0x${transaction.gasPrice.toString(16)}` : undefined,
|
|
46
|
+
maxFeePerGas: transaction.maxFeePerGas ? `0x${transaction.maxFeePerGas.toString(16)}` : undefined,
|
|
47
|
+
maxPriorityFeePerGas: transaction.maxPriorityFeePerGas ? `0x${transaction.maxPriorityFeePerGas.toString(16)}` : undefined,
|
|
48
|
+
nonce: transaction.nonce,
|
|
49
|
+
value: transaction.value ? `0x${transaction.value.toString(16)}` : undefined,
|
|
50
|
+
chainId: (_transaction_chainId = transaction.chainId) != null ? _transaction_chainId : 0
|
|
51
|
+
};
|
|
52
|
+
};
|
|
38
53
|
const formatEVMMessage = (message_)=>{
|
|
39
54
|
const message = (()=>{
|
|
40
55
|
if (typeof message_ === 'string') return viem.stringToHex(message_);
|
|
@@ -48,7 +63,32 @@ const formatEVMMessage = (message_)=>{
|
|
|
48
63
|
]);
|
|
49
64
|
};
|
|
50
65
|
const formatTypedData = (typedData)=>{
|
|
51
|
-
|
|
66
|
+
try {
|
|
67
|
+
return viem.hashTypedData(typedData).slice(2);
|
|
68
|
+
} catch (error) {
|
|
69
|
+
// hashTypedData throws opaque errors (e.g. "Cannot read properties of
|
|
70
|
+
// undefined (reading 'length')") when `message` doesn't match `types`.
|
|
71
|
+
// Re-throw with a clear, actionable message identifying the typedData as
|
|
72
|
+
// the cause. Two cases:
|
|
73
|
+
// 1) format issues (bad address, int out of range, wrong bytes size,
|
|
74
|
+
// invalid primaryType/struct) -> viem's validateTypedData pinpoints it.
|
|
75
|
+
// 2) a field declared in `types` is missing/undefined in `message`
|
|
76
|
+
// (incl. primitive `string`/`bytes`) -> validateTypedData does NOT
|
|
77
|
+
// flag this, so we surface it explicitly here.
|
|
78
|
+
const primaryType = typedData.primaryType;
|
|
79
|
+
try {
|
|
80
|
+
viem.validateTypedData(typedData);
|
|
81
|
+
} catch (validationError) {
|
|
82
|
+
throw Object.assign(new Error(`Invalid EIP-712 typedData: ${validationError.message}`), {
|
|
83
|
+
cause: error
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
const primaryTypeSuffix = primaryType ? ` (primaryType "${primaryType}")` : '';
|
|
87
|
+
const message = `Invalid EIP-712 typedData: a field declared in \`types\` is missing or ` + `undefined in \`message\`${primaryTypeSuffix}. ` + `Every field declared in \`types\` must be present in \`message\`. ` + `Underlying error: ${error.message}`;
|
|
88
|
+
throw Object.assign(new Error(message), {
|
|
89
|
+
cause: error
|
|
90
|
+
});
|
|
91
|
+
}
|
|
52
92
|
};
|
|
53
93
|
const serializeECDSASignature = (signature)=>{
|
|
54
94
|
return viem.serializeSignature({
|
|
@@ -70,25 +110,25 @@ const deriveAccountAddress = ({ rawPublicKey })=>{
|
|
|
70
110
|
};
|
|
71
111
|
};
|
|
72
112
|
|
|
73
|
-
const logError$
|
|
113
|
+
const logError$3 = node.createLogError('node-evm');
|
|
74
114
|
/**
|
|
75
115
|
* Creates a delegated EVM wallet client for functional operations
|
|
76
|
-
*/ const createDelegatedEvmWalletClient = ({ environmentId, baseApiUrl, baseMPCRelayApiUrl, apiKey, debug = false })=>{
|
|
116
|
+
*/ const createDelegatedEvmWalletClient = ({ environmentId, baseApiUrl, baseMPCRelayApiUrl, apiKey, debug = false, errorLogging })=>{
|
|
77
117
|
const baseClient = node.createDelegatedWalletClient({
|
|
78
118
|
environmentId,
|
|
79
119
|
baseApiUrl,
|
|
80
120
|
baseMPCRelayApiUrl,
|
|
81
121
|
apiKey,
|
|
82
|
-
debug
|
|
122
|
+
debug,
|
|
123
|
+
errorLogging
|
|
83
124
|
});
|
|
84
|
-
|
|
125
|
+
return _extends({}, baseClient, {
|
|
85
126
|
chainName: 'EVM'
|
|
86
127
|
});
|
|
87
|
-
return evmClient;
|
|
88
128
|
};
|
|
89
129
|
/**
|
|
90
130
|
* Signs a message using delegated signing for EVM
|
|
91
|
-
*/ const delegatedSignMessage = async (client, { walletId, walletApiKey, keyShare, message, context, onError })=>{
|
|
131
|
+
*/ const delegatedSignMessage = async (client, { walletId, shareSetId, walletApiKey, keyShare, message, derivationPath, context, onError })=>{
|
|
92
132
|
try {
|
|
93
133
|
if (!keyShare || !walletId || !walletApiKey) {
|
|
94
134
|
throw new Error('Delegated key share, wallet ID, and wallet API key are required to sign a message');
|
|
@@ -99,17 +139,18 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
99
139
|
};
|
|
100
140
|
const signatureEcdsa = await node.delegatedSignMessage(client, {
|
|
101
141
|
walletId,
|
|
142
|
+
shareSetId,
|
|
102
143
|
walletApiKey,
|
|
103
144
|
keyShare,
|
|
104
145
|
message: formattedMessage,
|
|
105
146
|
chainName: client.chainName,
|
|
147
|
+
derivationPath,
|
|
106
148
|
context: resolvedContext,
|
|
107
149
|
onError
|
|
108
150
|
});
|
|
109
|
-
|
|
110
|
-
return serializedSignature;
|
|
151
|
+
return serializeECDSASignature(signatureEcdsa);
|
|
111
152
|
} catch (error) {
|
|
112
|
-
logError$
|
|
153
|
+
logError$3({
|
|
113
154
|
message: 'Error in delegatedSignMessage',
|
|
114
155
|
error: error,
|
|
115
156
|
context: {
|
|
@@ -121,21 +162,29 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
121
162
|
};
|
|
122
163
|
/**
|
|
123
164
|
* Signs a transaction using delegated signing for EVM
|
|
124
|
-
*/ const delegatedSignTransaction = async (client, { walletId, walletApiKey, keyShare, transaction })=>{
|
|
165
|
+
*/ const delegatedSignTransaction = async (client, { walletId, shareSetId, walletApiKey, keyShare, transaction, derivationPath, context, onError })=>{
|
|
125
166
|
try {
|
|
167
|
+
if (!keyShare || !walletId || !walletApiKey) {
|
|
168
|
+
throw new Error('Delegated key share, wallet ID, and wallet API key are required to sign a transaction');
|
|
169
|
+
}
|
|
126
170
|
// Serialize the transaction
|
|
127
171
|
const serializedTx = viem.serializeTransaction(transaction);
|
|
128
172
|
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
129
173
|
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
130
|
-
throw new
|
|
174
|
+
throw new TypeError('Invalid serializedTxBytes');
|
|
131
175
|
}
|
|
132
|
-
// Use the delegated sign message function from node package
|
|
133
176
|
const signatureEcdsa = await node.delegatedSignMessage(client, {
|
|
134
177
|
walletId,
|
|
178
|
+
shareSetId,
|
|
135
179
|
walletApiKey,
|
|
136
180
|
keyShare,
|
|
137
181
|
message: serializedTxBytes,
|
|
138
|
-
chainName: client.chainName
|
|
182
|
+
chainName: client.chainName,
|
|
183
|
+
derivationPath,
|
|
184
|
+
context: _extends({}, context, {
|
|
185
|
+
evmTransaction: mapTransactionToEvmTransaction(transaction)
|
|
186
|
+
}),
|
|
187
|
+
onError
|
|
139
188
|
});
|
|
140
189
|
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
141
190
|
throw new Error('Invalid signature format returned from MPC signing');
|
|
@@ -149,10 +198,9 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
149
198
|
s: s,
|
|
150
199
|
v: v
|
|
151
200
|
});
|
|
152
|
-
|
|
153
|
-
return serializedSignedTx;
|
|
201
|
+
return viem.serializeTransaction(signedTx);
|
|
154
202
|
} catch (error) {
|
|
155
|
-
logError$
|
|
203
|
+
logError$3({
|
|
156
204
|
message: 'Error in delegatedSignTransaction',
|
|
157
205
|
error: error,
|
|
158
206
|
context: {
|
|
@@ -164,7 +212,7 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
164
212
|
};
|
|
165
213
|
/**
|
|
166
214
|
* Signs typed data using delegated signing for EVM
|
|
167
|
-
*/ const delegatedSignTypedData = async (client, { walletId, walletApiKey, keyShare, typedData })=>{
|
|
215
|
+
*/ const delegatedSignTypedData = async (client, { walletId, shareSetId, walletApiKey, keyShare, typedData, derivationPath, onError })=>{
|
|
168
216
|
try {
|
|
169
217
|
if (!keyShare || !walletId || !walletApiKey) {
|
|
170
218
|
throw new Error('Delegated key share, wallet ID, and wallet API key are required to sign typed data');
|
|
@@ -172,19 +220,21 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
172
220
|
const formattedTypedData = formatTypedData(typedData);
|
|
173
221
|
const signatureEcdsa = await node.delegatedSignMessage(client, {
|
|
174
222
|
walletId,
|
|
223
|
+
shareSetId,
|
|
175
224
|
walletApiKey,
|
|
176
225
|
keyShare,
|
|
177
226
|
message: formattedTypedData,
|
|
178
227
|
chainName: client.chainName,
|
|
228
|
+
derivationPath,
|
|
179
229
|
isFormatted: true,
|
|
180
230
|
context: {
|
|
181
231
|
evmTypedData: typedData
|
|
182
|
-
}
|
|
232
|
+
},
|
|
233
|
+
onError
|
|
183
234
|
});
|
|
184
|
-
|
|
185
|
-
return serializedSignature;
|
|
235
|
+
return serializeECDSASignature(signatureEcdsa);
|
|
186
236
|
} catch (error) {
|
|
187
|
-
logError$
|
|
237
|
+
logError$3({
|
|
188
238
|
message: 'Error in delegatedSignTypedData',
|
|
189
239
|
error: error,
|
|
190
240
|
context: {
|
|
@@ -196,7 +246,7 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
196
246
|
};
|
|
197
247
|
/**
|
|
198
248
|
* Signs EIP-7702 authorization using delegated signing for EVM
|
|
199
|
-
*/ const delegatedSignAuthorization = async (client, { walletId, walletApiKey, keyShare, authorization })=>{
|
|
249
|
+
*/ const delegatedSignAuthorization = async (client, { walletId, shareSetId, walletApiKey, keyShare, authorization, derivationPath, onError })=>{
|
|
200
250
|
try {
|
|
201
251
|
if (!keyShare || !walletId || !walletApiKey) {
|
|
202
252
|
throw new Error('Delegated key share, wallet ID, and wallet API key are required to sign authorization');
|
|
@@ -205,20 +255,22 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
205
255
|
const prehashed = digest.startsWith('0x') ? digest.slice(2) : digest;
|
|
206
256
|
const signatureEcdsa = await node.delegatedSignMessage(client, {
|
|
207
257
|
walletId,
|
|
258
|
+
shareSetId,
|
|
208
259
|
walletApiKey,
|
|
209
260
|
keyShare,
|
|
210
261
|
message: prehashed,
|
|
211
262
|
chainName: client.chainName,
|
|
263
|
+
derivationPath,
|
|
212
264
|
isFormatted: true,
|
|
213
265
|
context: {
|
|
214
266
|
eip7702Auth: authorization
|
|
215
|
-
}
|
|
267
|
+
},
|
|
268
|
+
onError
|
|
216
269
|
});
|
|
217
270
|
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
218
|
-
|
|
219
|
-
return signature;
|
|
271
|
+
return viem.parseSignature(serializedSignature);
|
|
220
272
|
} catch (error) {
|
|
221
|
-
logError$
|
|
273
|
+
logError$3({
|
|
222
274
|
message: 'Error in delegatedSignAuthorization',
|
|
223
275
|
error: error,
|
|
224
276
|
context: {
|
|
@@ -231,25 +283,31 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
231
283
|
/**
|
|
232
284
|
* Signs a pre-formatted (raw) message using delegated signing for EVM.
|
|
233
285
|
* The message must be a hex string representing already-hashed data.
|
|
234
|
-
|
|
286
|
+
*
|
|
287
|
+
* Provide either `rawSign` — the pre-image the digest comes from — or an
|
|
288
|
+
* explicit `context`, so a policy-enabled wallet can evaluate the request.
|
|
289
|
+
*/ const delegatedSignRawMessage = async (client, { walletId, shareSetId, walletApiKey, keyShare, message, derivationPath, context, rawSign, onError })=>{
|
|
235
290
|
try {
|
|
236
291
|
if (!keyShare || !walletId || !walletApiKey) {
|
|
237
292
|
throw new Error('Delegated key share, wallet ID, and wallet API key are required to sign a raw message');
|
|
238
293
|
}
|
|
294
|
+
const resolvedContext = node.resolveRawSignContext(context, rawSign);
|
|
239
295
|
const prehashed = node.stripHexPrefix(message);
|
|
240
296
|
const signatureEcdsa = await node.delegatedSignMessage(client, {
|
|
241
297
|
walletId,
|
|
298
|
+
shareSetId,
|
|
242
299
|
walletApiKey,
|
|
243
300
|
keyShare,
|
|
244
301
|
message: prehashed,
|
|
245
302
|
chainName: client.chainName,
|
|
303
|
+
derivationPath,
|
|
246
304
|
isFormatted: true,
|
|
247
|
-
context,
|
|
305
|
+
context: resolvedContext,
|
|
248
306
|
onError
|
|
249
307
|
});
|
|
250
308
|
return serializeECDSASignature(signatureEcdsa);
|
|
251
309
|
} catch (error) {
|
|
252
|
-
logError$
|
|
310
|
+
logError$3({
|
|
253
311
|
message: 'Error in delegatedSignRawMessage',
|
|
254
312
|
error: error,
|
|
255
313
|
context: {
|
|
@@ -261,17 +319,20 @@ const logError$1 = node.createLogError('node-evm');
|
|
|
261
319
|
};
|
|
262
320
|
/**
|
|
263
321
|
* Revoke delegation - delegates to the node package
|
|
264
|
-
*/ const revokeDelegation = async (client,
|
|
265
|
-
return node.revokeDelegation(client,
|
|
322
|
+
*/ const revokeDelegation = async (client, { walletId })=>{
|
|
323
|
+
return node.revokeDelegation(client, {
|
|
324
|
+
walletId
|
|
325
|
+
});
|
|
266
326
|
};
|
|
267
327
|
|
|
268
|
-
const createAccountAdapter = ({ evmClient,
|
|
328
|
+
const createAccountAdapter = ({ evmClient, walletMetadata, password, externalServerKeyShares, delegated })=>{
|
|
269
329
|
return accounts.toAccount({
|
|
270
|
-
address: accountAddress,
|
|
330
|
+
address: walletMetadata.accountAddress,
|
|
271
331
|
signMessage: async ({ message })=>{
|
|
272
332
|
if (delegated) {
|
|
273
333
|
return delegatedSignMessage(delegated.delegatedClient, {
|
|
274
334
|
walletId: delegated.walletId,
|
|
335
|
+
shareSetId: delegated.shareSetId,
|
|
275
336
|
walletApiKey: delegated.walletApiKey,
|
|
276
337
|
keyShare: delegated.keyShare,
|
|
277
338
|
message: message
|
|
@@ -279,7 +340,7 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
279
340
|
}
|
|
280
341
|
const signature = await evmClient.signMessage({
|
|
281
342
|
message: message,
|
|
282
|
-
|
|
343
|
+
walletMetadata,
|
|
283
344
|
password,
|
|
284
345
|
externalServerKeyShares
|
|
285
346
|
});
|
|
@@ -289,13 +350,14 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
289
350
|
if (delegated) {
|
|
290
351
|
return delegatedSignTypedData(delegated.delegatedClient, {
|
|
291
352
|
walletId: delegated.walletId,
|
|
353
|
+
shareSetId: delegated.shareSetId,
|
|
292
354
|
walletApiKey: delegated.walletApiKey,
|
|
293
355
|
keyShare: delegated.keyShare,
|
|
294
356
|
typedData: typedData
|
|
295
357
|
});
|
|
296
358
|
}
|
|
297
359
|
return evmClient.signTypedData({
|
|
298
|
-
|
|
360
|
+
walletMetadata,
|
|
299
361
|
typedData: typedData,
|
|
300
362
|
password: password,
|
|
301
363
|
externalServerKeyShares
|
|
@@ -305,13 +367,14 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
305
367
|
if (delegated) {
|
|
306
368
|
return delegatedSignTransaction(delegated.delegatedClient, {
|
|
307
369
|
walletId: delegated.walletId,
|
|
370
|
+
shareSetId: delegated.shareSetId,
|
|
308
371
|
walletApiKey: delegated.walletApiKey,
|
|
309
372
|
keyShare: delegated.keyShare,
|
|
310
373
|
transaction
|
|
311
374
|
});
|
|
312
375
|
}
|
|
313
376
|
const signedTx = await evmClient.signTransaction({
|
|
314
|
-
|
|
377
|
+
walletMetadata,
|
|
315
378
|
transaction,
|
|
316
379
|
password,
|
|
317
380
|
externalServerKeyShares
|
|
@@ -322,6 +385,7 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
322
385
|
if (delegated) {
|
|
323
386
|
const signature = await delegatedSignAuthorization(delegated.delegatedClient, {
|
|
324
387
|
walletId: delegated.walletId,
|
|
388
|
+
shareSetId: delegated.shareSetId,
|
|
325
389
|
walletApiKey: delegated.walletApiKey,
|
|
326
390
|
keyShare: delegated.keyShare,
|
|
327
391
|
authorization
|
|
@@ -340,7 +404,7 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
340
404
|
}
|
|
341
405
|
const signature = await evmClient.signAuthorization({
|
|
342
406
|
authorization,
|
|
343
|
-
|
|
407
|
+
walletMetadata,
|
|
344
408
|
password,
|
|
345
409
|
externalServerKeyShares
|
|
346
410
|
});
|
|
@@ -359,8 +423,751 @@ const createAccountAdapter = ({ evmClient, accountAddress, password, externalSer
|
|
|
359
423
|
});
|
|
360
424
|
};
|
|
361
425
|
|
|
362
|
-
|
|
363
|
-
|
|
426
|
+
function _object_without_properties_loose(source, excluded) {
|
|
427
|
+
if (source == null) return {};
|
|
428
|
+
var target = {};
|
|
429
|
+
var sourceKeys = Object.keys(source);
|
|
430
|
+
var key, i;
|
|
431
|
+
for(i = 0; i < sourceKeys.length; i++){
|
|
432
|
+
key = sourceKeys[i];
|
|
433
|
+
if (excluded.indexOf(key) >= 0) continue;
|
|
434
|
+
target[key] = source[key];
|
|
435
|
+
}
|
|
436
|
+
return target;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const ERROR_EARN_NOT_EVM_WALLET = 'Wallet metadata is not for an EVM wallet';
|
|
440
|
+
const ERROR_EARN_MISSING_EVM_TRANSACTION = 'Earn signing payload is missing EVM transaction data';
|
|
441
|
+
const ERROR_EARN_CHAIN_MISMATCH = 'The provided chain does not match the chain the Earn transaction was prepared for';
|
|
442
|
+
const ERROR_EARN_APPROVAL_FAILED = 'The ERC-20 approval for the Earn transaction reverted';
|
|
443
|
+
const ERROR_EARN_CHAIN_REQUIRED = 'Either chain or rpcUrl is required to execute an Earn transaction. Provide an RPC URL for the vault chain.';
|
|
444
|
+
/**
|
|
445
|
+
* Vault deposits and withdrawals rebalance the underlying lending markets, so gas estimated at
|
|
446
|
+
* the current block is regularly lower than what execution needs once the transaction is mined.
|
|
447
|
+
*/ const EARN_GAS_LIMIT_BUFFER_PERCENT = 30n;
|
|
448
|
+
const ERC20_ALLOWANCE_AND_APPROVE_ABI = [
|
|
449
|
+
{
|
|
450
|
+
name: 'allowance',
|
|
451
|
+
type: 'function',
|
|
452
|
+
stateMutability: 'view',
|
|
453
|
+
inputs: [
|
|
454
|
+
{
|
|
455
|
+
name: 'owner',
|
|
456
|
+
type: 'address'
|
|
457
|
+
},
|
|
458
|
+
{
|
|
459
|
+
name: 'spender',
|
|
460
|
+
type: 'address'
|
|
461
|
+
}
|
|
462
|
+
],
|
|
463
|
+
outputs: [
|
|
464
|
+
{
|
|
465
|
+
name: '',
|
|
466
|
+
type: 'uint256'
|
|
467
|
+
}
|
|
468
|
+
]
|
|
469
|
+
},
|
|
470
|
+
{
|
|
471
|
+
name: 'approve',
|
|
472
|
+
type: 'function',
|
|
473
|
+
stateMutability: 'nonpayable',
|
|
474
|
+
inputs: [
|
|
475
|
+
{
|
|
476
|
+
name: 'spender',
|
|
477
|
+
type: 'address'
|
|
478
|
+
},
|
|
479
|
+
{
|
|
480
|
+
name: 'amount',
|
|
481
|
+
type: 'uint256'
|
|
482
|
+
}
|
|
483
|
+
],
|
|
484
|
+
outputs: [
|
|
485
|
+
{
|
|
486
|
+
name: '',
|
|
487
|
+
type: 'bool'
|
|
488
|
+
}
|
|
489
|
+
]
|
|
490
|
+
}
|
|
491
|
+
];
|
|
492
|
+
|
|
493
|
+
/**
|
|
494
|
+
* Builds a stateless Earn client for EVM vaults: it prepares vault actions through the
|
|
495
|
+
* Dynamic API and executes the returned unsigned transactions with the caller's wallet,
|
|
496
|
+
* sending an ERC-20 approval first when the vault requires one.
|
|
497
|
+
*/ const createEvmEarnClient = ({ getEarnClient, createViemPublicClient, getWalletClient, sendSponsoredTransaction })=>{
|
|
498
|
+
const resolveChain = ({ chain, chainId, rpcUrl })=>{
|
|
499
|
+
if (chain) {
|
|
500
|
+
if (chain.id !== chainId) {
|
|
501
|
+
throw new Error(ERROR_EARN_CHAIN_MISMATCH);
|
|
502
|
+
}
|
|
503
|
+
return chain;
|
|
504
|
+
}
|
|
505
|
+
if (!rpcUrl) {
|
|
506
|
+
throw new Error(ERROR_EARN_CHAIN_REQUIRED);
|
|
507
|
+
}
|
|
508
|
+
return viem.defineChain({
|
|
509
|
+
id: chainId,
|
|
510
|
+
name: `Chain ${chainId}`,
|
|
511
|
+
nativeCurrency: {
|
|
512
|
+
name: 'Ether',
|
|
513
|
+
symbol: 'ETH',
|
|
514
|
+
decimals: 18
|
|
515
|
+
},
|
|
516
|
+
rpcUrls: {
|
|
517
|
+
default: {
|
|
518
|
+
http: [
|
|
519
|
+
rpcUrl
|
|
520
|
+
]
|
|
521
|
+
}
|
|
522
|
+
}
|
|
523
|
+
});
|
|
524
|
+
};
|
|
525
|
+
const buildGasLimitAndNonce = async ({ transaction, publicClient, accountAddress })=>{
|
|
526
|
+
const params = {};
|
|
527
|
+
if (transaction.gasLimit) {
|
|
528
|
+
params.gas = BigInt(transaction.gasLimit);
|
|
529
|
+
} else {
|
|
530
|
+
const estimatedGas = await publicClient.estimateGas({
|
|
531
|
+
account: accountAddress,
|
|
532
|
+
to: transaction.to,
|
|
533
|
+
data: transaction.data,
|
|
534
|
+
value: transaction.value
|
|
535
|
+
});
|
|
536
|
+
params.gas = estimatedGas + estimatedGas * EARN_GAS_LIMIT_BUFFER_PERCENT / 100n;
|
|
537
|
+
}
|
|
538
|
+
if (transaction.nonce !== undefined) {
|
|
539
|
+
params.nonce = transaction.nonce;
|
|
540
|
+
}
|
|
541
|
+
return params;
|
|
542
|
+
};
|
|
543
|
+
const relaySponsoredEarnTransaction = async ({ chainId, evmApproval, evmTransaction, externalServerKeyShares, onStepChange, password, publicClient, resolvedChain, rpcUrl, traceContext, walletMetadata })=>{
|
|
544
|
+
const calls = [];
|
|
545
|
+
if (evmApproval) {
|
|
546
|
+
const currentAllowance = await publicClient.readContract({
|
|
547
|
+
address: evmApproval.tokenAddress,
|
|
548
|
+
abi: ERC20_ALLOWANCE_AND_APPROVE_ABI,
|
|
549
|
+
functionName: 'allowance',
|
|
550
|
+
args: [
|
|
551
|
+
walletMetadata.accountAddress,
|
|
552
|
+
evmApproval.spenderAddress
|
|
553
|
+
]
|
|
554
|
+
});
|
|
555
|
+
const requiredAllowance = BigInt(evmApproval.amount);
|
|
556
|
+
if (currentAllowance < requiredAllowance) {
|
|
557
|
+
onStepChange == null ? void 0 : onStepChange('approval');
|
|
558
|
+
calls.push({
|
|
559
|
+
target: evmApproval.tokenAddress,
|
|
560
|
+
data: viem.encodeFunctionData({
|
|
561
|
+
abi: ERC20_ALLOWANCE_AND_APPROVE_ABI,
|
|
562
|
+
functionName: 'approve',
|
|
563
|
+
args: [
|
|
564
|
+
evmApproval.spenderAddress,
|
|
565
|
+
requiredAllowance
|
|
566
|
+
]
|
|
567
|
+
}),
|
|
568
|
+
value: 0n
|
|
569
|
+
});
|
|
570
|
+
}
|
|
571
|
+
}
|
|
572
|
+
onStepChange == null ? void 0 : onStepChange('transaction');
|
|
573
|
+
calls.push({
|
|
574
|
+
target: evmTransaction.to,
|
|
575
|
+
data: evmTransaction.data,
|
|
576
|
+
value: evmTransaction.value ? BigInt(evmTransaction.value) : 0n
|
|
577
|
+
});
|
|
578
|
+
return sendSponsoredTransaction({
|
|
579
|
+
walletMetadata,
|
|
580
|
+
calls,
|
|
581
|
+
chainId,
|
|
582
|
+
password,
|
|
583
|
+
externalServerKeyShares,
|
|
584
|
+
// The relayer needs an RPC to read the wallet's on-chain state; fall back to the
|
|
585
|
+
// resolved chain's default when the caller passed `chain` without `rpcUrl`.
|
|
586
|
+
rpcUrl: rpcUrl != null ? rpcUrl : resolvedChain.rpcUrls.default.http[0],
|
|
587
|
+
traceContext,
|
|
588
|
+
autoDelegate: true
|
|
589
|
+
});
|
|
590
|
+
};
|
|
591
|
+
const executeEarnTransaction = async ({ signingPayload, traceContext, walletMetadata, password, externalServerKeyShares, sponsored, chain, rpcUrl, onStepChange })=>{
|
|
592
|
+
if (walletMetadata.chainName !== 'EVM') {
|
|
593
|
+
throw new Error(ERROR_EARN_NOT_EVM_WALLET);
|
|
594
|
+
}
|
|
595
|
+
const { evmTransaction, evmApproval } = signingPayload;
|
|
596
|
+
if (!evmTransaction) {
|
|
597
|
+
throw new Error(ERROR_EARN_MISSING_EVM_TRANSACTION);
|
|
598
|
+
}
|
|
599
|
+
const chainId = Number(signingPayload.chainId);
|
|
600
|
+
const resolvedChain = resolveChain({
|
|
601
|
+
chain,
|
|
602
|
+
chainId,
|
|
603
|
+
rpcUrl
|
|
604
|
+
});
|
|
605
|
+
const publicClient = createViemPublicClient({
|
|
606
|
+
chain: resolvedChain,
|
|
607
|
+
rpcUrl
|
|
608
|
+
});
|
|
609
|
+
// Sponsored execution relays the approval and vault call as one batch through the
|
|
610
|
+
// gasless relayer, so no gas estimation or EOA sending is needed.
|
|
611
|
+
if (sponsored === true) {
|
|
612
|
+
return relaySponsoredEarnTransaction({
|
|
613
|
+
chainId,
|
|
614
|
+
evmApproval,
|
|
615
|
+
evmTransaction,
|
|
616
|
+
externalServerKeyShares,
|
|
617
|
+
onStepChange,
|
|
618
|
+
password,
|
|
619
|
+
publicClient,
|
|
620
|
+
resolvedChain,
|
|
621
|
+
rpcUrl,
|
|
622
|
+
traceContext,
|
|
623
|
+
walletMetadata
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
const walletClient = await getWalletClient({
|
|
627
|
+
walletMetadata,
|
|
628
|
+
password,
|
|
629
|
+
externalServerKeyShares,
|
|
630
|
+
chain: resolvedChain,
|
|
631
|
+
rpcUrl
|
|
632
|
+
});
|
|
633
|
+
if (evmApproval) {
|
|
634
|
+
const currentAllowance = await publicClient.readContract({
|
|
635
|
+
address: evmApproval.tokenAddress,
|
|
636
|
+
abi: ERC20_ALLOWANCE_AND_APPROVE_ABI,
|
|
637
|
+
functionName: 'allowance',
|
|
638
|
+
args: [
|
|
639
|
+
walletMetadata.accountAddress,
|
|
640
|
+
evmApproval.spenderAddress
|
|
641
|
+
]
|
|
642
|
+
});
|
|
643
|
+
const requiredAllowance = BigInt(evmApproval.amount);
|
|
644
|
+
if (currentAllowance < requiredAllowance) {
|
|
645
|
+
onStepChange == null ? void 0 : onStepChange('approval');
|
|
646
|
+
const approvalHash = await walletClient.sendTransaction({
|
|
647
|
+
to: evmApproval.tokenAddress,
|
|
648
|
+
data: viem.encodeFunctionData({
|
|
649
|
+
abi: ERC20_ALLOWANCE_AND_APPROVE_ABI,
|
|
650
|
+
functionName: 'approve',
|
|
651
|
+
args: [
|
|
652
|
+
evmApproval.spenderAddress,
|
|
653
|
+
requiredAllowance
|
|
654
|
+
]
|
|
655
|
+
}),
|
|
656
|
+
value: 0n
|
|
657
|
+
});
|
|
658
|
+
const approvalReceipt = await publicClient.waitForTransactionReceipt({
|
|
659
|
+
hash: approvalHash
|
|
660
|
+
});
|
|
661
|
+
if (approvalReceipt.status !== 'success') {
|
|
662
|
+
throw new Error(ERROR_EARN_APPROVAL_FAILED);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
onStepChange == null ? void 0 : onStepChange('transaction');
|
|
667
|
+
let value = 0n;
|
|
668
|
+
if (evmTransaction.value) {
|
|
669
|
+
value = BigInt(evmTransaction.value);
|
|
670
|
+
}
|
|
671
|
+
const gasLimitAndNonce = await buildGasLimitAndNonce({
|
|
672
|
+
transaction: _extends({}, evmTransaction, {
|
|
673
|
+
value
|
|
674
|
+
}),
|
|
675
|
+
publicClient,
|
|
676
|
+
accountAddress: walletMetadata.accountAddress
|
|
677
|
+
});
|
|
678
|
+
const request = _extends({
|
|
679
|
+
to: evmTransaction.to,
|
|
680
|
+
data: evmTransaction.data,
|
|
681
|
+
value
|
|
682
|
+
}, gasLimitAndNonce);
|
|
683
|
+
const hasEip1559Fees = Boolean(evmTransaction.maxFeePerGas && evmTransaction.maxPriorityFeePerGas);
|
|
684
|
+
if (hasEip1559Fees) {
|
|
685
|
+
const transactionHash = await walletClient.sendTransaction(_extends({}, request, {
|
|
686
|
+
maxFeePerGas: BigInt(evmTransaction.maxFeePerGas),
|
|
687
|
+
maxPriorityFeePerGas: BigInt(evmTransaction.maxPriorityFeePerGas)
|
|
688
|
+
}));
|
|
689
|
+
return {
|
|
690
|
+
transactionHash
|
|
691
|
+
};
|
|
692
|
+
}
|
|
693
|
+
if (evmTransaction.gasPrice) {
|
|
694
|
+
const transactionHash = await walletClient.sendTransaction(_extends({}, request, {
|
|
695
|
+
gasPrice: BigInt(evmTransaction.gasPrice)
|
|
696
|
+
}));
|
|
697
|
+
return {
|
|
698
|
+
transactionHash
|
|
699
|
+
};
|
|
700
|
+
}
|
|
701
|
+
const transactionHash = await walletClient.sendTransaction(request);
|
|
702
|
+
return {
|
|
703
|
+
transactionHash
|
|
704
|
+
};
|
|
705
|
+
};
|
|
706
|
+
const depositToEarnVault = async (_param)=>{
|
|
707
|
+
var { vaultId, amount, receiverAddress, traceContext } = _param, walletParams = _object_without_properties_loose(_param, [
|
|
708
|
+
"vaultId",
|
|
709
|
+
"amount",
|
|
710
|
+
"receiverAddress",
|
|
711
|
+
"traceContext"
|
|
712
|
+
]);
|
|
713
|
+
const signingPayload = await getEarnClient().prepareDeposit({
|
|
714
|
+
vaultId,
|
|
715
|
+
ownerAddress: walletParams.walletMetadata.accountAddress,
|
|
716
|
+
amount,
|
|
717
|
+
receiverAddress,
|
|
718
|
+
traceContext
|
|
719
|
+
});
|
|
720
|
+
return executeEarnTransaction(_extends({
|
|
721
|
+
signingPayload,
|
|
722
|
+
traceContext
|
|
723
|
+
}, walletParams));
|
|
724
|
+
};
|
|
725
|
+
const withdrawFromEarnVault = async (_param)=>{
|
|
726
|
+
var { vaultId, amount, receiverAddress, traceContext } = _param, walletParams = _object_without_properties_loose(_param, [
|
|
727
|
+
"vaultId",
|
|
728
|
+
"amount",
|
|
729
|
+
"receiverAddress",
|
|
730
|
+
"traceContext"
|
|
731
|
+
]);
|
|
732
|
+
const signingPayload = await getEarnClient().prepareWithdrawal({
|
|
733
|
+
vaultId,
|
|
734
|
+
ownerAddress: walletParams.walletMetadata.accountAddress,
|
|
735
|
+
amount,
|
|
736
|
+
receiverAddress,
|
|
737
|
+
traceContext
|
|
738
|
+
});
|
|
739
|
+
return executeEarnTransaction(_extends({
|
|
740
|
+
signingPayload,
|
|
741
|
+
traceContext
|
|
742
|
+
}, walletParams));
|
|
743
|
+
};
|
|
744
|
+
const claimEarnRewards = async (_param)=>{
|
|
745
|
+
var { vaultId, traceContext } = _param, walletParams = _object_without_properties_loose(_param, [
|
|
746
|
+
"vaultId",
|
|
747
|
+
"traceContext"
|
|
748
|
+
]);
|
|
749
|
+
const signingPayload = await getEarnClient().prepareClaimRewards({
|
|
750
|
+
vaultId,
|
|
751
|
+
ownerAddress: walletParams.walletMetadata.accountAddress,
|
|
752
|
+
traceContext
|
|
753
|
+
});
|
|
754
|
+
return executeEarnTransaction(_extends({
|
|
755
|
+
signingPayload,
|
|
756
|
+
traceContext
|
|
757
|
+
}, walletParams));
|
|
758
|
+
};
|
|
759
|
+
return {
|
|
760
|
+
executeEarnTransaction,
|
|
761
|
+
depositToEarnVault,
|
|
762
|
+
withdrawFromEarnVault,
|
|
763
|
+
claimEarnRewards
|
|
764
|
+
};
|
|
765
|
+
};
|
|
766
|
+
|
|
767
|
+
/** The Fireblocks Universal Gasless Delegate (UGD) contract address used for EIP-7702 delegation. */ const DELEGATION_CONTRACT_ADDRESS = '0x0000Fb7702036ff9f76044a501ac1aA74cbab16b';
|
|
768
|
+
/** Default sponsored transaction validity duration in seconds (10 minutes). */ const DEFAULT_VALID_FOR_SECONDS = 600;
|
|
769
|
+
/** EIP-7702 delegation code prefix used to identify delegated accounts. */ const DELEGATION_CODE_PREFIX = '0xef0100';
|
|
770
|
+
/** ERC-7579 batch call with opData and auth mode identifier. */ const BATCH_CALL_OPDATA_AUTH_MODE = '0x0100000000007821000100000000000000000000000000000000000000000000';
|
|
771
|
+
/** EIP-712 typed data types for UGD AuthorizedExecutions signing. */ const AUTHORIZED_EXECUTIONS_TYPES = {
|
|
772
|
+
AuthorizedExecutions: [
|
|
773
|
+
{
|
|
774
|
+
name: 'calls',
|
|
775
|
+
type: 'Execution[]'
|
|
776
|
+
},
|
|
777
|
+
{
|
|
778
|
+
name: 'deadline',
|
|
779
|
+
type: 'uint256'
|
|
780
|
+
},
|
|
781
|
+
{
|
|
782
|
+
name: 'mode',
|
|
783
|
+
type: 'bytes32'
|
|
784
|
+
},
|
|
785
|
+
{
|
|
786
|
+
name: 'nonce',
|
|
787
|
+
type: 'uint256'
|
|
788
|
+
},
|
|
789
|
+
{
|
|
790
|
+
name: 'relayer',
|
|
791
|
+
type: 'address'
|
|
792
|
+
}
|
|
793
|
+
],
|
|
794
|
+
Execution: [
|
|
795
|
+
{
|
|
796
|
+
name: 'target',
|
|
797
|
+
type: 'address'
|
|
798
|
+
},
|
|
799
|
+
{
|
|
800
|
+
name: 'value',
|
|
801
|
+
type: 'uint256'
|
|
802
|
+
},
|
|
803
|
+
{
|
|
804
|
+
name: 'data',
|
|
805
|
+
type: 'bytes'
|
|
806
|
+
}
|
|
807
|
+
]
|
|
808
|
+
};
|
|
809
|
+
/** Minimal ABI for the UGD contract's nonce validation function. */ const UGD_ABI = [
|
|
810
|
+
{
|
|
811
|
+
inputs: [
|
|
812
|
+
{
|
|
813
|
+
name: 'nonce',
|
|
814
|
+
type: 'uint256'
|
|
815
|
+
}
|
|
816
|
+
],
|
|
817
|
+
name: 'isNonceUsed',
|
|
818
|
+
outputs: [
|
|
819
|
+
{
|
|
820
|
+
name: '',
|
|
821
|
+
type: 'bool'
|
|
822
|
+
}
|
|
823
|
+
],
|
|
824
|
+
stateMutability: 'view',
|
|
825
|
+
type: 'function'
|
|
826
|
+
}
|
|
827
|
+
];
|
|
828
|
+
/** Default polling interval for sponsored transaction status checks (milliseconds). */ const EVM_GASLESS_DEFAULT_POLL_INTERVAL_MS = 2000;
|
|
829
|
+
/** Default timeout for sponsored transaction polling (milliseconds). */ const EVM_GASLESS_DEFAULT_TIMEOUT_MS = 60000;
|
|
830
|
+
// Error messages
|
|
831
|
+
const ERROR_GASLESS_RPC_REQUIRED_FOR_AUTO_DELEGATE = 'rpcUrl is required when autoDelegate is true and no explicit authorization is provided. ' + 'Pass rpcUrl, pass an explicit authorization, or set autoDelegate: false.';
|
|
832
|
+
const ERROR_GASLESS_RELAY_FAILED = 'EVM sponsored transaction relay failed';
|
|
833
|
+
const ERROR_GASLESS_TIMEOUT = 'EVM sponsored transaction timed out waiting for terminal status';
|
|
834
|
+
const ERROR_GASLESS_MISSING_REQUEST_ID = 'Relay response did not include a requestId';
|
|
835
|
+
const ERROR_GASLESS_MISSING_YPARITY = 'Signed authorization is missing yParity';
|
|
836
|
+
const ERROR_GASLESS_RPC_REQUIRED = 'rpcUrl is required for this operation';
|
|
837
|
+
|
|
838
|
+
const logError$2 = node.createLogError('node-evm');
|
|
839
|
+
/**
|
|
840
|
+
* Builds a stateless gasless client for EVM sponsored transactions (Fireblocks-relayer
|
|
841
|
+
* native gasless via EIP-7702 / AuthorizedExecutions). It holds no wallet state — the
|
|
842
|
+
* signing primitives and API client are injected by the caller that owns the wallet.
|
|
843
|
+
*/ const createEvmGaslessClient = ({ apiClient, createViemPublicClient, signAuthorization, signTypedData })=>{
|
|
844
|
+
const viemPublicClientForChain = (chainId, rpcUrl)=>{
|
|
845
|
+
if (!rpcUrl) {
|
|
846
|
+
throw new Error(ERROR_GASLESS_RPC_REQUIRED);
|
|
847
|
+
}
|
|
848
|
+
const chain = viem.defineChain({
|
|
849
|
+
id: chainId,
|
|
850
|
+
name: `Chain ${chainId}`,
|
|
851
|
+
nativeCurrency: {
|
|
852
|
+
name: 'Ether',
|
|
853
|
+
symbol: 'ETH',
|
|
854
|
+
decimals: 18
|
|
855
|
+
},
|
|
856
|
+
rpcUrls: {
|
|
857
|
+
default: {
|
|
858
|
+
http: [
|
|
859
|
+
rpcUrl
|
|
860
|
+
]
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
});
|
|
864
|
+
return createViemPublicClient({
|
|
865
|
+
chain,
|
|
866
|
+
rpcUrl
|
|
867
|
+
});
|
|
868
|
+
};
|
|
869
|
+
const generateRandomNonce = ()=>BigInt('0x' + node_crypto.randomBytes(32).toString('hex'));
|
|
870
|
+
const generateIntentNonce = async ({ walletAddress, chainId, rpcUrl })=>{
|
|
871
|
+
let nonce = generateRandomNonce();
|
|
872
|
+
if (rpcUrl) {
|
|
873
|
+
try {
|
|
874
|
+
const publicClient = viemPublicClientForChain(chainId, rpcUrl);
|
|
875
|
+
// On a brand-new (undelegated) wallet the address has no UGD code yet,
|
|
876
|
+
// so isNonceUsed reverts — the random value is safe regardless.
|
|
877
|
+
const isUsed = await publicClient.readContract({
|
|
878
|
+
address: walletAddress,
|
|
879
|
+
abi: UGD_ABI,
|
|
880
|
+
functionName: 'isNonceUsed',
|
|
881
|
+
args: [
|
|
882
|
+
nonce
|
|
883
|
+
]
|
|
884
|
+
});
|
|
885
|
+
if (isUsed) {
|
|
886
|
+
nonce = generateRandomNonce();
|
|
887
|
+
}
|
|
888
|
+
} catch (e) {
|
|
889
|
+
// RPC failure is tolerable — 256-bit random collision is negligible
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return nonce;
|
|
893
|
+
};
|
|
894
|
+
const is7702DelegationActive = async ({ walletAddress, chainId, rpcUrl })=>{
|
|
895
|
+
const publicClient = viemPublicClientForChain(chainId, rpcUrl);
|
|
896
|
+
const code = await publicClient.getCode({
|
|
897
|
+
address: walletAddress
|
|
898
|
+
});
|
|
899
|
+
if (!code) return false;
|
|
900
|
+
const expectedPrefix = DELEGATION_CODE_PREFIX + DELEGATION_CONTRACT_ADDRESS.slice(2).toLowerCase();
|
|
901
|
+
return code.toLowerCase().startsWith(expectedPrefix);
|
|
902
|
+
};
|
|
903
|
+
const sign7702Authorization = async ({ walletMetadata, chainId, rpcUrl, nonce, password, externalServerKeyShares })=>{
|
|
904
|
+
const address = walletMetadata.accountAddress;
|
|
905
|
+
try {
|
|
906
|
+
let eoaNonce;
|
|
907
|
+
if (nonce === undefined) {
|
|
908
|
+
if (!rpcUrl) {
|
|
909
|
+
throw new Error(ERROR_GASLESS_RPC_REQUIRED);
|
|
910
|
+
}
|
|
911
|
+
const publicClient = viemPublicClientForChain(chainId, rpcUrl);
|
|
912
|
+
eoaNonce = Number(await publicClient.getTransactionCount({
|
|
913
|
+
address
|
|
914
|
+
}));
|
|
915
|
+
} else {
|
|
916
|
+
eoaNonce = nonce;
|
|
917
|
+
}
|
|
918
|
+
const authorization = {
|
|
919
|
+
address: DELEGATION_CONTRACT_ADDRESS,
|
|
920
|
+
chainId,
|
|
921
|
+
nonce: eoaNonce
|
|
922
|
+
};
|
|
923
|
+
const sig = await signAuthorization({
|
|
924
|
+
authorization,
|
|
925
|
+
walletMetadata,
|
|
926
|
+
password,
|
|
927
|
+
externalServerKeyShares
|
|
928
|
+
});
|
|
929
|
+
if (sig.yParity === undefined) {
|
|
930
|
+
throw new Error(ERROR_GASLESS_MISSING_YPARITY);
|
|
931
|
+
}
|
|
932
|
+
return {
|
|
933
|
+
address: DELEGATION_CONTRACT_ADDRESS,
|
|
934
|
+
chainId,
|
|
935
|
+
nonce: eoaNonce,
|
|
936
|
+
r: sig.r,
|
|
937
|
+
s: sig.s,
|
|
938
|
+
yParity: sig.yParity
|
|
939
|
+
};
|
|
940
|
+
} catch (error) {
|
|
941
|
+
logError$2({
|
|
942
|
+
message: 'Error signing 7702 authorization',
|
|
943
|
+
error: error,
|
|
944
|
+
context: {
|
|
945
|
+
accountAddress: address,
|
|
946
|
+
chainId
|
|
947
|
+
}
|
|
948
|
+
});
|
|
949
|
+
throw error;
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
const getAvailableEvmGaslessRelayer = async ({ chainId, traceContext })=>{
|
|
953
|
+
const result = await apiClient.getAvailableEVMGaslessRelayer({
|
|
954
|
+
chainId,
|
|
955
|
+
traceContext
|
|
956
|
+
});
|
|
957
|
+
return {
|
|
958
|
+
relayerAddress: result.relayerAddress
|
|
959
|
+
};
|
|
960
|
+
};
|
|
961
|
+
const resolveSponsoredAuthorization = async ({ authorization, autoDelegate, walletMetadata, chainId, rpcUrl, password, externalServerKeyShares })=>{
|
|
962
|
+
if (authorization) {
|
|
963
|
+
return authorization;
|
|
964
|
+
}
|
|
965
|
+
if (!autoDelegate) {
|
|
966
|
+
return undefined;
|
|
967
|
+
}
|
|
968
|
+
// The EOA nonce and delegation status are only knowable on-chain; without
|
|
969
|
+
// an RPC we can't safely auto-sign a 7702 authorization, so fail loud
|
|
970
|
+
// instead of relaying an intent that would revert.
|
|
971
|
+
if (!rpcUrl) {
|
|
972
|
+
throw new Error(ERROR_GASLESS_RPC_REQUIRED_FOR_AUTO_DELEGATE);
|
|
973
|
+
}
|
|
974
|
+
const walletAddress = walletMetadata.accountAddress;
|
|
975
|
+
const isDelegated = await is7702DelegationActive({
|
|
976
|
+
walletAddress,
|
|
977
|
+
chainId,
|
|
978
|
+
rpcUrl
|
|
979
|
+
});
|
|
980
|
+
if (isDelegated) {
|
|
981
|
+
return undefined;
|
|
982
|
+
}
|
|
983
|
+
return sign7702Authorization({
|
|
984
|
+
walletMetadata,
|
|
985
|
+
chainId,
|
|
986
|
+
rpcUrl,
|
|
987
|
+
password,
|
|
988
|
+
externalServerKeyShares
|
|
989
|
+
});
|
|
990
|
+
};
|
|
991
|
+
const signSponsoredTransaction = async ({ walletMetadata, calls, chainId, rpcUrl, authorization, autoDelegate = true, nonce: providedNonce, validForSeconds = DEFAULT_VALID_FOR_SECONDS, password, externalServerKeyShares, traceContext })=>{
|
|
992
|
+
const walletAddress = walletMetadata.accountAddress;
|
|
993
|
+
try {
|
|
994
|
+
const resolvedAuth = await resolveSponsoredAuthorization({
|
|
995
|
+
authorization,
|
|
996
|
+
autoDelegate,
|
|
997
|
+
walletMetadata,
|
|
998
|
+
chainId,
|
|
999
|
+
rpcUrl,
|
|
1000
|
+
password,
|
|
1001
|
+
externalServerKeyShares
|
|
1002
|
+
});
|
|
1003
|
+
const nonce = providedNonce != null ? providedNonce : await generateIntentNonce({
|
|
1004
|
+
walletAddress,
|
|
1005
|
+
chainId,
|
|
1006
|
+
rpcUrl
|
|
1007
|
+
});
|
|
1008
|
+
const deadline = BigInt(Math.floor(Date.now() / 1000) + validForSeconds);
|
|
1009
|
+
const { relayerAddress } = await getAvailableEvmGaslessRelayer({
|
|
1010
|
+
chainId,
|
|
1011
|
+
traceContext
|
|
1012
|
+
});
|
|
1013
|
+
const signature = await signTypedData({
|
|
1014
|
+
walletMetadata,
|
|
1015
|
+
password,
|
|
1016
|
+
externalServerKeyShares,
|
|
1017
|
+
typedData: {
|
|
1018
|
+
domain: {
|
|
1019
|
+
chainId,
|
|
1020
|
+
verifyingContract: DELEGATION_CONTRACT_ADDRESS
|
|
1021
|
+
},
|
|
1022
|
+
message: {
|
|
1023
|
+
calls: calls.map((c)=>({
|
|
1024
|
+
data: c.data,
|
|
1025
|
+
target: c.target,
|
|
1026
|
+
value: c.value
|
|
1027
|
+
})),
|
|
1028
|
+
deadline,
|
|
1029
|
+
mode: BATCH_CALL_OPDATA_AUTH_MODE,
|
|
1030
|
+
nonce,
|
|
1031
|
+
relayer: relayerAddress
|
|
1032
|
+
},
|
|
1033
|
+
primaryType: 'AuthorizedExecutions',
|
|
1034
|
+
types: AUTHORIZED_EXECUTIONS_TYPES
|
|
1035
|
+
}
|
|
1036
|
+
});
|
|
1037
|
+
return {
|
|
1038
|
+
authorization: resolvedAuth,
|
|
1039
|
+
calls,
|
|
1040
|
+
chainId,
|
|
1041
|
+
deadline: String(deadline),
|
|
1042
|
+
nonce: String(nonce),
|
|
1043
|
+
relayer: relayerAddress,
|
|
1044
|
+
signature,
|
|
1045
|
+
walletAddress
|
|
1046
|
+
};
|
|
1047
|
+
} catch (error) {
|
|
1048
|
+
logError$2({
|
|
1049
|
+
message: 'Error signing sponsored transaction',
|
|
1050
|
+
error: error,
|
|
1051
|
+
context: {
|
|
1052
|
+
accountAddress: walletAddress,
|
|
1053
|
+
chainId
|
|
1054
|
+
}
|
|
1055
|
+
});
|
|
1056
|
+
throw error;
|
|
1057
|
+
}
|
|
1058
|
+
};
|
|
1059
|
+
const getEVMSponsoredTransactionStatus = async ({ requestId, traceContext })=>{
|
|
1060
|
+
const result = await apiClient.getEVMSponsoredTransactionStatus({
|
|
1061
|
+
requestId,
|
|
1062
|
+
traceContext
|
|
1063
|
+
});
|
|
1064
|
+
return result;
|
|
1065
|
+
};
|
|
1066
|
+
const isSignedTransactionParams = (params)=>'signedTransaction' in params;
|
|
1067
|
+
const relaySponsoredTransaction = async (params)=>{
|
|
1068
|
+
try {
|
|
1069
|
+
const signed = isSignedTransactionParams(params) ? params.signedTransaction : await signSponsoredTransaction(params);
|
|
1070
|
+
const { requestId } = await apiClient.sponsorEVMTransaction({
|
|
1071
|
+
traceContext: params.traceContext,
|
|
1072
|
+
request: _extends({
|
|
1073
|
+
authorization: signed.authorization,
|
|
1074
|
+
calls: signed.calls.map((c)=>({
|
|
1075
|
+
data: c.data,
|
|
1076
|
+
target: c.target,
|
|
1077
|
+
value: String(c.value)
|
|
1078
|
+
})),
|
|
1079
|
+
chainId: signed.chainId,
|
|
1080
|
+
deadline: signed.deadline,
|
|
1081
|
+
nonce: signed.nonce,
|
|
1082
|
+
relayer: signed.relayer,
|
|
1083
|
+
signature: signed.signature,
|
|
1084
|
+
walletAddress: signed.walletAddress
|
|
1085
|
+
}, params.userId ? {
|
|
1086
|
+
userId: params.userId
|
|
1087
|
+
} : {})
|
|
1088
|
+
});
|
|
1089
|
+
if (!requestId) {
|
|
1090
|
+
throw new Error(ERROR_GASLESS_MISSING_REQUEST_ID);
|
|
1091
|
+
}
|
|
1092
|
+
return {
|
|
1093
|
+
requestId
|
|
1094
|
+
};
|
|
1095
|
+
} catch (error) {
|
|
1096
|
+
const { accountAddress, chainId } = isSignedTransactionParams(params) ? {
|
|
1097
|
+
accountAddress: params.signedTransaction.walletAddress,
|
|
1098
|
+
chainId: params.signedTransaction.chainId
|
|
1099
|
+
} : {
|
|
1100
|
+
accountAddress: params.walletMetadata.accountAddress,
|
|
1101
|
+
chainId: params.chainId
|
|
1102
|
+
};
|
|
1103
|
+
logError$2({
|
|
1104
|
+
message: 'Error relaying sponsored transaction',
|
|
1105
|
+
error: error,
|
|
1106
|
+
context: {
|
|
1107
|
+
accountAddress,
|
|
1108
|
+
chainId,
|
|
1109
|
+
userId: params.userId
|
|
1110
|
+
}
|
|
1111
|
+
});
|
|
1112
|
+
throw error;
|
|
1113
|
+
}
|
|
1114
|
+
};
|
|
1115
|
+
const waitForSponsoredTransaction = async ({ requestId, pollInterval = EVM_GASLESS_DEFAULT_POLL_INTERVAL_MS, timeout = EVM_GASLESS_DEFAULT_TIMEOUT_MS, traceContext })=>{
|
|
1116
|
+
try {
|
|
1117
|
+
// A zero or negative pollInterval makes attempts Infinity and turns this into a
|
|
1118
|
+
// tight loop hammering the status API, so clamp it to a sane minimum.
|
|
1119
|
+
const effectivePoll = Math.max(1, pollInterval);
|
|
1120
|
+
const attempts = Math.max(1, Math.floor(timeout / effectivePoll));
|
|
1121
|
+
for(let i = 0; i < attempts; i++){
|
|
1122
|
+
const r = await getEVMSponsoredTransactionStatus({
|
|
1123
|
+
requestId,
|
|
1124
|
+
traceContext
|
|
1125
|
+
});
|
|
1126
|
+
if (r.status === 'failure') {
|
|
1127
|
+
var _r_errorMessage;
|
|
1128
|
+
throw new Error(`${ERROR_GASLESS_RELAY_FAILED}: ${(_r_errorMessage = r.errorMessage) != null ? _r_errorMessage : 'unknown'}`);
|
|
1129
|
+
}
|
|
1130
|
+
if (r.transactionHash) {
|
|
1131
|
+
return {
|
|
1132
|
+
transactionHash: r.transactionHash
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
if (i < attempts - 1) {
|
|
1136
|
+
await new Promise((resolve)=>setTimeout(resolve, effectivePoll));
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
throw new Error(`${ERROR_GASLESS_TIMEOUT}: ${requestId}`);
|
|
1140
|
+
} catch (error) {
|
|
1141
|
+
logError$2({
|
|
1142
|
+
message: 'Error waiting for sponsored transaction',
|
|
1143
|
+
error: error,
|
|
1144
|
+
context: {
|
|
1145
|
+
requestId
|
|
1146
|
+
}
|
|
1147
|
+
});
|
|
1148
|
+
throw error;
|
|
1149
|
+
}
|
|
1150
|
+
};
|
|
1151
|
+
const sendSponsoredTransaction = async (params)=>{
|
|
1152
|
+
const { requestId } = await relaySponsoredTransaction(params);
|
|
1153
|
+
return waitForSponsoredTransaction({
|
|
1154
|
+
requestId,
|
|
1155
|
+
traceContext: params.traceContext
|
|
1156
|
+
});
|
|
1157
|
+
};
|
|
1158
|
+
return {
|
|
1159
|
+
getAvailableEvmGaslessRelayer,
|
|
1160
|
+
getEVMSponsoredTransactionStatus,
|
|
1161
|
+
is7702DelegationActive,
|
|
1162
|
+
relaySponsoredTransaction,
|
|
1163
|
+
sendSponsoredTransaction,
|
|
1164
|
+
sign7702Authorization,
|
|
1165
|
+
signSponsoredTransaction,
|
|
1166
|
+
waitForSponsoredTransaction
|
|
1167
|
+
};
|
|
1168
|
+
};
|
|
1169
|
+
|
|
1170
|
+
const logError$1 = node.createLogError('node-evm');
|
|
364
1171
|
class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
365
1172
|
get jwtAuthToken() {
|
|
366
1173
|
return this.baseJWTAuthToken;
|
|
@@ -375,10 +1182,10 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
375
1182
|
transport: viem.http(rpcUrl)
|
|
376
1183
|
});
|
|
377
1184
|
}
|
|
378
|
-
async getWalletClient({
|
|
1185
|
+
async getWalletClient({ walletMetadata, password, externalServerKeyShares, chain, chainId, rpcUrl }) {
|
|
379
1186
|
const account = createAccountAdapter({
|
|
380
1187
|
evmClient: this,
|
|
381
|
-
|
|
1188
|
+
walletMetadata,
|
|
382
1189
|
password,
|
|
383
1190
|
externalServerKeyShares
|
|
384
1191
|
});
|
|
@@ -419,95 +1226,82 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
419
1226
|
* @param thresholdSignatureScheme - The threshold signature scheme to use for the wallet.
|
|
420
1227
|
* @param password - The password to use for the wallet.
|
|
421
1228
|
* @param onError - The function to call if an error occurs.
|
|
422
|
-
* @param
|
|
1229
|
+
* @param backUpToDynamic - Whether to back up the external server key shares to the client share service. By default, it is false.
|
|
423
1230
|
* @returns The account address, public key, raw public key, external server key shares, and wallet id.
|
|
424
|
-
*/ async createWalletAccount({ thresholdSignatureScheme, password
|
|
1231
|
+
*/ async createWalletAccount({ thresholdSignatureScheme, password, onError, backUpToDynamic = false, returnEncryptedWalletApiKey }) {
|
|
425
1232
|
try {
|
|
426
|
-
|
|
427
|
-
const ceremonyCompletePromise = new Promise((resolve)=>{
|
|
428
|
-
ceremonyCeremonyCompleteResolver = resolve;
|
|
429
|
-
});
|
|
1233
|
+
const ceremony = this.createCeremonyCompletionTracker();
|
|
430
1234
|
// Generate key shares for given threshold signature scheme (TSS)
|
|
431
1235
|
const { rawPublicKey, externalServerKeyShares } = await this.keyGen({
|
|
432
1236
|
chainName: this.chainName,
|
|
433
1237
|
thresholdSignatureScheme,
|
|
434
1238
|
skipLock: true,
|
|
1239
|
+
password,
|
|
1240
|
+
backUpToDynamic,
|
|
1241
|
+
returnEncryptedWalletApiKey,
|
|
435
1242
|
onError,
|
|
436
|
-
onCeremonyComplete:
|
|
437
|
-
// update wallet map
|
|
438
|
-
const checksumAddress = viem.getAddress(accountAddress);
|
|
439
|
-
const chainConfig = node.getMPCChainConfig(this.chainName);
|
|
440
|
-
this.walletMap[checksumAddress] = _extends({}, this.walletMap[checksumAddress] || {}, {
|
|
441
|
-
accountAddress: checksumAddress,
|
|
442
|
-
walletId,
|
|
443
|
-
chainName: this.chainName,
|
|
444
|
-
thresholdSignatureScheme,
|
|
445
|
-
derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map((value, index)=>[
|
|
446
|
-
index,
|
|
447
|
-
value
|
|
448
|
-
]))),
|
|
449
|
-
externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
|
|
450
|
-
});
|
|
451
|
-
this.logger.debug('walletMap updated for wallet', {
|
|
452
|
-
context: {
|
|
453
|
-
accountAddress,
|
|
454
|
-
walletId,
|
|
455
|
-
walletMap: this.walletMap
|
|
456
|
-
}
|
|
457
|
-
});
|
|
458
|
-
ceremonyCeremonyCompleteResolver(undefined);
|
|
459
|
-
}
|
|
1243
|
+
onCeremonyComplete: ceremony.onCeremonyComplete
|
|
460
1244
|
});
|
|
461
1245
|
// Wait for the ceremony to complete before proceeding
|
|
462
|
-
await
|
|
463
|
-
|
|
1246
|
+
await ceremony.promise;
|
|
1247
|
+
const { walletId: resolvedWalletId, shareSetId: resolvedShareSetId } = ceremony.completion;
|
|
1248
|
+
if (!rawPublicKey || !externalServerKeyShares || !resolvedWalletId) {
|
|
464
1249
|
throw new Error(ERROR_KEYGEN_FAILED);
|
|
465
1250
|
}
|
|
466
1251
|
// Get EVM address from public key
|
|
467
1252
|
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
468
1253
|
rawPublicKey: rawPublicKey
|
|
469
1254
|
});
|
|
470
|
-
const
|
|
1255
|
+
const chainConfig = node.getMPCChainConfig(this.chainName);
|
|
1256
|
+
const walletMetadata = {
|
|
1257
|
+
walletId: resolvedWalletId,
|
|
1258
|
+
accountAddress,
|
|
1259
|
+
chainName: this.chainName,
|
|
1260
|
+
thresholdSignatureScheme,
|
|
1261
|
+
derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map((value, index)=>[
|
|
1262
|
+
index,
|
|
1263
|
+
value
|
|
1264
|
+
]))),
|
|
1265
|
+
shareSetId: resolvedShareSetId
|
|
1266
|
+
};
|
|
1267
|
+
const { keySharesWithBackupStatus, backupInfo } = await this.storeEncryptedBackupByWalletWithRetry({
|
|
471
1268
|
accountAddress,
|
|
472
1269
|
externalServerKeyShares,
|
|
473
1270
|
password,
|
|
474
|
-
|
|
1271
|
+
backUpToDynamic,
|
|
1272
|
+
walletMetadata
|
|
475
1273
|
});
|
|
1274
|
+
walletMetadata.externalServerKeySharesBackupInfo = backupInfo;
|
|
1275
|
+
var _ceremony_completion_encryptedWalletApiKey;
|
|
476
1276
|
return {
|
|
477
|
-
|
|
478
|
-
accountAddress,
|
|
1277
|
+
walletMetadata,
|
|
479
1278
|
rawPublicKey,
|
|
480
1279
|
publicKeyHex,
|
|
481
1280
|
externalServerKeyShares,
|
|
482
|
-
externalKeySharesWithBackupStatus
|
|
1281
|
+
externalKeySharesWithBackupStatus: keySharesWithBackupStatus,
|
|
1282
|
+
encryptedWalletApiKey: (_ceremony_completion_encryptedWalletApiKey = ceremony.completion.encryptedWalletApiKey) != null ? _ceremony_completion_encryptedWalletApiKey : undefined
|
|
483
1283
|
};
|
|
484
1284
|
} catch (error) {
|
|
485
|
-
logError({
|
|
1285
|
+
logError$1({
|
|
486
1286
|
message: ERROR_CREATE_WALLET_ACCOUNT,
|
|
487
1287
|
error: error,
|
|
488
1288
|
context: {}
|
|
489
1289
|
});
|
|
490
|
-
throw new Error(ERROR_CREATE_WALLET_ACCOUNT
|
|
1290
|
+
throw new Error(ERROR_CREATE_WALLET_ACCOUNT, {
|
|
1291
|
+
cause: error
|
|
1292
|
+
});
|
|
491
1293
|
}
|
|
492
1294
|
}
|
|
493
|
-
async signMessage({ message,
|
|
1295
|
+
async signMessage({ message, walletMetadata, password = undefined, externalServerKeyShares, context, onError }) {
|
|
1296
|
+
const { accountAddress } = walletMetadata;
|
|
494
1297
|
try {
|
|
495
1298
|
if (!accountAddress) {
|
|
496
1299
|
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
497
1300
|
}
|
|
498
|
-
// Format the message for EVM signing
|
|
499
1301
|
const formattedMessage = formatEVMMessage(message);
|
|
500
1302
|
const resolvedContext = context != null ? context : {
|
|
501
1303
|
evmMessage: message
|
|
502
1304
|
};
|
|
503
|
-
// Attempt to recover key shares from backup if not provided
|
|
504
|
-
await this.ensureKeySharesRecovered({
|
|
505
|
-
accountAddress,
|
|
506
|
-
password,
|
|
507
|
-
walletOperation: node.WalletOperation.SIGN_MESSAGE,
|
|
508
|
-
externalServerKeyShares,
|
|
509
|
-
errorMessage: keySharesRequiredFor('sign a message')
|
|
510
|
-
});
|
|
511
1305
|
const signatureEcdsa = await this.sign({
|
|
512
1306
|
message: formattedMessage,
|
|
513
1307
|
accountAddress: accountAddress,
|
|
@@ -515,33 +1309,28 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
515
1309
|
password,
|
|
516
1310
|
externalServerKeyShares,
|
|
517
1311
|
context: resolvedContext,
|
|
518
|
-
onError
|
|
1312
|
+
onError,
|
|
1313
|
+
walletMetadata
|
|
519
1314
|
});
|
|
520
|
-
|
|
521
|
-
const serializedSignature = serializeECDSASignature(signatureEcdsa);
|
|
522
|
-
return serializedSignature;
|
|
1315
|
+
return serializeECDSASignature(signatureEcdsa);
|
|
523
1316
|
} catch (error) {
|
|
524
|
-
logError({
|
|
1317
|
+
logError$1({
|
|
525
1318
|
message: ERROR_SIGN_MESSAGE,
|
|
526
1319
|
error: error,
|
|
527
1320
|
context: {
|
|
528
1321
|
accountAddress
|
|
529
1322
|
}
|
|
530
1323
|
});
|
|
531
|
-
throw new Error(ERROR_SIGN_MESSAGE
|
|
1324
|
+
throw new Error(ERROR_SIGN_MESSAGE, {
|
|
1325
|
+
cause: error
|
|
1326
|
+
});
|
|
532
1327
|
}
|
|
533
1328
|
}
|
|
534
1329
|
isSignAuthorizationSupported() {
|
|
535
1330
|
return true;
|
|
536
1331
|
}
|
|
537
|
-
async signPrehashedMessage({ prehashed,
|
|
538
|
-
|
|
539
|
-
accountAddress,
|
|
540
|
-
password,
|
|
541
|
-
walletOperation: node.WalletOperation.SIGN_MESSAGE,
|
|
542
|
-
externalServerKeyShares,
|
|
543
|
-
errorMessage: recoverErrorMessage
|
|
544
|
-
});
|
|
1332
|
+
async signPrehashedMessage({ prehashed, walletMetadata, password, externalServerKeyShares, context, onError }) {
|
|
1333
|
+
const { accountAddress } = walletMetadata;
|
|
545
1334
|
const signatureEcdsa = await this.sign({
|
|
546
1335
|
message: prehashed,
|
|
547
1336
|
accountAddress: accountAddress,
|
|
@@ -550,11 +1339,13 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
550
1339
|
externalServerKeyShares,
|
|
551
1340
|
isFormatted: true,
|
|
552
1341
|
context,
|
|
553
|
-
onError
|
|
1342
|
+
onError,
|
|
1343
|
+
walletMetadata
|
|
554
1344
|
});
|
|
555
1345
|
return serializeECDSASignature(signatureEcdsa);
|
|
556
1346
|
}
|
|
557
|
-
async signAuthorization({ authorization,
|
|
1347
|
+
async signAuthorization({ authorization, walletMetadata, password = undefined, externalServerKeyShares, onError }) {
|
|
1348
|
+
const { accountAddress } = walletMetadata;
|
|
558
1349
|
try {
|
|
559
1350
|
if (!accountAddress) {
|
|
560
1351
|
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
@@ -563,77 +1354,87 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
563
1354
|
const prehashed = digest.startsWith('0x') ? digest.slice(2) : digest;
|
|
564
1355
|
const serializedSignature = await this.signPrehashedMessage({
|
|
565
1356
|
prehashed,
|
|
566
|
-
|
|
1357
|
+
walletMetadata,
|
|
567
1358
|
password,
|
|
568
1359
|
externalServerKeyShares,
|
|
569
1360
|
context: {
|
|
570
1361
|
eip7702Auth: authorization
|
|
571
1362
|
},
|
|
572
|
-
onError
|
|
573
|
-
recoverErrorMessage: keySharesRequiredFor('sign authorization')
|
|
1363
|
+
onError
|
|
574
1364
|
});
|
|
575
1365
|
return viem.parseSignature(serializedSignature);
|
|
576
1366
|
} catch (error) {
|
|
577
|
-
logError({
|
|
1367
|
+
logError$1({
|
|
578
1368
|
message: ERROR_SIGN_MESSAGE,
|
|
579
1369
|
error: error,
|
|
580
1370
|
context: {
|
|
581
1371
|
accountAddress
|
|
582
1372
|
}
|
|
583
1373
|
});
|
|
584
|
-
throw new Error(ERROR_SIGN_MESSAGE
|
|
1374
|
+
throw new Error(ERROR_SIGN_MESSAGE, {
|
|
1375
|
+
cause: error
|
|
1376
|
+
});
|
|
585
1377
|
}
|
|
586
1378
|
}
|
|
587
|
-
|
|
1379
|
+
/**
|
|
1380
|
+
* Signs an already-hashed digest. Provide either `rawSign` — the pre-image the
|
|
1381
|
+
* digest comes from — or an explicit `context`, so a policy-enabled wallet can
|
|
1382
|
+
* evaluate the request.
|
|
1383
|
+
*/ async signRawMessage({ message, walletMetadata, password = undefined, externalServerKeyShares, context, rawSign, onError }) {
|
|
1384
|
+
const { accountAddress } = walletMetadata;
|
|
588
1385
|
try {
|
|
589
1386
|
if (!accountAddress) {
|
|
590
1387
|
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
591
1388
|
}
|
|
1389
|
+
const resolvedContext = node.resolveRawSignContext(context, rawSign);
|
|
592
1390
|
return await this.signPrehashedMessage({
|
|
593
1391
|
prehashed: node.stripHexPrefix(message),
|
|
594
|
-
|
|
1392
|
+
walletMetadata,
|
|
595
1393
|
password,
|
|
596
1394
|
externalServerKeyShares,
|
|
597
|
-
context,
|
|
598
|
-
onError
|
|
599
|
-
recoverErrorMessage: keySharesRequiredFor('sign a raw message')
|
|
1395
|
+
context: resolvedContext,
|
|
1396
|
+
onError
|
|
600
1397
|
});
|
|
601
1398
|
} catch (error) {
|
|
602
|
-
logError({
|
|
1399
|
+
logError$1({
|
|
603
1400
|
message: ERROR_SIGN_RAW_MESSAGE,
|
|
604
1401
|
error: error,
|
|
605
1402
|
context: {
|
|
606
1403
|
accountAddress
|
|
607
1404
|
}
|
|
608
1405
|
});
|
|
609
|
-
throw new Error(ERROR_SIGN_RAW_MESSAGE
|
|
1406
|
+
throw new Error(ERROR_SIGN_RAW_MESSAGE, {
|
|
1407
|
+
cause: error
|
|
1408
|
+
});
|
|
610
1409
|
}
|
|
611
1410
|
}
|
|
612
|
-
async signTypedData({
|
|
1411
|
+
async signTypedData({ walletMetadata, typedData, password = undefined, externalServerKeyShares, onError }) {
|
|
1412
|
+
const { accountAddress } = walletMetadata;
|
|
613
1413
|
try {
|
|
614
1414
|
if (!accountAddress) {
|
|
615
1415
|
throw new Error(ERROR_ACCOUNT_ADDRESS_REQUIRED);
|
|
616
1416
|
}
|
|
617
1417
|
return await this.signPrehashedMessage({
|
|
618
1418
|
prehashed: formatTypedData(typedData),
|
|
619
|
-
|
|
1419
|
+
walletMetadata,
|
|
620
1420
|
password,
|
|
621
1421
|
externalServerKeyShares,
|
|
622
1422
|
context: {
|
|
623
1423
|
evmTypedData: typedData
|
|
624
1424
|
},
|
|
625
|
-
onError
|
|
626
|
-
recoverErrorMessage: keySharesRequiredFor('sign typed data')
|
|
1425
|
+
onError
|
|
627
1426
|
});
|
|
628
1427
|
} catch (error) {
|
|
629
|
-
logError({
|
|
1428
|
+
logError$1({
|
|
630
1429
|
message: ERROR_SIGN_TYPED_DATA,
|
|
631
1430
|
error: error,
|
|
632
1431
|
context: {
|
|
633
1432
|
accountAddress
|
|
634
1433
|
}
|
|
635
1434
|
});
|
|
636
|
-
throw new Error(ERROR_SIGN_TYPED_DATA
|
|
1435
|
+
throw new Error(ERROR_SIGN_TYPED_DATA, {
|
|
1436
|
+
cause: error
|
|
1437
|
+
});
|
|
637
1438
|
}
|
|
638
1439
|
}
|
|
639
1440
|
async verifyMessageSignature({ accountAddress, message, signature }) {
|
|
@@ -649,7 +1450,7 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
649
1450
|
});
|
|
650
1451
|
return verified;
|
|
651
1452
|
} catch (error) {
|
|
652
|
-
logError({
|
|
1453
|
+
logError$1({
|
|
653
1454
|
message: ERROR_VERIFY_MESSAGE_SIGNATURE,
|
|
654
1455
|
error: error,
|
|
655
1456
|
context: {
|
|
@@ -659,31 +1460,25 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
659
1460
|
throw new Error(ERROR_VERIFY_MESSAGE_SIGNATURE);
|
|
660
1461
|
}
|
|
661
1462
|
}
|
|
662
|
-
async signTransaction({
|
|
663
|
-
|
|
664
|
-
accountAddress: senderAddress,
|
|
665
|
-
password
|
|
666
|
-
});
|
|
667
|
-
// Attempt to recover key shares from backup if not provided
|
|
668
|
-
await this.ensureKeySharesRecovered({
|
|
669
|
-
accountAddress: senderAddress,
|
|
670
|
-
password,
|
|
671
|
-
walletOperation: node.WalletOperation.SIGN_TRANSACTION,
|
|
672
|
-
externalServerKeyShares,
|
|
673
|
-
errorMessage: keySharesRequiredFor('sign transaction')
|
|
674
|
-
});
|
|
1463
|
+
async signTransaction({ walletMetadata, transaction, password = undefined, externalServerKeyShares, context }) {
|
|
1464
|
+
const { accountAddress } = walletMetadata;
|
|
675
1465
|
const serializedTx = viem.serializeTransaction(transaction);
|
|
676
1466
|
const serializedTxBytes = Uint8Array.from(Buffer.from(serializedTx.slice(2), 'hex'));
|
|
677
1467
|
if (!(serializedTxBytes instanceof Uint8Array)) {
|
|
678
|
-
throw new
|
|
1468
|
+
throw new TypeError('Invalid serializedTxBytes');
|
|
679
1469
|
}
|
|
680
1470
|
// Get signature using MPC (this will coordinate with server party)
|
|
681
1471
|
const signatureEcdsa = await this.sign({
|
|
682
1472
|
message: serializedTxBytes,
|
|
683
|
-
accountAddress
|
|
1473
|
+
accountAddress,
|
|
684
1474
|
chainName: this.chainName,
|
|
685
1475
|
password,
|
|
686
|
-
externalServerKeyShares
|
|
1476
|
+
externalServerKeyShares,
|
|
1477
|
+
walletMetadata,
|
|
1478
|
+
walletOperation: node.WalletOperation.SIGN_TRANSACTION,
|
|
1479
|
+
context: _extends({}, context, {
|
|
1480
|
+
evmTransaction: mapTransactionToEvmTransaction(transaction)
|
|
1481
|
+
})
|
|
687
1482
|
});
|
|
688
1483
|
if (!('r' in signatureEcdsa && 's' in signatureEcdsa && 'v' in signatureEcdsa)) {
|
|
689
1484
|
throw new Error('Invalid signature format returned from MPC signing');
|
|
@@ -699,40 +1494,35 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
699
1494
|
});
|
|
700
1495
|
return serializedSignedTx;
|
|
701
1496
|
} catch (error) {
|
|
702
|
-
logError({
|
|
1497
|
+
logError$1({
|
|
703
1498
|
message: 'Error signing transaction:',
|
|
704
1499
|
error: error,
|
|
705
1500
|
context: {
|
|
706
|
-
|
|
1501
|
+
accountAddress
|
|
707
1502
|
}
|
|
708
1503
|
});
|
|
709
1504
|
throw error;
|
|
710
1505
|
}
|
|
711
1506
|
}
|
|
712
|
-
async exportPrivateKey({
|
|
713
|
-
|
|
714
|
-
accountAddress,
|
|
715
|
-
password
|
|
716
|
-
});
|
|
717
|
-
// Attempt to recover key shares from backup if not provided
|
|
718
|
-
await this.ensureKeySharesRecovered({
|
|
719
|
-
accountAddress,
|
|
720
|
-
password,
|
|
721
|
-
walletOperation: node.WalletOperation.EXPORT_PRIVATE_KEY,
|
|
722
|
-
externalServerKeyShares,
|
|
723
|
-
errorMessage: keySharesRequiredFor('export private key')
|
|
724
|
-
});
|
|
1507
|
+
async exportPrivateKey({ walletMetadata, password = undefined, externalServerKeyShares }) {
|
|
1508
|
+
const { accountAddress } = walletMetadata;
|
|
725
1509
|
const { derivedPrivateKey } = await this.exportKey({
|
|
726
1510
|
accountAddress,
|
|
727
1511
|
chainName: this.chainName,
|
|
728
1512
|
password,
|
|
729
|
-
externalServerKeyShares
|
|
1513
|
+
externalServerKeyShares,
|
|
1514
|
+
walletMetadata
|
|
730
1515
|
});
|
|
731
1516
|
return {
|
|
732
1517
|
derivedPrivateKey
|
|
733
1518
|
};
|
|
734
1519
|
}
|
|
735
|
-
|
|
1520
|
+
/**
|
|
1521
|
+
* `derivationPath` is the JSON-serialized index map that
|
|
1522
|
+
* `walletMetadata.derivationPath` carries (e.g. `'{"0":44,"1":60,"2":0,"3":0,"4":0}'`
|
|
1523
|
+
* or `'[44,60,0,0,0]'`). BIP-32 strings such as `"m/44'/60'/0'/0/0"` are rejected.
|
|
1524
|
+
* Omit it to export without path derivation.
|
|
1525
|
+
*/ async offlineExportPrivateKey({ keyShares, derivationPath }) {
|
|
736
1526
|
const { derivedPrivateKey } = await this.offlineExportKey({
|
|
737
1527
|
chainName: this.chainName,
|
|
738
1528
|
keyShares,
|
|
@@ -749,37 +1539,28 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
749
1539
|
* @param thresholdSignatureScheme - The threshold signature scheme to use for the wallet.
|
|
750
1540
|
* @param password - The password to use for the wallet.
|
|
751
1541
|
* @param onError - The function to call if an error occurs.
|
|
752
|
-
* @param
|
|
1542
|
+
* @param backUpToDynamic - Whether to back up the external server key shares to the client share service. By default, it is false.
|
|
753
1543
|
* @returns The account address, public key, raw public key, external server key shares.
|
|
754
|
-
*/ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password
|
|
1544
|
+
*/ async importPrivateKey({ privateKey, chainName, thresholdSignatureScheme, password, backUpToDynamic = false, onError }) {
|
|
1545
|
+
let resolvedWalletId;
|
|
1546
|
+
let resolvedShareSetId;
|
|
755
1547
|
let ceremonyCeremonyCompleteResolver;
|
|
756
1548
|
const ceremonyCompletePromise = new Promise((resolve)=>{
|
|
757
1549
|
ceremonyCeremonyCompleteResolver = resolve;
|
|
758
1550
|
});
|
|
759
|
-
// TODO: validate private key for EVM
|
|
760
1551
|
const { rawPublicKey, externalServerKeyShares } = await this.importRawPrivateKey({
|
|
761
1552
|
chainName,
|
|
762
1553
|
privateKey,
|
|
763
1554
|
thresholdSignatureScheme,
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
accountAddress: checksumAddress,
|
|
770
|
-
walletId,
|
|
771
|
-
chainName: this.chainName,
|
|
772
|
-
thresholdSignatureScheme,
|
|
773
|
-
derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map((value, index)=>[
|
|
774
|
-
index,
|
|
775
|
-
value
|
|
776
|
-
]))),
|
|
777
|
-
externalServerKeySharesBackupInfo: node.getExternalServerKeyShareBackupInfo()
|
|
778
|
-
});
|
|
1555
|
+
password,
|
|
1556
|
+
backUpToDynamic,
|
|
1557
|
+
onCeremonyComplete: ({ walletId, shareSetId })=>{
|
|
1558
|
+
resolvedWalletId = walletId;
|
|
1559
|
+
resolvedShareSetId = shareSetId;
|
|
779
1560
|
ceremonyCeremonyCompleteResolver(undefined);
|
|
780
1561
|
},
|
|
781
1562
|
onError: (e)=>{
|
|
782
|
-
logError({
|
|
1563
|
+
logError$1({
|
|
783
1564
|
message: 'importPrivateKey: onError',
|
|
784
1565
|
error: e,
|
|
785
1566
|
context: {}
|
|
@@ -787,45 +1568,286 @@ class DynamicEvmWalletClient extends node.DynamicWalletClient {
|
|
|
787
1568
|
onError == null ? void 0 : onError(e);
|
|
788
1569
|
}
|
|
789
1570
|
});
|
|
790
|
-
// Wait for the ceremony to complete before proceeding
|
|
791
1571
|
await ceremonyCompletePromise;
|
|
792
|
-
if (!rawPublicKey || !externalServerKeyShares) {
|
|
1572
|
+
if (!rawPublicKey || !externalServerKeyShares || !resolvedWalletId) {
|
|
793
1573
|
throw new Error('Error creating wallet account');
|
|
794
1574
|
}
|
|
795
1575
|
const { accountAddress, publicKeyHex } = deriveAccountAddress({
|
|
796
1576
|
rawPublicKey: rawPublicKey
|
|
797
1577
|
});
|
|
798
|
-
const
|
|
1578
|
+
const chainConfig = node.getMPCChainConfig(this.chainName);
|
|
1579
|
+
const walletMetadata = {
|
|
1580
|
+
walletId: resolvedWalletId,
|
|
1581
|
+
accountAddress,
|
|
1582
|
+
chainName: this.chainName,
|
|
1583
|
+
thresholdSignatureScheme,
|
|
1584
|
+
derivationPath: JSON.stringify(Object.fromEntries(chainConfig.derivationPath.map((value, index)=>[
|
|
1585
|
+
index,
|
|
1586
|
+
value
|
|
1587
|
+
]))),
|
|
1588
|
+
shareSetId: resolvedShareSetId
|
|
1589
|
+
};
|
|
1590
|
+
const { keySharesWithBackupStatus, backupInfo } = await this.storeEncryptedBackupByWalletWithRetry({
|
|
799
1591
|
accountAddress,
|
|
800
1592
|
externalServerKeyShares,
|
|
801
1593
|
password,
|
|
802
|
-
|
|
1594
|
+
backUpToDynamic,
|
|
1595
|
+
walletMetadata
|
|
803
1596
|
});
|
|
1597
|
+
walletMetadata.externalServerKeySharesBackupInfo = backupInfo;
|
|
804
1598
|
return {
|
|
805
|
-
|
|
1599
|
+
walletMetadata,
|
|
806
1600
|
rawPublicKey,
|
|
807
1601
|
publicKeyHex,
|
|
808
1602
|
externalServerKeyShares,
|
|
809
|
-
externalKeySharesWithBackupStatus
|
|
1603
|
+
externalKeySharesWithBackupStatus: keySharesWithBackupStatus
|
|
810
1604
|
};
|
|
811
1605
|
}
|
|
812
|
-
async getEvmWallets() {
|
|
813
|
-
|
|
1606
|
+
/** @deprecated Use getWalletByAddress instead for better performance */ async getEvmWallets() {
|
|
1607
|
+
// Calling the deprecated getWallets() is the point of this shim, and it is
|
|
1608
|
+
// public and overridable, so routing around it would drop a consumer's override.
|
|
1609
|
+
const wallets = await this.getWallets(); // NOSONAR
|
|
814
1610
|
const evmWallets = wallets.filter((wallet)=>wallet.chainName === 'EVM');
|
|
815
1611
|
return evmWallets;
|
|
816
1612
|
}
|
|
817
|
-
|
|
1613
|
+
// ---------------------------------------------------------------------------
|
|
1614
|
+
// EVM gasless (Fireblocks-relayer native gasless via EIP-7702 / AuthorizedExecutions)
|
|
1615
|
+
// ---------------------------------------------------------------------------
|
|
1616
|
+
get evmGasless() {
|
|
1617
|
+
return createEvmGaslessClient({
|
|
1618
|
+
apiClient: this.apiClient,
|
|
1619
|
+
createViemPublicClient: (args)=>this.createViemPublicClient(args),
|
|
1620
|
+
signAuthorization: (args)=>this.signAuthorization(args),
|
|
1621
|
+
signTypedData: (args)=>this.signTypedData(args)
|
|
1622
|
+
});
|
|
1623
|
+
}
|
|
1624
|
+
/**
|
|
1625
|
+
* Checks whether the given wallet address has an active EIP-7702 delegation
|
|
1626
|
+
* to the Universal Gasless Delegate (UGD) contract.
|
|
1627
|
+
*/ async is7702DelegationActive(params) {
|
|
1628
|
+
return this.evmGasless.is7702DelegationActive(params);
|
|
1629
|
+
}
|
|
1630
|
+
/**
|
|
1631
|
+
* Signs an EIP-7702 authorization that delegates the EOA to the UGD contract.
|
|
1632
|
+
* The EOA nonce is fetched from the chain unless explicitly provided.
|
|
1633
|
+
*/ async sign7702Authorization(params) {
|
|
1634
|
+
return this.evmGasless.sign7702Authorization(params);
|
|
1635
|
+
}
|
|
1636
|
+
/**
|
|
1637
|
+
* Fetches the available gasless relayer address for a given chain.
|
|
1638
|
+
*/ async getAvailableEvmGaslessRelayer(params) {
|
|
1639
|
+
return this.evmGasless.getAvailableEvmGaslessRelayer(params);
|
|
1640
|
+
}
|
|
1641
|
+
/**
|
|
1642
|
+
* Signs a sponsored transaction intent: resolves 7702 authorization, generates
|
|
1643
|
+
* a bitmap nonce, fetches the relayer, and MPC-signs the EIP-712 typed data.
|
|
1644
|
+
*/ async signSponsoredTransaction(params) {
|
|
1645
|
+
return this.evmGasless.signSponsoredTransaction(params);
|
|
1646
|
+
}
|
|
1647
|
+
/**
|
|
1648
|
+
* Returns the current status of an EVM sponsored transaction relay request.
|
|
1649
|
+
*/ async getEVMSponsoredTransactionStatus(params) {
|
|
1650
|
+
return this.evmGasless.getEVMSponsoredTransactionStatus(params);
|
|
1651
|
+
}
|
|
1652
|
+
/**
|
|
1653
|
+
* Relays a sponsored transaction to the Dynamic API.
|
|
1654
|
+
* Accepts either a pre-signed transaction or parameters to sign first.
|
|
1655
|
+
*/ async relaySponsoredTransaction(params) {
|
|
1656
|
+
return this.evmGasless.relaySponsoredTransaction(params);
|
|
1657
|
+
}
|
|
1658
|
+
/**
|
|
1659
|
+
* Relay then poll to completion. Fixed 60s/2s default (matches js-sdk).
|
|
1660
|
+
* For a custom timeout, use relaySponsoredTransaction + waitForSponsoredTransaction.
|
|
1661
|
+
*/ async sendSponsoredTransaction(params) {
|
|
1662
|
+
return this.evmGasless.sendSponsoredTransaction(params);
|
|
1663
|
+
}
|
|
1664
|
+
/**
|
|
1665
|
+
* Polls the relay status until a terminal state (success or failure) is reached.
|
|
1666
|
+
*/ async waitForSponsoredTransaction(params) {
|
|
1667
|
+
return this.evmGasless.waitForSponsoredTransaction(params);
|
|
1668
|
+
}
|
|
1669
|
+
// ---------------------------------------------------------------------------
|
|
1670
|
+
// Earn (yield vaults)
|
|
1671
|
+
// ---------------------------------------------------------------------------
|
|
1672
|
+
get evmEarn() {
|
|
1673
|
+
return createEvmEarnClient({
|
|
1674
|
+
getEarnClient: ()=>this.earn,
|
|
1675
|
+
createViemPublicClient: (args)=>this.createViemPublicClient(args),
|
|
1676
|
+
getWalletClient: (args)=>this.getWalletClient(args),
|
|
1677
|
+
sendSponsoredTransaction: (args)=>this.sendSponsoredTransaction(args)
|
|
1678
|
+
});
|
|
1679
|
+
}
|
|
1680
|
+
/**
|
|
1681
|
+
* Executes an Earn signing payload returned by the prepare* methods, sending the
|
|
1682
|
+
* ERC-20 approval first when the vault requires one.
|
|
1683
|
+
*/ async executeEarnTransaction(params) {
|
|
1684
|
+
return this.evmEarn.executeEarnTransaction(params);
|
|
1685
|
+
}
|
|
1686
|
+
/** Prepares and executes a deposit into an Earn vault. */ async depositToEarnVault(params) {
|
|
1687
|
+
return this.evmEarn.depositToEarnVault(params);
|
|
1688
|
+
}
|
|
1689
|
+
/** Prepares and executes a withdrawal from an Earn vault. */ async withdrawFromEarnVault(params) {
|
|
1690
|
+
return this.evmEarn.withdrawFromEarnVault(params);
|
|
1691
|
+
}
|
|
1692
|
+
/** Prepares and executes a claim of accrued Earn vault rewards. */ async claimEarnRewards(params) {
|
|
1693
|
+
return this.evmEarn.claimEarnRewards(params);
|
|
1694
|
+
}
|
|
1695
|
+
constructor({ environmentId, baseApiUrl, baseMPCRelayApiUrl, baseClientKeysharesRelayApiUrl, debug, enableMPCAccelerator, logger, requestSigning }){
|
|
818
1696
|
super({
|
|
819
1697
|
environmentId,
|
|
820
1698
|
baseApiUrl,
|
|
821
1699
|
baseMPCRelayApiUrl,
|
|
1700
|
+
baseClientKeysharesRelayApiUrl,
|
|
822
1701
|
debug,
|
|
823
1702
|
enableMPCAccelerator,
|
|
824
|
-
logger
|
|
1703
|
+
logger,
|
|
1704
|
+
requestSigning
|
|
825
1705
|
}), this.chainName = 'EVM';
|
|
826
1706
|
}
|
|
827
1707
|
}
|
|
828
1708
|
|
|
1709
|
+
const logError = node.createLogError('node-evm');
|
|
1710
|
+
const ERROR_DELEGATED_GASLESS_CREDENTIALS_REQUIRED = 'Delegated key share, wallet ID, and wallet API key are required to sponsor a transaction';
|
|
1711
|
+
const ERROR_DELEGATED_GASLESS_USER_ID_REQUIRED = 'userId is required — a delegated wallet is always owned by an end user, so a sponsored transaction cannot be attributed to the calling service';
|
|
1712
|
+
const createViemPublicClient = ({ chain, rpcUrl })=>viem.createPublicClient({
|
|
1713
|
+
chain,
|
|
1714
|
+
transport: viem.http(rpcUrl)
|
|
1715
|
+
});
|
|
1716
|
+
const signerArgs = ({ keyShare, onError, shareSetId, walletApiKey, walletId })=>({
|
|
1717
|
+
keyShare,
|
|
1718
|
+
onError,
|
|
1719
|
+
shareSetId,
|
|
1720
|
+
walletApiKey,
|
|
1721
|
+
walletId
|
|
1722
|
+
});
|
|
1723
|
+
const gaslessFor = (client, apiClient, wallet)=>createEvmGaslessClient({
|
|
1724
|
+
apiClient,
|
|
1725
|
+
createViemPublicClient,
|
|
1726
|
+
signAuthorization: ({ authorization })=>delegatedSignAuthorization(client, _extends({}, signerArgs(wallet), {
|
|
1727
|
+
authorization
|
|
1728
|
+
})),
|
|
1729
|
+
signTypedData: ({ typedData })=>delegatedSignTypedData(client, _extends({}, signerArgs(wallet), {
|
|
1730
|
+
typedData
|
|
1731
|
+
}))
|
|
1732
|
+
});
|
|
1733
|
+
const walletRef = ({ walletAddress })=>({
|
|
1734
|
+
accountAddress: walletAddress
|
|
1735
|
+
});
|
|
1736
|
+
const assertDelegatedWallet = (wallet)=>{
|
|
1737
|
+
if (!wallet.keyShare || !wallet.walletId || !wallet.walletApiKey) {
|
|
1738
|
+
throw new Error(ERROR_DELEGATED_GASLESS_CREDENTIALS_REQUIRED);
|
|
1739
|
+
}
|
|
1740
|
+
if (!wallet.userId) {
|
|
1741
|
+
throw new Error(ERROR_DELEGATED_GASLESS_USER_ID_REQUIRED);
|
|
1742
|
+
}
|
|
1743
|
+
};
|
|
1744
|
+
const splitWallet = (params)=>{
|
|
1745
|
+
const { keyShare, onError, shareSetId, userId, walletAddress, walletApiKey, walletId } = params, intent = _object_without_properties_loose(params, [
|
|
1746
|
+
"keyShare",
|
|
1747
|
+
"onError",
|
|
1748
|
+
"shareSetId",
|
|
1749
|
+
"userId",
|
|
1750
|
+
"walletAddress",
|
|
1751
|
+
"walletApiKey",
|
|
1752
|
+
"walletId"
|
|
1753
|
+
]);
|
|
1754
|
+
return {
|
|
1755
|
+
intent,
|
|
1756
|
+
wallet: {
|
|
1757
|
+
keyShare,
|
|
1758
|
+
onError,
|
|
1759
|
+
shareSetId,
|
|
1760
|
+
userId,
|
|
1761
|
+
walletAddress,
|
|
1762
|
+
walletApiKey,
|
|
1763
|
+
walletId
|
|
1764
|
+
}
|
|
1765
|
+
};
|
|
1766
|
+
};
|
|
1767
|
+
/**
|
|
1768
|
+
* Signs an EIP-7702 authorization delegating the end user's EOA to the UGD
|
|
1769
|
+
* contract, using the customer-held key share.
|
|
1770
|
+
*/ const delegatedSign7702Authorization = async (client, params)=>{
|
|
1771
|
+
const { chainId, nonce, rpcUrl } = params, wallet = _object_without_properties_loose(params, [
|
|
1772
|
+
"chainId",
|
|
1773
|
+
"nonce",
|
|
1774
|
+
"rpcUrl"
|
|
1775
|
+
]);
|
|
1776
|
+
assertDelegatedWallet(wallet);
|
|
1777
|
+
try {
|
|
1778
|
+
// The EOA nonce comes from the RPC and the signature from the delegated MPC
|
|
1779
|
+
// room, so this path makes no Dynamic-authenticated call.
|
|
1780
|
+
return await gaslessFor(client, client.createApiClient(), wallet).sign7702Authorization({
|
|
1781
|
+
chainId,
|
|
1782
|
+
nonce,
|
|
1783
|
+
rpcUrl,
|
|
1784
|
+
walletMetadata: walletRef(wallet)
|
|
1785
|
+
});
|
|
1786
|
+
} catch (error) {
|
|
1787
|
+
logError({
|
|
1788
|
+
message: 'Error in delegatedSign7702Authorization',
|
|
1789
|
+
error: error,
|
|
1790
|
+
context: {
|
|
1791
|
+
walletId: wallet.walletId
|
|
1792
|
+
}
|
|
1793
|
+
});
|
|
1794
|
+
throw error;
|
|
1795
|
+
}
|
|
1796
|
+
};
|
|
1797
|
+
/**
|
|
1798
|
+
* Signs the EIP-712 AuthorizedExecutions intent for a batch of calls, resolving
|
|
1799
|
+
* a first-use 7702 authorization along the way when the wallet is not yet delegated.
|
|
1800
|
+
*
|
|
1801
|
+
* The returned payload can be relayed later with `sendSponsoredTransaction` on a
|
|
1802
|
+
* `DynamicEvmWalletClient`, which needs no key share to submit an already-signed intent.
|
|
1803
|
+
*/ const delegatedSignSponsoredTransaction = async (client, params)=>{
|
|
1804
|
+
const { intent, wallet } = splitWallet(params);
|
|
1805
|
+
assertDelegatedWallet(wallet);
|
|
1806
|
+
try {
|
|
1807
|
+
return await node.withDelegatedServerAuth(client, (apiClient)=>gaslessFor(client, apiClient, wallet).signSponsoredTransaction(_extends({}, intent, {
|
|
1808
|
+
walletMetadata: walletRef(wallet)
|
|
1809
|
+
})));
|
|
1810
|
+
} catch (error) {
|
|
1811
|
+
logError({
|
|
1812
|
+
message: 'Error in delegatedSignSponsoredTransaction',
|
|
1813
|
+
error: error,
|
|
1814
|
+
context: {
|
|
1815
|
+
walletId: wallet.walletId
|
|
1816
|
+
}
|
|
1817
|
+
});
|
|
1818
|
+
throw error;
|
|
1819
|
+
}
|
|
1820
|
+
};
|
|
1821
|
+
/**
|
|
1822
|
+
* Signs, relays, and waits for a sponsored transaction on behalf of the wallet's
|
|
1823
|
+
* owner, resolving once the relayer reports an on-chain hash.
|
|
1824
|
+
*/ const delegatedSendSponsoredTransaction = async (client, params)=>{
|
|
1825
|
+
const { intent, wallet } = splitWallet(params);
|
|
1826
|
+
assertDelegatedWallet(wallet);
|
|
1827
|
+
try {
|
|
1828
|
+
// Relaying and polling each take their own server-auth JWT. Sharing one would
|
|
1829
|
+
// put the whole poll window inside a single retry scope, so a 401 on a late
|
|
1830
|
+
// poll would replay the relay and sponsor the transaction twice.
|
|
1831
|
+
const { requestId } = await node.withDelegatedServerAuth(client, (apiClient)=>gaslessFor(client, apiClient, wallet).relaySponsoredTransaction(_extends({}, intent, {
|
|
1832
|
+
userId: wallet.userId,
|
|
1833
|
+
walletMetadata: walletRef(wallet)
|
|
1834
|
+
})));
|
|
1835
|
+
return await node.withDelegatedServerAuth(client, (apiClient)=>gaslessFor(client, apiClient, wallet).waitForSponsoredTransaction({
|
|
1836
|
+
requestId,
|
|
1837
|
+
traceContext: intent.traceContext
|
|
1838
|
+
}));
|
|
1839
|
+
} catch (error) {
|
|
1840
|
+
logError({
|
|
1841
|
+
message: 'Error in delegatedSendSponsoredTransaction',
|
|
1842
|
+
error: error,
|
|
1843
|
+
context: {
|
|
1844
|
+
walletId: wallet.walletId
|
|
1845
|
+
}
|
|
1846
|
+
});
|
|
1847
|
+
throw error;
|
|
1848
|
+
}
|
|
1849
|
+
};
|
|
1850
|
+
|
|
829
1851
|
const getJwtExpiration = (jwt)=>{
|
|
830
1852
|
if (!jwt) {
|
|
831
1853
|
return 0;
|
|
@@ -891,7 +1913,7 @@ class DynamicEvmZeroDevClient {
|
|
|
891
1913
|
async createKernelClientForAddress(options) {
|
|
892
1914
|
const viemSigner = createAccountAdapter({
|
|
893
1915
|
evmClient: this.evmClient,
|
|
894
|
-
|
|
1916
|
+
walletMetadata: options.walletMetadata,
|
|
895
1917
|
password: options.password,
|
|
896
1918
|
externalServerKeyShares: options.externalServerKeyShares,
|
|
897
1919
|
delegated: options.delegated
|
|
@@ -1009,25 +2031,51 @@ const createZerodevClient = async (evmClient)=>{
|
|
|
1009
2031
|
return client;
|
|
1010
2032
|
};
|
|
1011
2033
|
|
|
2034
|
+
exports.AUTHORIZED_EXECUTIONS_TYPES = AUTHORIZED_EXECUTIONS_TYPES;
|
|
2035
|
+
exports.BATCH_CALL_OPDATA_AUTH_MODE = BATCH_CALL_OPDATA_AUTH_MODE;
|
|
2036
|
+
exports.DEFAULT_VALID_FOR_SECONDS = DEFAULT_VALID_FOR_SECONDS;
|
|
2037
|
+
exports.DELEGATION_CODE_PREFIX = DELEGATION_CODE_PREFIX;
|
|
2038
|
+
exports.DELEGATION_CONTRACT_ADDRESS = DELEGATION_CONTRACT_ADDRESS;
|
|
1012
2039
|
exports.DynamicEvmWalletClient = DynamicEvmWalletClient;
|
|
1013
2040
|
exports.ERROR_ACCOUNT_ADDRESS_REQUIRED = ERROR_ACCOUNT_ADDRESS_REQUIRED;
|
|
1014
2041
|
exports.ERROR_CREATE_WALLET_ACCOUNT = ERROR_CREATE_WALLET_ACCOUNT;
|
|
2042
|
+
exports.ERROR_DELEGATED_GASLESS_CREDENTIALS_REQUIRED = ERROR_DELEGATED_GASLESS_CREDENTIALS_REQUIRED;
|
|
2043
|
+
exports.ERROR_DELEGATED_GASLESS_USER_ID_REQUIRED = ERROR_DELEGATED_GASLESS_USER_ID_REQUIRED;
|
|
2044
|
+
exports.ERROR_EARN_APPROVAL_FAILED = ERROR_EARN_APPROVAL_FAILED;
|
|
2045
|
+
exports.ERROR_EARN_CHAIN_MISMATCH = ERROR_EARN_CHAIN_MISMATCH;
|
|
2046
|
+
exports.ERROR_EARN_CHAIN_REQUIRED = ERROR_EARN_CHAIN_REQUIRED;
|
|
2047
|
+
exports.ERROR_EARN_MISSING_EVM_TRANSACTION = ERROR_EARN_MISSING_EVM_TRANSACTION;
|
|
2048
|
+
exports.ERROR_EARN_NOT_EVM_WALLET = ERROR_EARN_NOT_EVM_WALLET;
|
|
2049
|
+
exports.ERROR_GASLESS_MISSING_REQUEST_ID = ERROR_GASLESS_MISSING_REQUEST_ID;
|
|
2050
|
+
exports.ERROR_GASLESS_MISSING_YPARITY = ERROR_GASLESS_MISSING_YPARITY;
|
|
2051
|
+
exports.ERROR_GASLESS_RELAY_FAILED = ERROR_GASLESS_RELAY_FAILED;
|
|
2052
|
+
exports.ERROR_GASLESS_RPC_REQUIRED = ERROR_GASLESS_RPC_REQUIRED;
|
|
2053
|
+
exports.ERROR_GASLESS_RPC_REQUIRED_FOR_AUTO_DELEGATE = ERROR_GASLESS_RPC_REQUIRED_FOR_AUTO_DELEGATE;
|
|
2054
|
+
exports.ERROR_GASLESS_TIMEOUT = ERROR_GASLESS_TIMEOUT;
|
|
1015
2055
|
exports.ERROR_KEYGEN_FAILED = ERROR_KEYGEN_FAILED;
|
|
1016
2056
|
exports.ERROR_SIGN_MESSAGE = ERROR_SIGN_MESSAGE;
|
|
1017
2057
|
exports.ERROR_SIGN_RAW_MESSAGE = ERROR_SIGN_RAW_MESSAGE;
|
|
1018
2058
|
exports.ERROR_SIGN_TYPED_DATA = ERROR_SIGN_TYPED_DATA;
|
|
1019
2059
|
exports.ERROR_VERIFY_MESSAGE_SIGNATURE = ERROR_VERIFY_MESSAGE_SIGNATURE;
|
|
2060
|
+
exports.EVM_GASLESS_DEFAULT_POLL_INTERVAL_MS = EVM_GASLESS_DEFAULT_POLL_INTERVAL_MS;
|
|
2061
|
+
exports.EVM_GASLESS_DEFAULT_TIMEOUT_MS = EVM_GASLESS_DEFAULT_TIMEOUT_MS;
|
|
1020
2062
|
exports.EVM_SIGN_MESSAGE_PREFIX = EVM_SIGN_MESSAGE_PREFIX;
|
|
2063
|
+
exports.UGD_ABI = UGD_ABI;
|
|
1021
2064
|
exports.createAccountAdapter = createAccountAdapter;
|
|
1022
2065
|
exports.createDelegatedEvmWalletClient = createDelegatedEvmWalletClient;
|
|
2066
|
+
exports.createEvmEarnClient = createEvmEarnClient;
|
|
1023
2067
|
exports.createZerodevClient = createZerodevClient;
|
|
2068
|
+
exports.delegatedSendSponsoredTransaction = delegatedSendSponsoredTransaction;
|
|
2069
|
+
exports.delegatedSign7702Authorization = delegatedSign7702Authorization;
|
|
1024
2070
|
exports.delegatedSignAuthorization = delegatedSignAuthorization;
|
|
1025
2071
|
exports.delegatedSignMessage = delegatedSignMessage;
|
|
1026
2072
|
exports.delegatedSignRawMessage = delegatedSignRawMessage;
|
|
2073
|
+
exports.delegatedSignSponsoredTransaction = delegatedSignSponsoredTransaction;
|
|
1027
2074
|
exports.delegatedSignTransaction = delegatedSignTransaction;
|
|
1028
2075
|
exports.delegatedSignTypedData = delegatedSignTypedData;
|
|
1029
2076
|
exports.deriveAccountAddress = deriveAccountAddress;
|
|
1030
2077
|
exports.formatEVMMessage = formatEVMMessage;
|
|
1031
2078
|
exports.formatTypedData = formatTypedData;
|
|
2079
|
+
exports.mapTransactionToEvmTransaction = mapTransactionToEvmTransaction;
|
|
1032
2080
|
exports.revokeDelegation = revokeDelegation;
|
|
1033
2081
|
exports.serializeECDSASignature = serializeECDSASignature;
|