Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: make add_key accept CreateAccountOptions #156

Merged
merged 2 commits into from
Apr 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions integration-tests/tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@ mod key {
.to_string()
}

#[allow(dead_code)]
pub fn malformed() -> String {
let random: String = rand::thread_rng()
.sample_iter(&Alphanumeric)
Expand Down
227 changes: 130 additions & 97 deletions integration-tests/tests/mpc/negative.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,11 @@ async fn test_invalid_token() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: token::invalid(),
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.clone().parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;
assert_eq!(status_code, StatusCode::UNAUTHORIZED);
Expand All @@ -71,18 +75,25 @@ async fn test_invalid_token() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token,
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.clone().parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;

assert_eq!(status_code, StatusCode::OK);
assert!(matches!(
add_key_response,
AddKeyResponse::Ok {
user_public_key: new_pk,
near_account_id: acc_id,
} if new_pk == new_user_public_key && acc_id == account_id.to_string()
));
let AddKeyResponse::Ok {
full_access_keys,
limited_access_keys,
near_account_id,
} = add_key_response else {
anyhow::bail!("unexpected pattern");
};
assert_eq!(full_access_keys, vec![new_user_public_key.clone()]);
assert_eq!(limited_access_keys, Vec::<String>::new());
assert_eq!(near_account_id, account_id.to_string());

tokio::time::sleep(Duration::from_millis(2000)).await;

Expand Down Expand Up @@ -149,7 +160,11 @@ async fn test_malformed_account_id() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(malformed_account_id.to_string()),
oidc_token: oidc_token.clone(),
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;
assert_eq!(status_code, StatusCode::BAD_REQUEST);
Expand All @@ -161,96 +176,24 @@ async fn test_malformed_account_id() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token,
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;
assert_eq!(status_code, StatusCode::OK);
assert!(matches!(
add_key_response,
AddKeyResponse::Ok {
user_public_key: new_pk,
near_account_id: acc_id,
} if new_pk == new_user_public_key && acc_id == account_id.to_string()
));

tokio::time::sleep(Duration::from_millis(2000)).await;

check::access_key_exists(&ctx, &account_id, &new_user_public_key).await?;

Ok(())
})
})
.await
}

#[tokio::test]
async fn test_malformed_public_key() -> anyhow::Result<()> {
with_nodes(1, |ctx| {
Box::pin(async move {
let account_id = account::random(ctx.worker)?;
let malformed_public_key = key::malformed();
let oidc_token = token::valid_random();
let user_public_key = key::random();

let create_account_options = CreateAccountOptions {
full_access_keys: Some(vec![user_public_key.clone().parse().unwrap()]),
limited_access_keys: None,
contract_bytes: None,
let AddKeyResponse::Ok {
full_access_keys,
limited_access_keys,
near_account_id,
} = add_key_response else {
anyhow::bail!("unexpected pattern");
};

// Check that the service is still available
let (status_code, new_acc_response) = ctx
.leader_node
.new_account(NewAccountRequest {
near_account_id: account_id.to_string(),
create_account_options,
oidc_token: oidc_token.clone(),
})
.await?;
assert_eq!(status_code, StatusCode::OK);
assert!(matches!(new_acc_response, NewAccountResponse::Ok {
create_account_options: _,
user_recovery_public_key: _,
near_account_id: acc_id,
} if acc_id == account_id.to_string()
));

tokio::time::sleep(Duration::from_millis(2000)).await;

check::access_key_exists(&ctx, &account_id, &user_public_key).await?;

let (status_code, add_key_response) = ctx
.leader_node
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: oidc_token.clone(),
public_key: malformed_public_key.clone(),
})
.await?;
assert_eq!(status_code, StatusCode::BAD_REQUEST);
assert!(matches!(add_key_response, AddKeyResponse::Err { .. }));

// Check that the service is still available
let new_user_public_key = key::random();

let (status_code, add_key_response) = ctx
.leader_node
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token,
public_key: new_user_public_key.clone(),
})
.await?;

assert_eq!(status_code, StatusCode::OK);

assert!(matches!(
add_key_response,
AddKeyResponse::Ok {
user_public_key: new_pk,
near_account_id: acc_id,
} if new_pk == new_user_public_key && acc_id == account_id.to_string()
));
assert_eq!(full_access_keys, vec![new_user_public_key.clone()]);
assert_eq!(limited_access_keys, Vec::<String>::new());
assert_eq!(near_account_id, account_id.to_string());

tokio::time::sleep(Duration::from_millis(2000)).await;

Expand All @@ -262,6 +205,92 @@ async fn test_malformed_public_key() -> anyhow::Result<()> {
.await
}

// TODO: uncomment once we can malformed payloads again

// #[tokio::test]
// async fn test_malformed_public_key() -> anyhow::Result<()> {
// with_nodes(1, |ctx| {
// Box::pin(async move {
// let account_id = account::random(ctx.worker)?;
// let malformed_public_key = key::malformed();
// let oidc_token = token::valid_random();
// let user_public_key = key::random();

// let create_account_options = CreateAccountOptions {
// full_access_keys: Some(vec![user_public_key.clone().parse().unwrap()]),
// limited_access_keys: None,
// contract_bytes: None,
// };

// // Check that the service is still available
// let (status_code, new_acc_response) = ctx
// .leader_node
// .new_account(NewAccountRequest {
// near_account_id: account_id.to_string(),
// create_account_options,
// oidc_token: oidc_token.clone(),
// })
// .await?;
// assert_eq!(status_code, StatusCode::OK);
// assert!(matches!(new_acc_response, NewAccountResponse::Ok {
// create_account_options: _,
// user_recovery_public_key: _,
// near_account_id: acc_id,
// } if acc_id == account_id.to_string()
// ));

// tokio::time::sleep(Duration::from_millis(2000)).await;

// check::access_key_exists(&ctx, &account_id, &user_public_key).await?;

// let (status_code, add_key_response) = ctx
// .leader_node
// .add_key(AddKeyRequest {
// near_account_id: Some(account_id.to_string()),
// oidc_token: oidc_token.clone(),
// public_key: malformed_public_key.clone(),
// })
// .await?;
// assert_eq!(status_code, StatusCode::BAD_REQUEST);
// assert!(matches!(add_key_response, AddKeyResponse::Err { .. }));

// // Check that the service is still available
// let new_user_public_key = key::random();

// let (status_code, add_key_response) = ctx
// .leader_node
// .add_key(AddKeyRequest {
// near_account_id: Some(account_id.to_string()),
// oidc_token,
// create_account_options: CreateAccountOptions {
// full_access_keys: Some(vec![new_user_public_key.parse()?]),
// limited_access_keys: None,
// contract_bytes: None,
// },
// })
// .await?;

// assert_eq!(status_code, StatusCode::OK);

// let AddKeyResponse::Ok {
// full_access_keys,
// limited_access_keys,
// near_account_id,
// } = add_key_response;
// assert_eq!(full_access_keys, vec![new_user_public_key]);
// assert_eq!(limited_access_keys, Vec::<String>::new());
// assert_eq!(near_account_id, account_id.to_string());

// tokio::time::sleep(Duration::from_millis(2000)).await;

// check::access_key_exists(&ctx, &account_id, &new_user_public_key).await?;

// Ok(())
// })
// })
// .await
// }

#[tokio::test]
async fn test_add_key_to_non_existing_account() -> anyhow::Result<()> {
with_nodes(1, |ctx| {
Expand All @@ -274,7 +303,11 @@ async fn test_add_key_to_non_existing_account() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: token::valid_random(),
public_key: user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![user_public_key.parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;

Expand Down
29 changes: 20 additions & 9 deletions integration-tests/tests/mpc/positive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,17 +95,24 @@ async fn test_basic_action() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token: oidc_token.clone(),
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;
assert_eq!(status_code, StatusCode::OK);
assert!(matches!(
add_key_response,
AddKeyResponse::Ok {
user_public_key: new_pk,
near_account_id: acc_id,
} if new_pk == new_user_public_key && acc_id == account_id.to_string()
));
let AddKeyResponse::Ok {
full_access_keys,
limited_access_keys,
near_account_id,
} = add_key_response else {
anyhow::bail!("unexpected pattern");
};
assert_eq!(full_access_keys, vec![new_user_public_key.clone()]);
assert_eq!(limited_access_keys, Vec::<String>::new());
assert_eq!(near_account_id, account_id.to_string());

tokio::time::sleep(Duration::from_millis(2000)).await;

Expand All @@ -117,7 +124,11 @@ async fn test_basic_action() -> anyhow::Result<()> {
.add_key(AddKeyRequest {
near_account_id: Some(account_id.to_string()),
oidc_token,
public_key: new_user_public_key.clone(),
create_account_options: CreateAccountOptions {
full_access_keys: Some(vec![new_user_public_key.clone().parse()?]),
limited_access_keys: None,
contract_bytes: None,
},
})
.await?;
assert_eq!(status_code, StatusCode::OK);
Expand Down
26 changes: 19 additions & 7 deletions mpc-recovery/src/leader_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,7 @@ async fn new_account<T: OAuthTokenVerifier>(
}

#[derive(thiserror::Error, Debug)]
#[allow(dead_code)]
enum AddKeyError {
#[error("malformed account id: {0}")]
MalformedAccountId(String, ParseAccountError),
Expand Down Expand Up @@ -369,10 +370,6 @@ async fn process_add_key<T: OAuthTokenVerifier>(
internal_acc_id.clone(),
)
.await?;
let new_public_key: PublicKey = request
.public_key
.parse()
.map_err(|e| AddKeyError::MalformedPublicKey(request.public_key, e))?;

let user_account_id: AccountId = match &request.near_account_id {
Some(near_account_id) => near_account_id
Expand All @@ -399,7 +396,7 @@ async fn process_add_key<T: OAuthTokenVerifier>(
let delegate_action = get_add_key_delegate_action(
user_account_id.clone(),
user_recovery_pk.clone(),
new_public_key.clone(),
request.create_account_options.clone(),
nonce,
max_block_height,
)?;
Expand Down Expand Up @@ -432,7 +429,22 @@ async fn process_add_key<T: OAuthTokenVerifier>(
// TODO: Probably need to check more fields
if matches!(resp.status, FinalExecutionStatus::SuccessValue(_)) {
Ok(AddKeyResponse::Ok {
user_public_key: new_public_key.to_string(),
full_access_keys: request
.create_account_options
.clone()
.full_access_keys
.unwrap_or_default()
.into_iter()
.map(|pk| pk.to_string())
.collect(),
limited_access_keys: request
.create_account_options
.clone()
.limited_access_keys
.unwrap_or_default()
.into_iter()
.map(|lak| lak.public_key.to_string())
.collect(),
near_account_id: user_account_id.to_string(),
})
} else {
Expand All @@ -452,7 +464,7 @@ async fn add_key<T: OAuthTokenVerifier>(
Some(ref near_account_id) => near_account_id,
None => "not specified",
},
public_key = &request.public_key,
create_account_options = request.create_account_options.to_string(),
iodc_token = format!("{:.5}...", request.oidc_token),
"add_key request"
);
Expand Down
Loading