Skip to content

feat(mbe): use generated client for type safety#32

Merged
mohammadalfaiyazbitgo merged 1 commit into
masterfrom
WP-000000/qol-api-specs
Jun 18, 2025
Merged

feat(mbe): use generated client for type safety#32
mohammadalfaiyazbitgo merged 1 commit into
masterfrom
WP-000000/qol-api-specs

Conversation

@mohammadalfaiyazbitgo

Copy link
Copy Markdown
Contributor

Ticket: WP-000000

@mohammadalfaiyazbitgo mohammadalfaiyazbitgo marked this pull request as ready for review June 18, 2025 01:41

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR replaces manual HTTP/TLS logic with a generated, type-safe API client and centralizes health response types.

  • Introduce shared PingResponseType and VersionResponseType for health endpoints
  • Remove legacy certificate utilities and inline health route setup
  • Swap raw superagent calls for buildApiClient-based requests and add version route

Reviewed Changes

Copilot reviewed 13 out of 13 changed files in this pull request and generated no comments.

Show a summary per file
File Description
src/types/health.ts Add shared io-ts types for ping and version responses
src/shared/appUtils.ts Remove readCertificates and setupHealthCheckRoutes
src/routes/enclaved.ts Update mounting of keygen router (drop /api prefix)
src/masterBitgoExpress/routers/index.ts Include EnclavedExpressApiSpec in combined spec
src/masterBitgoExpress/routers/healthCheck.ts Use imported health types and simplify router creation
src/masterBitgoExpress/routers/enclavedExpressHealth.ts Replace raw HTTPS logic with generated client and add version route
src/masterBitgoExpress/enclavedExpressClient.ts Build and use a type-safe API client for all enclaved calls
src/enclavedBitgoExpress/routers/enclavedApiSpec.ts Prefix key/multisig routes with /api
src/enclavedBitgoExpress/routers/index.ts Export consolidated EnclavedApiSpec
src/tests/masterBitgoExpress/sendMany.test.ts Update error assertion for missing enclave config
src/tests/masterBitgoExpress/generateWallet.test.ts Update error assertion for missing enclave config
masterBitgoExpress.json Extend OpenAPI with shared health schemas and version endpoint
Comments suppressed due to low confidence (5)

src/enclavedBitgoExpress/routers/index.ts:1

  • [nitpick] The constant name EnclavedAPiSpec uses inconsistent casing (APi vs Api). Rename it to EnclavedApiSpec for clarity and consistency.
import { EnclavedAPiSpec as ApiSpec } from './enclavedApiSpec';

src/masterBitgoExpress/routers/enclavedExpressHealth.ts:88

  • The new version endpoint (/version/enclavedExpress) lacks corresponding tests. Consider adding unit or integration tests to verify its behavior.
  router.get('v1.enclaved.version', [

src/routes/enclaved.ts:18

  • Removing the /api prefix here may break existing routes. Consider mounting the keygen router with a base path, e.g., app.use('/api', createKeyGenRouter(config));.
  app.use(createKeyGenRouter(config));

src/masterBitgoExpress/enclavedExpressClient.ts:153

  • The client is referencing v1.health.ping but the generated spec defines v1.enclaved.ping. Update the key to match this.apiClient['v1.enclaved.ping'].
      let request = this.apiClient['v1.health.ping'].post({});

src/masterBitgoExpress/enclavedExpressClient.ts:176

  • The client is referencing v1.health.version but the spec defines v1.enclaved.version. Update this to this.apiClient['v1.enclaved.version'].
      let request = this.apiClient['v1.health.version'].get({});

expressApp(invalidConfig as MasterExpressConfig);
assert(false, 'Expected error to be thrown when enclaved express client is not configured');
} catch (e) {
(e as Error).message.should.equal('enclavedExpressUrl and enclavedExpressCert are required');

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait im v confused. why is this removing the calls to the actual API?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what i mean is, how is this test defining the client to make calls to generate wallet?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for this case (when the config is invalid), the app won't startup. I can modify it so that the client is only created during the request, so it would fail then.

Comment on lines -60 to -61
coin: coin,
type: 'independent',

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this removed?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because it's not used in the actual request. Look at the Api spec for Enclaved Express.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  'v1.key.independent': {
    post: httpRoute({
      method: 'POST',
      path: '/api/{coin}/key/independent',
      request: httpRequest({
        params: {
          coin: t.string,
        },
        body: IndependentKeyRequest,
      }),
      response: IndependentKeyResponse,
      description: 'Generate an independent key',
    }),
  },

Comment on lines +281 to +288
try {
expressApp(invalidConfig as MasterExpressConfig);
assert(false, 'Expected error to be thrown when enclaved express client is not configured');
} catch (error) {
(error as Error).message.should.equal(
'enclavedExpressUrl and enclavedExpressCert are required',
);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

?? why is this just calling the app?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

see above.

Comment on lines -58 to -67
let response;
if (cfg.tlsMode === TlsMode.MTLS) {
// Use Master Express's own certificate as client cert when connecting to Enclaved Express
const httpsAgent = new https.Agent({
rejectUnauthorized: !cfg.allowSelfSigned,
ca: cfg.enclavedExpressCert,
// Provide client certificate for mTLS
key: cfg.tlsKey,
cert: cfg.tlsCert,
});

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the cert's being passed with the api client?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread src/shared/appUtils.ts
Comment on lines -119 to -127
export async function readCertificates(
keyPath: string,
crtPath: string,
): Promise<{ key: string; cert: string }> {
const privateKeyPromise = fs.promises.readFile(keyPath, 'utf8');
const certificatePromise = fs.promises.readFile(crtPath, 'utf8');
const [key, cert] = await Promise.all([privateKeyPromise, certificatePromise]);
return { key, cert };
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

where are the cert's being read now..?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mohammadalfaiyazbitgo mohammadalfaiyazbitgo merged commit 07d987f into master Jun 18, 2025
3 checks passed
@mohammadalfaiyazbitgo mohammadalfaiyazbitgo deleted the WP-000000/qol-api-specs branch June 18, 2025 15:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants