# `OpenPayments.Key`
[🔗](https://github.com/interledger/open-payments-elixir/blob/v1.0.0/lib/open_payments/key.ex#L1)

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.

# `t`

```elixir
@opaque t()
```

# `from_base64_pem`

```elixir
@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`

```elixir
@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`

```elixir
@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`

```elixir
@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`

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

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

# `generate`

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

Generates a new Ed25519 signing key using OTP crypto.

# `key_id`

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

Returns the explicitly configured key ID without exposing key material.

# `public_jwk`

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

Exports the public key as a typed Open Payments JWK.

# `public_jwks`

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

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

# `sign`

```elixir
@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.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
