Sign Markdown saved to the folder with the configured ENS name and key

This commit is contained in:
Danny Kim
2026-09-26 01:35:58 +09:00
parent a4d1fae1d3
commit cb7330b8cb
6 changed files with 762 additions and 12 deletions
+41 -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,10 +23,13 @@ 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,
}
thread_local! {
@@ -179,11 +184,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 +212,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 +230,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 +275,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 +396,7 @@ impl Guest for Webdav {
call,
done: RefCell::new(false),
written: Cell::new(0),
markdown: is_markdown(&path).then(|| RefCell::new(Vec::new())),
}))
}