#![allow(
clippy::unwrap_used,
clippy::print_stdout,
reason = "a test reports by panicking; the generator prints its vector"
)]
use teter_common::SessionId;
use teter_core::account::{AccountKeys, seal_to};
use teter_core::e2e::{Direction, Keys, Kind, Secret};
const SECRET: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";
const SESSION: &str = "6f1d3c52-7a49-4e0b-9a8e-2b6c1d0e4f73";
const CHANNEL: &str = "0123456789abcdef0123456789abcdef";
const INFO: &[u8] = b"teter-seal-v1 session key 0123456789abcdef0123456789abcdef";
const PLAIN: &[u8] = b"printf 'te%s\\n' ter";
fn account() -> AccountKeys {
let bytes: Vec<u8> = (0..64_u8)
.map(|n| n.wrapping_mul(7).wrapping_add(1))
.collect();
AccountKeys::from_private_bytes(&bytes).unwrap()
}
fn keys() -> Keys {
Keys::derive_on(
&Secret::parse(SECRET).unwrap(),
SessionId::parse_str(SESSION).unwrap(),
CHANNEL,
)
}
fn hex(bytes: &[u8]) -> String {
bytes.iter().map(|b| format!("{b:02x}")).collect()
}
fn unhex(text: &str) -> Vec<u8> {
(0..text.len())
.step_by(2)
.map(|at| u8::from_str_radix(text.get(at..at + 2).unwrap(), 16).unwrap())
.collect()
}
fn field<'a>(vector: &'a str, name: &str) -> &'a str {
vector
.lines()
.find_map(|line| line.strip_prefix(name)?.strip_prefix('='))
.unwrap()
}
fn opens(vector: &str) {
let mut opener = keys().opener(Direction::ToFrontend);
let plain = opener
.open(Kind::Output, &unhex(field(vector, "e2e")))
.unwrap();
assert_eq!(plain.as_slice(), PLAIN);
let plain = account()
.open_sealed(INFO, &unhex(field(vector, "sealed_to")))
.unwrap();
assert_eq!(plain.as_slice(), PLAIN);
assert_eq!(hex(&account().sign(PLAIN)), field(vector, "signature"));
}
#[test]
fn a_seal_made_natively_opens() {
opens(include_str!("vectors/native.txt"));
}
#[test]
fn a_seal_made_in_webassembly_opens() {
opens(include_str!("vectors/wasm.txt"));
}
#[test]
#[ignore = "the generator: run it to make a vector"]
fn make_a_vector() {
let mut sealer = keys().sealer(Direction::ToFrontend).unwrap();
println!("e2e={}", hex(&sealer.seal(Kind::Output, PLAIN)));
let sealed = seal_to(&account().public_keys(), INFO, PLAIN).unwrap();
println!("sealed_to={}", hex(&sealed));
println!("signature={}", hex(&account().sign(PLAIN)));
}