Skip to main content

crypto_dispatch/
keypair.rs

1// SPDX-FileCopyrightText: Copyright © 2026 ReallyMe LLC. All rights reserved
2//
3// SPDX-License-Identifier: Apache-2.0
4
5use zeroize::Zeroizing;
6
7use crate::multikey;
8use crate::registry;
9use crate::AlgorithmError;
10use crypto_core::Algorithm;
11
12/// A generated keypair together with the multikey-encoded public key.
13pub struct GeneratedKeypair {
14    /// Raw public key bytes.
15    pub public_key: Vec<u8>,
16    /// Secret half of the keypair; zeroized when the struct is dropped.
17    pub secret_key: Zeroizing<Vec<u8>>,
18    /// The public key encoded as a `z`-prefixed multikey string.
19    pub public_key_multikey: String,
20}
21
22/// Generate a keypair and multikey-encode the public key
23pub fn generate_multikey_keypair(alg: Algorithm) -> Result<GeneratedKeypair, AlgorithmError> {
24    let (public, secret) = registry::generate_keypair(alg)?;
25    let multikey = multikey::public_key_to_multikey(alg, &public)?;
26
27    Ok(GeneratedKeypair {
28        public_key: public,
29        secret_key: secret,
30        public_key_multikey: multikey,
31    })
32}