|
| 1 | +# Using Google Cloud KMS for Secure EVM Transaction Signing in OpenZeppelin Relayer |
| 2 | + |
| 3 | +This example demonstrates how to use Google Cloud KMS hosted private key to securely sign Stellar transactions in OpenZeppelin Relayer. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +1. A Google Cloud Platform account with KMS API enabled - [Get Started](https://cloud.google.com/kms) |
| 8 | +2. Rust and Cargo installed |
| 9 | +3. Git |
| 10 | +4. [Docker](https://docs.docker.com/get-docker/) |
| 11 | +5. [Docker Compose](https://docs.docker.com/compose/install/) |
| 12 | + |
| 13 | +## Getting Started |
| 14 | + |
| 15 | +### Step 1: Clone the Repository |
| 16 | + |
| 17 | +Clone this repository to your local machine: |
| 18 | + |
| 19 | +```bash |
| 20 | +git clone https://github.com/OpenZeppelin/openzeppelin-relayer |
| 21 | +cd openzeppelin-relayer |
| 22 | +``` |
| 23 | + |
| 24 | +### Step 2: Create KMS Key |
| 25 | + |
| 26 | +1. Login to Google Cloud Console |
| 27 | +2. Navigate to Security -> Key Management |
| 28 | +3. Create a new key ring or use an existing one |
| 29 | +4. Click "Create Key" and choose the following options: |
| 30 | + 1. Protection level: Software |
| 31 | + 2. Purpose: Asymmetric sign |
| 32 | + 3. Algorithm: Elliptic Curve ED25519 Key |
| 33 | +5. Take note of: |
| 34 | + - Project ID |
| 35 | + - Location (region) |
| 36 | + - Key ring ID |
| 37 | + - Key ID |
| 38 | + - Key version (usually 1 for new keys) |
| 39 | + |
| 40 | +### Step 3: Setup Google Cloud Service Account |
| 41 | + |
| 42 | +1. Go to IAM & Admin -> Service Accounts |
| 43 | +2. Create a new service account or use an existing one |
| 44 | +3. Grant the following roles: |
| 45 | + - Cloud KMS CryptoKey Signer |
| 46 | + - Cloud KMS CryptoKey Public Key Viewer |
| 47 | +4. Create and download a JSON key file for the service account |
| 48 | +5. Extract the following values from the JSON file: |
| 49 | + - `project_id` |
| 50 | + - `private_key_id` |
| 51 | + - `private_key` (PEM format) |
| 52 | + - `client_email` |
| 53 | + - `client_id` |
| 54 | + |
| 55 | +### Step 4: Configure the Relayer Service |
| 56 | + |
| 57 | +Create an environment file by copying the example: |
| 58 | + |
| 59 | +```bash |
| 60 | +cp examples/stellar-gcp-kms-signer/.env.example examples/stellar-gcp-kms-signer/.env |
| 61 | +``` |
| 62 | + |
| 63 | +#### Populate Google Cloud KMS config |
| 64 | + |
| 65 | +Edit the `config.json` file and update the following variables: |
| 66 | + |
| 67 | +```json |
| 68 | +{ |
| 69 | + "signers": [ |
| 70 | + { |
| 71 | + "id": "gcp-kms-signer-stellar", |
| 72 | + "type": "google_cloud_kms", |
| 73 | + "config": { |
| 74 | + "service_account": { |
| 75 | + "project_id": "your-gcp-project-id", |
| 76 | + "private_key_id": { |
| 77 | + "type": "env", |
| 78 | + "value": "GCP_PRIVATE_KEY_ID" |
| 79 | + }, |
| 80 | + "private_key": { |
| 81 | + "type": "env", |
| 82 | + "value": "GCP_PRIVATE_KEY" |
| 83 | + }, |
| 84 | + "client_email": { |
| 85 | + "type": "env", |
| 86 | + "value": "GCP_CLIENT_EMAIL" |
| 87 | + }, |
| 88 | + "client_id": "your-client-id" |
| 89 | + }, |
| 90 | + "key": { |
| 91 | + "location": "us-west2", |
| 92 | + "key_ring_id": "your-key-ring", |
| 93 | + "key_id": "your-key-id", |
| 94 | + "key_version": 1 |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +#### Populate Google Cloud KMS Credentials |
| 103 | + |
| 104 | +Add these values to your `.env` file (extracted from the service account JSON): |
| 105 | + |
| 106 | +```env |
| 107 | +GCP_PRIVATE_KEY_ID="private_key_id_from_service_account_json" |
| 108 | +GCP_PRIVATE_KEY="-----BEGIN PRIVATE EXAMPLE KEY-----\n...\n-----END PRIVATE EXAMPLE KEY-----\n" |
| 109 | +GCP_CLIENT_EMAIL="[email protected]" |
| 110 | +``` |
| 111 | + |
| 112 | +#### Generate Security Keys |
| 113 | + |
| 114 | +Generate random keys for API authentication and webhook signing: |
| 115 | + |
| 116 | +```bash |
| 117 | +# Generate API key |
| 118 | +cargo run --example generate_uuid |
| 119 | + |
| 120 | +# Generate webhook signing key |
| 121 | +cargo run --example generate_uuid |
| 122 | +``` |
| 123 | + |
| 124 | +Add these to your `.env` file: |
| 125 | + |
| 126 | +```env |
| 127 | +WEBHOOK_SIGNING_KEY=generated_webhook_key |
| 128 | +API_KEY=generated_api_key |
| 129 | +``` |
| 130 | + |
| 131 | +#### Configure Webhook URL |
| 132 | + |
| 133 | +Update the `examples/stellar-gcp-kms-signer/config/config.json` file with your webhook configuration: |
| 134 | + |
| 135 | +1. For testing, get a webhook URL from [Webhook.site](https://webhook.site) |
| 136 | +2. Update the config file: |
| 137 | + |
| 138 | +```json |
| 139 | +{ |
| 140 | + "notifications": [ |
| 141 | + { |
| 142 | + "url": "your_webhook_url" |
| 143 | + } |
| 144 | + ] |
| 145 | +} |
| 146 | +``` |
| 147 | + |
| 148 | +### Step 5: Run the Service |
| 149 | + |
| 150 | +Start the service with Docker Compose: |
| 151 | + |
| 152 | +```bash |
| 153 | +docker compose -f examples/stellar-gcp-kms-signer/docker-compose.yaml up |
| 154 | +``` |
| 155 | + |
| 156 | +### Step 6: Test the Service |
| 157 | + |
| 158 | +#### 6.1 Check Relayer Status |
| 159 | + |
| 160 | +First, verify that your relayer is running and properly configured: |
| 161 | + |
| 162 | +```bash |
| 163 | +curl -X GET http://localhost:8080/api/v1/relayers \ |
| 164 | + -H "Content-Type: application/json" \ |
| 165 | + -H "AUTHORIZATION: Bearer YOUR_API_KEY" |
| 166 | +``` |
| 167 | + |
| 168 | +This should return information about your relayer, including its address derived from the Google Cloud KMS public key. |
| 169 | + |
| 170 | +#### 6.2 Test Stellar Transaction Signing |
| 171 | + |
| 172 | +Test the complete transaction signing and submission process: |
| 173 | + |
| 174 | +```bash |
| 175 | +curl -X POST http://localhost:8080/api/v1/relayers/your-relayer-id/transactions \ |
| 176 | + -H "Content-Type: application/json" \ |
| 177 | + -H "AUTHORIZATION: Bearer YOUR_API_KEY" \ |
| 178 | + -d '{ |
| 179 | + "value": 1, |
| 180 | + "data": "0x", |
| 181 | + "to": "0x742d35cc6604c532532db3ae0f4d03e7c7b17e3e", |
| 182 | + "gas_limit": 21000, |
| 183 | + "speed": "average" |
| 184 | + }' |
| 185 | +``` |
| 186 | + |
| 187 | +**What this does:** |
| 188 | + |
| 189 | +- Creates a transaction sending 1 wei to the specified address |
| 190 | +- Uses Google Cloud KMS to sign the transaction |
| 191 | +- Submits the signed transaction to the network |
| 192 | +- Returns transaction details including the transaction hash |
| 193 | + |
| 194 | +### Troubleshooting |
| 195 | + |
| 196 | +If you encounter issues: |
| 197 | + |
| 198 | +1. **Authentication Issues**: |
| 199 | + |
| 200 | + - Verify your service account JSON is correct |
| 201 | + - Ensure the service account has the required KMS permissions |
| 202 | + - Check that the project ID matches your GCP project |
| 203 | + |
| 204 | +2. **Key Access Issues**: |
| 205 | + |
| 206 | + - Verify the key location, key ring ID, and key ID are correct |
| 207 | + - Ensure the key is created with the correct algorithm (Elliptic Curve ED25519) |
| 208 | + - Check that the key version exists |
| 209 | + |
| 210 | +3. **Signing Failures**: |
| 211 | + |
| 212 | + - Verify the key has signing permissions |
| 213 | + - Check that the key algorithm supports ED25519 operations |
| 214 | + - Review service logs for detailed error messages |
| 215 | + |
| 216 | +4. **Network Issues**: |
| 217 | + - Ensure your environment can reach Google Cloud KMS APIs |
| 218 | + - Check firewall settings if running in a restricted environment |
| 219 | + |
| 220 | +### Additional Resources |
| 221 | + |
| 222 | +- [Google Cloud KMS Documentation](https://cloud.google.com/kms/docs) |
| 223 | +- [Service Account Authentication](https://cloud.google.com/docs/authentication/getting-started) |
| 224 | +- [KMS Key Management](https://cloud.google.com/kms/docs/creating-keys) |
| 225 | +- [OpenZeppelin Relayer Documentation](https://docs.openzeppelin.com/relayer) |
0 commit comments