forked from rs-ipfs/rust-ipfs
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
838 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
[package] | ||
name = "wasm-example" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[lib] | ||
crate-type = ["cdylib", "rlib"] | ||
|
||
[dependencies] | ||
rust-ipfs = { path = "../.." } | ||
futures = "0.3" | ||
anyhow = "1.0.81" | ||
wasm-bindgen = "0.2.90" | ||
wasm-bindgen-futures = "0.4.42" | ||
web-sys = { version = "0.3", features = ['Document', 'Element', 'HtmlElement', 'Node', 'Response', 'Window'] } | ||
js-sys = "0.3.69" | ||
libipld.workspace = true |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use libipld::ipld; | ||
use rust_ipfs::UninitializedIpfsNoop as UninitializedIpfs; | ||
use rust_ipfs::{Multiaddr, Protocol}; | ||
use wasm_bindgen::prelude::*; | ||
use web_sys::{Document, HtmlElement}; | ||
|
||
#[wasm_bindgen] | ||
pub async fn run() -> Result<(), JsError> { | ||
let body = Body::from_current_window()?; | ||
body.append_p("Ipfs block exchange test")?; | ||
|
||
let node_a = UninitializedIpfs::new() | ||
.with_default() | ||
.add_listening_addr(Multiaddr::empty().with(Protocol::Memory(0))) | ||
.start() | ||
.await | ||
.unwrap(); | ||
let node_b = UninitializedIpfs::new() | ||
.with_default() | ||
.add_listening_addr(Multiaddr::empty().with(Protocol::Memory(0))) | ||
.start() | ||
.await | ||
.unwrap(); | ||
|
||
let peer_id = node_a.keypair().public().to_peer_id(); | ||
let peer_id_b = node_b.keypair().public().to_peer_id(); | ||
|
||
body.append_p(&format!("Our Node (A): {peer_id}"))?; | ||
body.append_p(&format!("Our Node (B): {peer_id_b}"))?; | ||
|
||
let addrs = node_a.listening_addresses().await.unwrap(); | ||
|
||
for addr in addrs { | ||
node_b.add_peer(peer_id, addr).await.unwrap(); | ||
} | ||
|
||
node_b.connect(peer_id).await.unwrap(); | ||
|
||
let block_a = ipld!({ | ||
"name": "alice", | ||
"age": 99, | ||
}); | ||
|
||
let cid = node_a.put_dag(block_a.clone()).await.unwrap(); | ||
|
||
let block_b = node_b.get_dag(cid).await.unwrap(); | ||
|
||
assert_eq!(block_b, block_a); | ||
|
||
body.append_p(&format!("Block from node A: {block_b:?}"))?; | ||
|
||
node_a.exit_daemon().await; | ||
node_b.exit_daemon().await; | ||
Ok(()) | ||
} | ||
|
||
/// Borrowed from libp2p | ||
struct Body { | ||
body: HtmlElement, | ||
document: Document, | ||
} | ||
|
||
impl Body { | ||
fn from_current_window() -> Result<Self, JsError> { | ||
// Use `web_sys`'s global `window` function to get a handle on the global | ||
// window object. | ||
let document = web_sys::window() | ||
.ok_or(js_error("no global `window` exists"))? | ||
.document() | ||
.ok_or(js_error("should have a document on window"))?; | ||
let body = document | ||
.body() | ||
.ok_or(js_error("document should have a body"))?; | ||
|
||
Ok(Self { body, document }) | ||
} | ||
|
||
fn append_p(&self, msg: &str) -> Result<(), JsError> { | ||
let val = self | ||
.document | ||
.create_element("p") | ||
.map_err(|_| js_error("failed to create <p>"))?; | ||
val.set_text_content(Some(msg)); | ||
self.body | ||
.append_child(&val) | ||
.map_err(|_| js_error("failed to append <p>"))?; | ||
|
||
Ok(()) | ||
} | ||
} | ||
|
||
fn js_error(msg: &str) -> JsError { | ||
std::io::Error::new(std::io::ErrorKind::Other, msg).into() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<html> | ||
<body> | ||
<script type="module" defer> | ||
import init, { run } from "./wasm_example.js" | ||
await init(); | ||
run(); | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
I noticed a small typo here:
httops
->https
😁