Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mint by auth method #253

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { ethers } from 'ethers';
import { AuthMethodType, AuthMethodScope } from '@lit-protocol/constants';

export async function main() {
// ========== Controller Setup ===========
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const controllerWallet = new ethers.Wallet(
LITCONFIG.CONTROLLER_PRIVATE_KEY,
provider
);

// ==================== LitContracts Setup ====================
const contractClient = new LitContracts({
signer: controllerWallet,
});

await contractClient.connect();

// ==================== Test Logic ====================
const mintInfo = await contractClient.mint({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we add a new namespace for higher level utility methods since we namepsace by contract name. We should call this method some other than mint as we are combining minting and permissions which is important for this function name to indicate

authMethod: {
authMethodType: AuthMethodType.EthWallet,
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG),
},
scopes: [AuthMethodScope.SignAnything, AuthMethodScope.OnlySignMessages],
});

if (!mintInfo.tx.transactionHash) {
return fail(`failed to mint a PKP`);
}

// ==================== Post-Validation ====================
// NOTE: When using other auth methods, you might need to wait for a block to be mined before you can read the scopes
// -- get the scopes
const scopes =
await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
mintInfo.pkp.tokenId,
AuthMethodType.EthWallet,
LITCONFIG.CONTROLLER_AUTHSIG.address, // auth id
3 // we only offer 2 scopes atm. and index 0 doesn't exist, so either 1 = sign anything or 2 = only sign messages
);

const signAnythingScope = scopes[1];
const onlySignMessagesScope = scopes[2];

if (!signAnythingScope) {
return fail(`signAnythingScope should be true`);
}

if (!onlySignMessagesScope) {
return fail(`onlySignMessagesScope should be true`);
}

// ==================== Success ====================
return success(`ContractsSDK mints a PKP and set scope 1 and 2
Logs:
---
tokenId: ${mintInfo.pkp.tokenId}
transactionHash: ${mintInfo.tx.transactionHash}
signAnythingScope: ${signAnythingScope}
onlySignMessagesScope: ${onlySignMessagesScope}
`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { ethers } from 'ethers';
import { AuthMethodType } from '@lit-protocol/constants';

export async function main() {
// ========== Controller Setup ===========
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const controllerWallet = new ethers.Wallet(
LITCONFIG.CONTROLLER_PRIVATE_KEY,
provider
);

// ==================== LitContracts Setup ====================
const contractClient = new LitContracts({
signer: controllerWallet,
});

await contractClient.connect();

// ==================== Test Logic ====================
const mintCost = await contractClient.pkpNftContract.read.mintCost();

// -- minting a PKP
const mintTx =
await contractClient.pkpHelperContract.write.mintNextAndAddAuthMethods(
2,
[AuthMethodType.EthWallet],
[LITCONFIG.CONTROLLER_AUTHSIG.address], // auth id
['0x'], // only for web3auth atm
[[1]],
true, // addPkpEthAddressAsPermittedAddress,
true, // sendPkpToItself,
{
value: mintCost,
}
);

const mintTxReceipt = await mintTx.wait();

const tokenId = mintTxReceipt.events[0].topics[1];
console.log('tokenId', tokenId);

// -- get the scopes
const scopes =
await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
tokenId,
AuthMethodType.EthWallet,
LITCONFIG.CONTROLLER_AUTHSIG.address, // auth id
3 // we only offer 2 scopes atm. and index 0 doesn't exist, so either 1 = sign anything or 2 = only sign messages
);

// ==================== Post-Validation ====================
if (mintCost === undefined || mintCost === null) {
return fail('mintCost should not be empty');
}

if (scopes[1] !== true) {
return fail('scope 1 (sign anything) should be true');
}

// ==================== Success ====================
return success(`ContractsSDK mints a PKP
Logs:
---
mintHash: ${mintTxReceipt.transactionHash}
tokenId: ${tokenId}
scope 1 (sign anything): ${scopes[1]}
scope 2 (only sign messages): ${scopes[2]}
`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { ethers } from 'ethers';
import { AuthMethodType } from '@lit-protocol/constants';

export async function main() {
// ========== Controller Setup ===========
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const controllerWallet = new ethers.Wallet(
LITCONFIG.CONTROLLER_PRIVATE_KEY,
provider
);

// ==================== LitContracts Setup ====================
const contractClient = new LitContracts({
signer: controllerWallet,
});

await contractClient.connect();

// ==================== Test Logic ====================
const mintCost = await contractClient.pkpNftContract.read.mintCost();

// -- minting a PKP using a PKP
const mintTx = await contractClient.pkpNftContract.write.mintNext(2, {
value: mintCost,
});

const mintTxReceipt = await mintTx.wait();

const tokenId = mintTxReceipt.events[0].topics[1];
console.log('tokenId', tokenId);

// -- get the scopes
const scopes =
await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
tokenId,
AuthMethodType.EthWallet,
LITCONFIG.CONTROLLER_AUTHSIG.address, // auth id
3 // we only offer 2 scopes atm. and index 0 doesn't exist, so either 1 = sign anything or 2 = only sign messages
);

// -- validate both scopes should be false
if (scopes[1] !== false) {
return fail('scope 1 (sign anything) should be false');
}

if (scopes[2] !== false) {
return fail('scope 2 (only sign messages) should be false');
}

// -- set the scope
const setScopeTx =
await contractClient.pkpPermissionsContract.write.addPermittedAuthMethodScope(
tokenId,
AuthMethodType.EthWallet,
LITCONFIG.CONTROLLER_AUTHSIG.address, // auth id
1 // sign anything
);

const setScopeTxReceipt = await setScopeTx.wait();

// -- check the scopes again
const scopes2 =
await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
tokenId,
AuthMethodType.EthWallet,
LITCONFIG.CONTROLLER_AUTHSIG.address, // auth id
3 // we only offer 2 scopes atm. and index 0 doesn't exist, so either 1 = sign anything or 2 = only sign messages
);

// ==================== Post-Validation ====================
if (mintCost === undefined || mintCost === null) {
return fail('mintCost should not be empty');
}

if (scopes2[1] !== true) {
return fail('scope 1 (sign anything) should be true');
}

// ==================== Success ====================
return success(`ContractsSDK mints a PKP
Logs:
---
mintHash: ${mintTxReceipt.transactionHash}
tokenId: ${tokenId}
setScopeHash: ${setScopeTxReceipt.transactionHash}
scope 1 (sign anything): ${scopes2[1]}
scope 2 (only sign messages): ${scopes2[2]}
`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers';
import { ethers } from 'ethers';

async function getFundsFromPKPController() {
// ========== Controller Setup ===========
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const controllerWallet = new ethers.Wallet(
LITCONFIG.CONTROLLER_PRIVATE_KEY,
provider
);

// get the fund
console.log(
'Controller Balance:',
(await controllerWallet.getBalance()).toString()
);

// send some funds to the pkp
const amount = '0.0000001';

const tx = await controllerWallet.sendTransaction({
to: LITCONFIG.PKP_ETH_ADDRESS,
value: ethers.utils.parseEther(amount),
});

await tx.wait();

console.log(
'New Controller Balance:',
(await controllerWallet.getBalance()).toString()
);
console.log(`Sent ${amount} ETH to ${LITCONFIG.PKP_ETH_ADDRESS}`);
}

export async function main() {
// ========== PKP WALLET SETUP ===========
const pkpWallet = new PKPEthersWallet({
pkpPubKey: LITCONFIG.PKP_PUBKEY,
controllerAuthSig: LITCONFIG.CONTROLLER_AUTHSIG,
rpc: LITCONFIG.CHRONICLE_RPC,
});

await pkpWallet.init();

const pkpBalance = parseFloat(await pkpWallet.getBalance());

if (pkpBalance <= 0) {
console.log(
`PKP Balance is ${pkpBalance}. Getting funds from controller...`
);
await getFundsFromPKPController();
console.log('New PKP Balance:', (await pkpWallet.getBalance()).toString());
}

if (pkpWallet._isSigner !== true) {
return fail('pkpWallet should be signer');
}

// ==================== LitContracts Setup ====================
const contractClient = new LitContracts({
signer: pkpWallet,
});

await contractClient.connect();

// ==================== Test Logic ====================
const mintCost = await contractClient.pkpNftContract.read.mintCost();

// -- minting a PKP using a PKP
const mintTx =
await contractClient.pkpNftContract.write.populateTransaction.mintNext(2, {
value: mintCost,
});

const signedMintTx = await pkpWallet.signTransaction(mintTx);

const sentTx = await pkpWallet.sendTransaction(signedMintTx);

// ==================== Post-Validation ====================
if (mintCost === undefined || mintCost === null) {
return fail('mintCost should not be empty');
}

// ==================== Success ====================
return success(`ContractsSDK mint a PKP using PKP Ethers wallet
hash: ${sentTx.hash}`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import path from 'path';
import { success, fail, testThis } from '../../tools/scripts/utils.mjs';
import LITCONFIG from '../../lit.config.json' assert { type: 'json' };
import { LitContracts } from '@lit-protocol/contracts-sdk';
import { ethers } from 'ethers';

export async function main() {
// ========== Controller Setup ===========
const provider = new ethers.providers.JsonRpcProvider(
LITCONFIG.CHRONICLE_RPC
);

const controllerWallet = new ethers.Wallet(
LITCONFIG.CONTROLLER_PRIVATE_KEY,
provider
);

// ==================== LitContracts Setup ====================
const contractClient = new LitContracts({
signer: controllerWallet,
});

await contractClient.connect();

// ==================== Test Logic ====================
const mintCost = await contractClient.pkpNftContract.read.mintCost();

// -- minting a PKP using a PKP
const mintTx = await contractClient.pkpNftContract.write.mintNext(2, {
value: mintCost,
});

const mintTxReceipt = await mintTx.wait();

// ==================== Post-Validation ====================
if (mintCost === undefined || mintCost === null) {
return fail('mintCost should not be empty');
}

// ==================== Success ====================
return success(`ContractsSDK mints a PKP ${mintTxReceipt.transactionHash}`);
}

await testThis({ name: path.basename(import.meta.url), fn: main });
Loading
Loading