From 5b4b8cb7aec2fa9a72530791e1a12414ddd0db01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 11:49:11 +0100 Subject: [PATCH 01/15] Create `kairos-tx` package. --- Cargo.lock | 4 ++++ Cargo.toml | 1 + kairos-tx/Cargo.toml | 6 ++++++ kairos-tx/src/lib.rs | 0 4 files changed, 11 insertions(+) create mode 100644 kairos-tx/Cargo.toml create mode 100644 kairos-tx/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 5f24967b..435be1bb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -635,6 +635,10 @@ dependencies = [ "tracing-subscriber", ] +[[package]] +name = "kairos-tx" +version = "0.1.0" + [[package]] name = "lazy_static" version = "1.4.0" diff --git a/Cargo.toml b/Cargo.toml index d19bb8a6..b4e5f974 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,7 @@ resolver = "2" members = [ "kairos-cli", "kairos-server", + "kairos-tx", ] [workspace.package] diff --git a/kairos-tx/Cargo.toml b/kairos-tx/Cargo.toml new file mode 100644 index 00000000..c7efc173 --- /dev/null +++ b/kairos-tx/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "kairos-tx" +version.workspace = true +edition.workspace = true + +[lib] diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs new file mode 100644 index 00000000..e69de29b From 8b323c01545994bdf25671a782aeb5ddcbb40b8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 11:50:49 +0100 Subject: [PATCH 02/15] Define ANS.1 schema. --- kairos-tx/schema.asn | 42 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 kairos-tx/schema.asn diff --git a/kairos-tx/schema.asn b/kairos-tx/schema.asn new file mode 100644 index 00000000..049315ea --- /dev/null +++ b/kairos-tx/schema.asn @@ -0,0 +1,42 @@ +---- + +TxSchema DEFINITIONS AUTOMATIC TAGS ::= BEGIN + + -- Basic types. + PublicKey ::= OCTET STRING + Amount ::= INTEGER (0..18446744073709551615) + Nonce ::= INTEGER (0..18446744073709551615) + + -- Transaction payload for signing. + SigningPayload ::= SEQUENCE { + nonce Nonce, + body TransactionBody, + ... + } + + -- Generic transaction body. + TransactionBody ::= CHOICE { + deposit [0] Deposit, + transfer [1] Transfer, + withdrawal [2] Withdrawal, + ... + } + + -- Detailed definitions of each transaction type. + Deposit ::= SEQUENCE { + amount Amount, + ... + } + + Transfer ::= SEQUENCE { + recipient PublicKey, + amount Amount, + ... + } + + Withdrawal ::= SEQUENCE { + amount Amount, + ... + } + +END From f34391d8baee2699d70db0475bd9c1836cb54f67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 11:55:38 +0100 Subject: [PATCH 03/15] Install `rasn` dependency. Coded framework for ASN.1. --- Cargo.lock | 311 ++++++++++++++++++++++++++++++++++++++++++- kairos-tx/Cargo.toml | 3 + 2 files changed, 307 insertions(+), 7 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 435be1bb..d372829e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -32,6 +32,12 @@ version = "1.0.79" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "080e9890a082662b09c1ad45f567faeeb47f22b5fb23895fbe1e651e718e25ca" +[[package]] +name = "arrayvec" +version = "0.7.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "96d30a06541fbafbc7f82ed10c06164cfbd2c401138f6addd8404629c4b16711" + [[package]] name = "async-trait" version = "0.1.77" @@ -40,7 +46,7 @@ checksum = "c980ee35e870bd1a4d2c8294d4c04d0499e67bca1e4b5cefcc693c2fa00caea9" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -147,7 +153,7 @@ dependencies = [ "heck", "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -227,6 +233,28 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bitvec" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1bc2832c24239b0141d5674bb9174f9d68a8b5b3f2753311927c172ca46f7e9c" +dependencies = [ + "funty", + "radium", + "tap", + "wyz", +] + +[[package]] +name = "bitvec-nom2" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4863ce31b7ff8812568eaffe956024c824d845a1f9f08c329706166c357cae53" +dependencies = [ + "bitvec", + "nom", +] + [[package]] name = "block-buffer" version = "0.10.4" @@ -257,6 +285,21 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "chrono" +version = "0.4.34" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" +dependencies = [ + "num-traits", +] + +[[package]] +name = "const_panic" +version = "0.2.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6051f239ecec86fde3410901ab7860d458d160371533842974fc61f96d15879b" + [[package]] name = "cookie" version = "0.18.0" @@ -276,6 +319,31 @@ dependencies = [ "libc", ] +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "crypto-common" version = "0.1.6" @@ -311,12 +379,24 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "doc-comment" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" + [[package]] name = "dotenvy" version = "0.15.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1aaf95b3e5c8f23aa320147307562d361db0ae0d51242340f558153b4eb2439b" +[[package]] +name = "either" +version = "1.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11157ac094ffbdde99aa67b23417ebdd801842852b500e395a45a9c0aac03e4a" + [[package]] name = "equivalent" version = "1.0.1" @@ -354,6 +434,12 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "funty" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e6d5a32815ae3f33302d95fdcb2ce17862f8c65363dcfd29360480ba1001fc9c" + [[package]] name = "futures-channel" version = "0.3.30" @@ -607,12 +693,27 @@ dependencies = [ "hashbrown", ] +[[package]] +name = "itertools" +version = "0.10.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473" +dependencies = [ + "either", +] + [[package]] name = "itoa" version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c" +[[package]] +name = "jzon" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "17ab85f84ca42c5ec520e6f3c9966ba1fd62909ce260f8837e248857d2560509" + [[package]] name = "kairos-cli" version = "0.1.0" @@ -638,6 +739,29 @@ dependencies = [ [[package]] name = "kairos-tx" version = "0.1.0" +dependencies = [ + "rasn", +] + +[[package]] +name = "konst" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8d712a8c49d4274f8d8a5cf61368cb5f3c143d149882b1a2918129e53395fdb0" +dependencies = [ + "const_panic", + "konst_kernel", + "typewit", +] + +[[package]] +name = "konst_kernel" +version = "0.3.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dac6ea8c376b6e208a81cf39b8e82bebf49652454d98a4829e907dac16ef1790" +dependencies = [ + "typewit", +] [[package]] name = "lazy_static" @@ -716,6 +840,12 @@ dependencies = [ "unicase", ] +[[package]] +name = "minimal-lexical" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a" + [[package]] name = "miniz_oxide" version = "0.7.1" @@ -736,6 +866,16 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "nom" +version = "7.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a" +dependencies = [ + "memchr", + "minimal-lexical", +] + [[package]] name = "nu-ansi-term" version = "0.46.0" @@ -746,6 +886,26 @@ dependencies = [ "winapi", ] +[[package]] +name = "num-bigint" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "608e7659b5c3d7cba262d894801b9ec9d00de989e8a82bd4bef91d08da45cdc0" +dependencies = [ + "autocfg", + "num-integer", + "num-traits", +] + +[[package]] +name = "num-integer" +version = "0.1.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7969661fd2958a5cb096e56c8e1ad0444ac2bbcd0061bd28660485a44879858f" +dependencies = [ + "num-traits", +] + [[package]] name = "num-traits" version = "0.2.17" @@ -833,7 +993,7 @@ checksum = "4359fd9c9171ec6e8c62926d6faaf553a8dc3f64e1507e76da7911b4f6a04405" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -914,6 +1074,12 @@ dependencies = [ "proc-macro2", ] +[[package]] +name = "radium" +version = "0.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc33ff2d4973d518d823d61aa239014831e521c75da58e3df4840d3f47749d09" + [[package]] name = "rand" version = "0.8.5" @@ -953,6 +1119,64 @@ dependencies = [ "rand_core", ] +[[package]] +name = "rasn" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf9b0d03fbc7d2dcfdd35086c43ce30ac5ff62ed7eff4397e4f4f2995a2b0e2a" +dependencies = [ + "arrayvec", + "bitvec", + "bitvec-nom2", + "bytes", + "chrono", + "either", + "jzon", + "konst", + "nom", + "num-bigint", + "num-integer", + "num-traits", + "once_cell", + "rasn-derive", + "snafu", +] + +[[package]] +name = "rasn-derive" +version = "0.12.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cbaf7105cd254b632f4732fbcc243ce750cef87d8335826125ef6df5733b5a0c" +dependencies = [ + "either", + "itertools", + "proc-macro2", + "quote", + "rayon", + "syn 1.0.109", + "uuid", +] + +[[package]] +name = "rayon" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4963ed1bc86e4f3ee217022bd855b297cef07fb9eac5dfa1f788b220b49b3bd" +dependencies = [ + "either", + "rayon-core", +] + +[[package]] +name = "rayon-core" +version = "1.12.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2" +dependencies = [ + "crossbeam-deque", + "crossbeam-utils", +] + [[package]] name = "redox_syscall" version = "0.4.1" @@ -1098,7 +1322,7 @@ checksum = "46fe8f8603d81ba86327b23a2e9cdf49e1255fb94a4c5f297f6ee0547178ea2c" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -1191,6 +1415,29 @@ version = "1.12.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e" +[[package]] +name = "snafu" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4de37ad025c587a29e8f3f5605c00f70b98715ef90b9061a815b9e59e9042d6" +dependencies = [ + "backtrace", + "doc-comment", + "snafu-derive", +] + +[[package]] +name = "snafu-derive" +version = "0.7.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "990079665f075b699031e9c08fd3ab99be5029b96f3b78dc0709e8f77e4efebf" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn 1.0.109", +] + [[package]] name = "socket2" version = "0.5.5" @@ -1201,6 +1448,17 @@ dependencies = [ "windows-sys 0.48.0", ] +[[package]] +name = "syn" +version = "1.0.109" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + [[package]] name = "syn" version = "2.0.48" @@ -1218,6 +1476,12 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2047c6ded9c721764247e62cd3b03c09ffc529b2ba5b10ec482ae507a4a70160" +[[package]] +name = "tap" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "55937e1799185b12863d447f42597ed69d9928686b8d88a1df17376a097d8369" + [[package]] name = "tempfile" version = "3.9.0" @@ -1248,7 +1512,7 @@ checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -1333,7 +1597,7 @@ checksum = "5b8a1e28f2deaa14e508979454cb3a223b10b938b45af148bc0986de36f1923b" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -1398,7 +1662,7 @@ checksum = "34704c8d6ebcbc939824180af020566b01a7c01f80641264eba0999f6c2b6be7" dependencies = [ "proc-macro2", "quote", - "syn", + "syn 2.0.48", ] [[package]] @@ -1452,6 +1716,21 @@ version = "1.17.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "42ff0bf0c66b8238c6f3b578df37d0b7848e55df8577b3f74f92a69acceeb825" +[[package]] +name = "typewit" +version = "1.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6fb9ae6a3cafaf0a5d14c2302ca525f9ae8e07a0f0e6949de88d882c37a6e24" +dependencies = [ + "typewit_proc_macros", +] + +[[package]] +name = "typewit_proc_macros" +version = "1.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e36a83ea2b3c704935a01b4642946aadd445cea40b10935e3f8bd8052b8193d6" + [[package]] name = "unarray" version = "0.1.4" @@ -1499,6 +1778,15 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "uuid" +version = "1.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f00cc9702ca12d3c81455259621e676d0f7251cec66a21e98fe2e9a37db93b2a" +dependencies = [ + "getrandom", +] + [[package]] name = "valuable" version = "0.1.0" @@ -1689,6 +1977,15 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "wyz" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05f360fc0b24296329c78fda852a1e9ae82de9cf7b27dae4b7f62f118f77b9ed" +dependencies = [ + "tap", +] + [[package]] name = "yansi" version = "0.5.1" diff --git a/kairos-tx/Cargo.toml b/kairos-tx/Cargo.toml index c7efc173..e63d4745 100644 --- a/kairos-tx/Cargo.toml +++ b/kairos-tx/Cargo.toml @@ -4,3 +4,6 @@ version.workspace = true edition.workspace = true [lib] + +[dependencies] +rasn = { version = "0.12.5", default-features = false, features = ["macros"] } From 44c204b5ac8149ccc989db1af9e75b0c40dd373e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:10:02 +0100 Subject: [PATCH 04/15] Install `thiserror` dependency. --- Cargo.lock | 9 +++++---- kairos-tx/Cargo.toml | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d372829e..882b5583 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -741,6 +741,7 @@ name = "kairos-tx" version = "0.1.0" dependencies = [ "rasn", + "thiserror", ] [[package]] @@ -1497,18 +1498,18 @@ dependencies = [ [[package]] name = "thiserror" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad" +checksum = "1e45bcbe8ed29775f228095caf2cd67af7a4ccf756ebff23a306bf3e8b47b24b" dependencies = [ "thiserror-impl", ] [[package]] name = "thiserror-impl" -version = "1.0.56" +version = "1.0.57" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471" +checksum = "a953cb265bef375dae3de6663da4d3804eee9682ea80d8e2542529b73c531c81" dependencies = [ "proc-macro2", "quote", diff --git a/kairos-tx/Cargo.toml b/kairos-tx/Cargo.toml index e63d4745..e31c3575 100644 --- a/kairos-tx/Cargo.toml +++ b/kairos-tx/Cargo.toml @@ -7,3 +7,4 @@ edition.workspace = true [dependencies] rasn = { version = "0.12.5", default-features = false, features = ["macros"] } +thiserror = "1.0.57" From aa3bf4596e13d7e2e357eec5a0b8b38f7e657fec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:18:37 +0100 Subject: [PATCH 05/15] Encode/decode payload with DER. Fully deterministic codec. --- kairos-tx/src/asn.rs | 73 ++++++++++++++++++++++++++++++++++++++++++ kairos-tx/src/error.rs | 13 ++++++++ kairos-tx/src/lib.rs | 2 ++ 3 files changed, 88 insertions(+) create mode 100644 kairos-tx/src/asn.rs create mode 100644 kairos-tx/src/error.rs diff --git a/kairos-tx/src/asn.rs b/kairos-tx/src/asn.rs new file mode 100644 index 00000000..37573d6a --- /dev/null +++ b/kairos-tx/src/asn.rs @@ -0,0 +1,73 @@ +use crate::error::TxError; + +// Expose types for the public API. +pub use rasn::types::{Integer, OctetString}; + +use rasn::types::AsnType; +use rasn::{Decode, Encode}; + +#[derive(AsnType, Encode, Decode, Debug)] +#[rasn(delegate)] +pub struct PublicKey(pub(crate) OctetString); + +#[derive(AsnType, Encode, Decode, Debug)] +#[rasn(delegate)] +pub struct Amount(pub(crate) Integer); + +#[derive(AsnType, Encode, Decode, Debug)] +#[rasn(delegate)] +pub struct Nonce(pub(crate) Integer); + +#[derive(AsnType, Encode, Decode, Debug)] +#[non_exhaustive] +pub struct SigningPayload { + pub nonce: Nonce, + pub body: TransactionBody, +} + +#[derive(AsnType, Encode, Decode, Debug)] +#[rasn(choice)] +#[non_exhaustive] +pub enum TransactionBody { + #[rasn(tag(0))] + Deposit(Deposit), + #[rasn(tag(1))] + Transfer(Transfer), + #[rasn(tag(2))] + Withdrawal(Withdrawal), +} + +#[derive(AsnType, Encode, Decode, Debug)] +#[non_exhaustive] +pub struct Deposit { + pub amount: Amount, +} + +#[derive(AsnType, Encode, Decode, Debug)] +#[non_exhaustive] +pub struct Transfer { + pub recipient: PublicKey, + pub amount: Amount, +} + +#[derive(AsnType, Encode, Decode, Debug)] +#[non_exhaustive] +pub struct Withdrawal { + pub amount: Amount, +} + +impl TryFrom<&[u8]> for SigningPayload { + type Error = TxError; + + fn try_from(value: &[u8]) -> Result { + rasn::der::decode(value).map_err(TxError::DecodeError) + } +} + +impl TryFrom for Vec { + type Error = TxError; + + fn try_from(value: SigningPayload) -> Result { + rasn::der::encode(&value).map_err(TxError::EncodeError) + } +} diff --git a/kairos-tx/src/error.rs b/kairos-tx/src/error.rs new file mode 100644 index 00000000..b770d9c2 --- /dev/null +++ b/kairos-tx/src/error.rs @@ -0,0 +1,13 @@ +use rasn::error::{DecodeError, EncodeError}; +use thiserror::Error; + +#[derive(Error, Debug)] +pub enum TxError { + /// Errors related to encoding. + #[error("encode error: {0}")] + EncodeError(EncodeError), + + /// Errors related to decoding. + #[error("decode error: {0}")] + DecodeError(DecodeError), +} diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index e69de29b..5ce5c062 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -0,0 +1,2 @@ +pub mod asn; +pub mod error; From 50a2df6dff79c26fe8baa8d3eac83f8669628c9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:28:01 +0100 Subject: [PATCH 06/15] Create payload helpers for deposit/transfer/withdrawal. --- kairos-tx/src/helpers.rs | 43 ++++++++++++++++++++++++++++++++++++++++ kairos-tx/src/lib.rs | 1 + 2 files changed, 44 insertions(+) create mode 100644 kairos-tx/src/helpers.rs diff --git a/kairos-tx/src/helpers.rs b/kairos-tx/src/helpers.rs new file mode 100644 index 00000000..4107b382 --- /dev/null +++ b/kairos-tx/src/helpers.rs @@ -0,0 +1,43 @@ +use crate::asn::{ + Amount, Deposit, Nonce, PublicKey, SigningPayload, TransactionBody, Transfer, Withdrawal, +}; +use crate::asn::{Integer, OctetString}; +use crate::error::TxError; + +pub fn make_deposit(nonce: u64, amount: u64) -> Result, TxError> { + create_payload( + nonce, + TransactionBody::Deposit(Deposit { + amount: Amount(Integer::from(amount)), + }), + ) +} + +pub fn make_transfer(nonce: u64, recipient: &[u8], amount: u64) -> Result, TxError> { + create_payload( + nonce, + TransactionBody::Transfer(Transfer { + recipient: PublicKey(OctetString::copy_from_slice(recipient)), + amount: Amount(Integer::from(amount)), + }), + ) +} + +pub fn make_withdrawal(nonce: u64, amount: u64) -> Result, TxError> { + create_payload( + nonce, + TransactionBody::Withdrawal(Withdrawal { + amount: Amount(Integer::from(amount)), + }), + ) +} + +// Generic function to create and serialize a payload. +fn create_payload(nonce: u64, body: TransactionBody) -> Result, TxError> { + let payload = SigningPayload { + nonce: Nonce(Integer::from(nonce)), + body, + }; + + payload.try_into() +} diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 5ce5c062..848c6a2e 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,2 +1,3 @@ pub mod asn; +pub mod helpers; pub mod error; From 2161d460feae2d40c7a2ac88e1b700f6281dd579 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:28:34 +0100 Subject: [PATCH 07/15] Add tests for payload encoding. Tests include comments with ASN.1 data explanation. --- kairos-tx/src/asn.rs | 60 ++++++++++++++++++++++++++++++++++++++++++++ kairos-tx/src/lib.rs | 2 +- 2 files changed, 61 insertions(+), 1 deletion(-) diff --git a/kairos-tx/src/asn.rs b/kairos-tx/src/asn.rs index 37573d6a..4c189f8d 100644 --- a/kairos-tx/src/asn.rs +++ b/kairos-tx/src/asn.rs @@ -71,3 +71,63 @@ impl TryFrom for Vec { rasn::der::encode(&value).map_err(TxError::EncodeError) } } + +#[cfg(test)] +mod tests { + use crate::helpers::{make_deposit, make_transfer, make_withdrawal}; + + #[test] + fn test_encode_deposit() { + const NONCE: u64 = 1; + const AMOUNT: u64 = 1000; + let encoded = make_deposit(NONCE, AMOUNT).unwrap(); + + assert_eq!( + encoded, + vec![ + 0b00110000, // T: 0b00 <- universal, 0b1 <- constructed, 0b10000 (16) <- SEQUENCE tag + 0b00001001, // L: 0b0 <- short form, 0b0001001 (9) <- length + 0b00000010, // T: 0b00 <- universal, 0b0 <- primitive, 0b00010 (2) <- INTEGER tag + 0b00000001, // L: 0b0 <- short form, 0b0000001 (1) <- length + 0b00000001, // V: 0b00000001 (1) <- value + 0b10100000, // T: 0b10 <- context-specific, 0b1 <- constructed, 0b00000 (0) <- CHOICE index + 0b00000100, // L: 0b0 <- short form, 0b0000100 (4) <- length + 0b00000010, // T: 0b00 <- universal, 0b0 <- primitive, 0b00010 (2) <- INTEGER tag + 0b00000010, // L: 0b0 <- short form, 0b0000010 (2) <- length + 0b00000011, // V: 512 + 256 + + 0b11101000, // 128 + 64 + 32 + 8 = 1000 <- value + ] + ); + } + + #[test] + fn test_encode_transfer() { + const NONCE: u64 = 1; + const RECIPIENT: [u8; 32] = [11; 32]; + const AMOUNT: u64 = 1000; + let encoded = make_transfer(NONCE, &RECIPIENT, AMOUNT).unwrap(); + + assert_eq!( + encoded, + vec![ + 0x30, 0x2B, // SEQUENCE (43 bytes) + 0x02, 0x01, 0x01, // INTEGER (1 byte), value = 1 + 0xA1, 0x26, // CHOICE (38 bytes), index = 1 (transfer body) + 0x04, 0x20, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, + 0x0B, 0x0B, 0x0B, 0x0B, 0x0B, + 0x0B, // OCTET STRING (32 bytes), value = [11; 32] + 0x02, 0x02, 0x03, 0xE8 // INTEGER (2 bytes), value = 1000 + ] + ); + } + + #[test] + fn test_encode_withdrawal() { + const NONCE: u64 = 1; + const AMOUNT: u64 = 1000; + let encoded = make_withdrawal(NONCE, AMOUNT).unwrap(); + + assert_eq!(encoded, vec![48, 9, 2, 1, 1, 162, 4, 2, 2, 3, 232]); + } +} diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 848c6a2e..930a804f 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,3 +1,3 @@ pub mod asn; -pub mod helpers; pub mod error; +pub mod helpers; From 587e92767e95830d623af9c4e3fc762b1060abc6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:30:05 +0100 Subject: [PATCH 08/15] Install `num-traits` dependency. --- Cargo.lock | 5 +++-- kairos-tx/Cargo.toml | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 882b5583..63a66f4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -740,6 +740,7 @@ dependencies = [ name = "kairos-tx" version = "0.1.0" dependencies = [ + "num-traits", "rasn", "thiserror", ] @@ -909,9 +910,9 @@ dependencies = [ [[package]] name = "num-traits" -version = "0.2.17" +version = "0.2.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "39e3200413f237f41ab11ad6d161bc7239c84dcb631773ccd7de3dfe4b5c267c" +checksum = "da0df0e5185db44f69b44f26786fe401b6c293d1907744beaa7fa62b2e5a517a" dependencies = [ "autocfg", "libm", diff --git a/kairos-tx/Cargo.toml b/kairos-tx/Cargo.toml index e31c3575..5123d60a 100644 --- a/kairos-tx/Cargo.toml +++ b/kairos-tx/Cargo.toml @@ -6,5 +6,6 @@ edition.workspace = true [lib] [dependencies] +num-traits = "0.2.18" rasn = { version = "0.12.5", default-features = false, features = ["macros"] } thiserror = "1.0.57" From 820cce0d928fc5c3ea11c89389acae5ec43a89b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:33:24 +0100 Subject: [PATCH 09/15] Add parsers for ASN fields: `recipient`, `amount` and `nonce`. Used to convert ASN data into native type, and perform constraint validation. --- kairos-tx/src/error.rs | 4 ++++ kairos-tx/src/lib.rs | 1 + kairos-tx/src/parsers.rs | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 kairos-tx/src/parsers.rs diff --git a/kairos-tx/src/error.rs b/kairos-tx/src/error.rs index b770d9c2..ba1607cb 100644 --- a/kairos-tx/src/error.rs +++ b/kairos-tx/src/error.rs @@ -10,4 +10,8 @@ pub enum TxError { /// Errors related to decoding. #[error("decode error: {0}")] DecodeError(DecodeError), + + /// Constraint violation for a specific field. + #[error("constraint violated for '{field}'")] + ConstraintViolation { field: &'static str }, } diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 930a804f..66735511 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,3 +1,4 @@ pub mod asn; pub mod error; pub mod helpers; +pub mod parsers; diff --git a/kairos-tx/src/parsers.rs b/kairos-tx/src/parsers.rs new file mode 100644 index 00000000..690a14c3 --- /dev/null +++ b/kairos-tx/src/parsers.rs @@ -0,0 +1,40 @@ +use crate::asn; +use crate::error::TxError; + +use num_traits::cast::ToPrimitive; + +// Converts an ASN.1 decoded public key into raw byte representation. +impl From for Vec { + fn from(value: asn::PublicKey) -> Self { + value.0.into() + } +} + +// Attempts to convert an ASN.1 decoded amount (which is represented as a big integer) +// into a `u64`. This conversion can fail if the decoded value is outside the `u64` range, +// thereby enforcing the specified ASN.1 constraints on the value: `INTEGER +// (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: asn::Amount) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "amount" }) + } +} + +// Similar to `asn::Amount`, attempts to convert an ASN.1 decoded nonce into a `u64`. +// This is crucial for validating that the nonce adheres to expected constraint: +// `INTEGER (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: asn::Nonce) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "nonce" }) + } +} From 2e651b1d2123bb6001d03e4e00dd71cdec55cde7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:44:44 +0100 Subject: [PATCH 10/15] Create mocked cryptography integration. Example of signature verification. --- kairos-tx/src/crypto.rs | 55 +++++++++++++++++++++++++++++++++++++++++ kairos-tx/src/error.rs | 4 +++ kairos-tx/src/lib.rs | 1 + 3 files changed, 60 insertions(+) create mode 100644 kairos-tx/src/crypto.rs diff --git a/kairos-tx/src/crypto.rs b/kairos-tx/src/crypto.rs new file mode 100644 index 00000000..171637fa --- /dev/null +++ b/kairos-tx/src/crypto.rs @@ -0,0 +1,55 @@ +use crate::error::TxError; + +/// This module provides mocked cryptographic functions. +/// TODO: Remove when the actual `kairos-crypto` package becomes available. +/// + +/// Signs the given data using a mock mechanism. +pub fn sign(data: &[u8], _private_key: &[u8], public_key: &[u8]) -> Vec { + // Note: Private key is unused in the mock implementation but included for interface compatibility. + // Mock signature combines data and public_key for simplicity. + [data, public_key].concat() +} + +/// Verifies a mock signature against the given data and public key. +pub fn verify(data: &[u8], signature: &[u8], public_key: &[u8]) -> Result<(), TxError> { + // Re-construct the expected signature using the mock `sign` function logic + let expected_signature = sign(data, &[], public_key); + + // Check if the provided signature matches the expected signature + if signature != expected_signature.as_slice() { + return Err(TxError::InvalidSignature); + } + + Ok(()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_sign_and_verify_success() { + let data = b"test data"; + let private_key = b"private_key"; + let public_key = b"public_key"; + + let signature = sign(data, private_key, public_key); + + // This should succeed since we're using the correct public key + assert!(verify(data, &signature, public_key).is_ok()); + } + + #[test] + fn test_verify_failure_due_to_incorrect_public_key() { + let data = b"test data"; + let private_key = b"private_key"; + let public_key = b"public_key"; + let incorrect_public_key = b"incorrect_public_key"; + + let signature = sign(data, private_key, public_key); + + // This should fail since the public key does not match + assert!(verify(data, &signature, incorrect_public_key).is_err()); + } +} diff --git a/kairos-tx/src/error.rs b/kairos-tx/src/error.rs index ba1607cb..bc5e98a5 100644 --- a/kairos-tx/src/error.rs +++ b/kairos-tx/src/error.rs @@ -14,4 +14,8 @@ pub enum TxError { /// Constraint violation for a specific field. #[error("constraint violated for '{field}'")] ConstraintViolation { field: &'static str }, + + /// Signature verification failure. + #[error("signature verification failed")] + InvalidSignature, } diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 66735511..a242f4f8 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,4 +1,5 @@ pub mod asn; +pub mod crypto; pub mod error; pub mod helpers; pub mod parsers; From d9672c219d6804f1669912246d23ce94eb9b36b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 4 Mar 2024 12:46:07 +0100 Subject: [PATCH 11/15] Allow to skip signature verification with ENV. Signature will not be verified if relevant env is set: ``` KAIROS_SKIP_SIGNATURE=true ``` --- kairos-tx/src/crypto.rs | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/kairos-tx/src/crypto.rs b/kairos-tx/src/crypto.rs index 171637fa..1be64085 100644 --- a/kairos-tx/src/crypto.rs +++ b/kairos-tx/src/crypto.rs @@ -1,3 +1,5 @@ +use std::env; + use crate::error::TxError; /// This module provides mocked cryptographic functions. @@ -11,8 +13,22 @@ pub fn sign(data: &[u8], _private_key: &[u8], public_key: &[u8]) -> Vec { [data, public_key].concat() } +// Name of ENV variable that can disable signature check. +// NOTE: This is useful for testing purposes. +pub const SKIP_SIGNATURE_ENV_VAR: &str = "KAIROS_SKIP_SIGNATURE"; + /// Verifies a mock signature against the given data and public key. +/// If the ENV variable "KAIROS_SKIP_SIGNATURE" is set to "true", +/// the verification step will be skipped. pub fn verify(data: &[u8], signature: &[u8], public_key: &[u8]) -> Result<(), TxError> { + // Skip verification if disabled by the environment variable. + if env::var(SKIP_SIGNATURE_ENV_VAR) + .map(|v| v == "true") + .unwrap_or(false) + { + return Ok(()); + } + // Re-construct the expected signature using the mock `sign` function logic let expected_signature = sign(data, &[], public_key); From 99dc80003b523d846dca6d4ec6de558f4af6c5de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 11 Mar 2024 14:37:32 +0100 Subject: [PATCH 12/15] Remove unused crypto module. It was used for testing integration with `kairos-server`, but should not be included in this branch. --- kairos-tx/src/crypto.rs | 71 ----------------------------------------- kairos-tx/src/lib.rs | 1 - 2 files changed, 72 deletions(-) delete mode 100644 kairos-tx/src/crypto.rs diff --git a/kairos-tx/src/crypto.rs b/kairos-tx/src/crypto.rs deleted file mode 100644 index 1be64085..00000000 --- a/kairos-tx/src/crypto.rs +++ /dev/null @@ -1,71 +0,0 @@ -use std::env; - -use crate::error::TxError; - -/// This module provides mocked cryptographic functions. -/// TODO: Remove when the actual `kairos-crypto` package becomes available. -/// - -/// Signs the given data using a mock mechanism. -pub fn sign(data: &[u8], _private_key: &[u8], public_key: &[u8]) -> Vec { - // Note: Private key is unused in the mock implementation but included for interface compatibility. - // Mock signature combines data and public_key for simplicity. - [data, public_key].concat() -} - -// Name of ENV variable that can disable signature check. -// NOTE: This is useful for testing purposes. -pub const SKIP_SIGNATURE_ENV_VAR: &str = "KAIROS_SKIP_SIGNATURE"; - -/// Verifies a mock signature against the given data and public key. -/// If the ENV variable "KAIROS_SKIP_SIGNATURE" is set to "true", -/// the verification step will be skipped. -pub fn verify(data: &[u8], signature: &[u8], public_key: &[u8]) -> Result<(), TxError> { - // Skip verification if disabled by the environment variable. - if env::var(SKIP_SIGNATURE_ENV_VAR) - .map(|v| v == "true") - .unwrap_or(false) - { - return Ok(()); - } - - // Re-construct the expected signature using the mock `sign` function logic - let expected_signature = sign(data, &[], public_key); - - // Check if the provided signature matches the expected signature - if signature != expected_signature.as_slice() { - return Err(TxError::InvalidSignature); - } - - Ok(()) -} - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_sign_and_verify_success() { - let data = b"test data"; - let private_key = b"private_key"; - let public_key = b"public_key"; - - let signature = sign(data, private_key, public_key); - - // This should succeed since we're using the correct public key - assert!(verify(data, &signature, public_key).is_ok()); - } - - #[test] - fn test_verify_failure_due_to_incorrect_public_key() { - let data = b"test data"; - let private_key = b"private_key"; - let public_key = b"public_key"; - let incorrect_public_key = b"incorrect_public_key"; - - let signature = sign(data, private_key, public_key); - - // This should fail since the public key does not match - assert!(verify(data, &signature, incorrect_public_key).is_err()); - } -} diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index a242f4f8..66735511 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,5 +1,4 @@ pub mod asn; -pub mod crypto; pub mod error; pub mod helpers; pub mod parsers; From a7698115069dfaadd7f0518a50bd2ab69424fe6d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Bro=C5=84ski?= Date: Mon, 11 Mar 2024 14:50:28 +0100 Subject: [PATCH 13/15] Place 'parsers' (or rather converters) directly in ASN schema. --- kairos-tx/src/asn.rs | 37 +++++++++++++++++++++++++++++++++++++ kairos-tx/src/lib.rs | 1 - kairos-tx/src/parsers.rs | 40 ---------------------------------------- 3 files changed, 37 insertions(+), 41 deletions(-) delete mode 100644 kairos-tx/src/parsers.rs diff --git a/kairos-tx/src/asn.rs b/kairos-tx/src/asn.rs index 4c189f8d..1e695455 100644 --- a/kairos-tx/src/asn.rs +++ b/kairos-tx/src/asn.rs @@ -3,6 +3,7 @@ use crate::error::TxError; // Expose types for the public API. pub use rasn::types::{Integer, OctetString}; +use num_traits::cast::ToPrimitive; use rasn::types::AsnType; use rasn::{Decode, Encode}; @@ -10,14 +11,50 @@ use rasn::{Decode, Encode}; #[rasn(delegate)] pub struct PublicKey(pub(crate) OctetString); +// Converts an ASN.1 decoded public key into raw byte representation. +impl From for Vec { + fn from(value: PublicKey) -> Self { + value.0.into() + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[rasn(delegate)] pub struct Amount(pub(crate) Integer); +// Attempts to convert an ASN.1 decoded amount (which is represented as a big integer) +// into a `u64`. This conversion can fail if the decoded value is outside the `u64` range, +// thereby enforcing the specified ASN.1 constraints on the value: `INTEGER +// (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: Amount) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "amount" }) + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[rasn(delegate)] pub struct Nonce(pub(crate) Integer); +// Similar to `Amount`, attempts to convert an ASN.1 decoded nonce into a `u64`. +// This is crucial for validating that the nonce adheres to expected constraint: +// `INTEGER (0..18446744073709551615)`. +impl TryFrom for u64 { + type Error = TxError; + + fn try_from(value: Nonce) -> Result { + value + .0 + .to_u64() + .ok_or(TxError::ConstraintViolation { field: "nonce" }) + } +} + #[derive(AsnType, Encode, Decode, Debug)] #[non_exhaustive] pub struct SigningPayload { diff --git a/kairos-tx/src/lib.rs b/kairos-tx/src/lib.rs index 66735511..930a804f 100644 --- a/kairos-tx/src/lib.rs +++ b/kairos-tx/src/lib.rs @@ -1,4 +1,3 @@ pub mod asn; pub mod error; pub mod helpers; -pub mod parsers; diff --git a/kairos-tx/src/parsers.rs b/kairos-tx/src/parsers.rs deleted file mode 100644 index 690a14c3..00000000 --- a/kairos-tx/src/parsers.rs +++ /dev/null @@ -1,40 +0,0 @@ -use crate::asn; -use crate::error::TxError; - -use num_traits::cast::ToPrimitive; - -// Converts an ASN.1 decoded public key into raw byte representation. -impl From for Vec { - fn from(value: asn::PublicKey) -> Self { - value.0.into() - } -} - -// Attempts to convert an ASN.1 decoded amount (which is represented as a big integer) -// into a `u64`. This conversion can fail if the decoded value is outside the `u64` range, -// thereby enforcing the specified ASN.1 constraints on the value: `INTEGER -// (0..18446744073709551615)`. -impl TryFrom for u64 { - type Error = TxError; - - fn try_from(value: asn::Amount) -> Result { - value - .0 - .to_u64() - .ok_or(TxError::ConstraintViolation { field: "amount" }) - } -} - -// Similar to `asn::Amount`, attempts to convert an ASN.1 decoded nonce into a `u64`. -// This is crucial for validating that the nonce adheres to expected constraint: -// `INTEGER (0..18446744073709551615)`. -impl TryFrom for u64 { - type Error = TxError; - - fn try_from(value: asn::Nonce) -> Result { - value - .0 - .to_u64() - .ok_or(TxError::ConstraintViolation { field: "nonce" }) - } -} From e22df5a9a2569bbf9297ee8e0f3505b1d91fd261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Mon, 18 Mar 2024 10:34:58 -0500 Subject: [PATCH 14/15] nixos/tests: test kairos-cli in end-to-end test --- nixos/default.nix | 1 + nixos/tests/end-to-end.nix | 14 +++++++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/nixos/default.nix b/nixos/default.nix index 02890ffb..1757047e 100644 --- a/nixos/default.nix +++ b/nixos/default.nix @@ -30,6 +30,7 @@ in ./tests/end-to-end.nix { inherit mkKairosHostConfig; + inherit (self.packages."x86_64-linux") kairos; }; }; nixosModules = { diff --git a/nixos/tests/end-to-end.nix b/nixos/tests/end-to-end.nix index 3611cb55..98d6396a 100644 --- a/nixos/tests/end-to-end.nix +++ b/nixos/tests/end-to-end.nix @@ -1,5 +1,7 @@ { nixosTest , mkKairosHostConfig +, kairos +, testResources ? ../../kairos-cli/tests/fixtures }: nixosTest { name = "kairos e2e test"; @@ -18,7 +20,7 @@ nixosTest { }; client = { config, pkgs, nodes, ... }: { - environment.systemPackages = [ pkgs.curl ]; + environment.systemPackages = [ pkgs.curl kairos ]; }; }; @@ -41,5 +43,15 @@ nixosTest { withdraw_request = { "public_key": "publickey", "signature": "signature", "amount": 10 } client.succeed("curl -X POST http://kairos/api/v1/withdraw -H 'Content-Type: application/json' -d '{}'".format(json.dumps(withdraw_request))) + + cli_output = client.succeed("kairos-cli deposit --amount 1000 --private-key ${testResources}/ed25519/secret_key.pem") + assert "ok\n" in cli_output + + cli_output = client.succeed("kairos-cli transfer --recipient '01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356' --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem") + assert "ok\n" in cli_output + + cli_output = client.succeed("kairos-cli withdraw --amount 1000 --private-key ${testResources}/ed25519/secret_key.pem") + assert "ok\n" in cli_output ''; } + From 7fd52ce3a2dbf074b8b035c5f3959c4418e8d6e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marijan=20Petri=C4=8Devi=C4=87?= Date: Tue, 19 Mar 2024 11:22:16 -0500 Subject: [PATCH 15/15] nixos/tests: test both ed25519 and secp256k1 in e2e --- nixos/tests/end-to-end.nix | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/nixos/tests/end-to-end.nix b/nixos/tests/end-to-end.nix index 98d6396a..335fa8b4 100644 --- a/nixos/tests/end-to-end.nix +++ b/nixos/tests/end-to-end.nix @@ -36,6 +36,7 @@ nixosTest { client.wait_for_unit ("multi-user.target") deposit_request = { "public_key": "publickey", "amount": 10 } + # REST API client.succeed("curl -X POST http://kairos/api/v1/deposit -H 'Content-Type: application/json' -d '{}'".format(json.dumps(deposit_request))) transfer_request = { "from": "publickey", "signature": "signature", "to": "publickey", "amount": 10 } @@ -44,14 +45,25 @@ nixosTest { withdraw_request = { "public_key": "publickey", "signature": "signature", "amount": 10 } client.succeed("curl -X POST http://kairos/api/v1/withdraw -H 'Content-Type: application/json' -d '{}'".format(json.dumps(withdraw_request))) + # CLI with ed25519 cli_output = client.succeed("kairos-cli deposit --amount 1000 --private-key ${testResources}/ed25519/secret_key.pem") assert "ok\n" in cli_output - cli_output = client.succeed("kairos-cli transfer --recipient '01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356' --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem") + cli_output = client.succeed("kairos-cli transfer --recipient '01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356' --amount 1000 --private-key ${testResources}/ed25519/secret_key.pem") assert "ok\n" in cli_output cli_output = client.succeed("kairos-cli withdraw --amount 1000 --private-key ${testResources}/ed25519/secret_key.pem") assert "ok\n" in cli_output + + # CLI with secp256k1 + cli_output = client.succeed("kairos-cli deposit --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem") + assert "ok\n" in cli_output + + cli_output = client.succeed("kairos-cli transfer --recipient '01a26419a7d82b2263deaedea32d35eee8ae1c850bd477f62a82939f06e80df356' --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem") + assert "ok\n" in cli_output + + cli_output = client.succeed("kairos-cli withdraw --amount 1000 --private-key ${testResources}/secp256k1/secret_key.pem") + assert "ok\n" in cli_output ''; }