-
Notifications
You must be signed in to change notification settings - Fork 73
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
Ansonhkg
merged 8 commits into
feat/SDK-V3
from
feature/lit-1859-example-of-setting-permission-scopes
Nov 10, 2023
Merged
mint by auth method #253
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b48fd1e
mint by auth method
Ansonhkg fdb17ae
remove url
Ansonhkg ab03b17
feat: fix authmethod type 6
Ansonhkg 0024a86
add a note
Ansonhkg e48dd4c
feat: add enums
Ansonhkg 017979c
remove declare
Ansonhkg 972ac5e
rename from 'mint' to 'mintWithAuth' for better clarity
Ansonhkg 0e8c8fe
fix: ignore_dirs
Ansonhkg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
72 changes: 72 additions & 0 deletions
72
e2e-nodejs/group-contracts/test-contracts-write-mint-a-pkp-and-set-scope-1-2-easy.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({ | ||
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 }); |
78 changes: 78 additions & 0 deletions
78
e2e-nodejs/group-contracts/test-contracts-write-mint-a-pkp-and-set-scope-1-advanced.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
98 changes: 98 additions & 0 deletions
98
e2e-nodejs/group-contracts/test-contracts-write-mint-a-pkp-then-set-scope-1.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
96 changes: 96 additions & 0 deletions
96
...nodejs/group-contracts/test-contracts-write-mint-a-pkp-with-a-pkp-with-no-permissions.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
44 changes: 44 additions & 0 deletions
44
e2e-nodejs/group-contracts/test-contracts-write-mint-a-pkp-with-no-permissions.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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