Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
feat: this was able to sign a transaction on-chain (#60)
Browse files Browse the repository at this point in the history
* feat: this was able to sign a transaction on-chain

* refactor: cleanup

* feat: make red light shine
  • Loading branch information
Slesarew authored Aug 10, 2023
1 parent 50827f0 commit 2545f93
Show file tree
Hide file tree
Showing 12 changed files with 120 additions and 23 deletions.
2 changes: 1 addition & 1 deletion firmware/development/kampela-system/src/draw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum DisplayError {}
///
//TODO tune these values for prod; something like 12k and 8k
const FAST_REFRESH_POWER: i32 = 4000;
const FULL_REFRESH_POWER: i32 = 4000;
const FULL_REFRESH_POWER: i32 = 6000;

/// Virtual display data storage
type PixelData = BitArr!(for SCREEN_SIZE_VALUE, in u8, Msb0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ fn map_gpio(gpio: &mut GPIO_S) {

/// Set GPIO pins to their starting values
fn set_gpio_pins(gpio: &mut GPIO_S) {
mcu_ok_clear(gpio);
mcu_ok_clear(gpio);
pow_set(gpio);
delay(100000); // wait after power set! (epaper manual for 2.8V setup)
Expand Down
6 changes: 6 additions & 0 deletions firmware/development/kampela-ui/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions firmware/development/kampela-ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ lazy_static = { version = "1.4.0", default_features = false }
patches = { path = "../kampela_experiments_efm32pg23/patches" }
kampela-display-common = { path = "../kampela-display-common" }
clap = { version = "4.2.1", features = ["derive"], optional = true }
qrcodegen-noheap = {git = "https://github.com/Slesarew/QR-Code-generator", branch = "patch-1"}

[features]
default = ["std"]
Expand Down
1 change: 1 addition & 0 deletions firmware/development/kampela-ui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ pub mod backup;
mod restore_or_generate;

pub mod transaction;
pub mod qr;

#[macro_use]
extern crate lazy_static;
Expand Down
18 changes: 17 additions & 1 deletion firmware/development/kampela-ui/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ mod data_state;
use data_state::{AppStateInit, NFCState, DataInit, StorageState};

mod transaction;
mod qr;

#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
Expand Down Expand Up @@ -89,6 +90,7 @@ struct DesktopSimulator {
seed: Vec<u8>,
transaction: String,
extensions: String,
signature: Option<[u8; 130]>,
}

impl DesktopSimulator {
Expand All @@ -103,12 +105,17 @@ impl DesktopSimulator {
NFCState::Empty => String::new(),
NFCState::Transaction => String::from("Hello, this is a transaction!"),
};
let signature = match init_state.nfc {
NFCState::Empty => None,
NFCState::Transaction => Some([0u8; 130]),
};
Self {
pin: pin,
display: display,
seed: Vec::new(),
transaction: transaction,
extensions: extensions,
signature: signature,
}
}
}
Expand Down Expand Up @@ -146,9 +153,10 @@ impl Platform for DesktopSimulator {
(&self.seed, &mut self.display)
}

fn set_transaction(&mut self, transaction: String, extensions: String) {
fn set_transaction(&mut self, transaction: String, extensions: String, signature: [u8; 130]) {
self.transaction = transaction;
self.extensions = extensions;
self.signature = Some(signature);
}

fn transaction(&mut self) -> Option<(&str, &mut Self::Display)> {
Expand All @@ -166,6 +174,14 @@ impl Platform for DesktopSimulator {
None
}
}

fn signature(&mut self) -> (&[u8; 130], &mut Self::Display) {
if let Some(ref a) = self.signature {
(a, &mut self.display)
} else {
panic!("qr not ready!");
}
}
}


Expand Down
10 changes: 9 additions & 1 deletion firmware/development/kampela-ui/src/platform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::pin::Pincode;
use crate::uistate::EventResult;
use crate::backup::draw_backup_screen;
use crate::transaction;
use crate::qr;

const ENTROPY_LEN: usize = 32; //TODO: move to appropriate place

Expand Down Expand Up @@ -48,11 +49,13 @@ pub trait Platform {
/// Getter for pincode and canvas
fn entropy_display(&mut self) -> (&Vec<u8>, &mut Self::Display);

fn set_transaction(&mut self, transaction: String, extensions: String);
fn set_transaction(&mut self, transaction: String, extensions: String, signature: [u8; 130]);

fn transaction(&mut self) -> Option<(&str, &mut Self::Display)>;

fn extensions(&mut self) -> Option<(&str, &mut Self::Display)>;

fn signature(&mut self) -> (&[u8; 130], &mut Self::Display);

//----derivatives----

Expand Down Expand Up @@ -101,5 +104,10 @@ pub trait Platform {
Ok(())
}
}

fn draw_qr(&mut self) -> Result<(), <Self::Display as DrawTarget>::Error> {
let (s, d) = self.signature();
qr::draw(s, d)
}
}

58 changes: 58 additions & 0 deletions firmware/development/kampela-ui/src/qr.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
use embedded_graphics::{
mono_font::{
ascii::{FONT_6X10},
MonoTextStyle,
},
Pixel,
primitives::Rectangle,
Drawable,
};
use embedded_graphics_core::{
draw_target::DrawTarget,
geometry::{Point, Size},
pixelcolor::BinaryColor,
};
use embedded_text::{
alignment::{HorizontalAlignment, VerticalAlignment},
style::{HeightMode, TextBoxStyleBuilder},
TextBox,
};

use crate::display_def::*;
use qrcodegen_noheap::{QrCode, QrCodeEcc, Version};

pub fn draw<D>(data_to_qr: &[u8], display: &mut D) -> Result<(), D::Error>
where
D: DrawTarget<Color = BinaryColor>,
{
let len = data_to_qr.len();

let mut outbuffer = [0u8; Version::new(18).buffer_len()].to_vec();
let mut dataandtemp = [0u8; Version::new(18).buffer_len()].to_vec();

dataandtemp[..len].copy_from_slice(data_to_qr);

let qr_code = QrCode::encode_binary(&mut dataandtemp, len, &mut outbuffer, QrCodeEcc::Low, Version::MIN, Version::new(18), None, true).unwrap();

let scaling = {
if qr_code.version() == Version::new(18) {2}
else {SCREEN_SIZE_Y as i32/qr_code.size()}
};

let size = qr_code.size() * scaling;
for y in 0..size {
for x in 0..size {
let color = {
if qr_code.get_module(x / scaling, y / scaling) {BinaryColor::On}
else {BinaryColor::Off}
};
let x_point = SCREEN_SIZE_X as i32/2 - size/2 + x;
let y_point = SCREEN_SIZE_Y as i32/2 - size/2 + y;
let point = Point::new(x_point, y_point);
let pixel = Pixel::<BinaryColor>(point, color);
pixel.draw(display)?;
}
}
Ok(())
}

9 changes: 6 additions & 3 deletions firmware/development/kampela-ui/src/uistate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,10 +200,10 @@ impl <P: Platform> UIState<P> {
/// Handle NFC message reception.
/// TODO this correctly
/// currently it is a quick demo for expo
pub fn handle_rx(&mut self, transaction: String, extensions: String) -> UpdateRequest
pub fn handle_rx(&mut self, transaction: String, extensions: String, signature: [u8; 130]) -> UpdateRequest
{
let mut out = UpdateRequest::new();
self.platform.set_transaction(transaction, extensions);
self.platform.set_transaction(transaction, extensions, signature);
match self.screen {
Screen::OnboardingRestoreOrGenerate => {
self.screen = Screen::ShowTransaction;
Expand Down Expand Up @@ -255,8 +255,11 @@ impl <P: Platform> UIState<P> {
self.platform.draw_transaction()?
},
Screen::ShowExtension => {
self.platform.draw_extensions()?
}
Screen::QR => (),
Screen::QR => {
self.platform.draw_qr()?
},
_ => {}
}
Ok(())
Expand Down
1 change: 1 addition & 0 deletions firmware/development/kampela/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 5 additions & 14 deletions firmware/development/kampela/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,6 @@ fn main() -> ! {
let public_key = psram_read_at_address(peripherals, start_address, 33usize).unwrap();
assert!(public_key.starts_with(&[1u8]) & (public_key[1..] == ALICE_KAMPELA_KEY[64..]), "Unknown address.");
});

let mut public_key_option: Option<[u8;32]> = None;
in_free(|peripherals| {
let start_address = nfc_payload.encoded_data.start_address.try_shift(position).unwrap();
public_key_option = Some(psram_read_at_address(peripherals, start_address, 32usize).unwrap().try_into().expect("static length, always fits"));
});
let public_key = public_key_option.unwrap();
assert!(public_key == &ALICE_KAMPELA_KEY[64..]);

in_free(|peripherals| {
let mut external_psram = ExternalPsram{peripherals};
Expand Down Expand Up @@ -315,12 +307,11 @@ fn main() -> ! {
signature_with_id[1..].copy_from_slice(&signature.to_bytes());
let signature_into_qr: [u8; 130] = hex::encode(signature_with_id).into_bytes().try_into().expect("static known length");

in_free(|peripherals| {
draw_qr(peripherals, &signature_into_qr);
});

ui.handle_rx(transaction.0, transaction.1);
ui.handle_rx(transaction.0, transaction.1, signature_into_qr);

loop {}
loop {
adc.advance(());
ui.advance(adc.read());
}
}

17 changes: 14 additions & 3 deletions firmware/development/kampela/src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ impl UI {
};
}

pub fn handle_rx(&mut self, transaction: String, extensions: String) {
self.update = self.state.handle_rx(transaction, extensions);
pub fn handle_rx(&mut self, transaction: String, extensions: String, signature: [u8; 130]) {
self.update = self.state.handle_rx(transaction, extensions, signature);
}
}

Expand All @@ -106,6 +106,7 @@ struct Hardware {
display: FrameBuffer,
transaction: Option<String>,
extensions: Option<String>,
signature: Option<[u8; 130]>,
}

impl Hardware {
Expand All @@ -120,6 +121,7 @@ impl Hardware {
display: display,
transaction: None,
extensions: None,
signature: None,
}
}
}
Expand Down Expand Up @@ -157,9 +159,10 @@ impl <'a> Platform for Hardware {
(&self.entropy, &mut self.display)
}

fn set_transaction(&mut self, transaction: String, extensions: String) {
fn set_transaction(&mut self, transaction: String, extensions: String, signature: [u8; 130]) {
self.transaction = Some(transaction);
self.extensions = Some(extensions);
self.signature = Some(signature);
}

fn transaction(&mut self) -> Option<(&str, &mut <Self as Platform>::Display)> {
Expand All @@ -177,6 +180,14 @@ impl <'a> Platform for Hardware {
None
}
}

fn signature(&mut self) -> (&[u8; 130], &mut <Self as Platform>::Display) {
if let Some(ref a) = self.signature {
(a, &mut self.display)
} else {
panic!("qr generation failed");
}
}
}

lazy_static! {
Expand Down

0 comments on commit 2545f93

Please sign in to comment.