@fluppy/core
Protocol-level primitives shared by @fluppy/browser and @fluppy/react. Pure TypeScript — no browser APIs, no React, no wallet dependency.
Module Map
constants.tsBN254_R, CIRCUIT_DEPTH, N_PUBLIC, POSEIDON_TAGS, DEFAULT_MAX_AMOUNT, usdcToStroops()errors.tsFluppyError class hierarchy and parseFluppyError() parserencoding.tsG1/G2 Soroban wire format encoding and field element conversiontypes.tsPaymentProofOutput, MerkleProof, ProofProgress, FluppyCoreConfig, PaymentResultrecipient-hash.tscomputeRecipientHash() — BN254-safe Stellar address hashchain-id.tscomputeChainId() — network passphrase → BN254-safe chain identifierKey Exports
Error classes
| Export | Type | Description |
|---|---|---|
| FluppyError | class | Base error class with code, userMessage, and action fields |
| FluppyProofError | class | Extends FluppyError — proof generation or verification failure |
| FluppyNetworkError | class | Extends FluppyError — RPC or API network failure |
| FluppyWalletError | class | Extends FluppyError — Freighter wallet rejection or not found |
| FluppyRootMismatchError | class | Extends FluppyError — on-chain root differs from frontend root |
| FluppyArtifactError | class | Extends FluppyError — WASM/ZKey load or integrity failure |
| parseFluppyError(err) | function | Maps any thrown value to a structured FluppyError |
Types
| Export | Type | Description |
|---|---|---|
| PaymentProofOutput | interface | Soroban-encoded proof: pi_a (128 hex), pi_b (256 hex), pi_c (128 hex), publicSignals (7 × 64 hex) |
| MerkleProof | interface | pathElements: bigint[], pathIndices: number[], root: bigint |
| ProofProgress | interface | stage: string, pct: number — emitted during proof generation |
| FluppyCoreConfig | interface | contractId, networkPassphrase, rpcUrl |
| PaymentResult | interface | txHash, explorerUrl, status |
Constants
| Export | Type | Description |
|---|---|---|
| BN254_R | bigint | BN254 scalar field order — all circuit inputs must be < this value |
| CIRCUIT_DEPTH | number | 20 — Merkle tree depth supporting up to 2^20 enrolled commitments |
| N_PUBLIC | number | 7 — number of public signals (nullifier, verifiedRoot, merkleRoot, recipientHash, minAmount, maxAmount, chainId) |
| POSEIDON_TAGS | object | NULLIFIER=1, LEAF=2, NODE=3 — domain separation constants, MUST match circuit |
| DEFAULT_MAX_AMOUNT | bigint | Default upper bound for payment amount (1000 USDC in stroops) |
| usdcToStroops() | function | Converts human-readable USDC string to stroops bigint |
Utilities
| Export | Type | Description |
|---|---|---|
| computeRecipientHash(address) | function | XDR-encodes a Stellar address, SHA-256 hashes it, zeroes MSB for BN254 safety |
| computeChainId(passphrase) | function | SHA-256 hashes the network passphrase, zeroes MSB — binds proof to one network |
| encodeG1(point) | function | Encodes BN254 G1 affine point to 64-byte big-endian hex for Soroban Bytes |
| encodeG2(point) | function | Encodes BN254 G2 affine point to 128-byte big-endian hex for Soroban Bytes |
| decimalToBe32Hex(decimal) | function | Converts a decimal string bigint to 32-byte big-endian hex (public signal encoding) |
| hexSecretToFieldElement(hex) | function | Reduces a 256-bit hex secret to a BN254 scalar field element (mod BN254_R) |
Proof Encoding
SnarkJS outputs Groth16 proof points as decimal strings. Soroban contracts expect fixed-size byte arrays (BytesN<64>, BytesN<128>, BytesN<32>). The encoding helpers in @fluppy/core handle this conversion:
Recipient Hash & Chain ID
computeRecipientHash()
XDR-encodes the merchant Stellar address, SHA-256 hashes the bytes, then zeroes the most-significant byte to guarantee the result fits within the BN254 scalar field. This value is used as the recipientHash public signal — binding the proof to a specific merchant address.
computeChainId()
SHA-256 hashes the Stellar network passphrase (e.g. "Test SDF Network ; September 2015"), then zeroes the MSB. The result is included as the chainId public signal. A proof generated on one network will be rejected on a different network because the chainId values differ — preventing cross-network replay attacks.
Usage (Monorepo)
import {
computeRecipientHash,
computeChainId,
encodeG1,
encodeG2,
decimalToBe32Hex,
usdcToStroops,
parseFluppyError,
type PaymentProofOutput,
type MerkleProof,
} from '@fluppy/core';
// Compute BN254-safe inputs
const recipientHash = computeRecipientHash('GDLST72T...');
const chainId = computeChainId('Test SDF Network ; September 2015');
// Convert amount
const stroops = usdcToStroops('1.50'); // → 15_000_000n
// Error handling
try {
// ... payment logic
} catch (err) {
const parsed = parseFluppyError(err);
console.error(parsed.code, parsed.userMessage); // safe to log
}