Skip to content

Commit 593fb9e

Browse files
committed
feat: add tria api and got session sigs. yaa!?
1 parent a2a57c2 commit 593fb9e

File tree

10 files changed

+328
-31
lines changed

10 files changed

+328
-31
lines changed

local-tests/test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ import { testFailBatchGeneratePrivateKeysAtomic } from './tests/wrapped-keys/tes
109109

110110
import { setLitActionsCodeToLocal } from './tests/wrapped-keys/util';
111111
import { testUseEoaSessionSigsToRequestSingleResponse } from './tests/testUseEoaSessionSigsToRequestSingleResponse';
112+
import { testUseTriaAuthAndWrappedKeysSessionSigsGen } from './tests/testUseTriaAuthAndWrappedKeysSessionSigsGen';
112113

113114
// Use the current LIT action code to test against
114115
setLitActionsCodeToLocal();
@@ -174,6 +175,7 @@ setLitActionsCodeToLocal();
174175
testUseEoaSessionSigsToEncryptDecryptFile,
175176
testUseEoaSessionSigsToEncryptDecryptZip,
176177
testUseEoaSessionSigsToRequestSingleResponse,
178+
testUseTriaAuthAndWrappedKeysSessionSigsGen
177179
};
178180

179181
const pkpSessionSigsTests = {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
import { LitActionResource, LitPKPResource } from '@lit-protocol/auth-helpers';
2+
import { log } from '@lit-protocol/misc';
3+
import { LitAbility } from '@lit-protocol/types';
4+
import {
5+
getLitActionSessionSigs,
6+
getLitActionSessionSigsUsingIpfsId,
7+
} from 'local-tests/setup/session-sigs/get-lit-action-session-sigs';
8+
import { TinnyEnvironment } from 'local-tests/setup/tinny-environment';
9+
import { api } from '@lit-protocol/wrapped-keys';
10+
11+
import Hash from "typestub-ipfs-only-hash";
12+
import { AuthMethodScope } from '@lit-protocol/constants';
13+
14+
const { triaBatchGeneratePrivateKeys, exportPrivateKey } = api;
15+
/**
16+
* Test Commands:
17+
* ✅ NETWORK=datil-dev yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen
18+
* ✅ NETWORK=datil-test yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen
19+
* ✅ NETWORK=datil yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen
20+
*/
21+
export const testUseTriaAuthAndWrappedKeysSessionSigsGen = async (
22+
devEnv: TinnyEnvironment
23+
) => {
24+
const alice = await devEnv.createRandomPerson();
25+
26+
// -- Start
27+
const triaAuthMethod = {
28+
// authMethodId: '', <-- Tria's managing this by permitting auth method to the user id
29+
authMethodType: process.env.TRIA_AUTHMETHOD_TYPE,
30+
accessToken: process.env.TRIA_ACCESS_TOKEN,
31+
};
32+
33+
// -- mint a pkp
34+
console.log(`🔄 Minting new PKP...`);
35+
const pkpMintRes =
36+
await devEnv.contractsClient.pkpNftContractUtils.write.mint();
37+
const pkp = pkpMintRes.pkp;
38+
console.log(` ✅ PKP minted:`);
39+
console.log(` - Token ID: ${pkp.tokenId}`);
40+
console.log(` - Public Key: ${pkp.publicKey}`);
41+
console.log(` - ETH Address: ${pkp.ethAddress}`);
42+
43+
// -- mint capacity token
44+
console.log(`🔄 Minting Capacity Credits NFT...`);
45+
const capacityTokenId = (
46+
await devEnv.contractsClient.mintCapacityCreditsNFT({
47+
requestsPerKilosecond: 10,
48+
daysUntilUTCMidnightExpiration: 1,
49+
})
50+
).capacityTokenIdStr;
51+
console.log(` ✅ Capacity Credits NFT minted:`);
52+
53+
// -- create capacity delegation auth sig
54+
console.log(`🔄 Creating Capacity Delegation AuthSig...`);
55+
const authSigResponse =
56+
await devEnv.litNodeClient.createCapacityDelegationAuthSig({
57+
dAppOwnerWallet: alice.wallet,
58+
capacityTokenId,
59+
delegateeAddresses: [pkp.ethAddress],
60+
uses: '1',
61+
});
62+
const capacityDelegationAuthSig = authSigResponse.capacityDelegationAuthSig;
63+
console.log(` ✅ Capacity Delegation AuthSig created:`);
64+
console.log(` - AuthSig: ${JSON.stringify(capacityDelegationAuthSig)}`);
65+
console.log(` - Uses: 1`);
66+
console.log(` - Delegatee Address: ${pkp.ethAddress}`);
67+
console.log(` - Capacity Token ID: ${capacityTokenId}`);
68+
69+
// -- Get the lit action code..
70+
const { litActionCode, litActionIpfsCid } = api.getLitActionCodeOrCidCommon(
71+
'triaAuthAndBatchGenerateEncryptedKeys'
72+
);
73+
74+
// -- detect which one we got
75+
const hashOfLitActionCode = litActionCode
76+
? await Hash.of(Buffer.from(litActionCode))
77+
: litActionIpfsCid;
78+
console.log(` ✅ Lit Action Code IPFS CID: ${hashOfLitActionCode}`);
79+
80+
console.log(`🔄 Adding permitted action...`);
81+
const permitTx = await devEnv.contractsClient.addPermittedAction({
82+
ipfsId: hashOfLitActionCode,
83+
pkpTokenId: pkp.tokenId,
84+
authMethodScopes: [AuthMethodScope.SignAnything]
85+
});
86+
console.log(` ✅ Permitted action added:`);
87+
console.log(` - Transaction Hash: ${permitTx.transactionHash}`);
88+
89+
const solanaMessageToSign = 'This is a test solana message';
90+
const evmMessageToSign = 'This is a test evm message';
91+
92+
const { results } = await triaBatchGeneratePrivateKeys({
93+
pkpPublicKey: pkp.publicKey,
94+
ipfsId: hashOfLitActionCode,
95+
actions: [
96+
{
97+
network: 'evm',
98+
signMessageParams: { messageToSign: evmMessageToSign },
99+
generateKeyParams: { memo: 'Test evm key' },
100+
},
101+
{
102+
network: 'solana',
103+
signMessageParams: { messageToSign: solanaMessageToSign },
104+
generateKeyParams: { memo: 'Test solana key' },
105+
},
106+
],
107+
litNodeClient: devEnv.litNodeClient,
108+
authMethod: triaAuthMethod,
109+
});
110+
111+
console.log("results:", results);
112+
};

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@
7979
"tslib": "^2.3.0",
8080
"tweetnacl": "^1.0.3",
8181
"tweetnacl-util": "^0.15.1",
82+
"typestub-ipfs-only-hash": "^4.0.0",
8283
"uint8arrays": "^4.0.3"
8384
},
8485
"devDependencies": {

packages/types/src/lib/ILitNodeClient.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import {
1818
RejectedNodePromises,
1919
SendNodeCommand,
2020
SuccessNodePromises,
21+
GetLitActionSessionSigs,
22+
SessionSigsMap,
2123
} from './interfaces';
2224
import { ILitResource, ISessionCapabilityObject } from './models';
2325
import { SupportedJsonRequests } from './types';
@@ -227,4 +229,15 @@ export interface ILitNodeClient {
227229
generateSessionCapabilityObjectWithWildcards(
228230
litResources: ILitResource[]
229231
): Promise<ISessionCapabilityObject>;
232+
233+
/**
234+
* Retrieves session signatures specifically for Lit Actions.
235+
* Unlike `getPkpSessionSigs`, this function requires either `litActionCode` or `litActionIpfsId`, and `jsParams` must be provided.
236+
*
237+
* @param params - The parameters required for retrieving the session signatures.
238+
* @returns A promise that resolves with the session signatures.
239+
*/
240+
getLitActionSessionSigs(
241+
params: GetLitActionSessionSigs
242+
): Promise<SessionSigsMap>;
230243
}

packages/types/src/lib/interfaces.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1943,6 +1943,11 @@ export type GetLitActionSessionSigs = CommonGetSessionSigsProps &
19431943
})
19441944
) & {
19451945
ipfsOptions?: IpfsOptions;
1946+
1947+
/**
1948+
* Special property to return all node responses
1949+
*/
1950+
getNodeResponses?: boolean;
19461951
};
19471952

19481953
export interface SessionKeyCache {

packages/wrapped-keys-lit-actions/src/lib/raw-action-functions/common/triaAuthAndBatchGenerateEncryptedKeys.js

Lines changed: 16 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,6 @@ function validateTriaParams(triaParams) {
141141
if (!triaParams.publicKey) {
142142
throw new Error('Missing required field: publicKey');
143143
}
144-
if (!triaParams.accessControlConditions) {
145-
throw new Error('Missing required field: accessControlConditions');
146-
}
147144
const { accessToken, authMethodType } = triaParams.authMethod;
148145

149146
if (!accessToken) {
@@ -167,13 +164,7 @@ async function triaAuth({ accessToken, publicKey, authMethodType }) {
167164
console.log('data', data);
168165

169166
if (!data.success) {
170-
Lit.Actions.setResponse({
171-
response: JSON.stringify({
172-
success: false,
173-
message: 'Authentication Failed',
174-
}),
175-
});
176-
return;
167+
throw new Error(`Authentication Failed`);
177168
}
178169

179170
// -- Authorization
@@ -242,31 +233,25 @@ export async function triaAuthAndBatchGenerateEncryptedKeys({
242233
// -- Authenticate and authorize with Tria
243234
await triaAuth({
244235
accessToken: triaParams.authMethod.accessToken,
245-
publicKey: triaParams.publicKey,
246236
authMethodType: triaParams.authMethod.authMethodType,
237+
publicKey: triaParams.publicKey,
247238
});
248239

249240
// -- Run once
250-
try {
251-
let res = await Lit.Actions.runOnce(
252-
{ waitForResponse: false, name: 'tria-auth-and-wrapped-keys' },
253-
async () => {
254-
const processedActions = await processActions({
255-
actions,
256-
accessControlConditions,
257-
});
258-
return JSON.stringify(processedActions);
259-
}
260-
);
261241

262-
Lit.Actions.setResponse({
263-
response: JSON.stringify(`(true, ${res})`),
264-
});
242+
let res = await Lit.Actions.runOnce(
243+
{ waitForResponse: false, name: 'tria-auth-and-wrapped-keys' },
244+
async () => {
245+
const processedActions = await processActions({
246+
actions,
247+
accessControlConditions,
248+
});
249+
return JSON.stringify(processedActions);
250+
}
251+
);
265252

266-
// 1. Generate both EVM and solana private keys
267-
// 2. Run appropriate signMessage for each key _and_ encrypt the keys for persistence to wrapped-keys backend
268-
// 3. Return results for both signMessage ops and both encrypted key payloads for persistence
269-
} catch (err) {
270-
Lit.Actions.setResponse({ response: `Error: ${err.message}` });
271-
}
253+
return JSON.stringify(`(true, ${res})`);
254+
// 1. Generate both EVM and solana private keys
255+
// 2. Run appropriate signMessage for each key _and_ encrypt the keys for persistence to wrapped-keys backend
256+
// 3. Return results for both signMessage ops and both encrypted key payloads for persistence
272257
}

packages/wrapped-keys/src/index.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
listEncryptedKeyMetadata,
1010
batchGeneratePrivateKeys,
1111
storeEncryptedKeyBatch,
12+
triaBatchGeneratePrivateKeys,
1213
} from './lib/api';
1314
import {
1415
CHAIN_ETHEREUM,
@@ -27,6 +28,7 @@ import {
2728
LitActionCodeRepositoryCommon,
2829
LitActionCodeRepositoryEntry,
2930
} from './lib/lit-actions-client/types';
31+
import { getLitActionCodeOrCidCommon } from './lib/lit-actions-client/utils';
3032

3133
import type { SupportedNetworks } from './lib/service-client/types';
3234
import type {
@@ -75,6 +77,8 @@ export const api = {
7577
storeEncryptedKey,
7678
storeEncryptedKeyBatch,
7779
batchGeneratePrivateKeys,
80+
triaBatchGeneratePrivateKeys,
81+
getLitActionCodeOrCidCommon,
7882
};
7983

8084
export const config = {

packages/wrapped-keys/src/lib/api/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { batchGeneratePrivateKeys } from './batch-generate-private-keys';
2+
import { triaBatchGeneratePrivateKeys } from './tria-batch-generate-private-keys';
23
import { exportPrivateKey } from './export-private-key';
34
import { generatePrivateKey } from './generate-private-key';
45
import { getEncryptedKey } from './get-encrypted-key';
@@ -20,4 +21,5 @@ export {
2021
storeEncryptedKeyBatch,
2122
getEncryptedKey,
2223
batchGeneratePrivateKeys,
24+
triaBatchGeneratePrivateKeys,
2325
};

0 commit comments

Comments
 (0)