Retrieve Value
Retrieve secret strings and integers from the network. Retrieve and decode a stored secret string (SecretBlob) or a stored secret integer (SecretInteger)
Get a Quote to Retrieve Value
- Get quote to retrieve secret
- getQuote helper
src/nillion/components/RetrieveSecretForm.tsx
const operation = nillion.Operation.retrieve_value();
const quote = await getQuote({
client: nillionClient,
operation,
});
src/nillion/helpers/getQuote.ts
import { config } from './nillion';
import * as nillion from '@nillion/client-web';
export async function getQuote({
client,
operation,
}: {
client: nillion.NillionClient;
operation: nillion.Operation;
}) {
return await client.request_price_quote(config.clusterId, operation);
}
Pay to Retrieve Value and get Payment Receipt
- Payment receipt
- helper functions
src/nillion/components/RetrieveSecretForm.tsx
const [nilChainClient, nilChainWallet] =
await createNilChainClientAndWalletFromPrivateKey();
const paymentReceipt = await payWithWalletFromPrivateKey(
nilChainClient,
nilChainWallet,
quote
);
src/nillion/helpers/nillion.ts
export async function createNilChainClientAndWalletFromPrivateKey(): Promise<
[SigningStargateClient, DirectSecp256k1Wallet]
> {
const key = Uint8Array.from(
config.chain.keys[0].match(/.{1,2}/g)!.map((byte) => parseInt(byte, 16))
);
const wallet = await DirectSecp256k1Wallet.fromKey(key, 'nillion');
const registry = new Registry();
registry.register(typeUrl, MsgPayFor);
const options = {
registry,
gasPrice: GasPrice.fromString('0.0unil'),
};
const client = await SigningStargateClient.connectWithSigner(
config.chain.endpoint,
wallet,
options
);
return [client, wallet];
}
export async function payWithWalletFromPrivateKey(
nilChainClient: SigningStargateClient,
wallet: DirectSecp256k1Wallet,
quoteInfo: any
): Promise<PaymentReceipt> {
const { quote } = quoteInfo;
const denom = 'unil';
const [account] = await wallet.getAccounts();
const from = account.address;
const payload: MsgPayFor = {
fromAddress: from,
resource: quote.nonce,
amount: [{ denom, amount: quote.cost.total }],
};
const result = await nilChainClient.signAndBroadcast(
from,
[{ typeUrl, value: payload }],
'auto'
);
return new PaymentReceipt(quote, result.transactionHash);
}
Retrieve SecretBlob or SecretInteger
- Retrieve value
- retrieveSecret helper
src/nillion/components/RetrieveSecretForm.tsx
const value = await retrieveSecret({
nillionClient,
store_id: storeId,
secret_name: secretName,
receipt: paymentReceipt,
});
src/nillion/helpers/retrieveSecret.ts
import * as nillion from '@nillion/client-web';
import { config } from './nillion';
interface RetrieveSecret {
nillionClient: nillion.NillionClient;
store_id: string;
secret_name: string;
receipt: nillion.PaymentReceipt;
}
export async function retrieveSecret({
nillionClient,
store_id,
secret_name,
receipt,
}: RetrieveSecret) {
const retrieved = await nillionClient.retrieve_value(
config.clusterId,
store_id,
secret_name,
receipt
);
try {
const intValue = retrieved.to_integer();
return intValue;
} catch (err) {
// gets byte array value
const byteArraySecret = retrieved.to_byte_array();
// decodes byte array to string
const decoded = new TextDecoder('utf-8').decode(byteArraySecret);
return decoded;
}
}