Skip to content

Commit 63fe051

Browse files
feat(nkeys): add graviola support
1 parent 3919b1e commit 63fe051

3 files changed

Lines changed: 36 additions & 4 deletions

File tree

Cargo.lock

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

watermelon-nkeys/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ rust-version.workspace = true
1212
[dependencies]
1313
aws-lc-rs = { version = "1.12.2", default-features = false, features = ["aws-lc-sys", "prebuilt-nasm"], optional = true }
1414
ring = { version = "0.17", optional = true }
15+
graviola = { git = "https://github.com/ctz/graviola.git", optional = true }
1516
crc = "3.2.1"
1617
thiserror = "2"
1718
data-encoding = { version = "2.7.0", default-features = false }
@@ -23,6 +24,7 @@ claims = "0.8"
2324
default = ["aws-lc-rs"]
2425
aws-lc-rs = ["dep:aws-lc-rs"]
2526
ring = ["dep:ring"]
27+
graviola = ["dep:graviola"]
2628
fips = ["aws-lc-rs", "aws-lc-rs/fips"]
2729

2830
[lints]

watermelon-nkeys/src/seed.rs

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,27 @@ use std::fmt::{self, Debug, Display};
33
#[cfg(feature = "aws-lc-rs")]
44
use aws_lc_rs::signature::{Ed25519KeyPair, KeyPair as _, Signature as LlSignature};
55
use data_encoding::{BASE32_NOPAD, BASE64URL_NOPAD};
6+
#[cfg(all(
7+
not(feature = "aws-lc-rs"),
8+
not(feature = "ring"),
9+
feature = "graviola"
10+
))]
11+
use graviola::signing::eddsa::Ed25519SigningKey as Ed25519KeyPair;
612
#[cfg(all(not(feature = "aws-lc-rs"), feature = "ring"))]
713
use ring::signature::{Ed25519KeyPair, KeyPair as _, Signature as LlSignature};
814

9-
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
10-
compile_error!("Please enable the `aws-lc-rs` or the `ring` feature");
15+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring", feature = "graviola")))]
16+
compile_error!("Please enable the `aws-lc-rs`, the `ring` or the `graviola` feature feature");
1117

1218
use crate::crc::Crc16;
1319

20+
#[cfg(all(
21+
not(feature = "aws-lc-rs"),
22+
not(feature = "ring"),
23+
feature = "graviola"
24+
))]
25+
type LlSignature = [u8; 64];
26+
1427
const SEED_PREFIX_BYTE: u8 = 18 << 3;
1528

1629
/// A `NKey` private/public key pair.
@@ -91,8 +104,11 @@ impl KeyPair {
91104

92105
let kind = raw_seed[1];
93106

107+
#[cfg(any(feature = "aws-lc-rs", feature = "ring"))]
94108
let key = Ed25519KeyPair::from_seed_unchecked(&raw_seed[2..])
95109
.map_err(|_| KeyPairFromSeedError::DecodeError)?;
110+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
111+
let key = Ed25519KeyPair::from_bytes(raw_seed[2..].try_into().unwrap()).unwrap();
96112
Ok(Self { kind, key })
97113
}
98114

@@ -126,7 +142,11 @@ impl Display for PublicKey<'_> {
126142
let mut full_raw_seed = [0; 36];
127143
full_raw_seed[0] = SEED_PREFIX_BYTE;
128144
full_raw_seed[1] = self.0.kind;
129-
full_raw_seed[2..34].copy_from_slice(self.0.key.public_key().as_ref());
145+
#[cfg(any(feature = "aws-lc-rs", feature = "ring"))]
146+
let public_key_bytes = self.0.key.public_key().as_ref();
147+
#[cfg(not(any(feature = "aws-lc-rs", feature = "ring")))]
148+
let public_key_bytes = &self.0.key.public_key().as_bytes();
149+
full_raw_seed[2..34].copy_from_slice(public_key_bytes);
130150
let crc = Crc16::compute(&full_raw_seed[..34]);
131151
full_raw_seed[34..36].copy_from_slice(&crc.to_raw_encoded());
132152
Display::fmt(&BASE32_NOPAD.encode_display(&full_raw_seed), f)

0 commit comments

Comments
 (0)