Token gate videos with Lit
Learn how to token gate videos using Livepeer and Lit: A step-by-step tutorial
This tutorial is based on Livepeer React version 3.9 or earlier, which is now deprecated. Please ensure that you use Livepeer React version 4 or later, with the new Livepeer JavaScript SDK. The integration process may appear different, but the underlying concepts remain same.
Token gating is a method of restricting access to videos based on ownership of a specific type of token or cryptocurrency. It can be used to restrict access to certain videos to those who hold a specific type of token, such as an NFT (non-fungible token). One of the primary benefits of token gating is that it allows creators to monetise their content more effectively. It can also provide creators with more control over their content and its distribution. By restricting access to certain videos to those who hold a specific token, creators can ensure that their content is only being viewed by those who have a genuine interest in it.
Lit Protocol is a decentralised key management network powered by threshold cryptography. A blockchain-agnostic middleware layer, Lit can be used to read and write data between blockchains and off-chain platforms, powering conditional decryption and programmatic signing.
When combined with Livepeer, developers can build token-gated video applications and allow creators to token-gate their video with specific tokens, NFTs, addresses, etc. In this tutorial, you will learn how to build a token-gated video application with Lit and Livepeer.
Here is a high-level workflow diagram of how Livepeer’s access control feature works and how we will use it in our app:
Prerequisites
Before you start with this tutorial, make sure you have the following tool installed on your machine:
- Latest Node.js version
- An Ethereum wallet such as Metamask or Rainbow
In addition to the above tools, this tutorial assumes that you have a basic understanding of Next.js
Setting up Next.js App
First, create a directory for the project and then initialize a Next.js app using the following command in your terminal:
This will create a new Next.js app in the current directory and install all the necessary dependencies.
While creating a Next.js application, you will be prompted if you would want to use Tailwind. In this tutorial, we will be using Tailwind, so make sure to set up a Next.js app with Tailwind.
Once the project is created successfully, run the following command to install a few other dependencies.
Setting up Clients
In this tutorial, we will be using different packages and SDKs which means we have to set up the context and clients for each of them. Let’s start with RainbowKit and WAGMI
RainbowKit and WAGMI
For this tutorial, we will be using Rainbow Kit and WAGMI to handle authentication such as connecting the wallet and signing message.
First, inside of _app.js
import the following from Rainbow and WAGMI.
Then, configure your desired chains (in this tutorial, we will be using Ethereum mainnet) and generate the required connectors. You will also need to set up a WAGMI client.
Finally, wrap your application with RainbowKitProvider
and WagmiConfig
Livepeer
Livepeer is a decentralized video infrastructure protocol that allows users to upload, transcode, and serve video content. The Livepeer React SDK provides a set of ready-to-use hooks that make it easy to integrate Livepeer into a project.
To get started, navigate to https://livepeer.studio/register and create a new account on Livepeer Studio. This will give you access to your Livepeer dashboard, where you can manage your account and access your API keys.
Once you have created an account, in the dashboard, click on the Developers on the sidebar.
Then, click on Create API Key, give a name to your key, and then copy it as we will need it later.
To use Livepeer React in your project, create a new directory named client
in
the root directory, and add the following code to index.js
Make sure to replace the YOUR_API_KEY
with the key which you just copied from
the Livepeer dashboard. And also replace the code inside of _app.js
in the
page directory with the below code.
And that is it, you can now use Livepeer to upload and transcode assets.
Lit Protocol
As mentioned above, we will be using Lit Protocol for token gating/access
control in our application. First, create a new directory named hooks
and
inside of it, create a new file named useLit
. This would be the file to handle
the connection with Lit’s node. Add the following code to it:
Here, we have created a provider and a custom hook to manage the connection to a Lit node client throughout the application. This is useful if you want to share the same instance of the Lit node client across multiple components in the app.
Next, add the following lit imports to _app.js
and also wrap the application
with LitProvider
Backend
The first step is to set up a background route that will handle the webhook and
verify the token. Since we are using Next.js, we can simply use the api
routes.
Create a Webhook Handler
The first step is to create an access control webhook handler. We need to set up an endpoint (e.g., https://yourdomain.com/api/check-access) to handle the logic for granting or denying access to your assets. This endpoint should accept a POST request with a JSON payload containing the access key and webhook context.
This is an example of a payload this endpoint would receive:
In the Next.js api
directory, create a new file named verify-lit-jwt.js
and
add the following code to it
The above code verifies a JWT access token and checks if its payload matches the
webhook context’s resource ID. The Lit SDK is used to verify the JWT access
token, which must contain a payload object with baseUrl
, path
, orgId
,
iat
, and exp
fields.
If the access token is not verified or if the resource ID does not match, an HTTP response with a status code of 403 is returned. If everything is fine, the handler returns an HTTP response with a status code of 200.
Next, deploy your Next.js app to Vercel or any other hosting provider as we will need a link to the verify JWT API.
Register an Access Control Webhook
Once you deployed, you can use the Livepeer Studio dashboard to create a new
webhook with the type playback.accessControl
and specify the URL of your
access control endpoint.
Once created, copy the id of Webhook as we will need it later in the next step.
Frontend
Now that we have set up the clients and backend, we can move to the front end and add the upload and access control features to our app.
Wallet Connection
First, we need to allow users to connect their wallets. Create a new directory
named components
and inside of it, create a new file named Connect
and add
the following code to it.
It is a simple component that imports ConnectButton from Rainbow which would handle the authentication.
Upload page
In the pages directory, we can use index.jsx
as the upload page, or you can
also create a new file named upload.jsx
for the upload page. In this tutorial,
we will be using index.jsx
as the upload page and it will be the index page of
our app.
First import the following in the index.jsx
file.
After that, we have to create a resourceId
that will be used by Lit for
checking the access control.
Next, add the following hooks inside the component. The code contains a few
useStates for managing the state, the lit hook which we created earlier, and
finally the useAccount
hook that is imported from WAGMI.
Next, we need to pre-sign the user’s wallet after it is connected using Lit. To do that, you can add the following code after the useAccount hook.
In the above code, we are checking if the user is connected, if they are, the
app asks to sign the message and then save it to AuthSign
state.
Next, after signing the message, we can use useCreateAsset
to upload the video
to Livepeer. In the below code, we also specify the webhook id, which we created
earlier. And useAsset
hook to check if the asset upload/processing is
completed
You can also add the below code to check the status of the upload.
Add the following code that would get called once everything is complete to save the signing conditions on Lit.
Finally, we can write a function that would call the createAsset to upload the video.
Next, in the return function add the following JSX code. It is pretty long, but I am going to explain it.
The first section contains a banner image and some text that describes the purpose of token gating on the Livepeer platform. Then we have two input fields. The first input field allows the user to select a video file to upload. The second input field allows the user to set access control conditions. Once a user clicks on the second input, it displays a modal window that allows the user to customize the access control conditions using the LitShareModal component.
And that is it for the upload page. Save the file and you should see a similar page:
Watch Page
For this watch page, you can create a new directory named watch
inside the
page and then create a new file named [playbackId].tsx
. This will tell Next.js
to match any link of /watch/anything
to this page. Next, import the following
into the page.
Then, add the following hooks inside of the page:
Next, we would need to fetch the playback info using the playback id. We can do
this very easily using the usePlaybackInfo
hook from @livepeer/react
Next, we need a function to check whether the users have access. This function can easily be handled with Lit SDK
And finally, we can add a useEffect
function to get playback info and check
Lit access control when the component mounts.
And that is it. We can add the following code to the return function to render the player if the JWT is valid, otherwise show a message that the user doesn’t have access.
And that is it. Save the file. Now try uploading a video and then once it is uploading come to the watch screen and check if you have access to it. Here is a short video of the whole flow:
Token gate multiple assets with the same ACL condition
If you want to token gate multiple assets with the same access control conditions, then you can simply get unified access control conditions from the Lit SDK and the same resources Id and then send the same info to as many assets as would like to token gate.
The access controls conditions and resource id should be inside of the Webhook Context.
Remove the ACL conditions
If you want to remove the access control conditions from an asset. You need to update both the Livepeer Studio API as well as the Lit state. However, this is only possible, if the access control condition is not chosen permanently.
Was this page helpful?