@@ -6,6 +6,7 @@ declare global {
66 const params : any ;
77 const pkpEthAddress : any ;
88 const publicKey : any ;
9+ const LitAuth : any ;
910}
1011
1112export default async ( ) => {
@@ -16,6 +17,38 @@ export default async () => {
1617 "0x728e8162603F35446D09961c4A285e2643f4FB91" ;
1718
1819 try {
20+ // Validate auth parameters
21+ if ( ! LitAuth . authSigAddress ) {
22+ throw new Error ( "Missing required parameter: LitAuth.authSigAddress" ) ;
23+ }
24+ if ( ! LitAuth . actionIpfsIds [ 0 ] ) {
25+ throw new Error ( "Missing required parameter: LitAuth.actionIpfsIds[0]" ) ;
26+ }
27+ if ( ! pkpEthAddress ) {
28+ throw new Error ( "Missing required parameter: pkpEthAddress" ) ;
29+ }
30+
31+ // Validate swap parameters
32+ if ( ! params . tokenIn ) {
33+ throw new Error ( "Missing required parameter: tokenIn" ) ;
34+ }
35+ if ( ! params . tokenOut ) {
36+ throw new Error ( "Missing required parameter: tokenOut" ) ;
37+ }
38+ if ( ! params . amountIn ) {
39+ throw new Error ( "Missing required parameter: amountIn" ) ;
40+ }
41+
42+ // Validate and normalize token addresses
43+ let tokenIn : string ;
44+ let tokenOut : string ;
45+ try {
46+ tokenIn = ethers . utils . getAddress ( params . tokenIn ) ;
47+ tokenOut = ethers . utils . getAddress ( params . tokenOut ) ;
48+ } catch ( error ) {
49+ throw new Error ( `Invalid token address: ${ error . message } ` ) ;
50+ }
51+
1952 const UNISWAP_V3_ROUTER = "0x2626664c2603336E57B271c5C0b26F421741e481" ;
2053 const ethersProvider = new ethers . providers . JsonRpcProvider (
2154 chainInfo . rpcUrl
@@ -33,9 +66,9 @@ export default async () => {
3366 ) ;
3467
3568 const [ isPermitted , , policy ] = await registryContract . getActionPolicy (
36- params . user , // The user who owns the PKP
69+ LitAuth . authSigAddress , // The user who owns the PKP
3770 pkpEthAddress , // The PKP address
38- params . recommendedCID // The IPFS CID of this Lit Action
71+ LitAuth . actionIpfsIds [ 0 ] // The IPFS CID of this Lit Action
3972 ) ;
4073
4174 if ( ! isPermitted ) {
@@ -73,17 +106,21 @@ export default async () => {
73106 ) ;
74107 }
75108
76- // Validate that allowedTokens contains valid addresses
109+ // Validate and normalize allowed token addresses
77110 if ( ! Array . isArray ( decodedPolicy . allowedTokens ) ) {
78111 throw new Error ( "Invalid policy format: allowedTokens is not an array" ) ;
79112 }
80- for ( const token of decodedPolicy . allowedTokens ) {
81- if ( ! ethers . utils . isAddress ( token ) ) {
82- throw new Error (
83- `Invalid policy format: ${ token } is not a valid address`
84- ) ;
113+ decodedPolicy . allowedTokens = decodedPolicy . allowedTokens . map (
114+ ( token : string ) => {
115+ if ( ! ethers . utils . isAddress ( token ) ) {
116+ throw new Error (
117+ `Invalid policy format: ${ token } is not a valid address`
118+ ) ;
119+ }
120+ // Normalize to checksum address
121+ return ethers . utils . getAddress ( token ) ;
85122 }
86- }
123+ ) ;
87124
88125 console . log ( "Policy:" , {
89126 maxAmount : decodedPolicy . maxAmount . toString ( ) ,
@@ -99,40 +136,45 @@ export default async () => {
99136 throw error ;
100137 }
101138
102- // Use the policy's allowed tokens
139+ // Use the policy's allowed tokens (already normalized to checksum)
103140 const allowedTokens = decodedPolicy . allowedTokens ;
104141
105- if ( ! allowedTokens . includes ( params . tokenIn ) ) {
142+ // Normalize input token addresses to checksum format for comparison
143+ const normalizedTokenIn = ethers . utils . getAddress ( tokenIn ) ;
144+ const normalizedTokenOut = ethers . utils . getAddress ( tokenOut ) ;
145+
146+ if ( ! allowedTokens . includes ( normalizedTokenIn ) ) {
106147 LitActions . setResponse ( {
107148 response : JSON . stringify ( {
108149 status : "error" ,
109- error : `Token not allowed: ${ params . tokenIn } ` ,
150+ error : `Token not allowed: ${ normalizedTokenIn } ` ,
110151 } ) ,
111152 } ) ;
112- throw new Error ( `Token not allowed: ${ params . tokenIn } ` ) ;
153+ throw new Error ( `Token not allowed: ${ normalizedTokenIn } ` ) ;
113154 }
114- if ( ! allowedTokens . includes ( params . tokenOut ) ) {
155+ if ( ! allowedTokens . includes ( normalizedTokenOut ) ) {
115156 LitActions . setResponse ( {
116157 response : JSON . stringify ( {
117158 status : "error" ,
118- error : `Token not allowed: ${ params . tokenOut } ` ,
159+ error : `Token not allowed: ${ normalizedTokenOut } ` ,
119160 } ) ,
120161 } ) ;
121- throw new Error ( `Token not allowed: ${ params . tokenOut } ` ) ;
162+ throw new Error ( `Token not allowed: ${ normalizedTokenOut } ` ) ;
122163 }
123164
124165 const tokenInterface = new ethers . utils . Interface ( [
125166 "function decimals() view returns (uint8)" ,
126167 "function approve(address spender, uint256 amount) external returns (bool)" ,
127168 ] ) ;
128169
170+ // Use normalized addresses for contract interactions
129171 const tokenInContract = new ethers . Contract (
130- params . tokenIn ,
172+ normalizedTokenIn ,
131173 tokenInterface ,
132174 ethersProvider
133175 ) ;
134176 const tokenOutContract = new ethers . Contract (
135- params . tokenOut ,
177+ normalizedTokenOut ,
136178 tokenInterface ,
137179 ethersProvider
138180 ) ;
@@ -156,7 +198,7 @@ export default async () => {
156198 const amountOutMin = ethers . BigNumber . from ( 0 ) ;
157199
158200 const approvalTx = {
159- to : params . tokenIn ,
201+ to : normalizedTokenIn , // Use normalized address
160202 data : tokenInterface . encodeFunctionData ( "approve" , [
161203 UNISWAP_V3_ROUTER ,
162204 amountIn ,
@@ -216,8 +258,8 @@ export default async () => {
216258 ] ) ;
217259
218260 const swapParamsArray = [
219- params . tokenIn ,
220- params . tokenOut ,
261+ normalizedTokenIn , // Use normalized address
262+ normalizedTokenOut , // Use normalized address
221263 3000 ,
222264 pkpEthAddress ,
223265 amountIn ,
0 commit comments