|
| 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 | +}; |
0 commit comments