Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Contracts] Experimental Slate v5 #677

Merged
merged 11 commits into from
Apr 18, 2023
65 changes: 65 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions api/src/foreign_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ pub trait ForeignRpc {
"Ok": {
"foreign_api_version": 2,
"supported_slate_versions": [
"V5",
"V4"
]
}
Expand Down Expand Up @@ -141,7 +142,7 @@ pub trait ForeignRpc {
}
],
"sta": "S1",
"ver": "4:2"
"ver": "5:2"
},
null,
null
Expand Down Expand Up @@ -176,7 +177,7 @@ pub trait ForeignRpc {
}
],
"sta": "S2",
"ver": "4:2"
"ver": "5:2"
}
}
}
Expand Down Expand Up @@ -205,7 +206,7 @@ pub trait ForeignRpc {
"method": "finalize_tx",
"id": 1,
"params": [{
"ver": "4:2",
"ver": "5:2",
"id": "0436430c-2b02-624c-2032-570501212b00",
"sta": "I2",
"off": "383bc9df0dd332629520a0a72f8dd7f0e97d579dccb4dbdc8592aa3d424c846c",
Expand Down Expand Up @@ -275,7 +276,7 @@ pub trait ForeignRpc {
}
],
"sta": "I3",
"ver": "4:2"
"ver": "5:2"
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions api/src/owner_rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,7 +504,7 @@ pub trait OwnerRpc {
}
],
"sta": "S1",
"ver": "4:2"
"ver": "5:2"
}
}
}
Expand Down Expand Up @@ -570,7 +570,7 @@ pub trait OwnerRpc {
}
],
"sta": "I1",
"ver": "4:2"
"ver": "5:2"
}
}
}
Expand Down Expand Up @@ -941,7 +941,7 @@ pub trait OwnerRpc {
"id": "0436430c-2b02-624c-2032-570501212b00",
"sigs": [],
"sta": "S3",
"ver": "4:3"
"ver": "5:3"
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions impls/src/adapters/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ impl HttpSlateSender {
return Err(Error::ClientCallback(report));
}

if supported_slate_versions.contains(&"V5".to_owned()) {
return Ok(SlateVersion::V5);
}

if supported_slate_versions.contains(&"V4".to_owned()) {
return Ok(SlateVersion::V4);
}
Expand Down Expand Up @@ -223,6 +227,7 @@ impl SlateSender for HttpSlateSender {
self.launch_tor()?;

let slate_send = match self.check_other_version(&url_str)? {
SlateVersion::V5 => VersionedSlate::into_version(slate.clone(), SlateVersion::V5)?,
SlateVersion::V4 => VersionedSlate::into_version(slate.clone(), SlateVersion::V4)?,
};
// Note: not using easy-jsonrpc as don't want the dependencies in this crate
Expand Down
1 change: 1 addition & 0 deletions libwallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ rand = "0.6"
serde = "1"
serde_derive = "1"
serde_json = "1"
serde_with = { version = "1", features = ["chrono"] }
log = "0.4"
uuid = { version = "0.8", features = ["serde", "v4"] }
chrono = { version = "0.4.11", features = ["serde"] }
Expand Down
18 changes: 10 additions & 8 deletions libwallet/src/api_impl/foreign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,14 +116,16 @@ where
let excess = ret_slate.calc_excess(keychain.secp())?;

if let Some(ref mut p) = ret_slate.payment_proof {
let sig = tx::create_payment_proof_signature(
ret_slate.amount,
&excess,
p.sender_address,
address::address_from_derivation_path(&keychain, &parent_key_id, 0)?,
)?;

p.receiver_signature = Some(sig);
if let Some(saddr) = p.sender_address {
let sig = tx::create_payment_proof_signature(
ret_slate.amount,
&excess,
saddr,
address::address_from_derivation_path(&keychain, &parent_key_id, 0)?,
)?;

p.promise_signature = Some(sig);
}
}

ret_slate.amount = 0;
Expand Down
7 changes: 5 additions & 2 deletions libwallet/src/api_impl/owner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ use crate::{
SlatepackAddress, Slatepacker, SlatepackerArgs, TxLogEntryType, ViewWallet, WalletInitStatus,
WalletInst, WalletLCProvider,
};
use chrono::prelude::{DateTime, NaiveDateTime, Utc};
use ed25519_dalek::PublicKey as DalekPublicKey;
use ed25519_dalek::SecretKey as DalekSecretKey;
use ed25519_dalek::Verifier;
Expand Down Expand Up @@ -568,9 +569,11 @@ where
let sender_address = OnionV3Address::from_private(&sec_addr_key.0)?;

slate.payment_proof = Some(PaymentInfo {
sender_address: sender_address.to_ed25519()?,
sender_address: Some(sender_address.to_ed25519()?),
receiver_address: a.pub_key,
receiver_signature: None,
promise_signature: None,
timestamp: DateTime::<Utc>::from_utc(NaiveDateTime::from_timestamp(0, 0), Utc),
memo: None,
});

context.payment_proof_derivation_index = Some(deriv_path);
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/internal/selection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ where
let sender_address = OnionV3Address::from_private(&sender_key.0)?;
t.payment_proof = Some(StoredProofInfo {
receiver_address: p.receiver_address,
receiver_signature: p.receiver_signature,
receiver_signature: p.promise_signature,
sender_address: sender_address.to_ed25519()?,
sender_address_path,
sender_signature: None,
Expand Down
52 changes: 30 additions & 22 deletions libwallet/src/internal/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,25 +421,27 @@ where
}

if let Some(ref p) = slate.clone().payment_proof {
let derivation_index = match context.payment_proof_derivation_index {
Some(i) => i,
None => 0,
};
let keychain = wallet.keychain(keychain_mask)?;
let parent_key_id = wallet.parent_key_id();
let excess = slate.calc_excess(keychain.secp())?;
let sender_key =
address::address_from_derivation_path(&keychain, &parent_key_id, derivation_index)?;
let sender_address = OnionV3Address::from_private(&sender_key.0)?;
let sig =
create_payment_proof_signature(slate.amount, &excess, p.sender_address, sender_key)?;
tx.payment_proof = Some(StoredProofInfo {
receiver_address: p.receiver_address,
receiver_signature: p.receiver_signature,
sender_address_path: derivation_index,
sender_address: sender_address.to_ed25519()?,
sender_signature: Some(sig),
})
if let Some(saddr) = p.sender_address {
let derivation_index = match context.payment_proof_derivation_index {
Some(i) => i,
None => 0,
};
let keychain = wallet.keychain(keychain_mask)?;
let parent_key_id = wallet.parent_key_id();
let excess = slate.calc_excess(keychain.secp())?;
let sender_key =
address::address_from_derivation_path(&keychain, &parent_key_id, derivation_index)?;
let sender_address = OnionV3Address::from_private(&sender_key.0)?;
let sig = create_payment_proof_signature(slate.amount, &excess, saddr, sender_key)?;
tx.payment_proof = Some(StoredProofInfo {
receiver_address: p.receiver_address,
receiver_signature: p.promise_signature,
sender_address_path: derivation_index,
sender_address: sender_address.to_ed25519()?,
sender_signature: Some(sig),
})
} else {
}
}

wallet.store_tx(&format!("{}", tx.tx_slate_id.unwrap()), slate.tx_or_err()?)?;
Expand Down Expand Up @@ -561,9 +563,15 @@ where
let orig_sender_sk =
address::address_from_derivation_path(&keychain, parent_key_id, index)?;
let orig_sender_address = OnionV3Address::from_private(&orig_sender_sk.0)?;
if p.sender_address != orig_sender_address.to_ed25519()? {
if let Some(saddr) = p.sender_address {
if saddr != orig_sender_address.to_ed25519()? {
return Err(Error::PaymentProof(
"Sender address on slate does not match original sender address".to_owned(),
));
}
} else {
return Err(Error::PaymentProof(
"Sender address on slate does not match original sender address".to_owned(),
"Sender address on slate is not provided".to_owned(),
));
}

Expand All @@ -577,7 +585,7 @@ where
&slate.calc_excess(&keychain.secp())?,
orig_sender_address.to_ed25519()?,
)?;
let sig = match p.receiver_signature {
let sig = match p.promise_signature {
Some(s) => s,
None => {
return Err(Error::PaymentProof(
Expand Down
2 changes: 1 addition & 1 deletion libwallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use grin_wallet_util as util;
use blake2_rfc as blake2;

#[macro_use]
extern crate serde_derive;
extern crate serde_with;
#[macro_use]
extern crate log;
#[macro_use]
Expand Down
Loading