alloy_signer_gcp/
signer.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
use alloy_consensus::SignableTransaction;
use alloy_primitives::{hex, Address, ChainId, PrimitiveSignature as Signature, B256};
use alloy_signer::{sign_transaction_with_chain_id, Result, Signer};
use async_trait::async_trait;
use gcloud_sdk::{
    google::cloud::kms::{
        self,
        v1::{
            key_management_service_client::KeyManagementServiceClient, AsymmetricSignRequest,
            GetPublicKeyRequest, PublicKey,
        },
    },
    tonic::{self, Request},
    GoogleApi, GoogleAuthMiddleware,
};
use k256::ecdsa::{self, VerifyingKey};
use spki::DecodePublicKey;
use std::{fmt, fmt::Debug};
use thiserror::Error;

type Client = GoogleApi<KeyManagementServiceClient<GoogleAuthMiddleware>>;

/// Reference to a GCP KeyRing.
#[derive(Clone, Debug)]
pub struct GcpKeyRingRef {
    /// The GCP project ID.
    pub google_project_id: String,
    /// The GCP location e.g. `global`.
    pub location: String,
    /// The GCP key ring name.
    pub name: String,
}

impl GcpKeyRingRef {
    /// Create a new GCP KeyRing reference.
    pub fn new(google_project_id: &str, location: &str, name: &str) -> Self {
        Self {
            google_project_id: google_project_id.to_string(),
            location: location.to_string(),
            name: name.to_string(),
        }
    }
}

/// Identifies a specific key version in the key ring.
#[derive(Debug)]
pub struct KeySpecifier(String);

impl KeySpecifier {
    /// Construct a new specifier for a key with a given keyring, id and version.
    pub fn new(keyring: GcpKeyRingRef, key_id: &str, version: u64) -> Self {
        Self(format!(
            "projects/{}/locations/{}/keyRings/{}/cryptoKeys/{}/cryptoKeyVersions/{}",
            keyring.google_project_id, keyring.location, keyring.name, key_id, version,
        ))
    }
}

/// Google Cloud Platform Key Management Service (GCP KMS) Ethereum signer.
///
/// The GCP Signer passes signing requests to the cloud service. GCP KMS keys belong to a key ring,
/// which is identified by a project ID, location, and key ring name. The key ring contains keys,
/// which are identified by a key ID and version.
///
/// Because the public key is unknown, we retrieve it on instantiation of the signer. This means
/// that the new function is `async` and must be called within some runtime.
///
/// Note that this wallet only supports asynchronous operations. Calling a non-asynchronous method
/// will always return an error.
///
/// # Examples
///
/// ```no_run
/// use alloy_signer::Signer;
/// use alloy_signer_gcp::{GcpKeyRingRef, GcpSigner, KeySpecifier};
/// use gcloud_sdk::{
///     google::cloud::kms::v1::key_management_service_client::KeyManagementServiceClient,
///     GoogleApi,
/// };
///
/// # async fn test() {
///
/// let project_id = std::env::var("GOOGLE_PROJECT_ID").expect("GOOGLE_PROJECT_ID");
/// let location = std::env::var("GOOGLE_LOCATION").expect("GOOGLE_LOCATION");
/// let keyring_name = std::env::var("GOOGLE_KEYRING").expect("GOOGLE_KEYRING");
///
/// let keyring = GcpKeyRingRef::new(&project_id, &location, &keyring_name);
/// let client = GoogleApi::from_function(
///     KeyManagementServiceClient::new,
///     "https://cloudkms.googleapis.com",
///     None,
/// )
/// .await
/// .expect("Failed to create GCP KMS Client");
///
/// let key_name = "...";
/// let key_version = 1;
/// let key_specifier = KeySpecifier::new(keyring, key_name, key_version);
/// let chain_id = Some(1);
/// let signer = GcpSigner::new(client, key_specifier, chain_id).await.unwrap();
///
/// let message = vec![0, 1, 2, 3];
///
/// let sig = signer.sign_message(&message).await.unwrap();
/// assert_eq!(sig.recover_address_from_msg(message).unwrap(), signer.address());
/// # }
/// ```
#[derive(Clone)]
pub struct GcpSigner {
    client: Client,
    key_name: String,
    chain_id: Option<ChainId>,
    pubkey: VerifyingKey,
    address: Address,
}

impl fmt::Debug for GcpSigner {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("GcpSigner")
            .field("key_name", &self.key_name)
            .field("chain_id", &self.chain_id)
            .field("pubkey", &hex::encode(self.pubkey.to_sec1_bytes()))
            .field("address", &self.address)
            .finish()
    }
}

/// Errors thrown by [`GcpSigner`].
#[derive(Debug, Error)]
pub enum GcpSignerError {
    /// Thrown when the GCP KMS API returns a signing error.
    #[error(transparent)]
    GoogleKmsError(#[from] gcloud_sdk::error::Error),

    /// Thrown on a request error.
    #[error(transparent)]
    RequestError(#[from] tonic::Status),

    /// [`spki`] error.
    #[error(transparent)]
    Spki(#[from] spki::Error),

    /// [`ecdsa`] error.
    #[error(transparent)]
    K256(#[from] ecdsa::Error),
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl alloy_network::TxSigner<Signature> for GcpSigner {
    fn address(&self) -> Address {
        self.address
    }

    #[inline]
    #[doc(alias = "sign_tx")]
    async fn sign_transaction(
        &self,
        tx: &mut dyn SignableTransaction<Signature>,
    ) -> Result<Signature> {
        sign_transaction_with_chain_id!(self, tx, self.sign_hash(&tx.signature_hash()).await)
    }
}

#[cfg_attr(target_arch = "wasm32", async_trait(?Send))]
#[cfg_attr(not(target_arch = "wasm32"), async_trait)]
impl Signer for GcpSigner {
    #[instrument(err)]
    #[allow(clippy::blocks_in_conditions)]
    async fn sign_hash(&self, hash: &B256) -> Result<Signature> {
        self.sign_digest_inner(hash).await.map_err(alloy_signer::Error::other)
    }

    #[inline]
    fn address(&self) -> Address {
        self.address
    }

    #[inline]
    fn chain_id(&self) -> Option<ChainId> {
        self.chain_id
    }

    #[inline]
    fn set_chain_id(&mut self, chain_id: Option<ChainId>) {
        self.chain_id = chain_id;
    }
}

impl GcpSigner {
    /// Instantiate a new signer from an existing `Client`, keyring reference, key ID, and version.
    ///
    /// Retrieves the public key from GCP and calculates the Ethereum address.
    #[instrument(skip(client), err)]
    pub async fn new(
        client: Client,
        key_specifier: KeySpecifier,
        chain_id: Option<ChainId>,
    ) -> Result<Self, GcpSignerError> {
        let key_name = key_specifier.0;
        let resp = request_get_pubkey(&client, &key_name).await?;
        let pubkey = decode_pubkey(resp)?;
        let address = alloy_signer::utils::public_key_to_address(&pubkey);
        debug!(?pubkey, %address, "instantiated GCP signer");
        Ok(Self { client, key_name, chain_id, pubkey, address })
    }

    /// Fetch the pubkey associated with this signer's key.
    pub async fn get_pubkey(&self) -> Result<VerifyingKey, GcpSignerError> {
        request_get_pubkey(&self.client, &self.key_name).await.and_then(decode_pubkey)
    }

    /// Sign a digest with this signer's key
    pub async fn sign_digest(&self, digest: &B256) -> Result<ecdsa::Signature, GcpSignerError> {
        request_sign_digest(&self.client, &self.key_name, digest).await.and_then(decode_signature)
    }

    /// Sign a digest with this signer's key and add the eip155 `v` value
    /// corresponding to the input chain_id
    #[instrument(err, skip(digest), fields(digest = %hex::encode(digest)))]
    async fn sign_digest_inner(&self, digest: &B256) -> Result<Signature, GcpSignerError> {
        let sig = self.sign_digest(digest).await?;
        Ok(sig_from_digest_bytes_trial_recovery(sig, digest, &self.pubkey))
    }
}

#[instrument(skip(client), err)]
async fn request_get_pubkey(
    client: &Client,
    kms_key_name: &str,
) -> Result<PublicKey, GcpSignerError> {
    let mut request = tonic::Request::new(GetPublicKeyRequest { name: kms_key_name.to_string() });
    request
        .metadata_mut()
        .insert("x-goog-request-params", format!("name={}", &kms_key_name).parse().unwrap());
    client.get().get_public_key(request).await.map(|r| r.into_inner()).map_err(Into::into)
}

#[instrument(skip(client, digest), fields(digest = %hex::encode(digest)), err)]
async fn request_sign_digest(
    client: &Client,
    kms_key_name: &str,
    digest: &B256,
) -> Result<Vec<u8>, GcpSignerError> {
    let mut request = Request::new(AsymmetricSignRequest {
        name: kms_key_name.to_string(),
        digest: Some(kms::v1::Digest {
            digest: Some(kms::v1::digest::Digest::Sha256(digest.to_vec())),
        }),
        ..Default::default()
    });

    // Add metadata for request routing: https://cloud.google.com/kms/docs/grpc
    request
        .metadata_mut()
        .insert("x-goog-request-params", format!("name={}", kms_key_name).parse().unwrap());

    let response = client.get().asymmetric_sign(request).await?;
    let signature = response.into_inner().signature;
    Ok(signature)
}

/// Parse the PEM-encoded public key returned by GCP KMS.
fn decode_pubkey(key: PublicKey) -> Result<VerifyingKey, GcpSignerError> {
    VerifyingKey::from_public_key_pem(&key.pem).map_err(Into::into)
}

/// Decode a raw GCP KMS Signature response.
fn decode_signature(raw: Vec<u8>) -> Result<ecdsa::Signature, GcpSignerError> {
    let sig = ecdsa::Signature::from_der(raw.as_ref())?;
    Ok(sig.normalize_s().unwrap_or(sig))
}

/// Recover an RSig from a signature under a known key by trial/error.
fn sig_from_digest_bytes_trial_recovery(
    sig: ecdsa::Signature,
    hash: &B256,
    pubkey: &VerifyingKey,
) -> Signature {
    let signature = Signature::from_signature_and_parity(sig, false);
    if check_candidate(&signature, hash, pubkey) {
        return signature;
    }

    let signature = signature.with_parity(true);
    if check_candidate(&signature, hash, pubkey) {
        return signature;
    }

    panic!("bad sig");
}

/// Makes a trial recovery to check whether an RSig corresponds to a known `VerifyingKey`.
fn check_candidate(signature: &Signature, hash: &B256, pubkey: &VerifyingKey) -> bool {
    signature.recover_from_prehash(hash).map(|key| key == *pubkey).unwrap_or(false)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn sign_message() {
        if std::env::var("GOOGLE_APPLICATION_CREDENTIALS").is_err() {
            return;
        }

        let project_id = std::env::var("GOOGLE_PROJECT_ID").expect("GOOGLE_PROJECT_ID");
        let location = std::env::var("GOOGLE_LOCATION").expect("GOOGLE_LOCATION");
        let keyring = std::env::var("GOOGLE_KEYRING").expect("GOOGLE_KEYRING");
        let key_name = std::env::var("GOOGLE_KEY_NAME").expect("GOOGLE_KEY_NAME");

        let keyring = GcpKeyRingRef::new(&project_id, &location, &keyring);
        let client = GoogleApi::from_function(
            KeyManagementServiceClient::new,
            "https://cloudkms.googleapis.com",
            None,
        )
        .await
        .expect("Failed to create GCP KMS Client");
        let key_version = 1;

        let specifier = KeySpecifier::new(keyring, &key_name, key_version);
        let signer = GcpSigner::new(client, specifier, None).await.expect("get key");

        let message = vec![0, 1, 2, 3];
        let sig = signer.sign_message(&message).await.unwrap();
        assert_eq!(sig.recover_address_from_msg(message).unwrap(), signer.address());
    }
}