@@ -11,7 +11,6 @@ const {
1111const { signMessageSolanaKey } = require ( '../../solana/internal/signMessage' ) ;
1212
1313/* "TRIA" global accessControlConditions, actions, Lit*/
14-
1514async function processEthereumAction ( action ) {
1615 const { network, generateKeyParams } = action ;
1716 const messageToSign = action . signMessageParams ?. messageToSign ;
@@ -94,16 +93,43 @@ async function processActions(actions) {
9493 ) ;
9594}
9695
97- function validateParams ( actions ) {
98- if ( ! actions ) {
96+ /**
97+ * - jsParams: Expected data type: Object (e.g., "{ authMethod: { accessToken: '...', authMethodType: '...' }, publicKey: '...', actions: [...] }")
98+ *
99+ * This parameter is an object containing the following properties:
100+ * - authMethod
101+ * - publicKey
102+ * - actions: Array of action objects, each containing network and key generation params.
103+ *
104+ */
105+ function validateJsParams ( jsParams ) {
106+ if ( ! jsParams . authMethod ) {
107+ throw new Error ( 'Missing required field: authMethod' ) ;
108+ }
109+ if ( ! jsParams . publicKey ) {
110+ throw new Error ( 'Missing required field: publicKey' ) ;
111+ }
112+ if ( ! jsParams . accessControlConditions ) {
113+ throw new Error ( 'Missing required field: accessControlConditions' ) ;
114+ }
115+ const { accessToken, authMethodType } = jsParams . authMethod ;
116+
117+ if ( ! accessToken ) {
118+ throw new Error ( 'Missing required field: authMethod.accessToken' ) ;
119+ }
120+ if ( ! authMethodType ) {
121+ throw new Error ( 'Missing required field: authMethod.authMethodType' ) ;
122+ }
123+
124+ if ( ! jsParams . actions ) {
99125 throw new Error ( 'Missing required field: actions' ) ;
100126 }
101127
102- if ( ! actions . length ) {
128+ if ( ! jsParams . actions . length ) {
103129 throw new Error ( 'No actions provided (empty array?)' ) ;
104130 }
105131
106- actions . forEach ( ( action , ndx ) => {
132+ jsParams . actions . forEach ( ( action , ndx ) => {
107133 if ( ! [ 'evm' , 'solana' ] . includes ( action . network ) ) {
108134 throw new Error (
109135 `Invalid field: actions[${ ndx } ].network: ${ action . network } `
@@ -149,9 +175,114 @@ function validateParams(actions) {
149175// })();
150176
151177const go = async ( ) => {
152- LitActions . setResponse ( {
153- response : "(true, 'Something else is here!')" ,
178+ // ========== Tria's Logic ==========
179+ // Lit Action:: Prepare jsParams
180+ const jsParams = {
181+ authMethod : {
182+ accessToken : authMethod . accessToken ,
183+ authMethodType : authMethod . authMethodType ,
184+ } ,
185+ publicKey : publicKey ,
186+ actions : actions ,
187+ accessControlConditions : accessControlConditions ,
188+ } ;
189+
190+ validateJsParams ( jsParams ) ;
191+
192+ // ========== Tria's Logic ==========
193+
194+ // Authentication
195+ const url = 'https://api.development.tria.so/api/v1/user/info' ;
196+ const response = await fetch ( url , {
197+ method : 'GET' ,
198+ headers : {
199+ Authorization : `Bearer ${ jsParams . authMethod . accessToken } ` ,
200+ } ,
154201 } ) ;
202+ const data = await response . json ( ) ;
203+ console . log ( 'data' , data ) ;
204+
205+ if ( ! data . success ) {
206+ Lit . Actions . setResponse ( {
207+ response : JSON . stringify ( {
208+ success : false ,
209+ message : 'Authentication Failed' ,
210+ } ) ,
211+ } ) ;
212+ return ;
213+ }
214+
215+ // Authorization:: Prepare params
216+ // -- 1. get the authMethodId from unique identify from the response
217+ const authMethodId = `${ ethers . utils . keccak256 (
218+ ethers . utils . toUtf8Bytes ( data . userInfo . uuid )
219+ ) } `;
220+ console . log ( 'Computed AuthMethodId' , authMethodId ) ;
221+
222+ // -- 2. get the PKP token id
223+ const tokenId = Lit . Actions . pubkeyToTokenId ( {
224+ publicKey : jsParams . publicKey ,
225+ } ) ;
226+ console . log ( 'tokenId' , tokenId ) ;
227+
228+ // -- 3. get the permitted auth methods of the PKP token id
229+ const permittedAuthMethods = await Lit . Actions . getPermittedAuthMethods ( {
230+ tokenId,
231+ } ) ;
232+ console . log ( 'permittedAuthMethods' , permittedAuthMethods ) ;
233+
234+ // -- 4. only get where authMethod that's equal to the authMethod Id
235+ const permittedAuthMethod = permittedAuthMethods . find (
236+ ( method ) => method . id === authMethodId
237+ ) ;
238+ console . log ( 'permittedAuthMethod' , permittedAuthMethod ) ;
239+
240+ // TODO: Uncomment this block to enable Authorization
241+ // Authorization:: Failed Authentication and Authorization
242+ // if (
243+ // !permittedAuthMethod ||
244+ // permittedAuthMethod.auth_method_type !== jsParams.authMethod.authMethodType
245+ // ) {
246+ // Lit.Actions.setResponse({
247+ // response: JSON.stringify({
248+ // success: false,
249+ // message: 'Authorization Failed',
250+ // }),
251+ // });
252+ // return;
253+ // }
254+
255+ // LitActions.setResponse({
256+ // response: `(true, ${JSON.stringify({
257+ // returnedData: data,
258+ // logs: {
259+ // authMethodId,
260+ // tokenId,
261+ // permittedAuthMethods,
262+ // permittedAuthMethod,
263+ // actions: jsParams.actions,
264+ // batchGeneratePrivateKeysActionResult,
265+ // },
266+ // }) })`,
267+ // });
268+
269+ try {
270+ const batchGeneratePrivateKeysActionResult = await processActions (
271+ jsParams . actions
272+ ) ;
273+
274+ Lit . Actions . setResponse ( {
275+ response : JSON . stringify (
276+ `(true, ${ JSON . stringify ( batchGeneratePrivateKeysActionResult ) } )`
277+ ) ,
278+ } ) ;
279+
280+ // 1. Generate both EVM and solana private keys
281+ // 2. Run appropriate signMessage for each key _and_ encrypt the keys for persistence to wrapped-keys backend
282+ // 3. Return results for both signMessage ops and both encrypted key payloads for persistence
283+ } catch ( err ) {
284+ Lit . Actions . setResponse ( { response : `Error: ${ err . message } ` } ) ;
285+ }
155286} ;
156287
157288go ( ) ;
0 commit comments