Skip to content

Commit

Permalink
Revert "[2/n] crypto: simplify keystore (MystenLabs#4909)" (MystenLab…
Browse files Browse the repository at this point in the history
…s#4992)

This reverts commit 957d718.
  • Loading branch information
mwtian authored Oct 5, 2022
1 parent 4f5983d commit bd0c07c
Show file tree
Hide file tree
Showing 28 changed files with 238 additions and 284 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 18 additions & 7 deletions crates/sui-benchmark/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use sui_sdk::crypto::{AccountKeystore, FileBasedKeystore};
use sui_sdk::crypto::FileBasedKeystore;
use sui_types::{
base_types::SuiAddress,
crypto::{AccountKeyPair, KeypairTraits, SuiKeyPair},
crypto::{AccountKeyPair, EncodeDecodeBase64, SuiKeyPair},
};

use std::path::PathBuf;
Expand All @@ -14,9 +14,20 @@ pub fn get_ed25519_keypair_from_keystore(
keystore_path: PathBuf,
requested_address: &SuiAddress,
) -> Result<AccountKeyPair> {
let keystore = FileBasedKeystore::new(&keystore_path)?;
match keystore.get_key(requested_address) {
Ok(SuiKeyPair::Ed25519SuiKeyPair(kp)) => Ok(kp.copy()),
_ => Err(anyhow::anyhow!("Unsupported key type")),
}
let keystore = FileBasedKeystore::load_or_create(&keystore_path)?;
let keypair = keystore
.key_pairs()
.iter()
.find(|x| {
let address: SuiAddress = Into::<SuiAddress>::into(&x.public());
address == *requested_address
})
.map(|x| x.encode_base64())
.unwrap();
// TODO(joyqvq): This is a hack to decode base64 keypair with added flag, ok for now since it is for benchmark use.
// Rework to get the typed keypair directly from above.
Ok(match SuiKeyPair::decode_base64(&keypair).unwrap() {
SuiKeyPair::Ed25519SuiKeyPair(x) => x,
_ => panic!("Unexpected keypair type"),
})
}
8 changes: 4 additions & 4 deletions crates/sui-cluster-test/src/cluster.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ use sui::config::SuiClientConfig;
use sui_config::genesis_config::GenesisConfig;
use sui_config::Config;
use sui_config::SUI_KEYSTORE_FILENAME;
use sui_sdk::crypto::AccountKeystore;
use sui_sdk::crypto::FileBasedKeystore;
use sui_sdk::crypto::Keystore;
use sui_sdk::crypto::KeystoreType;
use sui_sdk::ClientType;
use sui_swarm::memory::Node;
use sui_swarm::memory::Swarm;
Expand Down Expand Up @@ -287,9 +285,11 @@ pub async fn new_wallet_context_from_cluster(
let rpc_url = cluster.rpc_url();
info!("Use gateway: {}", &rpc_url);
let keystore_path = temp_dir.path().join(SUI_KEYSTORE_FILENAME);
let mut keystore = Keystore::from(FileBasedKeystore::new(&keystore_path).unwrap());
let keystore = KeystoreType::File(keystore_path);
let address: SuiAddress = key_pair.public().into();
keystore
.init()
.unwrap()
.add_key(SuiKeyPair::Ed25519SuiKeyPair(key_pair))
.unwrap();
SuiClientConfig {
Expand Down
2 changes: 0 additions & 2 deletions crates/sui-cluster-test/src/wallet_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::cluster::new_wallet_context_from_cluster;

use super::Cluster;
use sui::client_commands::WalletContext;
use sui_sdk::crypto::AccountKeystore;
use sui_sdk::SuiClient;
use sui_types::base_types::SuiAddress;
use sui_types::crypto::{KeypairTraits, Signature};
Expand Down Expand Up @@ -69,7 +68,6 @@ impl WalletClient {

pub fn sign(&self, txn_data: &TransactionData, desc: &str) -> Signature {
self.get_wallet()
.config
.keystore
.sign(&self.address, &txn_data.to_bytes())
.unwrap_or_else(|e| panic!("Failed to sign transaction for {}. {}", desc, e))
Expand Down
1 change: 0 additions & 1 deletion crates/sui-faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ sui-node = { path = "../sui-node" }
sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-types = { path = "../sui-types" }
sui-config = { path = "../sui-config" }
sui-sdk = { path = "../sui-sdk" }
telemetry-subscribers.workspace = true
workspace-hack.workspace = true

Expand Down
6 changes: 3 additions & 3 deletions crates/sui-faucet/src/faucet/simple_faucet.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::metrics::FaucetMetrics;
use anyhow::anyhow;
use async_trait::async_trait;

use crate::metrics::FaucetMetrics;
use prometheus::Registry;
use sui_sdk::crypto::AccountKeystore;

// HashSet is in fact used but linter does not think so
#[allow(unused_imports)]
Expand Down Expand Up @@ -259,7 +259,7 @@ impl SimpleFaucet {
.construct_transfer_sui_txn_with_retry(coin_id, signer, recipient, budget, amount, uuid)
.await?;

let signature = context.config.keystore.sign(&signer, &data.to_bytes())?;
let signature = context.keystore.sign(&signer, &data.to_bytes())?;

let tx = Transaction::new(data, signature);
info!(tx_digest = ?tx.digest(), ?recipient, ?coin_id, ?uuid, "Broadcasting transfer obj txn");
Expand Down
12 changes: 5 additions & 7 deletions crates/sui-gateway/src/unit_tests/rpc_server_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ use sui_json_rpc::api::{
RpcGatewayApiClient, RpcReadApiClient, RpcTransactionBuilderClient, WalletSyncApiClient,
};
use sui_json_rpc_types::{GetObjectDataResponse, SuiTransactionResponse, TransactionBytes};
use sui_sdk::crypto::AccountKeystore;
use sui_sdk::crypto::FileBasedKeystore;
use sui_sdk::crypto::Keystore;
use sui_sdk::crypto::KeystoreType;
use sui_types::base_types::ObjectID;
use sui_types::base_types::TransactionDigest;
use sui_types::gas_coin::GAS;
Expand Down Expand Up @@ -54,7 +52,7 @@ async fn test_public_transfer_object() -> Result<(), anyhow::Error> {
.await?;

let keystore_path = test_network.network.dir().join(SUI_KEYSTORE_FILENAME);
let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);
let keystore = KeystoreType::File(keystore_path).init()?;

let signature = keystore.sign(address, &transaction_bytes.tx_bytes.to_vec()?)?;
let tx = Transaction::new(transaction_bytes.to_data().unwrap(), signature);
Expand Down Expand Up @@ -93,7 +91,7 @@ async fn test_publish() -> Result<(), anyhow::Error> {
.await?;

let keystore_path = test_network.network.dir().join(SUI_KEYSTORE_FILENAME);
let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);
let keystore = KeystoreType::File(keystore_path).init()?;
let signature = keystore.sign(address, &transaction_bytes.tx_bytes.to_vec()?)?;

let tx = Transaction::new(transaction_bytes.to_data().unwrap(), signature);
Expand Down Expand Up @@ -141,7 +139,7 @@ async fn test_move_call() -> Result<(), anyhow::Error> {
.await?;

let keystore_path = test_network.network.dir().join(SUI_KEYSTORE_FILENAME);
let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);
let keystore = KeystoreType::File(keystore_path).init()?;

let signature = keystore.sign(address, &transaction_bytes.tx_bytes.to_vec()?)?;

Expand Down Expand Up @@ -194,7 +192,7 @@ async fn test_get_transaction() -> Result<(), anyhow::Error> {
.await?;

let keystore_path = test_network.network.dir().join(SUI_KEYSTORE_FILENAME);
let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);
let keystore = KeystoreType::File(keystore_path).init()?;

let signature = keystore.sign(address, &transaction_bytes.tx_bytes.to_vec()?)?;

Expand Down
1 change: 0 additions & 1 deletion crates/sui-open-rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ sui-json-rpc = { path = "../sui-json-rpc" }
sui-json-rpc-types = { path = "../sui-json-rpc-types" }
sui-json = { path = "../sui-json" }
sui-types = { path = "../sui-types" }
sui-sdk = { path = "../sui-sdk" }
sui-config = { path = "../sui-config" }
test-utils = { path = "../test-utils" }
rand = "0.8.5"
Expand Down
17 changes: 5 additions & 12 deletions crates/sui-open-rpc/src/generate_json_rpc_spec.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;

use clap::ArgEnum;
use clap::Parser;
use hyper::body::Buf;
Expand All @@ -9,11 +13,7 @@ use move_package::BuildConfig;
use pretty_assertions::assert_str_eq;
use serde::Serialize;
use serde_json::{json, Map, Value};
use std::collections::BTreeMap;
use std::fs::File;
use std::io::Write;
use sui_json_rpc::api::EventReadApiOpenRpc;
use sui_sdk::crypto::AccountKeystore;
use sui_types::messages::Transaction;

use crate::examples::RpcExampleProvider;
Expand Down Expand Up @@ -136,13 +136,7 @@ async fn create_response_sample() -> Result<
let config = working_dir.join(SUI_CLIENT_CONFIG);

let mut context = WalletContext::new(&config).await?;
let address = context
.config
.keystore
.addresses()
.first()
.cloned()
.unwrap();
let address = context.keystore.addresses().first().cloned().unwrap();

context
.client
Expand Down Expand Up @@ -409,7 +403,6 @@ async fn create_error_response(
.await?;

let signature = context
.config
.keystore
.sign(&address, &response.tx_bytes.to_vec()?)?;

Expand Down
1 change: 0 additions & 1 deletion crates/sui-rosetta/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ sui-types = { path = "../sui-types" }
sui-core = { path = "../sui-core" }
sui-node = { path = "../sui-node" }
sui-config = { path = "../sui-config" }
sui-sdk = { path = "../sui-sdk" }

move-core-types.workspace = true

Expand Down
4 changes: 2 additions & 2 deletions crates/sui-rosetta/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,12 +170,12 @@ fn read_prefunded_account(path: &Path) -> Result<Vec<PrefundedAccount>, anyhow::

#[test]
fn test_read_keystore() {
use sui_sdk::crypto::{AccountKeystore, FileBasedKeystore, Keystore};
use sui_sdk::crypto::KeystoreType;
use sui_types::crypto::SignatureScheme;

let temp_dir = tempfile::tempdir().unwrap();
let path = temp_dir.path().join("sui.keystore");
let mut ks = Keystore::from(FileBasedKeystore::new(&path).unwrap());
let mut ks = KeystoreType::File(path.clone()).init().unwrap();
let key1 = ks.generate_new_key(SignatureScheme::ED25519, None).unwrap();
let key2 = ks
.generate_new_key(SignatureScheme::Secp256k1, None)
Expand Down
7 changes: 3 additions & 4 deletions crates/sui-sdk/examples/tic_tac_toe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ use clap::Parser;
use clap::Subcommand;
use serde::Deserialize;

use sui_sdk::crypto::{AccountKeystore, FileBasedKeystore};
use sui_sdk::{
crypto::Keystore,
crypto::{KeystoreType, SuiKeystore},
json::SuiJsonValue,
rpc_types::SuiData,
types::{
Expand All @@ -30,7 +29,7 @@ use sui_sdk::{
async fn main() -> Result<(), anyhow::Error> {
let opts: TicTacToeOpts = TicTacToeOpts::parse();
let keystore_path = opts.keystore_path.unwrap_or_else(default_keystore_path);
let keystore = Keystore::File(FileBasedKeystore::new(&keystore_path)?);
let keystore = KeystoreType::File(keystore_path).init()?;

let game = TicTacToe {
game_package_id: opts.game_package_id,
Expand All @@ -56,7 +55,7 @@ async fn main() -> Result<(), anyhow::Error> {
struct TicTacToe {
game_package_id: ObjectID,
client: SuiClient,
keystore: Keystore,
keystore: SuiKeystore,
}

impl TicTacToe {
Expand Down
6 changes: 3 additions & 3 deletions crates/sui-sdk/examples/transfer_coins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use std::str::FromStr;
use sui_sdk::{
crypto::{AccountKeystore, FileBasedKeystore, Keystore},
crypto::KeystoreType,
types::{
base_types::{ObjectID, SuiAddress},
messages::Transaction,
Expand All @@ -30,8 +30,8 @@ async fn main() -> Result<(), anyhow::Error> {
.transfer_sui(my_address, gas_object_id, 1000, recipient, Some(1000))
.await?;

// Sign transaction
let keystore = Keystore::from(FileBasedKeystore::new(&keystore_path)?);
// Get signer from keystore
let keystore = KeystoreType::File(keystore_path).init()?;
let signature = keystore.sign(&my_address, &transfer_tx.to_bytes())?;

// Execute the transaction
Expand Down
Loading

0 comments on commit bd0c07c

Please sign in to comment.