@lit-protocol/vincent-scaffold-sdk
Version:
Shared build configuration and utilities for Vincent abilities and policies
58 lines (57 loc) • 3.2 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mintNewPkp = void 0;
const ethers_1 = require("ethers");
const constants_1 = require("@lit-protocol/constants");
const contracts_sdk_1 = require("@lit-protocol/contracts-sdk");
const env_manager_1 = require("../managers/env-manager");
/**
* Helper function to mint a new PKP and return its information
* @param pkpOwnerPrivateKey Private key of the wallet that will own the PKP
* @param litNetwork Lit network to use (e.g., "datil", "datil-test", "datil-dev")
* @param abilityAndPolicyIpfsCids IPFS CIDs for abilities and policies to authorize
* @returns PKP information including tokenId, publicKey, and ethAddress
*/
const mintNewPkp = async (pkpOwnerPrivateKey, litNetwork, ...abilityAndPolicyIpfsCids) => {
console.log(`🔁 Minting PKP for ${abilityAndPolicyIpfsCids.length} abilities and policies on "${litNetwork}" network`);
// Create ethers provider and owner wallet
const provider = new ethers_1.ethers.providers.JsonRpcProvider(env_manager_1.ENV.YELLOWSTONE_RPC_URL);
const pkpOwnerWallet = new ethers_1.ethers.Wallet(pkpOwnerPrivateKey, provider);
const litContractClient = new contracts_sdk_1.LitContracts({
signer: pkpOwnerWallet,
network: constants_1.LIT_NETWORK[litNetwork] || constants_1.LIT_NETWORK.Datil,
});
await litContractClient.connect();
// Dynamically create auth method types array
const authMethodTypes = [
constants_1.AUTH_METHOD_TYPE.EthWallet,
...abilityAndPolicyIpfsCids.map(() => constants_1.AUTH_METHOD_TYPE.LitAction),
];
// Dynamically create auth method IDs array
const authMethodIds = [
pkpOwnerWallet.address,
...abilityAndPolicyIpfsCids.map((ipfsCid) => `0x${Buffer.from(ethers_1.ethers.utils.base58.decode(ipfsCid)).toString("hex")}`),
];
// Create auth method pubkeys array (all "0x" for our use case)
const authMethodPubkeys = ["0x", ...abilityAndPolicyIpfsCids.map(() => "0x")];
// Create auth method scopes array
const authMethodScopes = [
[constants_1.AUTH_METHOD_SCOPE.SignAnything],
...abilityAndPolicyIpfsCids.map(() => [constants_1.AUTH_METHOD_SCOPE.SignAnything]),
];
const mintPkpTx = await litContractClient.pkpHelperContract.write.mintNextAndAddAuthMethods(constants_1.AUTH_METHOD_TYPE.EthWallet, authMethodTypes, authMethodIds, authMethodPubkeys, authMethodScopes, true, // addPkpEthAddressAsPermittedAddress
false, // sendPkpToItself
{ value: await litContractClient.pkpNftContract.read.mintCost() });
const mintPkpReceipt = await mintPkpTx.wait();
const pkpMintedEvent = mintPkpReceipt.events.find((event) => event.topics[0] ===
"0x3b2cc0657d0387a736293d66389f78e4c8025e413c7a1ee67b7707d4418c46b8");
const publicKey = "0x" + pkpMintedEvent.data.slice(130, 260);
const tokenId = ethers_1.ethers.utils.keccak256(publicKey);
const ethAddress = await litContractClient.pkpNftContract.read.getEthAddress(tokenId);
return {
tokenId: ethers_1.ethers.BigNumber.from(tokenId).toString(),
publicKey,
ethAddress,
};
};
exports.mintNewPkp = mintNewPkp;