Skip to content

Commit 4ea75b9

Browse files
committed
update packet
1 parent 8ddbbdd commit 4ea75b9

10 files changed

Lines changed: 157 additions & 38 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/node/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ build = "build.rs"
77
publish = false
88

99
[dependencies]
10+
lib_packet = { version = "*", package = "packet", path = "../../lib/packet" }
11+
lib_bytes = { version = "*", package = "bytes", path = "../../lib/bytes" }
12+
lib_cryptography_algorithm_ed25519 = { version = "*", package = "cryptography_algorithm_ed25519", path = "../../lib/cryptography_algorithm/ed25519" }
1013
kore = { version = "*", path = "../../lib/kore" }
1114
bytes = "*"
1215
rand = "*"
@@ -59,7 +62,6 @@ anyhow = "*"
5962
url = "*"
6063
nanoid = "*"
6164
hex = "*"
62-
6365
ed25519-dalek = { version = "2.1.0", features = ["rand_core"] }
6466

6567
[dev-dependencies]

app/node/src/saga/reservation.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,20 @@ where
5757
match self {
5858
Self::Parse(bytes) => {
5959
let content: Vec<_> = bytes.to_vec();
60-
let content: identity::SignedUnverified = content.try_into().unwrap();
61-
let content: identity::SignedVerified = content.try_into().unwrap();
62-
let (pk, mg) = content.unpack();
60+
let content: bytes::Bytes = content.into();
61+
let content: lib_bytes::NonEmpty = content.try_into().unwrap();
62+
let content: lib_packet::MarkedSignedUnverified<lib_cryptography_algorithm_ed25519::Ed25519Algorithm, sub_system::broker::An> = content.try_into().unwrap();
63+
let content: lib_packet::MarkedSignedVerified<_, _> = content.try_into().unwrap();
64+
let (_, public_key, message) = content.into();
65+
let message: lib_bytes::NonEmpty = message.into();
66+
let message: bytes::Bytes = message.into();
67+
let segments: std::iter::Filter<_, _> = message.split(|byte| byte.is_ascii_whitespace()).filter(|chunk| !chunk.is_empty());
68+
let src: &[u8] = segments.next().unwrap();
69+
let src: libp2p::Multiaddr = src.parse().unwrap();
70+
let dst: &[u8] = segments.next().unwrap();
71+
let dst: libp2p::Multiaddr = dst.parse().unwrap();
72+
73+
6374
let src: &str = segments.get(2).unwrap();
6475
let src: libp2p::Multiaddr = src.parse().unwrap();
6576
let dst: &str = segments.get(3).unwrap();
@@ -73,7 +84,7 @@ where
7384
// state transition
7485
Self::Validation {
7586
key: nanoid::nanoid!(),
76-
pk,
87+
pk: public_key,
7788
src,
7889
dst,
7990
ttl,

lib/cryptography/src/message.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,10 @@ use super::*;
77
#[derive(derive_more::Deref)]
88
#[derive(derive_more::DerefMut)]
99
#[derive(derive_more::From)]
10-
pub struct Message(lib_bytes::NonEmpty);
10+
pub struct Message(lib_bytes::NonEmpty);
11+
12+
impl Into<lib_bytes::NonEmpty> for Message {
13+
fn into(self) -> lib_bytes::NonEmpty {
14+
self.0
15+
}
16+
}

lib/cryptography/src/public_key.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use super::*;
2-
31
#[derive(Debug)]
42
#[derive(Clone)]
53
#[derive(PartialEq)]
@@ -21,4 +19,10 @@ impl<T> From<lib_bytes::NonEmpty> for PublicKey<T> {
2119
content
2220
}
2321
}
22+
}
23+
24+
impl<T> Into<lib_bytes::NonEmpty> for PublicKey<T> {
25+
fn into(self) -> lib_bytes::NonEmpty {
26+
self.content
27+
}
2428
}

lib/cryptography/src/secret_key.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,16 @@ impl<T> From<lib_bytes::NonEmpty> for SecretKey<T> {
2929
content
3030
}
3131
}
32+
}
33+
34+
impl<T> Into<lib_bytes::NonEmpty> for SecretKey<T> {
35+
fn into(self) -> lib_bytes::NonEmpty {
36+
self.content
37+
}
38+
}
39+
40+
impl<T> Into<lib_bytes::NonEmpty> for &SecretKey<T> {
41+
fn into(self) -> lib_bytes::NonEmpty {
42+
self.content.to_owned()
43+
}
3244
}

lib/cryptography/src/signature.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,28 @@
1-
use super::*;
2-
31
#[derive(Debug)]
42
#[derive(Clone)]
53
#[derive(PartialEq)]
64
#[derive(Eq)]
75
#[derive(derive_more::Deref)]
86
#[derive(derive_more::DerefMut)]
9-
#[derive(derive_more::From)]
107
pub struct Signature<T> {
118
phantom_data: std::marker::PhantomData<T>,
129
#[deref]
1310
#[deref_mut]
1411
content: lib_bytes::NonEmpty
12+
}
13+
14+
impl<T> From<lib_bytes::NonEmpty> for Signature<T> {
15+
fn from(value: lib_bytes::NonEmpty) -> Self {
16+
let content: lib_bytes::NonEmpty = value;
17+
Self {
18+
phantom_data: std::marker::PhantomData,
19+
content
20+
}
21+
}
22+
}
23+
24+
impl<T> Into<lib_bytes::NonEmpty> for Signature<T> {
25+
fn into(self) -> lib_bytes::NonEmpty {
26+
self.content
27+
}
1528
}

lib/cryptography_algorithm/ed25519/src/lib.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ const SECRET_KEY_LEN: usize = 32;
66
const SIGNATURE_LEN: usize = 64;
77

88
#[derive(Debug)]
9+
#[derive(Clone)]
910
pub struct Ed25519Algorithm;
1011

1112
impl lib_cryptography::Algorithm for Ed25519Algorithm {}
@@ -36,7 +37,11 @@ impl lib_cryptography::AsymmetricKeyGenAlgorithm for Ed25519Algorithm {
3637

3738
impl lib_cryptography::AsymmetricSignatureAlgorithm for Ed25519Algorithm {
3839
fn sign(secret_key: &lib_cryptography::secret_key::SecretKey<Self>, message: &lib_cryptography::message::Message) -> lib_cryptography::Result<lib_cryptography::signature::Signature<Self>> {
40+
let message: lib_bytes::NonEmpty = message.to_owned().into();
41+
let message: bytes::Bytes = message.into();
3942
let message: &[_] = message.as_ref();
43+
let secret_key: lib_bytes::NonEmpty = secret_key.to_owned().into();
44+
let secret_key: bytes::Bytes = secret_key.into();
4045
let secret_key: [u8; SECRET_KEY_LEN] = secret_key.as_ref().try_into()?;
4146
let signing_key: ed25519_dalek::SigningKey = ed25519_dalek::SigningKey::from_bytes(&secret_key);
4247
let out: ed25519::Signature = signing_key.sign(message);
@@ -48,12 +53,18 @@ impl lib_cryptography::AsymmetricSignatureAlgorithm for Ed25519Algorithm {
4853
}
4954

5055
fn verify(public_key: &lib_cryptography::public_key::PublicKey<Self>, message: &lib_cryptography::message::Message, signature: &lib_cryptography::signature::Signature<Self>) -> lib_cryptography::Result<bool> {
56+
let message: lib_bytes::NonEmpty = message.to_owned().into();
57+
let message: bytes::Bytes = message.into();
5158
let message: &[_] = message.as_ref();
59+
let public_key: lib_bytes::NonEmpty = public_key.to_owned().into();
60+
let public_key: bytes::Bytes = public_key.into();
5261
let public_key: [u8; PUBLIC_KEY_LEN] = public_key.as_ref().try_into()?;
53-
let verifying_key: ed25519_dalek::VerifyingKey = ed25519_dalek::VerifyingKey::from_bytes(&public_key)?;
62+
let signature: lib_bytes::NonEmpty = signature.to_owned().into();
63+
let signature: bytes::Bytes = signature.into();
5464
let signature: &[_; _] = signature.as_ref().try_into()?;
5565
let signature: ed25519::Signature = ed25519_dalek::Signature::from_bytes(signature);
56-
let out: bool = verifying_key.verify(message, &signature).is_ok();
66+
let out: ed25519_dalek::VerifyingKey = ed25519_dalek::VerifyingKey::from_bytes(&public_key)?;
67+
let out: bool = out.verify(message, &signature).is_ok();
5768
Ok(out)
5869
}
5970
}

lib/packet/src/lib.rs

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use bytes::Buf as _;
1+
pub mod opcode;
22

33
pub struct IsUnknownProtocol;
44

@@ -9,7 +9,6 @@ pub struct IsUnknownProtocol;
99
#[derive(derive_more::Deref)]
1010
#[derive(derive_more::DerefMut)]
1111
pub struct MarkedSignedVerified<A, B = IsUnknownProtocol> {
12-
phantom_data: std::marker::PhantomData<B>,
1312
signer: lib_cryptography::public_key::PublicKey<A>,
1413
signature: lib_cryptography::signature::Signature<A>,
1514
#[deref]
@@ -31,7 +30,6 @@ where
3130
}
3231
let content: Unsigned<_> = message.into();
3332
Ok(Self {
34-
phantom_data: std::marker::PhantomData,
3533
signer,
3634
signature,
3735
content
@@ -41,13 +39,35 @@ where
4139

4240
impl<A, B> TryFrom<(Unsigned<B>, &lib_cryptography::secret_key::SecretKey<A>)> for MarkedSignedVerified<A, B>
4341
where
42+
A: lib_cryptography::AsymmetricKeyDerivationAlgorithm,
4443
A: lib_cryptography::AsymmetricSignatureAlgorithm {
4544
type Error = Box<dyn std::error::Error>;
4645

4746
fn try_from(value: (Unsigned<B>, &lib_cryptography::secret_key::SecretKey<A>)) -> Result<Self, Self::Error> {
4847
let (unsigned, secret_key) = value;
49-
let signature = A::sign(&secret_key.as_ref().to_vec().into_boxed_slice(), &unsigned.content.to_vec().into_boxed_slice())?;
48+
let message: lib_bytes::NonEmpty = unsigned.into();
49+
let message: lib_cryptography::message::Message = message.into();
50+
let signer: lib_cryptography::public_key::PublicKey<_> = secret_key.public_key();
51+
let signature: lib_cryptography::signature::Signature<_> = A::sign(&secret_key, &message)?;
52+
let content: Unsigned<_> = message.into();
53+
Ok(Self {
54+
signer,
55+
signature,
56+
content
57+
})
58+
}
59+
}
5060

61+
impl<A, B> TryFrom<lib_bytes::NonEmpty> for MarkedSignedVerified<A, B>
62+
where
63+
A: lib_cryptography::AsymmetricSetLayout,
64+
A: lib_cryptography::AsymmetricSignatureAlgorithm {
65+
type Error = Box<dyn std::error::Error>;
66+
67+
fn try_from(value: lib_bytes::NonEmpty) -> Result<Self, Self::Error> {
68+
let out: MarkedSignedUnverified<A, B> = value.try_into()?;
69+
let out: Self = out.try_into()?;
70+
Ok(out)
5171
}
5272
}
5373

@@ -61,16 +81,13 @@ impl<A, B> Into<(lib_cryptography::signature::Signature<A>, lib_cryptography::pu
6181
}
6282
}
6383

64-
65-
6684
#[derive(Debug)]
6785
#[derive(Clone)]
6886
#[derive(PartialEq)]
6987
#[derive(Eq)]
7088
#[derive(derive_more::Deref)]
7189
#[derive(derive_more::DerefMut)]
7290
pub struct MarkedSignedUnverified<A, B = IsUnknownProtocol> {
73-
phantom_data: std::marker::PhantomData<B>,
7491
signer: lib_cryptography::public_key::PublicKey<A>,
7592
signature: lib_cryptography::signature::Signature<A>,
7693
#[deref]
@@ -95,7 +112,7 @@ where
95112
let mut bytes: bytes::Bytes = value.into();
96113
let header_len: usize = A::PUBLIC_KEY_LEN + A::SIGNATURE_LEN;
97114
if bytes.len() <= header_len {
98-
return Err(<Box<dyn std::error::Error>>::from(String::from("packet too short; insufficient header len")))
115+
return Err(<Box<dyn std::error::Error>>::from(String::from("packet too short; insufficient header length")))
99116
}
100117
let signer: bytes::Bytes = bytes.split_to(A::PUBLIC_KEY_LEN);
101118
let signer: lib_bytes::NonEmpty = signer.try_into()?;
@@ -106,7 +123,6 @@ where
106123
let content: lib_bytes::NonEmpty = bytes.try_into()?;
107124
let content: Unsigned<_> = content.into();
108125
Ok(Self {
109-
phantom_data: std::marker::PhantomData,
110126
signer,
111127
signature,
112128
content
@@ -124,8 +140,17 @@ impl<A, B> Into<(lib_cryptography::signature::Signature<A>, lib_cryptography::pu
124140
}
125141
}
126142

127-
pub struct SignedVerified<A, B = ()> {
128-
143+
#[derive(Debug)]
144+
#[derive(Clone)]
145+
#[derive(PartialEq)]
146+
#[derive(Eq)]
147+
#[derive(derive_more::Deref)]
148+
#[derive(derive_more::DerefMut)]
149+
pub struct SignedVerified<A, B = IsUnknownProtocol> {
150+
signature: lib_cryptography::signature::Signature<A>,
151+
#[deref]
152+
#[deref_mut]
153+
content: Unsigned<B>
129154
}
130155

131156
#[derive(Debug)]
@@ -135,26 +160,12 @@ pub struct SignedVerified<A, B = ()> {
135160
#[derive(derive_more::Deref)]
136161
#[derive(derive_more::DerefMut)]
137162
pub struct SignedUnverified<A, B = IsUnknownProtocol> {
138-
phantom_data: std::marker::PhantomData<B>,
139163
signature: lib_cryptography::signature::Signature<A>,
140164
#[deref]
141165
#[deref_mut]
142166
content: Unsigned<B>
143167
}
144168

145-
impl<A, B> TryFrom<lib_bytes::NonEmpty> for SignedUnverified<A, B> {
146-
type Error = Box<dyn std::error::Error>;
147-
148-
fn try_from(value: lib_bytes::NonEmpty) -> Result<Self, Self::Error> {
149-
150-
}
151-
}
152-
153-
// T designates the protocol the packet belongs to, () for generic, any, or unknown
154-
155-
156-
157-
158169
#[derive(Debug)]
159170
#[derive(Clone)]
160171
#[derive(PartialEq)]

lib/packet/src/opcode.rs

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
use super::*;
2+
3+
pub struct IsFromMarkedSignedVerified;
4+
pub struct IsFromMarkedSignedUnverified;
5+
pub struct IsFromSignedVerified;
6+
pub struct IsFromSignedUnverified;
7+
pub struct IsFromUnsigned;
8+
9+
#[derive(Debug)]
10+
#[derive(Clone)]
11+
pub struct Call<A, B, C = IsUnknownProtocol, D = IsFromUnsigned, E = (), F = ()> {
12+
phantom_data: std::marker::PhantomData<(B, C, D)>,
13+
pkt: A,
14+
signer: E,
15+
signature: F
16+
}
17+
18+
impl<A, B, C> TryFrom<lib_bytes::NonEmpty> for Call<A, B, C, IsFromMarkedSignedVerified, lib_cryptography::public_key::PublicKey<B>, lib_cryptography::signature::Signature<B>>
19+
where
20+
A: TryFrom<Unsigned<C>, Error = Box<dyn std::error::Error>>,
21+
B: lib_cryptography::AsymmetricSetLayout,
22+
B: lib_cryptography::AsymmetricSignatureAlgorithm {
23+
type Error = Box<dyn std::error::Error>;
24+
25+
fn try_from(value: lib_bytes::NonEmpty) -> Result<Self, Self::Error> {
26+
let out: MarkedSignedUnverified<B, C> = value.try_into()?;
27+
let out: MarkedSignedVerified<_, _> = out.try_into()?;
28+
let (signature, signer, message) = out.into();
29+
Ok(Self {
30+
phantom_data: std::marker::PhantomData,
31+
pkt: message.try_into()?,
32+
signer,
33+
signature
34+
})
35+
}
36+
}
37+
38+
impl<A, B, C> Into<(A, lib_cryptography::public_key::PublicKey<B>, lib_cryptography::signature::Signature<B>)> for Call<A, B, C, IsFromMarkedSignedVerified, lib_cryptography::public_key::PublicKey<B>, lib_cryptography::signature::Signature<B>> {
39+
fn into(self) -> (A, lib_cryptography::public_key::PublicKey<B>, lib_cryptography::signature::Signature<B>) {
40+
(
41+
self.pkt,
42+
self.signer,
43+
self.signature
44+
)
45+
}
46+
}

0 commit comments

Comments
 (0)