|
@@ -8,6 +8,7 @@ use lox_library::{
|
|
|
scalar_u32, IssuerPubKey, OPENINV_LENGTH,
|
|
|
};
|
|
|
use lox_utils::{EncBridgeTable, Invite};
|
|
|
+use serde_json::error::Error;
|
|
|
use std::collections::HashMap;
|
|
|
|
|
|
pub mod networking;
|
|
@@ -44,8 +45,15 @@ pub async fn eligible_for_trust_promotion(
|
|
|
Some(v) => v,
|
|
|
None => return false,
|
|
|
};
|
|
|
+ let date = match get_today(net).await {
|
|
|
+ Ok(v) => v,
|
|
|
+ Err(e) => {
|
|
|
+ eprintln!("Failed to get date, error: {}", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
scalar_u32(&cred.trust_level).unwrap() == 0
|
|
|
- && level_since + trust_promotion::UNTRUSTED_INTERVAL <= get_today(net).await
|
|
|
+ && level_since + trust_promotion::UNTRUSTED_INTERVAL <= date
|
|
|
}
|
|
|
|
|
|
// Helper function to check if credential is eligible for
|
|
@@ -55,56 +63,68 @@ pub async fn eligible_for_level_up(net: &dyn Networking, cred: &lox_library::cre
|
|
|
Some(v) => v,
|
|
|
None => return false,
|
|
|
};
|
|
|
+ let date = match get_today(net).await {
|
|
|
+ Ok(v) => v,
|
|
|
+ Err(e) => {
|
|
|
+ eprintln!("Failed to get date, error: {}", e);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ };
|
|
|
let trust_level = scalar_u32(&cred.trust_level).unwrap();
|
|
|
let blockages = scalar_u32(&cred.blockages).unwrap();
|
|
|
trust_level > 0
|
|
|
&& blockages <= MAX_BLOCKAGES[trust_level as usize]
|
|
|
- && level_since + LEVEL_INTERVAL[trust_level as usize] <= get_today(net).await
|
|
|
+ && level_since + LEVEL_INTERVAL[trust_level as usize] <= date
|
|
|
}
|
|
|
|
|
|
// Get current date from Lox Auth
|
|
|
-pub async fn get_today(net: &dyn Networking) -> u32 {
|
|
|
+pub async fn get_today(net: &dyn Networking) -> Result<u32, Error> {
|
|
|
let resp = net.request("/today".to_string(), [].to_vec()).await;
|
|
|
- let today: u32 = serde_json::from_slice(&resp).unwrap();
|
|
|
- today
|
|
|
+ let today: u32 = serde_json::from_slice(&resp)?;
|
|
|
+ Ok(today)
|
|
|
}
|
|
|
|
|
|
// Download Lox Auth pubkeys
|
|
|
-pub async fn get_lox_auth_keys(net: &dyn Networking) -> Vec<IssuerPubKey> {
|
|
|
+pub async fn get_lox_auth_keys(net: &dyn Networking) -> Result<Vec<IssuerPubKey>, Error> {
|
|
|
let resp = net.request("/pubkeys".to_string(), [].to_vec()).await;
|
|
|
- let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp).unwrap();
|
|
|
- lox_auth_pubkeys
|
|
|
+ let lox_auth_pubkeys: Vec<IssuerPubKey> = serde_json::from_slice(&resp)?;
|
|
|
+ Ok(lox_auth_pubkeys)
|
|
|
}
|
|
|
|
|
|
// Get encrypted bridge table
|
|
|
-pub async fn get_reachability_credential(net: &dyn Networking) -> HashMap<u32, EncryptedBucket> {
|
|
|
+pub async fn get_reachability_credential(
|
|
|
+ net: &dyn Networking,
|
|
|
+) -> Result<HashMap<u32, EncryptedBucket>, Error> {
|
|
|
let resp = net.request("/reachability".to_string(), [].to_vec()).await;
|
|
|
- let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp).unwrap();
|
|
|
- reachability_cred.etable
|
|
|
+ let reachability_cred: EncBridgeTable = serde_json::from_slice(&resp)?;
|
|
|
+ Ok(reachability_cred.etable)
|
|
|
}
|
|
|
|
|
|
// Get encrypted bridge table from BridgeDB and decrypt our entry
|
|
|
pub async fn get_bucket(
|
|
|
net: &dyn Networking,
|
|
|
lox_cred: &lox_library::cred::Lox,
|
|
|
-) -> (
|
|
|
- [BridgeLine; MAX_BRIDGES_PER_BUCKET],
|
|
|
- Option<cred::BucketReachability>,
|
|
|
-) {
|
|
|
- let encbuckets = get_reachability_credential(net).await;
|
|
|
+) -> Result<
|
|
|
+ (
|
|
|
+ [BridgeLine; MAX_BRIDGES_PER_BUCKET],
|
|
|
+ Option<cred::BucketReachability>,
|
|
|
+ ),
|
|
|
+ Error,
|
|
|
+> {
|
|
|
+ let encbuckets = get_reachability_credential(net).await?;
|
|
|
let (id, key) = from_scalar(lox_cred.bucket).unwrap();
|
|
|
let encbucket = match encbuckets.get(&id) {
|
|
|
Some(encbucket) => encbucket,
|
|
|
None => panic!("Provided ID not found"),
|
|
|
};
|
|
|
- BridgeTable::decrypt_bucket(id, &key, &encbucket).unwrap()
|
|
|
+ Ok(BridgeTable::decrypt_bucket(id, &key, &encbucket).unwrap())
|
|
|
}
|
|
|
|
|
|
// Get an open invitation
|
|
|
-pub async fn get_open_invitation(net: &dyn Networking) -> [u8; OPENINV_LENGTH] {
|
|
|
+pub async fn get_open_invitation(net: &dyn Networking) -> Result<[u8; OPENINV_LENGTH], Error> {
|
|
|
let resp = net.request("/invite".to_string(), [].to_vec()).await;
|
|
|
- let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp).unwrap().invite;
|
|
|
- open_invite
|
|
|
+ let open_invite: [u8; OPENINV_LENGTH] = serde_json::from_slice::<Invite>(&resp)?.invite;
|
|
|
+ Ok(open_invite)
|
|
|
}
|
|
|
|
|
|
// Get a Lox Credential from an open invitation
|
|
@@ -112,13 +132,13 @@ pub async fn get_lox_credential(
|
|
|
net: &dyn Networking,
|
|
|
open_invite: &[u8; OPENINV_LENGTH],
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
-) -> (lox_library::cred::Lox, BridgeLine) {
|
|
|
+) -> Result<(lox_library::cred::Lox, BridgeLine), Error> {
|
|
|
let (req, state) = open_invite::request(&open_invite);
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/openreq".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: open_invite::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let (cred, bridgeline) = open_invite::handle_response(state, decoded_resp, &lox_pub).unwrap();
|
|
|
- (cred, bridgeline)
|
|
|
+ Ok((cred, bridgeline))
|
|
|
}
|
|
|
|
|
|
// Get a migration credential to migrate to higher trust
|
|
@@ -126,13 +146,14 @@ pub async fn trust_promotion(
|
|
|
net: &dyn Networking,
|
|
|
lox_cred: &lox_library::cred::Lox,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
-) -> lox_library::cred::Migration {
|
|
|
- let (req, state) = trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await).unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+) -> Result<lox_library::cred::Migration, Error> {
|
|
|
+ let (req, state) =
|
|
|
+ trust_promotion::request(&lox_cred, &lox_pub, get_today(net).await?).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/trustpromo".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: trust_promotion::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let migration_cred = trust_promotion::handle_response(state, decoded_resp).unwrap();
|
|
|
- migration_cred
|
|
|
+ Ok(migration_cred)
|
|
|
}
|
|
|
|
|
|
// Promote from untrusted (trust level 0) to trusted (trust level 1)
|
|
@@ -142,14 +163,14 @@ pub async fn trust_migration(
|
|
|
migration_cred: &lox_library::cred::Migration,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
migration_pub: &IssuerPubKey,
|
|
|
-) -> lox_library::cred::Lox {
|
|
|
+) -> Result<lox_library::cred::Lox, Error> {
|
|
|
let (req, state) =
|
|
|
migration::request(lox_cred, migration_cred, lox_pub, migration_pub).unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/trustmig".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: migration::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let cred = migration::handle_response(state, decoded_resp, lox_pub).unwrap();
|
|
|
- cred
|
|
|
+ Ok(cred)
|
|
|
}
|
|
|
|
|
|
// Increase trust from at least level 1 to higher levels
|
|
@@ -159,20 +180,20 @@ pub async fn level_up(
|
|
|
reachcred: &cred::BucketReachability,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
reachability_pub: &IssuerPubKey,
|
|
|
-) -> lox_library::cred::Lox {
|
|
|
+) -> Result<lox_library::cred::Lox, Error> {
|
|
|
let (req, state) = level_up::request(
|
|
|
lox_cred,
|
|
|
&reachcred,
|
|
|
lox_pub,
|
|
|
reachability_pub,
|
|
|
- get_today(net).await,
|
|
|
+ get_today(net).await?,
|
|
|
)
|
|
|
.unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/levelup".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: level_up::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let cred = level_up::handle_response(state, decoded_resp, lox_pub).unwrap();
|
|
|
- cred
|
|
|
+ Ok(cred)
|
|
|
}
|
|
|
|
|
|
// Request an Invitation credential to give to a friend
|
|
@@ -183,7 +204,7 @@ pub async fn issue_invite(
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
reachability_pub: &IssuerPubKey,
|
|
|
invitation_pub: &IssuerPubKey,
|
|
|
-) -> (lox_library::cred::Lox, lox_library::cred::Invitation) {
|
|
|
+) -> Result<(lox_library::cred::Lox, lox_library::cred::Invitation), Error> {
|
|
|
// Read the bucket in the credential to get today's Bucket
|
|
|
// Reachability credential
|
|
|
|
|
@@ -196,15 +217,15 @@ pub async fn issue_invite(
|
|
|
&reachcred,
|
|
|
lox_pub,
|
|
|
reachability_pub,
|
|
|
- get_today(net).await,
|
|
|
+ get_today(net).await?,
|
|
|
)
|
|
|
.unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/issueinvite".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: issue_invite::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let (cred, invite) =
|
|
|
issue_invite::handle_response(state, decoded_resp, lox_pub, invitation_pub).unwrap();
|
|
|
- (cred, invite)
|
|
|
+ Ok((cred, invite))
|
|
|
}
|
|
|
|
|
|
// Redeem an Invitation credential to start at trust level 1
|
|
@@ -213,16 +234,16 @@ pub async fn redeem_invite(
|
|
|
invite: &lox_library::cred::Invitation,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
invitation_pub: &IssuerPubKey,
|
|
|
-) -> (lox_library::cred::Lox, [BridgeLine; MAX_BRIDGES_PER_BUCKET]) {
|
|
|
+) -> Result<(lox_library::cred::Lox, [BridgeLine; MAX_BRIDGES_PER_BUCKET]), Error> {
|
|
|
let (req, state) =
|
|
|
- redeem_invite::request(invite, invitation_pub, get_today(net).await).unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ redeem_invite::request(invite, invitation_pub, get_today(net).await?).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/redeem".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: redeem_invite::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let cred = redeem_invite::handle_response(state, decoded_resp, lox_pub).unwrap();
|
|
|
|
|
|
- let bucket = get_bucket(net, &cred).await.0;
|
|
|
- (cred, bucket)
|
|
|
+ let bucket = get_bucket(net, &cred).await?.0;
|
|
|
+ Ok((cred, bucket))
|
|
|
}
|
|
|
|
|
|
// Check for a migration credential to move to a new bucket
|
|
@@ -230,13 +251,13 @@ pub async fn check_blockage(
|
|
|
net: &dyn Networking,
|
|
|
lox_cred: &lox_library::cred::Lox,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
-) -> lox_library::cred::Migration {
|
|
|
+) -> Result<lox_library::cred::Migration, Error> {
|
|
|
let (req, state) = check_blockage::request(lox_cred, lox_pub).unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net.request("/checkblockage".to_string(), encoded_req).await;
|
|
|
- let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: check_blockage::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let migcred = check_blockage::handle_response(state, decoded_resp).unwrap();
|
|
|
- migcred
|
|
|
+ Ok(migcred)
|
|
|
}
|
|
|
|
|
|
// Migrate to a new bucket (must be level >= 3)
|
|
@@ -246,16 +267,16 @@ pub async fn blockage_migration(
|
|
|
migcred: &lox_library::cred::Migration,
|
|
|
lox_pub: &IssuerPubKey,
|
|
|
migration_pub: &IssuerPubKey,
|
|
|
-) -> lox_library::cred::Lox {
|
|
|
+) -> Result<lox_library::cred::Lox, Error> {
|
|
|
let (req, state) =
|
|
|
blockage_migration::request(lox_cred, migcred, lox_pub, migration_pub).unwrap();
|
|
|
- let encoded_req: Vec<u8> = serde_json::to_vec(&req).unwrap();
|
|
|
+ let encoded_req: Vec<u8> = serde_json::to_vec(&req)?;
|
|
|
let encoded_resp = net
|
|
|
.request("/blockagemigration".to_string(), encoded_req)
|
|
|
.await;
|
|
|
- let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp).unwrap();
|
|
|
+ let decoded_resp: blockage_migration::Response = serde_json::from_slice(&encoded_resp)?;
|
|
|
let cred = blockage_migration::handle_response(state, decoded_resp, lox_pub).unwrap();
|
|
|
- cred
|
|
|
+ Ok(cred)
|
|
|
}
|
|
|
|
|
|
#[cfg(test)]
|