@inco/shield-js 0.1.0 → 0.2.0
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/README.md +52 -308
- package/dist/contracts/abi.d.ts +2000 -4513
- package/dist/contracts/abi.js +2400 -5651
- package/dist/contracts/index.d.ts +2 -2
- package/dist/contracts/index.js +2 -2
- package/dist/contracts/utils.d.ts +5 -24
- package/dist/contracts/utils.js +13 -24
- package/dist/errors.d.ts +16 -47
- package/dist/errors.js +6 -53
- package/dist/generated/inco/shield/v2/conductor_pb.d.ts +77 -0
- package/dist/generated/inco/shield/v2/conductor_pb.js +6 -0
- package/dist/generated/inco/shield/v2/deposit_pb.d.ts +12 -103
- package/dist/generated/inco/shield/v2/deposit_pb.js +4 -141
- package/dist/generated/inco/shield/v2/drafting_pb.d.ts +45 -269
- package/dist/generated/inco/shield/v2/drafting_pb.js +8 -361
- package/dist/generated/inco/shield/v2/types_pb.d.ts +47 -166
- package/dist/generated/inco/shield/v2/types_pb.js +12 -261
- package/dist/index.d.ts +1 -7
- package/dist/index.js +1 -14
- package/dist/shield/abi.d.ts +5 -0
- package/dist/shield/abi.js +28 -0
- package/dist/shield/client.d.ts +27 -51
- package/dist/shield/client.js +73 -65
- package/dist/shield/convert.d.ts +2 -14
- package/dist/shield/convert.js +39 -115
- package/dist/shield/encryption.d.ts +0 -67
- package/dist/shield/encryption.js +0 -67
- package/dist/shield/envelope.d.ts +10 -11
- package/dist/shield/envelope.js +15 -14
- package/dist/shield/fee.d.ts +24 -0
- package/dist/shield/fee.js +151 -0
- package/dist/shield/index.d.ts +9 -3
- package/dist/shield/index.js +7 -2
- package/dist/shield/intent.d.ts +20 -933
- package/dist/shield/intent.js +92 -112
- package/dist/shield/permission-status.d.ts +9 -0
- package/dist/shield/permission-status.js +8 -0
- package/dist/shield/rpc.d.ts +50 -49
- package/dist/shield/rpc.js +207 -354
- package/dist/shield/shield.eip712.gen.d.ts +1096 -43
- package/dist/shield/shield.eip712.gen.js +572 -62
- package/dist/shield/types.d.ts +7 -102
- package/dist/shield/types.js +1 -4
- package/dist/shield/userop/gas-estimation.d.ts +7 -22
- package/dist/shield/userop/gas-estimation.js +100 -137
- package/dist/shield/userop/paymaster.d.ts +15 -0
- package/dist/shield/userop/paymaster.js +17 -0
- package/dist/shield/userop/token-exchange.d.ts +5 -52
- package/dist/shield/userop/token-exchange.js +41 -149
- package/dist/shield/userop/types.d.ts +2 -27
- package/dist/shield/userop/types.js +0 -3
- package/dist/shield/userop/userOp.d.ts +0 -19
- package/dist/shield/userop/userOp.js +4 -22
- package/dist/shield/utils/chain.d.ts +1 -0
- package/dist/shield/utils/chain.js +3 -0
- package/dist/shield/validate-withdrawal-split.d.ts +13 -0
- package/dist/shield/validate-withdrawal-split.js +93 -0
- package/package.json +18 -27
- package/dist/generated/inco/shield/v2/conductor_connect.d.ts +0 -137
- package/dist/generated/inco/shield/v2/conductor_connect.js +0 -141
- package/dist/generated/inco/shield/v2/permission_pb.d.ts +0 -443
- package/dist/generated/inco/shield/v2/permission_pb.js +0 -639
- package/dist/generated/inco/shield/v2/query_pb.d.ts +0 -103
- package/dist/generated/inco/shield/v2/query_pb.js +0 -141
- package/dist/shield/userop/index.d.ts +0 -1
- package/dist/shield/userop/index.js +0 -1
- package/dist/uniswap-adapter/config.d.ts +0 -34
- package/dist/uniswap-adapter/config.js +0 -45
- package/dist/uniswap-adapter/find-pools.d.ts +0 -91
- package/dist/uniswap-adapter/find-pools.js +0 -108
- package/dist/uniswap-adapter/index.d.ts +0 -10
- package/dist/uniswap-adapter/index.js +0 -13
- package/dist/uniswap-adapter/swap.d.ts +0 -344
- package/dist/uniswap-adapter/swap.js +0 -309
|
@@ -1,344 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* UniswapV4 swap functionality for IncoShield
|
|
3
|
-
*
|
|
4
|
-
* Provides functions to:
|
|
5
|
-
* - Build V4 swap actions
|
|
6
|
-
* - Encode swap arguments
|
|
7
|
-
* - Execute swaps through the IncoShield DeFi adapter
|
|
8
|
-
*/
|
|
9
|
-
import type { Account, Address, Chain, Hex, LocalAccount, Transport } from 'viem';
|
|
10
|
-
import { type PublicClient, type WalletClient } from 'viem';
|
|
11
|
-
import type { PoolKey } from './find-pools.js';
|
|
12
|
-
declare const ERC20_ABI: readonly [{
|
|
13
|
-
readonly name: "approve";
|
|
14
|
-
readonly type: "function";
|
|
15
|
-
readonly stateMutability: "nonpayable";
|
|
16
|
-
readonly inputs: readonly [{
|
|
17
|
-
readonly type: "address";
|
|
18
|
-
readonly name: "spender";
|
|
19
|
-
}, {
|
|
20
|
-
readonly type: "uint256";
|
|
21
|
-
readonly name: "amount";
|
|
22
|
-
}];
|
|
23
|
-
readonly outputs: readonly [{
|
|
24
|
-
readonly type: "bool";
|
|
25
|
-
}];
|
|
26
|
-
}, {
|
|
27
|
-
readonly name: "balanceOf";
|
|
28
|
-
readonly type: "function";
|
|
29
|
-
readonly stateMutability: "view";
|
|
30
|
-
readonly inputs: readonly [{
|
|
31
|
-
readonly type: "address";
|
|
32
|
-
readonly name: "account";
|
|
33
|
-
}];
|
|
34
|
-
readonly outputs: readonly [{
|
|
35
|
-
readonly type: "uint256";
|
|
36
|
-
}];
|
|
37
|
-
}];
|
|
38
|
-
declare const INCO_SHIELD_ABI: readonly [{
|
|
39
|
-
readonly name: "DepositSettled";
|
|
40
|
-
readonly type: "event";
|
|
41
|
-
readonly inputs: readonly [{
|
|
42
|
-
readonly type: "bytes32";
|
|
43
|
-
readonly name: "stateAfterDeposit";
|
|
44
|
-
readonly indexed: true;
|
|
45
|
-
}];
|
|
46
|
-
}, {
|
|
47
|
-
readonly name: "deposit";
|
|
48
|
-
readonly type: "function";
|
|
49
|
-
readonly stateMutability: "nonpayable";
|
|
50
|
-
readonly inputs: readonly [{
|
|
51
|
-
readonly type: "tuple";
|
|
52
|
-
readonly components: readonly [{
|
|
53
|
-
readonly type: "bytes";
|
|
54
|
-
readonly name: "toCiphertext";
|
|
55
|
-
}, {
|
|
56
|
-
readonly type: "address";
|
|
57
|
-
readonly name: "from";
|
|
58
|
-
}, {
|
|
59
|
-
readonly type: "tuple";
|
|
60
|
-
readonly components: readonly [{
|
|
61
|
-
readonly type: "address";
|
|
62
|
-
readonly name: "assetAdapter";
|
|
63
|
-
}, {
|
|
64
|
-
readonly type: "bytes";
|
|
65
|
-
readonly name: "assetIdentificationArgs";
|
|
66
|
-
}, {
|
|
67
|
-
readonly type: "bytes";
|
|
68
|
-
readonly name: "movementArgs";
|
|
69
|
-
}];
|
|
70
|
-
readonly name: "assetMovement";
|
|
71
|
-
}, {
|
|
72
|
-
readonly type: "uint256";
|
|
73
|
-
readonly name: "amount";
|
|
74
|
-
}, {
|
|
75
|
-
readonly type: "bytes";
|
|
76
|
-
readonly name: "memo";
|
|
77
|
-
}, {
|
|
78
|
-
readonly type: "bytes";
|
|
79
|
-
readonly name: "escapeHatch";
|
|
80
|
-
}];
|
|
81
|
-
readonly name: "_deposit";
|
|
82
|
-
}];
|
|
83
|
-
readonly outputs: readonly [];
|
|
84
|
-
}, {
|
|
85
|
-
readonly name: "performDefiInteraction";
|
|
86
|
-
readonly type: "function";
|
|
87
|
-
readonly stateMutability: "nonpayable";
|
|
88
|
-
readonly inputs: readonly [{
|
|
89
|
-
readonly type: "tuple";
|
|
90
|
-
readonly components: readonly [{
|
|
91
|
-
readonly type: "tuple";
|
|
92
|
-
readonly components: readonly [{
|
|
93
|
-
readonly type: "address";
|
|
94
|
-
readonly name: "to";
|
|
95
|
-
}, {
|
|
96
|
-
readonly type: "bytes32";
|
|
97
|
-
readonly name: "nullifier";
|
|
98
|
-
}, {
|
|
99
|
-
readonly type: "bytes32";
|
|
100
|
-
readonly name: "pastShieldHandle";
|
|
101
|
-
}, {
|
|
102
|
-
readonly type: "tuple";
|
|
103
|
-
readonly components: readonly [{
|
|
104
|
-
readonly type: "address";
|
|
105
|
-
readonly name: "assetAdapter";
|
|
106
|
-
}, {
|
|
107
|
-
readonly type: "bytes";
|
|
108
|
-
readonly name: "assetIdentificationArgs";
|
|
109
|
-
}, {
|
|
110
|
-
readonly type: "bytes";
|
|
111
|
-
readonly name: "movementArgs";
|
|
112
|
-
}];
|
|
113
|
-
readonly name: "assetMovement";
|
|
114
|
-
}, {
|
|
115
|
-
readonly type: "uint256";
|
|
116
|
-
readonly name: "amount";
|
|
117
|
-
}, {
|
|
118
|
-
readonly type: "bytes32";
|
|
119
|
-
readonly name: "sessionKeyHash";
|
|
120
|
-
}, {
|
|
121
|
-
readonly type: "bytes";
|
|
122
|
-
readonly name: "memo";
|
|
123
|
-
}];
|
|
124
|
-
readonly name: "withdrawal";
|
|
125
|
-
}, {
|
|
126
|
-
readonly type: "address";
|
|
127
|
-
readonly name: "defiAdapter";
|
|
128
|
-
}, {
|
|
129
|
-
readonly type: "bytes";
|
|
130
|
-
readonly name: "adapterArgs";
|
|
131
|
-
}];
|
|
132
|
-
readonly name: "_defiInteraction";
|
|
133
|
-
}, {
|
|
134
|
-
readonly type: "bytes";
|
|
135
|
-
readonly name: "signature";
|
|
136
|
-
}];
|
|
137
|
-
readonly outputs: readonly [];
|
|
138
|
-
}, {
|
|
139
|
-
readonly name: "defiInteractionDigest";
|
|
140
|
-
readonly type: "function";
|
|
141
|
-
readonly stateMutability: "view";
|
|
142
|
-
readonly inputs: readonly [{
|
|
143
|
-
readonly type: "tuple";
|
|
144
|
-
readonly components: readonly [{
|
|
145
|
-
readonly type: "tuple";
|
|
146
|
-
readonly components: readonly [{
|
|
147
|
-
readonly type: "address";
|
|
148
|
-
readonly name: "to";
|
|
149
|
-
}, {
|
|
150
|
-
readonly type: "bytes32";
|
|
151
|
-
readonly name: "nullifier";
|
|
152
|
-
}, {
|
|
153
|
-
readonly type: "bytes32";
|
|
154
|
-
readonly name: "pastShieldHandle";
|
|
155
|
-
}, {
|
|
156
|
-
readonly type: "tuple";
|
|
157
|
-
readonly components: readonly [{
|
|
158
|
-
readonly type: "address";
|
|
159
|
-
readonly name: "assetAdapter";
|
|
160
|
-
}, {
|
|
161
|
-
readonly type: "bytes";
|
|
162
|
-
readonly name: "assetIdentificationArgs";
|
|
163
|
-
}, {
|
|
164
|
-
readonly type: "bytes";
|
|
165
|
-
readonly name: "movementArgs";
|
|
166
|
-
}];
|
|
167
|
-
readonly name: "assetMovement";
|
|
168
|
-
}, {
|
|
169
|
-
readonly type: "uint256";
|
|
170
|
-
readonly name: "amount";
|
|
171
|
-
}, {
|
|
172
|
-
readonly type: "bytes32";
|
|
173
|
-
readonly name: "sessionKeyHash";
|
|
174
|
-
}, {
|
|
175
|
-
readonly type: "bytes";
|
|
176
|
-
readonly name: "memo";
|
|
177
|
-
}];
|
|
178
|
-
readonly name: "withdrawal";
|
|
179
|
-
}, {
|
|
180
|
-
readonly type: "address";
|
|
181
|
-
readonly name: "defiAdapter";
|
|
182
|
-
}, {
|
|
183
|
-
readonly type: "bytes";
|
|
184
|
-
readonly name: "adapterArgs";
|
|
185
|
-
}];
|
|
186
|
-
readonly name: "_defiInteraction";
|
|
187
|
-
}];
|
|
188
|
-
readonly outputs: readonly [{
|
|
189
|
-
readonly type: "bytes32";
|
|
190
|
-
}];
|
|
191
|
-
}];
|
|
192
|
-
/** Arguments for UniswapV4 swap */
|
|
193
|
-
export interface UniswapV4SwapArgs {
|
|
194
|
-
encodedV4Actions: Hex;
|
|
195
|
-
tokenIn: Address;
|
|
196
|
-
tokenOut: Address;
|
|
197
|
-
amountOutMinimum: bigint;
|
|
198
|
-
toCiphertext: Hex;
|
|
199
|
-
deadline: bigint;
|
|
200
|
-
unwrapWeth: boolean;
|
|
201
|
-
}
|
|
202
|
-
/** Parameters for building V4 swap actions */
|
|
203
|
-
export interface BuildV4ActionsParams {
|
|
204
|
-
poolKey: PoolKey;
|
|
205
|
-
zeroForOne: boolean;
|
|
206
|
-
amountIn: bigint;
|
|
207
|
-
amountOutMinimum: bigint;
|
|
208
|
-
}
|
|
209
|
-
/** Deposit parameters for IncoShield */
|
|
210
|
-
export interface DepositParams {
|
|
211
|
-
toCiphertext: Hex;
|
|
212
|
-
from: Address;
|
|
213
|
-
assetAdapter: Address;
|
|
214
|
-
tokenAddress: Address;
|
|
215
|
-
amount: bigint;
|
|
216
|
-
memo?: Hex;
|
|
217
|
-
escapeHatch?: Hex;
|
|
218
|
-
}
|
|
219
|
-
/** DefiInteraction parameters */
|
|
220
|
-
export interface DefiInteractionParams {
|
|
221
|
-
to: Address;
|
|
222
|
-
nullifier: Hex;
|
|
223
|
-
pastShieldHandle: Hex;
|
|
224
|
-
assetAdapter: Address;
|
|
225
|
-
tokenAddress: Address;
|
|
226
|
-
amount: bigint;
|
|
227
|
-
sessionKeyHash?: Hex;
|
|
228
|
-
memo?: Hex;
|
|
229
|
-
defiAdapter: Address;
|
|
230
|
-
adapterArgs: Hex;
|
|
231
|
-
}
|
|
232
|
-
type TransactionLog = {
|
|
233
|
-
address: Address;
|
|
234
|
-
topics: readonly Hex[];
|
|
235
|
-
};
|
|
236
|
-
type WaitForReceiptParams = Parameters<PublicClient<Transport, Chain>['waitForTransactionReceipt']>;
|
|
237
|
-
type WaitForReceiptResult = {
|
|
238
|
-
status: 'success' | 'reverted';
|
|
239
|
-
blockNumber?: bigint;
|
|
240
|
-
logs?: readonly TransactionLog[];
|
|
241
|
-
};
|
|
242
|
-
export interface ReadClient {
|
|
243
|
-
readContract(...args: Parameters<PublicClient<Transport, Chain>['readContract']>): Promise<unknown>;
|
|
244
|
-
waitForTransactionReceipt(...args: WaitForReceiptParams): Promise<WaitForReceiptResult>;
|
|
245
|
-
simulateContract?(...args: Parameters<PublicClient<Transport, Chain>['simulateContract']>): Promise<unknown>;
|
|
246
|
-
}
|
|
247
|
-
export interface WriteClient {
|
|
248
|
-
writeContract(...args: Parameters<WalletClient<Transport, Chain, Account>['writeContract']>): Promise<Hex>;
|
|
249
|
-
}
|
|
250
|
-
export type Signer = Pick<LocalAccount, 'address'> & {
|
|
251
|
-
sign: NonNullable<LocalAccount['sign']>;
|
|
252
|
-
};
|
|
253
|
-
/**
|
|
254
|
-
* Build encoded V4 actions for a swap
|
|
255
|
-
*
|
|
256
|
-
* @returns Encoded bytes to pass to Universal Router
|
|
257
|
-
*/
|
|
258
|
-
export declare function buildEncodedV4Actions(params: BuildV4ActionsParams): Hex;
|
|
259
|
-
/**
|
|
260
|
-
* Encode UniswapV4SwapArgs to bytes
|
|
261
|
-
*/
|
|
262
|
-
export declare function encodeSwapArgs(args: UniswapV4SwapArgs): Hex;
|
|
263
|
-
/**
|
|
264
|
-
* Generate a unique nullifier for a transaction.
|
|
265
|
-
*
|
|
266
|
-
* Uses cryptographically secure random bytes combined with address
|
|
267
|
-
* to prevent collision attacks and replay vulnerabilities.
|
|
268
|
-
*
|
|
269
|
-
* @param address - The user's address
|
|
270
|
-
* @param nonce - Optional explicit nonce (for deterministic testing)
|
|
271
|
-
*/
|
|
272
|
-
export declare function generateNullifier(address: Address, nonce?: bigint): Hex;
|
|
273
|
-
/**
|
|
274
|
-
* Build deposit struct for IncoShield
|
|
275
|
-
*/
|
|
276
|
-
export declare function buildDeposit(params: DepositParams): {
|
|
277
|
-
toCiphertext: `0x${string}`;
|
|
278
|
-
from: `0x${string}`;
|
|
279
|
-
assetMovement: {
|
|
280
|
-
assetAdapter: `0x${string}`;
|
|
281
|
-
assetIdentificationArgs: `0x${string}`;
|
|
282
|
-
movementArgs: `0x${string}`;
|
|
283
|
-
};
|
|
284
|
-
amount: bigint;
|
|
285
|
-
memo: `0x${string}`;
|
|
286
|
-
escapeHatch: `0x${string}`;
|
|
287
|
-
};
|
|
288
|
-
/**
|
|
289
|
-
* Build DefiInteraction struct for IncoShield
|
|
290
|
-
*/
|
|
291
|
-
export declare function buildDefiInteraction(params: DefiInteractionParams): {
|
|
292
|
-
withdrawal: {
|
|
293
|
-
to: `0x${string}`;
|
|
294
|
-
nullifier: `0x${string}`;
|
|
295
|
-
pastShieldHandle: `0x${string}`;
|
|
296
|
-
assetMovement: {
|
|
297
|
-
assetAdapter: `0x${string}`;
|
|
298
|
-
assetIdentificationArgs: `0x${string}`;
|
|
299
|
-
movementArgs: `0x${string}`;
|
|
300
|
-
};
|
|
301
|
-
amount: bigint;
|
|
302
|
-
sessionKeyHash: `0x${string}`;
|
|
303
|
-
memo: `0x${string}`;
|
|
304
|
-
};
|
|
305
|
-
defiAdapter: `0x${string}`;
|
|
306
|
-
adapterArgs: `0x${string}`;
|
|
307
|
-
};
|
|
308
|
-
/**
|
|
309
|
-
* Get token balance
|
|
310
|
-
*/
|
|
311
|
-
export declare function getTokenBalance(publicClient: ReadClient, tokenAddress: Address, account: Address): Promise<bigint>;
|
|
312
|
-
/**
|
|
313
|
-
* Approve token spending
|
|
314
|
-
*/
|
|
315
|
-
export declare function approveToken(walletClient: WriteClient, publicClient: ReadClient, tokenAddress: Address, spender: Address, amount: bigint): Promise<Hex>;
|
|
316
|
-
/**
|
|
317
|
-
* Deposit tokens into IncoShield
|
|
318
|
-
*/
|
|
319
|
-
export declare function depositToShield(walletClient: WriteClient, publicClient: ReadClient, incoShieldAddress: Address, params: DepositParams): Promise<{
|
|
320
|
-
txHash: Hex;
|
|
321
|
-
stateAfterDeposit: Hex;
|
|
322
|
-
}>;
|
|
323
|
-
/**
|
|
324
|
-
* Get digest for DefiInteraction signature
|
|
325
|
-
*/
|
|
326
|
-
export declare function getDefiInteractionDigest(publicClient: ReadClient, incoShieldAddress: Address, defiInteraction: ReturnType<typeof buildDefiInteraction>): Promise<Hex>;
|
|
327
|
-
/**
|
|
328
|
-
* Sign DefiInteraction and encode signature
|
|
329
|
-
*
|
|
330
|
-
* Uses raw signing (no message prefix) as expected by the contract.
|
|
331
|
-
*/
|
|
332
|
-
export declare function signDefiInteraction(signer: Signer, digest: Hex): Promise<Hex>;
|
|
333
|
-
/**
|
|
334
|
-
* Execute performDefiInteraction on IncoShield
|
|
335
|
-
*/
|
|
336
|
-
export declare function executeDefiInteraction(walletClient: WriteClient, publicClient: ReadClient, incoShieldAddress: Address, defiInteraction: ReturnType<typeof buildDefiInteraction>, signature: Hex, options?: {
|
|
337
|
-
gas?: bigint;
|
|
338
|
-
simulate?: boolean;
|
|
339
|
-
signerAddress?: Address;
|
|
340
|
-
}): Promise<{
|
|
341
|
-
txHash: Hex;
|
|
342
|
-
blockNumber: bigint;
|
|
343
|
-
}>;
|
|
344
|
-
export { ERC20_ABI, INCO_SHIELD_ABI };
|
|
@@ -1,309 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* UniswapV4 swap functionality for IncoShield
|
|
3
|
-
*
|
|
4
|
-
* Provides functions to:
|
|
5
|
-
* - Build V4 swap actions
|
|
6
|
-
* - Encode swap arguments
|
|
7
|
-
* - Execute swaps through the IncoShield DeFi adapter
|
|
8
|
-
*/
|
|
9
|
-
import { Actions, V4Planner } from '@uniswap/v4-sdk';
|
|
10
|
-
import { encodeAbiParameters, encodePacked, isHex, keccak256, parseAbi, parseAbiParameters, } from 'viem';
|
|
11
|
-
import { InvalidContractReturnType, InvalidTransactionReceipt, MalformedEventLog, MissingEventLog, TransactionReverted, } from '../errors.js';
|
|
12
|
-
// ABIs
|
|
13
|
-
const ERC20_ABI = parseAbi([
|
|
14
|
-
'function approve(address spender, uint256 amount) returns (bool)',
|
|
15
|
-
'function balanceOf(address account) view returns (uint256)',
|
|
16
|
-
]);
|
|
17
|
-
const INCO_SHIELD_ABI = parseAbi([
|
|
18
|
-
'event DepositSettled(bytes32 indexed stateAfterDeposit)',
|
|
19
|
-
'function deposit((bytes toCiphertext, address from, (address assetAdapter, bytes assetIdentificationArgs, bytes movementArgs) assetMovement, uint256 amount, bytes memo, bytes escapeHatch) _deposit) external',
|
|
20
|
-
'function performDefiInteraction(((address to, bytes32 nullifier, bytes32 pastShieldHandle, (address assetAdapter, bytes assetIdentificationArgs, bytes movementArgs) assetMovement, uint256 amount, bytes32 sessionKeyHash, bytes memo) withdrawal, address defiAdapter, bytes adapterArgs) _defiInteraction, bytes signature) external',
|
|
21
|
-
'function defiInteractionDigest(((address to, bytes32 nullifier, bytes32 pastShieldHandle, (address assetAdapter, bytes assetIdentificationArgs, bytes movementArgs) assetMovement, uint256 amount, bytes32 sessionKeyHash, bytes memo) withdrawal, address defiAdapter, bytes adapterArgs) _defiInteraction) view returns (bytes32)',
|
|
22
|
-
]);
|
|
23
|
-
// Event signature for DepositSettled - keccak256("DepositSettled(bytes32)")
|
|
24
|
-
const DEPOSIT_SETTLED_EVENT_SIGNATURE = '0x945b4585fc853bcf3d386694bcb5b7b32cba8727f8cdf849c067da29294ee71e';
|
|
25
|
-
function requireBigIntResult(value, functionName) {
|
|
26
|
-
if (typeof value !== 'bigint') {
|
|
27
|
-
throw new InvalidContractReturnType({
|
|
28
|
-
message: `${functionName} returned a non-bigint value`,
|
|
29
|
-
functionName,
|
|
30
|
-
expectedType: 'bigint',
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
return value;
|
|
34
|
-
}
|
|
35
|
-
function requireHexResult(value, functionName) {
|
|
36
|
-
if (!isHex(value)) {
|
|
37
|
-
throw new InvalidContractReturnType({
|
|
38
|
-
message: `${functionName} returned a non-hex value`,
|
|
39
|
-
functionName,
|
|
40
|
-
expectedType: 'Hex',
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
return value;
|
|
44
|
-
}
|
|
45
|
-
function requireBlockNumber(receipt) {
|
|
46
|
-
if (typeof receipt.blockNumber !== 'bigint') {
|
|
47
|
-
throw new InvalidTransactionReceipt({
|
|
48
|
-
message: 'Transaction receipt did not include a block number',
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
return receipt.blockNumber;
|
|
52
|
-
}
|
|
53
|
-
/**
|
|
54
|
-
* Build encoded V4 actions for a swap
|
|
55
|
-
*
|
|
56
|
-
* @returns Encoded bytes to pass to Universal Router
|
|
57
|
-
*/
|
|
58
|
-
export function buildEncodedV4Actions(params) {
|
|
59
|
-
const { poolKey, zeroForOne, amountIn, amountOutMinimum } = params;
|
|
60
|
-
const v4Planner = new V4Planner();
|
|
61
|
-
// Build swap config
|
|
62
|
-
const swapConfig = {
|
|
63
|
-
poolKey,
|
|
64
|
-
zeroForOne,
|
|
65
|
-
amountIn: amountIn.toString(),
|
|
66
|
-
amountOutMinimum: amountOutMinimum.toString(),
|
|
67
|
-
hookData: '0x',
|
|
68
|
-
};
|
|
69
|
-
v4Planner.addAction(Actions.SWAP_EXACT_IN_SINGLE, [swapConfig]);
|
|
70
|
-
// SETTLE_ALL: (currency, maxAmount)
|
|
71
|
-
const settleToken = zeroForOne ? poolKey.currency0 : poolKey.currency1;
|
|
72
|
-
v4Planner.addAction(Actions.SETTLE_ALL, [settleToken, amountIn.toString()]);
|
|
73
|
-
// TAKE_ALL: (currency, minAmount)
|
|
74
|
-
const takeToken = zeroForOne ? poolKey.currency1 : poolKey.currency0;
|
|
75
|
-
v4Planner.addAction(Actions.TAKE_ALL, [
|
|
76
|
-
takeToken,
|
|
77
|
-
amountOutMinimum.toString(),
|
|
78
|
-
]);
|
|
79
|
-
const encodedActions = v4Planner.finalize();
|
|
80
|
-
if (!isHex(encodedActions)) {
|
|
81
|
-
throw new InvalidContractReturnType({
|
|
82
|
-
message: 'Expected hex-encoded V4 actions',
|
|
83
|
-
functionName: 'V4Planner.finalize',
|
|
84
|
-
expectedType: 'Hex',
|
|
85
|
-
});
|
|
86
|
-
}
|
|
87
|
-
return encodedActions;
|
|
88
|
-
}
|
|
89
|
-
/**
|
|
90
|
-
* Encode UniswapV4SwapArgs to bytes
|
|
91
|
-
*/
|
|
92
|
-
export function encodeSwapArgs(args) {
|
|
93
|
-
return encodeAbiParameters([
|
|
94
|
-
{
|
|
95
|
-
type: 'tuple',
|
|
96
|
-
components: [
|
|
97
|
-
{ type: 'bytes', name: 'encodedV4Actions' },
|
|
98
|
-
{ type: 'address', name: 'tokenIn' },
|
|
99
|
-
{ type: 'address', name: 'tokenOut' },
|
|
100
|
-
{ type: 'uint256', name: 'amountOutMinimum' },
|
|
101
|
-
{ type: 'bytes', name: 'toCiphertext' },
|
|
102
|
-
{ type: 'uint256', name: 'deadline' },
|
|
103
|
-
{ type: 'bool', name: 'unwrapWeth' },
|
|
104
|
-
],
|
|
105
|
-
},
|
|
106
|
-
], [
|
|
107
|
-
{
|
|
108
|
-
encodedV4Actions: args.encodedV4Actions,
|
|
109
|
-
tokenIn: args.tokenIn,
|
|
110
|
-
tokenOut: args.tokenOut,
|
|
111
|
-
amountOutMinimum: args.amountOutMinimum,
|
|
112
|
-
toCiphertext: args.toCiphertext,
|
|
113
|
-
deadline: args.deadline,
|
|
114
|
-
unwrapWeth: args.unwrapWeth,
|
|
115
|
-
},
|
|
116
|
-
]);
|
|
117
|
-
}
|
|
118
|
-
/**
|
|
119
|
-
* Generate a unique nullifier for a transaction.
|
|
120
|
-
*
|
|
121
|
-
* Uses cryptographically secure random bytes combined with address
|
|
122
|
-
* to prevent collision attacks and replay vulnerabilities.
|
|
123
|
-
*
|
|
124
|
-
* @param address - The user's address
|
|
125
|
-
* @param nonce - Optional explicit nonce (for deterministic testing)
|
|
126
|
-
*/
|
|
127
|
-
export function generateNullifier(address, nonce) {
|
|
128
|
-
if (nonce !== undefined) {
|
|
129
|
-
return keccak256(encodePacked(['address', 'uint256'], [address, nonce]));
|
|
130
|
-
}
|
|
131
|
-
// Use crypto random for production - prevents collision attacks
|
|
132
|
-
const randomBytes = crypto.getRandomValues(new Uint8Array(32));
|
|
133
|
-
const randomHex = `0x${Array.from(randomBytes)
|
|
134
|
-
.map((b) => b.toString(16).padStart(2, '0'))
|
|
135
|
-
.join('')}`;
|
|
136
|
-
if (!isHex(randomHex)) {
|
|
137
|
-
throw new InvalidContractReturnType({
|
|
138
|
-
message: `Expected hex from random byte encoding: ${randomHex}`,
|
|
139
|
-
functionName: 'generateNullifier',
|
|
140
|
-
expectedType: 'Hex',
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
return keccak256(encodePacked(['address', 'bytes32'], [address, randomHex]));
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Build deposit struct for IncoShield
|
|
147
|
-
*/
|
|
148
|
-
export function buildDeposit(params) {
|
|
149
|
-
return {
|
|
150
|
-
toCiphertext: params.toCiphertext,
|
|
151
|
-
from: params.from,
|
|
152
|
-
assetMovement: {
|
|
153
|
-
assetAdapter: params.assetAdapter,
|
|
154
|
-
assetIdentificationArgs: encodeAbiParameters(parseAbiParameters('address'), [params.tokenAddress]),
|
|
155
|
-
movementArgs: encodeAbiParameters(parseAbiParameters('uint256'), [
|
|
156
|
-
params.amount,
|
|
157
|
-
]),
|
|
158
|
-
},
|
|
159
|
-
amount: params.amount,
|
|
160
|
-
memo: params.memo ?? '0x',
|
|
161
|
-
escapeHatch: params.escapeHatch ?? '0x',
|
|
162
|
-
};
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Build DefiInteraction struct for IncoShield
|
|
166
|
-
*/
|
|
167
|
-
export function buildDefiInteraction(params) {
|
|
168
|
-
return {
|
|
169
|
-
withdrawal: {
|
|
170
|
-
to: params.to,
|
|
171
|
-
nullifier: params.nullifier,
|
|
172
|
-
pastShieldHandle: params.pastShieldHandle,
|
|
173
|
-
assetMovement: {
|
|
174
|
-
assetAdapter: params.assetAdapter,
|
|
175
|
-
assetIdentificationArgs: encodeAbiParameters(parseAbiParameters('address'), [params.tokenAddress]),
|
|
176
|
-
movementArgs: encodeAbiParameters(parseAbiParameters('uint256'), [
|
|
177
|
-
params.amount,
|
|
178
|
-
]),
|
|
179
|
-
},
|
|
180
|
-
amount: params.amount,
|
|
181
|
-
sessionKeyHash: params.sessionKeyHash ??
|
|
182
|
-
'0x0000000000000000000000000000000000000000000000000000000000000001',
|
|
183
|
-
memo: params.memo ?? '0x',
|
|
184
|
-
},
|
|
185
|
-
defiAdapter: params.defiAdapter,
|
|
186
|
-
adapterArgs: params.adapterArgs,
|
|
187
|
-
};
|
|
188
|
-
}
|
|
189
|
-
/**
|
|
190
|
-
* Get token balance
|
|
191
|
-
*/
|
|
192
|
-
export async function getTokenBalance(publicClient, tokenAddress, account) {
|
|
193
|
-
const result = await publicClient.readContract({
|
|
194
|
-
address: tokenAddress,
|
|
195
|
-
abi: ERC20_ABI,
|
|
196
|
-
functionName: 'balanceOf',
|
|
197
|
-
args: [account],
|
|
198
|
-
});
|
|
199
|
-
return requireBigIntResult(result, 'balanceOf');
|
|
200
|
-
}
|
|
201
|
-
/**
|
|
202
|
-
* Approve token spending
|
|
203
|
-
*/
|
|
204
|
-
export async function approveToken(walletClient, publicClient, tokenAddress, spender, amount) {
|
|
205
|
-
const hash = await walletClient.writeContract({
|
|
206
|
-
address: tokenAddress,
|
|
207
|
-
abi: ERC20_ABI,
|
|
208
|
-
functionName: 'approve',
|
|
209
|
-
args: [spender, amount],
|
|
210
|
-
});
|
|
211
|
-
await publicClient.waitForTransactionReceipt({ hash });
|
|
212
|
-
return hash;
|
|
213
|
-
}
|
|
214
|
-
/**
|
|
215
|
-
* Deposit tokens into IncoShield
|
|
216
|
-
*/
|
|
217
|
-
export async function depositToShield(walletClient, publicClient, incoShieldAddress, params) {
|
|
218
|
-
const deposit = buildDeposit(params);
|
|
219
|
-
const hash = await walletClient.writeContract({
|
|
220
|
-
address: incoShieldAddress,
|
|
221
|
-
abi: INCO_SHIELD_ABI,
|
|
222
|
-
functionName: 'deposit',
|
|
223
|
-
args: [deposit],
|
|
224
|
-
});
|
|
225
|
-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
226
|
-
if (receipt.status === 'reverted') {
|
|
227
|
-
throw new TransactionReverted({
|
|
228
|
-
message: 'Deposit transaction reverted',
|
|
229
|
-
operation: 'deposit',
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
// Find DepositSettled event to get handle
|
|
233
|
-
const depositSettledLog = (receipt.logs ?? []).find((log) => log.address.toLowerCase() === incoShieldAddress.toLowerCase() &&
|
|
234
|
-
log.topics[0] === DEPOSIT_SETTLED_EVENT_SIGNATURE);
|
|
235
|
-
if (!depositSettledLog) {
|
|
236
|
-
throw new MissingEventLog({
|
|
237
|
-
message: 'Could not find DepositSettled event',
|
|
238
|
-
eventName: 'DepositSettled',
|
|
239
|
-
});
|
|
240
|
-
}
|
|
241
|
-
const stateAfterDeposit = depositSettledLog.topics[1];
|
|
242
|
-
if (!stateAfterDeposit || !isHex(stateAfterDeposit)) {
|
|
243
|
-
throw new MalformedEventLog({
|
|
244
|
-
message: 'DepositSettled log did not include a handle topic',
|
|
245
|
-
eventName: 'DepositSettled',
|
|
246
|
-
});
|
|
247
|
-
}
|
|
248
|
-
return { txHash: hash, stateAfterDeposit };
|
|
249
|
-
}
|
|
250
|
-
/**
|
|
251
|
-
* Get digest for DefiInteraction signature
|
|
252
|
-
*/
|
|
253
|
-
export async function getDefiInteractionDigest(publicClient, incoShieldAddress, defiInteraction) {
|
|
254
|
-
const result = await publicClient.readContract({
|
|
255
|
-
address: incoShieldAddress,
|
|
256
|
-
abi: INCO_SHIELD_ABI,
|
|
257
|
-
functionName: 'defiInteractionDigest',
|
|
258
|
-
args: [defiInteraction],
|
|
259
|
-
});
|
|
260
|
-
return requireHexResult(result, 'defiInteractionDigest');
|
|
261
|
-
}
|
|
262
|
-
/**
|
|
263
|
-
* Sign DefiInteraction and encode signature
|
|
264
|
-
*
|
|
265
|
-
* Uses raw signing (no message prefix) as expected by the contract.
|
|
266
|
-
*/
|
|
267
|
-
export async function signDefiInteraction(signer, digest) {
|
|
268
|
-
// Use the account's sign method directly for raw hash signing (no prefix)
|
|
269
|
-
const rawSignature = await signer.sign({ hash: digest });
|
|
270
|
-
const signerBytes = signer.address;
|
|
271
|
-
return encodeAbiParameters([
|
|
272
|
-
{ type: 'bytes[]', name: 'signers' },
|
|
273
|
-
{ type: 'bytes[]', name: 'signatures' },
|
|
274
|
-
], [[signerBytes], [rawSignature]]);
|
|
275
|
-
}
|
|
276
|
-
/**
|
|
277
|
-
* Execute performDefiInteraction on IncoShield
|
|
278
|
-
*/
|
|
279
|
-
export async function executeDefiInteraction(walletClient, publicClient, incoShieldAddress, defiInteraction, signature, options) {
|
|
280
|
-
// Optionally simulate first
|
|
281
|
-
if (options?.simulate &&
|
|
282
|
-
publicClient.simulateContract &&
|
|
283
|
-
options.signerAddress) {
|
|
284
|
-
await publicClient.simulateContract({
|
|
285
|
-
address: incoShieldAddress,
|
|
286
|
-
abi: INCO_SHIELD_ABI,
|
|
287
|
-
functionName: 'performDefiInteraction',
|
|
288
|
-
args: [defiInteraction, signature],
|
|
289
|
-
account: options.signerAddress,
|
|
290
|
-
});
|
|
291
|
-
}
|
|
292
|
-
const hash = await walletClient.writeContract({
|
|
293
|
-
address: incoShieldAddress,
|
|
294
|
-
abi: INCO_SHIELD_ABI,
|
|
295
|
-
functionName: 'performDefiInteraction',
|
|
296
|
-
args: [defiInteraction, signature],
|
|
297
|
-
gas: options?.gas,
|
|
298
|
-
});
|
|
299
|
-
const receipt = await publicClient.waitForTransactionReceipt({ hash });
|
|
300
|
-
if (receipt.status === 'reverted') {
|
|
301
|
-
throw new TransactionReverted({
|
|
302
|
-
message: 'DefiInteraction transaction reverted',
|
|
303
|
-
operation: 'performDefiInteraction',
|
|
304
|
-
});
|
|
305
|
-
}
|
|
306
|
-
return { txHash: hash, blockNumber: requireBlockNumber(receipt) };
|
|
307
|
-
}
|
|
308
|
-
// Re-export ABIs for external use
|
|
309
|
-
export { ERC20_ABI, INCO_SHIELD_ABI };
|