(wasm) Treat arbitrary byte data as Uint8Array, instead of base64url marshalling.

This commit is contained in:
Brandon Vandegrift
2023-09-20 00:46:45 -04:00
parent a7b073cddb
commit 80afa19678
17 changed files with 452 additions and 116 deletions

View File

@@ -58,7 +58,7 @@ impl VeilidCrypto {
APIResult::Ok(out.to_string())
}
pub fn randomBytes(kind: String, len: u32) -> APIResult<String> {
pub fn randomBytes(kind: String, len: u32) -> APIResult<Box<[u8]>> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let veilid_api = get_veilid_api()?;
@@ -71,7 +71,7 @@ impl VeilidCrypto {
)
})?;
let out = crypto_system.random_bytes(len);
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
let out = out.into_boxed_slice();
APIResult::Ok(out)
}
@@ -91,10 +91,8 @@ impl VeilidCrypto {
APIResult::Ok(out)
}
pub fn hashPassword(kind: String, password: String, salt: String) -> APIResult<String> {
pub fn hashPassword(kind: String, password: Box<[u8]>, salt: Box<[u8]>) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password = unmarshall(password)?;
let salt = unmarshall(salt)?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
@@ -111,11 +109,10 @@ impl VeilidCrypto {
pub fn verifyPassword(
kind: String,
password: String,
password: Box<[u8]>,
password_hash: String,
) -> APIResult<bool> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password = unmarshall(password)?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
@@ -130,10 +127,12 @@ impl VeilidCrypto {
APIResult::Ok(out)
}
pub fn deriveSharedSecret(kind: String, password: String, salt: String) -> APIResult<String> {
pub fn deriveSharedSecret(
kind: String,
password: Box<[u8]>,
salt: Box<[u8]>,
) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password = unmarshall(password)?;
let salt = unmarshall(salt)?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
@@ -182,7 +181,7 @@ impl VeilidCrypto {
pub fn verifySignatures(
node_ids: StringArray,
data: String,
data: Box<[u8]>,
signatures: StringArray,
) -> VeilidAPIResult<StringArray> {
let node_ids = into_unchecked_string_vec(node_ids);
@@ -199,8 +198,6 @@ impl VeilidCrypto {
})
.collect::<APIResult<Vec<TypedKey>>>()?;
let data: Vec<u8> = unmarshall(data)?;
let typed_signatures = into_unchecked_string_vec(signatures);
let typed_signatures: Vec<TypedSignature> = typed_signatures
.iter()
@@ -226,9 +223,7 @@ impl VeilidCrypto {
APIResult::Ok(out)
}
pub fn generateSignatures(data: String, key_pairs: StringArray) -> APIResult<StringArray> {
let data = unmarshall(data)?;
pub fn generateSignatures(data: Box<[u8]>, key_pairs: StringArray) -> APIResult<StringArray> {
let key_pairs = into_unchecked_string_vec(key_pairs);
let key_pairs: Vec<TypedKeyPair> = key_pairs
.iter()
@@ -269,11 +264,9 @@ impl VeilidCrypto {
APIResult::Ok(out)
}
pub fn generateHash(kind: String, data: String) -> APIResult<String> {
pub fn generateHash(kind: String, data: Box<[u8]>) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let data = unmarshall(data)?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let crypto_system = crypto.get(kind).ok_or_else(|| {
@@ -306,11 +299,9 @@ impl VeilidCrypto {
APIResult::Ok(out)
}
pub fn validateHash(kind: String, data: String, hash: String) -> APIResult<bool> {
pub fn validateHash(kind: String, data: Box<[u8]>, hash: String) -> APIResult<bool> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let data = unmarshall(data)?;
let hash: veilid_core::HashDigest = veilid_core::HashDigest::from_str(&hash)?;
let veilid_api = get_veilid_api()?;
@@ -345,14 +336,12 @@ impl VeilidCrypto {
APIResult::Ok(out.to_string())
}
pub fn sign(kind: String, key: String, secret: String, data: String) -> APIResult<String> {
pub fn sign(kind: String, key: String, secret: String, data: Box<[u8]>) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?;
let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?;
let data = unmarshall(data)?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let crypto_system = crypto.get(kind).ok_or_else(|| {
@@ -362,11 +351,10 @@ impl VeilidCrypto {
APIResult::Ok(out.to_string())
}
pub fn verify(kind: String, key: String, data: String, signature: String) -> APIResult<()> {
pub fn verify(kind: String, key: String, data: Box<[u8]>, signature: String) -> APIResult<()> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?;
let data = unmarshall(data)?;
let signature: veilid_core::Signature = veilid_core::Signature::from_str(&signature)?;
let veilid_api = get_veilid_api()?;
@@ -396,24 +384,18 @@ impl VeilidCrypto {
pub fn decryptAead(
kind: String,
body: String,
body: Box<[u8]>,
nonce: String,
shared_secret: String,
associated_data: Option<String>,
) -> APIResult<String> {
associated_data: Option<Box<[u8]>>,
) -> APIResult<Box<[u8]>> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let body = unmarshall(body)?;
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret =
veilid_core::SharedSecret::from_str(&shared_secret)?;
let associated_data = associated_data
.map(unmarshall)
.map_or(APIResult::Ok(None), |r| r.map(Some))?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let crypto_system = crypto.get(kind).ok_or_else(|| {
@@ -428,34 +410,28 @@ impl VeilidCrypto {
&nonce,
&shared_secret,
match &associated_data {
Some(ad) => Some(ad.as_slice()),
Some(ad) => Some(ad),
None => None,
},
)?;
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
let out = out.into_boxed_slice();
APIResult::Ok(out)
}
pub fn encryptAead(
kind: String,
body: String,
body: Box<[u8]>,
nonce: String,
shared_secret: String,
associated_data: Option<String>,
) -> APIResult<String> {
associated_data: Option<Box<[u8]>>,
) -> APIResult<Box<[u8]>> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let body = unmarshall(body)?;
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret =
veilid_core::SharedSecret::from_str(&shared_secret)?;
let associated_data: Option<Vec<u8>> = associated_data
.map(unmarshall)
.map_or(APIResult::Ok(None), |r| r.map(Some))?;
let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?;
let crypto_system = crypto.get(kind).ok_or_else(|| {
@@ -470,24 +446,21 @@ impl VeilidCrypto {
&nonce,
&shared_secret,
match &associated_data {
Some(ad) => Some(ad.as_slice()),
Some(ad) => Some(ad),
None => None,
},
)?;
let out = data_encoding::BASE64URL_NOPAD.encode(&out);
APIResult::Ok(out)
APIResult::Ok(out.into_boxed_slice())
}
pub fn cryptNoAuth(
kind: String,
body: String,
mut body: Box<[u8]>,
nonce: String,
shared_secret: String,
) -> APIResult<String> {
) -> APIResult<Box<[u8]>> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let mut body = unmarshall(body)?;
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret =
@@ -503,7 +476,6 @@ impl VeilidCrypto {
)
})?;
crypto_system.crypt_in_place_no_auth(&mut body, &nonce, &shared_secret);
let out = data_encoding::BASE64URL_NOPAD.encode(&body);
APIResult::Ok(out)
APIResult::Ok(body)
}
}

View File

@@ -83,8 +83,8 @@ impl VeilidRoutingContext {
///
/// * `call_id` - specifies which call to reply to, and it comes from a VeilidUpdate::AppCall, specifically the VeilidAppCall::id() value.
/// * `message` - is an answer blob to be returned by the remote node's RoutingContext::app_call() function, and may be up to 32768 bytes
pub async fn appCallReply(call_id: String, message: String) -> APIResult<()> {
let message = unmarshall(message)?;
pub async fn appCallReply(call_id: String, message: Box<[u8]>) -> APIResult<()> {
let message = message.into_vec();
let call_id = match call_id.parse() {
Ok(v) => v,
Err(e) => {
@@ -148,10 +148,9 @@ impl VeilidRoutingContext {
/// @param {string} target - can be either a direct node id or a private route.
/// @param {string} message - an arbitrary message blob of up to `32768` bytes.
#[wasm_bindgen(skip_jsdoc)]
pub async fn appMessage(&self, target_string: String, message: String) -> APIResult<()> {
pub async fn appMessage(&self, target_string: String, message: Box<[u8]>) -> APIResult<()> {
let routing_context = self.getRoutingContext()?;
let message = unmarshall(message)?;
let message = message.into_vec();
let veilid_api = get_veilid_api()?;
let target = veilid_api.parse_as_target(target_string).await?;
routing_context.app_message(target, message).await?;
@@ -162,18 +161,22 @@ impl VeilidRoutingContext {
///
/// Veilid apps may use this for arbitrary message passing.
///
/// @param {string} target_string - can be either a direct node id or a private route, base64Url encoded.
/// @param {string} message - an arbitrary message blob of up to `32768` bytes, base64Url encoded.
/// @returns an answer blob of up to `32768` bytes, base64Url encoded.
/// @param {string} target_string - can be either a direct node id or a private route.
/// @param {Uint8Array} message - an arbitrary message blob of up to `32768` bytes.
/// @returns {Uint8Array} an answer blob of up to `32768` bytes.
#[wasm_bindgen(skip_jsdoc)]
pub async fn appCall(&self, target_string: String, request: String) -> APIResult<String> {
let request: Vec<u8> = unmarshall(request)?;
pub async fn appCall(
&self,
target_string: String,
request: Box<[u8]>,
) -> APIResult<Uint8Array> {
let request: Vec<u8> = request.into_vec();
let routing_context = self.getRoutingContext()?;
let veilid_api = get_veilid_api()?;
let target = veilid_api.parse_as_target(target_string).await?;
let answer = routing_context.app_call(target, request).await?;
let answer = marshall(&answer);
let answer = Uint8Array::from(answer.as_slice());
APIResult::Ok(answer)
}
@@ -250,7 +253,7 @@ impl VeilidRoutingContext {
/// May pull the latest value from the network, but by settings 'force_refresh' you can force a network data refresh.
///
/// Returns `undefined` if the value subkey has not yet been set.
/// Returns base64Url encoded `data` if the value subkey has valid data.
/// Returns a Uint8Array of `data` if the value subkey has valid data.
pub async fn getDhtValue(
&self,
key: String,
@@ -268,15 +271,15 @@ impl VeilidRoutingContext {
/// Pushes a changed subkey value to the network
///
/// Returns `undefined` if the value was successfully put.
/// Returns base64Url encoded `data` if the value put was older than the one available on the network.
/// Returns a Uint8Array of `data` if the value put was older than the one available on the network.
pub async fn setDhtValue(
&self,
key: String,
subKey: u32,
data: String,
data: Box<[u8]>,
) -> APIResult<Option<ValueData>> {
let key = TypedKey::from_str(&key)?;
let data = unmarshall(data)?;
let data = data.into_vec();
let routing_context = self.getRoutingContext()?;
let res = routing_context.set_dht_value(key, subKey, data).await?;

View File

@@ -63,22 +63,24 @@ impl VeilidTableDB {
}
/// Read a key from a column in the TableDB immediately.
pub async fn load(&mut self, columnId: u32, key: String) -> APIResult<Option<String>> {
pub async fn load(&mut self, columnId: u32, key: Box<[u8]>) -> APIResult<Option<Uint8Array>> {
self.ensureOpen().await;
let key = unmarshall(key)?;
let table_db = self.getTableDB()?;
let out = table_db.load(columnId, &key).await?;
let out = out.map(|out| marshall(&out));
let out = out.map(|out| Uint8Array::from(out.as_slice()));
APIResult::Ok(out)
}
/// Store a key with a value in a column in the TableDB.
/// Performs a single transaction immediately.
pub async fn store(&mut self, columnId: u32, key: String, value: String) -> APIResult<()> {
pub async fn store(
&mut self,
columnId: u32,
key: Box<[u8]>,
value: Box<[u8]>,
) -> APIResult<()> {
self.ensureOpen().await;
let key = unmarshall(key)?;
let value = unmarshall(value)?;
let table_db = self.getTableDB()?;
table_db.store(columnId, &key, &value).await?;
@@ -86,26 +88,29 @@ impl VeilidTableDB {
}
/// Delete key with from a column in the TableDB.
pub async fn delete(&mut self, columnId: u32, key: String) -> APIResult<Option<String>> {
pub async fn delete(&mut self, columnId: u32, key: Box<[u8]>) -> APIResult<Option<Uint8Array>> {
self.ensureOpen().await;
let key = unmarshall(key)?;
let table_db = self.getTableDB()?;
let out = table_db.delete(columnId, &key).await?;
let out = out.map(|out| marshall(&out));
let out = out.map(|out| Uint8Array::from(out.as_slice()));
APIResult::Ok(out)
}
/// Get the list of keys in a column of the TableDB.
///
/// Returns an array of base64Url encoded keys.
pub async fn getKeys(&mut self, columnId: u32) -> APIResult<StringArray> {
/// Returns an array of Uint8Array keys.
pub async fn getKeys(&mut self, columnId: u32) -> APIResult<Uint8ArrayArray> {
self.ensureOpen().await;
let table_db = self.getTableDB()?;
let keys = table_db.clone().get_keys(columnId).await?;
let out: Vec<String> = keys.into_iter().map(|k| marshall(&k)).collect();
let out = into_unchecked_string_array(out);
let out: Vec<Uint8Array> = keys
.into_iter()
.map(|k| Uint8Array::from(k.as_slice()))
.collect();
let out = into_unchecked_uint8array_array(out);
APIResult::Ok(out)
}
@@ -164,16 +169,13 @@ impl VeilidTableDBTransaction {
/// Store a key with a value in a column in the TableDB.
/// Does not modify TableDB until `.commit()` is called.
pub fn store(&self, col: u32, key: String, value: String) -> APIResult<()> {
let key = unmarshall(key)?;
let value = unmarshall(value)?;
pub fn store(&self, col: u32, key: Box<[u8]>, value: Box<[u8]>) -> APIResult<()> {
let transaction = self.getTransaction()?;
transaction.store(col, &key, &value)
}
/// Delete key with from a column in the TableDB
pub fn deleteKey(&self, col: u32, key: String) -> APIResult<()> {
let key = unmarshall(key)?;
pub fn deleteKey(&self, col: u32, key: Box<[u8]>) -> APIResult<()> {
let transaction = self.getTransaction()?;
transaction.delete(col, &key)
}

View File

@@ -37,6 +37,19 @@ pub(crate) fn into_unchecked_string_array(items: Vec<String>) -> StringArray {
.unchecked_into::<StringArray>() // TODO: can I do this a better way?
}
#[wasm_bindgen]
extern "C" {
#[wasm_bindgen(typescript_type = "Uint8Array[]")]
pub type Uint8ArrayArray;
}
/// Convert a `Vec<Uint8Array>` into a `js_sys::Array` with the type of `Uint8Array[]`
pub(crate) fn into_unchecked_uint8array_array(items: Vec<Uint8Array>) -> Uint8ArrayArray {
items
.iter()
.collect::<js_sys::Array>()
.unchecked_into::<Uint8ArrayArray>() // TODO: can I do this a better way?
}
/// Convert a StringArray (`js_sys::Array` with the type of `string[]`) into `Vec<String>`
pub(crate) fn into_unchecked_string_vec(items: StringArray) -> Vec<String> {
items