diff --git a/api/src/foreign_rpc.rs b/api/src/foreign_rpc.rs index 2b3fc8171..08fffd34d 100644 --- a/api/src/foreign_rpc.rs +++ b/api/src/foreign_rpc.rs @@ -53,6 +53,7 @@ pub trait ForeignRpc { "Ok": { "foreign_api_version": 2, "supported_slate_versions": [ + "V5", "V4" ] } @@ -142,7 +143,7 @@ pub trait ForeignRpc { } ], "sta": "S1", - "ver": "4:2" + "ver": "5:2" }, null, null @@ -177,7 +178,7 @@ pub trait ForeignRpc { } ], "sta": "S2", - "ver": "4:2" + "ver": "5:2" } } } @@ -218,7 +219,7 @@ pub trait ForeignRpc { } ], "sta": "A1", - "ver": "4:2" + "ver": "5:2" }, null, null, @@ -256,7 +257,7 @@ pub trait ForeignRpc { } ], "sta": "A2", - "ver": "4:2" + "ver": "5:2" } } } @@ -285,7 +286,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", @@ -355,7 +356,7 @@ pub trait ForeignRpc { } ], "sta": "I3", - "ver": "4:2" + "ver": "5:2" } } } @@ -378,7 +379,7 @@ where fn build_coinbase(&self, block_fees: &BlockFees) -> Result { let cb: CbData = Foreign::build_coinbase(self, block_fees).map_err(|e| e.kind())?; - Ok(VersionedCoinbase::into_version(cb, SlateVersion::V4)) + Ok(VersionedCoinbase::into_version(cb, SlateVersion::V5)) } fn receive_tx( @@ -387,6 +388,7 @@ where dest_acct_name: Option, dest: Option, ) -> Result { + let v = in_slate.version(); let slate_from = Slate::from(in_slate); let out_slate = Foreign::receive_tx( self, @@ -395,7 +397,7 @@ where dest, ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate, out_slate.version()).map_err(|e| e.kind())?) + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn receive_atomic_tx( @@ -404,6 +406,7 @@ where dest_acct_name: Option, dest: Option, ) -> Result { + let v = in_slate.version(); let slate_from = Slate::from(in_slate); let out_slate = Foreign::receive_atomic_tx( self, @@ -412,13 +415,14 @@ where dest, ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate).map_err(|e| e.kind())?) + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn finalize_tx(&self, in_slate: VersionedSlate) -> Result { + let v = in_slate.version(); let out_slate = Foreign::finalize_tx(self, &Slate::from(in_slate), true).map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate, out_slate.version()).map_err(|e| e.kind())?) + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } } diff --git a/api/src/owner_rpc.rs b/api/src/owner_rpc.rs index 326bce55b..c961cd01d 100644 --- a/api/src/owner_rpc.rs +++ b/api/src/owner_rpc.rs @@ -20,8 +20,8 @@ use crate::core::global; use crate::keychain::{Identifier, Keychain}; use crate::libwallet::{ AcctPathMapping, ErrorKind, InitTxArgs, IssueInvoiceTxArgs, NodeClient, NodeHeightResult, - OutputCommitMapping, PaymentProof, Slate, Slatepack, SlatepackAddress, StatusMessage, TxFlow, - TxLogEntry, VersionedSlate, WalletInfo, WalletLCProvider, + OutputCommitMapping, PaymentProof, Slate, SlateVersion, Slatepack, SlatepackAddress, + StatusMessage, TxFlow, TxLogEntry, VersionedSlate, WalletInfo, WalletLCProvider, }; use crate::util::logger::LoggingConfig; use crate::util::secp::key::{PublicKey, SecretKey}; @@ -406,7 +406,7 @@ pub trait OwnerRpc { } ], "sta": "S1", - "ver": "4:2" + "ver": "5:2" } } } @@ -453,7 +453,7 @@ pub trait OwnerRpc { } ], "sta": "I1", - "ver": "4:2" + "ver": "5:2" } } } @@ -490,7 +490,7 @@ pub trait OwnerRpc { } ], "sta": "I1", - "ver": "4:2" + "ver": "5:2" }, "args": { "src_acct_name": null, @@ -536,7 +536,7 @@ pub trait OwnerRpc { } ], "sta": "I2", - "ver": "4:2" + "ver": "5:2" } } } @@ -635,7 +635,7 @@ pub trait OwnerRpc { "params": { "token": "d202964900000000d302964900000000d402964900000000d502964900000000", "slate": { - "ver": "4:2", + "ver": "5:2", "id": "0436430c-2b02-624c-2032-570501212b00", "sta": "S1", "off": "d202964900000000d302964900000000d402964900000000d502964900000000", @@ -714,7 +714,7 @@ pub trait OwnerRpc { } ], "sta": "A1", - "ver": "4:2" + "ver": "5:2" } } } @@ -738,7 +738,7 @@ pub trait OwnerRpc { "params": { "token": "d202964900000000d302964900000000d402964900000000d502964900000000", "slate": { - "ver": "4:2", + "ver": "5:2", "id": "0436430c-2b02-624c-2032-570501212b00", "sta": "A2", "off": "a5a632f26f27a9b71e98c1c8b8098bb41204ffcfd206d995f9c16d10764ad95a", @@ -807,7 +807,7 @@ pub trait OwnerRpc { } ], "sta": "A3", - "ver": "4:2" + "ver": "5:2" } } } @@ -836,7 +836,7 @@ pub trait OwnerRpc { "token": "d202964900000000d302964900000000d402964900000000d502964900000000", "slate": { - "ver": "4:2", + "ver": "5:2", "id": "0436430c-2b02-624c-2032-570501212b00", "sta": "S2", "off": "6c6a69136154775488782121887bb3c32787a8320551fdb9732ec2d333fe54ee", @@ -898,7 +898,7 @@ pub trait OwnerRpc { } ], "sta": "S3", - "ver": "4:2" + "ver": "5:2" } } } @@ -922,7 +922,7 @@ pub trait OwnerRpc { "params": { "token": "d202964900000000d302964900000000d402964900000000d502964900000000", "slate": { - "ver": "4:2", + "ver": "5:2", "id": "0436430c-2b02-624c-2032-570501212b00", "sta": "S3", "off": "750dbf4fd43b7f4cfd68d2698a522f3ff6e6a00ad9895b33f1ec46493b837b49", @@ -1050,7 +1050,7 @@ pub trait OwnerRpc { "id": "0436430c-2b02-624c-2032-570501212b00", "sigs": [], "sta": "S3", - "ver": "4:3" + "ver": "5:3" } } } @@ -1689,7 +1689,7 @@ pub trait OwnerRpc { "sender_index": 0, "recipients": [], "slate": { - "ver": "4:2", + "ver": "5:2", "id": "0436430c-2b02-624c-2032-570501212b00", "sta": "S1", "off": "d202964900000000d302964900000000d402964900000000d502964900000000", @@ -1712,7 +1712,7 @@ pub trait OwnerRpc { "id": 1, "jsonrpc": "2.0", "result": { - "Ok": "BEGINSLATEPACK. xyfzdULuUuM5r3R kS68aywyCuYssPs Jf1JbvnBcK6NDDo ajiGAgh2SPx4t49 xtKuJE3BZCcSEue ksecMmbSoV2DQbX gGcmJniP9UadcmR N1KSc5FBhwAaUjy LXeYDP7EV7Cmsj4 pLaJdZTJTQbccUH 2zG8QTgoEiEWP5V T6rKst1TibmDAFm RRVHYDtskdYJb5G krqfpgN7RjvPfpm Z5ZFyz6ipAt5q9T 2HCjrTxkHdVi9js 22tr2Lx6iXT5vm8 JL6HhjwyFrSaEmN AjsBE8jgiaAABA6 GGZKwcXeXToMfRt nL9DeX1. ENDSLATEPACK." + "Ok": "BEGINSLATEPACK. 3mz5n34kmt5bXXV ubrnwhufnUc3MHD Jp7ZvuMJTTD96ru 7R1T3SpbmENmkvt 1Tazq77CBGmyNAY RxLCCi7TtGigAtY 7CbhdhNhLpNFK2J 82WoGtDPGPc7ciq TkEiRoopr1hVh1p JKqy9mwHb3HHcHR Znz1QDxBbULjmfU rcQcbuXkGzFEg4i m5xGnvYPt8HMTm8 d3kSe7hSX5929N3 xcdAFBVMYm5GUgd PWvW7QUP5frB4aP zazzB7P1w89x7Jg GmLiX3wBzt4bqJD 83sqrJR5BW8x4NB rvRqsXRDScQTqZd CacQSv7y. ENDSLATEPACK." } } # "# @@ -1766,7 +1766,7 @@ pub trait OwnerRpc { } ], "sta": "S1", - "ver": "4:2" + "ver": "5:2" } } } @@ -2021,7 +2021,8 @@ where fn init_send_tx(&self, token: Token, args: InitTxArgs) -> Result { let slate = Owner::init_send_tx(self, (&token.keychain_mask).as_ref(), args) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(slate, slate.version()).map_err(|e| e.kind())?) + let v = slate.version(); + Ok(VersionedSlate::into_version(slate, v).map_err(|e| e.kind())?) } fn issue_invoice_tx( @@ -2031,7 +2032,8 @@ where ) -> Result { let slate = Owner::issue_invoice_tx(self, (&token.keychain_mask).as_ref(), args) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(slate, slate.version()).map_err(|e| e.kind())?) + let v = slate.version(); + Ok(VersionedSlate::into_version(slate, v).map_err(|e| e.kind())?) } fn process_invoice_tx( @@ -2047,7 +2049,8 @@ where args, ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate, out_slate.version()).map_err(|e| e.kind())?) + let v = out_slate.version(); + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn process_multisig_tx( @@ -2061,7 +2064,7 @@ where &Slate::from(in_slate), ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate).map_err(|e| e.kind())?) + Ok(VersionedSlate::into_version(out_slate, SlateVersion::V5).map_err(|e| e.kind())?) } fn init_atomic_swap( @@ -2071,7 +2074,8 @@ where ) -> Result { let out_slate = Owner::init_atomic_swap(self, (&token.keychain_mask).as_ref(), args) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate).map_err(|e| e.kind())?) + let v = out_slate.version(); + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn countersign_atomic_swap( @@ -2084,7 +2088,8 @@ where let out_slate = Owner::countersign_atomic_swap(self, &slate, (&token.keychain_mask).as_ref(), r_addr) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate).map_err(|e| e.kind())?) + let v = out_slate.version(); + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn finalize_tx( @@ -2098,7 +2103,8 @@ where &Slate::from(in_slate), ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(out_slate).map_err(|e| e.kind())?) + let v = out_slate.version(); + Ok(VersionedSlate::into_version(out_slate, v).map_err(|e| e.kind())?) } fn tx_lock_outputs(&self, token: Token, in_slate: VersionedSlate) -> Result<(), ErrorKind> { @@ -2134,9 +2140,10 @@ where ) .map_err(|e| e.kind())?; match out_slate { - Some(s) => Ok(Some( - VersionedSlate::into_version(s, s.version()).map_err(|e| e.kind())?, - )), + Some(s) => Ok({ + let v = s.version(); + Some(VersionedSlate::into_version(s, v).map_err(|e| e.kind())?) + }), None => Ok(None), } } @@ -2337,7 +2344,8 @@ where secret_indices, ) .map_err(|e| e.kind())?; - Ok(VersionedSlate::into_version(slate, slate.version()).map_err(|e| e.kind())?) + let v = slate.version(); + Ok(VersionedSlate::into_version(slate, v).map_err(|e| e.kind())?) } fn decode_slatepack_message( diff --git a/api/tests/slate_versioning.rs b/api/tests/slate_versioning.rs index 2f675998c..7bf69aaea 100644 --- a/api/tests/slate_versioning.rs +++ b/api/tests/slate_versioning.rs @@ -84,11 +84,7 @@ fn receive_tx(vs: VersionedSlate) -> VersionedSlate { ) .unwrap(); let (call, tracker) = bound_method.call(); -<<<<<<< HEAD - let json_response = run_doctest_foreign(call.as_request(), dir, true, 5, false, false) -======= - let json_response = run_doctest_foreign(call.as_request(), dir, 5, TxFlow::Standard) ->>>>>>> 6535bda (Add atomic swap owner/foreign RPC functions) + let json_response = run_doctest_foreign(call.as_request(), dir, 5, TxFlow::Standard, false) .unwrap() .unwrap(); let mut response = easy_jsonrpc_mw::Response::from_json_response(json_response).unwrap(); diff --git a/impls/src/adapters/file.rs b/impls/src/adapters/file.rs index 00291f5a9..134b9b2a3 100644 --- a/impls/src/adapters/file.rs +++ b/impls/src/adapters/file.rs @@ -37,7 +37,7 @@ impl SlatePutter for PathToSlate { }*/ let mut pub_tx = File::create(&self.0)?; // TODO: - let out_slate = VersionedSlate::into_version(slate.clone())?; + let out_slate = VersionedSlate::into_version(slate.clone(), slate.version())?; if as_bin { let bin_slate = VersionedBinSlate::try_from(out_slate).map_err(|_| ErrorKind::SlateSer)?; diff --git a/impls/src/adapters/http.rs b/impls/src/adapters/http.rs index 3f0b93fe4..58078e99a 100644 --- a/impls/src/adapters/http.rs +++ b/impls/src/adapters/http.rs @@ -145,6 +145,10 @@ impl HttpSlateSender { return Err(ErrorKind::ClientCallback(report).into()); } + if supported_slate_versions.contains(&"V5".to_owned()) { + return Ok(SlateVersion::V5); + } + if supported_slate_versions.contains(&"V4".to_owned()) { return Ok(SlateVersion::V4); } diff --git a/libwallet/src/slate.rs b/libwallet/src/slate.rs index d8e84cf0b..3be7d705f 100644 --- a/libwallet/src/slate.rs +++ b/libwallet/src/slate.rs @@ -936,7 +936,7 @@ impl Slate { 4 => SlateVersion::V4, 5 | _ => SlateVersion::V5, } - } + } /// Calculate multisig key ID pub fn create_multisig_id(&self) -> Identifier { @@ -970,8 +970,16 @@ impl Serialize for Slate { where S: Serializer, { - let v5 = SlateV5::from(self); - v5.serialize(serializer) + match self.version() { + SlateVersion::V4 => { + let v4 = SlateV4::from(self); + v4.serialize(serializer) + } + SlateVersion::V5 => { + let v5 = SlateV5::from(self); + v5.serialize(serializer) + } + } } } // Current slate version to versioned conversions diff --git a/libwallet/src/slate_versions/mod.rs b/libwallet/src/slate_versions/mod.rs index 7ff1a0934..4f63d5ca1 100644 --- a/libwallet/src/slate_versions/mod.rs +++ b/libwallet/src/slate_versions/mod.rs @@ -39,7 +39,7 @@ pub mod v5; pub mod v5_bin; /// The most recent version of the slate -pub const CURRENT_SLATE_VERSION: u16 = 4; +pub const CURRENT_SLATE_VERSION: u16 = 5; /// The grin block header this slate is intended to be compatible with pub const GRIN_BLOCK_HEADER_VERSION: u16 = 3; @@ -47,10 +47,10 @@ pub const GRIN_BLOCK_HEADER_VERSION: u16 = 3; /// Existing versions of the slate #[derive(EnumIter, Serialize, Deserialize, Clone, Debug, PartialEq, PartialOrd, Eq, Ord)] pub enum SlateVersion { - /// V4 (most current) - V4, /// V5 (next version) V5, + /// V4 (most current) + V4, } #[derive(Debug, Serialize, Deserialize)] @@ -58,10 +58,10 @@ pub enum SlateVersion { /// Versions are ordered newest to oldest so serde attempts to /// deserialize newer versions first, then falls back to older versions. pub enum VersionedSlate { - /// Current (4.0.0 Onwards ) - V4(SlateV4), /// Next (5.1.0 Onwards) V5(SlateV5), + /// Current (4.0.0 Onwards ) + V4(SlateV4), } impl VersionedSlate { @@ -74,8 +74,8 @@ impl VersionedSlate { } /// convert this slate type to a specified older version - pub fn into_version(slate: Slate) -> Result { - match slate.version() { + pub fn into_version(slate: Slate, version: SlateVersion) -> Result { + match version { SlateVersion::V4 => Ok(VersionedSlate::V4(slate.into())), SlateVersion::V5 => Ok(VersionedSlate::V5(slate.into())), } @@ -96,10 +96,10 @@ impl From for Slate { /// Binary versions, can only be parsed 1:1 into the appropriate /// version, and VersionedSlate can up/downgrade from there pub enum VersionedBinSlate { - /// Version 4, binary - V4(SlateV4Bin), /// Version 5, binary V5(SlateV5Bin), + /// Version 4, binary + V4(SlateV4Bin), } impl TryFrom for VersionedBinSlate { @@ -126,10 +126,10 @@ impl From for VersionedSlate { /// Versions are ordered newest to oldest so serde attempts to /// deserialize newer versions first, then falls back to older versions. pub enum VersionedCoinbase { - /// Current supported coinbase version. - V4(CoinbaseV4), /// Next supported coinbase version. V5(CoinbaseV5), + /// Current supported coinbase version. + V4(CoinbaseV4), } impl VersionedCoinbase { diff --git a/libwallet/src/slate_versions/v5_bin.rs b/libwallet/src/slate_versions/v5_bin.rs index 9c742bdc3..814460aac 100644 --- a/libwallet/src/slate_versions/v5_bin.rs +++ b/libwallet/src/slate_versions/v5_bin.rs @@ -459,7 +459,7 @@ impl serde::Serialize for SlateV5Bin { S: serde::Serializer, { let mut vec = vec![]; - grin_ser::serialize(&mut vec, grin_ser::ProtocolVersion(4), self) + grin_ser::serialize(&mut vec, grin_ser::ProtocolVersion(5), self) .map_err(|err| serde::ser::Error::custom(err.to_string()))?; serializer.serialize_bytes(&vec) } diff --git a/libwallet/src/slatepack/types.rs b/libwallet/src/slatepack/types.rs index 877dd8ab1..60bc095a4 100644 --- a/libwallet/src/slatepack/types.rs +++ b/libwallet/src/slatepack/types.rs @@ -20,7 +20,7 @@ use x25519_dalek::StaticSecret; use crate::dalek_ser; use crate::grin_core::ser::{self, Readable, Reader, Writeable, Writer}; -use crate::{Error, ErrorKind}; +use crate::{Error, ErrorKind, CURRENT_SLATE_VERSION}; use grin_wallet_util::byte_ser; use super::SlatepackAddress; @@ -260,8 +260,12 @@ impl serde::Serialize for SlatepackBin { S: serde::Serializer, { let mut vec = vec![]; - ser::serialize(&mut vec, ser::ProtocolVersion(4), self) - .map_err(|err| serde::ser::Error::custom(err.to_string()))?; + ser::serialize( + &mut vec, + ser::ProtocolVersion(CURRENT_SLATE_VERSION as u32), + self, + ) + .map_err(|err| serde::ser::Error::custom(err.to_string()))?; serializer.serialize_bytes(&vec) } } @@ -285,8 +289,11 @@ impl<'de> serde::Deserialize<'de> for SlatepackBin { E: serde::de::Error, { let mut reader = std::io::Cursor::new(value.to_vec()); - let s = ser::deserialize(&mut reader, ser::ProtocolVersion(4)) - .map_err(|err| serde::de::Error::custom(err.to_string()))?; + let s = ser::deserialize( + &mut reader, + ser::ProtocolVersion(CURRENT_SLATE_VERSION as u32), + ) + .map_err(|err| serde::de::Error::custom(err.to_string()))?; Ok(s) } } @@ -488,8 +495,12 @@ impl serde::Serialize for SlatepackEncMetadataBin { S: serde::Serializer, { let mut vec = vec![]; - ser::serialize(&mut vec, ser::ProtocolVersion(4), self) - .map_err(|err| serde::ser::Error::custom(err.to_string()))?; + ser::serialize( + &mut vec, + ser::ProtocolVersion(CURRENT_SLATE_VERSION as u32), + self, + ) + .map_err(|err| serde::ser::Error::custom(err.to_string()))?; serializer.serialize_bytes(&vec) } } @@ -513,8 +524,11 @@ impl<'de> serde::Deserialize<'de> for SlatepackEncMetadataBin { E: serde::de::Error, { let mut reader = std::io::Cursor::new(value.to_vec()); - let s = ser::deserialize(&mut reader, ser::ProtocolVersion(4)) - .map_err(|err| serde::de::Error::custom(err.to_string()))?; + let s = ser::deserialize( + &mut reader, + ser::ProtocolVersion(CURRENT_SLATE_VERSION as u32), + ) + .map_err(|err| serde::de::Error::custom(err.to_string()))?; Ok(s) } } diff --git a/tests/owner_v3_lifecycle.rs b/tests/owner_v3_lifecycle.rs index a2ea0bd1b..784526a9f 100644 --- a/tests/owner_v3_lifecycle.rs +++ b/tests/owner_v3_lifecycle.rs @@ -405,12 +405,13 @@ fn owner_v3_lifecycle() -> Result<(), grin_wallet_controller::Error> { //16) Finalize the invoice tx (to foreign api) // (Tests that foreign API on same port also has its stored mask updated) + let v = slate.version(); let req = serde_json::json!({ "jsonrpc": "2.0", "id": 1, "method": "finalize_tx", "params": { - "slate": VersionedSlate::into_version(slate, slate.version())?, + "slate": VersionedSlate::into_version(slate, v)?, } }); let res =