Object Secp256k1
-
- All Implemented Interfaces:
-
web5.sdk.crypto.KeyGenerator,web5.sdk.crypto.Signer
public class Secp256k1 implements KeyGenerator, Signer
A cryptographic object responsible for key generation, signature creation, and signature verification utilizing the SECP256K1 elliptic curve, widely used for Bitcoin and Ethereum transactions.
The object uses the Nimbus JOSE+JWT library and implements the KeyGenerator and Signer interfaces, providing specific implementation details for SECP256K1.
Utilizes the ES256K algorithm for signing JWTs.
Utilizes BouncyCastle as the underlying security provider.
Public and private keys can be encoded with PUB_MULTICODEC and PRIV_MULTICODEC respectively.
val privateKey = Secp256k1.generatePrivateKey() val publicKey = Secp256k1.getPublicKey(privateKey)generatePrivateKey: Generates a private key for the SECP256K1 curve.getPublicKey: Derives the corresponding public key from a private key.
sign: Generates a digital signature.verify: Verifies a digital signature.
-
-
Field Summary
Fields Modifier and Type Field Description private final Jwaalgorithmprivate final JwaCurvecurveprivate final StringkeyTypepublic final static IntegerPUB_MULTICODECpublic final static IntegerPRIV_MULTICODECpublic final static ByteUNCOMPRESSED_KEY_IDpublic final static ByteCOMP_KEY_EVEN_Y_IDpublic final static ByteCOMP_KEY_ODD_Y_IDpublic final static IntegerUNCOMPRESSED_KEY_SIZEpublic final static IntegerCOMPRESSED_KEY_SIZEpublic final static IntegerSIG_SIZEprivate final IntRangepublicKeyXRangeprivate final IntRangepublicKeyYRangepublic final static Secp256k1INSTANCE
-
Method Summary
Modifier and Type Method Description JwkgeneratePrivateKey(KeyGenOptions options)Generates a private key using the SECP256K1 curve and ES256K algorithm. JwkcomputePublicKey(Jwk privateKey)Derives a public key from the private key provided. ByteArrayprivateKeyToBytes(Jwk privateKey)Converts a private key to bytes. ByteArraypublicKeyToBytes(Jwk publicKey)Converts a public key to bytes. JwkbytesToPrivateKey(ByteArray privateKeyBytes)Converts a private key as bytes into a Jwk. JwkbytesToPublicKey(ByteArray publicKeyBytes)Converts a public key as bytes into a Jwk. ByteArraysign(Jwk privateKey, ByteArray payload, SignOptions options)Deterministically signs the provided payload using the ECDSA (Elliptic Curve Digital Signature Algorithm) with the curve secp256k1.Unitverify(Jwk publicKey, ByteArray signedPayload, ByteArray signature, VerifyOptions options)Verifies a signature against a given payload using the ECDSA (Elliptic Curve Digital Signature Algorithm) with the curve secp256k1.final UnitvalidateKey(Jwk key)Validates the provided Jwk (JSON Web Key) to ensure it conforms to the expected key type and format. final ByteArraycompressPublicKey(ByteArray publicKeyBytes)Compresses a public key represented by its X and Y coordinates concatenated in a single byte array. final ByteArrayinflatePublicKey(ByteArray publicKeyBytes)Inflates a compressed public key. JwagetAlgorithm()Indicates the algorithm intended to be used with the key. JwaCurvegetCurve()The curve used for the key generation. StringgetKeyType()KeyType in String format (OKP, EC, etc.). final IntRangegetPublicKeyXRange()Range that defines the position of the X coordinate in an uncompressed public key byte array. final IntRangegetPublicKeyYRange()Range that defines the position of the Y coordinate in an uncompressed public key byte array. -
-
Method Detail
-
generatePrivateKey
Jwk generatePrivateKey(KeyGenOptions options)
Generates a private key using the SECP256K1 curve and ES256K algorithm.
The generated key will have its key ID derived from the thumbprint and will be intended for signature use.
- Parameters:
options- Options for key generation (currently unused, provided for possible future expansion).- Returns:
A Jwk representing the generated private key.
-
computePublicKey
Jwk computePublicKey(Jwk privateKey)
Derives a public key from the private key provided. Applicable for asymmetric Key Generators only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception
-
privateKeyToBytes
ByteArray privateKeyToBytes(Jwk privateKey)
Converts a private key to bytes.
-
publicKeyToBytes
ByteArray publicKeyToBytes(Jwk publicKey)
Converts a public key to bytes. Applicable for asymmetric KeyGenerator implementations only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception
-
bytesToPrivateKey
Jwk bytesToPrivateKey(ByteArray privateKeyBytes)
Converts a private key as bytes into a Jwk.
-
bytesToPublicKey
Jwk bytesToPublicKey(ByteArray publicKeyBytes)
Converts a public key as bytes into a Jwk. Applicable for asymmetric Key Generators only. Implementers of symmetric key generators should throw an UnsupportedOperation Exception
-
sign
ByteArray sign(Jwk privateKey, ByteArray payload, SignOptions options)
Deterministically signs the provided payload using the ECDSA (Elliptic Curve Digital Signature Algorithm) with the curve
secp256k1.This function is designed to generate deterministic signatures, meaning that signing the same payload with the same private key will always produce the same signature.
- Parameters:
privateKey- The private key used for signing, provided as aJwk(JSON Web Key).payload- The byte array containing the data to be signed.options- Optional parameter to provide additional configuration for the signing process.- Returns:
A byte array representing the signature, generated by concatenating the
randscomponents of the ECDSA signature.
-
verify
Unit verify(Jwk publicKey, ByteArray signedPayload, ByteArray signature, VerifyOptions options)
Verifies a signature against a given payload using the ECDSA (Elliptic Curve Digital Signature Algorithm) with the curve
secp256k1. This function supports deterministic k-value generation through HMAC and SHA-256, ensuring consistent verification outcomes for identical payloads and signatures.- Parameters:
publicKey- The public key used for verification, provided as aJwk(JSON Web Key).signedPayload- The byte array containing the data that was signed.signature- The byte array representing the signature to be verified against the payload.options- Optional parameter to provide additional configuration for the verification process.
-
validateKey
final Unit validateKey(Jwk key)
Validates the provided Jwk (JSON Web Key) to ensure it conforms to the expected key type and format.
This function checks the following:
The key must be an instance of ECKey.
If any of these checks fail, this function throws an IllegalArgumentException with a descriptive error message.
val jwk: Jwk = //...obtain or generate a Jwk try { Secp256k1.validateKey(jwk) // Key is valid, proceed with further operations... } catch (e: IllegalArgumentException) { // Handle invalid key... }Ensure to call this function before using a Jwk in cryptographic operations to safeguard against invalid key usage and potential vulnerabilities.
- Parameters:
key- The Jwk to validate.
-
compressPublicKey
final ByteArray compressPublicKey(ByteArray publicKeyBytes)
Compresses a public key represented by its X and Y coordinates concatenated in a single byte array.
Assumes the input starts with a leading 0x04 byte, which is commonly used to denote an uncompressed public key in some elliptic curve representations.
- Parameters:
publicKeyBytes- A byte array representing the public key, expected to be 65 bytes with the first byte being 0x04.- Returns:
The compressed public key as a byte array.
-
inflatePublicKey
final ByteArray inflatePublicKey(ByteArray publicKeyBytes)
Inflates a compressed public key.
-
getAlgorithm
Jwa getAlgorithm()
Indicates the algorithm intended to be used with the key.
-
getKeyType
String getKeyType()
KeyType in String format (OKP, EC, etc.).
-
getPublicKeyXRange
final IntRange getPublicKeyXRange()
Range that defines the position of the X coordinate in an uncompressed public key byte array.
The X coordinate is typically found in bytes 1 through 32 (inclusive) in the byte array representation of an uncompressed public key, assuming the first byte is reserved for the prefix (0x04).
-
getPublicKeyYRange
final IntRange getPublicKeyYRange()
Range that defines the position of the Y coordinate in an uncompressed public key byte array.
The Y coordinate is typically found in bytes 33 through 64 (inclusive) in the byte array representation of an uncompressed public key, following the X coordinate.
-
-
-
-