-
Notifications
You must be signed in to change notification settings - Fork 0
feat(ebe): KMS client and post independent key API #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+683
−545
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| import { BitGo } from 'bitgo'; | ||
| import * as express from 'express'; | ||
| import { KmsClient } from '../../kms/kmsClient'; | ||
|
|
||
| export async function postIndependentKey( | ||
| req: express.Request, | ||
| res: express.Response, | ||
| ): Promise<any> { | ||
| const { source, seed }: { source: string; seed?: string } = req.body; | ||
| if (!source) { | ||
| throw new Error('Source is required for key generation'); | ||
| } | ||
|
|
||
| // setup clients | ||
| const bitgo: BitGo = req.body.bitgo; | ||
| const kms = new KmsClient(); | ||
|
|
||
| // create public and private key pairs on BitGo SDK | ||
| const coin = bitgo.coin(req.params.coin); | ||
| const { pub, prv } = coin.keychains().create(); | ||
|
|
||
| if (!pub) { | ||
| throw new Error('BitGo SDK failed to create public key'); | ||
| } | ||
|
|
||
| // post key to KMS for encryption and storage | ||
| try { | ||
| return await kms.postKey({ | ||
| pub, | ||
| prv, | ||
| coin: req.params.coin, | ||
| source, | ||
| type: 'independent', | ||
| seed, | ||
| }); | ||
| } catch (error: any) { | ||
| res.status(error.status || 500).json({ | ||
| message: error.message || 'Failed to post key to KMS', | ||
| }); | ||
| return; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import debug from 'debug'; | ||
| import * as superagent from 'superagent'; | ||
| import { config, isMasterExpressConfig } from '../config'; | ||
| import { PostKeyKmsSchema, PostKeyParams, PostKeyResponse } from './types/postKey'; | ||
|
|
||
| const debugLogger = debug('bitgo:express:kmsClient'); | ||
|
|
||
| export class KmsClient { | ||
| private readonly url: string; | ||
|
|
||
| constructor() { | ||
| const cfg = config(); | ||
| if (isMasterExpressConfig(cfg)) { | ||
| throw new Error('Configuration is not in enclaved express mode'); | ||
| } | ||
|
|
||
| if (!cfg.kmsUrl) { | ||
| throw new Error('KMS URL not configured. Please set KMS_URL in your environment.'); | ||
| } | ||
|
|
||
| this.url = cfg.kmsUrl; | ||
| debugLogger('kmsClient initialized with URL: %s', this.url); | ||
| } | ||
|
|
||
| async postKey(params: PostKeyParams): Promise<PostKeyResponse> { | ||
| debugLogger('Posting key to KMS: %O', params); | ||
|
|
||
| const kmsResponse = await superagent | ||
| .post(`${this.url}/key`) | ||
| .set('x-api-key', 'abc') | ||
| .send(params); | ||
|
|
||
| try { | ||
| PostKeyKmsSchema.parse(kmsResponse.body); | ||
| } catch (error: any) { | ||
| throw new Error( | ||
| `KMS returned unexpected response${error.message ? `: ${error.message}` : ''}`, | ||
| ); | ||
| } | ||
|
|
||
| const { pub, coin, source } = kmsResponse.body; | ||
| return { pub, coin, source } as PostKeyResponse; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| import * as z from 'zod'; | ||
|
|
||
| export interface PostKeyParams { | ||
| prv: string; | ||
| pub: string; | ||
| coin: string; | ||
| source: string; | ||
| type: 'independent' | 'enclaved'; | ||
| seed?: string; // Optional seed for key generation | ||
| } | ||
|
|
||
| export interface PostKeyResponse { | ||
| pub: string; | ||
| coin: string; | ||
| source: string; | ||
| type: 'independent' | 'enclaved'; | ||
| } | ||
|
|
||
| export const PostKeyKmsSchema = z.object({ | ||
| pub: z.string(), | ||
| coin: z.string(), | ||
| source: z.enum(['independent', 'tss']), | ||
| type: z.enum(['user', 'backup']), | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.