Merge branch 'main' into wt/t_6ddd495e
Some checks failed
ci / test (pull_request) Failing after 2m29s
Some checks failed
ci / test (pull_request) Failing after 2m29s
This commit is contained in:
commit
0a888eced5
5 changed files with 371 additions and 16 deletions
26
src/loc.rs
26
src/loc.rs
|
|
@ -27,7 +27,7 @@ pub fn sign(identity_sk: &[u8], onion: &str, ts: i64) -> Result<Loc> {
|
|||
.try_into()
|
||||
.map_err(|_| Error("identity secret key must be 32 bytes".into()))?;
|
||||
let sk = SigningKey::from_bytes(&sk_bytes);
|
||||
let sig = sk.sign(&sign_msg(onion, ts)).to_bytes().to_vec();
|
||||
let sig = sk.sign(&sign_msg_v2(onion, ts)).to_bytes().to_vec();
|
||||
Ok(Loc {
|
||||
onion: onion.to_string(),
|
||||
ts,
|
||||
|
|
@ -50,8 +50,12 @@ pub fn verify(identity_pk: &[u8], loc: &Loc) -> bool {
|
|||
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
||||
return false;
|
||||
};
|
||||
vk.verify(&sign_msg(&loc.onion, loc.ts), &Signature::from_bytes(&sig))
|
||||
let signature = Signature::from_bytes(&sig);
|
||||
vk.verify(&sign_msg_v2(&loc.onion, loc.ts), &signature)
|
||||
.is_ok()
|
||||
|| vk
|
||||
.verify(&sign_msg_v1(&loc.onion, loc.ts), &signature)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn encode(loc: &Loc) -> Vec<u8> {
|
||||
|
|
@ -77,13 +81,29 @@ pub fn decode(pt: &[u8]) -> Option<Loc> {
|
|||
Some(Loc { onion, ts, sig })
|
||||
}
|
||||
|
||||
fn sign_msg(onion: &str, ts: i64) -> Vec<u8> {
|
||||
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> {
|
||||
let mut msg = Vec::from(onion.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(ts.to_string().as_bytes());
|
||||
msg
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
|
|
|||
59
src/pay.rs
59
src/pay.rs
|
|
@ -179,7 +179,10 @@ pub fn sign_invoice(
|
|||
ts: i64,
|
||||
) -> Result<Invoice> {
|
||||
check_invoice_fields(amount_atomic, address, memo)?;
|
||||
let sig = sign_bytes(identity_sk, &inv_sign_msg(amount_atomic, address, memo, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&inv_sign_msg_v2(amount_atomic, address, memo, ts),
|
||||
)?;
|
||||
Ok(Invoice {
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
address: address.to_string(),
|
||||
|
|
@ -195,7 +198,11 @@ pub fn verify_invoice(identity_pk: &[u8], inv: &Invoice) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&inv_sign_msg(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv_sign_msg_v2(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv.sig,
|
||||
) || verify_bytes(
|
||||
identity_pk,
|
||||
&inv_sign_msg_v1(&inv.amount_atomic, &inv.address, &inv.memo, inv.ts),
|
||||
&inv.sig,
|
||||
)
|
||||
}
|
||||
|
|
@ -243,7 +250,10 @@ pub fn sign_receipt(
|
|||
ts: i64,
|
||||
) -> Result<Receipt> {
|
||||
check_receipt_fields(txid, amount_atomic, address)?;
|
||||
let sig = sign_bytes(identity_sk, &rcp_sign_msg(txid, amount_atomic, address, ts))?;
|
||||
let sig = sign_bytes(
|
||||
identity_sk,
|
||||
&rcp_sign_msg_v2(txid, amount_atomic, address, ts),
|
||||
)?;
|
||||
Ok(Receipt {
|
||||
txid: txid.to_string(),
|
||||
amount_atomic: amount_atomic.to_string(),
|
||||
|
|
@ -259,7 +269,11 @@ pub fn verify_receipt(identity_pk: &[u8], rcp: &Receipt) -> bool {
|
|||
}
|
||||
verify_bytes(
|
||||
identity_pk,
|
||||
&rcp_sign_msg(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp_sign_msg_v2(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp.sig,
|
||||
) || verify_bytes(
|
||||
identity_pk,
|
||||
&rcp_sign_msg_v1(&rcp.txid, &rcp.amount_atomic, &rcp.address, rcp.ts),
|
||||
&rcp.sig,
|
||||
)
|
||||
}
|
||||
|
|
@ -316,7 +330,19 @@ fn check_receipt_fields(txid: &str, amount_atomic: &str, address: &str) -> Resul
|
|||
check_address(address)
|
||||
}
|
||||
|
||||
fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
fn inv_sign_msg_v2(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-inv-v1",
|
||||
&[
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
memo.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn inv_sign_msg_v1(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(amount_atomic.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(address.as_bytes());
|
||||
|
|
@ -327,7 +353,19 @@ fn inv_sign_msg(amount_atomic: &str, address: &str, memo: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
fn rcp_sign_msg_v2(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-rcp-v1",
|
||||
&[
|
||||
txid.as_bytes(),
|
||||
amount_atomic.as_bytes(),
|
||||
address.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn rcp_sign_msg_v1(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(txid.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(amount_atomic.as_bytes());
|
||||
|
|
@ -338,6 +376,15 @@ fn rcp_sign_msg(txid: &str, amount_atomic: &str, address: &str, ts: i64) -> Vec<
|
|||
msg
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn sign_bytes(identity_sk: &[u8], msg: &[u8]) -> Result<Vec<u8>> {
|
||||
let sk_bytes: [u8; 32] = identity_sk
|
||||
.try_into()
|
||||
|
|
|
|||
|
|
@ -43,7 +43,7 @@ pub fn sign(
|
|||
.map_err(|_| Error("identity secret key must be 32 bytes".into()))?;
|
||||
let sk = SigningKey::from_bytes(&sk_bytes);
|
||||
let sig = sk
|
||||
.sign(&sign_msg(display_name, bio, xmr_addr, ts))
|
||||
.sign(&sign_msg_v2(display_name, bio, xmr_addr, ts))
|
||||
.to_bytes()
|
||||
.to_vec();
|
||||
Ok(Profile {
|
||||
|
|
@ -73,11 +73,18 @@ pub fn verify(identity_pk: &[u8], prf: &Profile) -> bool {
|
|||
let Ok(vk) = VerifyingKey::from_bytes(&pk) else {
|
||||
return false;
|
||||
};
|
||||
let signature = Signature::from_bytes(&sig);
|
||||
vk.verify(
|
||||
&sign_msg(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&Signature::from_bytes(&sig),
|
||||
&sign_msg_v2(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&signature,
|
||||
)
|
||||
.is_ok()
|
||||
|| vk
|
||||
.verify(
|
||||
&sign_msg_v1(&prf.display_name, &prf.bio, &prf.xmr_addr, prf.ts),
|
||||
&signature,
|
||||
)
|
||||
.is_ok()
|
||||
}
|
||||
|
||||
pub fn encode(prf: &Profile) -> Vec<u8> {
|
||||
|
|
@ -131,7 +138,19 @@ fn check_fields(display_name: &str, bio: &str, xmr_addr: &str) -> Result<()> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
fn sign_msg_v2(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
tagged(
|
||||
b"onionwire-prf-v1",
|
||||
&[
|
||||
display_name.as_bytes(),
|
||||
bio.as_bytes(),
|
||||
xmr_addr.as_bytes(),
|
||||
ts.to_string().as_bytes(),
|
||||
],
|
||||
)
|
||||
}
|
||||
|
||||
fn sign_msg_v1(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
||||
let mut msg = Vec::from(display_name.as_bytes());
|
||||
msg.push(b'\n');
|
||||
msg.extend_from_slice(bio.as_bytes());
|
||||
|
|
@ -142,6 +161,15 @@ fn sign_msg(display_name: &str, bio: &str, xmr_addr: &str, ts: i64) -> Vec<u8> {
|
|||
msg
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue