Skip to content

Commit

Permalink
Merge branch 'main' into fr/readmeUpdates
Browse files Browse the repository at this point in the history
  • Loading branch information
furqanrydhan committed Sep 8, 2023
2 parents a4f3e05 + 7ff1cf5 commit 07b4bac
Show file tree
Hide file tree
Showing 56 changed files with 809 additions and 255 deletions.
16 changes: 13 additions & 3 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,22 @@
# Either a private key
WALLET_PRIVATE_KEY=<your_admin_wallet_private_key>
# Or from AWS KMS
AWS_KMS_KEY_ID=<your_aws_kms_key_id>
AWS_ACCESS_KEY_ID=<your_aws_access_key_id>
AWS_SECRET_ACCESS_KEY=<your_aws_secret_access_key>
AWS_KMS_KEY_ID=<your_aws_kms_key_id>
AWS_REGION=<your_aws_region>

# Or from GOOGLE KMS
# Required for Google Auth
GOOGLE_APPLICATION_CREDENTIAL_EMAIL=<client_email_from_download_service_account_json>
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY=<private_key_from_download_service_account_json>
# Required for Google KMS
GOOGLE_APPLICATION_PROJECT_ID=<google_project_id>
GOOGLE_KMS_KEY_RING_ID=<key_ring_id>
GOOGLE_KMS_LOCATION_ID=<location_of_key_ring>
GOOGLE_KMS_CRYPTO_KEY_ID=<kms_key_id> # If created on Google Console


# THIRDWEB SDK SECRET KEY [Required]
# -----------------------
# Obtain an Secret Key from thirdweb.com/dashboard
Expand Down Expand Up @@ -57,5 +68,4 @@ BENCHMARK_POST_BODY='{
"args": ["0x1946267d81Fb8aDeeEa28e6B98bcD446c8248473", 100000]
}'
BENCHMARK_CONCURRENCY=10
BENCHMARK_REQUESTS=10

BENCHMARK_REQUESTS=10
33 changes: 33 additions & 0 deletions .github/aws_kms_how_to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
Web3-API supports AWS KMS for signing & sending transactions over any EVM chain. This is a guide on how to set up AWS KMS for Web3-API.

### Steps to set up AWS KMS

1. Create IAM user with programmatic access, see [here](https://docs.aws.amazon.com/IAM/latest/UserGuide/id_users_create.html#id_users_create_console) for more details.
2. Add create, get, read permission to KMS, see [here](https://docs.aws.amazon.com/kms/latest/developerguide/control-access.html) for more details.
3. Create a AWS KMS key, see [here](https://docs.aws.amazon.com/kms/latest/developerguide/create-keys.html) for more details. or, you can use the `/wallet/create` to create a key.

NOTE:

If you are creating the key yourself on AWS KMS Console, then please select the below config to create a key with sign permission.

```
Key Type: Asymmetric
Key Spec: ECC_SECG_P256K1
Key Usage: Sign and verify
```

Once you create the key above, you can use `/wallet/add` and send details on the end-point to create the wallet

### Set up Web3-API with AWS KMS

Create a `.env` file in the root directory of the project and add the below details.

```
# Required for AWS Auth
AWS_ACCESS_KEY_ID=<aws_access_key_id>
AWS_SECRET_ACCESS_KEY=<aws_secret_access_key>
AWS_REGION=<aws_region>
# Required for AWS KMS Admin Wallet
AWS_KMS_KEY_ID=<kms_key_id>
```
32 changes: 32 additions & 0 deletions .github/google_kms_how_to.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
Web3-API supports Google KMS for signing & sending transactions over any EVM chain. This is a guide on how to set up Google KMS for Web3-API.

### Steps to set up Google KMS

1. Enable Google KMS API for your Google project, see [here](https://cloud.google.com/kms/docs/create-encryption-keys#before-you-begin) for more details.
2. Create a Service Account (here)[https://cloud.google.com/iam/docs/service-accounts-create] and create a key under this service account and download the JSON file. This JSON file details will be used to authenticate with Google KMS.
3. Add the below permissions to the service account created in step 2.

```
Cloud KMS Admin
Cloud KMS CryptoKey Signer/Verifier
```

4. Create a keyring in Google KMS, see [here](https://cloud.google.com/kms/docs/create-key-ring) for more details.

Optional: Create a key in the keyring, see [here](https://cloud.google.com/kms/docs/create-key) for more details. or, you can use the `/wallet/create` to create a key in the keyring.

### Set up Web3-API with Google KMS

Create a `.env` file in the root directory of the project and add the below details.

```
# Required for Google Auth
GOOGLE_APPLICATION_CREDENTIAL_EMAIL=<client_email_from_download_service_account_json>
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY=<private_key_from_download_service_account_json>
# Required for Google KMS
GOOGLE_APPLICATION_PROJECT_ID=<google_project_id>
GOOGLE_KMS_KEY_RING_ID=<key_ring_id>
GOOGLE_KMS_LOCATION_ID=<location_of_key_ring>
GOOGLE_KMS_CRYPTO_KEY_ID=<kms_key_id> # If created on Google Console
```
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ View all end-points details (Open API Specification) : [User Guide](./docs/UserG

View all end-points details (Open API Specification) : https://web3-api-akbv.chainsaw-dev.zeet.app

# <<<<<<< HEAD

> > > > > > > main
## Contributing

We welcome contributions from all developers, regardless of experience level. If you are interested in contributing, please read our [Contributing Guide](./.github/contributing.md) where you'll learn how the repo works, how to test your changes, and how to submit a pull request.
Expand Down
17 changes: 15 additions & 2 deletions core/database/dbOperation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ interface WalletExtraData {
walletType?: string;
gcpKmsKeyId?: string;
gcpKmsKeyRingId?: string;
gcpKmsLocationId?: string;
gcpKmsKeyVersionId?: string;
gcpKmsProjectId?: string;
gcpKmsResourcePath?: string;
}

export const insertIntoWallets = async (
Expand Down Expand Up @@ -89,19 +93,28 @@ export const addWalletDataWithSupportChainsNonceToDB = async (
extraTableData?: WalletExtraData,
): Promise<void> => {
try {
server.log.info("Setting up wallet Table");
server.log.info(
`Setting up wallet Table for walletType ${extraTableData?.walletType}, walletAddress ${walletAddress}`,
);
const supportedChains = await getSupportedChains();
const promises = supportedChains.map(async (chain) => {
try {
const { slug } = chain;
let lastUsedNonce = -1;
let walletType = isWeb3APIInitWallet
let walletType = extraTableData?.walletType
? extraTableData?.walletType
: isWeb3APIInitWallet
? getInstanceAdminWalletType()
: getWalletBackUpType();
const sdk = await getSDK(slug, {
walletAddress,
walletType,
awsKmsKeyId: extraTableData?.awsKmsKeyId,
gcpKmsKeyId: extraTableData?.gcpKmsKeyId,
gcpKmsKeyRingId: extraTableData?.gcpKmsKeyRingId,
gcpKmsLocationId: extraTableData?.gcpKmsLocationId,
gcpKmsKeyVersionId: extraTableData?.gcpKmsKeyVersionId,
gcpKmsResourcePath: extraTableData?.gcpKmsResourcePath,
});
walletAddress =
(await sdk.getSigner()?.getAddress())?.toLowerCase() ?? "";
Expand Down
20 changes: 13 additions & 7 deletions core/database/sql-schemas/wallets.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ CREATE TABLE IF NOT EXISTS wallets (
"lastUsedNonce" BIGINT NOT NULL,
"awsKmsKeyId" VARCHAR(255),
"awsKmsArn" VARCHAR(255),
-- "gcpKmsKeyRingId" VARCHAR(50),
-- "gcpKmsKeyId" VARCHAR(50),
-- "gcpKmsKeyVersion" VARCHAR(20),
"gcpKmsKeyRingId" VARCHAR(50),
"gcpKmsKeyId" VARCHAR(50),
"gcpKmsKeyVersionId" VARCHAR(20),
"gcpKmsLocationId" VARCHAR(20),
"gcpKmsResourcePath" TEXT,
PRIMARY KEY ("walletAddress", "chainId")
);

Expand All @@ -22,7 +24,11 @@ ALTER COLUMN "lastUsedNonce" TYPE BIGINT,
ADD COLUMN IF NOT EXISTS "awsKmsKeyId" VARCHAR(255),
ADD COLUMN IF NOT EXISTS "awsKmsArn" VARCHAR(255),
ADD COLUMN IF NOT EXISTS "slug" VARCHAR(255),
DROP COLUMN IF EXISTS "chainName";
-- ADD COLUMN IF NOT EXISTS "gcpKmsKeyRingId" VARCHAR(50),
-- ADD COLUMN IF NOT EXISTS "gcpKmsKeyId" VARCHAR(50),
-- ADD COLUMN IF NOT EXISTS "gcpKmsKeyVersion" VARCHAR(20);
DROP COLUMN IF EXISTS "chainName",
ADD COLUMN IF NOT EXISTS "gcpKmsKeyRingId" VARCHAR(50),
ADD COLUMN IF NOT EXISTS "gcpKmsKeyId" VARCHAR(50),
ADD COLUMN IF NOT EXISTS "gcpKmsKeyVersionId" VARCHAR(20),
ADD COLUMN IF NOT EXISTS "gcpKmsLocationId" VARCHAR(20),
DROP COLUMN IF EXISTS "gcpKmsKeyVersion",
DROP COLUMN IF EXISTS "gcpKmsKeyPath",
ADD COLUMN IF NOT EXISTS "gcpKmsResourcePath" TEXT;
18 changes: 11 additions & 7 deletions core/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export const env = createEnv({
.default("development"),
WALLET_PRIVATE_KEY: z.string().min(1).optional(),
AWS_KMS_KEY_ID: z.string().min(1).optional(),
GOOGLE_KMS_KEY_ID: z.string().min(1).optional(),
AWS_ACCESS_KEY_ID: z.string().min(1).optional(),
AWS_SECRET_ACCESS_KEY: z.string().min(1).optional(),
AWS_REGION: z.string().min(1).optional(),
Expand All @@ -50,9 +51,10 @@ export const env = createEnv({
MINED_TX_CRON_ENABLED: boolSchema("true"),
MINED_TX_CRON_SCHEDULE: z.string().default("*/5 * * * * *"),
MIN_TX_TO_CHECK_FOR_MINED_STATUS: z.coerce.number().default(50),
GCP_PROJECT_ID: z.string().min(1).optional(),
GCP_KEY_RING_ID: z.string().min(1).optional(),
GCP_LOCATION_ID: z.string().min(1).optional(),
GOOGLE_APPLICATION_PROJECT_ID: z.string().min(1).optional(),
GOOGLE_KMS_KEY_RING_ID: z.string().min(1).optional(),
GOOGLE_KMS_LOCATION_ID: z.string().min(1).optional(),
GOOGLE_KMS_KEY_VERSION_ID: z.string().min(1).optional(),
GOOGLE_APPLICATION_CREDENTIAL_EMAIL: z.string().min(1).optional(),
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY: z.string().min(1).optional(),
},
Expand All @@ -63,11 +65,13 @@ export const env = createEnv({
NODE_ENV: process.env.NODE_ENV,
WALLET_PRIVATE_KEY: process.env.WALLET_PRIVATE_KEY,
AWS_KMS_KEY_ID: process.env.AWS_KMS_KEY_ID,
GOOGLE_KMS_KEY_ID: process.env.GOOGLE_KMS_KEY_ID,
AWS_ACCESS_KEY_ID: process.env.AWS_ACCESS_KEY_ID,
AWS_SECRET_ACCESS_KEY: process.env.AWS_SECRET_ACCESS_KEY,
AWS_REGION: process.env.AWS_REGION,
THIRDWEB_API_SECRET_KEY: process.env.THIRDWEB_API_SECRET_KEY,
THIRDWEB_API_ORIGIN: process.env.THIRDWEB_API_ORIGIN,
DATABASE_CLIENT: process.env.DATABASE_CLIENT,
POSTGRES_CONNECTION_URL: process.env.POSTGRES_CONNECTION_URL,
DB_TABLES_LIST: process.env.DB_TABLES_LIST,
DB_TRIGGERS_LIST: process.env.DB_TRIGGERS_LIST,
Expand All @@ -82,10 +86,10 @@ export const env = createEnv({
MINED_TX_CRON_SCHEDULE: process.env.MINED_TX_CRON_SCHEDULE,
MIN_TX_TO_CHECK_FOR_MINED_STATUS:
process.env.MIN_TX_TO_CHECK_FOR_MINED_STATUS,
DATABASE_CLIENT: undefined,
GCP_PROJECT_ID: process.env.GCP_PROJECT_ID,
GCP_KEY_RING_ID: process.env.GCP_KEY_RING_ID,
GCP_LOCATION_ID: process.env.GCP_LOCATION_ID,
GOOGLE_APPLICATION_PROJECT_ID: process.env.GOOGLE_APPLICATION_PROJECT_ID,
GOOGLE_KMS_KEY_RING_ID: process.env.GOOGLE_KMS_KEY_RING_ID,
GOOGLE_KMS_LOCATION_ID: process.env.GOOGLE_KMS_LOCATION_ID,
GOOGLE_KMS_KEY_VERSION_ID: process.env.GOOGLE_KMS_KEY_VERSION_ID,
GOOGLE_APPLICATION_CREDENTIAL_EMAIL:
process.env.GOOGLE_APPLICATION_CREDENTIAL_EMAIL,
GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY:
Expand Down
13 changes: 12 additions & 1 deletion core/helpers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,25 @@ export const getInstanceAdminWalletType = (): string => {
}

// ToDo GCP KMS
return "";
return "gcp_kms";
};

export const getWalletBackUpType = (): string => {
if (AWS_ACCESS_KEY_ID && AWS_SECRET_ACCESS_KEY && AWS_REGION) {
return "aws_kms";
}

if (
env.GOOGLE_APPLICATION_CREDENTIAL_EMAIL &&
env.GOOGLE_APPLICATION_CREDENTIAL_PRIVATE_KEY &&
env.GOOGLE_APPLICATION_PROJECT_ID &&
env.GOOGLE_KMS_KEY_RING_ID &&
env.GOOGLE_KMS_KEY_VERSION_ID &&
env.GOOGLE_KMS_LOCATION_ID
) {
return "gcp_kms";
}

// ToDo GCP KMS
return "ppk";
};
4 changes: 3 additions & 1 deletion core/interfaces/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ export interface WalletData {
gcpKmsKeyId?: string;
awsKmsKeyArn?: string;
gcpKmsKeyRingId?: string;
gcpKmsKeyVersion?: string;
gcpKmsKeyVersionId?: string;
gcpKmsLocationId?: string;
gcpKmsProjectId?: string;
}
Loading

0 comments on commit 07b4bac

Please sign in to comment.