-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[feat]: Overhaul private key management (#27)
* feat: Overhaul the 'privateKey' loading mechanism * chore: Minor code refactor * chore: Undo the removal of the wallet service 'publicKey' property * Merge branch 'testnet' into jsanmi/overhaul-private-key-management
- Loading branch information
1 parent
03a2e38
commit 2736f9b
Showing
9 changed files
with
161 additions
and
23 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,35 @@ | ||
import { PrivateKeyLoader, PrivateKeyLoaderConfig } from "./privateKeyLoader"; | ||
|
||
export const PRIVATE_KEY_LOADER_TYPE_ENVIRONMENT_VARIABLE = 'env'; | ||
const DEFAULT_ENV_VARIABLE_NAME = 'RELAYER_PRIVATE_KEY'; | ||
|
||
export interface EnvPrivateKeyLoaderConfig extends PrivateKeyLoaderConfig { | ||
envVariableName?: string, | ||
} | ||
|
||
export class EnvPrivateKeyLoader extends PrivateKeyLoader { | ||
override loaderType: string = PRIVATE_KEY_LOADER_TYPE_ENVIRONMENT_VARIABLE; | ||
private readonly envVariableName: string; | ||
|
||
constructor( | ||
protected override readonly config: EnvPrivateKeyLoaderConfig, | ||
) { | ||
super(config); | ||
|
||
this.envVariableName = config.envVariableName ?? DEFAULT_ENV_VARIABLE_NAME; | ||
} | ||
|
||
override async loadPrivateKey(): Promise<string> { | ||
const privateKey = process.env[this.envVariableName]; | ||
|
||
if (privateKey == undefined) { | ||
throw new Error( | ||
`Failed to load privateKey from enviornment variable '${this.envVariableName}'.`, | ||
); | ||
} | ||
|
||
return privateKey; | ||
} | ||
} | ||
|
||
export default EnvPrivateKeyLoader; |
This file contains 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,51 @@ | ||
import { BYTES_32_HEX_EXPR } from "../config.schema"; | ||
|
||
export const PRIVATE_KEY_LOADER_TYPE_BASE = 'base'; | ||
|
||
const DEFAULT_PRIVATE_KEY_LOADER = 'env'; | ||
|
||
export interface PrivateKeyLoaderConfig { | ||
} | ||
|
||
export function loadPrivateKeyLoader( | ||
loader: string | null, | ||
config: PrivateKeyLoaderConfig | ||
): BasePrivateKeyLoader { | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-var-requires | ||
const module = require(`./${loader ?? DEFAULT_PRIVATE_KEY_LOADER}`); | ||
const loaderClass: typeof BasePrivateKeyLoader = module.default; | ||
|
||
return new loaderClass( | ||
config, | ||
) | ||
} | ||
|
||
export abstract class PrivateKeyLoader { | ||
abstract readonly loaderType: string; | ||
|
||
constructor( | ||
protected readonly config: PrivateKeyLoaderConfig, | ||
) {} | ||
|
||
abstract loadPrivateKey(): Promise<string>; | ||
|
||
async load(): Promise<string> { | ||
const privateKey = await this.loadPrivateKey(); | ||
|
||
if (!new RegExp(BYTES_32_HEX_EXPR).test(privateKey)) { | ||
throw new Error('Invalid loaded privateKey format.') | ||
} | ||
|
||
return privateKey; | ||
} | ||
} | ||
|
||
|
||
// ! 'BasePrivateKeyLoader' should only be used as a type. | ||
export class BasePrivateKeyLoader extends PrivateKeyLoader { | ||
override loaderType: string = PRIVATE_KEY_LOADER_TYPE_BASE; | ||
override loadPrivateKey(): Promise<string> { | ||
throw new Error("Method not implemented."); | ||
} | ||
} |
This file contains 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 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 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