Skip to content

Commit 8d52dde

Browse files
morichoEvalirzerosnacks
authored
Support the gcp option in cast wallet list (#8232)
* Support gcp option in `cast wallet list` * implement `gcp_sugners` to `MultiWalletOpts` * add comment * Make gcp option infallible if the env vars are missing * align version with Alloy --------- Co-authored-by: evalir <[email protected]> Co-authored-by: zerosnacks <[email protected]> Co-authored-by: zerosnacks <[email protected]>
1 parent 709f266 commit 8d52dde

File tree

5 files changed

+68
-3
lines changed

5 files changed

+68
-3
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/cast/Cargo.toml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ alloy-provider = { workspace = true, features = [
4242
"ws",
4343
"ipc",
4444
"trace-api",
45-
"txpool-api"
45+
"txpool-api",
4646
] }
4747
alloy-rlp.workspace = true
4848
alloy-rpc-types = { workspace = true, features = ["eth", "trace"] }
@@ -63,6 +63,9 @@ serde.workspace = true
6363
# aws-kms
6464
aws-sdk-kms = { workspace = true, default-features = false, optional = true }
6565

66+
# gcp-kms
67+
gcloud-sdk = { version = "0.26.4", default-features = false, optional = true }
68+
6669
# bin
6770
foundry-cli.workspace = true
6871

@@ -93,4 +96,5 @@ default = ["jemalloc"]
9396
asm-keccak = ["alloy-primitives/asm-keccak"]
9497
jemalloc = ["dep:tikv-jemallocator"]
9598
aws-kms = ["foundry-wallets/aws-kms", "dep:aws-sdk-kms"]
99+
gcp-kms = ["foundry-wallets/gcp-kms", "dep:gcloud-sdk"]
96100
isolate-by-default = ["foundry-config/isolate-by-default"]

crates/cast/src/cmd/wallet/list.rs

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clap::Parser;
22
use eyre::Result;
3+
use std::env;
34

45
use foundry_common::{fs, sh_err, sh_println};
56
use foundry_config::Config;
@@ -25,6 +26,10 @@ pub struct ListArgs {
2526
#[arg(long, hide = !cfg!(feature = "aws-kms"))]
2627
aws: bool,
2728

29+
/// List accounts from Google Cloud KMS.
30+
#[arg(long, hide = !cfg!(feature = "gcp-kms"))]
31+
gcp: bool,
32+
2833
/// List all configured accounts.
2934
#[arg(long, group = "hw-wallets")]
3035
all: bool,
@@ -37,7 +42,10 @@ pub struct ListArgs {
3742
impl ListArgs {
3843
pub async fn run(self) -> Result<()> {
3944
// list local accounts as files in keystore dir, no need to unlock / provide password
40-
if self.dir.is_some() || self.all || (!self.ledger && !self.trezor && !self.aws) {
45+
if self.dir.is_some() ||
46+
self.all ||
47+
(!self.ledger && !self.trezor && !self.aws && !self.gcp)
48+
{
4149
let _ = self.list_local_senders();
4250
}
4351

@@ -47,6 +55,7 @@ impl ListArgs {
4755
.mnemonic_indexes(Some(vec![0]))
4856
.trezor(self.trezor || self.all)
4957
.aws(self.aws || self.all)
58+
.gcp(self.gcp || (self.all && gcp_env_vars_set()))
5059
.interactives(0)
5160
.build()
5261
.expect("build multi wallet");
@@ -108,3 +117,10 @@ impl ListArgs {
108117
Ok(())
109118
}
110119
}
120+
121+
fn gcp_env_vars_set() -> bool {
122+
let required_vars =
123+
["GCP_PROJECT_ID", "GCP_LOCATION", "GCP_KEY_RING", "GCP_KEY_NAME", "GCP_KEY_VERSION"];
124+
125+
required_vars.iter().all(|&var| env::var(var).is_ok())
126+
}

crates/wallets/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ aws-sdk-kms = { workspace = true, default-features = false, optional = true }
3232

3333
# gcp-kms
3434
alloy-signer-gcp = { workspace = true, features = ["eip712"], optional = true }
35-
gcloud-sdk = { version = "0.26", features = [
35+
gcloud-sdk = { version = "0.26.4", features = [
3636
"google-cloud-kms-v1",
3737
"google-longrunning",
3838
], optional = true }

crates/wallets/src/multi_wallet.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ pub struct MultiWalletOpts {
221221
/// Use AWS Key Management Service.
222222
#[arg(long, help_heading = "Wallet options - remote", hide = !cfg!(feature = "aws-kms"))]
223223
pub aws: bool,
224+
225+
/// Use Google Cloud Key Management Service.
226+
#[arg(long, help_heading = "Wallet options - remote", hide = !cfg!(feature = "gcp-kms"))]
227+
pub gcp: bool,
224228
}
225229

226230
impl MultiWalletOpts {
@@ -238,6 +242,9 @@ impl MultiWalletOpts {
238242
if let Some(aws_signers) = self.aws_signers().await? {
239243
signers.extend(aws_signers);
240244
}
245+
if let Some(gcp_signer) = self.gcp_signers().await? {
246+
signers.extend(gcp_signer);
247+
}
241248
if let Some((pending_keystores, unlocked)) = self.keystores()? {
242249
pending.extend(pending_keystores);
243250
signers.extend(unlocked);
@@ -397,6 +404,43 @@ impl MultiWalletOpts {
397404

398405
Ok(None)
399406
}
407+
408+
/// Returns a list of GCP signers if the GCP flag is set.
409+
///
410+
/// The GCP signers are created from the following environment variables:
411+
/// - GCP_PROJECT_ID: The GCP project ID. e.g. `my-project-123456`.
412+
/// - GCP_LOCATION: The GCP location. e.g. `us-central1`.
413+
/// - GCP_KEY_RING: The GCP key ring name. e.g. `my-key-ring`.
414+
/// - GCP_KEY_NAME: The GCP key name. e.g. `my-key`.
415+
/// - GCP_KEY_VERSION: The GCP key version. e.g. `1`.
416+
///
417+
/// For more information on GCP KMS, see the [official documentation](https://cloud.google.com/kms/docs).
418+
pub async fn gcp_signers(&self) -> Result<Option<Vec<WalletSigner>>> {
419+
#[cfg(feature = "gcp-kms")]
420+
if self.gcp {
421+
let mut wallets = vec![];
422+
423+
let project_id = std::env::var("GCP_PROJECT_ID")?;
424+
let location = std::env::var("GCP_LOCATION")?;
425+
let key_ring = std::env::var("GCP_KEY_RING")?;
426+
let key_names = std::env::var("GCP_KEY_NAME")?;
427+
let key_version = std::env::var("GCP_KEY_VERSION")?;
428+
429+
let gcp_signer = WalletSigner::from_gcp(
430+
project_id,
431+
location,
432+
key_ring,
433+
key_names,
434+
key_version.parse()?,
435+
)
436+
.await?;
437+
wallets.push(gcp_signer);
438+
439+
return Ok(Some(wallets));
440+
}
441+
442+
Ok(None)
443+
}
400444
}
401445

402446
#[cfg(test)]

0 commit comments

Comments
 (0)