Skip to content

Commit

Permalink
mint by auth method (#253)
Browse files Browse the repository at this point in the history
* mint by auth method

* remove url

* feat: fix authmethod type 6

* add a note

* feat: add enums

* remove declare

* rename from 'mint' to 'mintWithAuth' for better clarity

* fix: ignore_dirs
  • Loading branch information
Ansonhkg authored Nov 10, 2023
1 parent 64fef0d commit 245aa76
Show file tree
Hide file tree
Showing 14 changed files with 579 additions and 7 deletions.
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.mintWithAuth({
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

0 comments on commit 245aa76

Please sign in to comment.