Skip to content

Commit

Permalink
feat: add gcp-signer (#165)
Browse files Browse the repository at this point in the history
* feat: add gcp-signer

* chore: format import

* chore: add eyre and adapt signing to aws implementation

* fix example

* add gcp signer to README

* polish

---------

Co-authored-by: Adegbite Ademola Kelvin <adegbiteademolakelvin@Adegbites-MacBook-Pro.local>
Co-authored-by: zerosnacks <zerosnacks@protonmail.com>
  • Loading branch information
3 people authored Dec 9, 2024
1 parent 5a6776f commit 77848d0
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 4 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -96,15 +96,16 @@ needless_return = "allow"

[workspace.dependencies]
alloy = { version = "0.7", features = [
"full",
"eips",
"full",
"hyper",
"json-rpc",
"node-bindings",
"rpc-client",
"rpc-types-debug",
"rpc-types-trace",
"signer-aws",
"signer-gcp",
"signer-keystore",
"signer-ledger",
"signer-mnemonic",
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ This repository contains the following examples:
- [x] [Send transaction with access list](./examples/transactions/examples/with_access_list.rs)
- [x] Wallets
- [x] [AWS signer](./examples/wallets/examples/aws_signer.rs)
- [x] [GCP signer](./examples/wallets/examples/gcp_signer.rs)
- [x] [Ledger signer](./examples/wallets/examples/ledger_signer.rs)
- [x] [Private key signer](./examples/wallets/examples/private_key_signer.rs)
- [x] [Mnemonic signer](./examples/wallets/examples/mnemonic_signer.rs)
Expand Down
4 changes: 4 additions & 0 deletions examples/wallets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ alloy.workspace = true
aws-config = { version = "1.5", default-features = false }
aws-sdk-kms = { version = "1.50", default-features = false }
eyre.workspace = true
gcloud-sdk = {version = "0.25", features = [
"google-cloud-kms-v1",
"google-longrunning"
]}
rand = "0.8"
serde = { workspace = true, features = ["derive"] }
tempfile = "3.14"
Expand Down
4 changes: 1 addition & 3 deletions examples/wallets/examples/aws_signer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ use eyre::Result;

#[tokio::main]
async fn main() -> Result<()> {
let Ok(key_id) = std::env::var("AWS_KEY_ID") else {
return Ok(());
};
let key_id = std::env::var("AWS_KEY_ID").expect("AWS_KEY_ID not set in .env file");

let config = aws_config::load_defaults(BehaviorVersion::latest()).await;
let client = aws_sdk_kms::Client::new(&config);
Expand Down
38 changes: 38 additions & 0 deletions examples/wallets/examples/gcp_signer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
//! Example showing how to use the GCP KMS signer.
use alloy::signers::{
gcp::{GcpKeyRingRef, GcpSigner, KeySpecifier},
Signer,
};
use eyre::{Ok, Result};
use gcloud_sdk::{
google::cloud::kms::v1::key_management_service_client::KeyManagementServiceClient, GoogleApi,
};

#[tokio::main]
async fn main() -> Result<()> {
let project_id =
std::env::var("GOOGLE_PROJECT_ID").expect("GOOGLE_PROJECT_ID not set in .env file");
let location = std::env::var("GOOGLE_LOCATION").expect("GOOGLE_LOCATION not set in .env file");
let keyring = std::env::var("GOOGLE_KEYRING").expect("GOOGLE_KEYRING not set in .env file");
let key_name = std::env::var("GOOGLE_KEY_NAME").expect("GOOGLE_KEY_NAME not set in .env file");

let keyring = GcpKeyRingRef::new(&project_id, &location, &keyring);
let client = GoogleApi::from_function(
KeyManagementServiceClient::new,
"https://cloudkms.googleapis.com",
None,
)
.await?;

let key_version = 1;
let specifier = KeySpecifier::new(keyring, &key_name, key_version);
let signer = GcpSigner::new(client, specifier, Some(key_version)).await?;

let message = "Hello, world!";
let signature = signer.sign_message(message.as_bytes()).await?;

assert_eq!(signature.recover_address_from_msg(message)?, signer.address());

Ok(())
}
2 changes: 2 additions & 0 deletions scripts/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ function main () {
| grep -E '^ ' \
| grep -v \
-e 'any_network' \
-e 'aws_signer' \
-e 'builtin' \
-e 'debug_trace_call_many' \
-e 'gcp_signer' \
-e 'geth_local_instance' \
-e 'ipc' \
-e 'ledger_signer' \
Expand Down

0 comments on commit 77848d0

Please sign in to comment.