Crates.io | dwn |
lib.rs | dwn |
version | 0.0.10 |
source | src |
created_at | 2023-12-16 02:53:44.471749 |
updated_at | 2024-09-25 20:14:59.250854 |
description | Rust implementation of a Decentralized Web Node. |
homepage | |
repository | https://github.com/unavi-xyz/dwn |
max_upload_size | |
id | 1071523 |
size | 195,589 |
Rust implementation of a Decentralized Web Node.
use std::sync::Arc;
use dwn::{actor::Actor, message::Data, store::SurrealStore, DWN};
use surrealdb::{Surreal, engine::local::Mem};
#[tokio::main]
async fn main() {
// Create a DWN, using in-memory SurrealDB for storage.
let db = Surreal::new::<Mem>(()).await.unwrap();
let store = SurrealStore::new(db).await.unwrap();
let dwn = Arc::new(DWN::from(store));
// Create an actor to send messages.
// Here we generate a new `did:key` for the actor's identity,
// but you could use any DID method.
let actor = Actor::new_did_key(dwn).unwrap();
// Create a new record.
let data = "Hello, world!".bytes().collect::<Vec<_>>();
let create = actor
.create_record()
.data(data.clone())
.data_format("text/plain".to_string())
.process()
.await
.unwrap();
assert_eq!(create.reply.status.code, 200);
// Read the record.
let read = actor
.read_record(create.record_id)
.process()
.await
.unwrap();
assert_eq!(read.status.code, 200);
assert_eq!(read.record.data, Some(Data::new_base64(&data)));
}