4. Prepare DON hosted secrets
Where are we?

How to use API key in Chainlink Functions?
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: DON-hosted secrets and offchain secrets.
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 Threshold encryption and it ensures the high system availability and fault tolerance.
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-toolkitis a NPM (NodeJS Package Manager) library that can be used to encrypt and upload secrets to DON.
Use Functions-Toolkit inside Remix
The Chainlink Functions Toolkit NPM package 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.
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.
Enable Chainlink Packages in Remix
Create a new script.js file in your workspace (ignore the screenshot's scripe name which shows test-script.js - you should just make a script.js file!)
Then, with that file open in Remix, we need to configure scripting dependencies inside Remix IDE as below: (Note: if you don't have a JS file open in Remix it may not show you these options in the configurations page).

Steps to upload secret to Chainlink Functions DON
We can encrypt the secrets and then upload to the Chainlink Functions DON using the below script. Copy the js code below and paste to your newly created script.js file.
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 functions.chain.link WebApp UI so we don't need to do this now - that's why createAndFundSub() is commented out at the bottom.
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.
Last updated