Store Secrets
Store secret values (SecretBlob or SecretInteger values) in the network by the cluster, secret values, permissions, and payment receipt.
tip
Open the Nillion Python Client Reference in another tab to search for available Nillion Client classes.
Store a SecretBlob
Create a SecretBlob, with one or more secret strings encoded as bytearrays, get a quote to store values, pay to store values, get a payment receipt, and finally store the SecretBlob
examples_and_tutorials/core_concept_store_and_retrieve_secrets/store_and_retrieve_blob.py
load_dotenv(f"{home}/.config/nillion/nillion-devnet.env")
# Store and retrieve a SecretBlob using the Python Client
async def main():
# Use the devnet configuration generated by `nillion-devnet`
network = Network.from_config("devnet")
# Create payments config and set up Nillion wallet with a private key to pay for operations
nilchain_key: str = os.getenv("NILLION_NILCHAIN_PRIVATE_KEY_0") # type: ignore
payer = NilChainPayer(
network,
wallet_private_key=NilChainPrivateKey(bytes.fromhex(nilchain_key)),
gas_limit=10000000,
)
signing_key = PrivateKey()
client = await VmClient.create(signing_key, network, payer)
# Adding funds to the client balance so the upcoming operations can be paid for
funds_amount = 3000000
print(f"💰 Adding some funds to the client balance: {funds_amount} uNIL")
await client.add_funds(funds_amount)
##### STORE SECRET
print("-----STORE SECRET")
# Create a SecretBlob
secret_name = "my_blob"
# create a bytearray from the string using UTF-8 encoding
secret_value = bytearray("gm, builder!", "utf-8")
# Create a secret
values = {
secret_name: SecretBlob(secret_value),
}
# Create a permissions object to attach to the stored secret
permissions = Permissions.defaults_for_user(client.user_id)
# Store the secret
values_id = await client.store_values(
values, ttl_days=5, permissions=permissions
).invoke()
print(f"The secret is stored at: {values_id}")
##### RETRIEVE SECRET
print("-----RETRIEVE SECRET")
Store a SecretInteger
Create a SecretInteger, with one or more secret integers, get a quote to store values, pay to store values, get a payment receipt, and finally store the SecretInteger
examples_and_tutorials/core_concept_store_and_retrieve_secrets/store_and_retrieve_integer.py
# Store and retrieve a SecretInteger using the Python Client
async def main():
# Use the devnet configuration generated by `nillion-devnet`
network = Network.from_config("devnet")
# Create payments config and set up Nillion wallet with a private key to pay for operations
nilchain_key: str = os.getenv("NILLION_NILCHAIN_PRIVATE_KEY_0") # type: ignore
payer = NilChainPayer(
network,
wallet_private_key=NilChainPrivateKey(bytes.fromhex(nilchain_key)),
gas_limit=10000000,
)
signing_key = PrivateKey()
client = await VmClient.create(signing_key, network, payer)
# Adding funds to the client balance so the upcoming operations can be paid for
funds_amount = 3000000
print(f"💰 Adding some funds to the client balance: {funds_amount} uNIL")
await client.add_funds(funds_amount)
##### STORE SECRET
print("-----STORE SECRET")
# Create a SecretInteger
secret_name = "my_int1"
secret_value = 100
values = {
secret_name: SecretInteger(secret_value),
}
# Create a permissions object to attach to the stored secret
permissions = Permissions.defaults_for_user(client.user_id)
# Store the secret
values_id = await client.store_values(
values=values, ttl_days=5, permissions=permissions
).invoke()
print(f"The secret is stored at store_id: {values_id}")
##### RETRIEVE SECRET
print("-----RETRIEVE SECRET")
retrieved_values = await client.retrieve_values(values_id).invoke()
value: SecretInteger = retrieved_values[secret_name] # type: ignore
print(f"The secret value is {value.value}")