|
| 1 | +import { ethers } from 'ethers'; |
| 2 | + |
| 3 | +import { LitPKPResource } from '@lit-protocol/auth-helpers'; |
| 4 | +import { LIT_ABILITY } from '@lit-protocol/constants'; |
| 5 | +import { EthWalletProvider } from '@lit-protocol/lit-auth-client'; |
| 6 | +import { LitNodeClient } from '@lit-protocol/lit-node-client'; |
| 7 | +import { log } from '@lit-protocol/misc'; |
| 8 | +import { AuthCallbackParams, AuthSig } from '@lit-protocol/types'; |
| 9 | +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Test Commands: |
| 13 | + * ✅ NETWORK=datil-dev yarn test:local --filter=testUseEoaAuthContextToPkpSign |
| 14 | + * ✅ NETWORK=datil-test yarn test:local --filter=testUseEoaAuthContextToPkpSign |
| 15 | + * ✅ NETWORK=custom yarn test:local --filter=testUseEoaAuthContextToPkpSign |
| 16 | + */ |
| 17 | +export const testUseEoaAuthContextToPkpSign = async ( |
| 18 | + devEnv: TinnyEnvironment |
| 19 | +) => { |
| 20 | + const alice = await devEnv.createRandomPerson(); |
| 21 | + |
| 22 | + const ONE_MINUTE = 1 * 60 * 1000; |
| 23 | + const expiration = new Date(Date.now() + ONE_MINUTE).toISOString(); |
| 24 | + const resourceAbilityRequests = [ |
| 25 | + { |
| 26 | + resource: new LitPKPResource('*'), |
| 27 | + ability: LIT_ABILITY.PKPSigning, |
| 28 | + }, |
| 29 | + ]; |
| 30 | + const litNodeClient = new LitNodeClient({ |
| 31 | + litNetwork: alice.envConfig.network, |
| 32 | + authContext: { |
| 33 | + getSessionSigsProps: { |
| 34 | + resourceAbilityRequests, |
| 35 | + expiration, |
| 36 | + authNeededCallback: async function ( |
| 37 | + params: AuthCallbackParams |
| 38 | + ): Promise<AuthSig> { |
| 39 | + const response = await litNodeClient.signSessionKey({ |
| 40 | + sessionKey: params.sessionKey, |
| 41 | + statement: params.statement || 'Some custom statement.', |
| 42 | + authMethods: [ |
| 43 | + await EthWalletProvider.authenticate({ |
| 44 | + signer: alice.wallet, |
| 45 | + litNodeClient: litNodeClient, |
| 46 | + expiration: params.expiration, |
| 47 | + }), |
| 48 | + ], |
| 49 | + pkpPublicKey: alice.pkp.publicKey, |
| 50 | + expiration: params.expiration, |
| 51 | + resources: params.resources, |
| 52 | + chainId: 1, |
| 53 | + |
| 54 | + // -- required fields |
| 55 | + resourceAbilityRequests: params.resourceAbilityRequests, |
| 56 | + }); |
| 57 | + |
| 58 | + return response.authSig; |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }); |
| 63 | + |
| 64 | + const runWithAuthContext = await litNodeClient.pkpSign({ |
| 65 | + toSign: alice.loveLetter, |
| 66 | + pubKey: alice.pkp.publicKey, |
| 67 | + }); |
| 68 | + |
| 69 | + devEnv.releasePrivateKeyFromUser(alice); |
| 70 | + |
| 71 | + // Expected output: |
| 72 | + // { |
| 73 | + // r: "25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9", |
| 74 | + // s: "549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b101833214", |
| 75 | + // recid: 1, |
| 76 | + // signature: "0x25fc0d2fecde8ed801e9fee5ad26f2cf61d82e6f45c8ad1ad1e4798d3b747fd9549fe745b4a09536e6e7108d814cf7e44b93f1d73c41931b8d57d1b1018332141c", |
| 77 | + // publicKey: "04A3CD53CCF63597D3FFCD1DF1E8236F642C7DF8196F532C8104625635DC55A1EE59ABD2959077432FF635DF2CED36CC153050902B71291C4D4867E7DAAF964049", |
| 78 | + // dataSigned: "7D87C5EA75F7378BB701E404C50639161AF3EFF66293E9F375B5F17EB50476F4", |
| 79 | + // } |
| 80 | + |
| 81 | + // -- assertions |
| 82 | + // r, s, dataSigned, and public key should be present |
| 83 | + if (!runWithAuthContext.r) { |
| 84 | + throw new Error(`Expected "r" in runWithAuthContext`); |
| 85 | + } |
| 86 | + if (!runWithAuthContext.s) { |
| 87 | + throw new Error(`Expected "s" in runWithAuthContext`); |
| 88 | + } |
| 89 | + if (!runWithAuthContext.dataSigned) { |
| 90 | + throw new Error(`Expected "dataSigned" in runWithAuthContext`); |
| 91 | + } |
| 92 | + if (!runWithAuthContext.publicKey) { |
| 93 | + throw new Error(`Expected "publicKey" in runWithAuthContext`); |
| 94 | + } |
| 95 | + |
| 96 | + // signature must start with 0x |
| 97 | + if (!runWithAuthContext.signature.startsWith('0x')) { |
| 98 | + throw new Error(`Expected "signature" to start with 0x`); |
| 99 | + } |
| 100 | + |
| 101 | + // recid must be parseable as a number |
| 102 | + if (isNaN(runWithAuthContext.recid)) { |
| 103 | + throw new Error(`Expected "recid" to be parseable as a number`); |
| 104 | + } |
| 105 | + |
| 106 | + const signature = ethers.utils.joinSignature({ |
| 107 | + r: '0x' + runWithAuthContext.r, |
| 108 | + s: '0x' + runWithAuthContext.s, |
| 109 | + recoveryParam: runWithAuthContext.recid, |
| 110 | + }); |
| 111 | + const recoveredPubKey = ethers.utils.recoverPublicKey( |
| 112 | + alice.loveLetter, |
| 113 | + signature |
| 114 | + ); |
| 115 | + if (recoveredPubKey !== `0x${runWithAuthContext.publicKey.toLowerCase()}`) { |
| 116 | + throw new Error( |
| 117 | + `Expected recovered public key to match runWithAuthContext.publicKey` |
| 118 | + ); |
| 119 | + } |
| 120 | + if (recoveredPubKey !== `0x${alice.pkp.publicKey.toLowerCase()}`) { |
| 121 | + throw new Error( |
| 122 | + `Expected recovered public key to match alice.pkp.publicKey` |
| 123 | + ); |
| 124 | + } |
| 125 | + |
| 126 | + log('✅ testUseEoaAuthContextToPkpSign'); |
| 127 | +}; |
0 commit comments