yoshidan / google-cloud-rust

Google Cloud Client Libraries for Rust.
MIT License
241 stars 86 forks source link

[kms] Add feature to sign ethereum transaction #276

Closed yoshidan closed 3 months ago

yoshidan commented 3 months ago

Integration with https://github.com/gakonst/ethers-rs.

Added since there did not seem to be any plans to support Google Cloud KMS in https://github.com/gakonst/ethers-rs/issues/1.

I would like to port it to ethers-rs if possible.

 use ethers::prelude::SignerMiddleware;
 use ethers::providers::{Http, Middleware, Provider};
 use ethers_core::types::{TransactionReceipt, TransactionRequest};
 use ethers_signers::Signer as EthSigner;
 use google_cloud_kms::client::Client;
 use google_cloud_kms::signer::ethereum::{Error, Signer};

 pub async fn send_bnb(client: Client, key_name: &str) {

     // BSC testnet
     let chain_id = 97;

     let signer = Signer::new(client, key_name, chain_id, None).await.unwrap();
     let provider = Provider::<Http>::try_from("https://bsc-testnet-rpc.publicnode.com").unwrap();

     let eth_client = SignerMiddleware::new_with_provider_chain(provider, signer).await.unwrap();

     let tx = TransactionRequest::new()
             .to(signer.address())
             .value(100_000_000_000_000_u128)
             .gas(1_500_000_u64)
             .gas_price(4_000_000_000_u64)
             .chain_id(chain_id);

     let res = eth_client.send_transaction(tx, None).await.unwrap();
     let receipt: TransactionReceipt = res.confirmations(3).await.unwrap().unwrap();
 }