diff --git a/contracts/cayenne/PKPHelperV2.json b/contracts/cayenne/PKPHelperV2.json new file mode 100644 index 0000000..c4f6c98 --- /dev/null +++ b/contracts/cayenne/PKPHelperV2.json @@ -0,0 +1,340 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_resolver", + "type": "address" + }, + { + "internalType": "enum ContractResolver.Env", + "name": "_env", + "type": "uint8" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "newResolverAddress", + "type": "address" + } + ], + "name": "ContractResolverAddressSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "inputs": [], + "name": "contractResolver", + "outputs": [ + { + "internalType": "contract ContractResolver", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "env", + "outputs": [ + { + "internalType": "enum ContractResolver.Env", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getDomainWalletRegistry", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPKPNftMetdataAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPkpNftAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPkpPermissionsAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "keyType", + "type": "uint256" + }, + { + "internalType": "uint256[]", + "name": "permittedAuthMethodTypes", + "type": "uint256[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodIds", + "type": "bytes[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodPubkeys", + "type": "bytes[]" + }, + { + "internalType": "uint256[][]", + "name": "permittedAuthMethodScopes", + "type": "uint256[][]" + }, + { + "internalType": "bool", + "name": "addPkpEthAddressAsPermittedAddress", + "type": "bool" + }, + { + "internalType": "uint256[]", + "name": "pkpEthAddressScopes", + "type": "uint256[]" + }, + { + "internalType": "bool", + "name": "sendPkpToItself", + "type": "bool" + }, + { + "internalType": "bool", + "name": "burnPkp", + "type": "bool" + } + ], + "internalType": "struct PKPHelperV2.NewPKPParams", + "name": "params", + "type": "tuple" + } + ], + "name": "mintNextAndAddAuthMethods", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "keyType", + "type": "uint256" + }, + { + "internalType": "uint256[]", + "name": "permittedAuthMethodTypes", + "type": "uint256[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodIds", + "type": "bytes[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodPubkeys", + "type": "bytes[]" + }, + { + "internalType": "uint256[][]", + "name": "permittedAuthMethodScopes", + "type": "uint256[][]" + }, + { + "internalType": "bool", + "name": "addPkpEthAddressAsPermittedAddress", + "type": "bool" + }, + { + "internalType": "uint256[]", + "name": "pkpEthAddressScopes", + "type": "uint256[]" + }, + { + "internalType": "bool", + "name": "sendPkpToItself", + "type": "bool" + }, + { + "internalType": "bool", + "name": "burnPkp", + "type": "bool" + } + ], + "internalType": "struct PKPHelperV2.NewPKPParams", + "name": "params", + "type": "tuple" + } + ], + "name": "mintNextAndAddAuthMethodsWithTypes", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "", + "type": "bytes" + } + ], + "name": "onERC721Received", + "outputs": [ + { + "internalType": "bytes4", + "name": "", + "type": "bytes4" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newResolverAddress", + "type": "address" + } + ], + "name": "setContractResolver", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] diff --git a/contracts/serrano/PKPHelperV2.json b/contracts/serrano/PKPHelperV2.json new file mode 100644 index 0000000..c4f6c98 --- /dev/null +++ b/contracts/serrano/PKPHelperV2.json @@ -0,0 +1,340 @@ +[ + { + "inputs": [ + { + "internalType": "address", + "name": "_resolver", + "type": "address" + }, + { + "internalType": "enum ContractResolver.Env", + "name": "_env", + "type": "uint8" + } + ], + "stateMutability": "nonpayable", + "type": "constructor" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": false, + "internalType": "address", + "name": "newResolverAddress", + "type": "address" + } + ], + "name": "ContractResolverAddressSet", + "type": "event" + }, + { + "anonymous": false, + "inputs": [ + { + "indexed": true, + "internalType": "address", + "name": "previousOwner", + "type": "address" + }, + { + "indexed": true, + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "OwnershipTransferred", + "type": "event" + }, + { + "inputs": [], + "name": "contractResolver", + "outputs": [ + { + "internalType": "contract ContractResolver", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "env", + "outputs": [ + { + "internalType": "enum ContractResolver.Env", + "name": "", + "type": "uint8" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getDomainWalletRegistry", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPKPNftMetdataAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPkpNftAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "getPkpPermissionsAddress", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "keyType", + "type": "uint256" + }, + { + "internalType": "uint256[]", + "name": "permittedAuthMethodTypes", + "type": "uint256[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodIds", + "type": "bytes[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodPubkeys", + "type": "bytes[]" + }, + { + "internalType": "uint256[][]", + "name": "permittedAuthMethodScopes", + "type": "uint256[][]" + }, + { + "internalType": "bool", + "name": "addPkpEthAddressAsPermittedAddress", + "type": "bool" + }, + { + "internalType": "uint256[]", + "name": "pkpEthAddressScopes", + "type": "uint256[]" + }, + { + "internalType": "bool", + "name": "sendPkpToItself", + "type": "bool" + }, + { + "internalType": "bool", + "name": "burnPkp", + "type": "bool" + } + ], + "internalType": "struct PKPHelperV2.NewPKPParams", + "name": "params", + "type": "tuple" + } + ], + "name": "mintNextAndAddAuthMethods", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "components": [ + { + "internalType": "uint256", + "name": "keyType", + "type": "uint256" + }, + { + "internalType": "uint256[]", + "name": "permittedAuthMethodTypes", + "type": "uint256[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodIds", + "type": "bytes[]" + }, + { + "internalType": "bytes[]", + "name": "permittedAuthMethodPubkeys", + "type": "bytes[]" + }, + { + "internalType": "uint256[][]", + "name": "permittedAuthMethodScopes", + "type": "uint256[][]" + }, + { + "internalType": "bool", + "name": "addPkpEthAddressAsPermittedAddress", + "type": "bool" + }, + { + "internalType": "uint256[]", + "name": "pkpEthAddressScopes", + "type": "uint256[]" + }, + { + "internalType": "bool", + "name": "sendPkpToItself", + "type": "bool" + }, + { + "internalType": "bool", + "name": "burnPkp", + "type": "bool" + } + ], + "internalType": "struct PKPHelperV2.NewPKPParams", + "name": "params", + "type": "tuple" + } + ], + "name": "mintNextAndAddAuthMethodsWithTypes", + "outputs": [ + { + "internalType": "uint256", + "name": "", + "type": "uint256" + } + ], + "stateMutability": "payable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "address", + "name": "", + "type": "address" + }, + { + "internalType": "uint256", + "name": "", + "type": "uint256" + }, + { + "internalType": "bytes", + "name": "", + "type": "bytes" + } + ], + "name": "onERC721Received", + "outputs": [ + { + "internalType": "bytes4", + "name": "", + "type": "bytes4" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "owner", + "outputs": [ + { + "internalType": "address", + "name": "", + "type": "address" + } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "renounceOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newResolverAddress", + "type": "address" + } + ], + "name": "setContractResolver", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address", + "name": "newOwner", + "type": "address" + } + ], + "name": "transferOwnership", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] diff --git a/index.ts b/index.ts index 7a65b86..9408b97 100644 --- a/index.ts +++ b/index.ts @@ -53,6 +53,7 @@ import { import { fetchPKPsHandler, mintNextAndAddAuthMethodsHandler, + mintNextAndAddAuthMethodsHandlerV2, } from "./routes/auth/mintAndFetch"; import config from "./config"; @@ -214,6 +215,10 @@ app.post("/store-condition", storeConditionHandler); // --- Mint PKP for authorized account app.post("/mint-next-and-add-auth-methods", mintNextAndAddAuthMethodsHandler); +// --- Mint PKP for authorized account using v2 of the helper contract +app.post("/mint-next-and-add-auth-methods-v2", mintNextAndAddAuthMethodsHandlerV2); + + // --- Fetch PKPs tied to authorized account app.post("/fetch-pkps-by-auth-method", fetchPKPsHandler); diff --git a/lit.ts b/lit.ts index c5d489d..6112163 100644 --- a/lit.ts +++ b/lit.ts @@ -95,6 +95,15 @@ function getAccessControlConditionsContract() { } } +function getPkpHelperV2ContractAbiPath() { + switch (config.network) { + case "serrano": + return "./contracts/serrano/PKPHelperV2.json"; + case "cayenne": + return "./contracts/cayenne/PKPHelperV2.json"; + } +} + function getPkpHelperContractAbiPath() { if (config.useSoloNet) { return "./contracts/serrano/SoloNetPKPHelper.json"; @@ -119,10 +128,20 @@ function getPkpNftContractAbiPath() { } } +async function getPkpHelperV2Contract() { + switch (config.network) { + case "manzano": + return getContractFromWorker('manzano', 'PKPHelperV2'); + case "habanero": + return getContractFromWorker('habanero', 'PKPHelperV2'); + } + + throw new Error(`PKPHelperV2 contract not available for network ${config.network}`); +} + async function getPkpHelperContract() { switch (config.network) { case "serrano": - return getContract( getPkpHelperContractAbiPath()!, config?.serranoContract?.pkpHelperAddress as string, @@ -217,6 +236,61 @@ export async function storeConditionWithSigner( return tx; } +export async function mintPKPV3({ + keyType, + permittedAuthMethodTypes, + permittedAuthMethodIds, + permittedAuthMethodPubkeys, + permittedAuthMethodScopes, + addPkpEthAddressAsPermittedAddress, + pkpEthAddressScopes, + sendPkpToItself, + burnPkp, +}: { + keyType: string; + permittedAuthMethodTypes: string[]; + permittedAuthMethodIds: string[]; + permittedAuthMethodPubkeys: string[]; + permittedAuthMethodScopes: string[][]; + addPkpEthAddressAsPermittedAddress: boolean; + pkpEthAddressScopes: string[][]; + sendPkpToItself: boolean; + burnPkp: boolean; +}): Promise { + console.log( + "In mintPKPV3", + keyType, + permittedAuthMethodTypes, + permittedAuthMethodIds, + permittedAuthMethodPubkeys, + permittedAuthMethodScopes, + addPkpEthAddressAsPermittedAddress, + sendPkpToItself, + ); + + console.log('config.network:', config.network); + + const pkpHelper = await getPkpHelperV2Contract(); + const pkpNft = await getPkpNftContract(); + + // first get mint cost + const mintCost = await pkpNft.mintCost(); + const tx = await pkpHelper.mintNextAndAddAuthMethods( + keyType, + permittedAuthMethodTypes, + permittedAuthMethodIds, + permittedAuthMethodPubkeys, + permittedAuthMethodScopes, + addPkpEthAddressAsPermittedAddress, + pkpEthAddressScopes, + sendPkpToItself, + burnPkp, + { value: mintCost }, + ); + console.log("tx", tx); + return tx; +} + export async function mintPKPV2({ keyType, permittedAuthMethodTypes, diff --git a/models/index.ts b/models/index.ts index 183d60c..dba6373 100644 --- a/models/index.ts +++ b/models/index.ts @@ -13,6 +13,18 @@ export interface OTPAuthVerifyRegistrationRequest { accessToken: string; } +export interface MintNextAndAddAuthMethodsV2Request { + keyType: string; + permittedAuthMethodTypes: string[]; + permittedAuthMethodIds: string[]; + permittedAuthMethodPubkeys: string[]; + permittedAuthMethodScopes: string[][]; + addPkpEthAddressAsPermittedAddress: boolean; + pkpEthAddressScopes: string[][]; + sendPkpToItself: boolean; + burnPkp: boolean; +} + export interface MintNextAndAddAuthMethodsRequest { keyType: string; permittedAuthMethodTypes: string[]; @@ -129,6 +141,7 @@ export interface SessionSigSignedMessage { export interface Contract { accessControlConditionsAddress: string; pkpHelperAddress: string; + pkpHelperV2Address?: string; pkpPermissionsAddress: string; pkpNftAddress: string; } diff --git a/routes/auth/mintAndFetch.ts b/routes/auth/mintAndFetch.ts index f765e6c..06c480b 100644 --- a/routes/auth/mintAndFetch.ts +++ b/routes/auth/mintAndFetch.ts @@ -1,14 +1,48 @@ import { Request } from "express"; import { Response } from "express-serve-static-core"; import { ParsedQs } from "qs"; -import { getPKPsForAuthMethod, mintPKPV2 } from "../../lit"; +import { getPKPsForAuthMethod, mintPKPV2, mintPKPV3 } from "../../lit"; import { AuthMethodVerifyToFetchResponse, FetchRequest, MintNextAndAddAuthMethodsRequest, MintNextAndAddAuthMethodsResponse, + MintNextAndAddAuthMethodsV2Request, } from "../../models"; +export async function mintNextAndAddAuthMethodsHandlerV2( + req: Request< + {}, + MintNextAndAddAuthMethodsResponse, + MintNextAndAddAuthMethodsV2Request, + ParsedQs, + Record + >, + res: Response< + MintNextAndAddAuthMethodsResponse, + Record, + number + >, +) { + // mint PKP for user + try { + const mintTx = await mintPKPV3(req.body); + console.info("Minted PKP", { + requestId: mintTx.hash, + }); + return res.status(200).json({ + requestId: mintTx.hash, + }); + } catch (err) { + console.error("Unable to mint PKP", { + err, + }); + return res.status(500).json({ + error: `Unable to mint PKP`, + }); + } +} + export async function mintNextAndAddAuthMethodsHandler( req: Request< {},