-
Notifications
You must be signed in to change notification settings - Fork 91
feat: Enable Transaction Service API key for local development #2829
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
LucieFaire
merged 9 commits into
main
from
COR-944/set-up-tx-service-api-key-for-local-development
Dec 19, 2025
Merged
Changes from 3 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
cff49bf
feat: add Auth header for Tx Service calls (local dev)
LucieFaire 89d8eb4
add extra validation
LucieFaire 103e6dc
Update .env.sample
LucieFaire 356fd6a
inject headers
LucieFaire ecc7916
Merge branch 'main' into COR-944/set-up-tx-service-api-key-for-local-…
LucieFaire 4bdb784
Merge branch 'main' into COR-944/set-up-tx-service-api-key-for-local-…
LucieFaire 58a541b
fix tests
LucieFaire 6f37b07
address comments
LucieFaire b8f7a57
Merge branch 'main' into COR-944/set-up-tx-service-api-key-for-local-…
LucieFaire 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
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
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 |
|---|---|---|
| @@ -1,9 +1,46 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source'; | ||
| import { HttpErrorFactory } from '@/datasources/errors/http-error-factory'; | ||
| import { | ||
| CacheService, | ||
| type ICacheService, | ||
| } from '@/datasources/cache/cache.service.interface'; | ||
| import { | ||
| INetworkService, | ||
| TxNetworkService, | ||
| } from '@/datasources/network/network.service.interface'; | ||
| import { ILoggingService, LoggingService } from '@/logging/logging.interface'; | ||
| import { IConfigurationService } from '@/config/configuration.service.interface'; | ||
|
|
||
| export const TxCacheFirstDataSource = Symbol('TxCacheFirstDataSource'); | ||
|
|
||
| @Module({ | ||
| providers: [CacheFirstDataSource, HttpErrorFactory], | ||
| exports: [CacheFirstDataSource], | ||
| providers: [ | ||
| CacheFirstDataSource, | ||
| HttpErrorFactory, | ||
| { | ||
| provide: TxCacheFirstDataSource, | ||
| // Tx-flavored CacheFirstDataSource wired to the TxNetworkService so Tx Service consumers get the auth header behavior. | ||
| useFactory: ( | ||
| cacheService: ICacheService, | ||
| networkService: INetworkService, | ||
| loggingService: ILoggingService, | ||
| configurationService: IConfigurationService, | ||
| ): CacheFirstDataSource => | ||
| new CacheFirstDataSource( | ||
| cacheService, | ||
| networkService, | ||
| loggingService, | ||
| configurationService, | ||
| ), | ||
| inject: [ | ||
| CacheService, | ||
| TxNetworkService, | ||
| LoggingService, | ||
| IConfigurationService, | ||
| ], | ||
| }, | ||
| ], | ||
| exports: [CacheFirstDataSource, TxCacheFirstDataSource], | ||
| }) | ||
| export class CacheFirstDataSourceModule {} |
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
120 changes: 120 additions & 0 deletions
120
src/datasources/network/tx-auth.network.service.spec.ts
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,120 @@ | ||
| import { TxAuthNetworkService } from '@/datasources/network/tx-auth.network.service'; | ||
| import type { INetworkService } from '@/datasources/network/network.service.interface'; | ||
| import type { IConfigurationService } from '@/config/configuration.service.interface'; | ||
| import { rawify } from '@/validation/entities/raw.entity'; | ||
|
|
||
| describe('TxAuthNetworkService', () => { | ||
| const baseNetworkService: jest.MockedObjectDeep<INetworkService> = { | ||
| get: jest.fn(), | ||
| post: jest.fn(), | ||
| delete: jest.fn(), | ||
| }; | ||
|
|
||
| const config = { | ||
| getOrThrow: jest.fn(), | ||
| get: jest.fn(), | ||
| } as jest.MockedObjectDeep<IConfigurationService>; | ||
|
|
||
| const buildService = ( | ||
| options: Partial<{ | ||
| isDevelopment: boolean; | ||
| useVpcUrl: boolean; | ||
| apiKey: string | undefined; | ||
| }> = {}, | ||
| ): TxAuthNetworkService => { | ||
| const { isDevelopment = true, useVpcUrl = false } = options; | ||
| const apiKey = 'apiKey' in options ? options.apiKey : 'tx-key'; | ||
|
|
||
| config.getOrThrow.mockImplementation((key: string) => { | ||
| switch (key) { | ||
| case 'application.isDevelopment': | ||
| return isDevelopment; | ||
| case 'safeTransaction.useVpcUrl': | ||
| return useVpcUrl; | ||
| default: | ||
| throw new Error(`Unexpected key: ${key}`); | ||
| } | ||
| }); | ||
| config.get.mockImplementation((key: string) => { | ||
| if (key === 'safeTransaction.apiKey') { | ||
| return apiKey; | ||
| } | ||
| throw new Error(`Unexpected key: ${key}`); | ||
| }); | ||
|
|
||
| return new TxAuthNetworkService(baseNetworkService, config); | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| }); | ||
|
|
||
| it('adds Authorization header when in development, not using VPC, and apiKey exists', async () => { | ||
| const service = buildService(); | ||
| const args = { | ||
| url: 'https://example.test', | ||
| networkRequest: { headers: { Foo: 'bar' } }, | ||
| }; | ||
| baseNetworkService.get.mockResolvedValue({ | ||
| data: rawify('ok'), | ||
| status: 200, | ||
| }); | ||
|
|
||
| await service.get(args); | ||
|
|
||
| expect(baseNetworkService.get).toHaveBeenCalledWith({ | ||
| url: args.url, | ||
| networkRequest: { | ||
| headers: { Foo: 'bar', Authorization: 'Bearer tx-key' }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it('preserves params when adding Authorization header', async () => { | ||
| const service = buildService(); | ||
| const args = { | ||
| url: 'https://example.test', | ||
| networkRequest: { params: { a: '1' } }, | ||
| }; | ||
| baseNetworkService.post.mockResolvedValue({ | ||
| data: rawify('ok'), | ||
| status: 200, | ||
| }); | ||
|
|
||
| await service.post(args); | ||
|
|
||
| expect(baseNetworkService.post).toHaveBeenCalledWith({ | ||
| url: args.url, | ||
| networkRequest: { | ||
| params: { a: '1' }, | ||
| headers: { Authorization: 'Bearer tx-key' }, | ||
| }, | ||
| }); | ||
| }); | ||
|
|
||
| it.each([ | ||
| ['not development', { isDevelopment: false }], | ||
| ['using VPC', { useVpcUrl: true }], | ||
| ['missing apiKey', { apiKey: undefined }], | ||
| ])('does not add Authorization header when %s', async (_name, overrides) => { | ||
| const service = buildService( | ||
| overrides as Partial<{ | ||
| isDevelopment: boolean; | ||
| useVpcUrl: boolean; | ||
| apiKey: string | undefined; | ||
| }>, | ||
| ); | ||
| const args = { | ||
| url: 'https://example.test', | ||
| networkRequest: { headers: { Foo: 'bar' } }, | ||
| }; | ||
| baseNetworkService.delete.mockResolvedValue({ | ||
| data: rawify('ok'), | ||
| status: 200, | ||
| }); | ||
|
|
||
| await service.delete(args); | ||
|
|
||
| expect(baseNetworkService.delete).toHaveBeenCalledWith(args); | ||
| }); | ||
| }); |
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,80 @@ | ||
| import { Inject, Injectable } from '@nestjs/common'; | ||
| import { IConfigurationService } from '@/config/configuration.service.interface'; | ||
| import { | ||
| INetworkService, | ||
| NetworkService, | ||
| } from '@/datasources/network/network.service.interface'; | ||
| import { NetworkRequest } from '@/datasources/network/entities/network.request.entity'; | ||
| import type { NetworkResponse } from '@/datasources/network/entities/network.response.entity'; | ||
|
|
||
| /** | ||
| * Decorates the base {@link INetworkService} to add Tx API auth when | ||
| * running in development against the public Transaction Service. | ||
| */ | ||
| @Injectable() | ||
| export class TxAuthNetworkService implements INetworkService { | ||
LucieFaire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| private readonly isDevelopment: boolean; | ||
| private readonly useVpcUrl: boolean; | ||
| private readonly apiKey: string | undefined; | ||
|
|
||
| constructor( | ||
| @Inject(NetworkService) | ||
| private readonly networkService: INetworkService, | ||
| @Inject(IConfigurationService) | ||
| private readonly configurationService: IConfigurationService, | ||
| ) { | ||
| this.isDevelopment = this.configurationService.getOrThrow<boolean>( | ||
| 'application.isDevelopment', | ||
| ); | ||
| this.useVpcUrl = this.configurationService.getOrThrow<boolean>( | ||
| 'safeTransaction.useVpcUrl', | ||
| ); | ||
| this.apiKey = this.configurationService.get<string | undefined>( | ||
| 'safeTransaction.apiKey', | ||
| ); | ||
| } | ||
|
|
||
| get<T>(args: { | ||
| url: string; | ||
| networkRequest?: NetworkRequest; | ||
| }): Promise<NetworkResponse<T>> { | ||
| return this.networkService.get<T>(this.withAuth(args)); | ||
| } | ||
|
|
||
| post<T>(args: { | ||
| url: string; | ||
| data?: object; | ||
| networkRequest?: NetworkRequest; | ||
| }): Promise<NetworkResponse<T>> { | ||
| return this.networkService.post<T>(this.withAuth(args)); | ||
| } | ||
|
|
||
| delete<T>(args: { | ||
| url: string; | ||
| data?: object; | ||
| networkRequest?: NetworkRequest; | ||
| }): Promise<NetworkResponse<T>> { | ||
| return this.networkService.delete<T>(this.withAuth(args)); | ||
| } | ||
|
|
||
| private withAuth<TArgs extends { networkRequest?: NetworkRequest }>( | ||
| args: TArgs, | ||
| ): TArgs { | ||
| const isTxAuthEnabled = this.isDevelopment && !this.useVpcUrl; | ||
LucieFaire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!isTxAuthEnabled || !this.apiKey) { | ||
| return args; | ||
| } | ||
|
|
||
| // Only add auth header when in development, and using the public Tx Service and API key is set | ||
| return { | ||
| ...args, | ||
| networkRequest: { | ||
| ...args.networkRequest, | ||
| headers: { | ||
LucieFaire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| ...(args.networkRequest?.headers ?? {}), | ||
| Authorization: `Bearer ${this.apiKey}`, | ||
LucieFaire marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| }, | ||
| }, | ||
| }; | ||
| } | ||
| } | ||
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.