Deciding who can and can't see your private data, and how to do it.
Permissioned viewing is entirely unique to Secret Network with its encrypted state. It gives you ability to have precise control over who has access to what data. In order to determine when data is allowed to be released however, you need to have a way to prove who is trying to access it. Our two ways of doing this are through Viewing Keys and Permits/Certs.
With the new Shockwave alpha update, Permits have become more efficient in almost every application. The exception is inter-contract communication permissions. Here, viewing keys still hold a lead. However, in some cases you may want to include an option to use both in your smart contract and leave the decision up to the users. Some users will have a preference for one over the other.
Permits are the successor to viewing keys. With increases in efficiency made in the Shockwave update they are now the defacto viewing permission method in almost every situation.
Permits are simply a dummy transaction signed by a wallet. The wallet is able to use this signature as proof that the transactor is who they say they are. Any wallet capable of using Tendermint is able to carry out this process.
For an in-depth implementation of query permits, navigate to the snip-24 section of the docs here.
Generating a permit happens outside of the smart-contract, but checking them in your smart contract is extremely simple. Once again the Secret-Toolkit provides a package for us, aptly named permit
. The function that checks the permit is called validate
shown below.
To use this, simply call validate
inside of your query provided with the necessary permit variables.
Permits can be used at any time, and are amazing for almost every permissioned viewing application. The only time it may be more efficient to use viewing keys instead is during inter-contract communication.
Introduction to Secret Network viewing keys with code examples
Viewing keys are passwords meant to validate users at times when the blockchain cannot. Specifically in queries, the query sender isn't authenticated and the contract doesn't know who is the querier. Therefore viewing keys were invented to provide a way of access control for users:
Alice sends a transaction set_viewing_key(password)
The contract stores (alice,password)
Later on, a query is sent to the contract query("balance",alice,password)
If (alice,password)
matches what's in storage, the contract returns Alice's balance to the querier.
To see an implementation of viewing keys in a Secret smart contract, check out the
Secret Network developed the for creating and managing viewing keys in Secret smart contracts. The methods provided include:
set_seed
: Sets an initial pseudorandom number generator (PRNG) seed for the store.
create
: Creates a new viewing key, saves it to the storage, and returns it.
set
: Sets a new viewing key based on a predetermined value.
check
: Checks if a viewing key matches an account.
To make use of the secret-toolkit viewing keys package, import it into your cargo.toml
file:
This contract is designed to create top secret messages that can only be decrypted if the user has the correct viewing key that is associated with the secret_message
struct.
This function stores a secret message at a specified index in the contract's storage, which is mapped to a viewing key. This ensures that the secret message can only be accessed by the correct viewing key, and that each secret message has its own unique viewing key.
Let's go over it in more detail:
A new viewing key is created by calling ViewingKey::create
. The parameters passed include the mutable dependencies (which includes the storage), the environment env
, the MessageInfo
, the sender account, and a hard-coded entropy value b"entropy"
.
The secret message is then stored in the contract's storage and is mapped to the viewing key. The SECRET_MESSAGE.insert
line is performing this task.
Next, the viewing key itself is stored in the contract's storage, but this time indexed with a user-defined index
parameter and prefixed by the sender's account address. This is done by VIEWING_KEY.add_suffix(info.sender.as_bytes()).insert(deps.storage, &index, &viewing_key)
.
Finally, the function returns a default Response
instance indicating successful execution of the function.
If you would like to see an example implementation of Secret Network viewing keys, see the .
Let's review the function:
Viewing keys provide a mechanism for access control in blockchain applications when the blockchain itself can't authenticate the query sender. Secret-toolkit
allows developers to set seeds, create and check viewing keys, and set a new viewing key based on a predetermined value
Should you have further questions, please reach out on and a Secret developer will get back to you shortly