Skip to content

Commit 54114f0

Browse files
Feature/explicit gateway addressing (#239)
* validator: fixing a warning, untestify this when you need it for real code * webassembly: minor readme changes. * README changes in wasm * Updated wasm version * clients/webassembly: security vuln updates * typo fix * WIP commit * Significantly simplified the API * Changed switch to have default branch * Managed to get rid of `this` bind * Moved 'Recipient' definition * Examples update * Slightly more generalised 'try_from_string' for recipient * Updated to use client@gateway addressing * Updated tests Co-authored-by: Dave Hrycyszyn <futurechimp@users.noreply.github.com>
1 parent 8745fb4 commit 54114f0

File tree

16 files changed

+174
-148
lines changed

16 files changed

+174
-148
lines changed

clients/desktop/examples/websocket_filesend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use futures::{SinkExt, StreamExt};
2-
use nym_client::client::Recipient;
32
use nym_client::websocket::{BinaryClientRequest, ClientRequest, ServerResponse};
3+
use nymsphinx::addressing::clients::Recipient;
44
use std::convert::TryFrom;
55
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
66

clients/desktop/examples/websocket_textsend.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use futures::{SinkExt, StreamExt};
22
use nym_client::websocket::{ClientRequest, ServerResponse};
33
use std::convert::TryFrom;
44
use tokio_tungstenite::{connect_async, tungstenite::protocol::Message};
5+
56
#[tokio::main]
67
async fn main() {
78
let message = "Hello Nym!".to_string();

clients/desktop/src/client/cover_traffic_stream.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,12 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::client::inbound_messages::Recipient;
1615
use crate::client::mix_traffic::{MixMessage, MixMessageSender};
1716
use crate::client::topology_control::TopologyAccessor;
1817
use futures::task::{Context, Poll};
1918
use futures::{Future, Stream, StreamExt};
2019
use log::*;
20+
use nymsphinx::addressing::clients::Recipient;
2121
use nymsphinx::utils::{encapsulation, poisson};
2222
use std::pin::Pin;
2323
use std::time::Duration;
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
use futures::channel::mpsc;
2-
use nymsphinx::{
3-
DestinationAddressBytes, Error, NodeAddressBytes, DESTINATION_ADDRESS_LENGTH,
4-
NODE_ADDRESS_LENGTH,
5-
};
2+
use nymsphinx::addressing::clients::Recipient;
63

74
pub(crate) type InputMessageSender = mpsc::UnboundedSender<InputMessage>;
85
pub(crate) type InputMessageReceiver = mpsc::UnboundedReceiver<InputMessage>;
@@ -23,83 +20,3 @@ impl InputMessage {
2320
(self.recipient, self.data)
2421
}
2522
}
26-
27-
#[derive(Debug)]
28-
pub struct RecipientFormattingError;
29-
30-
impl From<nymsphinx::Error> for RecipientFormattingError {
31-
fn from(_: Error) -> Self {
32-
Self
33-
}
34-
}
35-
36-
// TODO: this should a different home... somewhere, but where?
37-
#[derive(Clone, Debug)]
38-
pub struct Recipient {
39-
destination: DestinationAddressBytes,
40-
gateway: NodeAddressBytes,
41-
}
42-
43-
impl Recipient {
44-
pub const LEN: usize = DESTINATION_ADDRESS_LENGTH + NODE_ADDRESS_LENGTH;
45-
46-
pub fn new(destination: DestinationAddressBytes, gateway: NodeAddressBytes) -> Self {
47-
Recipient {
48-
destination,
49-
gateway,
50-
}
51-
}
52-
53-
pub fn destination(&self) -> DestinationAddressBytes {
54-
self.destination.clone()
55-
}
56-
57-
pub fn gateway(&self) -> NodeAddressBytes {
58-
self.gateway.clone()
59-
}
60-
61-
pub fn into_bytes(self) -> [u8; Self::LEN] {
62-
let mut out = [0u8; Self::LEN];
63-
out[..DESTINATION_ADDRESS_LENGTH].copy_from_slice(self.destination.as_bytes());
64-
out[DESTINATION_ADDRESS_LENGTH..].copy_from_slice(self.gateway.as_bytes());
65-
66-
out
67-
}
68-
69-
pub fn from_bytes(bytes: [u8; Self::LEN]) -> Self {
70-
let mut destination_bytes = [0u8; DESTINATION_ADDRESS_LENGTH];
71-
destination_bytes.copy_from_slice(&bytes[..DESTINATION_ADDRESS_LENGTH]);
72-
73-
let mut gateway_address_bytes = [0u8; NODE_ADDRESS_LENGTH];
74-
gateway_address_bytes.copy_from_slice(&bytes[DESTINATION_ADDRESS_LENGTH..]);
75-
76-
let destination = DestinationAddressBytes::from_bytes(destination_bytes);
77-
let gateway = NodeAddressBytes::from_bytes(gateway_address_bytes);
78-
79-
Self {
80-
destination,
81-
gateway,
82-
}
83-
}
84-
85-
pub fn try_from_string(full_address: String) -> Result<Self, RecipientFormattingError> {
86-
let split: Vec<_> = full_address.split("@").collect();
87-
if split.len() != 2 {
88-
return Err(RecipientFormattingError);
89-
}
90-
let destination = DestinationAddressBytes::try_from_base58_string(split[0])?;
91-
let gateway = NodeAddressBytes::try_from_base58_string(split[1])?;
92-
Ok(Recipient {
93-
destination,
94-
gateway,
95-
})
96-
}
97-
98-
pub fn to_string(&self) -> String {
99-
format!(
100-
"{}@{}",
101-
self.destination.to_base58_string(),
102-
self.gateway.to_base58_string()
103-
)
104-
}
105-
}

clients/desktop/src/client/mod.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use futures::channel::mpsc;
2929
use gateway_client::{GatewayClient, SphinxPacketReceiver, SphinxPacketSender};
3030
use gateway_requests::auth_token::AuthToken;
3131
use log::*;
32+
use nymsphinx::addressing::clients::Recipient;
3233
use nymsphinx::chunking::split_and_prepare_payloads;
3334
use nymsphinx::NodeAddressBytes;
3435
use received_buffer::{ReceivedBufferMessage, ReconstructedMessagesReceiver};
@@ -42,11 +43,6 @@ mod real_traffic_stream;
4243
pub(crate) mod received_buffer;
4344
pub(crate) mod topology_control;
4445

45-
// I'm not sure if that is the right place for it to live, but I could not find a better one...
46-
// (it definitely can't be in websocket, because it's not websocket specific; neither can it just
47-
// live in common/nymsphinx, as it's sphinx related at all, it's only for client-client communication)
48-
pub use inbound_messages::Recipient;
49-
5046
pub struct NymClient {
5147
config: Config,
5248
runtime: Runtime,

clients/desktop/src/client/real_traffic_stream.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::client::inbound_messages::{InputMessage, Recipient};
15+
use crate::client::inbound_messages::InputMessage;
1616
use crate::client::mix_traffic::MixMessage;
1717
use crate::client::topology_control::TopologyAccessor;
1818
use futures::channel::mpsc;
1919
use futures::task::{Context, Poll};
2020
use futures::{Future, Stream, StreamExt};
2121
use log::{error, info, trace, warn};
22+
use nymsphinx::addressing::clients::Recipient;
2223
use nymsphinx::utils::{encapsulation, poisson};
2324
use std::pin::Pin;
2425
use std::time::Duration;

clients/desktop/src/websocket/handler.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
use super::types::{BinaryClientRequest, ClientRequest, ServerResponse};
1616
use crate::client::{
17-
inbound_messages::{InputMessage, InputMessageSender, Recipient},
17+
inbound_messages::{InputMessage, InputMessageSender},
1818
received_buffer::{
1919
ReceivedBufferMessage, ReceivedBufferRequestSender, ReconstructedMessagesReceiver,
2020
},
@@ -23,6 +23,7 @@ use crate::client::{
2323
use futures::channel::mpsc;
2424
use futures::{SinkExt, StreamExt};
2525
use log::*;
26+
use nymsphinx::addressing::clients::Recipient;
2627
use nymsphinx::chunking::split_and_prepare_payloads;
2728
use std::convert::TryFrom;
2829
use tokio::net::TcpStream;

clients/desktop/src/websocket/types.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
use crate::client::inbound_messages::Recipient;
15+
use nymsphinx::addressing::clients::Recipient;
1616
use serde::{Deserialize, Serialize};
1717
use std::convert::TryFrom;
1818
use tokio_tungstenite::tungstenite::protocol::Message;

clients/webassembly/client.js

+4-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ export class Identity {
1111
}
1212

1313
export class Client {
14-
// constructor(gateway_url, ownAddress, registeredCallback) {
1514
constructor(directoryUrl, identity, authToken) {
1615
this.authToken = authToken
1716
this.gateway = null; // {socketAddress, mixAddress, conn}
@@ -20,6 +19,10 @@ export class Client {
2019
this.topologyEndpoint = directoryUrl + "/api/presence/topology";
2120
}
2221

22+
formatAsRecipient() {
23+
return `${this.identity.address}@${this.gateway.mixAddress}`
24+
}
25+
2326
async start() {
2427
await this.updateTopology();
2528
this._getInitialGateway();
@@ -206,7 +209,6 @@ export class Client {
206209
}
207210
}
208211

209-
210212
function makeRegisterRequest(address) {
211213
return JSON.stringify({ "type": "register", "address": address });
212214
}

clients/webassembly/js-example/index.html

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
<body>
1111
<p>
12-
<label for="fname">Our address: </label><input disabled="true" size="40" type="text" id="sender" value="">
12+
<label for="fname">Our address: </label><input disabled="true" size="120" type="text" id="sender" value="">
1313
</p>
1414

1515
<p>
16-
<label for="fname">Recipient address: </label><input size="40" type="text" id="recipient" value="">
16+
<label for="fname">Recipient address: </label><input size="120" type="text" id="recipient" value="">
1717
</p>
1818
<label for="fname">Text to send: </label><input type="text" id="sendtext" value="Hello mixnet!">
19-
<button id="send-button">Send</button><button id="refresh-button">Refresh</button>
19+
<button id="send-button">Send</button>
2020

2121

2222
<p>Send messages to the mixnet using the "send" button.</p>

clients/webassembly/js-example/index.js

+4-5
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,16 @@ import {
1717
Identity
1818
} from "nym-client-wasm/client"
1919

20-
2120
async function main() {
2221
let directory = "https://qa-directory.nymtech.net";
23-
// let identity = new Identity(); // or load one from storage if you have one already
24-
// because I'm about to make a new PR tomorrow, just hardcode it to not recreate client every single recompilation
25-
let identity = { address: "7mVwY9uFRBBTW91dAWaDtuusSpzc16ZwANLSXxXVYT7M", privateKey: "H4cp7DFMsPXD4Qm7ZW3TgsJETr2CpJhxmBJohko7HrDE", publicKey: "7mVwY9uFRBBTW91dAWaDtuusSpzc16ZwANLSXxXVYT7M" }
22+
let identity = new Identity(); // or load one from storage if you have one already
2623

27-
document.getElementById("sender").value = identity.address;
24+
document.getElementById("sender").value = "loading...";
2825

2926
let nymClient = new Client(directory, identity, null); // provide your authToken if you've registered before
27+
nymClient.onEstablishedGatewayConnection = (_) => document.getElementById("sender").value = nymClient.formatAsRecipient() // overwrite default behaviour with our implementation
3028
nymClient.onParsedBlobResponse = displayReceived // overwrite default behaviour with our implementation
29+
nymClient.onErrorResponse = (event) => alert("Received invalid gateway response", event.data)
3130
await nymClient.start();
3231

3332
const sendButton = document.querySelector('#send-button');

clients/webassembly/js-example/package-lock.json

-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)