Replace .unwrap() with ? operator

This commit is contained in:
Brandon Vandegrift 2023-09-12 08:06:49 -04:00
parent c2c607efac
commit ac8bbe9a83
4 changed files with 88 additions and 96 deletions

View File

@ -66,10 +66,15 @@ fn take_veilid_api() -> Result<veilid_core::VeilidAPI, veilid_core::VeilidAPIErr
} }
// Marshalling helpers // Marshalling helpers
pub fn unmarshall(b64: String) -> Vec<u8> { pub fn unmarshall(b64: String) -> APIResult<Vec<u8>> {
data_encoding::BASE64URL_NOPAD data_encoding::BASE64URL_NOPAD
.decode(b64.as_bytes()) .decode(b64.as_bytes())
.unwrap() .map_err(|e| {
VeilidAPIError::generic(format!(
"error decoding base64url string '{}' into bytes: {}",
b64, e
))
})
} }
pub fn marshall(data: &Vec<u8>) -> String { pub fn marshall(data: &Vec<u8>) -> String {

View File

@ -93,12 +93,8 @@ impl VeilidCrypto {
pub fn hashPassword(kind: String, password: String, salt: String) -> APIResult<String> { pub fn hashPassword(kind: String, password: String, salt: String) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD let password = unmarshall(password)?;
.decode(password.as_bytes()) let salt = unmarshall(salt)?;
.unwrap();
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(salt.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -119,9 +115,7 @@ impl VeilidCrypto {
password_hash: String, password_hash: String,
) -> APIResult<bool> { ) -> APIResult<bool> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD let password = unmarshall(password)?;
.decode(password.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -138,12 +132,8 @@ impl VeilidCrypto {
pub fn deriveSharedSecret(kind: String, password: String, salt: String) -> APIResult<String> { pub fn deriveSharedSecret(kind: String, password: String, salt: String) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let password: Vec<u8> = data_encoding::BASE64URL_NOPAD let password = unmarshall(password)?;
.decode(password.as_bytes()) let salt = unmarshall(salt)?;
.unwrap();
let salt: Vec<u8> = data_encoding::BASE64URL_NOPAD
.decode(salt.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -196,20 +186,34 @@ impl VeilidCrypto {
signatures: StringArray, signatures: StringArray,
) -> VeilidAPIResult<StringArray> { ) -> VeilidAPIResult<StringArray> {
let node_ids = into_unchecked_string_vec(node_ids); let node_ids = into_unchecked_string_vec(node_ids);
let node_ids: Vec<veilid_core::TypedKey> = node_ids let node_ids: Vec<TypedKey> = node_ids
.iter() .iter()
.map(|k| veilid_core::TypedKey::from_str(k).unwrap()) .map(|k| {
.collect(); veilid_core::TypedKey::from_str(k).map_err(|e| {
VeilidAPIError::invalid_argument(
"verifySignatures()",
format!("error decoding nodeid in node_ids[]: {}", e),
k,
)
})
})
.collect::<APIResult<Vec<TypedKey>>>()?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data: Vec<u8> = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let typed_signatures = into_unchecked_string_vec(signatures); let typed_signatures = into_unchecked_string_vec(signatures);
let typed_signatures: Vec<veilid_core::TypedSignature> = typed_signatures let typed_signatures: Vec<TypedSignature> = typed_signatures
.iter() .iter()
.map(|k| veilid_core::TypedSignature::from_str(k).unwrap()) .map(|k| {
.collect(); TypedSignature::from_str(k).map_err(|e| {
VeilidAPIError::invalid_argument(
"verifySignatures()",
format!("error decoding keypair in key_pairs[]: {}", e),
k,
)
})
})
.collect::<APIResult<Vec<TypedSignature>>>()?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -223,15 +227,21 @@ impl VeilidCrypto {
} }
pub fn generateSignatures(data: String, key_pairs: StringArray) -> APIResult<StringArray> { pub fn generateSignatures(data: String, key_pairs: StringArray) -> APIResult<StringArray> {
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let key_pairs = into_unchecked_string_vec(key_pairs); let key_pairs = into_unchecked_string_vec(key_pairs);
let key_pairs: Vec<veilid_core::TypedKeyPair> = key_pairs let key_pairs: Vec<TypedKeyPair> = key_pairs
.iter() .iter()
.map(|k| veilid_core::TypedKeyPair::from_str(k).unwrap()) .map(|k| {
.collect(); veilid_core::TypedKeyPair::from_str(k).map_err(|e| {
VeilidAPIError::invalid_argument(
"generateSignatures()",
format!("error decoding keypair in key_pairs[]: {}", e),
k,
)
})
})
.collect::<APIResult<Vec<veilid_core::TypedKeyPair>>>()?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -262,9 +272,7 @@ impl VeilidCrypto {
pub fn generateHash(kind: String, data: String) -> APIResult<String> { pub fn generateHash(kind: String, data: String) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -301,9 +309,7 @@ impl VeilidCrypto {
pub fn validateHash(kind: String, data: String, hash: String) -> APIResult<bool> { pub fn validateHash(kind: String, data: String, hash: String) -> APIResult<bool> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let hash: veilid_core::HashDigest = veilid_core::HashDigest::from_str(&hash)?; let hash: veilid_core::HashDigest = veilid_core::HashDigest::from_str(&hash)?;
@ -345,9 +351,7 @@ impl VeilidCrypto {
let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?;
let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?; let secret: veilid_core::SecretKey = veilid_core::SecretKey::from_str(&secret)?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -362,9 +366,7 @@ impl VeilidCrypto {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?; let key: veilid_core::PublicKey = veilid_core::PublicKey::from_str(&key)?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(data.as_bytes())
.unwrap();
let signature: veilid_core::Signature = veilid_core::Signature::from_str(&signature)?; let signature: veilid_core::Signature = veilid_core::Signature::from_str(&signature)?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
@ -401,20 +403,16 @@ impl VeilidCrypto {
) -> APIResult<String> { ) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let body: Vec<u8> = data_encoding::BASE64URL_NOPAD let body = unmarshall(body)?;
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?; let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret = let shared_secret: veilid_core::SharedSecret =
veilid_core::SharedSecret::from_str(&shared_secret)?; veilid_core::SharedSecret::from_str(&shared_secret)?;
let associated_data: Option<Vec<u8>> = associated_data.map(|ad| { let associated_data = associated_data
data_encoding::BASE64URL_NOPAD .map(|ad| unmarshall(ad))
.decode(ad.as_bytes()) .map_or(APIResult::Ok(None), |r| r.map(Some))?;
.unwrap()
});
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -447,20 +445,16 @@ impl VeilidCrypto {
) -> APIResult<String> { ) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let body: Vec<u8> = data_encoding::BASE64URL_NOPAD let body = unmarshall(body)?;
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce).unwrap(); let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret = let shared_secret: veilid_core::SharedSecret =
veilid_core::SharedSecret::from_str(&shared_secret).unwrap(); veilid_core::SharedSecret::from_str(&shared_secret)?;
let associated_data: Option<Vec<u8>> = associated_data.map(|ad| { let associated_data: Option<Vec<u8>> = associated_data
data_encoding::BASE64URL_NOPAD .map(|ad| unmarshall(ad))
.decode(ad.as_bytes()) .map_or(APIResult::Ok(None), |r| r.map(Some))?;
.unwrap()
});
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;
@ -492,14 +486,12 @@ impl VeilidCrypto {
) -> APIResult<String> { ) -> APIResult<String> {
let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?; let kind: veilid_core::CryptoKind = veilid_core::FourCC::from_str(&kind)?;
let mut body: Vec<u8> = data_encoding::BASE64URL_NOPAD let mut body = unmarshall(body)?;
.decode(body.as_bytes())
.unwrap();
let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce).unwrap(); let nonce: veilid_core::Nonce = veilid_core::Nonce::from_str(&nonce)?;
let shared_secret: veilid_core::SharedSecret = let shared_secret: veilid_core::SharedSecret =
veilid_core::SharedSecret::from_str(&shared_secret).unwrap(); veilid_core::SharedSecret::from_str(&shared_secret)?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let crypto = veilid_api.crypto()?; let crypto = veilid_api.crypto()?;

View File

@ -44,9 +44,7 @@ impl VeilidRoutingContext {
/// ///
/// Returns a route id that can be used to send private messages to the node creating this route. /// Returns a route id that can be used to send private messages to the node creating this route.
pub fn importRemotePrivateRoute(&self, blob: String) -> APIResult<RouteId> { pub fn importRemotePrivateRoute(&self, blob: String) -> APIResult<RouteId> {
let blob: Vec<u8> = data_encoding::BASE64URL_NOPAD let blob = unmarshall(blob)?;
.decode(blob.as_bytes())
.unwrap();
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let route_id = veilid_api.import_remote_private_route(blob)?; let route_id = veilid_api.import_remote_private_route(blob)?;
APIResult::Ok(route_id) APIResult::Ok(route_id)
@ -86,7 +84,7 @@ impl VeilidRoutingContext {
/// * `call_id` - specifies which call to reply to, and it comes from a VeilidUpdate::AppCall, specifically the VeilidAppCall::id() value. /// * `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 /// * `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<()> { pub async fn appCallReply(call_id: String, message: String) -> APIResult<()> {
let message = unmarshall(message); let message = unmarshall(message)?;
let call_id = match call_id.parse() { let call_id = match call_id.parse() {
Ok(v) => v, Ok(v) => v,
Err(e) => { Err(e) => {
@ -152,7 +150,7 @@ impl VeilidRoutingContext {
#[wasm_bindgen(skip_jsdoc)] #[wasm_bindgen(skip_jsdoc)]
pub async fn appMessage(&self, target_string: String, message: String) -> APIResult<()> { pub async fn appMessage(&self, target_string: String, message: String) -> APIResult<()> {
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
let message = unmarshall(message); let message = unmarshall(message)?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
let target = veilid_api.parse_as_target(target_string).await?; let target = veilid_api.parse_as_target(target_string).await?;
@ -169,7 +167,7 @@ impl VeilidRoutingContext {
/// @returns an answer blob of up to `32768` bytes, base64Url encoded. /// @returns an answer blob of up to `32768` bytes, base64Url encoded.
#[wasm_bindgen(skip_jsdoc)] #[wasm_bindgen(skip_jsdoc)]
pub async fn appCall(&self, target_string: String, request: String) -> APIResult<String> { pub async fn appCall(&self, target_string: String, request: String) -> APIResult<String> {
let request: Vec<u8> = unmarshall(request); let request: Vec<u8> = unmarshall(request)?;
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
let veilid_api = get_veilid_api()?; let veilid_api = get_veilid_api()?;
@ -215,11 +213,10 @@ impl VeilidRoutingContext {
key: String, key: String,
writer: Option<String>, writer: Option<String>,
) -> APIResult<DHTRecordDescriptor> { ) -> APIResult<DHTRecordDescriptor> {
let key = TypedKey::from_str(&key).unwrap(); let key = TypedKey::from_str(&key)?;
let writer = match writer { let writer = writer
Some(writer) => Some(KeyPair::from_str(&writer).unwrap()), .map(|writer| KeyPair::from_str(&writer))
_ => None, .map_or(APIResult::Ok(None), |r| r.map(Some))?;
};
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
let dht_record_descriptor = routing_context.open_dht_record(key, writer).await?; let dht_record_descriptor = routing_context.open_dht_record(key, writer).await?;
@ -230,7 +227,7 @@ impl VeilidRoutingContext {
/// ///
/// Closing a record allows you to re-open it with a different routing context /// Closing a record allows you to re-open it with a different routing context
pub async fn closeDhtRecord(&self, key: String) -> APIResult<()> { pub async fn closeDhtRecord(&self, key: String) -> APIResult<()> {
let key = TypedKey::from_str(&key).unwrap(); let key = TypedKey::from_str(&key)?;
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
routing_context.close_dht_record(key).await?; routing_context.close_dht_record(key).await?;
APIRESULT_UNDEFINED APIRESULT_UNDEFINED
@ -242,7 +239,7 @@ impl VeilidRoutingContext {
/// Deleting a record does not delete it from the network, but will remove the storage of the record locally, /// Deleting a record does not delete it from the network, but will remove the storage of the record locally,
/// and will prevent its value from being refreshed on the network by this node. /// and will prevent its value from being refreshed on the network by this node.
pub async fn deleteDhtRecord(&self, key: String) -> APIResult<()> { pub async fn deleteDhtRecord(&self, key: String) -> APIResult<()> {
let key = TypedKey::from_str(&key).unwrap(); let key = TypedKey::from_str(&key)?;
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
routing_context.delete_dht_record(key).await?; routing_context.delete_dht_record(key).await?;
APIRESULT_UNDEFINED APIRESULT_UNDEFINED
@ -260,7 +257,7 @@ impl VeilidRoutingContext {
subKey: u32, subKey: u32,
forceRefresh: bool, forceRefresh: bool,
) -> APIResult<Option<ValueData>> { ) -> APIResult<Option<ValueData>> {
let key = TypedKey::from_str(&key).unwrap(); let key = TypedKey::from_str(&key)?;
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
let res = routing_context let res = routing_context
.get_dht_value(key, subKey, forceRefresh) .get_dht_value(key, subKey, forceRefresh)
@ -278,10 +275,8 @@ impl VeilidRoutingContext {
subKey: u32, subKey: u32,
data: String, data: String,
) -> APIResult<Option<ValueData>> { ) -> APIResult<Option<ValueData>> {
let key = TypedKey::from_str(&key).unwrap(); let key = TypedKey::from_str(&key)?;
let data: Vec<u8> = data_encoding::BASE64URL_NOPAD let data = unmarshall(data)?;
.decode(&data.as_bytes())
.unwrap();
let routing_context = self.getRoutingContext()?; let routing_context = self.getRoutingContext()?;
let res = routing_context.set_dht_value(key, subKey, data).await?; let res = routing_context.set_dht_value(key, subKey, data).await?;

View File

@ -63,11 +63,11 @@ impl VeilidTableDB {
/// Read a key from a column in the TableDB immediately. /// 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: String) -> APIResult<Option<String>> {
self.ensureOpen().await; self.ensureOpen().await;
let key = unmarshall(key); let key = unmarshall(key)?;
let table_db = self.getTableDB()?; let table_db = self.getTableDB()?;
let out = table_db.load(columnId, &key).await?.unwrap(); let out = table_db.load(columnId, &key).await?;
let out = Some(marshall(&out)); let out = out.map(|out| marshall(&out));
APIResult::Ok(out) APIResult::Ok(out)
} }
@ -75,8 +75,8 @@ impl VeilidTableDB {
/// Performs a single transaction immediately. /// 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: String, value: String) -> APIResult<()> {
self.ensureOpen().await; self.ensureOpen().await;
let key = unmarshall(key); let key = unmarshall(key)?;
let value = unmarshall(value); let value = unmarshall(value)?;
let table_db = self.getTableDB()?; let table_db = self.getTableDB()?;
table_db.store(columnId, &key, &value).await?; table_db.store(columnId, &key, &value).await?;
@ -86,11 +86,11 @@ impl VeilidTableDB {
/// Delete key with from a column in the TableDB. /// 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: String) -> APIResult<Option<String>> {
self.ensureOpen().await; self.ensureOpen().await;
let key = unmarshall(key); let key = unmarshall(key)?;
let table_db = self.getTableDB()?; let table_db = self.getTableDB()?;
let out = table_db.delete(columnId, &key).await?.unwrap(); let out = table_db.delete(columnId, &key).await?;
let out = Some(marshall(&out)); let out = out.map(|out| marshall(&out));
APIResult::Ok(out) APIResult::Ok(out)
} }
@ -161,15 +161,15 @@ impl VeilidTableDBTransaction {
/// Store a key with a value in a column in the TableDB. /// Store a key with a value in a column in the TableDB.
/// Does not modify TableDB until `.commit()` is called. /// Does not modify TableDB until `.commit()` is called.
pub fn store(&self, col: u32, key: String, value: String) -> APIResult<()> { pub fn store(&self, col: u32, key: String, value: String) -> APIResult<()> {
let key = unmarshall(key); let key = unmarshall(key)?;
let value = unmarshall(value); let value = unmarshall(value)?;
let transaction = self.getTransaction()?; let transaction = self.getTransaction()?;
transaction.store(col, &key, &value) transaction.store(col, &key, &value)
} }
/// Delete key with from a column in the TableDB /// Delete key with from a column in the TableDB
pub fn deleteKey(&self, col: u32, key: String) -> APIResult<()> { pub fn deleteKey(&self, col: u32, key: String) -> APIResult<()> {
let key = unmarshall(key); let key = unmarshall(key)?;
let transaction = self.getTransaction()?; let transaction = self.getTransaction()?;
transaction.delete(col, &key) transaction.delete(col, &key)
} }