Skip to main content

Installation

npm install privacycash --save
Requires Node.js 24+. The SDK is written in TypeScript and includes type definitions.

Deriving Encryption Key

Client need to ask user to sign an offchain message, before the user can interact with Privacy Cash.
async function getSignedSignature(signed: Signed) {
    if (signed.signature) {
        return
    }
    const encodedMessage = new TextEncoder().encode(`Privacy Money account sign in`)
    const cacheKey = `zkcash-signature-${signed.publicKey.toBase58()}`

    // ask for sign
    let signature: Uint8Array
    try {
        signature = await signed.provider.signMessage(encodedMessage)
    } catch (err: any) {
        if (err instanceof Error && err.message?.toLowerCase().includes('user rejected')) {
            throw new Error('User rejected the signature request')
        }
        throw new Error('Failed to sign message: ' + err.message)
    }
    
    if (currentSigned.pubKeyStr != signed.publicKey.toString()) {
        throw new Error(`Don't switch account when signing in. Refresh the page and try again.`)
    }

    // If wallet.signMessage returned an object, extract `signature`
    // @ts-ignore
    if (signature.signature) {
        // @ts-ignore
        signature = signature.signature
    }

    if (!(signature instanceof Uint8Array)) {
        throw new Error('signature is not an Uint8Array type')
    }
    signed.signature = signature
}

export type Signed = {
    publicKey: PublicKey,
    signature?: Uint8Array,
    provider: any
}
Once the user signed the message, pass the resulting signature to EncryptionService of the SDK:
export * from "privacycash/utils"

let encryptionService = new EncryptionService();
encryptionService.deriveEncryptionKeyFromSignature(signed.signature);

Interacting with Privacy Cash

Once above steps are done, client can query balance, make deposits and withdrawals.

Common Issues

  1. Next.js project needs to update the postinstall build scripts:
    "scripts": {
        "postinstall": "cp node_modules/@lightprotocol/hasher.rs/dist/hasher_wasm_simd_bg.wasm node_modules/@lightprotocol/hasher.rs/dist/browser-fat/es/ && cp node_modules/@lightprotocol/hasher.rs/dist/light_wasm_hasher_bg.wasm node_modules/@lightprotocol/hasher.rs/dist/browser-fat/es/",
    },..}