diff --git a/local-tests/test.ts b/local-tests/test.ts index e140a2d91..5b71ad259 100644 --- a/local-tests/test.ts +++ b/local-tests/test.ts @@ -107,6 +107,7 @@ import { testFailBatchGeneratePrivateKeysAtomic } from './tests/wrapped-keys/tes import { setLitActionsCodeToLocal } from './tests/wrapped-keys/util'; import { testUseEoaSessionSigsToRequestSingleResponse } from './tests/testUseEoaSessionSigsToRequestSingleResponse'; +import { testUseEoaSessionSigsClaimAndMint } from './tests/testUseEoaSessionSigsClaimAndMint'; // Use the current LIT action code to test against setLitActionsCodeToLocal(); @@ -171,6 +172,7 @@ setLitActionsCodeToLocal(); testUseEoaSessionSigsToEncryptDecryptString, testUseEoaSessionSigsToEncryptDecryptUint8Array, testUseEoaSessionSigsToEncryptDecryptFile, + testUseEoaSessionSigsClaimAndMint, }; const pkpSessionSigsTests = { diff --git a/local-tests/tests/testUseEoaSessionSigsClaimAndMint.ts b/local-tests/tests/testUseEoaSessionSigsClaimAndMint.ts new file mode 100644 index 000000000..341b1599d --- /dev/null +++ b/local-tests/tests/testUseEoaSessionSigsClaimAndMint.ts @@ -0,0 +1,69 @@ +import { log } from '@lit-protocol/misc'; +import { getEoaSessionSigs } from 'local-tests/setup/session-sigs/get-eoa-session-sigs'; +import { TinnyEnvironment } from 'local-tests/setup/tinny-environment'; + +/** + * Test Commands: + * NETWORK=datil-dev yarn test:local --filter=testUseEoaSessionSigsClaimAndMint + * NETWORK=datil-test yarn test:local --filter=testUseEoaSessionSigsClaimAndMint + * NETWORK=custom yarn test:local --filter=testUseEoaSessionSigsClaimAndMint + */ +export const testUseEoaSessionSigsClaimAndMint = async ( + devEnv: TinnyEnvironment +) => { + const alice = await devEnv.createRandomPerson(); + + const eoaSessionSigs = await getEoaSessionSigs(devEnv, alice); + + const { claims } = await devEnv.litNodeClient.executeJs({ + sessionSigs: eoaSessionSigs, + code: `(async () => { + Lit.Actions.claimKey({ keyId: "keyId" }); + })();`, + }); + + const { signatures, derivedKeyId } = claims.keyId; + + console.log('signatures:', signatures); + console.log('derivedKeyId:', derivedKeyId); + + const keyType = 2; + + const claimMaterial = { + keyType: keyType, + derivedKeyId: `0x${derivedKeyId}`, + signatures: signatures, + }; + + const authMethodData = { + keyType: keyType, + permittedIpfsCIDs: [], + permittedIpfsCIDScopes: [], + permittedAddresses: [], + permittedAddressScopes: [], + permittedAuthMethodTypes: [alice.authMethod.authMethodType], + permittedAuthMethodIds: [await alice.getAuthMethodId()], + permittedAuthMethodPubkeys: [`0x${alice.pkp.publicKey}`], + permittedAuthMethodScopes: [], + addPkpEthAddressAsPermittedAddress: true, + addPkpPubkeyAsPermittedAuthMethod: true, + sendPkpToItself: true, + }; + + const tx = + await devEnv.contractsClient.pkpHelperContract.write.claimAndMintNextAndAddAuthMethodsWithTypes( + claimMaterial, + authMethodData, + { + gasLimit: 500000, + } + ); + + console.log('tx:', tx); + + devEnv.releasePrivateKeyFromUser(alice); + + // Expected output: + + log('✅ testUseEoaSessionSigsClaimAndMint'); +}; diff --git a/package.json b/package.json index a9a1c83f2..525d52adc 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "@cosmjs/stargate": "0.30.1", "@dotenvx/dotenvx": "^1.6.4", "@lit-protocol/accs-schemas": "^0.0.15", - "@lit-protocol/contracts": "^0.0.63", + "@lit-protocol/contracts": "^0.0.71", "@metamask/eth-sig-util": "5.0.2", "@mysten/sui.js": "^0.37.1", "@openagenda/verror": "^3.1.4", diff --git a/packages/contracts-sdk/src/lib/contracts-sdk.ts b/packages/contracts-sdk/src/lib/contracts-sdk.ts index 8561fbbd2..78bd4f1bd 100644 --- a/packages/contracts-sdk/src/lib/contracts-sdk.ts +++ b/packages/contracts-sdk/src/lib/contracts-sdk.ts @@ -1663,6 +1663,74 @@ https://developer.litprotocol.com/v3/sdk/wallets/auth-methods/#auth-method-scope } }; + /** + * Claims and mints a PKP with auth methods + * @param {Object} params - The parameters object + * @param {Object} params.claimMaterial - The claim material containing keyType, derivedKeyId and signatures + * @param {number} params.claimMaterial.keyType - The type of key to mint + * @param {string} params.claimMaterial.derivedKeyId - The derived key ID + * @param {Array<{signature: string, derivedKeyId: string, signedData: string}>} params.claimMaterial.signatures - Array of signature objects + * @param {Object} params.authMethodData - The auth method data for permissions + * @param {number} params.authMethodData.keyType - The type of key + * @param {string[]} params.authMethodData.permittedIpfsCIDs - Permitted IPFS CIDs + * @param {string[][]} params.authMethodData.permittedIpfsCIDScopes - Scopes for permitted IPFS CIDs + * @param {string[]} params.authMethodData.permittedAddresses - Permitted addresses + * @param {string[][]} params.authMethodData.permittedAddressScopes - Scopes for permitted addresses + * @param {number[]} params.authMethodData.permittedAuthMethodTypes - Types of permitted auth methods + * @param {string[]} params.authMethodData.permittedAuthMethodIds - IDs of permitted auth methods + * @param {string[]} params.authMethodData.permittedAuthMethodPubkeys - Public keys of permitted auth methods + * @param {string[][]} params.authMethodData.permittedAuthMethodScopes - Scopes for permitted auth methods + * @param {boolean} params.authMethodData.addPkpEthAddressAsPermittedAddress - Whether to add PKP ETH address + * @param {boolean} params.authMethodData.addPkpPubkeyAsPermittedAuthMethod - Whether to add PKP pubkey + * @param {boolean} params.authMethodData.sendPkpToItself - Whether to send PKP to itself + * @returns {Promise} The transaction result + */ + claimAndMintPKPWithAuth = async ({ + claimMaterial, + authMethodData, + }: { + claimMaterial: { + keyType: number; + derivedKeyId: string; + signatures: Array<{ + r: `0x${string}` | string; + s: `0x${string}` | string; + v: number; + }>; + }; + authMethodData: { + keyType: number; + permittedIpfsCIDs: string[]; + permittedIpfsCIDScopes: string[][]; + permittedAddresses: string[]; + permittedAddressScopes: string[][]; + permittedAuthMethodTypes: number[]; + permittedAuthMethodIds: string[]; + permittedAuthMethodPubkeys: string[]; + permittedAuthMethodScopes: string[][]; + addPkpEthAddressAsPermittedAddress: boolean; + addPkpPubkeyAsPermittedAuthMethod: boolean; + sendPkpToItself: boolean; + }; + }): Promise => { + // Ensure derivedKeyId has 0x prefix + const derivedKeyId = claimMaterial.derivedKeyId.startsWith('0x') + ? claimMaterial.derivedKeyId + : `0x${claimMaterial.derivedKeyId}`; + + claimMaterial.derivedKeyId = derivedKeyId; + + const tx = await this._callWithAdjustedOverrides( + this.pkpHelperContract.write, + 'claimAndMintNextAndAddAuthMethodsWithTypes', + [claimMaterial, authMethodData] + ); + + this.log('Claim and mint PKP with auth method tx:', tx); + + return tx; + }; + // getRandomPrivateKeySignerProvider = () => { // const privateKey = ethers.utils.hexlify(ethers.utils.randomBytes(32)); diff --git a/yarn.lock b/yarn.lock index a3858b1e6..5890b9e1f 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3042,10 +3042,10 @@ dependencies: ajv "^8.12.0" -"@lit-protocol/contracts@^0.0.63": - version "0.0.63" - resolved "https://registry.yarnpkg.com/@lit-protocol/contracts/-/contracts-0.0.63.tgz#8700c37df9d2422e9c97aa27871fb64de6186f6c" - integrity sha512-CAorNt72ybIY/g//dDeR837izNGuYQR99XwPSK2X2AJ6c+aZX1kdXCrOnxsbY40BzFrOk/dIFo+ymJ9E3qh48w== +"@lit-protocol/contracts@^0.0.71": + version "0.0.71" + resolved "https://registry.yarnpkg.com/@lit-protocol/contracts/-/contracts-0.0.71.tgz#f436b35a4cfe5b49e7ceaf3ea1dc4d73ad953c53" + integrity sha512-T5cPgo7tZG3+qLmNk2KDbB5t8f0yBThJEVs5lxsagkhZaabx0AOte0kJpGioWICvlaiGYKtACWh6ukBEo3bKzw== "@ljharb/resumer@~0.0.1": version "0.0.1"