Skip to content

Commit

Permalink
Merge pull request #3713 from BitGo/WP-186-improve-fetchEncryptedPrvK…
Browse files Browse the repository at this point in the history
…eys-script

feat(express): improve fetchEncryptedPrivKeys script
  • Loading branch information
alebusse authored Jul 10, 2023
2 parents e15f9b8 + 8c0f569 commit aaa6b78
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
18 changes: 13 additions & 5 deletions modules/express/EXTERNAL_SIGNER.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ The file is located at `src/fetchEncryptedPrivKeys.ts`. Before using this tool,
1. Fill in the `TODOs` by providing a valid `accessToken` as well as the list of `walletIds`, grouped by the cryptocurrency.
2. Using the same information as #1, update the .env file with the `accessToken` and `walletIds` information.

| Name | Value | Description |
| ---------------------------------- | ------ | ---------------------------------------------- |
| BITGO_EXTERNAL_SIGNER_ENV | string | test |
| BITGO_EXTERNAL_SIGNER_ACCESS_TOKEN | string | Access token used for access BitGo Wallets |
| BITGO_EXTERNAL_SIGNER_WALLET_IDS | string | JSON formatted string of wallets and their ids |
| Name | Value | Description |
| ----------------------------------------- | ------ | ---------------------------------------------- |
| BITGO_EXTERNAL_SIGNER_ENV | string | test |
| BITGO_EXTERNAL_SIGNER_ACCESS_TOKEN | string | Access token used for access BitGo Wallets |
| BITGO_EXTERNAL_SIGNER_WALLET_IDS | string | JSON formatted string of wallets and their ids |
| BITGO_EXTERNAL_SIGNER_WALLET_IDS_WITH_PRV | string | JSON formatted string of wallets ids and |
| | | their encrypted private keys |

BITGO_EXTERNAL_SIGNER_WALLET_IDS examples:

Expand All @@ -43,6 +45,12 @@ BITGO_EXTERNAL_SIGNER_WALLET_IDS={"tbtc":["xxx", "xxx"], "gteth": ["xxx"]}
BITGO_EXTERNAL_SIGNER_WALLET_IDS={"tbtc":[{"walletId":"xxx","walletPassword":"xxx","secret":"xxx"}]}
```

BITGO_EXTERNAL_SIGNER_WALLET_IDS_WITH_PRV examples:

```
BITGO_EXTERNAL_SIGNER_WALLET_IDS_WITH_PRV=[{"walletId":"xxx","encryptedPrv":"xxx"}]
```

Option #2 may make be more convenient for configuration instead of reconfiguring the file every time a new version is released both locally and to Docker.

An example is provided in the file. To run the file, use the command:
Expand Down
19 changes: 19 additions & 0 deletions modules/express/src/fetchEncryptedPrivKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ type WalletIds = {
[key: string]: (string | Credentials)[];
};

type WalletWithPrv = Array<{
walletId: string;
encryptedPrv: string;
}>;

const esAccessToken = process.env.BITGO_EXTERNAL_SIGNER_ACCESS_TOKEN;
const esWalletIDs = process.env.BITGO_EXTERNAL_SIGNER_WALLET_IDS; // example: {"tbtc":[{"walletId":"xxx","walletPassword":"xxx","secret":"xxx"}]}
const esWalletWithPrv = process.env.BITGO_EXTERNAL_SIGNER_WALLET_IDS_WITH_PRV; // example: [{"walletId":"xxx","encryptedPrv":"xxx"}]

// TODO: set env to 'test' or 'prod'
const bg = new BitGo({ env: (process.env.BITGO_EXTERNAL_SIGNER_ENV as EnvironmentName) ?? 'test' });
Expand All @@ -46,6 +52,15 @@ const walletIds: WalletIds = esWalletIDs ? JSON.parse(esWalletIDs) : {};
// gteth: ['61fb21819c54dd000755f8de3a18e333'],
// };

// TODO: set walletId and encryptedPrv here e.g.
const walletWithPrv: WalletWithPrv = esWalletWithPrv ? JSON.parse(esWalletWithPrv) : [];
// [
// {
// walletId: '<WALLET_ID>',
// encryptedPrv: '<ENCRYPTED_PRV>',
// },
// ];

export async function fetchKeys(ids: WalletIds, token: string, accessToken?: string): Promise<Record<string, string>> {
bg.authenticateWithAccessToken({ accessToken: token });

Expand Down Expand Up @@ -73,6 +88,10 @@ export async function fetchKeys(ids: WalletIds, token: string, accessToken?: str
}
}

for (const { walletId, encryptedPrv } of walletWithPrv) {
output[walletId] = encryptedPrv;
}

const data = JSON.stringify(output, null, '\t');
const fileName = 'encryptedPrivKeys.json';
writeFile(fileName, data, (err) => {
Expand Down

0 comments on commit aaa6b78

Please sign in to comment.