From 3bf009122c09a90df495049e0182712c675c2b26 Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Mon, 11 Mar 2024 17:23:11 +0100 Subject: [PATCH 1/2] lib: add ASCII armoring --- .github/workflows/build.yml | 2 +- Cargo.lock | 19 +++++++++++--- Cargo.toml | 10 +++++--- src/lib.rs | 2 ++ src/library/lib.rs | 50 +++++++++++++++++++++++++++++++++++++ src/library/mod.rs | 2 ++ stl/AluVM@0.1.0.sta | 4 +-- 7 files changed, 80 insertions(+), 9 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 6d3281d..05152e4 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -27,7 +27,7 @@ jobs: strategy: fail-fast: false matrix: - feature: [ default, stl, std, alloc, all, secp256k1, curve25519 ] + feature: [ default, stl, std, alloc, ascii-armor, all, secp256k1, curve25519 ] steps: - uses: actions/checkout@v2 - name: Install rust stable diff --git a/Cargo.lock b/Cargo.lock index dc0e992..3553a25 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7,6 +7,7 @@ name = "aluvm" version = "0.11.0-beta.4" dependencies = [ "amplify", + "ascii-armor", "baid58", "curve25519-dalek", "getrandom 0.2.12", @@ -104,6 +105,19 @@ dependencies = [ "serde", ] +[[package]] +name = "ascii-armor" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "743d90b41a39d6e3920eef64a70f6411097cbb47141606a45b2a96533ec7111c" +dependencies = [ + "amplify", + "baid58", + "base85", + "sha2", + "strict_encoding", +] + [[package]] name = "baid58" version = "0.4.4" @@ -543,12 +557,11 @@ dependencies = [ [[package]] name = "strict_types" version = "2.7.0-beta.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d66de5cdf197b68e13fcac9fad7ed288f44052a319a3df3abbaba9c6e52f735b" +source = "git+https://github.com/strict-types/strict-types?branch=armor#00741ba09189536d8cdaf048a7f17795393ee382" dependencies = [ "amplify", + "ascii-armor", "baid58", - "base85", "half", "indexmap", "sha2", diff --git a/Cargo.toml b/Cargo.toml index 07e0db5..4a505d0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,6 +22,7 @@ required-features = ["stl"] [dependencies] amplify = { version = "4.6.0", default-features = false, features = ["apfloat", "derive", "hex"] } +ascii-armor = { version = "0.2.0", optional = true } paste = "1" strict_encoding = { version = "2.7.0-beta.1", default-features = false, features = ["float", "derive"] } strict_types = { version = "2.7.0-beta.1", optional = true } @@ -35,8 +36,8 @@ serde_crate = { package = "serde", version = "1", optional = true } [features] default = ["std"] -all = ["stl", "std", "log", "secp256k1", "curve25519", "serde"] -stl = ["strict_types/base85", "std"] +all = ["stl", "std", "log", "secp256k1", "curve25519", "serde", "ascii-armor"] +stl = ["strict_types/armor", "std"] std = ["amplify/std"] log = ["std"] alloc = ["amplify/alloc"] @@ -52,4 +53,7 @@ getrandom = { version = "0.2", features = ["js"] } wasm-bindgen-test = "0.3" [package.metadata.docs.rs] -features = [ "all" ] +features = ["all"] + +[patch.crates-io] +strict_types = { git = "https://github.com/strict-types/strict-types", branch = "armor" } diff --git a/src/lib.rs b/src/lib.rs index 01101af..d469711 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,8 @@ pub mod stl; mod vm; pub use isa::Isa; +#[cfg(feature = "ascii-armor")] +pub use library::LibArmorError; #[doc(hidden)] pub use paste::paste; pub use program::{Prog, ProgError, Program}; diff --git a/src/library/lib.rs b/src/library/lib.rs index 38780fd..a60f9a8 100644 --- a/src/library/lib.rs +++ b/src/library/lib.rs @@ -35,6 +35,8 @@ use amplify::{ByteArray, Bytes32}; use baid58::{Baid58ParseError, FromBaid58, ToBaid58}; use sha2::{Digest, Sha256}; +#[cfg(feature = "ascii-armor")] +pub use self::_armor::LibArmorError; use super::{Cursor, Read}; use crate::data::ByteStr; use crate::isa::{Bytecode, BytecodeError, ExecStep, Instr, InstructionSet}; @@ -168,6 +170,54 @@ impl RustHash for Lib { fn hash(&self, state: &mut H) { state.write(&self.id()[..]) } } +#[cfg(feature = "ascii-armor")] +mod _armor { + use armor::{ArmorHeader, ArmorParseError, AsciiArmor, ASCII_ARMOR_ID}; + + use super::*; + use crate::data::encoding::{Decode, DecodeError, Encode}; + + const ASCII_ARMOR_ISAE: &str = "ISA-Extensions"; + const ASCII_ARMOR_DEPENDENCY: &str = "Dependency"; + + /// Errors while deserializing library from an ASCII Armor. + #[derive(Clone, Eq, PartialEq, Debug, Display, Error, From)] + #[display(inner)] + pub enum LibArmorError { + /// Armor parse error. + #[from] + Armor(ArmorParseError), + + /// Library data deserialization error. + #[from] + Decode(DecodeError), + } + + impl AsciiArmor for Lib { + type Err = LibArmorError; + const PLATE_TITLE: &'static str = "ALUVM LIB"; + + fn ascii_armored_headers(&self) -> Vec { + let mut headers = vec![ + ArmorHeader::new(ASCII_ARMOR_ID, self.id().to_string()), + ArmorHeader::new(ASCII_ARMOR_ISAE, self.isae.to_string()), + ]; + for dep in &self.libs { + headers.push(ArmorHeader::new(ASCII_ARMOR_DEPENDENCY, dep.to_string())); + } + headers + } + + fn to_ascii_armored_data(&self) -> Vec { self.serialize() } + + fn with_headers_data(headers: Vec, data: Vec) -> Result { + // TODO: check id, dependencies and ISAE + let me = Self::deserialize(&data)?; + Ok(me) + } + } +} + /// Errors while assembling library from the instruction set #[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug, Display, From)] #[display(inner)] diff --git a/src/library/mod.rs b/src/library/mod.rs index a6b5005..6114ab5 100644 --- a/src/library/mod.rs +++ b/src/library/mod.rs @@ -30,6 +30,8 @@ mod rw; mod segs; pub use cursor::Cursor; +#[cfg(feature = "ascii-armor")] +pub use lib::LibArmorError; pub use lib::{AssemblerError, Lib, LibId, LibSite}; pub use rw::{CodeEofError, Read, Write, WriteError}; pub use segs::{IsaSeg, IsaSegError, LibSeg, LibSegOverflow, SegmentError}; diff --git a/stl/AluVM@0.1.0.sta b/stl/AluVM@0.1.0.sta index d3fb180..8dc829f 100644 --- a/stl/AluVM@0.1.0.sta +++ b/stl/AluVM@0.1.0.sta @@ -1,7 +1,7 @@ -----BEGIN STRICT TYPE LIB----- -Id: DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy +Id: urn:ubideco:stl:DVtm25LRKU4TjbyZmVxPhvCmctZ6vKkPKqfpU2QsDNUo#exodus-axiom-tommy Name: AluVM -Dependencies: ~ +Checksum-SHA256: 225116c3d4fca2a386113db3fd9ffcd2952f3a5e7ea4f1391dd0a6fc139e97ec 1wm|eR!sl^0ssX}X<|ua1pxpD002NB00&HIVpC~!Wd;HRY-wTvr!Z9lE%{u?@QI^ EqCb}2Q7OO^w+`_q*ddTXmHSf)18{G10006 From 39e37eec82e64458eafc2efcb07e02c9e438574d Mon Sep 17 00:00:00 2001 From: Dr Maxim Orlovsky Date: Tue, 12 Mar 2024 19:03:32 +0100 Subject: [PATCH 2/2] chore: fix clippy lints --- src/library/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/library/lib.rs b/src/library/lib.rs index a60f9a8..85e3c23 100644 --- a/src/library/lib.rs +++ b/src/library/lib.rs @@ -210,9 +210,9 @@ mod _armor { fn to_ascii_armored_data(&self) -> Vec { self.serialize() } - fn with_headers_data(headers: Vec, data: Vec) -> Result { + fn with_headers_data(_headers: Vec, data: Vec) -> Result { // TODO: check id, dependencies and ISAE - let me = Self::deserialize(&data)?; + let me = Self::deserialize(data)?; Ok(me) } }