Installation and initialization
Before using any SPF functions, installation and initialization are required. This page covers installing the TypeScript client library, initialization, and creating signers for authentication.
Installation
The @sunscreen/spf-client package is available on npm. Install it in your project:
npm install @sunscreen/spf-client
The package includes TypeScript definitions and works in both Node.js and browser environments.
Initialization
Before calling any SPF functions, the client must be initialized. This loads the required modules and fetches the SPF public key. All encryption, upload, run, and decryption operations require initialization.
export async function basicInitialization(): Promise<void> {
// Initialize the SPF client
await spf.initialize();
}
Creating signers
Many SPF operations require authentication via a signer. The client library provides a lightweight PrivateKeySigner class, and also supports ethers.js signers (MetaMask, etc.) for compatibility with existing applications should you need it.
export function createPrivateKeySigner(): {
random: spf.PrivateKeySigner;
fromKey: spf.PrivateKeySigner;
address: string;
} {
// Create a random signer for testing
const randomSigner = spf.PrivateKeySigner.random();
// Create from an existing private key
const existingSigner = new spf.PrivateKeySigner(
spf.asPrivateKey(
"0x1234567890abcdef1234567890abcdef1234567890abcdef1234567890abcdef",
),
);
// Get the address
const address = randomSigner.getAddress();
return {
random: randomSigner,
fromKey: existingSigner,
address,
};
}
Note that the application developer determines how to securely manage private keys. The PrivateKeySigner class does not provide key storage or management features.
Complete initialization example
Here is a complete example showing initialization and signer creation:
export async function completeInitExample(): Promise<string> {
// Initialize the client
await spf.initialize();
console.log("SPF client initialized");
// Create a signer for authentication
const signer = spf.PrivateKeySigner.random();
console.log("Signer address:", signer.getAddress());
// Return the address for testing
return signer.getAddress();
}
You can now move on to using other SPF features such as upload ciphertexts, running computations, and decrypting results.