@fluppy/core✓ Complete

@fluppy/core

Protocol-level primitives shared by @fluppy/browser and @fluppy/react. Pure TypeScript — no browser APIs, no React, no wallet dependency.

~10 KB
ESM bundle
~11 KB
CJS bundle
~9 KB
TypeScript declarations

Module Map

constants.tsBN254_R, CIRCUIT_DEPTH, N_PUBLIC, POSEIDON_TAGS, DEFAULT_MAX_AMOUNT, usdcToStroops()
errors.tsFluppyError class hierarchy and parseFluppyError() parser
encoding.tsG1/G2 Soroban wire format encoding and field element conversion
types.tsPaymentProofOutput, MerkleProof, ProofProgress, FluppyCoreConfig, PaymentResult
recipient-hash.tscomputeRecipientHash() — BN254-safe Stellar address hash
chain-id.tscomputeChainId() — network passphrase → BN254-safe chain identifier

Key Exports

Error classes

ExportTypeDescription
FluppyErrorclassBase error class with code, userMessage, and action fields
FluppyProofErrorclassExtends FluppyError — proof generation or verification failure
FluppyNetworkErrorclassExtends FluppyError — RPC or API network failure
FluppyWalletErrorclassExtends FluppyError — Freighter wallet rejection or not found
FluppyRootMismatchErrorclassExtends FluppyError — on-chain root differs from frontend root
FluppyArtifactErrorclassExtends FluppyError — WASM/ZKey load or integrity failure
parseFluppyError(err)functionMaps any thrown value to a structured FluppyError

Types

ExportTypeDescription
PaymentProofOutputinterfaceSoroban-encoded proof: pi_a (128 hex), pi_b (256 hex), pi_c (128 hex), publicSignals (7 × 64 hex)
MerkleProofinterfacepathElements: bigint[], pathIndices: number[], root: bigint
ProofProgressinterfacestage: string, pct: number — emitted during proof generation
FluppyCoreConfiginterfacecontractId, networkPassphrase, rpcUrl
PaymentResultinterfacetxHash, explorerUrl, status

Constants

ExportTypeDescription
BN254_RbigintBN254 scalar field order — all circuit inputs must be < this value
CIRCUIT_DEPTHnumber20 — Merkle tree depth supporting up to 2^20 enrolled commitments
N_PUBLICnumber7 — number of public signals (nullifier, verifiedRoot, merkleRoot, recipientHash, minAmount, maxAmount, chainId)
POSEIDON_TAGSobjectNULLIFIER=1, LEAF=2, NODE=3 — domain separation constants, MUST match circuit
DEFAULT_MAX_AMOUNTbigintDefault upper bound for payment amount (1000 USDC in stroops)
usdcToStroops()functionConverts human-readable USDC string to stroops bigint

Utilities

ExportTypeDescription
computeRecipientHash(address)functionXDR-encodes a Stellar address, SHA-256 hashes it, zeroes MSB for BN254 safety
computeChainId(passphrase)functionSHA-256 hashes the network passphrase, zeroes MSB — binds proof to one network
encodeG1(point)functionEncodes BN254 G1 affine point to 64-byte big-endian hex for Soroban Bytes
encodeG2(point)functionEncodes BN254 G2 affine point to 128-byte big-endian hex for Soroban Bytes
decimalToBe32Hex(decimal)functionConverts a decimal string bigint to 32-byte big-endian hex (public signal encoding)
hexSecretToFieldElement(hex)functionReduces 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:

// G1 (pi_a, pi_c): 64 bytes = x_be32 ‖ y_be32
encodeG1([x, y, '1']) → 128-char hex string
// G2 (pi_b): 128 bytes = x_c1 ‖ x_c0 ‖ y_c1 ‖ y_c0
encodeG2([[x_c1,x_c0],[y_c1,y_c0],…]) → 256-char hex string
// Public signals: 7 × 32 bytes each
decimalToBe32Hex('123456…') → 64-char hex string

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)

ℹ
Packages are internal workspace packages. See Installation for setup. Do not use public npm package installation yet — packages are not yet published to npm.
example.ts
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
}