//! Domain-separated application-frame signatures (loc/prf/inv/rcp). //! //! Encode still emits typed prefixes and LF-joined fields. Only the signed //! bytes change: tagged `0x00`-separated form on emit, dual-accept of the //! legacy untagged LF layout on verify. use ed25519_dalek::{Signer, SigningKey}; use onionwire::loc::{self, Loc}; use onionwire::pay::{self, Invoice, Receipt}; use onionwire::profile::{self, Profile}; use rand::rngs::OsRng; const MAINNET_STD: &str = "4AdUndXHHZ6cfufTMvppY6JwXNouMBzSkbLYfpAV5Usx3skxNgYeYTRj5UzqtReoS44qo9mtmXCqY45DJ852K5Jv2684Rge"; fn keypair() -> (SigningKey, [u8; 32]) { let sk = SigningKey::generate(&mut OsRng); let pk = sk.verifying_key().to_bytes(); (sk, pk) } #[test] fn tagged_loc_roundtrip() { let (sk, pk) = keypair(); let loc = loc::sign(&sk.to_bytes(), "abc.onion", 42).unwrap(); let parsed = loc::decode(&loc::encode(&loc)).expect("loc frame"); assert!(loc::verify(&pk, &parsed)); assert_eq!(parsed.onion, "abc.onion"); assert_eq!(parsed.ts, 42); } #[test] fn tagged_prf_roundtrip() { let (sk, pk) = keypair(); let prf = profile::sign(&sk.to_bytes(), "Ali", "hi", "", 2_000_000_000).unwrap(); let parsed = profile::decode(&profile::encode(&prf)).expect("prf frame"); assert!(profile::verify(&pk, &parsed)); assert_eq!(parsed.display_name, "Ali"); assert_eq!(parsed.bio, "hi"); assert_eq!(parsed.xmr_addr, ""); assert_eq!(parsed.ts, 2_000_000_000); } #[test] fn tagged_inv_roundtrip() { let (sk, pk) = keypair(); let inv = pay::sign_invoice(&sk.to_bytes(), "120000000000", MAINNET_STD, "coffee", 42).unwrap(); let parsed = pay::decode_invoice(&pay::encode_invoice(&inv)).expect("inv frame"); assert!(pay::verify_invoice(&pk, &parsed)); assert_eq!(parsed.amount_atomic, "120000000000"); assert_eq!(parsed.address, MAINNET_STD); assert_eq!(parsed.memo, "coffee"); assert_eq!(parsed.ts, 42); } #[test] fn tagged_rcp_roundtrip() { let (sk, pk) = keypair(); let rcp = pay::sign_receipt(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, 99).unwrap(); let parsed = pay::decode_receipt(&pay::encode_receipt(&rcp)).expect("rcp frame"); assert!(pay::verify_receipt(&pk, &parsed)); assert_eq!(parsed.txid, "deadbeef"); assert_eq!(parsed.amount_atomic, "1"); assert_eq!(parsed.address, MAINNET_STD); assert_eq!(parsed.ts, 99); } #[test] fn legacy_untagged_loc_still_verifies() { let (sk, pk) = keypair(); let onion = "abc.onion"; let ts = 42i64; let mut msg = Vec::from(onion.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); let loc = Loc { onion: onion.into(), ts, sig: sk.sign(&msg).to_bytes().to_vec(), }; assert!( loc::verify(&pk, &loc), "legacy loc LF layout must still verify" ); } #[test] fn legacy_untagged_prf_still_verifies() { let (sk, pk) = keypair(); let ts = 2_000_000_000i64; let mut msg = Vec::from(b"Ali".as_slice()); msg.push(b'\n'); msg.extend_from_slice(b"hi"); msg.push(b'\n'); msg.extend_from_slice(b""); msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); let prf = Profile { display_name: "Ali".into(), bio: "hi".into(), xmr_addr: String::new(), ts, sig: sk.sign(&msg).to_bytes().to_vec(), }; assert!( profile::verify(&pk, &prf), "legacy prf LF layout must still verify" ); } #[test] fn legacy_untagged_inv_still_verifies() { let (sk, pk) = keypair(); let ts = 42i64; let mut msg = Vec::from(b"1".as_slice()); msg.push(b'\n'); msg.extend_from_slice(MAINNET_STD.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(b"coffee"); msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); let inv = Invoice { amount_atomic: "1".into(), address: MAINNET_STD.into(), memo: "coffee".into(), ts, sig: sk.sign(&msg).to_bytes().to_vec(), }; assert!( pay::verify_invoice(&pk, &inv), "legacy inv LF layout must still verify" ); } #[test] fn legacy_untagged_rcp_still_verifies() { let (sk, pk) = keypair(); let ts = 99i64; let mut msg = Vec::from(b"deadbeef".as_slice()); msg.push(b'\n'); msg.extend_from_slice(b"1"); msg.push(b'\n'); msg.extend_from_slice(MAINNET_STD.as_bytes()); msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); let rcp = Receipt { txid: "deadbeef".into(), amount_atomic: "1".into(), address: MAINNET_STD.into(), ts, sig: sk.sign(&msg).to_bytes().to_vec(), }; assert!( pay::verify_receipt(&pk, &rcp), "legacy rcp LF layout must still verify" ); } /// prf and rcp both sign four fields. Without a type tag, a profile whose /// fields are also a valid receipt (name=txid, bio=amount, xmr=address) /// verifies as a receipt under the same key. #[test] fn prf_signature_does_not_verify_as_rcp() { let (sk, pk) = keypair(); let ts = 42i64; let prf = profile::sign(&sk.to_bytes(), "deadbeef", "1", MAINNET_STD, ts).unwrap(); assert!(profile::verify(&pk, &prf)); let rcp = Receipt { txid: "deadbeef".into(), amount_atomic: "1".into(), address: MAINNET_STD.into(), ts, sig: prf.sig.clone(), }; assert!( !pay::verify_receipt(&pk, &rcp), "prf-shaped payload must not verify as rcp" ); } /// inv field order is amount/address/memo/ts; rcp is txid/amount/address/ts. /// Reinterpret the invoice fields in order as a receipt and the signature /// must not verify (and field checks fail closed on a non-decimal amount). #[test] fn inv_signature_does_not_verify_as_rcp() { let (sk, pk) = keypair(); let ts = 42i64; let inv = pay::sign_invoice(&sk.to_bytes(), "1", MAINNET_STD, MAINNET_STD, ts).unwrap(); assert!(pay::verify_invoice(&pk, &inv)); let rcp = Receipt { txid: "1".into(), amount_atomic: MAINNET_STD.into(), address: MAINNET_STD.into(), ts, sig: inv.sig.clone(), }; assert!( !pay::verify_receipt(&pk, &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_prf(sk: &SigningKey, name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Profile { let mut msg = Vec::from(name.as_bytes()); for f in [bio, xmr_addr] { msg.push(b'\n'); msg.extend_from_slice(f.as_bytes()); } msg.push(b'\n'); msg.extend_from_slice(ts.to_string().as_bytes()); Profile { display_name: name.into(), bio: bio.into(), xmr_addr: xmr_addr.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_prf() { let (sk, pk) = keypair(); let legacy = untagged_prf(&sk, "Ali", "hi", "", 2_000_000_000); let tagged = profile::sign(&sk.to_bytes(), "Ali", "hi", "", 2_000_000_000).unwrap(); pay::with_legacy_sigs(false, || { assert!( !profile::verify(&pk, &legacy), "untagged prf must be rejected with legacy off" ); assert!( profile::verify(&pk, &tagged), "tagged prf must still verify with legacy off" ); }); assert!( profile::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)); }