A possibly-working wasm Sphinx packet.

This commit is contained in:
Dave Hrycyszyn
2020-04-09 16:42:29 +01:00
parent 61e0ec5ab5
commit fd68994e64
2 changed files with 34 additions and 22 deletions
+5 -1
View File
@@ -11,10 +11,14 @@ crate-type = ["cdylib", "rlib"]
default = ["console_error_panic_hook"]
[dependencies]
curve25519-dalek = "2.0.0"
slice_as_array = "1.1.0"
wasm-bindgen = "0.2"
# internal
sphinx = { path = "../../sphinx"}
sphinx = { git = "https://github.com/nymtech/sphinx", rev="5ccf41f6e55e10e9879206f2fc2dfeba6cf35c8c" }
crypto = { path = "../common/crypto" }
nymsphinx = { path = "../common/nymsphinx" }
+29 -21
View File
@@ -11,21 +11,21 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
use nymsphinx::addressing::nodes::{NymNodeRoutingAddress, NymNodeRoutingAddressError};
use sphinx::crypto;
use crypto::identity::MixIdentityPublicKey;
use curve25519_dalek::montgomery::MontgomeryPoint;
use nymsphinx::addressing::nodes::NymNodeRoutingAddress;
use sphinx::header::delays;
use sphinx::route::{Destination, Node};
use sphinx::route::{DestinationAddressBytes, NodeAddressBytes};
use sphinx::SphinxPacket;
use std::convert::{From, Into, TryFrom, TryInto};
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};
use std::convert::TryInto;
use std::net::{IpAddr, SocketAddr};
use std::time::Duration;
// use wasm_bindgen::prelude::*;
use wasm_bindgen::prelude::*;
mod utils;
const NODE_ADDRESS_LENGTH: usize = 32;
const DESTINATION_ADDRESS_LENGTH: usize = 32;
const IDENTIFIER_LENGTH: usize = 16;
@@ -39,27 +39,28 @@ static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;
///
/// The `wasm-pack build` command will cause this to output JS bindings and a
/// wasm executable in the `pkg/` directory.
// #[wasm_bindgen]
#[wasm_bindgen]
pub fn create_sphinx_packet() -> Vec<u8> {
utils::set_panic_hook(); // nicer js errors.
let address1 = NymNodeRoutingAddress(SocketAddr::new(IpAddr::from([216, 211, 110, 161]), 1789));
let address_bytes1: NodeAddressBytes = address1.try_into().unwrap();
// let address_bytes1 = NodeAddressBytes::try_from(address1);
let node1_pk = public_key_from_str("HShr7AQvJNxp64qKk23cmHUvnZqKD9BnpcUJWaskXiAN");
let address2 = NymNodeRoutingAddress(SocketAddr::new(IpAddr::from([80, 218, 232, 173]), 1791));
let address_bytes2: NodeAddressBytes = address2.try_into().unwrap();
let node2_pk = public_key_from_str("8SMWvu6mMBwTNKyRcQx11J2mkZnYk5r6PJVKq2ZyT5P7");
let address3 = NymNodeRoutingAddress(SocketAddr::new(IpAddr::from([213, 52, 129, 218]), 1789));
let address_bytes3: NodeAddressBytes = address3.try_into().unwrap();
let node3_pk = public_key_from_str("D4Zte8tBrc6aXW9BkG9rQNxG2PZ7Wzqk1sFCF6j4tiJN");
let provider = NymNodeRoutingAddress(SocketAddr::new(IpAddr::from([139, 162, 246, 48]), 1789));
let provider_bytes: NodeAddressBytes = provider.try_into().unwrap();
let provider_pk = public_key_from_str("7vhgER4Gz789QHNTSu4apMpTcpTuUaRiLxJnbz1g2HFh");
let (_, node1_pk) = crypto::keygen();
let node1 = Node::new(address_bytes1, node1_pk);
let (_, node2_pk) = crypto::keygen();
let node2 = Node::new(
NodeAddressBytes::from_bytes([4u8; NODE_ADDRESS_LENGTH]),
node2_pk,
);
let (_, node3_pk) = crypto::keygen();
let node3 = Node::new(
NodeAddressBytes::from_bytes([2u8; NODE_ADDRESS_LENGTH]),
node3_pk,
);
let node2 = Node::new(address_bytes2, node2_pk);
let node3 = Node::new(address_bytes3, node3_pk);
let provider = Node::new(provider_bytes, provider_pk);
let route = [node1, node2, node3];
let route = [node1, node2, node3, provider];
let average_delay = Duration::from_secs_f64(1.0);
let delays = delays::generate_from_average_duration(route.len(), average_delay);
let destination = Destination::new(
@@ -67,7 +68,7 @@ pub fn create_sphinx_packet() -> Vec<u8> {
[4u8; IDENTIFIER_LENGTH],
);
let message = vec![13u8, 16];
let message = ("FOOMP FOOMP FOOMP\n\n").as_bytes().to_vec();
let sphinx_packet =
match SphinxPacket::new(message.clone(), &route, &destination, &delays).unwrap() {
SphinxPacket { header, payload } => SphinxPacket { header, payload },
@@ -75,3 +76,10 @@ pub fn create_sphinx_packet() -> Vec<u8> {
sphinx_packet.to_bytes()
}
fn public_key_from_str(s: &str) -> MontgomeryPoint {
let src = MixIdentityPublicKey::from_base58_string(s.to_owned()).to_bytes();
let mut dest: [u8; 32] = [0; 32];
dest.copy_from_slice(&src);
MontgomeryPoint(dest)
}