Secp256k1
Utility functions for secp256k1 ECDSA cryptography.
Examples
Below are some examples demonstrating common usages of the Secp256k1
module:
Computing a Random Private Key
A random private key can be computed using Secp256k1.randomPrivateKey
:
import { Secp256k1 } from 'ox'
const privateKey = Secp256k1.randomPrivateKey()
'0x...'
Getting a Public Key
A public key can be derived from a private key using Secp256k1.getPublicKey
:
import { Secp256k1 } from 'ox'
const privateKey = Secp256k1.randomPrivateKey()
const publicKey = Secp256k1.getPublicKey({ privateKey })
{ x: 3251...5152n, y: 1251...5152n }
Signing a Payload
A payload can be signed using Secp256k1.sign
:
import { Secp256k1 } from 'ox'
const privateKey = Secp256k1.randomPrivateKey()
const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey })
{ r: 1251...5152n, s: 1251...5152n, yParity: 1 }
Verifying a Signature
A signature can be verified using Secp256k1.verify
:
import { Secp256k1 } from 'ox'
const privateKey = Secp256k1.randomPrivateKey()
const publicKey = Secp256k1.getPublicKey({ privateKey })
const signature = Secp256k1.sign({ payload: '0xdeadbeef', privateKey })
const isValid = Secp256k1.verify({
payload: '0xdeadbeef',
publicKey,
signature,
})
true
Functions
Name | Description |
---|---|
Secp256k1.getPublicKey | Computes the secp256k1 ECDSA public key from a provided private key. |
Secp256k1.randomPrivateKey | Generates a random ECDSA private key on the secp256k1 curve. |
Secp256k1.recoverAddress | Recovers the signing address from the signed payload and signature. |
Secp256k1.recoverPublicKey | Recovers the signing public key from the signed payload and signature. |
Secp256k1.sign | Signs the payload with the provided private key. |
Secp256k1.verify | Verifies a payload was signed by the provided address. |