Sign Markdown saved to the folder with the configured ENS name and key (#2)

Co-authored-by: Danny Kim <temeddix@gmail.com>
Co-committed-by: Danny Kim <temeddix@gmail.com>
This commit was merged in pull request #2.
This commit is contained in:
Danny Kim
2026-09-25 17:02:52 +00:00
committed by temeddix
parent a4d1fae1d3
commit 5db8ed318a
6 changed files with 766 additions and 12 deletions
+45 -2
View File
@@ -3,6 +3,7 @@
mod dav;
mod sdk;
mod sign;
use sdk::host;
use sdk::{
@@ -10,6 +11,7 @@ use sdk::{
GuestReader, GuestWriter, ListPage, Locator, Reader, Setting, WriteCondition,
Writer,
};
use sign::Signer;
use std::cell::{Cell, RefCell};
/// The PROPFIND body. Asking for named properties rather than allprop keeps
@@ -21,12 +23,19 @@ const PROPS: &str = concat!(
"</d:prop></d:propfind>",
);
const MAX_CHUNK: u32 = 1024 * 1024;
/// The largest Markdown file this plugin will hold whole to sign.
const MAX_MARKDOWN: usize = 8 * 1024 * 1024;
struct Session {
root: String,
authorization: Option<String>,
signer: Signer,
}
// The host creates one component instance per connection and calls into it
// on a single thread, so this slot holds exactly one connection's session and
// is not shared state between connections. A WASI component has no other place
// to keep state between exported calls.
thread_local! {
static SESSION: RefCell<Option<Session>> = const { RefCell::new(None) };
}
@@ -179,11 +188,27 @@ impl GuestReader for DavReader {
}
}
/// Whether a path names a Markdown file, the only kind this plugin signs.
fn is_markdown(path: &str) -> bool {
path.to_ascii_lowercase().ends_with(".md")
}
/// Writes one file through a PUT whose body is pushed in pieces.
struct DavWriter {
call: host::Exchange,
done: RefCell<bool>,
written: Cell<u64>,
/// A Markdown file waits here whole until `finish`, because its signature
/// covers the entire body and lands in the frontmatter at the front.
markdown: Option<RefCell<Vec<u8>>>,
}
impl DavWriter {
fn send(&self, data: &[u8]) -> Result<(), ErrorCode> {
self.call.push(data).map_err(transport)?;
self.written.set(self.written.get() + data.len() as u64);
Ok(())
}
}
impl GuestWriter for DavWriter {
@@ -191,8 +216,14 @@ impl GuestWriter for DavWriter {
if *self.done.borrow() {
return Err(ErrorCode::InvalidInput);
}
self.call.push(&data).map_err(transport)?;
self.written.set(self.written.get() + data.len() as u64);
let Some(markdown) = &self.markdown else {
return self.send(&data);
};
let mut markdown = markdown.borrow_mut();
if markdown.len() + data.len() > MAX_MARKDOWN {
return Err(ErrorCode::Unsupported);
}
markdown.extend_from_slice(&data);
Ok(())
}
/// One call is enough: the request body is already complete, so the response
@@ -203,6 +234,12 @@ impl GuestWriter for DavWriter {
return Err(ErrorCode::InvalidInput);
}
*self.done.borrow_mut() = true;
if let Some(markdown) = &self.markdown {
let signed = session(|session| session.signer.sign(&markdown.borrow()))?;
for piece in signed.chunks(MAX_CHUNK as usize) {
self.send(piece)?;
}
}
let head = self.call.respond().map_err(transport)?;
dav::outcome(head.status)?;
let revision = head
@@ -242,9 +279,14 @@ impl Guest for Webdav {
}
_ => None,
};
let signer = Signer::new(
&lookup("ensName").ok_or(ErrorCode::InvalidInput)?,
&lookup("privateKey").ok_or(ErrorCode::InvalidInput)?,
)?;
SESSION.set(Some(Session {
root,
authorization,
signer,
}));
Ok(Capabilities {
write: true,
@@ -358,6 +400,7 @@ impl Guest for Webdav {
call,
done: RefCell::new(false),
written: Cell::new(0),
markdown: is_markdown(&path).then(|| RefCell::new(Vec::new())),
}))
}