-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
163 additions
and
7 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
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,124 @@ | ||
# Adding and Removing Auth Methods | ||
|
||
Configure auth methods for your PKP using the [`@lit-protocol/contracts-sdk`](https://js-sdk.litprotocol.com/modules/contracts_sdk_src.html) package. | ||
|
||
To write to the blockchain, the `LitContracts` instance must be created with a `ethers.Signer` that is authorized to sign transactions using the PKP. The [`@lit-protocol/pkp-ethers` package](https://js-sdk.litprotocol.com/modules/pkp_ethers_src.html) provides a convenient class, `PKPEthersWallet`, which can be used as a signer. | ||
|
||
## Initialize `PKPEthersWallet` | ||
|
||
`PKPEthersWallet` must be instantiated with an `AuthSig` or a `SessionSig` in order to authorize signing requests. To learn how to generate these signatures, refer to the [Authentication section](/SDK/Explanation/authentication). | ||
|
||
```js | ||
import { PKPEthersWallet } from '@lit-protocol/pkp-ethers'; | ||
|
||
const pkpWallet = new PKPEthersWallet({ | ||
controllerAuthSig: '<Your AuthSig>', | ||
// Or you can also pass in controllerSessionSigs | ||
pkpPubKey: '<Your PKP public key>', | ||
rpc: 'https://chain-rpc.litprotocol.com/http' | ||
}); | ||
await pkpWallet.init(); | ||
``` | ||
|
||
To view more constructor options for `PKPEthersWallet`, check out the [API docs](https://js-sdk.litprotocol.com/interfaces/types_src.PKPEthersWalletProp.html). | ||
|
||
:::note | ||
|
||
**Passing `SessionSigs`** | ||
|
||
When generating session signatures for `PKPEthersWallet`, be sure to request the ability to execute Lit Actions by passing the following object in the `resourceAbilityRequests` array: | ||
|
||
```js | ||
{ | ||
resource: new LitActionResource('*'), | ||
ability: LitAbility.LitActionExecution, | ||
} | ||
``` | ||
|
||
::: | ||
|
||
## Initialize `LitContracts` | ||
|
||
Create an instance of `LitContracts` and pass in your `PKPEthersWallet`. | ||
|
||
```js | ||
import { LitContracts } from '@lit-protocol/contracts-sdk'; | ||
|
||
const litContracts = new LitContracts({ | ||
signer: pkpWallet, | ||
}); | ||
await litContracts.connect(); | ||
``` | ||
|
||
To view more constructor options for `LitContracts`, check out the [API docs](https://js-sdk.litprotocol.com/classes/contracts_sdk_src.LitContracts.html#constructor). | ||
|
||
## Construct the `AuthMethod` Object | ||
|
||
To add or remove an auth method, you must pass an `authMethod` object, which should have the following properties: | ||
|
||
- `authMethodType`: A number representing the type of auth method you want to add. Refer to the supported auth methods table [here](/pkp/authHelpers#existing-supported-auth-methods). | ||
- `id`: Bytes that represent a hash of a string that uniquely identifies the auth method | ||
- `userPubkey`: Public key of a WebAuthn credential (only required when using WebAuthn as an auth method) | ||
|
||
You can use the [`@lit-protocol/lit-auth-client` package](https://js-sdk.litprotocol.com/modules/lit_auth_client_src.html) to first authenticate the user and then derive the unique auth method `id`. | ||
|
||
To authenticate the user, check out these [guides](/SDK/Explanation/authentication/sessionSigs/authMethods/overview) that walk you through the process of obtaining authentication material from a `LitAuthClient` auth provider. Once the user is authenticated, you can call the `getAuthMethodId` utility function. | ||
|
||
```js | ||
import { getAuthMethodId } from '@lit-protocol/lit-auth-client'; | ||
|
||
// Pass in the auth method object generated by a LitAuthClient auth provider | ||
const authMethodId = await getAuthMethodId('<Auth method object from the auth provider>'); | ||
``` | ||
|
||
## Add an Auth Method | ||
|
||
Auth methods can be modified by interacting with the [PKPPermissions contract](https://github.com/LIT-Protocol/LitNodeContracts/blob/main/contracts/PKPPermissions.sol). To add an auth method, call the `addPermittedAuthMethod` function on the `PKPPermissions` contract. | ||
|
||
```js | ||
const transaction = await litContracts.pkpPermissionsContract.write.addPermittedAuthMethod( | ||
'<The token ID of the PKP you want to add an auth method to>', | ||
'<The auth method object you want to add>', | ||
[], | ||
{ gasPrice: utils.parseUnits('0.001', 'gwei'), gasLimit: 400000 } | ||
); | ||
const result = await transaction.wait(); | ||
``` | ||
|
||
The `addPermittedAuthMethod` function takes the following arguments: | ||
|
||
- `tokenId`: The token ID of the PKP you want to add an auth method to | ||
- `authMethod`: The auth method you want to add | ||
- `overrides`: An optional object that allows you to customize [certain parameters](https://docs.ethers.org/v5/api/contract/contract/#contract-functionsSend) of the transaction (e.g, `gasPrice`, `gasLimit`) | ||
|
||
|
||
## Remove an Auth Method | ||
|
||
Similarly, to remove an auth method, call the `removePermittedAuthMethod` function on the `PKPPermissions` contract. | ||
|
||
```js | ||
const transaction = await litContracts.pkpPermissionsContract.write.removePermittedAuthMethod( | ||
'<The token ID of your PKP>', | ||
'<The auth method you want to remove>', | ||
{ gasPrice: utils.parseUnits('0.001', 'gwei'), gasLimit: 400000 } | ||
); | ||
``` | ||
|
||
The `removePermittedAuthMethod` function takes the following arguments: | ||
|
||
- `tokenId`: The token ID of the PKP you want to remove an auth method from | ||
- `authMethod`: The auth method you want to remove | ||
- `overrides`: An optional object that allows you to customize [certain parameters](https://docs.ethers.org/v5/api/contract/contract/#contract-functionsSend) of the transaction (e.g, `gasPrice`, `gasLimit`) | ||
|
||
|
||
## Fetch Auth Methods | ||
|
||
To check that the auth method was added or removed successfully, call the `getPermittedAuthMethods` function on the `PKPPermissions` contract. | ||
|
||
```js | ||
const authMethods = await litContracts.pkpPermissionsContract.read.getPermittedAuthMethods( | ||
'<The token ID of your PKP>' | ||
); | ||
``` | ||
|
||
The `getPermittedAuthMethods` function returns an array of `authMethod` objects, each of which will include `authMethodType`, `id`, and `userPubkey` values. |
File renamed without changes.
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
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
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
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