Skip to content

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

NameDescription
Secp256k1.createKeyPairCreates a new secp256k1 ECDSA key pair consisting of a private key and its corresponding public key.
Secp256k1.getPublicKeyComputes the secp256k1 ECDSA public key from a provided private key.
Secp256k1.getSharedSecretComputes a shared secret using ECDH (Elliptic Curve Diffie-Hellman) between a private key and a public key.
Secp256k1.randomPrivateKeyGenerates a random ECDSA private key on the secp256k1 curve.
Secp256k1.recoverAddressRecovers the signing address from the signed payload and signature.
Secp256k1.recoverPublicKeyRecovers the signing public key from the signed payload and signature.
Secp256k1.signSigns the payload with the provided private key.
Secp256k1.verifyVerifies a payload was signed by the provided address.