Just getting started building on the Internet Computer? Plug can be a great starting point! By integrating into Plug, you can solve core features of your project (authentication & identity ā in-app transactions) by just interacting with the Plug extension & the userās wallet in Plug.
Weāve been building several application examples using Plug at different levels. This way, you can go from zero knowledge to building an app that uses Plug to authenticate users, trigger in-app ICP transactions, and sign canister requests using the PlugAgent.
Letās do a quick recap of what Plug is for users and developers. You can skip this part by clicking here if you know it already š
What is Plug for a general user? š
Plug is a browser wallet extension for the Internet Computer. You install it on your browser, and it allows you to hold, use, and transfer ICP and other Internet Computer tokens in a browser wallet experience.
Your wallet address in Plug? A Principal ID that is both your wallet address and the identifier that will serve as your identity in IC applications.
Much like MetaMask on Ethereum, you can use the extension to log into applications that support it as an authentication provider and use your tokens in those applications.
What is Plug for a developer? š§°
So, if users can use Plug to sign into integrated apps with their wallet/identity in Plug, and use their tokens in-appā¦
As a developer, you can integrate Plug to enable just that! Plug is an authentication provider and provides pluggable ICP/Token features by allowing users to interact with their balances through actions in your app.
In a nutshell, when Plug is present on a visitorās browser (installed and with an active wallet), Plug injects the IC Provider API that enables any app/website to talk to the Plug extension and the userās wallet. That way, your application can use the app to call a userās wallet in Plug and request to:
- Connect to the userās wallet and authenticate them as users.
- Give users access to their ICP (or another token) balances in-app.
- Trigger transfer requests from app interactions (e.g. āTip this postā).
- Sign canister requests with the userās identity in Plug.
Level 1š: Meet the IC Provider API & Plugged
All right. Level 1. Some intro theory first.
Letās get the basics out of the way! Dig into the first resources thatāll get you familiar with the IC Provider API, the main thing you will be using to integrate Plug into your app.
The IC Provider is an API that is injected by the Plug extension onto the browser/website the user visits. As an app/site you will be calling that API to ātalkā with the userās wallet. That is how Plug is integrated!
- First step: Get familiar with our introduction to the IC Provider API.
- Second step: Install Plug, and interact with the Plugged app.
- Third step: Check out Pluggedās build guide to learn how it works.
Plugged is an E2E application, with buttons that trigger the API calls that the injected IC Provider API has available for you as a developer.
Install Plugās latest version, and visit the Plugged live demo page to play around with the interactions and see how works from the userās perspective, and what each method outputs to the console, for developers.
Level 2ā: Buy Me A Coffee ā ICP ātip meā button.
Done with the basics? Move on to level 2!
Hereās the guide (link!) weāll be using for the āBuy me a coffeeā project!
In Level 2, you will be creating a website that connects to a visitorās wallet in Plug, and requests a transfer of ICP from the user to a predefined address (can be your Plug Principal ID or any Account ID).
Hereās a live demo š (Replace the āxxxā in the URL with a valid Principal ID or Account ID, and the āyyyā by an amount like 1000, to set the parameters the button will use to trigger the test transaction to the Plug wallet! Make sure to hit send to visit the URL with the set parameters).
The user will be able to click a button that you created to launch a modal from Plug that gives the option to āDeclineā or āAllowā the userās request to connect to Plug.
If the user clicks āAllowā ā the button changes to āPlug is now connectedā & launches a modal requesting a transfer or ātipā of ICP to the address you specify, with the amount you specify as well.
This example doesnāt involve authentication and therefore doesnāt need to use the PlugAgent. It teaches you the basic building block of how to requestConnect, and make an ICP transfer from the userās wallet using the requestTransfer() method.
Good to go? Cheers! Youāre ready to level up to Level 3 and start working with the PlugAgent to authenticate users, and use the agent to sign canister requests with their identity to, for exampleā¦
Check the NNS canisterās stats in a web-app UI!
Level 3š: NNS ā An NNS statistics application.
Letās get started with Level 3.
As before, hereās the guide weāll be using for Level 3 (link).
In Level 3, youāll be creating an app that authenticates using Plugās PlugAgent, and uses the userās identity to request global statistics from the NNS/UI Canister.
This oneās a little bit different. Instead of just doing requestConnect(), weāll do so but also requesting permission to use the PlugAgent. When we do so, we have to requestConnect() and also pass a whitelist of canister IDs we want to get authorized (the NNS canister in this case).
This is because when you perform an action on an IC application or website (for example, checking the NNS stats) behind the scenes, you -as a user- are telling the canister that hosts that app (NNS) to do that action (tell me the stats!) for you.
Those requests are signed with an identity, and the PlugAgent allows applications to āproxyā sign THOSE requests using the userās keys (without exposing them) so that the user can perform the action they want.
In this project, the user will authorize you to use the PlugAgent to sign canister requests with their identity for very specific reasons only, checking the NNS canisterās stats.
Once the connection is allowed by the user, you will be able to use the agent and create an actor that represents the canister.
To complete, weāll use the actorās interface to interact with the NNS canister, mankind the necessary request to get the stats, and show them on the frontend of the appā¦ and VoilĆ” weāre done!
Creating custom signed calls through Plug enables the full circle of user authentication, where not only do you gather a userās identity to authorize them into the app as a user (ID = John); but ensure all actions on the app are performed by the userās identity (John says: āCall NNS stats!ā).
Itās DIY Time! šā”
Thatās all levelsā¦ For now! With everything youāve learned, you should be in pretty good shape to start exploring your first DIY (do it yourself!) custom project with Plug. Got an idea in mind?
Go for it! Hop on our Discord if you need any help, or tweet at us to share what youāre jamming on with the team! šø