|
1 | 1 | export enum KeyType {
|
2 |
| - RSA_65537 = "rsa_65537", |
3 |
| - EC_SECP256K1 = "ec_secp256k1", |
4 |
| -}; |
| 2 | + RSA_65537 = "rsa_65537", |
| 3 | + EC_SECP256K1 = "ec_secp256k1", |
| 4 | +} |
5 | 5 |
|
6 | 6 | export type Format = "jwk" | "raw";
|
7 | 7 |
|
8 | 8 | export interface SerializationParams<T extends Format = Format> {
|
9 |
| - format: T; |
| 9 | + format: T; |
10 | 10 | }
|
11 | 11 |
|
12 | 12 | export interface SigningParams {
|
13 |
| - payload: Uint8Array; |
14 |
| - isDigest?: boolean; |
| 13 | + payload: Uint8Array; |
| 14 | + isDigest?: boolean; |
15 | 15 | }
|
16 | 16 |
|
17 | 17 | export interface VerifyingParams {
|
18 |
| - payload: Uint8Array; |
19 |
| - signature: Uint8Array; |
20 |
| - isDigest?: boolean; |
| 18 | + payload: Uint8Array; |
| 19 | + signature: Uint8Array; |
| 20 | + isDigest?: boolean; |
21 | 21 | }
|
22 | 22 |
|
23 | 23 | export interface EncryptionParams {
|
24 |
| - secret: Uint8Array; |
25 |
| - |
| 24 | + secret: Uint8Array; |
26 | 25 | }
|
27 | 26 |
|
28 | 27 | export interface DecryptionParams {
|
29 |
| - payload: Uint8Array; |
| 28 | + payload: Uint8Array; |
30 | 29 | }
|
31 | 30 |
|
32 | 31 | export abstract class PrivateKey {
|
33 |
| - public readonly type: KeyType; |
| 32 | + public readonly type: KeyType; |
34 | 33 |
|
35 |
| - constructor({type}: {type: KeyType}) { |
36 |
| - this.type = type; |
37 |
| - } |
38 |
| - static async new(_: any): Promise<PrivateKey> { |
39 |
| - throw new Error(`PrivateKey does not implement instantiation interface.`); |
40 |
| - } |
41 |
| - static async deserialize(_: any): Promise<PrivateKey> { |
42 |
| - throw new Error(`PrivateKey does not implement deserialization interface.`); |
43 |
| - } |
44 |
| - abstract serialize(params: SerializationParams): Promise<JsonWebKey | Uint8Array>; |
45 |
| - abstract sign(params: SigningParams): Promise<Uint8Array>; |
46 |
| - abstract public(): Promise<PublicKey>; |
47 |
| - public async decrypt(_: DecryptionParams): Promise<Uint8Array> { |
48 |
| - throw new Error(`PrivateKey ${this.type} does not provide decription interface.`); |
49 |
| - } |
| 34 | + constructor({ type }: { type: KeyType }) { |
| 35 | + this.type = type; |
| 36 | + } |
| 37 | + static async new(_: any): Promise<PrivateKey> { |
| 38 | + throw new Error(`PrivateKey does not implement instantiation interface.`); |
| 39 | + } |
| 40 | + static async deserialize(_: any): Promise<PrivateKey> { |
| 41 | + throw new Error(`PrivateKey does not implement deserialization interface.`); |
| 42 | + } |
| 43 | + abstract serialize( |
| 44 | + params: SerializationParams |
| 45 | + ): Promise<JsonWebKey | Uint8Array>; |
| 46 | + abstract sign(params: SigningParams): Promise<Uint8Array>; |
| 47 | + abstract public(): Promise<PublicKey>; |
| 48 | + public async decrypt(_: DecryptionParams): Promise<Uint8Array> { |
| 49 | + throw new Error( |
| 50 | + `PrivateKey ${this.type} does not provide decription interface.` |
| 51 | + ); |
| 52 | + } |
50 | 53 | }
|
51 | 54 |
|
52 | 55 | export abstract class PublicKey {
|
53 |
| - public readonly type: KeyType; |
54 |
| - constructor({type}: {type: KeyType}) { |
55 |
| - this.type = type; |
56 |
| - } |
57 |
| - static async deserialize(_: any): Promise<PublicKey> { |
58 |
| - throw new Error(`PublicKey does not implement deserialization interface.`); |
59 |
| - } |
60 |
| - abstract serialize(params: SerializationParams): Promise<JsonWebKey | Uint8Array>; |
61 |
| - abstract verify(params: VerifyingParams): Promise<boolean>; |
62 |
| - abstract identifier(): Promise<Uint8Array>; |
63 |
| - public async encrypt(_: EncryptionParams): Promise<Uint8Array> { |
64 |
| - throw new Error(`PrivateKey ${this.type} does not provide encyrption interface.`); |
65 |
| - } |
| 56 | + public readonly type: KeyType; |
| 57 | + constructor({ type }: { type: KeyType }) { |
| 58 | + this.type = type; |
| 59 | + } |
| 60 | + static async deserialize(_: any): Promise<PublicKey> { |
| 61 | + throw new Error(`PublicKey does not implement deserialization interface.`); |
| 62 | + } |
| 63 | + abstract serialize( |
| 64 | + params: SerializationParams |
| 65 | + ): Promise<JsonWebKey | Uint8Array>; |
| 66 | + abstract verify(params: VerifyingParams): Promise<boolean>; |
| 67 | + abstract identifier(): Promise<Uint8Array>; |
| 68 | + public async encrypt(_: EncryptionParams): Promise<Uint8Array> { |
| 69 | + throw new Error( |
| 70 | + `PrivateKey ${this.type} does not provide encyrption interface.` |
| 71 | + ); |
| 72 | + } |
66 | 73 | }
|
67 | 74 |
|
68 |
| -export const getInitializationOptions = (type: KeyType): AlgorithmIdentifier | RsaHashedKeyGenParams | EcKeyGenParams => { |
69 |
| - switch(type) { |
70 |
| - case KeyType.RSA_65537: |
71 |
| - return { |
72 |
| - name: "RSA-PSS", |
73 |
| - publicExponent: new Uint8Array([0x01, 0x00, 0x01]), |
74 |
| - hash: { |
75 |
| - name: "SHA-256" |
76 |
| - } |
77 |
| - }; |
78 |
| - default: |
79 |
| - throw new Error(`Unsupported RSA KeyType ${type}`); |
80 |
| - } |
81 |
| -} |
| 75 | +export const getInitializationOptions = ( |
| 76 | + type: KeyType |
| 77 | +): AlgorithmIdentifier | RsaHashedKeyGenParams | EcKeyGenParams => { |
| 78 | + switch (type) { |
| 79 | + case KeyType.RSA_65537: |
| 80 | + return { |
| 81 | + name: "RSA-PSS", |
| 82 | + publicExponent: new Uint8Array([0x01, 0x00, 0x01]), |
| 83 | + hash: { |
| 84 | + name: "SHA-256", |
| 85 | + }, |
| 86 | + }; |
| 87 | + default: |
| 88 | + throw new Error(`Unsupported RSA KeyType ${type}`); |
| 89 | + } |
| 90 | +}; |
82 | 91 |
|
83 |
| -export const getSigningParameters = (type: KeyType): AlgorithmIdentifier | RsaPssParams | EcdsaParams => { |
84 |
| - switch(type) { |
85 |
| - case KeyType.RSA_65537: |
86 |
| - return { |
87 |
| - name: "RSA-PSS", |
88 |
| - saltLength: 32, |
89 |
| - }; |
90 |
| - default: |
91 |
| - throw new Error(`Unsupported RSA KeyType ${type}`); |
92 |
| - } |
93 |
| -} |
| 92 | +export const getSigningParameters = ( |
| 93 | + type: KeyType |
| 94 | +): AlgorithmIdentifier | RsaPssParams | EcdsaParams => { |
| 95 | + switch (type) { |
| 96 | + case KeyType.RSA_65537: |
| 97 | + return { |
| 98 | + name: "RSA-PSS", |
| 99 | + saltLength: 32, |
| 100 | + }; |
| 101 | + default: |
| 102 | + throw new Error(`Unsupported RSA KeyType ${type}`); |
| 103 | + } |
| 104 | +}; |
0 commit comments