4. Prepare DON hosted secrets
Last updated
Last updated
In this workshop, we choose save the Supabase anon API key in the DON. Before the secrets upload, we need to encrypt the API key with the DON's mater key. @chainlink/functions-toolkit
is a NPM (NodeJS Package Manager) library that can be used to encrypt and upload secrets to DON.
The Remix Foundation and Chainlink have partnered to make some Chainlink NPM packages available directly inside remix, so you don't need to write separate scripts and run them in VS Code etc.
First we need to configure scripting dependencies inside Remix IDE as below:
We can encrypt the secrets and then upload to the Chainlink Functions DON using the below script. Create a new file in file explorer called script.js
. Copy the js code below and paste to the file.
IMPORTANT: Ensure that you have to add your Supabase anon key to the script (Get the key from your Supabase Organization > Project > Project Settings > API Keys):
Also ensure that these two function calls inside main()
at the bottom of the script are uncommented.
Then click on the green "Play" button(see the image below) on the upper left of editor and watch the Remix IDE terminal for updates.
Metamask will then open twice to get you to confirm the transaction and sign the upload request. Once you've confirmed the Metamask transactions the script will communicate with the Chainlink Functions DON gateways and send your encrypted secrets to be hosted on the DON securely.
When successfully uploaded you should see something like:
For now, the secret is encrypted and saved in DON, and you will have a secret version code in your Remix Terminal. Make a note of the version number printed (and do not clear your Remix console) as you will need this as input shortly.
The secret version will expire in 24 hours. If you want to update the secrets within 24 hours, run the command again and the old secret will be overwrited.
Before the Chainlink Functions help us to send an API call, it has to know the Supabase API "anon" key with which to communicate with our database. Chainlink Functions provides 2 ways to host secrets that the DON can consume when executing the custom JS: and .
In Chainlink Functions, secrets are encrypted as a security feature, regardless of which of the 2 ways you use. Instead of a centralized private key, the DON uses a Master Key and partitions the master key into distinct shares and each node in the DON has only one "shard" of the key. This encryption method is called and it ensures the high system availability and fault tolerance.
The is a collection of JS helper functions that help you interact with Chainlink Functions smart contracts to create and manage subscriptions, encrypt and upload secrets, make requests etc. The README contains extensive documentation.
Note that to illustrate how powerful Functions Toolkit is we have left in code that creates and funds a Functions Subscription programmatically. We had previously done this via the WebApp UI so we don't need to do this now - that's why createAndFundSub()
is commented out at the bottom.