2026-09-10 02:43:45 -04:00
|
|
|
use ed25519_dalek::{Signature, Signer, SigningKey, Verifier, VerifyingKey};
|
|
|
|
|
|
|
|
|
|
pub type Result<T> = std::result::Result<T, Error>;
|
|
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
|
pub struct Error(String);
|
|
|
|
|
|
|
|
|
|
impl std::fmt::Display for Error {
|
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
|
self.0.fmt(f)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl std::error::Error for Error {}
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
|
|
|
pub struct Loc {
|
|
|
|
|
pub onion: String,
|
|
|
|
|
pub ts: i64,
|
|
|
|
|
pub sig: Vec<u8>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const PREFIX: &[u8] = b"loc ";
|
|
|
|
|
|
|
|
|
|
pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result<Loc> {
|
2026-09-12 14:06:20 -04:00
|
|
|
// Same one-shot raw identity-secret copy as `profile::sign`; shared so
|
|
|
|
|
// `Zeroize` covers it here too.
|
|
|
|
|
let sk_bytes =
|
|
|
|
|
crate::profile::signing_key_bytes(identity_sk).map_err(|e| Error(e.to_string()))?;
|
2026-09-10 02:43:45 -04:00
|
|
|
let sk = SigningKey::from_bytes(&sk_bytes);
|
2026-09-11 23:42:41 -04:00
|
|
|
let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec();
|
2026-09-10 02:43:45 -04:00
|
|
|
Ok(Loc {
|
|
|
|
|
onion: onion.to_string(),
|
|
|
|
|
ts,
|
|
|
|
|
sig,
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool {
|
|
|
|
|
if identity_pk.len() != 32 || loc.sig.len() != 64 {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
let pk: [u8; 32] = match identity_pk.try_into() {
|
|
|
|
|
Ok(p) => p,
|
|
|
|
|
Err(_) => return false,
|
|
|
|
|
};
|
|
|
|
|
let sig: [u8; 64] = match loc.sig.as_slice().try_into() {
|
|
|
|
|
Ok(s) => s,
|
|
|
|
|
Err(_) => return false,
|
|
|
|
|
};
|
|
|
|
|
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
|
|
|
|
return false;
|
|
|
|
|
};
|
2026-09-11 23:42:41 -04:00
|
|
|
let signature = Signature::from_bytes(&sig);
|
|
|
|
|
vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature)
|
2026-09-10 02:43:45 -04:00
|
|
|
.is_ok()
|
pay: domain-separate tip and gate legacy signature acceptance
`tip` was the one payment frame with no domain tag: sign_tip signed the
LF-joined fields, so its signature could be confused with any other
newline-joined preimage. Mirror inv/rcp: sign_tip now emits
"onionwire-tip-v1" || 0x00-separated txid/amount/address/memo/ts, and
verify_tip tries that form then the historical layout.
Legacy acceptance is now one switch instead of nine call sites:
`pay::LEGACY_SIGS` (default true) plus `pay::legacy_sigs()`. Every v1 /
concat fallback reads it — the `onionwire:v1` invite string and its two
historical signature forms, `loc`, `inv`, `rcp`, and `tip`. `prf` is not
gated here because src/profile.rs is outside this card's file scope; the
spec says so.
Tests: legacy-on keeps every untagged form verifying (incl. a new tip
case); legacy-off rejects each untagged form while the tagged form still
verifies (loc, inv, rcp, tip, invite); sign_tip is pinned to the tagged
layout.
docs/PROTOCOL.md gains the emitted/accepted table per signed frame and
the mixed-fleet consequence.
2026-10-22 22:23:44 -04:00
|
|
|
|| (crate::pay::legacy_sigs()
|
|
|
|
|
&& vk
|
|
|
|
|
.verify(&sign_msg_v1(&loc.onion, loc.ts), &signature)
|
|
|
|
|
.is_ok())
|
2026-09-10 02:43:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn encode(loc: &Loc) -> Vec<u8> {
|
|
|
|
|
let mut out = Vec::from(PREFIX);
|
|
|
|
|
out.extend_from_slice(loc.onion.as_bytes());
|
|
|
|
|
out.push(b'\n');
|
|
|
|
|
out.extend_from_slice(loc.ts.to_string().as_bytes());
|
|
|
|
|
out.push(b'\n');
|
|
|
|
|
out.extend_from_slice(to_hex(&loc.sig).as_bytes());
|
|
|
|
|
out
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn decode(pt: &[u8]) -> Option<Loc> {
|
|
|
|
|
let rest = pt.strip_prefix(PREFIX)?;
|
|
|
|
|
let text = std::str::from_utf8(rest).ok()?;
|
|
|
|
|
let mut parts = text.splitn(3, '\n');
|
|
|
|
|
let onion = parts.next()?.to_string();
|
|
|
|
|
let ts = parts.next()?.parse::<i64>().ok()?;
|
|
|
|
|
let sig = from_hex(parts.next()?)?;
|
|
|
|
|
if onion.is_empty() {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
Some(Loc { onion, ts, sig })
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 23:42:41 -04:00
|
|
|
fn sign_msg_v2(onion: &str, ts: i64) -> Vec<u8> {
|
|
|
|
|
tagged(
|
|
|
|
|
b"onionwire-loc-v1",
|
|
|
|
|
&[onion.as_bytes(), ts.to_string().as_bytes()],
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn sign_msg_v1(onion: &str, ts: i64) -> Vec<u8> {
|
2026-09-10 02:43:45 -04:00
|
|
|
let mut msg = Vec::from(onion.as_bytes());
|
|
|
|
|
msg.push(b'\n');
|
|
|
|
|
msg.extend_from_slice(ts.to_string().as_bytes());
|
|
|
|
|
msg
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-11 23:42:41 -04:00
|
|
|
fn tagged(tag: &[u8], fields: &[&[u8]]) -> Vec<u8> {
|
|
|
|
|
let mut msg = Vec::from(tag);
|
|
|
|
|
for f in fields {
|
|
|
|
|
msg.push(0);
|
|
|
|
|
msg.extend_from_slice(f);
|
|
|
|
|
}
|
|
|
|
|
msg
|
|
|
|
|
}
|
|
|
|
|
|
2026-09-10 02:43:45 -04:00
|
|
|
fn to_hex(bytes: &[u8]) -> String {
|
|
|
|
|
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn from_hex(s: &str) -> Option<Vec<u8>> {
|
|
|
|
|
if s.is_empty() || !s.len().is_multiple_of(2) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
if !s.bytes().all(|c| c.is_ascii_hexdigit()) {
|
|
|
|
|
return None;
|
|
|
|
|
}
|
|
|
|
|
(0..s.len())
|
|
|
|
|
.step_by(2)
|
|
|
|
|
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
|
|
|
|
|
.collect()
|
|
|
|
|
}
|