Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parser #1

Merged
merged 30 commits into from
Jun 29, 2020
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
4fd9f57
[ledger-blockstack] Initial parser implementation, Rust files
neithanmo Jun 16, 2020
9c3561b
[ledger-blockstack] rust formatting implementation
neithanmo Jun 16, 2020
7387573
[ledger-blockstack] Initial parser implementation - c files
neithanmo Jun 16, 2020
6ca2eda
Added some spending-contition tests
neithanmo Jun 18, 2020
194ca49
Added two post-condition tests
neithanmo Jun 18, 2020
c2160be
Test for token transfer payloads
neithanmo Jun 18, 2020
ee7e1fa
Check if a transaction has a standard auth field which enable token-t…
neithanmo Jun 18, 2020
5b592aa
[ledger-blockstack] Initial parser implementation, Rust files
neithanmo Jun 16, 2020
93284f3
[ledger-blockstack] rust formatting implementation
neithanmo Jun 16, 2020
07cc029
[ledger-blockstack] Initial parser implementation - c files
neithanmo Jun 16, 2020
d209c6e
Added some spending-contition tests
neithanmo Jun 18, 2020
9fa3f55
Added two post-condition tests
neithanmo Jun 18, 2020
1c74085
Test for token transfer payloads
neithanmo Jun 18, 2020
3627af8
Check if a transaction has a standard auth field which enable token-t…
neithanmo Jun 18, 2020
d4c4f31
disable zemu signing test
jleni Jun 18, 2020
72ba50e
improve JS implementation
jleni Jun 18, 2020
062bda7
correct JS naming
jleni Jun 18, 2020
3839331
Merging from latest Parser
neithanmo Jun 19, 2020
bb80b81
Fixing minor issues which were about hex crate version, parser state …
neithanmo Jun 19, 2020
94adb44
Added calls to the canary checker - aso, fixed some clippy warnings
neithanmo Jun 20, 2020
c92776f
Lets start with TokenTransfer payload first - the other payloads woul…
neithanmo Jun 20, 2020
221a608
Adapted the base32-check blockstack's address encoder
neithanmo Jun 25, 2020
bad3cac
address get/show APDU
jleni Jun 27, 2020
b097099
upgrade zxlib + icons
jleni Jun 27, 2020
e3879e3
Fix CI issues
jleni Jun 27, 2020
d879f75
A global variable wrapper and pic macro
neithanmo Jun 28, 2020
ef86ded
New tests, debug, minor fixes according to cargo fmt
neithanmo Jun 28, 2020
ec12101
Restoring to previous cicleci config.yml
neithanmo Jun 28, 2020
84ac923
Adding missing test files
neithanmo Jun 28, 2020
f77aba6
updating zemu tests
jleni Jun 29, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
110 changes: 110 additions & 0 deletions app/rust/Cargo.lock

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

7 changes: 7 additions & 0 deletions app/rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,17 @@ name = "rslib"
crate-type = ["staticlib"]

[dependencies]
nom = {version = "5.1.1", default-features = false}

[target.thumbv6m-none-eabi.dev-dependencies]
panic-halt = "0.2.0"

[dev-dependencies]
serde_json = "1.0.53"
hex = "0.4.2"
serde = { version="1.0.110", features = ["derive"] }


[profile.release]
lto=false
codegen-units = 1
Expand Down
21 changes: 21 additions & 0 deletions app/rust/include/rslib.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,25 @@
#pragma once

#include <stdint.h>
#include "parser_common.h"
#include "parser_txdef.h"



/****************************** others ********************************************************************************/

parser_error_t _parser_init(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize, uint16_t *alloc_size);

parser_error_t _read(const parser_context_t *c, parser_tx_t *v);

parser_error_t _validate(const parser_context_t *ctx, const parser_tx_t *v);

uint8_t _getNumItems(const parser_context_t *ctx, const parser_tx_t *v);

parser_error_t _getItem(const parser_context_t *ctx,
int8_t displayIdx,
char *outKey, uint16_t outKeyLen,
char *outValue, uint16_t outValueLen,
uint8_t pageIdx, uint8_t *pageCount,
const parser_tx_t *v);

16 changes: 6 additions & 10 deletions app/rust/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
#![no_std]
#![no_builtins]

#![allow(dead_code, unused_imports)]

#[cfg(test)]
#[macro_use]
extern crate std;

mod bolos;
mod parser;
mod zxformat;

extern crate core;

Expand All @@ -17,12 +22,3 @@ use core::panic::PanicInfo;
fn panic(_info: &PanicInfo) -> ! {
loop {}
}

#[cfg(test)]
mod tests {
use crate::*;

#[test]
fn test_helloworld() {
}
}
11 changes: 11 additions & 0 deletions app/rust/src/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
mod parser;
mod parser_common;
mod post_condition;
mod spending_condition;
mod transaction;
mod transaction_auth;
mod transaction_payload;
mod value;
pub use parser::{_getItem, _getNumItems, _parser_init, _read, _validate};
pub use parser_common::{Hash160, ParserError};
pub use transaction_auth::TransactionAuth;
132 changes: 132 additions & 0 deletions app/rust/src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
#![allow(non_camel_case_types, non_snake_case)]

use crate::parser::{
parser_common::ParserError, post_condition::TransactionPostCondition, transaction::Transaction,
};

#[repr(C)]
#[no_mangle]
pub struct parser_context_t {
pub buffer: *const u8,
pub bufferLen: u16,
pub offset: u16,
}

#[repr(C)]
#[no_mangle]
pub struct parse_tx_t {
state: *mut u8,
len: u16,
}

#[no_mangle]
pub extern "C" fn _parser_init(
ctx: *mut parser_context_t,
buffer: *const u8,
bufferSize: u16,
alloc_size: *mut u16,
) -> u32 {
// Lets the caller know how much memory we need for allocating
// our global state
if alloc_size.is_null() {
return ParserError::parser_no_memory_for_state as u32;
}
unsafe {
let size = core::mem::size_of::<Transaction>() as u16;
// Aproximate this size to the nearest(roof) multiple of 4
*alloc_size = size + (4 - size.rem_euclid(4));
jleni marked this conversation as resolved.
Show resolved Hide resolved
}
parser_init_context(ctx, buffer, bufferSize) as u32
}

fn parser_init_context(
ctx: *mut parser_context_t,
buffer: *const u8,
bufferSize: u16,
) -> ParserError {
unsafe {
(*ctx).offset = 0;

if bufferSize == 0 || buffer.is_null() {
(*ctx).buffer = core::ptr::null_mut();
(*ctx).bufferLen = 0;
return ParserError::parser_init_context_empty;
}

(*ctx).buffer = buffer;
(*ctx).bufferLen = bufferSize;
return ParserError::parser_ok;
}
}

#[no_mangle]
pub extern "C" fn _read(context: *const parser_context_t, parser_state: *mut parse_tx_t) -> u32 {
unsafe {
let data = core::slice::from_raw_parts((*context).buffer, (*context).bufferLen as _);
match Transaction::from_bytes(data) {
Ok(transaction) => {
if parser_state.is_null()
|| (*parser_state).state.is_null()
|| (*parser_state).len < core::mem::size_of::<Transaction>() as _
{
return ParserError::parser_no_memory_for_state as u32;
}
let tx = &transaction as *const _ as *const u8;
core::ptr::copy_nonoverlapping(tx, (*parser_state).state, (*parser_state).len as _);
ParserError::parser_ok as u32
}
Err(e) => e as u32,
}
}
}

#[no_mangle]
pub extern "C" fn _validate(_ctx: *const parser_context_t, _tx_t: *const parse_tx_t) -> u32 {
// TODO
ParserError::parser_ok as u32
}

#[no_mangle]
pub extern "C" fn _getNumItems(_ctx: *const parser_context_t, tx_t: *const parse_tx_t) -> u8 {
unsafe {
if tx_t.is_null() || (*tx_t).state.is_null() {
return 0;
}
if let Some(_tx) = ((*tx_t).state as *const Transaction).as_ref() {
return 0;
}
return 0;
}
}

#[no_mangle]
pub extern "C" fn _getItem(
_ctx: *const parser_context_t,
displayIdx: u8,
outKey: *mut i8,
outKeyLen: u16,
outValue: *mut i8,
outValueLen: u16,
pageIdx: u8,
pageCount: *mut u8,
tx_t: *const parse_tx_t,
) -> u32 {
unsafe {
*pageCount = 0u8;
let key = core::slice::from_raw_parts_mut(outKey as *mut u8, outKeyLen as usize);
let value = core::slice::from_raw_parts_mut(outValue as *mut u8, outValueLen as usize);
if tx_t.is_null() || (*tx_t).state.is_null() {
return ParserError::parser_context_mismatch as _;
}
if let Some(tx) = ((*tx_t).state as *const Transaction).as_ref() {
return match tx.get_item(displayIdx, key, value, pageIdx) {
Ok(page) => {
*pageCount = page;
ParserError::parser_ok as _
}
Err(e) => e as _,
};
}
ParserError::parser_context_mismatch as _
}
}
Loading