Created
March 12, 2018 16:51
-
-
Save AljoschaMeyer/7d0589dbf9fd1f6e9d5955f5fe84d708 to your computer and use it in GitHub Desktop.
Example of statically typed messages.
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
[package] | |
name = "demo-ssb-client" | |
version = "0.1.0" | |
authors = ["AljoschaMeyer <[email protected]>"] | |
[dependencies] | |
tokio = "0.1.3" | |
futures = "0.1.18" | |
sodiumoxide = "0.0.16" | |
secret_stream = "0.2.0" | |
ssb-keyfile = "0.1.2" | |
ssb-common = "0.3.0" | |
ssb-client = "0.3.1" | |
ssb-rpc = "0.3.1" | |
serde = "1.0.27" | |
serde_derive = "1.0.27" | |
serde_json = "1.0.9" |
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
#![feature(try_from)] | |
#![feature(ip_constructors)] | |
extern crate tokio; | |
extern crate futures; | |
extern crate sodiumoxide; | |
extern crate secret_stream; | |
extern crate ssb_keyfile; | |
extern crate ssb_common; | |
extern crate ssb_client; | |
extern crate ssb_rpc; | |
extern crate serde; | |
#[macro_use] | |
extern crate serde_derive; | |
extern crate serde_json; | |
use std::net::{SocketAddr, Ipv6Addr}; | |
use std::convert::TryInto; | |
use tokio::executor::current_thread; | |
use tokio::net::TcpStream; | |
use tokio::io::AsyncRead; | |
use futures::prelude::*; | |
use futures::future::ok; | |
use secret_stream::OwningClient; | |
use sodiumoxide::crypto::box_; | |
use ssb_keyfile::load_or_create_keys; | |
use ssb_common::{MAINNET_IDENTIFIER, DEFAULT_TCP_PORT}; | |
use ssb_common::links::MessageId; | |
use ssb_client::ssb; | |
use serde_json::Value; | |
use ssb_rpc::ssb::{Latest, LatestItem}; | |
#[derive(Debug, Deserialize)] | |
struct Post { | |
text: String, | |
// foo: u64, // uncomment this line and you get an error upon deserializing | |
// bar: Option<u64>, // uncomment this line and deserializing still works (note the `bar: None` in the console) | |
} | |
fn main() { | |
sodiumoxide::init(); | |
let (pk, sk) = load_or_create_keys().unwrap(); | |
let pk = pk.try_into().unwrap(); | |
let sk = sk.try_into().unwrap(); | |
let (ephemeral_pk, ephemeral_sk) = box_::gen_keypair(); | |
let addr = SocketAddr::new(Ipv6Addr::localhost().into(), DEFAULT_TCP_PORT); | |
let do_stuff = TcpStream::connect(&addr) | |
.and_then(move |tcp| { | |
OwningClient::new(tcp, | |
MAINNET_IDENTIFIER, | |
pk, | |
sk, | |
ephemeral_pk, | |
ephemeral_sk, | |
pk) | |
.map_err(|err| panic!("{:?}", err)) | |
}) | |
.map(|connection| { | |
let (read, write) = connection.unwrap().split(); | |
let (mut client, receive, _) = ssb(read, write); | |
current_thread::spawn(receive.map_err(|err| panic!("{:?}", err))); | |
let (req, res) = client | |
.get::<Post>("%Igm25FZEje8LeruZ0MnCajFz9e1LoMO3EHB5C0fRMmw=.sha256" | |
.parse::<MessageId>() | |
.unwrap()); | |
current_thread::spawn(req.map_err(|err| panic!("{:?}", err))); | |
current_thread::spawn(res.map(|msg| println!("{:?}", msg)) | |
.map_err(|err| panic!("{:?}", err))); | |
}) | |
.map_err(|err| panic!("{:?}", err)); | |
current_thread::run(|_| current_thread::spawn(do_stuff)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment