-
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 7 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
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
33 changes: 33 additions & 0 deletions
33
src/datasources/network/__tests__/test.tx-auth.network.module.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,33 @@ | ||
| import { Module } from '@nestjs/common'; | ||
| import { | ||
| INetworkService, | ||
| NetworkService, | ||
| } from '@/datasources/network/network.service.interface'; | ||
| import { CacheFirstDataSource } from '@/datasources/cache/cache.first.data.source'; | ||
| import { HttpErrorFactory } from '@/datasources/errors/http-error-factory'; | ||
| import { CacheFirstDataSourceModule } from '@/datasources/cache/cache.first.data.source.module'; | ||
| import { networkService } from '@/datasources/network/__tests__/test.network.module'; | ||
|
|
||
| /** | ||
| * Test module that overrides {@link TxAuthNetworkModule} with mocked dependencies. | ||
| * | ||
| * Key points: | ||
| * - Reuses the same NetworkService mock instance from {@link TestNetworkModule} | ||
| * - Exports real CacheFirstDataSource & HttpErrorFactory (with mocked NetworkService injected) | ||
| * - Required by TransactionApiManager and other consumers that inject these dependencies | ||
| */ | ||
| @Module({ | ||
| imports: [CacheFirstDataSourceModule], | ||
| providers: [ | ||
| { | ||
| provide: NetworkService, | ||
| useFactory: (): jest.MockedObjectDeep<INetworkService> => { | ||
| return jest.mocked(networkService); | ||
| }, | ||
| }, | ||
| CacheFirstDataSource, | ||
| HttpErrorFactory, | ||
| ], | ||
| exports: [NetworkService, CacheFirstDataSource, HttpErrorFactory], | ||
| }) | ||
| export class TestTxAuthNetworkModule {} |
86 changes: 86 additions & 0 deletions
86
src/datasources/network/auth/tx-auth-headers.helper.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,86 @@ | ||
| import type { IConfigurationService } from '@/config/configuration.service.interface'; | ||
| import { getTxAuthHeaders } from '@/datasources/network/auth/tx-auth-headers.helper'; | ||
|
|
||
| describe('getTxAuthHeaders', () => { | ||
| let mockConfigService: jest.Mocked<IConfigurationService>; | ||
|
|
||
| beforeEach(() => { | ||
| jest.resetAllMocks(); | ||
| mockConfigService = { | ||
| getOrThrow: jest.fn(), | ||
| get: jest.fn(), | ||
| } as jest.Mocked<IConfigurationService>; | ||
| }); | ||
|
|
||
| it('should return Authorization header when TX auth is enabled', () => { | ||
| const apiKey = 'test-api-key-123'; | ||
| mockConfigService.getOrThrow.mockImplementation((key: string) => { | ||
| if (key === 'application.isDevelopment') return true; | ||
| if (key === 'safeTransaction.useVpcUrl') return false; | ||
| throw new Error(`Unexpected key: ${key}`); | ||
| }); | ||
| mockConfigService.get.mockReturnValue(apiKey); | ||
|
|
||
| const result = getTxAuthHeaders(mockConfigService); | ||
|
|
||
| expect(result).toEqual({ | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }); | ||
| expect(mockConfigService.getOrThrow).toHaveBeenCalledWith( | ||
| 'application.isDevelopment', | ||
| ); | ||
| expect(mockConfigService.getOrThrow).toHaveBeenCalledWith( | ||
| 'safeTransaction.useVpcUrl', | ||
| ); | ||
| expect(mockConfigService.get).toHaveBeenCalledWith( | ||
| 'safeTransaction.apiKey', | ||
| ); | ||
| }); | ||
|
|
||
| it.each([ | ||
| { | ||
| description: 'not in development mode', | ||
| isDevelopment: false, | ||
| useVpcUrl: false, | ||
| apiKey: 'test-key', | ||
| }, | ||
| { | ||
| description: 'useVpcUrl is true', | ||
| isDevelopment: true, | ||
| useVpcUrl: true, | ||
| apiKey: 'test-key', | ||
| }, | ||
| { | ||
| description: 'API key is undefined', | ||
| isDevelopment: true, | ||
| useVpcUrl: false, | ||
| apiKey: undefined, | ||
| }, | ||
| { | ||
| description: 'API key is empty string', | ||
| isDevelopment: true, | ||
| useVpcUrl: false, | ||
| apiKey: '', | ||
| }, | ||
| { | ||
| description: 'in production with VPC URL', | ||
| isDevelopment: false, | ||
| useVpcUrl: true, | ||
| apiKey: 'test-key', | ||
| }, | ||
| ])( | ||
| 'should return undefined when $description', | ||
| ({ isDevelopment, useVpcUrl, apiKey }) => { | ||
| mockConfigService.getOrThrow.mockImplementation((key: string) => { | ||
| if (key === 'application.isDevelopment') return isDevelopment; | ||
| if (key === 'safeTransaction.useVpcUrl') return useVpcUrl; | ||
| throw new Error(`Unexpected key: ${key}`); | ||
| }); | ||
| mockConfigService.get.mockReturnValue(apiKey); | ||
|
|
||
| const result = getTxAuthHeaders(mockConfigService); | ||
|
|
||
| expect(result).toBeUndefined(); | ||
| }, | ||
| ); | ||
| }); |
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,32 @@ | ||
| import type { IConfigurationService } from '@/config/configuration.service.interface'; | ||
|
|
||
| /** | ||
| * Returns Transaction Service auth headers when running in development | ||
| * against the public Transaction Service. | ||
| * | ||
| * @param configurationService - Configuration service used to read settings | ||
| * @returns An object containing the `Authorization` header (`{ Authorization: `Bearer ${apiKey}` }`) | ||
| * when TX auth is enabled and an API key is configured; otherwise `undefined`. | ||
| */ | ||
| export function getTxAuthHeaders( | ||
LucieFaire marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| configurationService: IConfigurationService, | ||
| ): Record<string, string> | undefined { | ||
| const isDevelopment = configurationService.getOrThrow<boolean>( | ||
| 'application.isDevelopment', | ||
| ); | ||
| const useVpcUrl = configurationService.getOrThrow<boolean>( | ||
| 'safeTransaction.useVpcUrl', | ||
| ); | ||
| const apiKey = configurationService.get<string | undefined>( | ||
| 'safeTransaction.apiKey', | ||
| ); | ||
|
|
||
| const isTxAuthEnabled = isDevelopment && !useVpcUrl; | ||
| if (!isTxAuthEnabled || !apiKey) { | ||
| return undefined; | ||
| } | ||
|
|
||
| return { | ||
| Authorization: `Bearer ${apiKey}`, | ||
| }; | ||
| } | ||
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.