hippo-sdk
v3.0.0
Published
`hippo-sdk` is a WebAssembly-based npm library for cryptographic operations, including **key pair generation**, **ECDH encryption**, **ECDSA signing**, **DID management**, and **Bulletproof range proofs**. It's built with Rust for high performance and com
Readme
🦛 hippo-sdk
hippo-sdk is a WebAssembly-based npm library for cryptographic operations, including key pair generation, ECDH encryption, ECDSA signing, DID management, and Bulletproof range proofs. It's built with Rust for high performance and compiled to WebAssembly for seamless use in web applications.
📦 Installation
To add hippo-sdk to your project, use either npm or yarn:
npm install hippo-sdk🚀 Usage
This library provides a set of cryptographic utilities. You can import the functions directly and use them in your JavaScript or TypeScript projects.
Example
Here's a simple example demonstrating how to generate a key pair and then use it to encrypt and decrypt data.
import {
create_keypair,
key_to_did,
encrypt,
decrypt,
sign,
verify,
generate_bulletproof,
verify_bulletproof,
} from "hippo-sdk";
// 1. Generate a new key pair
const keypair = create_keypair();
const { pubkey, privkey } = keypair.to_object();
console.log("Public Key:", pubkey);
console.log("Private Key:", privkey);
// 2. Create a DID from the public key
const myDid = key_to_did(pubkey);
console.log("DID:", myDid.to_object());
// Expected output: { "id": "did:hp:<pubkey>" }
// 3. Encrypt some data using the public key
const rawData = "Hello, wasm!";
const encryptedData = encrypt(rawData, pubkey);
console.log("Encrypted Data:", encryptedData.to_object());
// 4. Decrypt the data using the private key
const decryptedData = decrypt(encryptedData, privkey);
console.log("Decrypted Data:", decryptedData);
// Expected output: "Hello, wasm!"
// 5. Sign a message using the private key
const messageToSign = "This is a message to be signed.";
const signature = sign(messageToSign, privkey);
console.log("Signature:", signature);
// 6. Verify the signature using the public key
const isVerified = verify(messageToSign, signature, pubkey);
console.log("Signature Verified:", isVerified);
// Expected output: true
// 7. Prove a secret value lies in [0, 2^32) without revealing it
const bulletproof = generate_bulletproof(42n, 32, "my-app");
const isProofValid = verify_bulletproof(bulletproof, 32, "my-app");
console.log("Bulletproof:", bulletproof.to_object());
console.log("Bulletproof Verified:", isProofValid);
// Expected output: true📖 API
The library exposes several functions for various cryptographic tasks.
create_keypair(): KeyPair
Generates a new secp256k1 key pair. Returns a KeyPair object containing the public and private keys.
- Returns:
KeyPair
key_to_did(pubkey: string): Did
Converts a public key string into a DID (Decentralized Identifier) with the format did:hp:<pubkey>.
pubkey: The public key string.- Returns:
Did
did_to_key(did: Did): string
Extracts the public key string from a DID.
did: TheDidobject.- Returns:
string
encrypt(data: string, pubkey: string): EncryptedData
Encrypts a string of data using ECDH key exchange and AES-256-GCM encryption.
data: The string to be encrypted.pubkey: The public key of the recipient.- Returns:
EncryptedData
decrypt(data: EncryptedData, privkey: string): string
Decrypts data that was encrypted using the encrypt function.
data: TheEncryptedDataobject.privkey: The private key of the recipient.- Returns:
string
sign(data: string, privkey: string): string
Signs a string of data using ECDSA.
data: The string to be signed.privkey: The private key of the signer.- Returns: The signature string.
verify(data: string, sig: string, pubkey: string): boolean
Verifies an ECDSA signature against the original data and a public key.
data: The original string of data.sig: The signature string.pubkey: The public key of the signer.- Returns:
booleanindicating if the signature is valid.
sha256(data: string): string
Computes the SHA-256 hash of a string.
data: The string to hash.- Returns: The hash as a string.
ecdh(privkey: string, pubkey: string): string
Performs an Elliptic Curve Diffie-Hellman (ECDH) key exchange to compute a shared secret.
privkey: Your private key.pubkey: The other party's public key.- Returns: The shared secret as a hex string.
generate_bulletproof(value: bigint, bits: number, tag: string): Bulletproof
Generates a Bulletproof zero-knowledge range proof that a secret value lies in [0, 2^bits), without revealing the value.
value: The secret value, which must be less than2^bits.bits: The range bit size:8,16,32, or64.tag: A domain separation tag naming the proof's purpose (e.g. your app), as withpedersen_commit. The proof only verifies with the same tag.- Returns:
Bulletproofwith the hex-encodedproofandcommitment.
verify_bulletproof(bulletproof: Bulletproof, bits: number, tag: string): boolean
Verifies a Bulletproof range proof against its commitment. A received proof can be rebuilt with new Bulletproof(proof, commitment) or Bulletproof.from_object({ proof, commitment }).
bulletproof: TheBulletproofobject.bits: The range bit size to verify:8,16,32, or64.tag: The tag the proof was generated with.- Returns:
booleanindicating if the proof is valid for the range and tag.
🤝 Contributing
We welcome contributions! Feel free to open an issue or submit a pull request on the GitHub repository.
📄 License
This project is licensed under the MIT License.
