-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: fixed eslint warnings and refactored crypto layer provider…
… to be more readable BREAKING CHANGE: * Renamed `CryptoLayerProviderConfig` to `CryptoLayerProviderFilter`. * Renamed `providers` key of `CryptoLayerConfig` to `providersToBeInitialized`. * Refactored `CryptoLayerConfig` to be an interface.
- Loading branch information
1 parent
23dab29
commit bab59c5
Showing
4 changed files
with
172 additions
and
55 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
import { serialize, type, validate } from "@js-soft/ts-serval"; | ||
import { CoreBuffer, Encoding, ICoreBuffer } from "./CoreBuffer"; | ||
import { CryptoSerializable } from "./CryptoSerializable"; | ||
import { CryptoExchangeAlgorithm } from "./exchange/CryptoExchange"; | ||
import { CryptoSignatureAlgorithm } from "./signature/CryptoSignatureAlgorithm"; | ||
|
||
export interface ICryptoPrivateKeyHandle { | ||
privateKey: ICoreBuffer; | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm; | ||
toString(): string; | ||
toPEM(): string; | ||
} | ||
|
||
export interface ICryptoPrivateKeyHandleStatic { | ||
new (): ICryptoPrivateKeyHandle; | ||
fromPEM( | ||
pem: string, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm | ||
): Promise<ICryptoPrivateKeyHandle>; | ||
fromString( | ||
value: string, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, | ||
encoding: Encoding | ||
): Promise<ICryptoPrivateKeyHandle>; | ||
fromNativeKey( | ||
key: any, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm | ||
): Promise<ICryptoPrivateKeyHandle>; | ||
} | ||
|
||
@type("CryptoPrivateKeyHandle") | ||
export class CryptoPrivateKeyHandle extends CryptoSerializable implements ICryptoPrivateKeyHandle { | ||
@validate() | ||
@serialize() | ||
public algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm; | ||
|
||
@validate() | ||
@serialize() | ||
public privateKey: CoreBuffer; | ||
|
||
public toPEM(): string { | ||
return this.privateKey.toString(Encoding.Pem, "PRIVATE KEY"); | ||
} | ||
|
||
public override toString(): string { | ||
return this.privateKey.toString(Encoding.Base64_UrlSafe_NoPadding); | ||
} | ||
|
||
protected static stripPEM(pem: string): string { | ||
pem = pem.replace(/-----BEGIN [\w ]* KEY-----/, ""); | ||
pem = pem.replace(/-----END [\w ]* KEY-----/, ""); | ||
pem = pem.replace(/----- BEGIN [\w ]* KEY -----/, ""); | ||
pem = pem.replace(/----- END [\w ]* KEY -----/, ""); | ||
pem = pem.replace(/(?:\r\n|\r|\n)/g, ""); | ||
return pem; | ||
} | ||
|
||
public static fromString( | ||
value: string, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm, | ||
encoding: Encoding = Encoding.Base64_UrlSafe_NoPadding | ||
): CryptoPrivateKeyHandle { | ||
const buffer: CoreBuffer = CoreBuffer.fromString(value, encoding); | ||
return this.fromAny({ algorithm, privateKey: buffer }); | ||
} | ||
|
||
public static fromObject( | ||
value: any, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm | ||
): CryptoPrivateKeyHandle { | ||
const buffer: ICoreBuffer = CoreBuffer.fromObject(value); | ||
|
||
return this.fromAny({ algorithm, privateKey: buffer }); | ||
} | ||
|
||
public static fromPEM( | ||
pem: string, | ||
algorithm: CryptoExchangeAlgorithm | CryptoSignatureAlgorithm | ||
): CryptoPrivateKeyHandle { | ||
const value = this.stripPEM(pem); | ||
return this.fromString(value, algorithm, Encoding.Base64); | ||
} | ||
|
||
public static from(value: any): CryptoPrivateKeyHandle { | ||
return this.fromAny(value); | ||
} | ||
|
||
public static fromBase64(value: string): CryptoPrivateKeyHandle { | ||
return this.deserialize(CoreBuffer.base64_utf8(value)); | ||
} | ||
} |
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