Key-value Store Developer Tutorial
Learn how to send encrypted strings from Solana to Secret Network
Overview
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
Key Value Store Demo
Getting Started
To get started, clone the repo:
Solana Prerequisites
Understanding SecretPath ✅
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! 🤓
Understanding the Key-Value store program on 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!
Uploading the Key-Value store program 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.
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 😄
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!
Key-Value storage 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 😄
Generating the encryption key using ECDH
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:
Define the Calldata for the Secret program & Callback information
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 theprogramId
and thefunctionIdentifier
, 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:
Encrypting the Payload
Next, we encrypt the payload using ChaCha20-Poly1305. Then, we hash the encrypted payload into a ciphertextHash
using Keccak256.
Signing the Payload with Phantom Wallet
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.
Packing the Transaction & Send
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 🚀
Connecting Your Solana Key-value store program to Frontend
Open a new terminal window and cd
into solana-kv-store/solana-frontend:
Install the dependencies:
Initializing the Solana Client
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!
Summary
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.
Last updated