{"library":"secp256k1-wasm","title":"WebAssembly Bindings for libsecp256k1","description":"secp256k1-wasm provides high-performance WebAssembly bindings for `libsecp256k1`, the highly optimized C library for secp256k1 elliptic curve cryptography. This package enables cryptographic operations such as key generation, signature signing, and verification directly within JavaScript environments like web browsers and Node.js, offering near-native performance without requiring platform-specific native add-ons. The current stable version is 2.0.0, released in November 2023. While an explicit release cadence is not defined, releases appear to be feature-driven and occur periodically. Its primary differentiator is its reliance on Emscripten to compile the robust `libsecp256k1` to WebAssembly, making it ideal for applications where cryptographic throughput is critical, such as blockchain wallets or transaction processing, by avoiding the overhead of pure JavaScript implementations.","language":"javascript","status":"active","last_verified":"Tue Apr 21","install":{"commands":["npm install secp256k1-wasm"],"cli":null},"imports":["import initSecp256k1 from 'secp256k1-wasm';","const secp256k1 = await initSecp256k1(); // 'secp256k1' now holds functions like .sign, .verify","const initSecp256k1 = require('secp256k1-wasm');"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import initSecp256k1 from 'secp256k1-wasm';\nimport { randomBytes } from 'crypto'; // Node.js built-in for secure randomness\n\nasync function runSecp256k1Example() {\n  console.log(\"Loading secp256k1 WASM module...\");\n  // Initialize the WASM module. This returns a Promise that resolves to the API object.\n  const secp256k1 = await initSecp256k1();\n  console.log(\"secp256k1 WASM module loaded successfully.\");\n\n  // 1. Generate a random 32-byte private key\n  const privateKey = randomBytes(32);\n  console.log(`\\nPrivate Key: ${privateKey.toString('hex')}`);\n\n  // 2. Create a public key from the private key\n  // 'false' means uncompressed public key (65 bytes)\n  const publicKey = secp256k1.pubkeyCreate(privateKey, false);\n  console.log(`Public Key (uncompressed): ${publicKey.toString('hex')}`);\n\n  // 3. Prepare a 32-byte message hash to sign\n  const messageHash = randomBytes(32);\n  console.log(`Message Hash: ${messageHash.toString('hex')}`);\n\n  // 4. Sign the message hash with the private key\n  const { signature, recid } = secp256k1.sign(messageHash, privateKey);\n  console.log(`Signature: ${signature.toString('hex')}`);\n  console.log(`Recovery ID (RecID): ${recid}`);\n\n  // 5. Verify the signature using the original message hash, signature, and public key\n  const isValid = secp256k1.verify(messageHash, signature, publicKey);\n  console.log(`Signature valid: ${isValid}`);\n\n  // 6. Recover the public key from the message hash, signature, and recovery ID\n  const recoveredPublicKey = secp256k1.recover(messageHash, signature, recid, false);\n  console.log(`Recovered Public Key: ${recoveredPublicKey.toString('hex')}`);\n  console.log(`Recovery matches original public key: ${publicKey.equals(recoveredPublicKey)}`);\n}\n\nrunSecp256k1Example().catch(console.error);\n","lang":"typescript","description":"This quickstart demonstrates how to asynchronously load the secp256k1 WASM module, generate a private/public key pair, sign a random message hash, and then verify and recover the public key from the signature. It showcases the high-level API functions exposed by the package.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}