/
test-contracts-write-mint-a-pkp-and-set-scope-1-advanced.mjs
85 lines (70 loc) · 2.34 KB
/
test-contracts-write-mint-a-pkp-and-set-scope-1-advanced.mjs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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';
import { LitAuthClient } from '@lit-protocol/lit-auth-client';
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();
const authMethod = {
authMethodType: 1,
accessToken: JSON.stringify(LITCONFIG.CONTROLLER_AUTHSIG),
};
const authId = LitAuthClient.getAuthIdByAuthMethod(authMethod);
// -- minting a PKP
const mintTx =
await contractClient.pkpHelperContract.write.mintNextAndAddAuthMethods(
2,
[AuthMethodType.EthWallet],
[authId],
['0x'], // only for web3auth atm
[[1]],
true, // addPkpEthAddressAsPermittedAddress,
true, // sendPkpToItself,
{
value: mintCost,
}
);
const mintTxReceipt = await mintTx.wait();
const tokenId = mintTxReceipt.events[0].topics[1];
// -- get the scopes
const scopes =
await contractClient.pkpPermissionsContract.read.getPermittedAuthMethodScopes(
tokenId,
AuthMethodType.EthWallet,
authId,
3
);
// ==================== 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 });