pay: domain-separate tip and gate legacy signature acceptance
All checks were successful
ci / test (pull_request) Successful in 19m52s
ci / sdk (pull_request) Successful in 2m16s

`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.
This commit is contained in:
rust-dev 2026-10-22 22:23:44 -04:00
commit b8c52ca97d
No known key found for this signature in database
6 changed files with 352 additions and 35 deletions

View file

@ -198,3 +198,188 @@ fn inv_signature_does_not_verify_as_rcp() {
"inv-shaped payload must not verify as rcp"
);
}
// ---- legacy acceptance is one switch (`pay::LEGACY_SIGS`) ----
fn untagged_loc(sk: &SigningKey, onion: &str, ts: i64) -> Loc {
let mut msg = Vec::from(onion.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
Loc {
onion: onion.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
}
}
fn untagged_inv(sk: &SigningKey, amount: &str, address: &str, memo: &str, ts: i64) -> Invoice {
let mut msg = Vec::from(amount.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(address.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(memo.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
Invoice {
amount_atomic: amount.into(),
address: address.into(),
memo: memo.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
}
}
fn untagged_rcp(sk: &SigningKey, txid: &str, amount: &str, address: &str, ts: i64) -> Receipt {
let mut msg = Vec::from(txid.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(amount.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(address.as_bytes());
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
Receipt {
txid: txid.into(),
amount_atomic: amount.into(),
address: address.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
}
}
fn untagged_tip(
sk: &SigningKey,
txid: &str,
amount: &str,
address: &str,
memo: &str,
ts: i64,
) -> onionwire::pay::Tip {
let mut msg = Vec::from(txid.as_bytes());
for f in [amount, address, memo] {
msg.push(b'\n');
msg.extend_from_slice(f.as_bytes());
}
msg.push(b'\n');
msg.extend_from_slice(ts.to_string().as_bytes());
onionwire::pay::Tip {
txid: txid.into(),
amount_atomic: amount.into(),
address: address.into(),
memo: memo.into(),
ts,
sig: sk.sign(&msg).to_bytes().to_vec(),
}
}
#[test]
fn legacy_untagged_tip_still_verifies() {
let (sk, pk) = keypair();
let tip = untagged_tip(&sk, "abc123", "1", MAINNET_STD, "coffee", 42);
assert!(
pay::verify_tip(&pk, &tip),
"legacy tip LF layout must still verify while LEGACY_SIGS is on"
);
}
#[test]
fn legacy_off_rejects_untagged_loc() {
let (sk, pk) = keypair();
let legacy = untagged_loc(&sk, "abc.onion", 42);
let tagged = loc::sign(&sk.to_bytes(), "abc.onion", 42).unwrap();
pay::with_legacy_sigs(false, || {
assert!(
!loc::verify(&pk, &legacy),
"untagged loc must be rejected with legacy off"
);
assert!(
loc::verify(&pk, &tagged),
"tagged loc must still verify with legacy off"
);
});
assert!(
loc::verify(&pk, &legacy),
"the switch restores acceptance after the scoped override"
);
}
#[test]
fn legacy_off_rejects_untagged_inv() {
let (sk, pk) = keypair();
let legacy = untagged_inv(&sk, "1", MAINNET_STD, "coffee", 42);
let tagged = pay::sign_invoice(&sk.to_bytes(), "1", MAINNET_STD, "coffee", 42).unwrap();
pay::with_legacy_sigs(false, || {
assert!(
!pay::verify_invoice(&pk, &legacy),
"untagged inv must be rejected with legacy off"
);
assert!(
pay::verify_invoice(&pk, &tagged),
"tagged inv must still verify with legacy off"
);
});
assert!(pay::verify_invoice(&pk, &legacy));
}
#[test]
fn legacy_off_rejects_untagged_rcp() {
let (sk, pk) = keypair();
let legacy = untagged_rcp(&sk, "deadbeef", "1", MAINNET_STD, 99);
let tagged = pay::sign_receipt(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, 99).unwrap();
pay::with_legacy_sigs(false, || {
assert!(
!pay::verify_receipt(&pk, &legacy),
"untagged rcp must be rejected with legacy off"
);
assert!(
pay::verify_receipt(&pk, &tagged),
"tagged rcp must still verify with legacy off"
);
});
assert!(pay::verify_receipt(&pk, &legacy));
}
#[test]
fn legacy_off_rejects_untagged_tip() {
let (sk, pk) = keypair();
let legacy = untagged_tip(&sk, "abc123", "1", MAINNET_STD, "coffee", 42);
let tagged = pay::sign_tip(&sk.to_bytes(), "abc123", "1", MAINNET_STD, "coffee", 42).unwrap();
pay::with_legacy_sigs(false, || {
assert!(
!pay::verify_tip(&pk, &legacy),
"untagged tip must be rejected with legacy off"
);
assert!(
pay::verify_tip(&pk, &tagged),
"tagged tip must still verify with legacy off"
);
});
assert!(pay::verify_tip(&pk, &legacy));
}
/// `sign_tip` emits the tagged form only: its signature must not verify over
/// the untagged LF layout.
#[test]
fn tip_emits_tagged_form_only() {
use ed25519_dalek::{Signature, Verifier};
let (sk, pk) = keypair();
let tip = pay::sign_tip(&sk.to_bytes(), "abc123", "1", MAINNET_STD, "coffee", 42).unwrap();
assert!(pay::verify_tip(&pk, &tip));
let sig: [u8; 64] = tip.sig.as_slice().try_into().expect("64-byte sig");
let mut msg = Vec::from(b"abc123".as_slice());
for f in ["1", MAINNET_STD, "coffee"] {
msg.push(b'\n');
msg.extend_from_slice(f.as_bytes());
}
msg.push(b'\n');
msg.extend_from_slice(b"42");
assert!(
sk.verifying_key()
.verify(&msg, &Signature::from_bytes(&sig))
.is_err(),
"sign_tip must not sign the untagged layout"
);
// And the tagged signature covers the memo: changing it fails.
let mut tampered = tip.clone();
tampered.memo = String::new();
assert!(!pay::verify_tip(&pk, &tampered));
}

View file

@ -312,3 +312,25 @@ fn huge_hex_is_err_quickly() {
"decode must fail closed before a multi-MB alloc"
);
}
#[test]
fn legacy_off_rejects_v1_invite_but_v2_still_decodes() {
let _home = TempHome::new();
let store = Store::open().expect("open");
let me = store.self_identity().expect("self");
let sk: [u8; 32] = me.identity_sk.as_slice().try_into().expect("32-byte sk");
let onion = v3onion("switch");
let raw_v1 = legacy_v1(&sk, &onion, &[8u8; 32]);
let raw_v2 = qr::encode(&me.identity_sk, &onion, &[8u8; 32], &me.kem_pk).expect("encode v2");
onionwire::pay::with_legacy_sigs(false, || {
assert!(
qr::decode(&raw_v1).is_err(),
"a v1 invite must be rejected with legacy off"
);
let p = qr::decode(&raw_v2).expect("v2 invite still decodes with legacy off");
assert!(p.kem_pk.is_some(), "v2 invite keeps its PQ key");
});
// Legacy acceptance returns with the default switch state.
assert!(qr::decode(&raw_v1).is_ok());
}