@privora/sdk package provides a TypeScript interface for Privora.
Installation
Copy
npm install @privora/sdk
Quick Start
Copy
import { Privora } from '@privora/sdk';
import { Keypair } from '@solana/web3.js';
const privora = await Privora.connect('http://localhost:8899');
const payer = Keypair.generate();
// Encrypt
const encrypted = privora.encrypt(100, 'u8');
// Submit
const hash = await privora.submit(encrypted);
// Send transaction
const signature = await privora
.transaction()
.add(instruction)
.withFheData([hash])
.sign(payer)
.send();
Encryption
Copy
// Type-specified encryption
const encrypted_u8 = privora.encrypt(100, 'u8');
const encrypted_u32 = privora.encrypt(10000, 'u32');
const encrypted_u64 = privora.encrypt(1000000, 'u64');
// Batch encryption
const batch = privora.encryptBatch([
{ value: 100, type: 'u8' },
{ value: 200, type: 'u8' },
]);
User Recovery
Copy
const userCrypto = privora.userCrypto(keypair);
// Encrypt with recovery
const encrypted = privora
.encrypt(100, 'u8')
.withUserRecovery(userCrypto);
// Decrypt locally
if (encrypted.userRecovery) {
const plaintext = userCrypto.decryptRecovery(encrypted.userRecovery);
}
Transactions
Copy
const signature = await privora
.transaction()
.add(instruction1)
.add(instruction2)
.withFheData([hash1, hash2])
.sign(payer)
.send();
Complete Example
Copy
import { Privora } from '@privora/sdk';
import { Keypair, PublicKey, TransactionInstruction } from '@solana/web3.js';
const PROGRAM_ID = new PublicKey('...');
async function submitOrder(price: number, quantity: number) {
const privora = await Privora.connect('http://localhost:8899');
const payer = Keypair.generate();
await privora.requestAirdrop(payer.publicKey, 1_000_000_000);
const userCrypto = privora.userCrypto(payer);
// Encrypt with recovery
const encPrice = privora.encrypt(price, 'u8').withUserRecovery(userCrypto);
const encQty = privora.encrypt(quantity, 'u8').withUserRecovery(userCrypto);
// Submit
const priceHash = await privora.submit(encPrice);
const qtyHash = await privora.submit(encQty);
// Build instruction
const data = Buffer.concat([
Buffer.from([1]),
Buffer.from(priceHash, 'hex'),
Buffer.from(qtyHash, 'hex'),
]);
const instruction = new TransactionInstruction({
programId: PROGRAM_ID,
keys: [{ pubkey: payer.publicKey, isSigner: true, isWritable: true }],
data,
});
// Send
const signature = await privora
.transaction()
.add(instruction)
.withFheData([priceHash, qtyHash])
.sign(payer)
.send();
return signature;
}