mirror of
https://github.com/fluencelabs/aqua-ipfs
synced 2025-03-15 13:10:49 +00:00
Add new methods, validate input multiaddresses (#4)
This commit is contained in:
parent
2957e026eb
commit
7a05a39b5a
2
.gitignore
vendored
2
.gitignore
vendored
@ -8,7 +8,7 @@
|
||||
# Added by cargo
|
||||
.idea
|
||||
|
||||
artifacts/*.wasm
|
||||
**/artifacts/*.wasm
|
||||
|
||||
**/node_modules
|
||||
**/dist
|
||||
|
@ -1,11 +1,30 @@
|
||||
data IpfsGetFromResult:
|
||||
success: bool
|
||||
error: string
|
||||
path: string
|
||||
|
||||
data IpfsMultiaddrResult:
|
||||
success: bool
|
||||
error: string
|
||||
multiaddr: string
|
||||
|
||||
data IpfsPutResult:
|
||||
success: bool
|
||||
error: string
|
||||
hash: string
|
||||
|
||||
data IpfsResult:
|
||||
success: bool
|
||||
result: string
|
||||
error: string
|
||||
|
||||
service Ipfs("ipfs-adapter"):
|
||||
get_from(hash: string, multiaddr: string) -> IpfsResult
|
||||
get_multiaddr() -> IpfsResult
|
||||
put(file_path: string) -> IpfsResult
|
||||
set_multiaddr(multiaddr: string) -> IpfsResult
|
||||
get_external_api_multiaddr() -> IpfsMultiaddrResult
|
||||
get_external_swarm_multiaddr() -> IpfsMultiaddrResult
|
||||
get_from(hash: string, swarm_multiaddr: string) -> IpfsGetFromResult
|
||||
get_local_api_multiaddr() -> IpfsMultiaddrResult
|
||||
invoke() -> string
|
||||
put(file_path: string) -> IpfsPutResult
|
||||
set_external_api_multiaddr(multiaddr: string) -> IpfsResult
|
||||
set_external_swarm_multiaddr(multiaddr: string) -> IpfsResult
|
||||
set_local_api_multiaddr(multiaddr: string) -> IpfsResult
|
||||
set_timeout(timeout_sec: u64)
|
||||
|
Binary file not shown.
Binary file not shown.
@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env bash
|
||||
set -o errexit -o nounset -o pipefail
|
||||
|
||||
# set current working directory to script directory to run script from everywhere
|
||||
cd "$(dirname "$0")"
|
||||
|
||||
# This script builds all subprojects and puts all created Wasm modules in one dir
|
||||
cd effector
|
||||
cargo update --aggressive
|
||||
@ -15,3 +18,4 @@ mkdir -p artifacts
|
||||
rm -f artifacts/*.wasm
|
||||
cp effector/target/wasm32-wasi/release/ipfs_effector.wasm artifacts/
|
||||
cp pure/target/wasm32-wasi/release/ipfs_pure.wasm artifacts/
|
||||
marine aqua artifacts/ipfs_pure.wasm -s Ipfs -i ipfs-adapter > ../aqua/ipfs.aqua
|
||||
|
265
service/effector/Cargo.lock
generated
265
service/effector/Cargo.lock
generated
@ -2,12 +2,30 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bs58"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
@ -27,6 +45,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "data-encoding"
|
||||
version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57"
|
||||
|
||||
[[package]]
|
||||
name = "eyre"
|
||||
version = "0.6.5"
|
||||
@ -37,6 +61,44 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-fork-parity-multiaddr"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06228daafbf129ef49806f15779857012c01d49658930d086498e78f12191da7"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"bs58",
|
||||
"byteorder",
|
||||
"data-encoding",
|
||||
"multihash",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
"static_assertions",
|
||||
"unsigned-varint 0.7.0",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
|
||||
dependencies = [
|
||||
"matches",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.3"
|
||||
@ -48,6 +110,17 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
|
||||
dependencies = [
|
||||
"matches",
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
@ -59,6 +132,7 @@ name = "ipfs-effector"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"eyre",
|
||||
"fluence-fork-parity-multiaddr",
|
||||
"log",
|
||||
"marine-rs-sdk",
|
||||
"serde_json",
|
||||
@ -73,9 +147,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.97"
|
||||
version = "0.2.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
|
||||
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
@ -142,6 +216,37 @@ dependencies = [
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||
|
||||
[[package]]
|
||||
name = "multihash"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dac63698b887d2d929306ea48b63760431ff8a24fac40ddb22f9c7f49fb7cab"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"multihash-derive",
|
||||
"unsigned-varint 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multihash-derive"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "424f6e86263cd5294cbd7f1e95746b95aca0e0d66bff31e5a40d6baa87b4aa99"
|
||||
dependencies = [
|
||||
"proc-macro-crate",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
@ -167,6 +272,46 @@ version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41fdbd1df62156fbc5945f4762632564d7d038153091c3fcf1067f6aef7cff92"
|
||||
dependencies = [
|
||||
"thiserror",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.27"
|
||||
@ -222,6 +367,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.73"
|
||||
@ -233,6 +384,38 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.44"
|
||||
@ -244,6 +427,36 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342"
|
||||
dependencies = [
|
||||
"tinyvec_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec_macros"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a31142970826733df8241ef35dc040ef98c679ab14d7c3e54d827099b3acecaa"
|
||||
dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
|
||||
|
||||
[[package]]
|
||||
name = "types"
|
||||
version = "0.1.0"
|
||||
@ -252,12 +465,54 @@ dependencies = [
|
||||
"marine-rs-sdk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0"
|
||||
dependencies = [
|
||||
"matches",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
|
||||
dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "unsigned-varint"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fdeedbf205afadfe39ae559b75c3240f24e257d0ca27e85f85cb82aa19ac35"
|
||||
|
||||
[[package]]
|
||||
name = "unsigned-varint"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f8d425fafb8cd76bc3f22aace4af471d3156301d7508f2107e98fbeae10bc7f"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"matches",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "0.8.2"
|
||||
@ -267,6 +522,12 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
|
@ -11,6 +11,7 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
|
||||
multiaddr = { version = "0.11.2", package = "fluence-fork-parity-multiaddr" }
|
||||
log = "0.4.14"
|
||||
eyre = "0.6.5"
|
||||
serde_json = "1.0.64"
|
||||
|
@ -17,14 +17,16 @@
|
||||
#![allow(improper_ctypes)]
|
||||
#![feature(try_blocks)]
|
||||
|
||||
use types::IpfsResult;
|
||||
use types::{IpfsResult, IpfsPutResult, IpfsGetPeerIdResult};
|
||||
|
||||
use marine_rs_sdk::marine;
|
||||
use marine_rs_sdk::module_manifest;
|
||||
use marine_rs_sdk::MountedBinaryResult;
|
||||
use marine_rs_sdk::WasmLoggerBuilder;
|
||||
|
||||
use eyre::Result;
|
||||
use eyre::{Result, WrapErr};
|
||||
use multiaddr::Multiaddr;
|
||||
use std::str::FromStr;
|
||||
|
||||
module_manifest!();
|
||||
|
||||
@ -35,6 +37,16 @@ fn unwrap_mounted_binary_result(result: MountedBinaryResult) -> Result<String> {
|
||||
#[inline]
|
||||
fn get_timeout_string(timeout: u64) -> String { format!("{}s", timeout) }
|
||||
|
||||
fn make_cmd_args(args: Vec<String>, local_multiaddr: String, timeout_sec: u64) -> Vec<String> {
|
||||
args.into_iter().chain(
|
||||
vec![
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
String::from("--api"),
|
||||
local_multiaddr
|
||||
]).collect()
|
||||
}
|
||||
|
||||
pub fn main() {
|
||||
WasmLoggerBuilder::new()
|
||||
.with_log_level(log::LevelFilter::Info)
|
||||
@ -43,36 +55,33 @@ pub fn main() {
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn connect(multiaddr: String, timeout_sec: u64) -> IpfsResult {
|
||||
pub fn connect(multiaddr: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult {
|
||||
log::info!("connect called with multiaddr {}", multiaddr);
|
||||
|
||||
let cmd = vec![
|
||||
let args = vec![
|
||||
String::from("swarm"),
|
||||
String::from("connect"),
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
multiaddr
|
||||
];
|
||||
multiaddr];
|
||||
let cmd = make_cmd_args(args, local_multiaddr, timeout_sec);
|
||||
|
||||
unwrap_mounted_binary_result(ipfs(cmd)).map(|_| ()).into()
|
||||
}
|
||||
|
||||
/// Put file from specified path to IPFS and return its hash.
|
||||
#[marine]
|
||||
pub fn put(file_path: String, timeout_sec: u64) -> IpfsResult {
|
||||
pub fn put(file_path: String, local_multiaddr: String, timeout_sec: u64) -> IpfsPutResult {
|
||||
log::info!("put called with file path {}", file_path);
|
||||
|
||||
if !std::path::Path::new(&file_path).exists() {
|
||||
return IpfsResult { success: false, result: format!("path {} doesn't exist", file_path) }
|
||||
return IpfsPutResult { success: false, error: format!("path {} doesn't exist", file_path), hash: "".to_string() };
|
||||
}
|
||||
|
||||
let cmd = vec![
|
||||
let args = vec![
|
||||
String::from("add"),
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
String::from("-Q"),
|
||||
inject_vault_host_path(file_path)
|
||||
];
|
||||
let cmd = make_cmd_args(args, local_multiaddr, timeout_sec);
|
||||
|
||||
log::info!("ipfs put args {:?}", cmd);
|
||||
|
||||
@ -81,17 +90,16 @@ pub fn put(file_path: String, timeout_sec: u64) -> IpfsResult {
|
||||
|
||||
/// Get file by provided hash from IPFS, saves it to a temporary file and returns a path to it.
|
||||
#[marine]
|
||||
pub fn get(hash: String, file_path: String, timeout_sec: u64) -> IpfsResult {
|
||||
pub fn get(hash: String, file_path: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult {
|
||||
log::info!("get called with hash {}", hash);
|
||||
|
||||
let cmd = vec![
|
||||
let args = vec![
|
||||
String::from("get"),
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
String::from("-o"),
|
||||
inject_vault_host_path(file_path),
|
||||
hash,
|
||||
];
|
||||
let cmd = make_cmd_args(args, local_multiaddr, timeout_sec);
|
||||
|
||||
log::info!("ipfs get args {:?}", cmd);
|
||||
|
||||
@ -101,13 +109,9 @@ pub fn get(hash: String, file_path: String, timeout_sec: u64) -> IpfsResult {
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn get_peer_id(timeout_sec: u64) -> IpfsResult {
|
||||
pub fn get_peer_id(local_multiaddr: String, timeout_sec: u64) -> IpfsGetPeerIdResult {
|
||||
let result: Result<String> = try {
|
||||
let cmd = vec![
|
||||
String::from("id"),
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
];
|
||||
let cmd = make_cmd_args(vec![String::from("id")], local_multiaddr, timeout_sec);
|
||||
|
||||
let result: serde_json::Value = serde_json::from_str(&unwrap_mounted_binary_result(ipfs(cmd))?)?;
|
||||
result.get("ID").ok_or(eyre::eyre!("ID field not found in response"))?.as_str().ok_or(eyre::eyre!("ID value is not string"))?.to_string()
|
||||
@ -117,18 +121,21 @@ pub fn get_peer_id(timeout_sec: u64) -> IpfsResult {
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_external_multiaddr(multiaddr: String, timeout_sec: u64) -> IpfsResult {
|
||||
|
||||
let cmd = vec![
|
||||
pub fn set_external_api_multiaddr(multiaddr: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult {
|
||||
let result: Result<()> = try {
|
||||
let multiaddr = Multiaddr::from_str(&multiaddr).wrap_err(format!("invalid multiaddr {}", multiaddr))?;
|
||||
let args = vec![
|
||||
String::from("config"),
|
||||
String::from("--timeout"),
|
||||
get_timeout_string(timeout_sec),
|
||||
String::from("Addresses.Announce"),
|
||||
format!("[\"{}\"]", multiaddr),
|
||||
format!(r#"["{}"]"#, multiaddr.to_string()),
|
||||
String::from("--json"),
|
||||
];
|
||||
let cmd = make_cmd_args(args, local_multiaddr, timeout_sec);
|
||||
|
||||
unwrap_mounted_binary_result(ipfs(cmd)).map(|_| ()).into()
|
||||
unwrap_mounted_binary_result(ipfs(cmd)).map(|_| ())?
|
||||
};
|
||||
|
||||
result.into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
@ -147,41 +154,3 @@ fn inject_vault_host_path(path: String) -> String {
|
||||
path
|
||||
}
|
||||
}
|
||||
|
||||
fn to_full_path<S>(cmd: S) -> String
|
||||
where
|
||||
S: Into<String>,
|
||||
{
|
||||
use std::path::Path;
|
||||
use std::path::Component;
|
||||
|
||||
let cmd = cmd.into();
|
||||
let path = Path::new(&cmd);
|
||||
|
||||
let mut components = path.components();
|
||||
let is_absolute = components.next() == Some(Component::RootDir);
|
||||
|
||||
if !is_absolute {
|
||||
return cmd;
|
||||
}
|
||||
|
||||
let parent = match components.next() {
|
||||
Some(Component::Normal(path)) => path.to_str().unwrap(),
|
||||
_ => return cmd,
|
||||
};
|
||||
|
||||
match std::env::var(parent) {
|
||||
Ok(to_dir) => {
|
||||
let mut full_path = std::path::PathBuf::from(to_dir);
|
||||
|
||||
// TODO: optimize this
|
||||
#[allow(clippy::while_let_on_iterator)]
|
||||
while let Some(component) = components.next() {
|
||||
full_path.push(component);
|
||||
}
|
||||
full_path.to_string_lossy().into_owned()
|
||||
}
|
||||
Err(_) => cmd,
|
||||
}
|
||||
}
|
||||
|
||||
|
257
service/pure/Cargo.lock
generated
257
service/pure/Cargo.lock
generated
@ -2,12 +2,30 @@
|
||||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "arrayref"
|
||||
version = "0.3.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a4c527152e37cf757a3f78aae5a06fbeefdb07ccc535c980a3208ee3060dd544"
|
||||
|
||||
[[package]]
|
||||
name = "autocfg"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
|
||||
|
||||
[[package]]
|
||||
name = "bs58"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "771fe0050b883fcc3ea2359b1a96bcfbc090b7116eae7c3c512c7a083fdf23d3"
|
||||
|
||||
[[package]]
|
||||
name = "byteorder"
|
||||
version = "1.4.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
@ -27,6 +45,12 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "data-encoding"
|
||||
version = "2.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3ee2393c4a91429dffb4bedf19f4d6abf27d8a732c8ce4980305d782e5426d57"
|
||||
|
||||
[[package]]
|
||||
name = "eyre"
|
||||
version = "0.6.5"
|
||||
@ -37,6 +61,44 @@ dependencies = [
|
||||
"once_cell",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "fluence-fork-parity-multiaddr"
|
||||
version = "0.11.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "06228daafbf129ef49806f15779857012c01d49658930d086498e78f12191da7"
|
||||
dependencies = [
|
||||
"arrayref",
|
||||
"bs58",
|
||||
"byteorder",
|
||||
"data-encoding",
|
||||
"multihash",
|
||||
"percent-encoding",
|
||||
"serde",
|
||||
"static_assertions",
|
||||
"unsigned-varint 0.7.0",
|
||||
"url",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "form_urlencoded"
|
||||
version = "1.0.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fc25a87fa4fd2094bffb06925852034d90a17f0d1e05197d4956d3555752191"
|
||||
dependencies = [
|
||||
"matches",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "generic-array"
|
||||
version = "0.14.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "501466ecc8a30d1d3b7fc9229b122b2ce8ed6e9d9223f1138d4babb253e51817"
|
||||
dependencies = [
|
||||
"typenum",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "getrandom"
|
||||
version = "0.2.3"
|
||||
@ -48,6 +110,17 @@ dependencies = [
|
||||
"wasi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "idna"
|
||||
version = "0.2.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "418a0a6fab821475f634efe3ccc45c013f742efe03d853e8d3355d5cb850ecf8"
|
||||
dependencies = [
|
||||
"matches",
|
||||
"unicode-bidi",
|
||||
"unicode-normalization",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "indenter"
|
||||
version = "0.3.3"
|
||||
@ -58,7 +131,9 @@ checksum = "ce23b50ad8242c51a442f3ff322d56b02f08852c77e4c0b4d3fd684abc89c683"
|
||||
name = "ipfs-pure"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"bs58",
|
||||
"eyre",
|
||||
"fluence-fork-parity-multiaddr",
|
||||
"log",
|
||||
"marine-rs-sdk",
|
||||
"serde",
|
||||
@ -74,9 +149,9 @@ checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
|
||||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.97"
|
||||
version = "0.2.98"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "12b8adadd720df158f4d70dfe7ccc6adb0472d7c55ca83445f6a5ab3e36f8fb6"
|
||||
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
|
||||
|
||||
[[package]]
|
||||
name = "log"
|
||||
@ -143,6 +218,37 @@ dependencies = [
|
||||
"quote",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "matches"
|
||||
version = "0.1.8"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "7ffc5c5338469d4d3ea17d269fa8ea3512ad247247c30bd2df69e68309ed0a08"
|
||||
|
||||
[[package]]
|
||||
name = "multihash"
|
||||
version = "0.13.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4dac63698b887d2d929306ea48b63760431ff8a24fac40ddb22f9c7f49fb7cab"
|
||||
dependencies = [
|
||||
"generic-array",
|
||||
"multihash-derive",
|
||||
"unsigned-varint 0.5.1",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "multihash-derive"
|
||||
version = "0.7.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "424f6e86263cd5294cbd7f1e95746b95aca0e0d66bff31e5a40d6baa87b4aa99"
|
||||
dependencies = [
|
||||
"proc-macro-crate",
|
||||
"proc-macro-error",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"synstructure",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.44"
|
||||
@ -168,6 +274,46 @@ version = "1.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "692fcb63b64b1758029e0a96ee63e049ce8c5948587f2f7208df04625e5f6b56"
|
||||
|
||||
[[package]]
|
||||
name = "percent-encoding"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d4fd5641d01c8f18a23da7b6fe29298ff4b55afcccdf78973b24cf3175fee32e"
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-crate"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "41fdbd1df62156fbc5945f4762632564d7d038153091c3fcf1067f6aef7cff92"
|
||||
dependencies = [
|
||||
"thiserror",
|
||||
"toml",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "da25490ff9892aab3fcf7c36f08cfb902dd3e71ca0f9f9517bea02a73a5ce38c"
|
||||
dependencies = [
|
||||
"proc-macro-error-attr",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro-error-attr"
|
||||
version = "1.0.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a1be40180e52ecc98ad80b184934baf3d0d29f979574e439af5a55274b35f869"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"version_check",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.27"
|
||||
@ -223,6 +369,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "static_assertions"
|
||||
version = "1.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a2eb9349b6444b326872e140eb1cf5e7c522154d69e7a0ffb0fb81c06b37543f"
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "1.0.73"
|
||||
@ -234,6 +386,38 @@ dependencies = [
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "synstructure"
|
||||
version = "0.12.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b834f2d66f734cb897113e34aaff2f1ab4719ca946f9a7358dba8f8064148701"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
"unicode-xid",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "93119e4feac1cbe6c798c34d3a53ea0026b0b1de6a120deef895137c0529bfe2"
|
||||
dependencies = [
|
||||
"thiserror-impl",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "thiserror-impl"
|
||||
version = "1.0.26"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "060d69a0afe7796bf42e9e2ff91f5ee691fb15c53d38b4b62a9a53eb23164745"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.1.44"
|
||||
@ -245,6 +429,21 @@ dependencies = [
|
||||
"winapi",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec"
|
||||
version = "1.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5b5220f05bb7de7f3f53c7c065e1199b3172696fe2db9f9c4d8ad9b4ee74c342"
|
||||
dependencies = [
|
||||
"tinyvec_macros",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tinyvec_macros"
|
||||
version = "0.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
||||
|
||||
[[package]]
|
||||
name = "toml"
|
||||
version = "0.5.8"
|
||||
@ -254,6 +453,12 @@ dependencies = [
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "typenum"
|
||||
version = "1.13.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "879f6906492a7cd215bfa4cf595b600146ccfac0c79bcbd1f3000162af5e8b06"
|
||||
|
||||
[[package]]
|
||||
name = "types"
|
||||
version = "0.1.0"
|
||||
@ -262,12 +467,54 @@ dependencies = [
|
||||
"marine-rs-sdk",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-bidi"
|
||||
version = "0.3.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "eeb8be209bb1c96b7c177c7420d26e04eccacb0eeae6b980e35fcb74678107e0"
|
||||
dependencies = [
|
||||
"matches",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-normalization"
|
||||
version = "0.1.19"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d54590932941a9e9266f0832deed84ebe1bf2e4c9e4a3554d393d18f5e854bf9"
|
||||
dependencies = [
|
||||
"tinyvec",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
|
||||
|
||||
[[package]]
|
||||
name = "unsigned-varint"
|
||||
version = "0.5.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f7fdeedbf205afadfe39ae559b75c3240f24e257d0ca27e85f85cb82aa19ac35"
|
||||
|
||||
[[package]]
|
||||
name = "unsigned-varint"
|
||||
version = "0.7.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5f8d425fafb8cd76bc3f22aace4af471d3156301d7508f2107e98fbeae10bc7f"
|
||||
|
||||
[[package]]
|
||||
name = "url"
|
||||
version = "2.2.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a507c383b2d33b5fc35d1861e77e6b383d158b2da5e14fe51b83dfedf6fd578c"
|
||||
dependencies = [
|
||||
"form_urlencoded",
|
||||
"idna",
|
||||
"matches",
|
||||
"percent-encoding",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "uuid"
|
||||
version = "0.8.2"
|
||||
@ -277,6 +524,12 @@ dependencies = [
|
||||
"getrandom",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "version_check"
|
||||
version = "0.9.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
|
||||
|
||||
[[package]]
|
||||
name = "wasi"
|
||||
version = "0.10.0+wasi-snapshot-preview1"
|
||||
|
@ -11,9 +11,11 @@ path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
marine-rs-sdk = { version = "0.6.10", features = ["logger"] }
|
||||
multiaddr = { version = "0.11.2", package = "fluence-fork-parity-multiaddr" }
|
||||
log = "0.4.14"
|
||||
eyre = "0.6.5"
|
||||
toml = "0.5.8"
|
||||
serde = "1.0.118"
|
||||
bs58 = "0.4.0"
|
||||
|
||||
types = { path = "../types" }
|
||||
|
@ -15,8 +15,9 @@
|
||||
*/
|
||||
|
||||
#![allow(improper_ctypes)]
|
||||
#![feature(try_blocks)]
|
||||
|
||||
use types::IpfsResult;
|
||||
use types::{IpfsResult, IpfsGetFromResult, IpfsPutResult, IpfsGetPeerIdResult, IpfsMultiaddrResult};
|
||||
|
||||
use marine_rs_sdk::marine;
|
||||
use marine_rs_sdk::module_manifest;
|
||||
@ -24,25 +25,31 @@ use marine_rs_sdk::WasmLoggerBuilder;
|
||||
|
||||
use std::fs;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use multiaddr::{Multiaddr, Protocol, multihash::Multihash};
|
||||
use std::str::FromStr;
|
||||
use eyre::WrapErr;
|
||||
|
||||
const CONFIG_FILE_PATH: &str = "/tmp/multiaddr_config";
|
||||
const DEFAULT_TIMEOUT_SEC: u64 = 1u64;
|
||||
const DEFAULT_LOCAL_API_MULTIADDR: &str = "/ip4/127.0.0.1/tcp/5001";
|
||||
module_manifest!();
|
||||
|
||||
#[derive(Deserialize, Serialize)]
|
||||
pub struct Config {
|
||||
pub timeout: u64,
|
||||
pub multiaddr: Option<String>,
|
||||
pub external_api_multiaddr: Option<Multiaddr>,
|
||||
pub external_swarm_multiaddr: Option<Multiaddr>,
|
||||
pub local_api_multiaddr: Multiaddr,
|
||||
}
|
||||
|
||||
fn save_multiaddr(multiaddr: String) {
|
||||
fn save_external_api_multiaddr(multiaddr: Multiaddr) {
|
||||
let mut config = load_config();
|
||||
config.multiaddr = Some(multiaddr);
|
||||
config.external_api_multiaddr = Some(multiaddr);
|
||||
write_config(config);
|
||||
}
|
||||
|
||||
fn load_multiaddr() -> eyre::Result<String> {
|
||||
load_config().multiaddr.ok_or(eyre::eyre!("multiaddr is not set"))
|
||||
fn load_external_api_multiaddr() -> eyre::Result<Multiaddr> {
|
||||
load_config().external_api_multiaddr.ok_or(eyre::eyre!("multiaddr is not set"))
|
||||
}
|
||||
|
||||
pub fn write_config(config: Config) {
|
||||
@ -59,7 +66,9 @@ pub(crate) fn create_config() {
|
||||
if fs::metadata(CONFIG_FILE_PATH).is_err() {
|
||||
write_config(Config {
|
||||
timeout: DEFAULT_TIMEOUT_SEC,
|
||||
multiaddr: None,
|
||||
external_api_multiaddr: None,
|
||||
external_swarm_multiaddr: None,
|
||||
local_api_multiaddr: Multiaddr::from_str(DEFAULT_LOCAL_API_MULTIADDR).unwrap(),
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -78,62 +87,136 @@ pub fn invoke() -> String {
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn put(file_path: String) -> IpfsResult {
|
||||
pub fn put(file_path: String) -> IpfsPutResult {
|
||||
log::info!("put called with {:?}", file_path);
|
||||
let timeout = load_config().timeout;
|
||||
ipfs_put(file_path, timeout)
|
||||
ipfs_put(file_path, load_config().local_api_multiaddr.to_string(), timeout)
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn get_from(hash: String, multiaddr: String) -> IpfsResult {
|
||||
pub fn get_from(hash: String, swarm_multiaddr: String) -> IpfsGetFromResult {
|
||||
log::info!("get called with hash: {}", hash);
|
||||
let timeout = load_config().timeout;
|
||||
let config = load_config();
|
||||
let timeout = config.timeout;
|
||||
let local_maddr = config.local_api_multiaddr.to_string();
|
||||
|
||||
let particle_id = marine_rs_sdk::get_call_parameters().particle_id;
|
||||
let connect_result = ipfs_connect(multiaddr, timeout.clone());
|
||||
let connect_result = ipfs_connect(swarm_multiaddr, local_maddr.clone(), timeout.clone());
|
||||
|
||||
if !connect_result.success {
|
||||
return connect_result;
|
||||
return Err(eyre::eyre!(connect_result.error)).into();
|
||||
}
|
||||
|
||||
let particle_vault_path = format!("/tmp/vault/{}", particle_id);
|
||||
let file_path = format!("{}/{}", particle_vault_path, hash);
|
||||
let get_result = ipfs_get(hash, file_path.clone(), timeout);
|
||||
let path = format!("{}/{}", particle_vault_path, hash);
|
||||
let get_result = ipfs_get(hash, path.clone(), local_maddr, timeout);
|
||||
|
||||
return if get_result.success {
|
||||
IpfsResult { success: true, result: file_path }
|
||||
if get_result.success {
|
||||
Ok(path).into()
|
||||
} else {
|
||||
get_result
|
||||
Err(eyre::eyre!(get_result.error)).into()
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn get_multiaddr() -> IpfsResult {
|
||||
load_multiaddr().into()
|
||||
pub fn get_external_api_multiaddr() -> IpfsMultiaddrResult {
|
||||
load_external_api_multiaddr().map(|m| m.to_string()).into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_multiaddr(multiaddr: String) -> IpfsResult {
|
||||
pub fn set_external_api_multiaddr(multiaddr: String) -> IpfsResult {
|
||||
if load_external_api_multiaddr().is_ok() {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("external multiaddr can only be set once")).into();
|
||||
}
|
||||
|
||||
let call_parameters = marine_rs_sdk::get_call_parameters();
|
||||
if load_multiaddr().is_ok() || call_parameters.init_peer_id != call_parameters.service_creator_peer_id {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("only service creator can set multiaddr only once")).into();
|
||||
if call_parameters.init_peer_id != call_parameters.service_creator_peer_id {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("only service creator can set external multiaddr")).into();
|
||||
}
|
||||
|
||||
let timeout = load_config().timeout;
|
||||
let set_result = ipfs_set_external_multiaddr(multiaddr.clone(), timeout.clone());
|
||||
let config = load_config();
|
||||
let timeout = config.timeout;
|
||||
let local_maddr = config.local_api_multiaddr.to_string();
|
||||
let result: eyre::Result<()> = try {
|
||||
let mut multiaddr = Multiaddr::from_str(&multiaddr).wrap_err(format!("invalid multiaddr {}", multiaddr))?;
|
||||
let mut passed_peer_id = None;
|
||||
match multiaddr.iter().count() {
|
||||
3 => {
|
||||
passed_peer_id = multiaddr.pop();
|
||||
}
|
||||
2 => {}
|
||||
n => Err(eyre::eyre!("multiaddr should containt 2 or 3 components, {} given", n))?,
|
||||
}
|
||||
|
||||
let set_result = ipfs_set_external_api_multiaddr(multiaddr.to_string(), local_maddr.clone(), timeout.clone());
|
||||
if !set_result.success {
|
||||
return set_result;
|
||||
}
|
||||
|
||||
let peer_id_result = ipfs_get_peer_id(timeout);
|
||||
let peer_id_result = ipfs_get_peer_id(local_maddr, timeout);
|
||||
if !peer_id_result.success {
|
||||
return peer_id_result;
|
||||
Err(eyre::eyre!(peer_id_result.error.clone()))?;
|
||||
}
|
||||
|
||||
// trim trailing /
|
||||
let multiaddr = if multiaddr.ends_with("/") { multiaddr[..multiaddr.len() - 1].to_string() } else { multiaddr.clone() };
|
||||
save_multiaddr(format!("{}/{}", multiaddr, peer_id_result.result));
|
||||
Ok(()).into()
|
||||
let peer_id = Protocol::P2p(Multihash::from_bytes(&bs58::decode(peer_id_result.error).into_vec()?)?);
|
||||
if passed_peer_id.is_some() && passed_peer_id != Some(peer_id.clone()) {
|
||||
Err(eyre::eyre!("given peer id is different from node peer_id: given {}, actual {}", passed_peer_id.unwrap().to_string(), peer_id.to_string()))?;
|
||||
}
|
||||
|
||||
multiaddr.push(peer_id);
|
||||
save_external_api_multiaddr(multiaddr);
|
||||
()
|
||||
};
|
||||
|
||||
result.into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn get_local_api_multiaddr() -> IpfsMultiaddrResult {
|
||||
Ok(load_config().local_api_multiaddr.to_string()).into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_local_api_multiaddr(multiaddr: String) -> IpfsResult {
|
||||
let call_parameters = marine_rs_sdk::get_call_parameters();
|
||||
if call_parameters.init_peer_id != call_parameters.service_creator_peer_id {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("only service creator can set local multiaddr")).into();
|
||||
}
|
||||
|
||||
let result: eyre::Result<()> = try {
|
||||
let mut config = load_config();
|
||||
config.local_api_multiaddr = Multiaddr::from_str(&multiaddr).wrap_err(format!("invalid multiaddr {}", multiaddr))?;
|
||||
write_config(config);
|
||||
()
|
||||
};
|
||||
|
||||
result.into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn get_external_swarm_multiaddr() -> IpfsMultiaddrResult {
|
||||
load_config().external_swarm_multiaddr.ok_or(eyre::eyre!("multiaddr is not set")).map(|m| m.to_string()).into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub fn set_external_swarm_multiaddr(multiaddr: String) -> IpfsResult {
|
||||
if load_config().external_swarm_multiaddr.is_some() {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("external swarm multiaddr can only be set once")).into();
|
||||
}
|
||||
|
||||
let call_parameters = marine_rs_sdk::get_call_parameters();
|
||||
if call_parameters.init_peer_id != call_parameters.service_creator_peer_id {
|
||||
return eyre::Result::<()>::Err(eyre::eyre!("only service creator can set external swarm multiaddr")).into();
|
||||
}
|
||||
|
||||
let result: eyre::Result<()> = try {
|
||||
let mut config = load_config();
|
||||
config.external_swarm_multiaddr = Some(Multiaddr::from_str(&multiaddr).wrap_err(format!("invalid multiaddr {}", multiaddr))?);
|
||||
write_config(config);
|
||||
()
|
||||
};
|
||||
|
||||
result.into()
|
||||
}
|
||||
|
||||
#[marine]
|
||||
@ -148,20 +231,19 @@ pub fn set_timeout(timeout_sec: u64) {
|
||||
#[link(wasm_import_module = "ipfs_effector")]
|
||||
extern "C" {
|
||||
#[link_name = "connect"]
|
||||
pub fn ipfs_connect(multiaddr: String, timeout_sec: u64) -> IpfsResult;
|
||||
|
||||
pub fn ipfs_connect(external_multiaddr: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult;
|
||||
|
||||
/// Put provided file to ipfs, return ipfs hash of the file.
|
||||
#[link_name = "put"]
|
||||
pub fn ipfs_put(file_path: String, timeout_sec: u64) -> IpfsResult;
|
||||
pub fn ipfs_put(file_path: String, local_multiaddr: String, timeout_sec: u64) -> IpfsPutResult;
|
||||
|
||||
/// Get file from ipfs by hash.
|
||||
#[link_name = "get"]
|
||||
pub fn ipfs_get(hash: String, file_path: String, timeout_sec: u64) -> IpfsResult;
|
||||
pub fn ipfs_get(hash: String, file_path: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult;
|
||||
|
||||
#[link_name = "get_peer_id"]
|
||||
pub fn ipfs_get_peer_id(timeout_sec: u64) -> IpfsResult;
|
||||
pub fn ipfs_get_peer_id(local_multiaddr: String, timeout_sec: u64) -> IpfsGetPeerIdResult;
|
||||
|
||||
#[link_name = "set_external_multiaddr"]
|
||||
pub fn ipfs_set_external_multiaddr(multiaddr: String, timeout_sec: u64) -> IpfsResult;
|
||||
#[link_name = "set_external_api_multiaddr"]
|
||||
pub fn ipfs_set_external_api_multiaddr(external_multiaddr: String, local_multiaddr: String, timeout_sec: u64) -> IpfsResult;
|
||||
}
|
||||
|
@ -1,3 +1,3 @@
|
||||
mod results;
|
||||
|
||||
pub use results::IpfsResult;
|
||||
pub use results::{IpfsGetFromResult, IpfsResult, IpfsPutResult, IpfsGetPeerIdResult, IpfsMultiaddrResult};
|
||||
|
@ -20,23 +20,78 @@ use eyre::Result;
|
||||
#[marine]
|
||||
pub struct IpfsResult {
|
||||
pub success: bool,
|
||||
pub result: String,
|
||||
}
|
||||
|
||||
impl From<Result<String>> for IpfsResult {
|
||||
fn from(result: Result<String>) -> Self {
|
||||
match result {
|
||||
Ok(result) => Self { success: true, result },
|
||||
Err(err) => Self { success: false, result: err.to_string() }
|
||||
}
|
||||
}
|
||||
pub error: String,
|
||||
}
|
||||
|
||||
impl From<Result<()>> for IpfsResult {
|
||||
fn from(result: Result<()>) -> Self {
|
||||
match result {
|
||||
Ok(_) => Self { success: true, result: "".to_string() },
|
||||
Err(err) => Self { success: false, result: err.to_string() }
|
||||
Ok(_) => Self { success: true, error: "".to_string() },
|
||||
Err(err) => Self { success: false, error: err.to_string() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub struct IpfsGetFromResult {
|
||||
pub success: bool,
|
||||
pub error: String,
|
||||
pub path: String,
|
||||
}
|
||||
|
||||
impl From<Result<String>> for IpfsGetFromResult {
|
||||
fn from(result: Result<String>) -> Self {
|
||||
match result {
|
||||
Ok(path) => Self { success: true, error: "".to_string(), path },
|
||||
Err(err) => Self { success: false, error: err.to_string(), path: "".to_string() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub struct IpfsPutResult {
|
||||
pub success: bool,
|
||||
pub error: String,
|
||||
pub hash: String,
|
||||
}
|
||||
|
||||
impl From<Result<String>> for IpfsPutResult {
|
||||
fn from(result: Result<String>) -> Self {
|
||||
match result {
|
||||
Ok(hash) => Self { success: true, error: "".to_string(), hash },
|
||||
Err(err) => Self { success: false, error: err.to_string(), hash: "".to_string() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub struct IpfsGetPeerIdResult {
|
||||
pub success: bool,
|
||||
pub error: String,
|
||||
pub peer_id: String,
|
||||
}
|
||||
|
||||
impl From<Result<String>> for IpfsGetPeerIdResult {
|
||||
fn from(result: Result<String>) -> Self {
|
||||
match result {
|
||||
Ok(peer_id) => Self { success: true, error: "".to_string(), peer_id },
|
||||
Err(err) => Self { success: false, error: err.to_string(), peer_id: "".to_string() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[marine]
|
||||
pub struct IpfsMultiaddrResult {
|
||||
pub success: bool,
|
||||
pub error: String,
|
||||
pub multiaddr: String,
|
||||
}
|
||||
|
||||
impl From<Result<String>> for IpfsMultiaddrResult {
|
||||
fn from(result: Result<String>) -> Self {
|
||||
match result {
|
||||
Ok(multiaddr) => Self { success: true, error: "".to_string(), multiaddr },
|
||||
Err(err) => Self { success: false, error: err.to_string(), multiaddr: "".to_string() }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user