From 2fcad6d1ef1ed97c555a47f036990fa50e3563a4 Mon Sep 17 00:00:00 2001 From: Hugo Trentesaux <hugo@trentesaux.fr> Date: Tue, 4 Jun 2024 13:17:50 +0200 Subject: [PATCH] enable offchain comment using ipfs --- Cargo.lock | 193 ++++++++++++++++++++++++++++++-------- Cargo.toml | 3 +- src/commands/account.rs | 10 +- src/commands/commented.rs | 33 ++++++- src/commands/ipfs.rs | 9 +- 5 files changed, 200 insertions(+), 48 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 43f67b8..4ca28c0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -106,7 +106,7 @@ version = "0.8.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "77c3a9648d43b9cd48db467b3f87fdd6e146bcc88ab0180006cef2179fe11d01" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "getrandom", "once_cell", "version_check", @@ -496,7 +496,7 @@ dependencies = [ "ark-std", "digest 0.10.7", "rand_core 0.6.4", - "sha3", + "sha3 0.10.8", ] [[package]] @@ -520,6 +520,12 @@ dependencies = [ "nodrop", ] +[[package]] +name = "arrayvec" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23b62fc65de8e4e7f52534fb52b0f3ed04746ae267519eef2a83941e8085068b" + [[package]] name = "arrayvec" version = "0.7.4" @@ -577,7 +583,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8f97ab0c5b00a7cdbe5a371b9a782ee7be1316095885c8a4ea1daf490eb0ef65" dependencies = [ "async-lock 3.3.0", - "cfg-if", + "cfg-if 1.0.0", "concurrent-queue", "futures-io", "futures-lite", @@ -631,7 +637,7 @@ dependencies = [ "async-lock 3.3.0", "async-signal", "blocking", - "cfg-if", + "cfg-if 1.0.0", "event-listener 4.0.3", "futures-lite", "rustix 0.38.31", @@ -647,7 +653,7 @@ dependencies = [ "async-io", "async-lock 2.8.0", "atomic-waker", - "cfg-if", + "cfg-if 1.0.0", "futures-core", "futures-io", "rustix 0.38.31", @@ -699,7 +705,7 @@ checksum = "2089b7e3f35b9dd2d0ed921ead4f6d318c27680d4a5bd167b3ee120edb105837" dependencies = [ "addr2line 0.21.0", "cc", - "cfg-if", + "cfg-if 1.0.0", "libc", "miniz_oxide", "object 0.32.2", @@ -867,6 +873,17 @@ dependencies = [ "constant_time_eq 0.1.5", ] +[[package]] +name = "blake2b_simd" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "afa748e348ad3be8263be728124b24a24f268266f6f5d58af9d75f6a40b5c587" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "constant_time_eq 0.1.5", +] + [[package]] name = "blake2b_simd" version = "1.0.2" @@ -878,12 +895,39 @@ dependencies = [ "constant_time_eq 0.3.0", ] +[[package]] +name = "blake2s_simd" +version = "0.5.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9e461a7034e85b211a4acb57ee2e6730b32912b06c08cc242243c39fc21ae6a2" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "constant_time_eq 0.1.5", +] + +[[package]] +name = "blake3" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b64485778c4f16a6a5a9d335e80d449ac6c70cdd6a06d2af18a6f6f775a125b3" +dependencies = [ + "arrayref", + "arrayvec 0.5.2", + "cc", + "cfg-if 0.1.10", + "constant_time_eq 0.1.5", + "crypto-mac", + "digest 0.9.0", +] + [[package]] name = "block-buffer" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4152116fd6e9dadb291ae18fc1ec3575ed6d84c29642d97890f4b4a3417297e4" dependencies = [ + "block-padding", "generic-array", ] @@ -896,6 +940,12 @@ dependencies = [ "generic-array", ] +[[package]] +name = "block-padding" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d696c370c750c948ada61c69a0ee2cbbb9c50b1019ddb86d9317157a99c2cae" + [[package]] name = "blocking" version = "1.5.1" @@ -966,6 +1016,12 @@ dependencies = [ "libc", ] +[[package]] +name = "cfg-if" +version = "0.1.10" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822" + [[package]] name = "cfg-if" version = "1.0.0" @@ -978,7 +1034,7 @@ version = "0.9.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c3613f74bd2eac03dad61bd53dbe620703d4371614fe0bc3b9f04dd36fe4e818" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cipher", "cpufeatures", ] @@ -1008,6 +1064,17 @@ dependencies = [ "windows-targets 0.52.0", ] +[[package]] +name = "cid" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f6b8976b33648136e969aafa6eb33d58ff0d301fa0b4e8d513db58fd32cd81aa" +dependencies = [ + "multibase", + "multihash 0.14.0", + "unsigned-varint", +] + [[package]] name = "cipher" version = "0.4.4" @@ -1216,7 +1283,7 @@ version = "0.3.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eeaa953eaad386a53111e47172c2fedba671e5684c8dd601a5f474f4f118710f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1243,7 +1310,7 @@ version = "1.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b540bd8bc810d3885c6ea91e2018302f68baba2129ab3e88f32389ee9370880d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -1376,7 +1443,7 @@ version = "4.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0a677b8922c94e01bdbb12126b0bc852f00447528dee1782229af9c720c3f348" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "curve25519-dalek-derive", "digest 0.10.7", @@ -1474,7 +1541,7 @@ version = "5.5.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "978747c1d849a7d2ee5e8adc0159961c48fb7e5db2f06af6723b80123bb53856" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "hashbrown 0.14.3", "lock_api", "once_cell", @@ -1810,7 +1877,7 @@ version = "0.8.33" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7268b386296a025e474d5140678f75d6de9493ae55a5d709eeb9dd08149945e1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -2015,7 +2082,7 @@ version = "15.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "878babb0b136e731cc77ec2fd883ff02745ff21e6fb662729953d44923df009c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "parity-scale-codec", "scale-info", ] @@ -2026,7 +2093,7 @@ version = "16.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "87cf1549fba25a6fcac22785b61698317d958e96cac72a59102ea45b9ae64692" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "parity-scale-codec", "scale-info", "serde", @@ -2164,6 +2231,7 @@ dependencies = [ "anyhow", "bip39", "bs58", + "cid", "clap", "comfy-table", "confy", @@ -2205,7 +2273,7 @@ version = "0.2.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "190092ea657667030ac6a35e305e62fc4dd69fd98ac98631e5d3a2b1575a12b5" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "wasi", ] @@ -2742,7 +2810,7 @@ version = "0.1.12" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7a5bbe824c507c5da5956355e86a746d82e0e1464f65d862cc5e71da70e94b2c" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -2816,7 +2884,7 @@ checksum = "9b74065805db266ba2c6edbd670b23c4714824a955628472b2e46cc9f3a869cb" dependencies = [ "async-trait", "bytes", - "cfg-if", + "cfg-if 1.0.0", "common-multipart-rfc7578", "dirs", "futures", @@ -2978,7 +3046,7 @@ version = "0.13.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "956ff9b67e26e1a6a866cb758f12c6f8746208489e3e4a4b5580802f2f0a587b" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "ecdsa", "elliptic-curve", "once_cell", @@ -3235,7 +3303,7 @@ dependencies = [ "data-encoding", "log", "multibase", - "multihash", + "multihash 0.17.0", "percent-encoding", "serde", "static_assertions", @@ -3254,6 +3322,23 @@ dependencies = [ "data-encoding-macro", ] +[[package]] +name = "multihash" +version = "0.14.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "752a61cd890ff691b4411423d23816d5866dd5621e4d1c5687a53b94b5a979d8" +dependencies = [ + "blake2b_simd 0.5.11", + "blake2s_simd", + "blake3", + "digest 0.9.0", + "generic-array", + "multihash-derive 0.7.2", + "sha2 0.9.9", + "sha3 0.9.1", + "unsigned-varint", +] + [[package]] name = "multihash" version = "0.17.0" @@ -3261,10 +3346,24 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "835d6ff01d610179fbce3de1694d007e500bf33a7f29689838941d6bf783ae40" dependencies = [ "core2", - "multihash-derive", + "multihash-derive 0.8.1", "unsigned-varint", ] +[[package]] +name = "multihash-derive" +version = "0.7.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "424f6e86263cd5294cbd7f1e95746b95aca0e0d66bff31e5a40d6baa87b4aa99" +dependencies = [ + "proc-macro-crate 1.1.3", + "proc-macro-error", + "proc-macro2", + "quote", + "syn 1.0.109", + "synstructure", +] + [[package]] name = "multihash-derive" version = "0.8.1" @@ -3507,7 +3606,7 @@ version = "0.9.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c42a9226546d68acdd9c0a280d17ce19bfe27a46bf68784e4066115788d008e" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "redox_syscall", "smallvec", @@ -3650,7 +3749,7 @@ version = "3.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "30054e72317ab98eddd8561db0f6524df3367636884b7b21b703e4b280a84a14" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "concurrent-queue", "pin-project-lite", "rustix 0.38.31", @@ -4333,7 +4432,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "eca070c12893629e2cc820a9761bedf6ce1dcddc9852984d1dc734b8bd9bd024" dependencies = [ "bitvec", - "cfg-if", + "cfg-if 1.0.0", "derive_more", "parity-scale-codec", "scale-info-derive", @@ -4412,7 +4511,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "772575a524feeb803e5b0fcbc6dd9f367e579488197c94c6e4023aad2305774d" dependencies = [ "ahash 0.8.7", - "cfg-if", + "cfg-if 1.0.0", "hashbrown 0.13.2", ] @@ -4626,7 +4725,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "99cd6713db3cf16b6c84e06321e049a9b9f699826e16096d23bbcc44d15d51a6" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", @@ -4639,7 +4738,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4d58a1e1bf39749807d89cf2d98ac2dfa0ff1cb3faa38fbb64dd88ac8013d800" dependencies = [ "block-buffer 0.9.0", - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.9.0", "opaque-debug", @@ -4651,11 +4750,23 @@ version = "0.10.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "793db75ad2bcafc3ffa7c68b215fee268f537982cd901d132f89c6343f3a3dc8" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "cpufeatures", "digest 0.10.7", ] +[[package]] +name = "sha3" +version = "0.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f81199417d4e5de3f04b1e871023acea7389672c4135918f05aa9cbf2f2fa809" +dependencies = [ + "block-buffer 0.9.0", + "digest 0.9.0", + "keccak", + "opaque-debug", +] + [[package]] name = "sha3" version = "0.10.8" @@ -4803,7 +4914,7 @@ dependencies = [ "serde", "serde_json", "sha2 0.10.8", - "sha3", + "sha3 0.10.8", "siphasher", "slab", "smallvec", @@ -4994,11 +5105,11 @@ name = "sp-crypto-hashing" version = "0.1.0" source = "git+https://github.com/duniter/duniter-polkadot-sdk.git?branch=duniter-substrate-v1.11.0#030a7b8fd5ef03e4da466e65ce8fe92e3cff90e4" dependencies = [ - "blake2b_simd", + "blake2b_simd 1.0.2", "byteorder", "digest 0.10.7", "sha2 0.10.8", - "sha3", + "sha3 0.10.8", "twox-hash", ] @@ -5621,7 +5732,7 @@ version = "1.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "once_cell", ] @@ -5954,7 +6065,7 @@ version = "1.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "97fee6b57c6a41524a810daee9286c02d7752c4253064d0b05472833a438f675" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "digest 0.10.7", "rand", "static_assertions", @@ -6145,7 +6256,7 @@ dependencies = [ "rand_chacha", "rand_core 0.6.4", "sha2 0.10.8", - "sha3", + "sha3 0.10.8", "thiserror", "zeroize", ] @@ -6181,7 +6292,7 @@ version = "0.2.91" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c1e124130aee3fb58c5bdd6b639a0509486b0338acaaae0c84a5124b0f588b7f" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "wasm-bindgen-macro", ] @@ -6206,7 +6317,7 @@ version = "0.4.41" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "877b9c3f61ceea0e56331985743b13f3d25c406a7098d45180fb5f09bc19ed97" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "js-sys", "wasm-bindgen", "web-sys", @@ -6299,7 +6410,7 @@ checksum = "f907fdead3153cb9bfb7a93bbd5b62629472dc06dee83605358c64c52ed3dda9" dependencies = [ "anyhow", "bincode", - "cfg-if", + "cfg-if 1.0.0", "indexmap 1.9.3", "libc", "log", @@ -6322,7 +6433,7 @@ version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d3b9daa7c14cd4fa3edbf69de994408d5f4b7b0959ac13fa69d465f6597f810d" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", ] [[package]] @@ -6353,7 +6464,7 @@ dependencies = [ "addr2line 0.19.0", "anyhow", "bincode", - "cfg-if", + "cfg-if 1.0.0", "cpp_demangle", "gimli 0.27.3", "log", @@ -6382,7 +6493,7 @@ version = "8.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "aecae978b13f7f67efb23bd827373ace4578f2137ec110bbf6a4a7cde4121bbd" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "libc", "windows-sys 0.45.0", ] @@ -6395,7 +6506,7 @@ checksum = "658cf6f325232b6760e202e5255d823da5e348fdea827eff0a2a22319000b441" dependencies = [ "anyhow", "cc", - "cfg-if", + "cfg-if 1.0.0", "indexmap 1.9.3", "libc", "log", @@ -6702,7 +6813,7 @@ version = "0.50.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "524e57b2c537c0f9b1e69f1965311ec12182b4122e45035b1508cd24d2adadb1" dependencies = [ - "cfg-if", + "cfg-if 1.0.0", "windows-sys 0.48.0", ] diff --git a/Cargo.toml b/Cargo.toml index e1f33e9..5430a2c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -46,8 +46,9 @@ inquire = "^0.6.2" directories = "^5.0.1" comfy-table = "^7.1.1" -# ipfs RPC API +# IPFS (RPC API and CID) ipfs-api-backend-hyper = { version = "0.6", features = ["with-hyper-rustls"] } +cid = "0.7.0" # crypto scrypt = { version = "^0.11", default-features = false } # for old-style key generation nacl = { version = "^0.5.3" } # for old-style key generation diff --git a/src/commands/account.rs b/src/commands/account.rs index 0f0f138..6241c43 100644 --- a/src/commands/account.rs +++ b/src/commands/account.rs @@ -21,6 +21,9 @@ pub enum Subcommand { /// Add a transaction comment #[clap(short = 'c', long = "comment")] comment: Option<String>, + /// Make transaction comment offchain instead of onchain (requires IPFS) + #[clap(short = 'x', long = "offchain")] + offchain: bool, }, /// Transfer the same amount for each space-separated address. /// If an address appears mutiple times, it will get multiple times the same amount @@ -45,6 +48,7 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE allow_death, is_ud, comment, + offchain, } => { if let Some(comment) = comment { if is_ud || allow_death { @@ -52,7 +56,11 @@ pub async fn handle_command(data: Data, command: Subcommand) -> Result<(), GcliE "ud or allow death commented transfers are not handled yet".to_string(), )); } - commands::commented::transfer(&data, amount, dest, comment).await?; + if offchain { + commands::commented::transfer_offchain(&data, amount, dest, comment).await?; + } else { + commands::commented::transfer_onchain(&data, amount, dest, comment).await?; + } } else { commands::transfer::transfer(&data, amount, dest, allow_death, is_ud).await?; } diff --git a/src/commands/commented.rs b/src/commands/commented.rs index 6d24e2b..062a6d9 100644 --- a/src/commands/commented.rs +++ b/src/commands/commented.rs @@ -1,4 +1,5 @@ use crate::*; +use cid::Cid; #[cfg(any(feature = "dev", feature = "gdev"))] // find how to get runtime calls type Call = runtime::runtime_types::gdev_runtime::RuntimeCall; @@ -6,11 +7,37 @@ type BalancesCall = runtime::runtime_types::pallet_balances::pallet::Call; type SystemCall = runtime::runtime_types::frame_system::pallet::Call; /// commented balance transfer -pub async fn transfer( +pub async fn transfer_onchain( data: &Data, amount: u64, dest: AccountId, comment: String, +) -> Result<(), subxt::Error> { + transfer(data, amount, dest, comment.as_bytes().to_vec()).await +} + +/// commented balance transfer, offchain version uploading to local ipfs node +pub async fn transfer_offchain( + data: &Data, + amount: u64, + dest: AccountId, + comment: String, +) -> Result<(), GcliError> { + let response = commands::ipfs::upload_string(&comment) + .await + .map_err(|e| anyhow::anyhow!(e))?; + let cid = Cid::from_str(&response.hash).map_err(|e| anyhow!(e))?; + transfer(data, amount, dest, cid.to_bytes().to_vec()) + .await + .map_err(|e| e.into()) +} + +/// wrap a transfer call and a remark and submit them +async fn transfer( + data: &Data, + amount: u64, + dest: AccountId, + comment: Vec<u8>, ) -> Result<(), subxt::Error> { // build transfer call let transfer_call = Call::Balances(BalancesCall::transfer_keep_alive { @@ -18,9 +45,7 @@ pub async fn transfer( value: amount, }); // build comment call - let comment_call = Call::System(SystemCall::remark_with_event { - remark: comment.as_bytes().to_vec(), - }); + let comment_call = Call::System(SystemCall::remark_with_event { remark: comment }); // wrap these calls in a batch call submit_call_and_look_event::< diff --git a/src/commands/ipfs.rs b/src/commands/ipfs.rs index e00b7d5..abd4e4b 100644 --- a/src/commands/ipfs.rs +++ b/src/commands/ipfs.rs @@ -10,12 +10,19 @@ async fn get_version() -> Result<VersionResponse, ipfs_api_backend_hyper::Error> } /// upload string as text file -async fn upload_string(txt: &str) -> Result<AddResponse, ipfs_api_backend_hyper::Error> { +pub async fn upload_string(txt: &str) -> Result<AddResponse, ipfs_api_backend_hyper::Error> { let client = IpfsClient::default(); let data = Cursor::new(txt.to_owned()); client.add(data).await } +// /// upload bytes +// pub async fn upload_bytes(bytes: &[u8]) -> Result<AddResponse, ipfs_api_backend_hyper::Error> { +// let client = IpfsClient::default(); +// let data = Cursor::new(bytes.to_owned()); +// client.add(data).await +// } + /// define ipfs subcommands #[derive(Clone, Default, Debug, clap::Parser)] pub enum Subcommand { -- GitLab