use base64::Engine as _;
use base64::engine::general_purpose::URL_SAFE_NO_PAD as B64URL;
use ed25519_dalek::{Signature, Verifier as _, VerifyingKey};
use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use sha2::Digest as _;
use crate::CloudStore;
pub const OPTIN_KEY_FILE: &str = "optin_key.json";
pub const BIND_DOMAIN: &str = "leanctx.cloud.optin_key_bind.v1\n";
pub const SCOPE: &str = "optin.v1";
pub const ALG: &str = "ed25519";
pub const FEATURE_INSTANCE_OPTIN_KEY: &str = "instance_optin_key";
pub const REQUEST_MEMBER: &str = "optin_key";
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct OptinKeyOffer {
pub alg: String,
pub public_key: String,
pub key_id: String,
pub bind_signature: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct BoundOptinKey {
pub alg: String,
pub key_id: String,
pub public_key: String,
#[serde(default)]
pub accepted: bool,
}
pub fn key_id_of(public_key: &[u8]) -> String {
hex::encode(sha2::Sha256::digest(public_key))[..16].to_owned()
}
pub fn bind_statement(
instance_public_key: &str,
key_id: &str,
alg: &str,
public_key: &str,
) -> Result<Vec<u8>, String> {
lean_ctx_protocol::cloud::signing_input(
BIND_DOMAIN,
&json!({
"alg": alg,
"instance_public_key": instance_public_key,
"key_id": key_id,
"public_key": public_key,
"scope": SCOPE,
}),
)
.map_err(|e| e.to_string())
}
impl BoundOptinKey {
fn verifying_key(&self) -> Option<VerifyingKey> {
if self.alg != ALG {
return None;
}
let bytes = B64URL.decode(&self.public_key).ok()?;
if key_id_of(&bytes) != self.key_id {
return None;
}
VerifyingKey::from_bytes(&<[u8; 32]>::try_from(bytes).ok()?).ok()
}
pub fn consistent(&self) -> bool {
self.verifying_key().is_some()
}
pub fn verifies(&self, message: &[u8], signature: &str) -> bool {
let (Some(key), Ok(raw)) = (self.verifying_key(), B64URL.decode(signature)) else {
return false;
};
Signature::from_slice(&raw).is_ok_and(|sig| key.verify(message, &sig).is_ok())
}
pub fn admits_turn_on(&self, body: &Value, instance_id: &str, signature: &str) -> bool {
super::person_sign::signing_input(
lean_ctx_protocol::cloud::optin::OPTIN_SIGNING_DOMAIN,
body,
instance_id,
)
.is_ok_and(|input| self.verifies(&input, signature))
}
}
impl OptinKeyOffer {
pub fn check(&self, instance_public_key: &str) -> Result<(), String> {
let key = self.public();
if self.alg != ALG {
return Err("the opt-in key's alg is not ed25519".into());
}
if !key.consistent() {
return Err("the opt-in key's key_id does not match its public key".into());
}
let statement = bind_statement(
instance_public_key,
&self.key_id,
&self.alg,
&self.public_key,
)?;
if !key.verifies(&statement, &self.bind_signature) {
return Err("the opt-in key's bind signature does not verify".into());
}
Ok(())
}
pub fn well_formed(&self) -> bool {
self.alg == ALG && self.public().consistent()
}
pub fn request_member(&self) -> Value {
json!({
"alg": self.alg,
"public_key": self.public_key,
"key_id": self.key_id,
"bind_signature": self.bind_signature,
})
}
pub fn public(&self) -> BoundOptinKey {
BoundOptinKey {
alg: self.alg.clone(),
key_id: self.key_id.clone(),
public_key: self.public_key.clone(),
accepted: false,
}
}
}
fn stored(store: &CloudStore) -> Option<BoundOptinKey> {
store
.read_config::<BoundOptinKey>(OPTIN_KEY_FILE)
.ok()
.flatten()
.filter(BoundOptinKey::consistent)
}
pub fn store_pending(store: &CloudStore, offer: &OptinKeyOffer) -> Result<(), crate::CloudError> {
let accepted = stored(store).is_some_and(|k| k.accepted && k.key_id == offer.key_id);
store.write_config(
OPTIN_KEY_FILE,
&BoundOptinKey {
accepted,
..offer.public()
},
)
}
pub fn mark_accepted(store: &CloudStore, key_id: &str) -> Result<bool, crate::CloudError> {
match stored(store) {
Some(mut key) if key.key_id == key_id => {
key.accepted = true;
store.write_config(OPTIN_KEY_FILE, &key)?;
Ok(true)
}
_ => Ok(false),
}
}
pub fn clear_accepted(store: &CloudStore) -> Result<(), crate::CloudError> {
if let Some(mut key) = stored(store).filter(|k| k.accepted) {
key.accepted = false;
store.write_config(OPTIN_KEY_FILE, &key)?;
}
Ok(())
}
pub fn pending_optin_key(store: &CloudStore) -> Option<BoundOptinKey> {
stored(store)
}
pub fn bound_optin_key(store: &CloudStore) -> Option<BoundOptinKey> {
stored(store)
.filter(|k| k.accepted && crate::session_ref::announced(store, FEATURE_INSTANCE_OPTIN_KEY))
}
#[cfg(test)]
pub mod test_key {
use super::*;
use ed25519_dalek::{Signer as _, SigningKey};
pub const INSTANCE_ID: &str = "3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c09";
pub const INSTANCE_PUBLIC_KEY: &str = "AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQE=";
pub fn key() -> SigningKey {
SigningKey::from_bytes(&[7u8; 32])
}
pub fn sign(key: &SigningKey, message: &[u8]) -> String {
B64URL.encode(key.sign(message).to_bytes())
}
pub fn offer(key: &SigningKey, instance_public_key: &str) -> OptinKeyOffer {
let public_key = B64URL.encode(key.verifying_key().to_bytes());
let key_id = key_id_of(&key.verifying_key().to_bytes());
let statement = bind_statement(instance_public_key, &key_id, ALG, &public_key).unwrap();
OptinKeyOffer {
alg: ALG.into(),
public_key,
key_id,
bind_signature: sign(key, &statement),
}
}
}
#[cfg(test)]
mod tests {
use super::test_key::{INSTANCE_ID, INSTANCE_PUBLIC_KEY, key, offer, sign};
use super::*;
const VECTOR_SEED: &str = "46da4b21837ca6e42b4e8f26541d12a8152f962c97ee51f52eb4d00b5c2cbc58";
const VECTOR_INPUT: &str = "leanctx.cloud.optin.v1\n{\"active\":true,\"daily_budget\":{\"tasks\":3,\"tokens\":1000000},\"instance_id\":\"3f1c2b6a-8d4e-4a7b-9c10-5e2f6a7b8c09\",\"prev_optin_id\":\"opt_7a1d4c2e9b3f4e6a8c5d0f1e2a3b4c5d\",\"schema\":\"leanctx.cloud.instance-optin-on/v1\",\"vaults\":[\"0f3c2b6a8d4e4a7b9c105e2f6a7b8c01\"]}";
const VECTOR_SIGNATURE: &str =
"tKv5Z23pifLgCDpvE2jqgMR9uih1A-9lukZt8ko_inLa2DqoQ0uqUW0wfJV6B-IuP3QFyRyj66zZdxeFhxuIBQ";
fn vector_body() -> Value {
json!({
"schema": "leanctx.cloud.instance-optin-on/v1",
"active": true,
"prev_optin_id": "opt_7a1d4c2e9b3f4e6a8c5d0f1e2a3b4c5d",
"vaults": ["0f3c2b6a8d4e4a7b9c105e2f6a7b8c01"],
"daily_budget": {"tasks": 3, "tokens": 1_000_000},
})
}
#[test]
fn pickup_optin_signing_input_and_signature_match_the_cloud_vector() {
let input = super::super::person_sign::signing_input(
lean_ctx_protocol::cloud::optin::OPTIN_SIGNING_DOMAIN,
&vector_body(),
INSTANCE_ID,
)
.unwrap();
assert_eq!(String::from_utf8(input.clone()).unwrap(), VECTOR_INPUT);
let seed: [u8; 32] = hex::decode(VECTOR_SEED).unwrap().try_into().unwrap();
let key = ed25519_dalek::SigningKey::from_bytes(&seed);
assert_eq!(sign(&key, &input), VECTOR_SIGNATURE);
let bound = test_key::offer(&key, INSTANCE_PUBLIC_KEY).public();
assert!(bound.admits_turn_on(&vector_body(), INSTANCE_ID, VECTOR_SIGNATURE));
assert!(!bound.admits_turn_on(
&vector_body(),
"00000000-0000-4000-8000-000000000000",
VECTOR_SIGNATURE
));
let mut changed = vector_body();
changed["daily_budget"]["tasks"] = json!(4);
assert!(!bound.admits_turn_on(&changed, INSTANCE_ID, VECTOR_SIGNATURE));
}
mod optin_key_vector {
pub(super) const SEED: &str =
"9255f0c0a33e1b276f658b9ddf007e5017482d3b5d29a3c1cb3aae45df460251";
pub(super) const PUBLIC_KEY: &str = "h-5BejEvZBRC3aIStZGiozVi4uf7gZ8veK-p7mkpyjU";
pub(super) const KEY_ID: &str = "1b20740d444f4fb5";
pub(super) const INSTANCE_PUBLIC_KEY: &str = "ioEkWHZo5VtdH2xcZCQff/UUv9C2VZlpZzW7qf3qhNo=";
pub(super) const BIND_SIGNATURE: &str = "0cwbxvvScrR7fS3rBT6JmlCmSDpbrBD7xBrkdU4cTG0T6LhkJDp6xTpePzXrSo9NePrFZz4nYV_I6VEC0_AXBQ";
pub(super) const OPTIN_SIGNATURE: &str = "TBWmm3lcYfhlZH_gByquAks8MwdiA5UyzoGOvpwUOBaJUKv5t4pc8pswzRJigB4ewgpI5MF8uLjQ4yWwlqK5DA";
}
#[test]
fn pickup_optin_key_bind_and_turn_on_match_the_cloud_vector() {
use optin_key_vector as v;
use sha2::Digest as _;
let seed: [u8; 32] = hex::decode(v::SEED).unwrap().try_into().unwrap();
let key = ed25519_dalek::SigningKey::from_bytes(&seed);
assert_eq!(B64URL.encode(key.verifying_key().to_bytes()), v::PUBLIC_KEY);
assert_eq!(key_id_of(&key.verifying_key().to_bytes()), v::KEY_ID);
let instance_seed: [u8; 32] =
sha2::Sha256::digest(b"leanctx-cloud test instance key: rc4-67")
.as_slice()
.try_into()
.unwrap();
let instance = ed25519_dalek::SigningKey::from_bytes(&instance_seed);
assert_eq!(
base64::engine::general_purpose::STANDARD.encode(instance.verifying_key().to_bytes()),
v::INSTANCE_PUBLIC_KEY
);
let statement =
bind_statement(v::INSTANCE_PUBLIC_KEY, v::KEY_ID, ALG, v::PUBLIC_KEY).unwrap();
assert_eq!(
String::from_utf8(statement.clone()).unwrap(),
format!(
"leanctx.cloud.optin_key_bind.v1\n{{\"alg\":\"ed25519\",\"instance_public_key\":\"{}\",\"key_id\":\"{}\",\"public_key\":\"{}\",\"scope\":\"optin.v1\"}}",
v::INSTANCE_PUBLIC_KEY,
v::KEY_ID,
v::PUBLIC_KEY
)
);
assert_eq!(sign(&key, &statement), v::BIND_SIGNATURE);
let offer = OptinKeyOffer {
alg: ALG.into(),
public_key: v::PUBLIC_KEY.into(),
key_id: v::KEY_ID.into(),
bind_signature: v::BIND_SIGNATURE.into(),
};
offer.check(v::INSTANCE_PUBLIC_KEY).unwrap();
assert!(offer.check(INSTANCE_PUBLIC_KEY).is_err());
let input = super::super::person_sign::signing_input(
lean_ctx_protocol::cloud::optin::OPTIN_SIGNING_DOMAIN,
&vector_body(),
INSTANCE_ID,
)
.unwrap();
assert_eq!(String::from_utf8(input.clone()).unwrap(), VECTOR_INPUT);
assert_eq!(sign(&key, &input), v::OPTIN_SIGNATURE);
let bound = offer.public();
assert!(bound.admits_turn_on(&vector_body(), INSTANCE_ID, v::OPTIN_SIGNATURE));
assert!(!bound.admits_turn_on(&vector_body(), INSTANCE_ID, VECTOR_SIGNATURE));
assert!(!bound.verifies(&statement, v::OPTIN_SIGNATURE));
}
#[test]
fn pickup_optin_key_bind_is_signed_by_the_key_over_the_instance_public_key() {
let key = key();
let good = offer(&key, INSTANCE_PUBLIC_KEY);
let statement =
bind_statement(INSTANCE_PUBLIC_KEY, &good.key_id, ALG, &good.public_key).unwrap();
let text = String::from_utf8(statement).unwrap();
assert_eq!(
text,
format!(
"leanctx.cloud.optin_key_bind.v1\n{{\"alg\":\"ed25519\",\"instance_public_key\":\"{INSTANCE_PUBLIC_KEY}\",\"key_id\":\"{}\",\"public_key\":\"{}\",\"scope\":\"optin.v1\"}}",
good.key_id, good.public_key
)
);
good.check(INSTANCE_PUBLIC_KEY).unwrap();
assert!(
good.check("AgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgI=")
.is_err()
);
assert!(offer(&key, INSTANCE_ID).check(INSTANCE_PUBLIC_KEY).is_err());
let mut alg = good.clone();
alg.alg = "p256".into();
assert!(alg.check(INSTANCE_PUBLIC_KEY).is_err());
let mut id = good.clone();
id.key_id = "0000000000000000".into();
assert!(id.check(INSTANCE_PUBLIC_KEY).is_err());
let mut sig = good;
sig.bind_signature = sign(&key, b"something else");
assert!(sig.check(INSTANCE_PUBLIC_KEY).is_err());
}
#[test]
fn pickup_optin_key_is_usable_only_once_the_cloud_took_and_announced_it() {
let dir = tempfile::tempdir().unwrap();
let store = CloudStore::at(dir.path().join("config"), dir.path().join("data"));
assert!(bound_optin_key(&store).is_none());
let good = offer(&key(), INSTANCE_PUBLIC_KEY);
store_pending(&store, &good).unwrap();
let text = std::fs::read_to_string(dir.path().join("config").join(OPTIN_KEY_FILE)).unwrap();
assert!(!text.contains("bind_signature"));
assert_eq!(pending_optin_key(&store), Some(good.public()));
assert!(bound_optin_key(&store).is_none(), "pending");
assert!(!mark_accepted(&store, "0000000000000000").unwrap());
assert!(mark_accepted(&store, &good.key_id).unwrap());
assert!(bound_optin_key(&store).is_none(), "not announced");
let instance: crate::state::InstanceRecord = serde_json::from_value(json!({
"instance_id": INSTANCE_ID, "org_id": "org_acme", "org_name": "Platform",
"label": "laptop", "url": "https://cloud.example", "org_salt_id": "salt",
"enrolled_at": "2026-11-16T12:00:00Z"
}))
.unwrap();
store
.write_config(crate::state::INSTANCE_FILE, &instance)
.unwrap();
crate::session_ref::record(
&store,
"https://cloud.example",
vec![FEATURE_INSTANCE_OPTIN_KEY.into()],
)
.unwrap();
assert!(bound_optin_key(&store).is_some_and(|k| k.accepted));
store_pending(&store, &good).unwrap();
assert!(bound_optin_key(&store).is_some());
clear_accepted(&store).unwrap();
assert!(bound_optin_key(&store).is_none(), "cleared");
mark_accepted(&store, &good.key_id).unwrap();
let other = offer(
&ed25519_dalek::SigningKey::from_bytes(&[8u8; 32]),
INSTANCE_PUBLIC_KEY,
);
store_pending(&store, &other).unwrap();
assert!(bound_optin_key(&store).is_none(), "another key");
}
}