Keypair class manages cryptographic keys for Tornado Nova transactions. Each keypair consists of a private key, public key, and encryption key.
Constructor
Initialize a new keypair with an optional private key.Parameters
A 32-byte hex string private key with
0x prefix. If not provided, generates a random private key using ethers.Wallet.Properties
After construction, a Keypair instance has these properties:privkey- The private key as a hex stringpubkey- Public key derived from Poseidon hash of private key (BigNumber)encryptionKey- Base64-encoded encryption public key for encrypting UTXO data
Methods
toString()
Returns the keypair address as a hex string.string - 128-character hex string (130 with 0x prefix)
The address format:
- First 64 hex chars: public key
- Last 64 hex chars: encryption key
address()
Alias fortoString(). Returns the keypair address.
string - The keypair address
fromString() (static)
Initializes a new keypair from an address string. Used to create a keypair for encryption without the private key.A 128-character hex string (or 130 with
0x prefix) representing the keypair addressKeypair - A new Keypair instance without a private key (privkey is null)
Throws: Error if the string length is invalid
Keypairs created with
fromString() cannot sign or decrypt data since they lack a private key. They can only be used for encryption.sign()
Sign a message using the keypair’s private key.The UTXO commitment value
The Merkle tree path or index
BigNumber - The signature computed using Poseidon hash
The signature is calculated as: poseidonHash([privkey, commitment, merklePath])
encrypt()
Encrypt data using the keypair’s encryption key.A Buffer containing the data to encrypt
string - Hex string with 0x prefix containing encrypted data
Uses the x25519-xsalsa20-poly1305 encryption scheme.
decrypt()
Decrypt data using the keypair’s private key.A hex string (with or without
0x prefix) containing encrypted dataBuffer - A Buffer containing the decrypted data
Example usage
Generate a new keypair
Create keypair from existing private key
Share public address for receiving funds
Encrypt and decrypt UTXO data
Sign a commitment
Address format
A Tornado Nova address is 130 characters (with0x prefix):
Security considerations
- Store private keys securely
- Use randomly generated keypairs for each transaction
- The encryption key is derived from the private key and is safe to share
- Keypairs without private keys (from
fromString()) can only encrypt, not decrypt
Related
- Utxo class - UTXOs use keypairs for ownership
- Proof generation - Keypairs are used in proof generation