Skip to content

Commit

Permalink
docs: add, remove auth methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahzdev committed Jun 19, 2023
1 parent d783726 commit 6b47af0
Show file tree
Hide file tree
Showing 7 changed files with 163 additions and 7 deletions.
4 changes: 0 additions & 4 deletions docs/pkp/_category_.json

This file was deleted.

124 changes: 124 additions & 0 deletions docs/pkp/authHelpers/addRemoveAuthMethods.md
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.
19 changes: 17 additions & 2 deletions docs/pkp/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ The [`@lit-protocol/pkp-ethers` package](https://github.com/LIT-Protocol/js-sdk/

## Initialize `PKPEthersWallet`

`PKPEthersWallet` must be initialized 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';

Expand All @@ -18,9 +20,22 @@ const pkpWallet = new PKPEthersWallet({
await pkpWallet.init();
```

The `controllerAuthSig` (or `controllerSessionSigs`) is used to authorize requests to the Lit nodes. To learn how to leverage different authentication methods, refer to the [Authentication section](/SDK/Explanation/authentication).
To view more `PKPEthersWallet` constructor options, refer to 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,
}
```

To view more constructor options, refer to the [API docs](https://js-sdk.litprotocol.com/interfaces/types_src.PKPEthersWalletProp.html).
:::

## Sign Message

Expand Down
2 changes: 2 additions & 0 deletions docs/resources/supportedChains.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ Don't see a blockchain you want? Fill out this form for EVM chains and we'll ad
- moonbaseAlpha
- filecoin
- hyperspace
- sepolia
- scrollAlphaTestnet
- zksync
- zksyncTestnet
- lineaGoerli
- litprotocol
- solana
Expand Down
8 changes: 8 additions & 0 deletions netlify.toml
Original file line number Diff line number Diff line change
Expand Up @@ -613,3 +613,11 @@ force = false
to = "/concepts/pkpsAsWallet"
status = 301
force = false

# 6/19/23

[[redirects]]
from = "/pkp/authHelpers"
to = "/pkp/authHelpers/overview"
status = 301
force = false
13 changes: 12 additions & 1 deletion sidebars.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,18 @@ const sidebars = {
items: [
"pkp/intro",
"pkp/minting",
"pkp/authHelpers",
{
type: "category",
label: "Authentication with PKPs",
link: {
type: "doc",
id: "pkp/authHelpers/overview",
},
collapsed: true,
items: [
"pkp/authHelpers/addRemoveAuthMethods",
],
},
"pkp/pkpsAndActions",
"pkp/usage",
"pkp/walletconnect",
Expand Down

0 comments on commit 6b47af0

Please sign in to comment.