diff --git a/crates/seed-keeper-wit-ui/src/input.rs b/crates/seed-keeper-wit-ui/src/input.rs index 7876efd..b72ec0e 100644 --- a/crates/seed-keeper-wit-ui/src/input.rs +++ b/crates/seed-keeper-wit-ui/src/input.rs @@ -33,12 +33,56 @@ impl From for Input { } } +impl From<&wurbo_types::Content> for Input { + fn from(content: &wurbo_types::Content) -> Self { + // if context.load.encrypted is Some, use it, + // the rest of Input is taken from context.input + println!("context.load: {:?}", content.load); + let encrypted = match &content.load { + Some(loaded_str) => { + println!("loaded_str: {:?}", loaded_str); + // try to parse the JSON + let v: serde_json::Value = + serde_json::from_str(&loaded_str).unwrap_or(serde_json::Value::Null); + + println!("v: {:?}", v); + println!("v[\"encrypted\"]: {:?}", v["encrypted"]); + + match &v["encrypted"] { + serde_json::Value::Array(encrypted) => { + println!("encrypted: {:?}", encrypted); + Some( + // encrypted into Vec + encrypted + .iter() + .map(|v| v.as_u64().unwrap_or_default() as u8) + .collect::>(), + ) + } + _ => None, + } + } + None => None, + }; + + let encrypted: Encrypted = encrypted.into(); + + Input(Some(wurbo_types::Input { + placeholder: content + .input + .as_ref() + .map(|c| c.placeholder.clone()) + .unwrap_or_default(), + encrypted_seed: encrypted.to_string().into(), + })) + } +} + impl From> for Input { fn from(context: Option) -> Self { Input(context) } } - impl Deref for Input { type Target = Option; diff --git a/crates/seed-keeper-wit-ui/src/lib.rs b/crates/seed-keeper-wit-ui/src/lib.rs index 61c07e2..dff3cb2 100644 --- a/crates/seed-keeper-wit-ui/src/lib.rs +++ b/crates/seed-keeper-wit-ui/src/lib.rs @@ -104,10 +104,10 @@ impl From<&wurbo_types::Context> for StructContext { /// We have all the content, convert it to PageContext impl From for StructContext { - fn from(context: wurbo_types::Content) -> Self { + fn from(content: wurbo_types::Content) -> Self { StructContext { - page: Page::from(context.page), - input: Input::from(context.input), + page: Page::from(&content.page), + input: Input::from(&content), // We can use default for Output because the minijinja Object impl will // calculate the values from the above inouts for us output: Output::default(), diff --git a/crates/seed-keeper-wit-ui/src/output.rs b/crates/seed-keeper-wit-ui/src/output.rs index 28396cf..64e56fd 100644 --- a/crates/seed-keeper-wit-ui/src/output.rs +++ b/crates/seed-keeper-wit-ui/src/output.rs @@ -103,10 +103,17 @@ impl Object for Output { } } -/// [Encrypted] is the encrypted seed passed from the User, if any. +/// [Encrypted] [Output] is the encrypted seed passed from the User, if any. #[derive(Debug, Clone, Default)] pub(super) struct Encrypted(Option>); +/// impl From Option> for Encrypted +impl From>> for Encrypted { + fn from(context: Option>) -> Self { + Encrypted(context) + } +} + impl ToString for Encrypted { fn to_string(&self) -> String { // encode to base64 diff --git a/crates/seed-keeper-wit-ui/src/page.rs b/crates/seed-keeper-wit-ui/src/page.rs index 4fdd0ae..acde5c1 100644 --- a/crates/seed-keeper-wit-ui/src/page.rs +++ b/crates/seed-keeper-wit-ui/src/page.rs @@ -21,15 +21,15 @@ impl Object for Page { } } -impl From for Page { - fn from(context: wurbo_types::Page) -> Self { - Page(Some(context)) +impl From<&wurbo_types::Page> for Page { + fn from(context: &wurbo_types::Page) -> Self { + Page(Some(context.clone())) } } -impl From> for Page { - fn from(context: Option) -> Self { - Page(context) +impl From<&Option> for Page { + fn from(context: &Option) -> Self { + Page(context.clone()) } } diff --git a/examples/sveltekit/src/lib/Aggregate.svelte b/examples/sveltekit/src/lib/Aggregate.svelte index e7d8386..139f6f9 100644 --- a/examples/sveltekit/src/lib/Aggregate.svelte +++ b/examples/sveltekit/src/lib/Aggregate.svelte @@ -63,6 +63,9 @@ // 119, 144, 218, 99 // ]) }, + load: JSON.stringify({ + encrypted: [42, 69, 69] + }), output: null } }, @@ -107,7 +110,7 @@ Seed Keeper - +

Aggregate User Interfaces

diff --git a/examples/sveltekit/src/lib/index.js b/examples/sveltekit/src/lib/index.js index 9717005..4715df7 100644 --- a/examples/sveltekit/src/lib/index.js +++ b/examples/sveltekit/src/lib/index.js @@ -5,6 +5,7 @@ import { default as Calculator } from './Calculator.svelte'; import { default as Aggregate } from './Aggregate.svelte'; export const examples = [ - // Seed, Edwards, Calculator, + // Seed, + // Edwards, Calculator, Aggregate ];