Skip to content

Commit

Permalink
Merge ebde9af into db19ee5
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat committed Apr 15, 2020
2 parents db19ee5 + ebde9af commit 241a9c8
Show file tree
Hide file tree
Showing 15 changed files with 524 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
members = [
"sigma-chain",
"sigma-chain-c",
"sigma-ser",
]
1 change: 1 addition & 0 deletions sigma-chain/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2"
sigma-ser = { path = "../sigma-ser" }

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
Expand Down
5 changes: 5 additions & 0 deletions sigma-chain/src/context_extension.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
use std::collections::HashMap;

pub struct ContextExtension {
pub values: HashMap<u8, Box<[u8]>>,
}
20 changes: 20 additions & 0 deletions sigma-chain/src/data_input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use crate::ergo_box::BoxId;
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::io;

pub struct DataInput {
pub box_id: BoxId,
}

impl SigmaSerializable for DataInput {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: W) -> Result<(), io::Error> {
self.box_id.sigma_serialize(w)?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: R) -> Result<Self, SerializationError> {
let box_id = BoxId::sigma_parse(r)?;
Ok(DataInput { box_id })
}
}
39 changes: 39 additions & 0 deletions sigma-chain/src/ergo_box.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
use crate::ergo_tree::ErgoTree;
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::collections::HashMap;
use std::io;

// TODO: extract BoxId
pub const DIGEST32_SIZE: usize = 32;
pub struct BoxId(pub [u8; DIGEST32_SIZE]);

impl SigmaSerializable for BoxId {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, mut w: W) -> Result<(), io::Error> {
w.write_all(&self.0)?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(mut r: R) -> Result<Self, SerializationError> {
let mut bytes = [0; DIGEST32_SIZE];
r.read_exact(&mut bytes)?;
Ok(Self(bytes))
}
}

pub struct TokenId(Box<[u8; DIGEST32_SIZE]>);

pub struct TokenInfo {
pub token_id: TokenId,
pub amount: u64,
}

pub struct NonMandatoryRegisterId(u8);

pub struct ErgoBoxCandidate {
pub value: u64,
pub ergo_tree: ErgoTree,
pub tokens: Vec<TokenInfo>,
pub additional_registers: HashMap<NonMandatoryRegisterId, Box<[u8]>>,
pub creation_height: u32,
}
1 change: 1 addition & 0 deletions sigma-chain/src/ergo_tree.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub struct ErgoTree {}
7 changes: 7 additions & 0 deletions sigma-chain/src/input.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
use crate::ergo_box::BoxId;
use crate::prover_result::ProverResult;

pub struct Input {
pub box_id: BoxId,
pub spending_proof: ProverResult,
}
7 changes: 7 additions & 0 deletions sigma-chain/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,10 @@ mod misc;
mod utils;

pub use misc::*;
pub mod context_extension;
pub mod data_input;
pub mod ergo_box;
pub mod ergo_tree;
pub mod input;
pub mod prover_result;
pub mod transaction;
6 changes: 6 additions & 0 deletions sigma-chain/src/prover_result.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use crate::context_extension::ContextExtension;

pub struct ProverResult {
pub proof: Box<[u8]>,
pub extension: ContextExtension,
}
23 changes: 23 additions & 0 deletions sigma-chain/src/transaction.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use crate::data_input::DataInput;
use crate::ergo_box::ErgoBoxCandidate;
use crate::input::Input;
use sigma_ser::serializer::SerializationError;
use sigma_ser::serializer::SigmaSerializable;
use sigma_ser::vlq_encode;
use std::io;

pub struct Transaction {
pub inputs: Vec<Input>,
pub data_inputs: Vec<DataInput>,
pub outputs: Vec<ErgoBoxCandidate>,
}

impl SigmaSerializable for Transaction {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, mut w: W) -> Result<(), io::Error> {
w.put_u16(self.inputs.len() as u16)?;
Ok(())
}
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(_: R) -> Result<Self, SerializationError> {
unimplemented!()
}
}
29 changes: 29 additions & 0 deletions sigma-ser/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
[package]
name = "sigma-ser"
version = "0.1.0"
authors = ["Denys Zadorozhnyi <denys@zadorozhnyi.com>"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.10"
proptest = "0.9"
proptest-derive = "0.1.2"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
3 changes: 3 additions & 0 deletions sigma-ser/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod serializer;
pub mod vlq_encode;
pub mod zig_zag_encode;
25 changes: 25 additions & 0 deletions sigma-ser/src/serializer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::vlq_encode;
use std::io;

#[derive(Debug)]
pub enum SerializationError {
VlqEncode(vlq_encode::VlqEncodingError),
Io(io::Error),
}

impl From<vlq_encode::VlqEncodingError> for SerializationError {
fn from(error: vlq_encode::VlqEncodingError) -> Self {
SerializationError::VlqEncode(error)
}
}

impl From<io::Error> for SerializationError {
fn from(error: io::Error) -> Self {
SerializationError::Io(error)
}
}

pub trait SigmaSerializable: Sized {
fn sigma_serialize<W: vlq_encode::WriteSigmaVlqExt>(&self, w: W) -> Result<(), io::Error>;
fn sigma_parse<R: vlq_encode::ReadSigmaVlqExt>(r: R) -> Result<Self, SerializationError>;
}

0 comments on commit 241a9c8

Please sign in to comment.