-
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.
feat: implemented import and export methods for raw public key in `Cr…
…yptoPublicKeyHandle`
- Loading branch information
1 parent
58cdb80
commit bfce95e
Showing
2 changed files
with
53 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,60 @@ | ||
import { type } from "@js-soft/ts-serval"; | ||
import { KeyPairHandle, KeyPairSpec } from "@nmshd/rs-crypto-types"; | ||
import { CoreBuffer, Encoding } from "src/CoreBuffer"; | ||
import { CryptoAsymmetricKeyHandle } from "./CryptoAsymmetricKeyHandle"; | ||
import { getProviderOrThrow, ProviderIdentifier } from "./CryptoLayerProviders"; | ||
|
||
export interface ICryptoPublicKeyHandle { | ||
keyPairHandle: KeyPairHandle; | ||
spec: KeyPairSpec; | ||
toSerializedString(): Promise<string>; | ||
toPEM(): Promise<string>; | ||
toJSON(): Object; | ||
} | ||
|
||
export interface ICryptoPublicKeyHandleStatic { | ||
new (): ICryptoPublicKeyHandle; | ||
fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPublicKeyHandle>; | ||
fromPEM(providerIdent: ProviderIdentifier, pem: string, spec: KeyPairSpec): Promise<CryptoPublicKeyHandle>; | ||
fromString( | ||
providerIdent: ProviderIdentifier, | ||
value: string, | ||
spec: KeyPairSpec, | ||
encoding: Encoding | ||
): Promise<CryptoPublicKeyHandle>; | ||
// fromNativeKey(key: any, spec: KeyPairSpec): Promise<ICryptoPublicKeyHandle>; | ||
} | ||
|
||
@type("CryptoPublicKeyHandle") | ||
export class CryptoPublicKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPublicKeyHandle {} | ||
export class CryptoPublicKeyHandle extends CryptoAsymmetricKeyHandle implements ICryptoPublicKeyHandle { | ||
public async toSerializedString(): Promise<string> { | ||
const raw = await this.keyPairHandle.getPublicKey(); | ||
return CoreBuffer.from(raw).toString(Encoding.Base64_UrlSafe_NoPadding); | ||
} | ||
|
||
public async toPEM(): Promise<string> { | ||
const raw = await this.keyPairHandle.getPublicKey(); | ||
return CoreBuffer.from(raw).toString(Encoding.Pem, "PRIVATE KEY"); | ||
} | ||
|
||
public static async fromString( | ||
providerIdent: ProviderIdentifier, | ||
value: string, | ||
spec: KeyPairSpec, | ||
encoding: Encoding | ||
): Promise<CryptoPublicKeyHandle> { | ||
const raw = CoreBuffer.fromString(value, encoding).buffer; | ||
const provider = getProviderOrThrow(providerIdent); | ||
const keyPairHandle = await provider.importPublicKey(spec, raw); | ||
return await CryptoPublicKeyHandle.newFromProviderAndKeyPairHandle(provider, keyPairHandle, { | ||
keySpec: spec | ||
}); | ||
} | ||
|
||
public static async fromPEM( | ||
providerIdent: ProviderIdentifier, | ||
pem: string, | ||
spec: KeyPairSpec | ||
): Promise<CryptoPublicKeyHandle> { | ||
return await CryptoPublicKeyHandle.fromString(providerIdent, pem, spec, Encoding.Pem); | ||
} | ||
} |