Lens SDK
This package enables gated encrypting and decrypting publication metadata compatible with the Lens API. Works on Node.js 18+ and the browser.
Installation
npm
npm install @lens-protocol/sdk-gated
yarn
yarn add @lens-protocol/sdk-gated
Usage
Pretty straightforward, instantiate the LensGatedSDK client providing the required parameters.
Make sure you are encrypting valid MetadataV2 objects or you will get errors. See more on valid
metadata standards here. To validate metadata using the Lens API before
encrypting, see here.
import { Web3Provider } from '@ethersproject/providers';
import {
AndCondition,
OrCondition,
FollowCondition,
CollectCondition,
EncryptedMetadata,
EoaOwnership,
Erc20TokenOwnership,
MetadataV2,
NftOwnership,
ProfileOwnership,
PublicationMainFocus,
ContractType,
ScalarOperator,
LensGatedSDK,
LensEnvironment,
} from '@lens-protocol/sdk-gated';
let metadata: MetadataV2 = {
version: '2.0.0',
name: 'name',
description: 'description',
attributes: [],
content: 'content',
metadata_id: '1',
appId: 'app_id',
mainContentFocus: PublicationMainFocus.TextOnly,
locale: 'en',
};
const uploadMetadataHandler = async (data: EncryptedMetadata): Promise<string> => {
return Promise.resolve('test');
};
const nftAccessCondition: NftOwnership = {
contractAddress: '0x0000000000000000000000000000000000000000',
chainID: 80001,
contractType: ContractType.Erc721,
tokenIds: ['1', '2', '3'],
};
const eoaAccessCondition: EoaOwnership = {
address: '0x0000000000000000000000000000000000000000',
chainID: 80001,
};
const erc20AccessCondition: Erc20TokenOwnership = {
contractAddress: '0x0000000000000000000000000000000000000000',
chainID: 80001,
amount: '1000000000000000000',
decimals: 18,
condition: ScalarOperator.GreaterThanOrEqual,
};
const profileAccessCondition: ProfileOwnership = {
profileId: '0x01',
};
const followAccessCondition: FollowCondition = {
profileId: '0x01',
};
const collectAccessCondition: CollectCondition = {
publicationId: '0x01-0x01',
};
const andCondition: AndCondition = {
criteria: [
{
token: erc20AccessCondition,
},
{
follow: followAccessCondition,
},
],
};
const orCondition: OrCondition = {
criteria: [
{
collect: collectAccessCondition,
},
{
profile: profileAccessCondition,
},
],
}(async () => {
const sdk = await LensGatedSDK.create({
provider: new Web3Provider(window.ethereum),
signer: signer,
env: LensEnvironment.Mumbai,
});
await sdk.connect({
address: '0x1234123412341234123412341234123412341234',
env: LensEnvironment.Polygon,
});
const { error, contentURI, encryptedMetadata } = await sdk.gated.encryptMetadata(
metadata,
'0x01',
{
nft: nftAccessCondition,
},
uploadMetadataHandler
);
console.log(contentURI);
const { decrypted } = await sdk.gated.decryptMetadata(encryptedMetadata);
await sdk.disconnect();
})();
Access Control
The currently supported access control models are:
- Follow condition: decrypts content if you follow a given profile id
- Collect condition: decrypts content if you have collected a given publication
- Profile ownership: decrypts content if you own a given profile NFT
- NFT ownership: generic NFT ownership condition for all remaining usecases like:
- If you own an NFT from a given collection (Bored Apes etc.)
- If you own an NFT within a specific range of Token IDs (Your first 100 followers)
- ERC20 token ownership: decrypts content if you own X amount of a given ERC20 token (People with more than 0.1 WETH)
- EOA ownership: decrypts content exclusively to a given EOA address
- Boolean combinations of the above (using AND and OR operators)
- Please note that boolean combinations are limited to 5 conditions at a time and cannot be nested.