|
| 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 { getLitActionSessionSigs, getLitActionSessionSigsUsingIpfsId } from 'local-tests/setup/session-sigs/get-lit-action-session-sigs'; |
| 5 | +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; |
| 6 | + |
| 7 | +/** |
| 8 | + * Test Commands: |
| 9 | + * ✅ NETWORK=datil-dev yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen |
| 10 | + * ✅ NETWORK=datil-test yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen |
| 11 | + * ✅ NETWORK=datil yarn test:local --filter=testUseTriaAuthAndWrappedKeysSessionSigsGen |
| 12 | + */ |
| 13 | +export const testUseTriaAuthAndWrappedKeysSessionSigsGen = |
| 14 | + async (devEnv: TinnyEnvironment) => { |
| 15 | + |
| 16 | + const alice = await devEnv.createRandomPerson(); |
| 17 | + |
| 18 | + // -- Start |
| 19 | + |
| 20 | + // -- mint a pkp |
| 21 | + console.log(`🔄 Minting new PKP...`); |
| 22 | + const pkpMintRes = await devEnv.contractsClient.pkpNftContractUtils.write.mint(); |
| 23 | + const pkp = pkpMintRes.pkp; |
| 24 | + console.log(` ✅ PKP minted:`); |
| 25 | + console.log(` - Token ID: ${pkp.tokenId}`); |
| 26 | + console.log(` - Public Key: ${pkp.publicKey}`); |
| 27 | + console.log(` - ETH Address: ${pkp.ethAddress}`); |
| 28 | + |
| 29 | + // -- mint capacity token |
| 30 | + console.log(`🔄 Minting Capacity Credits NFT...`); |
| 31 | + const capacityTokenId = ( |
| 32 | + await devEnv.contractsClient.mintCapacityCreditsNFT({ |
| 33 | + requestsPerKilosecond: 10, |
| 34 | + daysUntilUTCMidnightExpiration: 1, |
| 35 | + }) |
| 36 | + ).capacityTokenIdStr; |
| 37 | + console.log(` ✅ Capacity Credits NFT minted:`); |
| 38 | + |
| 39 | + // -- create capacity delegation auth sig |
| 40 | + console.log(`🔄 Creating Capacity Delegation AuthSig...`); |
| 41 | + const authSigResponse = await devEnv.litNodeClient.createCapacityDelegationAuthSig({ |
| 42 | + dAppOwnerWallet: alice.wallet, |
| 43 | + capacityTokenId, |
| 44 | + delegateeAddresses: [pkp.ethAddress], |
| 45 | + uses: "1", |
| 46 | + }); |
| 47 | + const capacityDelegationAuthSig = authSigResponse.capacityDelegationAuthSig; |
| 48 | + console.log(` ✅ Capacity Delegation AuthSig created:`); |
| 49 | + console.log(` - AuthSig: ${JSON.stringify(capacityDelegationAuthSig)}`); |
| 50 | + console.log(` - Uses: 1`); |
| 51 | + console.log(` - Delegatee Address: ${pkp.ethAddress}`); |
| 52 | + console.log(` - Capacity Token ID: ${capacityTokenId}`); |
| 53 | + |
| 54 | + // -- Get the lit action code.. |
| 55 | + |
| 56 | + |
| 57 | + process.exit(); |
| 58 | + |
| 59 | + const litActionSessionSigs = await devEnv.litNodeClient.getLitActionSessionSigs({ |
| 60 | + pkpPublicKey: alice.authMethodOwnedPkp.publicKey, |
| 61 | + expiration: new Date(Date.now() + 1000 * 60 * 10).toISOString(), // 10 mins expiry |
| 62 | + resourceAbilityRequests: [ |
| 63 | + { |
| 64 | + resource: new LitPKPResource("*"), |
| 65 | + ability: LitAbility.PKPSigning, |
| 66 | + }, |
| 67 | + { |
| 68 | + resource: new LitActionResource("*"), |
| 69 | + ability: LitAbility.LitActionExecution, |
| 70 | + }, |
| 71 | + ], |
| 72 | + litActionCode: Buffer.from(` |
| 73 | + // Works with an AuthSig AuthMethod |
| 74 | + if (Lit.Auth.authMethodContexts.some(e => e.authMethodType === 1)) { |
| 75 | + LitActions.setResponse({ response: "true" }); |
| 76 | + } else { |
| 77 | + LitActions.setResponse({ response: "false" }); |
| 78 | + } |
| 79 | + `).toString("base64"), |
| 80 | + jsParams: { |
| 81 | + magicNumber: 42, |
| 82 | + }, |
| 83 | + capabilityAuthSigs: [capacityDelegationAuthSig], |
| 84 | + }); |
| 85 | + |
| 86 | + // const litActionSessionSigs = await getLitActionSessionSigs(devEnv, alice) |
| 87 | + |
| 88 | + console.log("litActionSessionSigs:", litActionSessionSigs); |
| 89 | + process.exit(); |
| 90 | + |
| 91 | + // -- Remove below |
| 92 | + // const litActionSessionSigs = await getLitActionSessionSigsUsingIpfsId( |
| 93 | + // devEnv, |
| 94 | + // alice, |
| 95 | + // [ |
| 96 | + // { |
| 97 | + // resource: new LitPKPResource('*'), |
| 98 | + // ability: LitAbility.PKPSigning, |
| 99 | + // }, |
| 100 | + // { |
| 101 | + // resource: new LitActionResource('*'), |
| 102 | + // ability: LitAbility.LitActionExecution, |
| 103 | + // }, |
| 104 | + // ] |
| 105 | + // ); |
| 106 | + |
| 107 | + // const res = await devEnv.litNodeClient.executeJs({ |
| 108 | + // sessionSigs: litActionSessionSigs, |
| 109 | + // code: `(async () => { |
| 110 | + // const sigShare = await LitActions.signEcdsa({ |
| 111 | + // toSign: dataToSign, |
| 112 | + // publicKey, |
| 113 | + // sigName: "sig", |
| 114 | + // }); |
| 115 | + // })();`, |
| 116 | + // jsParams: { |
| 117 | + // dataToSign: alice.loveLetter, |
| 118 | + // publicKey: alice.authMethodOwnedPkp.publicKey, |
| 119 | + // }, |
| 120 | + // }); |
| 121 | + |
| 122 | + }; |
0 commit comments