OpenPayments.Key (open_payments_elixir v1.0.0)

Copy Markdown View Source

An Ed25519 signing key with an explicit key ID.

generate/1 creates a new key using OTP crypto. from_seed/2 imports an exact 32-byte Ed25519 seed and derives its public key. from_pem/2 imports a single unencrypted Ed25519 PKCS#8 private-key entry using OTP's PEM parser. from_file/2 reads PEM from a file; from_base64_pem/2 decodes base64 PEM text. from_jwk/2 imports a private Ed25519 JWK and checks its public-key consistency. Key IDs must be nonblank strings; their spelling is preserved, matching the Node SDK's public-key generator.

Use public_jwk/1 or public_jwks/1 to export public data for registration with a wallet address server. These functions never include the private seed.

Treat this type as opaque and construct it through these functions. Inspection hides key material and there is no JSON encoder for private keys. The private seed remains in process memory; inspection redaction is not memory protection.

Summary

Functions

Imports standard base64-encoded PEM text, as commonly stored in configuration.

Reads a PEM private key from a UTF-8 file path without modifying the file.

Imports a private Ed25519 JWK from a JSON object with string keys.

Imports a single unencrypted Ed25519 PKCS#8 private-key entry.

Imports a raw 32-byte Ed25519 seed, deriving its public key.

Generates a new Ed25519 signing key using OTP crypto.

Returns the explicitly configured key ID without exposing key material.

Exports the public key as a typed Open Payments JWK.

Exports the public key in a typed Open Payments JWKS document.

Signs exact binary data with Ed25519, returning the raw signature bytes.

Types

t()

@opaque t()

Functions

from_base64_pem(encoded, key_id)

@spec from_base64_pem(binary(), String.t()) ::
  {:ok, t()} | {:error, OpenPayments.Error.t()}

Imports standard base64-encoded PEM text, as commonly stored in configuration.

Padding is optional and ASCII whitespace is ignored. Invalid base64 returns a configuration issue on base64_pem; decoded contents use from_pem/2 validation. No alternate key format is inferred from the decoded data.

from_file(path, key_id)

@spec from_file(String.t(), String.t()) ::
  {:ok, t()} | {:error, OpenPayments.Error.t()}

Reads a PEM private key from a UTF-8 file path without modifying the file.

File-read failures return configuration errors without retaining the path. The contents are validated by from_pem/2; an import never creates or replaces a key file. Paths must be nonempty strings without null bytes.

from_jwk(jwk, key_id)

@spec from_jwk(map(), String.t()) :: {:ok, t()} | {:error, OpenPayments.Error.t()}

Imports a private Ed25519 JWK from a JSON object with string keys.

Requires kty: "OKP", crv: "Ed25519", and canonical unpadded base64url d and x values encoding 32 bytes each. The public value must match the seed. Optional alg and use must be EdDSA and sig; key_ops, when present, must permit sign and may also permit verify, without duplicates.

The explicit key_id sets the exported ID. An optional JWK kid must be a string but does not override it. Other JSON metadata is accepted and discarded. Private JWKs are input only; public exports never include d.

from_pem(pem, key_id)

@spec from_pem(binary(), String.t()) :: {:ok, t()} | {:error, OpenPayments.Error.t()}

Imports a single unencrypted Ed25519 PKCS#8 private-key entry.

Public-only keys, other algorithms, encrypted keys, malformed data, and bundles with multiple PEM entries return configuration errors. No password is accepted and invalid input never causes a replacement key to be generated.

from_seed(seed, key_id)

@spec from_seed(binary(), String.t()) :: {:ok, t()} | {:error, OpenPayments.Error.t()}

Imports a raw 32-byte Ed25519 seed, deriving its public key.

generate(key_id)

@spec generate(String.t()) :: {:ok, t()} | {:error, OpenPayments.Error.t()}

Generates a new Ed25519 signing key using OTP crypto.

key_id(key)

@spec key_id(t()) :: String.t()

Returns the explicitly configured key ID without exposing key material.

public_jwk(key)

@spec public_jwk(t()) :: OpenPayments.Types.JWK.t()

Exports the public key as a typed Open Payments JWK.

public_jwks(key)

@spec public_jwks(t()) :: OpenPayments.Types.JWKS.t()

Exports the public key in a typed Open Payments JWKS document.

sign(key, data)

@spec sign(t(), binary()) :: {:ok, binary()} | {:error, OpenPayments.Error.t()}

Signs exact binary data with Ed25519, returning the raw signature bytes.

This operation does not construct an HTTP signature or send a request. Signing failures return an error without retaining key material, input, or crypto details.