Compute
Perform computation using secrets stored in the network.
Get a Quote to Compute
- Get quote to compute
- getQuote helper
src/nillion/components/ComputeForm.tsx
await nillion.default();
const operation = nillion.Operation.compute(
programId,
additionalComputeValues
);
const quote = await getQuote({
client: nillionClient,
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 Compute and get Payment Receipt
- Payment receipt
- helper functions
src/nillion/components/ComputeForm.tsx
if (nillionClient && quote?.operation) {
setLoadingPayment(true);
const [nilChainClient, nilChainWallet] =
await createNilChainClientAndWalletFromPrivateKey();
const paymentReceipt = await payWithWalletFromPrivateKey(
nilChainClient,
nilChainWallet,
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);
}
Compute
- compute
- computeProgram helper
src/nillion/components/ComputeForm.tsx
setPaymentReceipt(paymentReceipt);
const value = await computeProgram({
nillionClient,
receipt: paymentReceipt,
programId,
storeIds,
inputParties,
outputParties,
outputName,
src/nillion/helpers/compute.ts
import { config } from './nillion';
import * as nillion from '@nillion/client-web';
interface ComputeProgram {
nillionClient: nillion.NillionClient;
receipt: nillion.PaymentReceipt;
programId: string;
storeIds: string[];
inputParties: ComputeParty[];
outputParties: ComputeParty[];
outputName: string;
additionalComputeValues: nillion.NadaValues;
}
interface ComputeParty {
partyName: string;
partyId: string;
}
export async function computeProgram({
nillionClient,
receipt,
programId,
storeIds,
inputParties,
outputParties,
outputName,
additionalComputeValues,
}: ComputeProgram): Promise<any> {
await nillion.default();
let program_bindings = new nillion.ProgramBindings(programId);
inputParties.forEach(({ partyName, partyId }: ComputeParty) => {
program_bindings.add_input_party(partyName, partyId);
});
outputParties.forEach(({ partyName, partyId }: ComputeParty) => {
program_bindings.add_output_party(partyName, partyId);
});
try {
const compute_result_uuid = await nillionClient.compute(
config.clusterId,
program_bindings,
storeIds,
additionalComputeValues,
receipt
);
console.log(compute_result_uuid);
const compute_result =
await nillionClient.compute_result(compute_result_uuid);
console.log(compute_result);
const result = compute_result[outputName].toString();
return result;
} catch (error) {
return error;
}
}