@inco/shield-js 0.0.0-bootstrap.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.
Files changed (65) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +359 -0
  3. package/dist/binary.d.ts +3 -0
  4. package/dist/binary.js +16 -0
  5. package/dist/contracts/abi.d.ts +6800 -0
  6. package/dist/contracts/abi.js +8836 -0
  7. package/dist/contracts/index.d.ts +2 -0
  8. package/dist/contracts/index.js +2 -0
  9. package/dist/contracts/utils.d.ts +25 -0
  10. package/dist/contracts/utils.js +28 -0
  11. package/dist/errors.d.ts +125 -0
  12. package/dist/errors.js +91 -0
  13. package/dist/generated/inco/shield/v2/conductor_connect.d.ts +137 -0
  14. package/dist/generated/inco/shield/v2/conductor_connect.js +141 -0
  15. package/dist/generated/inco/shield/v2/deposit_pb.d.ts +103 -0
  16. package/dist/generated/inco/shield/v2/deposit_pb.js +141 -0
  17. package/dist/generated/inco/shield/v2/drafting_pb.d.ts +279 -0
  18. package/dist/generated/inco/shield/v2/drafting_pb.js +372 -0
  19. package/dist/generated/inco/shield/v2/permission_pb.d.ts +443 -0
  20. package/dist/generated/inco/shield/v2/permission_pb.js +639 -0
  21. package/dist/generated/inco/shield/v2/query_pb.d.ts +103 -0
  22. package/dist/generated/inco/shield/v2/query_pb.js +141 -0
  23. package/dist/generated/inco/shield/v2/types_pb.d.ts +166 -0
  24. package/dist/generated/inco/shield/v2/types_pb.js +261 -0
  25. package/dist/index.d.ts +12 -0
  26. package/dist/index.js +19 -0
  27. package/dist/shield/client.d.ts +72 -0
  28. package/dist/shield/client.js +136 -0
  29. package/dist/shield/convert.d.ts +18 -0
  30. package/dist/shield/convert.js +152 -0
  31. package/dist/shield/encryption.d.ts +86 -0
  32. package/dist/shield/encryption.js +180 -0
  33. package/dist/shield/envelope.d.ts +14 -0
  34. package/dist/shield/envelope.js +29 -0
  35. package/dist/shield/index.d.ts +13 -0
  36. package/dist/shield/index.js +11 -0
  37. package/dist/shield/intent.d.ts +977 -0
  38. package/dist/shield/intent.js +188 -0
  39. package/dist/shield/rpc.d.ts +72 -0
  40. package/dist/shield/rpc.js +423 -0
  41. package/dist/shield/shield.eip712.gen.d.ts +396 -0
  42. package/dist/shield/shield.eip712.gen.js +318 -0
  43. package/dist/shield/types.d.ts +119 -0
  44. package/dist/shield/types.js +4 -0
  45. package/dist/shield/userop/gas-estimation.d.ts +36 -0
  46. package/dist/shield/userop/gas-estimation.js +284 -0
  47. package/dist/shield/userop/index.d.ts +1 -0
  48. package/dist/shield/userop/index.js +1 -0
  49. package/dist/shield/userop/token-exchange.d.ts +54 -0
  50. package/dist/shield/userop/token-exchange.js +165 -0
  51. package/dist/shield/userop/types.d.ts +91 -0
  52. package/dist/shield/userop/types.js +4 -0
  53. package/dist/shield/userop/userOp.d.ts +26 -0
  54. package/dist/shield/userop/userOp.js +98 -0
  55. package/dist/shield/utils/chain.d.ts +2 -0
  56. package/dist/shield/utils/chain.js +8 -0
  57. package/dist/uniswap-adapter/config.d.ts +34 -0
  58. package/dist/uniswap-adapter/config.js +45 -0
  59. package/dist/uniswap-adapter/find-pools.d.ts +91 -0
  60. package/dist/uniswap-adapter/find-pools.js +108 -0
  61. package/dist/uniswap-adapter/index.d.ts +10 -0
  62. package/dist/uniswap-adapter/index.js +13 -0
  63. package/dist/uniswap-adapter/swap.d.ts +344 -0
  64. package/dist/uniswap-adapter/swap.js +309 -0
  65. package/package.json +72 -0
@@ -0,0 +1,98 @@
1
+ /**
2
+ * Constitution: builds a v0.7 UserOperation from a drafted cheque in three pure
3
+ * stages — (1) assemble the unsigned op, (2) compute the hash the quorum signs,
4
+ * (3) attach a signature. Every stage is deterministic: identical inputs produce
5
+ * identical bytes across all quorum members. That is the property the on-chain
6
+ * TEE smart account relies on when it verifies that every quorum signature is
7
+ * over the same userOpHash. Quorum signing itself is enclave-side and
8
+ * intentionally absent here — the SDK builds and assembles, it does not sign.
9
+ */
10
+ import { encodeFunctionData } from 'viem';
11
+ import { entryPoint07Address, getUserOperationHash, } from 'viem/account-abstraction';
12
+ import { shieldAbiStubAbi, teeSmartAccountAbi } from '../../contracts/abi.js';
13
+ import { IntentKind } from '../../generated/inco/shield/v2/drafting_pb.js';
14
+ import { ENTRYPOINT_VERSION, } from './types.js';
15
+ export function buildUnsignedUserOp(params) {
16
+ return {
17
+ sender: params.sender,
18
+ nonce: params.nonce,
19
+ callData: encodeFunctionData({
20
+ abi: teeSmartAccountAbi,
21
+ functionName: 'execute',
22
+ args: [
23
+ params.shieldAddress,
24
+ // value: shield ops carry no native ETH
25
+ 0n,
26
+ encodeSettlementOpCall(params.settlementOp),
27
+ ],
28
+ }),
29
+ callGasLimit: params.gas.callGasLimit,
30
+ verificationGasLimit: params.gas.verificationGasLimit,
31
+ preVerificationGas: params.gas.preVerificationGas,
32
+ maxFeePerGas: params.gas.maxFeePerGas,
33
+ maxPriorityFeePerGas: params.gas.maxPriorityFeePerGas,
34
+ paymasterVerificationGasLimit: params.gas.paymasterVerificationGasLimit,
35
+ paymasterPostOpGasLimit: params.gas.paymasterPostOpGasLimit,
36
+ };
37
+ }
38
+ export function userOpHash(unsignedUserOp, chainId, entryPointAddress = entryPoint07Address) {
39
+ return getUserOperationHash({
40
+ chainId,
41
+ entryPointAddress,
42
+ entryPointVersion: ENTRYPOINT_VERSION,
43
+ userOperation: { ...unsignedUserOp, signature: '0x' },
44
+ });
45
+ }
46
+ export function assembleUserOp(unsigned, signature) {
47
+ return { ...unsigned, signature };
48
+ }
49
+ /**
50
+ * Encode the shield settlement call for a drafted {@link OnchainCheque}: the
51
+ * `kind` selects the on-chain function and `op` is its sole argument. This is
52
+ * the inner call wrapped by {@link buildUnsignedUserOp} inside `execute`, and is
53
+ * exported so the enclave can reuse the canonical encoding when it assembles
54
+ * the cheque calldata itself.
55
+ *
56
+ * TODO(#639): the enclave OnChainOp path ABI-encodes only the struct while this
57
+ * emits selector + struct. Harmonize so both produce identical calldata.
58
+ */
59
+ export function encodeSettlementOpCall(settlementOp) {
60
+ const abi = shieldAbiStubAbi;
61
+ switch (settlementOp.kind) {
62
+ case IntentKind.TRANSFER:
63
+ case IntentKind.DELEGATED_TRANSFER:
64
+ return encodeFunctionData({
65
+ abi,
66
+ functionName: 'transfer',
67
+ args: [settlementOp.op],
68
+ });
69
+ case IntentKind.WITHDRAW:
70
+ case IntentKind.DELEGATED_WITHDRAW:
71
+ return encodeFunctionData({
72
+ abi,
73
+ functionName: 'withdraw',
74
+ args: [settlementOp.op],
75
+ });
76
+ case IntentKind.DEFI_INTERACTION:
77
+ case IntentKind.DELEGATED_DEFI_INTERACTION:
78
+ return encodeFunctionData({
79
+ abi,
80
+ functionName: 'performDefiInteraction',
81
+ args: [settlementOp.op],
82
+ });
83
+ case IntentKind.INDIVIDUAL_REVOCATION:
84
+ return encodeFunctionData({
85
+ abi,
86
+ functionName: 'revokeOne',
87
+ args: [settlementOp.op],
88
+ });
89
+ case IntentKind.USER_WIDE_REVOCATION:
90
+ return encodeFunctionData({
91
+ abi,
92
+ functionName: 'revokeAll',
93
+ args: [settlementOp.op],
94
+ });
95
+ default:
96
+ throw new Error(`Invalid operation specified ${settlementOp}`);
97
+ }
98
+ }
@@ -0,0 +1,2 @@
1
+ import type { Chain } from 'viem';
2
+ export declare function getChainFromId(chainId: number): Chain;
@@ -0,0 +1,8 @@
1
+ import * as allChains from 'viem/chains';
2
+ export function getChainFromId(chainId) {
3
+ const chain = Object.values(allChains).find((c) => c.id === chainId);
4
+ if (!chain) {
5
+ throw new Error(`Chain with id ${chainId} not found`);
6
+ }
7
+ return chain;
8
+ }
@@ -0,0 +1,34 @@
1
+ /**
2
+ * Sepolia testnet configuration for UniswapV4Adapter testing
3
+ */
4
+ import type { Address } from 'viem';
5
+ export declare const SEPOLIA_CHAIN_ID = 11155111;
6
+ interface UniswapSepoliaContracts {
7
+ UNIVERSAL_ROUTER: Address;
8
+ PERMIT2: Address;
9
+ POOL_MANAGER: Address;
10
+ V4_QUOTER: Address;
11
+ STATE_VIEW: Address;
12
+ }
13
+ interface SepoliaTokens {
14
+ NATIVE_ETH: Address;
15
+ WETH: Address;
16
+ USDC: Address;
17
+ }
18
+ interface PoolConstants {
19
+ FEE: number;
20
+ TICK_SPACING: number;
21
+ HOOKS: Address;
22
+ }
23
+ export declare const UNISWAP_V4_SEPOLIA: UniswapSepoliaContracts;
24
+ export declare const SEPOLIA_TOKENS: SepoliaTokens;
25
+ export declare const POOL_CONFIG: PoolConstants;
26
+ export declare const WETH_USDC_POOL_CONFIG: PoolConstants;
27
+ export interface DeployedContracts {
28
+ incoShield: Address;
29
+ teeSmartAccount: Address;
30
+ erc20Adapter: Address;
31
+ uniswapV4Adapter: Address;
32
+ }
33
+ export declare const DEPLOYED_CONTRACTS: DeployedContracts;
34
+ export {};
@@ -0,0 +1,45 @@
1
+ /**
2
+ * Sepolia testnet configuration for UniswapV4Adapter testing
3
+ */
4
+ // Chain configuration
5
+ export const SEPOLIA_CHAIN_ID = 11155111;
6
+ // Uniswap V4 contracts on Sepolia
7
+ export const UNISWAP_V4_SEPOLIA = {
8
+ UNIVERSAL_ROUTER: '0x3A9D48AB9751398BbFa63ad67599Bb04e4BdF98b',
9
+ PERMIT2: '0x000000000022D473030F116dDEE9F6B43aC78BA3',
10
+ POOL_MANAGER: '0xE03A1074c86CFeDd5C142C4F04F1a1536e203543',
11
+ V4_QUOTER: '0x61b3f2011a92d183c7dbadbda940a7555ccf9227',
12
+ STATE_VIEW: '0xe1dd9c3fa50edb962e442f60dfbc432e24537e4c',
13
+ };
14
+ // Common test tokens on Sepolia
15
+ export const SEPOLIA_TOKENS = {
16
+ // Native ETH (address(0) in V4)
17
+ NATIVE_ETH: '0x0000000000000000000000000000000000000000',
18
+ WETH: '0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9',
19
+ USDC: '0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238', // Circle's testnet USDC
20
+ };
21
+ // Pool configuration for ETH/USDC (best liquidity pool on Sepolia V4)
22
+ // Pool ID: 0x3390c733d8252e864d4ce769398dd3fb8680d1f626719e8d7736d062665f0987
23
+ // Currency0: Native ETH (0x0...0)
24
+ // Currency1: USDC
25
+ // Liquidity: 2805238952407756
26
+ export const POOL_CONFIG = {
27
+ FEE: 3000, // 0.3%
28
+ TICK_SPACING: 60,
29
+ HOOKS: '0x0000000000000000000000000000000000000000',
30
+ };
31
+ // Alternative: WETH/USDC pool with some liquidity
32
+ // Pool ID: 0xda4df63a89fcfed42a03810ab15dd5df417167b9ecef407de884465f23181d4e
33
+ // Fee: 10000 (1%), TickSpacing: 200, Liquidity: 39867548
34
+ export const WETH_USDC_POOL_CONFIG = {
35
+ FEE: 10000, // 1%
36
+ TICK_SPACING: 200,
37
+ HOOKS: '0x0000000000000000000000000000000000000000',
38
+ };
39
+ // Deployed contracts on Sepolia
40
+ export const DEPLOYED_CONTRACTS = {
41
+ incoShield: '0xf203cf28b03c8c3c47678c243c49E34A654e1bfb',
42
+ teeSmartAccount: '0xF468DCD280d345e1a2429Ae569Ebbf23c3D43795',
43
+ erc20Adapter: '0xF1CD72CFE89c6FF352bdDe4c126Bfec218D7EF95',
44
+ uniswapV4Adapter: '0xe634f32b0add29b975a58412eff6dfbd8cf946b6',
45
+ };
@@ -0,0 +1,91 @@
1
+ /**
2
+ * Find Uniswap V4 pools with liquidity
3
+ *
4
+ * Uses the StateView contract to query pool state and liquidity.
5
+ */
6
+ import type { Address, Hex } from 'viem';
7
+ declare const STATE_VIEW_ABI: readonly [{
8
+ readonly name: "getSlot0";
9
+ readonly type: "function";
10
+ readonly stateMutability: "view";
11
+ readonly inputs: readonly [{
12
+ readonly type: "bytes32";
13
+ readonly name: "poolId";
14
+ }];
15
+ readonly outputs: readonly [{
16
+ readonly type: "uint160";
17
+ readonly name: "sqrtPriceX96";
18
+ }, {
19
+ readonly type: "int24";
20
+ readonly name: "tick";
21
+ }, {
22
+ readonly type: "uint24";
23
+ readonly name: "protocolFee";
24
+ }, {
25
+ readonly type: "uint24";
26
+ readonly name: "lpFee";
27
+ }];
28
+ }, {
29
+ readonly name: "getLiquidity";
30
+ readonly type: "function";
31
+ readonly stateMutability: "view";
32
+ readonly inputs: readonly [{
33
+ readonly type: "bytes32";
34
+ readonly name: "poolId";
35
+ }];
36
+ readonly outputs: readonly [{
37
+ readonly type: "uint128";
38
+ }];
39
+ }];
40
+ /** Pool key configuration */
41
+ export interface PoolKey {
42
+ currency0: Address;
43
+ currency1: Address;
44
+ fee: number;
45
+ tickSpacing: number;
46
+ hooks: Address;
47
+ }
48
+ /** Pool configuration with name for display */
49
+ export interface PoolConfig extends PoolKey {
50
+ name: string;
51
+ }
52
+ /** Pool state from on-chain query */
53
+ export interface PoolState {
54
+ pool: PoolConfig;
55
+ poolId: Hex;
56
+ liquidity: bigint;
57
+ tick: number;
58
+ sqrtPriceX96: bigint;
59
+ isInitialized: boolean;
60
+ hasLiquidity: boolean;
61
+ }
62
+ /** Client capable of reading contracts */
63
+ export interface ReadClient {
64
+ readContract: (params: {
65
+ address: Address;
66
+ abi: typeof STATE_VIEW_ABI;
67
+ functionName: string;
68
+ args: readonly unknown[];
69
+ }) => Promise<unknown>;
70
+ }
71
+ /**
72
+ * Compute pool ID from pool key
73
+ *
74
+ * Pool ID is keccak256(abi.encode(poolKey))
75
+ */
76
+ export declare function computePoolId(pool: PoolKey): Hex;
77
+ /**
78
+ * Get pool state from StateView contract
79
+ */
80
+ export declare function getPoolState(publicClient: ReadClient, stateViewAddress: Address, pool: PoolConfig): Promise<PoolState>;
81
+ /**
82
+ * Find pools with liquidity from a list of pool configurations
83
+ *
84
+ * @returns Array of pool states sorted by liquidity (descending)
85
+ */
86
+ export declare function findPoolsWithLiquidity(publicClient: ReadClient, stateViewAddress: Address, pools: PoolConfig[]): Promise<PoolState[]>;
87
+ /**
88
+ * Get the best pool (highest liquidity) from a list
89
+ */
90
+ export declare function findBestPool(publicClient: ReadClient, stateViewAddress: Address, pools: PoolConfig[]): Promise<PoolState | null>;
91
+ export {};
@@ -0,0 +1,108 @@
1
+ /**
2
+ * Find Uniswap V4 pools with liquidity
3
+ *
4
+ * Uses the StateView contract to query pool state and liquidity.
5
+ */
6
+ import { encodeAbiParameters, keccak256, parseAbi } from 'viem';
7
+ import { InvalidContractReturnType } from '../errors.js';
8
+ // StateView ABI for reading pool state
9
+ const STATE_VIEW_ABI = parseAbi([
10
+ 'function getSlot0(bytes32 poolId) view returns (uint160 sqrtPriceX96, int24 tick, uint24 protocolFee, uint24 lpFee)',
11
+ 'function getLiquidity(bytes32 poolId) view returns (uint128)',
12
+ ]);
13
+ /**
14
+ * Compute pool ID from pool key
15
+ *
16
+ * Pool ID is keccak256(abi.encode(poolKey))
17
+ */
18
+ export function computePoolId(pool) {
19
+ return keccak256(encodeAbiParameters([
20
+ { type: 'address', name: 'currency0' },
21
+ { type: 'address', name: 'currency1' },
22
+ { type: 'uint24', name: 'fee' },
23
+ { type: 'int24', name: 'tickSpacing' },
24
+ { type: 'address', name: 'hooks' },
25
+ ], [pool.currency0, pool.currency1, pool.fee, pool.tickSpacing, pool.hooks]));
26
+ }
27
+ /**
28
+ * Get pool state from StateView contract
29
+ */
30
+ export async function getPoolState(publicClient, stateViewAddress, pool) {
31
+ const poolId = computePoolId(pool);
32
+ const [slot0, liquidity] = await Promise.all([
33
+ publicClient.readContract({
34
+ address: stateViewAddress,
35
+ abi: STATE_VIEW_ABI,
36
+ functionName: 'getSlot0',
37
+ args: [poolId],
38
+ }),
39
+ publicClient.readContract({
40
+ address: stateViewAddress,
41
+ abi: STATE_VIEW_ABI,
42
+ functionName: 'getLiquidity',
43
+ args: [poolId],
44
+ }),
45
+ ]);
46
+ if (!Array.isArray(slot0) ||
47
+ typeof slot0[0] !== 'bigint' ||
48
+ typeof slot0[1] !== 'number') {
49
+ throw new InvalidContractReturnType({
50
+ message: 'Expected slot0 tuple from getSlot0',
51
+ functionName: 'getSlot0',
52
+ expectedType: '[bigint sqrtPriceX96, number tick, ...]',
53
+ });
54
+ }
55
+ const sqrtPriceX96 = slot0[0];
56
+ const tick = slot0[1];
57
+ if (typeof liquidity !== 'bigint') {
58
+ throw new InvalidContractReturnType({
59
+ message: 'Expected bigint liquidity from getLiquidity',
60
+ functionName: 'getLiquidity',
61
+ expectedType: 'bigint',
62
+ });
63
+ }
64
+ const liquidityValue = liquidity;
65
+ return {
66
+ pool,
67
+ poolId,
68
+ liquidity: liquidityValue,
69
+ tick,
70
+ sqrtPriceX96,
71
+ isInitialized: sqrtPriceX96 > 0n,
72
+ hasLiquidity: liquidityValue > 0n,
73
+ };
74
+ }
75
+ /**
76
+ * Find pools with liquidity from a list of pool configurations
77
+ *
78
+ * @returns Array of pool states sorted by liquidity (descending)
79
+ */
80
+ export async function findPoolsWithLiquidity(publicClient, stateViewAddress, pools) {
81
+ const results = [];
82
+ for (const pool of pools) {
83
+ try {
84
+ const state = await getPoolState(publicClient, stateViewAddress, pool);
85
+ results.push(state);
86
+ }
87
+ catch (error) {
88
+ // Only skip if pool doesn't exist (sqrtPriceX96 is 0)
89
+ // Re-throw unexpected errors (network failures, etc.)
90
+ if (error instanceof Error &&
91
+ error.message.includes('returned no data')) {
92
+ continue; // Pool not initialized, expected
93
+ }
94
+ console.warn(`Failed to query pool ${pool.name}:`, error);
95
+ }
96
+ }
97
+ // Filter to pools with liquidity and sort by liquidity descending
98
+ return results
99
+ .filter((r) => r.hasLiquidity)
100
+ .sort((a, b) => (b.liquidity > a.liquidity ? 1 : -1));
101
+ }
102
+ /**
103
+ * Get the best pool (highest liquidity) from a list
104
+ */
105
+ export async function findBestPool(publicClient, stateViewAddress, pools) {
106
+ const poolsWithLiquidity = await findPoolsWithLiquidity(publicClient, stateViewAddress, pools);
107
+ return poolsWithLiquidity[0] ?? null;
108
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * UniswapV4 Adapter SDK for IncoShield
3
+ *
4
+ * Provides functions for:
5
+ * - Finding Uniswap V4 pools with liquidity
6
+ * - Building and executing swaps through IncoShield
7
+ */
8
+ export { DEPLOYED_CONTRACTS, type DeployedContracts, POOL_CONFIG, SEPOLIA_CHAIN_ID, SEPOLIA_TOKENS, UNISWAP_V4_SEPOLIA, WETH_USDC_POOL_CONFIG, } from './config.js';
9
+ export { computePoolId, findBestPool, findPoolsWithLiquidity, getPoolState, type PoolConfig, type PoolKey, type PoolState, } from './find-pools.js';
10
+ export { approveToken, type BuildV4ActionsParams, buildDefiInteraction, buildDeposit, buildEncodedV4Actions, type DefiInteractionParams, type DepositParams, depositToShield, encodeSwapArgs, executeDefiInteraction, generateNullifier, getDefiInteractionDigest, getTokenBalance, signDefiInteraction, type UniswapV4SwapArgs, } from './swap.js';
@@ -0,0 +1,13 @@
1
+ /**
2
+ * UniswapV4 Adapter SDK for IncoShield
3
+ *
4
+ * Provides functions for:
5
+ * - Finding Uniswap V4 pools with liquidity
6
+ * - Building and executing swaps through IncoShield
7
+ */
8
+ // Configuration
9
+ export { DEPLOYED_CONTRACTS, POOL_CONFIG, SEPOLIA_CHAIN_ID, SEPOLIA_TOKENS, UNISWAP_V4_SEPOLIA, WETH_USDC_POOL_CONFIG, } from './config.js';
10
+ // Pool discovery
11
+ export { computePoolId, findBestPool, findPoolsWithLiquidity, getPoolState, } from './find-pools.js';
12
+ // Swap functionality
13
+ export { approveToken, buildDefiInteraction, buildDeposit, buildEncodedV4Actions, depositToShield, encodeSwapArgs, executeDefiInteraction, generateNullifier, getDefiInteractionDigest, getTokenBalance, signDefiInteraction, } from './swap.js';