Today, we will review the implementation of Switchboard Secrets within your oracle feed! We recommend setting up a simple feed first by following the Developers: Build and Use! page first before attempting to embed a secret in your feed! Switchboard-Secrets is designed for maximum speed, security, and simplicity. Switchboard offers both a Rust and TypeScript SDK for initializing your feed object as well as embedding and retrieving secrets. You may get started with the JavaScript/TypeScript SDKs via:
npm i @switchboard-xyz/on-demand @switchboard-xyz/common
Using these SDK's you can design your data feed to securely embed and use secrets just like your regular Switchboard on-demand data feeds.
Step 1: Set Up Your Environment:
Install the required packages.
Initialize your environment variables and key pairs.
The function sbSecrets.createOrUpdateUserRequest takes in 3 parameters:
1.userPubkey(string): The public key address of the user.
2.ciphersuite(string): Specifies the cryptographic suite used for signing. For Solana, use "ed25519" , for EVM, use "ethers".
3.contactInfo(string): This optional field can be used to store stringified contact information, such as an email address or other contact details. Can be left empty "".
Once created, you can query your user profile like this!
The function sbSecrets.createSecretRequest takes in 4 parameters:
userPubkey(string): The public key address of the user.
ciphersuite(string): Specifies the cryptographic suite used for signing. For Solana, use "ed25519" , for EVM, use "ethers".
secretName(string): Key of the secret, must be unique per user profile.
secretValue(string): Value of the secret.
Step 3: Create Your Feed
Using the SDK, you can now design your data feed just like your Switchboard-v2 data feeds but now using an embedded secret.
Note - due to variable expansion, make sure that the name of your embedded secret in your feed is passed in correctly.
Invokes a secrets task to fetch the secret to embed in the httpTask.
Fetching the weather REST service from openweathermap.org for the temperature in Aspen.
Using JSONPATH syntax to parse out the temperature field
Now lets finish creating the feed below and initializing it!
constconf= {// the feed name (max 32 bytes) name:"Feed Weather Temp Aspen",// the queue of oracles to bind to queue,// the jobs for the feed to perform jobs: [buildSecretsJob(secretNameTask, keypair)],// allow 1% variance between submissions and jobs maxVariance:1.0,// minimum number of responses of jobs to allow minResponses:1,// number of signatures to fetch per update numSignatures:3, };// Initialize the feed if neededlet pullFeed:PullFeed;if (argv.feed ===undefined) {// Generate the feed keypairconst [pullFeed_,feedKp] =PullFeed.generate(program);consttx=awaitInstructionUtils.asV0TxWithComputeIxs( program, [awaitpullFeed_.initIx(conf)],1.2,// The compute units to cap the tx as a multiple of the simulated units consumed (e.g. 1.2x)75_000// The price per compute unit in microlamports );tx.sign([keypair, feedKp]);// Simulate the transaction to get the price and send the txawaitconnection.simulateTransaction(tx, txOpts);console.log("Sending initialize transaction");constsig=awaitconnection.sendTransaction(tx, txOpts);awaitconnection.confirmTransaction(sig,"processed");console.log(`Feed ${feedKp.publicKey} initialized: ${sig}`); pullFeed = pullFeed_; } else { pullFeed =newPullFeed(program,newPublicKey(argv.feed)); }
For a more detailed description of configuring your feed visit this page -
Step 4: Whitelist your Feed to your Secret
As an added safety measure, you are required to whitelist your feed to your secret. This is accomplished by obtaining the hash of your feed and whitelisting it ot the secret!
Here we go..