Solana developers can now use SecretPath to pass encrypted messages from Solana applications to a Secret Network smart contract. With this capability, Solana developers can build privacy-centric applications, such as:
Private DeFi protocols: Allow users to interact with decentralized finance applications while keeping transaction details private.
Confidential NFTs: Create NFTs where ownership or content can be privately verified and shared.
Encrypted messaging apps: Build decentralized applications where users can exchange encrypted messages, ensuring confidentiality.
Secure Voting Systems: Implement voting mechanisms where individual votes remain private but outcomes are publicly verifiable.
By integrating Solana with Secret Network via SecretPath, developers can bring enhanced privacy features to decentralized applications, improving security and user trust.
SecretPath seamlessly handles encrypted payloads, as the master gateway contract on Secret Network automatically decrypts payloads and passes the decrypted payload over to the target contract deployed on Secret Network.
If this sounds confusing, fret now, we will walk you through each step in subsequent Solana developer tutorials 😊
The encryption of the payload is done using the ChaCha20-Poly1305, an authenticated encryption with additional data (AEAD) algorithm.
The key for this symmetric encryption is created by using the Elliptic-curve Diffie-Hellman (ECDH) scheme, comprising of two components:
An extra encryption public key provided from the Secret Gateway Contract
A randomly created (ephemeral) encryption private key on the user side (independent of the user wallet's private key)
Combining both of these keys together via the ECDH Scheme yields our encryption key, which we use to encrypt the payload with ChaCha20-Poly1305.
Let's learn how to use SecretPath on Solana in the key-value store developer tutorial 😊.
Learn how to send encrypted strings from Solana to Secret Network
SecretPath seamlessly handles encrypted payloads on Solana, which means Solana developers can use SecretPath to encrypt and decrypt messages cross-chain on Secret Network!
In this tutorial, you will learn how to upload your own Key-value store program to Secret Network, which you can use to encrypt values on Solana and send them cross-chain to Secret Network!
By this end of this tutorial you will:
Have an understanding of SecretPath, Secret Network's trustless Solana and EVM bridge
Upload and instantiate your very own key value store contract on Secret Network
Pass an encrypted string from Solana to Secret Network
Connect a frontend to your Solana application
To get started, clone the repo:
Before you upload a key-value store program to Secret Network, it is important for you to understand how messages are sent from Solana to Secret Network and vice-versa. This is thanks to a protocol that Secret Network developed called SecretPath, which allows Solana developers to execute programs on Secret Network while preserving the privacy of the inputs and validity of the outputs using ECDH cryptography 🤯.
On Solana, smart contracts are referred to as "Programs," which is the terminology we will use for smart contracts on Solana and Secret Network throughout this tutorial.
SecretPath has two main components: gateways and relayers.
The Gateway is a Solana program created by Secret Network that acts as the interface for handling messages between Solana and Secret Network. The gateway program packages, verifies, and encrypts/decrypts messages. For your purposes, all that you must know is how to properly format your Secret Network program's functions so they can be understood by the Solana gateway program, which you will learn shortly.
Relayers watch for messages on Solana and then pass them to Secret Network, and vice-versa. They do not have access to the actual data (since messages are encrypted), so they can't compromise security. Their main job is ensuring the network runs smoothly by transmitting the messages, but they don’t move tokens or handle funds. For your purposes, you needn't focus on relayers as Secret Network maintains a relayer for all Solana transactions.
Now that you understand the basics of SecretPath, let's upload your very own key-value store program to Secret Network! 🤓
cd
into solana-kv-store/secret-contract
:
Let's examine the key-value store program. Open the src
folder and you will see four files:
Like Solana, programs on Secret Network are written in Rust. Unlike Solana, Secret Network developers use a framework called Cosmwasm instead of Anchor. Let's examine each of these files before uploading the program to Secret Network testnet.
On Solana, programs are stateless. On Secret Network, programs (ie smart contracts) are stateful. This means that unlike Solana's stateless programs, Secret Network programs maintain persistent state across transactions, allowing for private data storage and processing.
state.rs is where you manage your contract's state, which in this case is simply the program info for the master Solana Gateway Contract and the key-value pair that users will store in your program:
When you instantiate your program on Secret Network, you save the Solana Gateway Program info (gateway_address,
gateway_hash
, and gateway_key
) in the Secret program so that it knows how to correctly route messages to the Solana Gateway program.
Key-Value storage struct:
In Secret Network programs, a msg.rs
file typically defines the structure of messages that your program will send or receive. It outlines how the program interacts with external users, other programs, or blockchain modules, specifying what kind of data is expected in those interactions.
For example, on Solana, you'd define instruction data formats in a similar way when developing programs that interact with accounts. In CosmWasm (Secret Network's smart contract framework), a msg.rs
file similarly defines the data types for actions like instantiating, executing, and querying programs.
In the context of SecretPath, this msg.rs
file defines the messages that will be sent to and received from the master Solana gateway program, which is responsible for managing cross-chain communication.
In your case, you will be passing the InputStoreMsg
to state:
In the context of the Secret Network, the contract.rs
file is the core of your program logic, much like the Solana program's main instruction handler. It contains the main program functions, handling instantiation, execution, and migration, as well as any specific functionality you’ve built in.
In this file, you define how the program will handle inputs, store state, interact with other programs (like the Solana gateway in SecretPath), and process outputs. Let’s focus on the key function, store_value
, since that's the core of how key-value pairs are handled.
The store_value
function takes in data (input values, task info, and an input hash), stores a key-value pair in the program's storage, and then sends a confirmation message back to the Solana master gateway program.
The input values are deserialized to extract the key and value, which are saved in the program's storage:
A success message is created, serialized to JSON, encoded in base64, and sent as part of the response:
This function stores data securely on-chain in your Key-Value store program and notifies the master Solana gateway program once the task is complete.
If you want to make any changes to the Secret Network key-valye store program before compiling, feel free to do so! You can learn more about Secret Network contracts in the Secret Network docs here.
Now that you understand how the program works, let's upload it to Secret Network!
Before you upload the key-value store program to Secret Network, you first must compile the code.
Compile the program:
cd
into node
:
Install secretjs
:
Open upload.js
and examine the code. You needn't change anything here unless you want to upload the Secret Network program to Mainnet instead of testnet.
Upload and instantiate the program on Secret Network testnet:
You will see a contract id, codehash, and address returned:
Now let's use this program to store encrypted strings on Solana!
Now that you have deployed your key-value store program on Secret Network, all that's left is to execute it from Solana! To do this, you will use the IDL of the master Solana Gateway program in order to execute your program on Secret Network and correctly format the parameters that you pass to the master Solana Gateway program, which you will now learn how to do 🤓
Let's start by examining solana-kv-store/solana-frontend/src/submit.ts.
Submit.ts
is where you format and submit the transaction data to the Solana Gateway program so that it can execute your Secret Network key-value store program, and then send the callback message successfully back to the master Solana Gateway program 🤓
Notice that the IDL of the master Solana Gateway Program is imported into submit.ts
, which you can find here: Gateway Contract IDL.
Import the IDL using:
To start, we first define all of our variables that we need for the encryption, as well as the gateway information:
Replace routing_contract
and routing_code_hash
with your contract address and code hash! This is the Solana program that you just uploaded to Secret network testnet 😄
At this point, you can run the application exactly as is and the master Solana gateway program will correctly route to the program you deployed on Secret Network. But let's go more in depth so you understand how everything is working together 😄
Next, you generate ephermal keys and load in the public encryption key for the Solana Secret Gateway program. Then, use ECDH to create the encryption key:
Next, you define all of the information that you need for calling the key-value store program on Secret AND add the callback information for the message on its way back.
First, we define the function that we are going to call on your Secret key-value store program, which in this case is store_value
. Next, we add the parameters/calldata for this function and convert it into a JSON string:
1. Define the Function and Parameters (Calldata)
The first step is to define the function name and the parameters that you want to pass into your key-value store program on the Secret Network.
Function Name (handle
): This specifies the function you wish to invoke within the Secret program. In this example, "store_value"
is a function that stores a key-value pair.
Parameters (data
): This is the data that you will pass to the function in the Secret program. Here, we use a JSON string that contains the key-value pair.
2. Derive the Program Derived Addresses (PDAs)
In Solana, Program Derived Addresses (PDAs) are special types of addresses that are derived deterministically based on a seed and the program ID. Both PDAs are used here to store the gateway and the tasks' state. You do not need to manually save them as both of these can deterministically derived from the program id at any time.
gateway_pda
: This is the Program Derived Address associated with the gateway's state. It's derived from a seed ("gateway_state") and the program ID.
tasks_pda
: This is the Program Derived Address associated with the tasks' state. Similarly, it's derived from a seed ("task_state") and the program ID.
3. Define the Callback Information
The callback information specifies where and how the call should be handled. This involves setting a callback address and a callback selector.
Callback Address (callbackAddress
): These are the addresses where the callback addresses needed for the CPI are included. In this example, it’s simply a test address. In a real-world application, this would typically be your callback contract's address or the addresses designated to handle the callback. The callback addresses are the concatenated 32 bytes of all addresses that need to be accessed for the callback CPI. We take two address public keys (32 bytes each), concatenate them together and then base64
encode them.
Next, we define the callback selector. The callback selector is a unique identifier that indicates which program and function the callback CPI should access. It's a combination of the program ID and a specific function identifier.
Function Identifier (functionIdentifier
): This is an array of bytes that uniquely identifies the function within the contract that should process the callback. In this example, the identifier corresponds to a hypothetical function "CallbackTest"
in the SecretPath Solana program.
Program ID (programId
): This is callback program on Solana that is involved for the callback.
Callback Selector (callbackSelector
): This is a combination of the programId
and the functionIdentifier
, and it uniquely identifies the callback program and function within the Solana program.
Finally, we specify the callback compute limit or callback gas limit:
Callback Gas Limit (callbackGasLimit
): This represents the amount of gas that should be allocated to process the callback on the Solana side. It's important to estimate this correctly to ensure that the callback can be executed without running out of resources.
After defining the contract call and callback, we now construct the payload:
Next, we encrypt the payload using ChaCha20-Poly1305. Then, we hash the encrypted payload into a ciphertextHash
using Keccak256.
Next, we use Phantom to sign the payloadHash
using signMessage
.
Internally, Phantom Wallet only allows for ASCII encoded strings to be signed to prevent any wallet drainers from signing arbitrary bytes. For us this means that we take the payloadHash
and base64
encode it. Phantom then actually directly signs the base64
string (NOT: the payloadHash
directly) of the payloadHash
to get the signature. Keep this in mind when verifying the signature against the payloadHash.
Lastly, we pack all the information we collected during previous steps into an info
struct that we send into the Solana Gateway program. We encode the function data. Finally, we set the tx_params
. Please make sure to set an appropriate gas amount for your contract call, here we used 150k gas. For the value of the TX, we send over the estimated callback gas that we calculated above.
Now that you know how to correctly format and package data for the Solana Gateway program, let's learn how to connect your program to a frontend 🚀
Open a new terminal window and cd
into solana-kv-store/solana-frontend:
Install the dependencies:
Next, initialize the Solana client that you are using to interact with the program. Connect to Phantom wallet and set up the Anchor provider with the Program IDL imported earlier.
Now it's time to run the program! In the terminal:
Congrats! You now have your very own Solana key-value store program deployed on Secret Network and can encrypt strings
on Solana!
In conclusion we constructed and encrypted a payload for SecretPath for Solana direct in the Frontend as well as calling the SecretPath gateway program.
Need help with using encrypted payloads with Secretpath or want to discuss use cases for your dApp? Please ask in the Secret Network Telegram or Discord.
Remember earlier when you learned that you must instantiate your Secret Network program to correctly communicate with the master Solana Gateway Program? Now that you are uploading your Secret Network key-value store program, note that you are instantiating it with the master Solana Gateway address, code_hash, and public_key
Learn how to send encrypted on-chain random numbers to Solana from Secret Network
SecretVRF, Secret Network's on-chain random number generator, enables Solana developers to access encrypted on-chain verifiable random numbers from Secret Network.
In this tutorial, you will learn how to send random numbers from Secret Network to a Solana program. By this end of this tutorial you will:
Have an understanding of SecretPath, Secret Network's trustless Solana and EVM bridge
Upload and instantiate your very own randomness contract on Secret Network
Request random numbers on Solana from Secret Network
Connect a frontend to your Solana application
Try out requesting a random number on Solana Devnet from Secret Network testnet using the demo here!
Simply request a random number (up to 2000), and click "submit." You can view your returned random number on the gateway program here.
After you make the request, you will see two transaction signatures within a few seconds of each other. Open the second transaction signature (which contains the callback info with your random numbers) in a new window, then, scroll down to the program instruction logs to view the returned random numbers as a base64 string
:
Way to go! You just sent an encrypted on-chain random number from Secret Network to a Solana program. Now it's time to learn how to upload your own randomness contract on Secret Network so that you can program it as you see fit. Let's get started 😊
To get started, clone the repo:
Before you upload a randomness program to Secret Network, it is important for you to understand how messages are sent from Solana to Secret Network and vice-versa. This is thanks to a protocol that Secret Network developed called SecretPath, which allows Solana developers to execute programs on Secret Network while preserving the privacy of the inputs and validity of the outputs using ECDH cryptography 🤯.
On Solana, smart contracts are referred to as "Programs," which is the terminology we will use for smart contracts on Solana and Secret Network throughout this tutorial.
SecretPath has two main components: gateways and relayers.
The Gateway is a Solana program created by Secret Network that acts as the interface for handling messages between Solana and Secret Network. The gateway program packages, verifies, and encrypts/decrypts messages. For your purposes, all that you must know is how to properly format your Secret Network program's functions so they can be understood by the Solana gateway program, which you will learn shortly.
Relayers watch for messages on Solana and then pass them to Secret Network, and vice-versa. They do not have access to the actual data (since messages are encrypted), so they can't compromise security. Their main job is ensuring the network runs smoothly by transmitting the messages, but they don’t move tokens or handle funds. For your purposes, you needn't focus on relayers as Secret Network maintains a relayer for all Solana transactions.
Now that you understand the basics of SecretPath, let's upload your very own RNG program to Secret Network! 🤓
cd
into solana-rng/rng
:
Let's examine the RNG program. Open the src
folder and you will see four files:
Like Solana, programs on Secret Network are written in Rust. Unlike Solana, Secret Network developers use a framework called Cosmwasm instead of Anchor. Let's examine each of these files before uploading the program to Secret Network testnet.
On Solana, programs are stateless. On Secret Network, programs (ie smart contracts) are stateful. This means that unlike Solana's stateless programs, Secret Network programs maintain persistent state across transactions, allowing for private data storage and processing.
state.rs is where you manage your contract's state, which in this case is simply the program info for the master Solana Gateway Contract:
When you instantiate your program on Secret Network, you save the Solana Gateway Program info (gateway_address,
gateway_hash
, and gateway_key
) in the Secret program so that it knows how to correctly route messages to the Solana Gateway program.
In Secret Network programs, a msg.rs
file typically defines the structure of messages that your program will send or receive. It outlines how the program interacts with external users, other programs, or blockchain modules, specifying what kind of data is expected in those interactions.
For example, on Solana, you'd define instruction data formats in a similar way when developing programs that interact with accounts. In CosmWasm (Secret Network's smart contract framework), a msg.rs
file similarly defines the data types for actions like instantiating, executing, and querying programs.
In the context of SecretPath, this msg.rs
file defines the messages that will be sent to and received from the master Solana gateway program, which is responsible for managing cross-chain communication.
In the context of the Secret Network, the contract.rs
file is the core of your program logic, much like the Solana program's main instruction handler. It contains the main program functions, handling instantiation, execution, and migration, as well as any specific functionality you’ve built in.
In this file, you define how the program will handle inputs, store state, interact with other programs (like the Solana gateway in SecretPath), and process outputs. Let’s focus on the key function, try_random
, since that's the core of how randomness is handled.
The try_random
function is designed to generate random numbers on Secret Network and send them to the Solana Gateway program. Here's a high-level explanation:
The randomness generation in the contract creates a buffer to store random data, fills it using a pseudorandom number generator (PRNG), and sends the result to the gateway.
You can modify this code by changing the buffer size, applying custom post-processing like converting to hex, combining randomness with on-chain data, generating numbers in specific ranges, using a different algorithm like a hash function, etc. The sky's the limit!
After generating random numbers, the contract returns the result back to the master gateway program. The randomness result is packaged into a PostExecutionMsg
and passed to the Solana gateway as a callback.
If you want to make any changes to the Secret Network RNG program before compiling, feel free to do so! You can learn more about Secret Network contracts in the Secret Network docs here.
Now that you understand how the program works, let's upload it to Secret Network!
Before you upload the randomness program to Secret Network, you first must compile the code.
Compile the randomness contract:
cd
into node
:
Install secretjs:
Open upload.js
and examine the code. You needn't change anything here unless you want to upload the Secret Network program to Mainnet instead of testnet.
Remember earlier when you learned that you must instantiate your Secret Network program to correctly communicate with the master Solana Gateway Program? Now that you are uploading your Secret Network randomness program, note that you are instantiating it with the master Solana Gateway address, code_hash, and public_key :D
Upload and instantiate the program on Secret Network testnet:
You will see a contract id, codehash, and address returned:
Now let's use this program to request random numbers on Solana!
Now that you have deployed your randomness program on Secret Network, all that's left is to execute it from Solana! To do this, you will use the IDL of the master Solana Gateway program in order to execute your randomness program on Secret Network and correctly format the parameters that you pass to the master Solana Gateway program, which you will now learn how to do 🤓
Let's start by examining solana-rng/solana-frontend/src/submit.ts
.
Submit.ts
is where you format and submit the transaction data to the Solana Gateway program so that it can execute your Secret Network randomness program, and then send the randomness back to the Solana Gateway program 🤓
Notice that the IDL of the master Solana Gateway Program is imported into submit.ts
, which you can find here: Gateway Contract IDL.
Import the IDL using:
To start, we first define all of our variables that we need for the encryption, as well as the gateway information.
Next, you generate ephermal keys and load in the public encryption key for the Solana Secret Gateway program. Then, use ECDH to create the encryption key:
Next, you define all of the information that you need for calling the RNG program on Secret AND add the callback information for the message on its way back.
First, we define the function that we are going to call on your Secret RNG program, which in this case is request_random
. Next, we add the parameters/calldata for this function, which is ("{ numWords: Number(numWords) }"
and convert it into a JSON string
.
1. Define the Function and Parameters (Calldata)
The first step is to define the function name and the parameters that you want to pass into your RNG program on the Secret Network.
Function Name (handle
): This specifies the function you wish to invoke within the Secret program. In this example, "request_random"
is a function that generates a random number.
Parameters (data
): This is the data that you will pass to the function in the Secret program. Here, we use a JSON string that contains the number of random words (numWords
) that the function will generate.
2. Derive the Program Derived Addresses (PDAs)
In Solana, Program Derived Addresses (PDAs) are special types of addresses that are derived deterministically based on a seed and the program ID. Both PDAs are used here to store the gateway and the tasks' state. You do not need to manually save them as both of these can deterministically derived from the program id at any time.
gateway_pda
: This is the Program Derived Address associated with the gateway's state. It's derived from a seed ("gateway_state") and the program ID.
tasks_pda
: This is the Program Derived Address associated with the tasks' state. Similarly, it's derived from a seed ("task_state") and the program ID.
3. Define the Callback Information
The callback information specifies where and how the call should be handled. This involves setting a callback address and a callback selector.
Callback Address (callbackAddress
): These are the addresses where the callback addresses needed for the CPI are included. In this example, it’s simply a test address. In a real-world application, this would typically be your callback contract's address or the addresses designated to handle the callback. The callback addresses are the concatenated 32 bytes of all addresses that need to be accessed for the callback CPI. We take two address public keys (32 bytes each), concatinate them together and then base64
encode them.
Next, we define the callback selector. The callback selector is a unique identifier that indicates which program and function the callback CPI should access. It's a combination of the program ID and a specific function identifier.
Function Identifier (functionIdentifier
): This is an array of bytes that uniquely identifies the function within the contract that should process the callback. In this example, the identifier corresponds to a hypothetical function "CallbackTest"
in the SecretPath Solana program.
Program ID (programId
): This is callback program on Solana that is involved for the callback.
Callback Selector (callbackSelector
): This is a combination of the programId
and the functionIdentifier
, and it uniquely identifies the callback program and function within the Solana program.
Finally, we specify the callback compute limit or callback gas limit:
Callback Gas Limit (callbackGasLimit
): This represents the amount of gas that should be allocated to process the callback on the Solana side. It's important to estimate this correctly to ensure that the callback can be executed without running out of resources.
After defining the contract call and callback, we now construct the payload:
Next, we encrypt the payload using ChaCha20-Poly1305. Then, we hash the encrypted payload into a ciphertextHash
using Keccak256.
Next, we use Phantom to sign the payloadHash
using signMessage
.
Internally, Phantom Wallet only allows for ASCII encoded strings to be signed to prevent any wallet drainers from signing arbitrary bytes. For us this means that we take the payloadHash
and base64
encode it. Phantom then actually directly signs the base64
string (NOT: the payloadHash
directly) of the payloadHash
to get the signature. Keep this in mind when verifying the signature against the payloadHash.
Lastly, we pack all the information we collected during previous steps into an info
struct that we send into the Solana Gateway program. We encode the function data. Finally, we set the tx_params
. Please make sure to set an appropriate gas amount for your contract call, here we used 150k gas. For the value of the TX, we send over the estimated callback gas that we calculated above.
Now that you know how to correctly format and package data for the Solana Gateway program, let's learn how to connect your program to a frontend 🚀
Open a new terminal window and cd
into solana-rng/solana-frontend:
Install the dependencies:
Next, initialize the Solana client that you are using to interact with the program. Connect to Phantom wallet and set up the Anchor provider with the Program IDL imported earlier.
Now it's time to run the progam! In the terminal:
Congrats! You now have your very own Solana RNG program deployed on Secret Network and can request encrypted random numbers on Solana!
In conclusion we constructed and encrypted a payload for SecretPath for Solana direct in the Frontend as well as calling the SecretPath gateway program.
Need help with using encrypted payloads with Secretpath or want to discuss use cases for your dApp? Please ask in the Secret Network Telegram or Discord.
Replace routing_contract
and routing_code_hash
with your contract address and code hash! This is the Solana program that you just uploaded to Secret network testnet
At this point, you can run the application exactly as is and the master Solana gateway program will correctly route to the program you deployed on Secret Network. But let's go more in depth so you understand how everything is working together