Skip to content

Commit

Permalink
add feat: load encrypted seed into form
Browse files Browse the repository at this point in the history
  • Loading branch information
DougAnderson444 committed Jun 27, 2024
1 parent 251d50a commit 27fc50d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 13 deletions.
46 changes: 45 additions & 1 deletion crates/seed-keeper-wit-ui/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,56 @@ impl From<wurbo_types::Input> 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<u8>
encrypted
.iter()
.map(|v| v.as_u64().unwrap_or_default() as u8)
.collect::<Vec<u8>>(),
)
}
_ => 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<Option<wurbo_types::Input>> for Input {
fn from(context: Option<wurbo_types::Input>) -> Self {
Input(context)
}
}

impl Deref for Input {
type Target = Option<wurbo_types::Input>;

Expand Down
6 changes: 3 additions & 3 deletions crates/seed-keeper-wit-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ impl From<&wurbo_types::Context> for StructContext {

/// We have all the content, convert it to PageContext
impl From<wurbo_types::Content> 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(),
Expand Down
9 changes: 8 additions & 1 deletion crates/seed-keeper-wit-ui/src/output.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<u8>>);

/// impl From Option<Vec<u8>> for Encrypted
impl From<Option<Vec<u8>>> for Encrypted {
fn from(context: Option<Vec<u8>>) -> Self {
Encrypted(context)
}
}

impl ToString for Encrypted {
fn to_string(&self) -> String {
// encode to base64
Expand Down
12 changes: 6 additions & 6 deletions crates/seed-keeper-wit-ui/src/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ impl Object for Page {
}
}

impl From<wurbo_types::Page> 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<Option<wurbo_types::Page>> for Page {
fn from(context: Option<wurbo_types::Page>) -> Self {
Page(context)
impl From<&Option<wurbo_types::Page>> for Page {
fn from(context: &Option<wurbo_types::Page>) -> Self {
Page(context.clone())
}
}

Expand Down
5 changes: 4 additions & 1 deletion examples/sveltekit/src/lib/Aggregate.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@
// 119, 144, 218, 99
// ])
},
load: JSON.stringify({
encrypted: [42, 69, 69]
}),
output: null
}
},
Expand Down Expand Up @@ -107,7 +110,7 @@

<svelte:head>
<title>Seed Keeper</title>
<script src="https://cdn.tailwindcss.com"></script>
<!-- <script src="https://cdn.tailwindcss.com"></script> -->
</svelte:head>
<div>
<h1>Aggregate User Interfaces</h1>
Expand Down
3 changes: 2 additions & 1 deletion examples/sveltekit/src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

0 comments on commit 27fc50d

Please sign in to comment.