@sovrahq/kms-client 1.4.0-4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +201 -0
- package/__test__/mock/bbs-signed-vc.json +45 -0
- package/__test__/mock/did-document-2.json +23 -0
- package/__test__/mock/did-document.json +45 -0
- package/__test__/mock/signed-vc.json +45 -0
- package/__test__/mock/vc-rsa.json +38 -0
- package/__test__/mock/vc.json +38 -0
- package/dist/jest.config.d.ts +0 -0
- package/dist/jest.config.js +12 -0
- package/dist/jest.config.js.map +1 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +9 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/services/kms-client.d.ts +59 -0
- package/dist/src/services/kms-client.js +510 -0
- package/dist/src/services/kms-client.js.map +1 -0
- package/jest.config.ts +11 -0
- package/package.json +41 -0
- package/readme.md +142 -0
- package/src/index.ts +2 -0
- package/src/services/kms-client.ts +497 -0
- package/tsconfig.json +20 -0
package/readme.md
ADDED
|
@@ -0,0 +1,142 @@
|
|
|
1
|
+
# Extrimian - KMS Client
|
|
2
|
+
KMS comes from the English key management system and is the component in charge of creating key pairs, encrypting, decrypting and signing content.
|
|
3
|
+
The current Extrimian KMS implementation supports the ES256k, DIDComm, and BBSBLS2020 algorithms.
|
|
4
|
+
|
|
5
|
+
## KMS Constructor
|
|
6
|
+
```
|
|
7
|
+
constructor(
|
|
8
|
+
config: {
|
|
9
|
+
lang: LANG;
|
|
10
|
+
storage: KMSStorage;
|
|
11
|
+
didResolver ?: (did: string) => Promise<DIDDocument>;
|
|
12
|
+
mobile ?: boolean
|
|
13
|
+
}
|
|
14
|
+
)
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
* lang: The preferred language for the creation of ES256k keys mnemonic.
|
|
18
|
+
* storage: The reference to an object that satisfies the KMSStorage interface is needed, which will store the generated key pairs, and which is accessed to make use of the keys either for signature operations or to export them, using the public component as an index.
|
|
19
|
+
* didResolver: Optionally, a callback can be defined that provides the functionality to resolve a DID. If this parameter is not defined, the functionality of signing verifiable credentials will not be available.
|
|
20
|
+
* mobile: Optionally, a flag can be set to indicate whether the KMSClient instance will work on a mobile platform or not. By default this option is false. In case this option is true, the Bbsbls2020 suite will not be available.
|
|
21
|
+
|
|
22
|
+
## KMS Storage
|
|
23
|
+
KMS applies dependency inversion concepts so it requires send a KMSStorage by its constructor.
|
|
24
|
+
|
|
25
|
+
```
|
|
26
|
+
interface KMSStorage {
|
|
27
|
+
add(key: string, data: any): Promise<void>;
|
|
28
|
+
get(key: string): Promise<any>;
|
|
29
|
+
getAll(): Promise<Map<string, any>>;
|
|
30
|
+
update(key: string, data: any);
|
|
31
|
+
remove(key: string);
|
|
32
|
+
}
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
### Example of KMS Storage Implementation
|
|
36
|
+
This is a mock implementation example
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
import { KMSStorage } from "@quarkid/kms-core";
|
|
40
|
+
|
|
41
|
+
export class SecureStorage implements KMSStorage {
|
|
42
|
+
map = new Map<string, any>();
|
|
43
|
+
|
|
44
|
+
async add(key: string, data: any): Promise<void> {
|
|
45
|
+
this.map.set(key, data);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async get(key: string): Promise<any> {
|
|
49
|
+
return this.map.get(key);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
async getAll(): Promise<Map<string, any>> {
|
|
53
|
+
return this.map;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async update(key: string, data: any) {
|
|
57
|
+
this.map.set(key, data);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
async remove(key: string) {
|
|
61
|
+
this.map.delete(key);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
## Example to create Keys
|
|
67
|
+
```
|
|
68
|
+
const updateKey = await kms.create(Suite.ES256k);
|
|
69
|
+
const recoveryKey = await kms.create(Suite.ES256k);
|
|
70
|
+
const didComm = await kms.create(Suite.DIDComm);
|
|
71
|
+
const bbsbls = await kms.create(Suite.Bbsbls2020);
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
## Sign Content
|
|
75
|
+
```
|
|
76
|
+
sign(
|
|
77
|
+
suite: Suite,
|
|
78
|
+
publicKeyJWK: IJWK,
|
|
79
|
+
content: any
|
|
80
|
+
): Promise<string>
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
### Example signing using ES256k Suite
|
|
84
|
+
```
|
|
85
|
+
return await kms.sign(Suite.ES256k, updateKey, content)
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
## Sign VC
|
|
89
|
+
It is used to sign a verifiable credential.
|
|
90
|
+
|
|
91
|
+
* suite: Suite to use for the signature.
|
|
92
|
+
* publicKeyJWK: Public component of the key pair to use as signer, in JWK format.
|
|
93
|
+
* vc: Credential to sign.
|
|
94
|
+
* did: DID of the issuer of the credential.
|
|
95
|
+
* verificationMethodId: Identifier of the Verification Method to use to verify.
|
|
96
|
+
* purpose: Indicates the Verification Relationship corresponding to the Verification Method to be used.
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
signVC(
|
|
100
|
+
suite: Suite,
|
|
101
|
+
publicKeyJWK: IJWK,
|
|
102
|
+
vc: any,
|
|
103
|
+
did: string,
|
|
104
|
+
verificationMethodId: string,
|
|
105
|
+
purpose: Purpose
|
|
106
|
+
): Promise<VerifiableCredential>
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
## DIDComm Pack and Unpack
|
|
110
|
+
```
|
|
111
|
+
pack(
|
|
112
|
+
publicKeyJWK: IJWK,
|
|
113
|
+
toHexPublicKeys: string[],
|
|
114
|
+
contentToSign: string
|
|
115
|
+
): Promise<string>
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
```
|
|
119
|
+
unpack(
|
|
120
|
+
publicKeyJWK: IJWK,
|
|
121
|
+
packedContent: string
|
|
122
|
+
): Promise<string>
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
## Export private keys
|
|
126
|
+
```
|
|
127
|
+
export(
|
|
128
|
+
publicKeyJWK: IJWK
|
|
129
|
+
): Promise<any>
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
## Get public keys by specific suite
|
|
133
|
+
```
|
|
134
|
+
getPublicKeysBySuiteType(
|
|
135
|
+
suite: Suite
|
|
136
|
+
): Promise<IJWK[]>
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
## Get All public keys
|
|
140
|
+
```
|
|
141
|
+
getAllPublicKeys(): Promise<IJWK[]>
|
|
142
|
+
```
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,497 @@
|
|
|
1
|
+
import {
|
|
2
|
+
DIDCommMessage,
|
|
3
|
+
DIDDocument,
|
|
4
|
+
DIDDocumentUtils,
|
|
5
|
+
Purpose,
|
|
6
|
+
VerificationMethodJwk
|
|
7
|
+
} from "@sovrahq/did-core";
|
|
8
|
+
import {
|
|
9
|
+
IES256kSuite,
|
|
10
|
+
IVCSuite,
|
|
11
|
+
IKMS,
|
|
12
|
+
Suite,
|
|
13
|
+
VCSuite,
|
|
14
|
+
LANG,
|
|
15
|
+
KMSStorage,
|
|
16
|
+
IVCJsonLDKeyPair,
|
|
17
|
+
IKeyPair,
|
|
18
|
+
IES256kKeyPair,
|
|
19
|
+
IDIDCommSuite,
|
|
20
|
+
IDidCommKeyPair,
|
|
21
|
+
BaseConverter,
|
|
22
|
+
Base,
|
|
23
|
+
IJWK,
|
|
24
|
+
IDIDCommV2Suite,
|
|
25
|
+
IPackedDIDCommMessage,
|
|
26
|
+
IDIDCommMessage,
|
|
27
|
+
DIDCommMessagePacking,
|
|
28
|
+
SelectiveDisclosureZKPSuite,
|
|
29
|
+
DIDCommPackedMessage,
|
|
30
|
+
} from "@sovrahq/kms-core";
|
|
31
|
+
import { VerifiableCredential } from "@sovrahq/vc-core";
|
|
32
|
+
import "@sovrahq/kms-suite-didcomm";
|
|
33
|
+
import "@sovrahq/kms-suite-didcomm-v2";
|
|
34
|
+
import "@sovrahq/kms-suite-es256k";
|
|
35
|
+
import "@sovrahq/kms-suite-rsa-signature-2018";
|
|
36
|
+
import { DIDCommSuite as DIDCommSuiteV1 } from "@sovrahq/kms-suite-didcomm";
|
|
37
|
+
import { DIDCommSuite } from "@sovrahq/kms-suite-didcomm-v2";
|
|
38
|
+
import { BbsBls2020Suite } from "@sovrahq/kms-suite-bbsbls2020";
|
|
39
|
+
import { RSASignature2018Suite } from "@sovrahq/kms-suite-rsa-signature-2018";
|
|
40
|
+
import { ES256kSuite } from "@sovrahq/kms-suite-es256k";
|
|
41
|
+
|
|
42
|
+
export class KMSClient implements IKMS {
|
|
43
|
+
suites: Map<Suite, new (...args: never[]) => any> = new Map();
|
|
44
|
+
|
|
45
|
+
constructor(
|
|
46
|
+
private config: {
|
|
47
|
+
lang: LANG;
|
|
48
|
+
storage: KMSStorage;
|
|
49
|
+
didResolver?: (did: string) => Promise<DIDDocument>;
|
|
50
|
+
mobile?: boolean;
|
|
51
|
+
}
|
|
52
|
+
) {
|
|
53
|
+
this.suites.set(Suite.DIDComm, DIDCommSuiteV1)
|
|
54
|
+
this.suites.set(Suite.DIDCommV2, DIDCommSuite)
|
|
55
|
+
this.suites.set(Suite.ES256k, ES256kSuite)
|
|
56
|
+
this.suites.set(Suite.RsaSignature2018, RSASignature2018Suite)
|
|
57
|
+
|
|
58
|
+
if (!config.mobile) {
|
|
59
|
+
import("@sovrahq/kms-suite-bbsbls2020");
|
|
60
|
+
this.suites.set(Suite.Bbsbls2020, BbsBls2020Suite)
|
|
61
|
+
}
|
|
62
|
+
if (!config.didResolver)
|
|
63
|
+
console.info(
|
|
64
|
+
"KMS didResolver not configured. You need set a didResolver to signVC"
|
|
65
|
+
);
|
|
66
|
+
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async create(suite: Suite): Promise<{ publicKeyJWK: IJWK }> {
|
|
70
|
+
const suiteType = this.suites.get(suite);
|
|
71
|
+
let suiteInstance;
|
|
72
|
+
|
|
73
|
+
if (!suiteType) {
|
|
74
|
+
throw new Error("Unsupported Suite");
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
suiteInstance = new suiteType();
|
|
78
|
+
|
|
79
|
+
let secrets: IKeyPair;
|
|
80
|
+
let jwk: IJWK;
|
|
81
|
+
|
|
82
|
+
if (suite == Suite.ES256k) {
|
|
83
|
+
const ethrSuite = suiteInstance as IES256kSuite;
|
|
84
|
+
secrets = await ethrSuite.create({ lang: this.config.lang });
|
|
85
|
+
jwk = BaseConverter.convert(secrets.publicKey, Base.Hex, Base.JWK, secrets.keyType || (<IES256kKeyPair>secrets).curve);
|
|
86
|
+
} else if (suite == Suite.Bbsbls2020) {
|
|
87
|
+
secrets = await suiteInstance.create();
|
|
88
|
+
jwk = BaseConverter.convert(secrets.publicKey, Base.Base58, Base.JWK, secrets.keyType);
|
|
89
|
+
} else if (suite == Suite.RsaSignature2018) {
|
|
90
|
+
secrets = await suiteInstance.create();
|
|
91
|
+
jwk = secrets!.publicKeyJWK!;
|
|
92
|
+
} else if (suite == Suite.DIDComm) {
|
|
93
|
+
secrets = await suiteInstance.create();
|
|
94
|
+
jwk = BaseConverter.convert(secrets.publicKey, Base.Hex, Base.JWK, secrets.keyType);
|
|
95
|
+
} else if (suite == Suite.DIDCommV2) {
|
|
96
|
+
secrets = await suiteInstance.create();
|
|
97
|
+
// Convert Ed25519 public key to X25519 for DIDComm key agreement
|
|
98
|
+
const ed25519 = require('@stablelib/ed25519');
|
|
99
|
+
const rawHex = secrets.publicKey.replace('0x', '');
|
|
100
|
+
const edPub = new Uint8Array(Buffer.from(rawHex, 'hex'));
|
|
101
|
+
const x25519Pub = ed25519.convertPublicKeyToX25519(edPub);
|
|
102
|
+
const x25519B64 = Buffer.from(x25519Pub).toString('base64')
|
|
103
|
+
.replace(/\+/g, '-').replace(/\//g, '_').replace(/=+$/, '');
|
|
104
|
+
jwk = { kty: 'OKP', crv: 'X25519', x: x25519B64 } as any;
|
|
105
|
+
} else {
|
|
106
|
+
throw new Error("Unsupported Suite");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
const pkHex = BaseConverter.convert(jwk, Base.JWK, Base.Hex);
|
|
110
|
+
|
|
111
|
+
await this.config.storage.add(pkHex, {
|
|
112
|
+
suite,
|
|
113
|
+
...secrets,
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
return { publicKeyJWK: jwk };
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
async sign(suite: Suite, publicKeyJWK: IJWK, content: any): Promise<string> {
|
|
120
|
+
const publicKeyHex = BaseConverter.convert(
|
|
121
|
+
publicKeyJWK,
|
|
122
|
+
Base.JWK,
|
|
123
|
+
Base.Hex
|
|
124
|
+
);
|
|
125
|
+
const suiteType = this.suites.get(suite);
|
|
126
|
+
|
|
127
|
+
if (!suiteType) {
|
|
128
|
+
throw new Error("Unsupported Suite");
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
const privateKey = (await this.config.storage.get(
|
|
132
|
+
publicKeyHex
|
|
133
|
+
)) as IES256kKeyPair;
|
|
134
|
+
const suiteInstance = new suiteType() as IES256kSuite;
|
|
135
|
+
|
|
136
|
+
await suiteInstance.load(privateKey);
|
|
137
|
+
|
|
138
|
+
return await suiteInstance.sign(content);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async verifySignature(publicKeyJWK: IJWK, originalContent: string, signature: string): Promise<boolean> {
|
|
142
|
+
const suiteType = this.suites.get(Suite.ES256k);
|
|
143
|
+
const suiteInstance = new suiteType() as IES256kSuite;
|
|
144
|
+
return await suiteInstance.verifySignature(originalContent, signature, publicKeyJWK);
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
async signVC(
|
|
148
|
+
suite: Suite,
|
|
149
|
+
publicKeyJWK: IJWK,
|
|
150
|
+
vc: any,
|
|
151
|
+
did: string,
|
|
152
|
+
verificationMethodId: string,
|
|
153
|
+
purpose: Purpose
|
|
154
|
+
): Promise<VerifiableCredential> {
|
|
155
|
+
const suiteType = this.suites.get(suite);
|
|
156
|
+
|
|
157
|
+
if (!suiteType) {
|
|
158
|
+
throw new Error("Unsupported Suite");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const suiteInstance = new suiteType() as IVCSuite;
|
|
162
|
+
|
|
163
|
+
const publicKeyHex = BaseConverter.convert(
|
|
164
|
+
publicKeyJWK,
|
|
165
|
+
Base.JWK,
|
|
166
|
+
Base.Hex
|
|
167
|
+
);
|
|
168
|
+
|
|
169
|
+
const key = (await this.config.storage.get(
|
|
170
|
+
publicKeyHex
|
|
171
|
+
)) as IVCJsonLDKeyPair;
|
|
172
|
+
|
|
173
|
+
suiteInstance.loadSuite({
|
|
174
|
+
secrets: key,
|
|
175
|
+
useCache: true,
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
return (await suiteInstance.sign(
|
|
179
|
+
vc,
|
|
180
|
+
did,
|
|
181
|
+
verificationMethodId,
|
|
182
|
+
purpose
|
|
183
|
+
)) as VerifiableCredential;
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
async deriveVC(params: {
|
|
187
|
+
vc: VerifiableCredential<any>,
|
|
188
|
+
frame: any,
|
|
189
|
+
}): Promise<VerifiableCredential<any>> {
|
|
190
|
+
const suiteType = this.suites.get(Suite.Bbsbls2020);
|
|
191
|
+
|
|
192
|
+
if (!suiteType) {
|
|
193
|
+
throw new Error("Unsupported Suite");
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const suiteInstance = new suiteType() as SelectiveDisclosureZKPSuite;
|
|
197
|
+
|
|
198
|
+
const derivedVc = await suiteInstance.deriveVC(params.vc, params.frame, this.config.didResolver);
|
|
199
|
+
|
|
200
|
+
return derivedVc;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async signVCPresentation(params: {
|
|
204
|
+
publicKeyJWK: IJWK,
|
|
205
|
+
presentationObject: any,
|
|
206
|
+
did: string,
|
|
207
|
+
verificationMethodId: string,
|
|
208
|
+
purpose: Purpose
|
|
209
|
+
}) {
|
|
210
|
+
const suiteType = this.suites.get(Suite.RsaSignature2018);
|
|
211
|
+
|
|
212
|
+
if (!suiteType) {
|
|
213
|
+
throw new Error("Unsupported Suite");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const suiteInstance = new suiteType() as IVCSuite;
|
|
217
|
+
|
|
218
|
+
const publicKeyHex = BaseConverter.convert(
|
|
219
|
+
params.publicKeyJWK,
|
|
220
|
+
Base.JWK,
|
|
221
|
+
Base.Hex
|
|
222
|
+
);
|
|
223
|
+
|
|
224
|
+
const key = (await this.config.storage.get(
|
|
225
|
+
publicKeyHex
|
|
226
|
+
)) as IVCJsonLDKeyPair;
|
|
227
|
+
|
|
228
|
+
suiteInstance.loadSuite({
|
|
229
|
+
secrets: key,
|
|
230
|
+
useCache: true,
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
return (await suiteInstance.sign(
|
|
234
|
+
params.presentationObject,
|
|
235
|
+
params.did,
|
|
236
|
+
params.verificationMethodId,
|
|
237
|
+
params.purpose
|
|
238
|
+
));
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
async pack(
|
|
242
|
+
publicKeyJWK: IJWK,
|
|
243
|
+
toHexPublicKeys: string[],
|
|
244
|
+
contentToSign: string
|
|
245
|
+
): Promise<string> {
|
|
246
|
+
const suiteType = this.suites.get(Suite.DIDComm);
|
|
247
|
+
|
|
248
|
+
if (!suiteType) {
|
|
249
|
+
throw new Error("Unsupported Suite");
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
const publicKeyHex = BaseConverter.convert(
|
|
253
|
+
publicKeyJWK,
|
|
254
|
+
Base.JWK,
|
|
255
|
+
Base.Hex
|
|
256
|
+
);
|
|
257
|
+
|
|
258
|
+
const privateKey = (await this.config.storage.get(
|
|
259
|
+
publicKeyHex
|
|
260
|
+
)) as IDidCommKeyPair;
|
|
261
|
+
const suiteInstance = new suiteType() as IDIDCommSuite;
|
|
262
|
+
|
|
263
|
+
suiteInstance.load(privateKey);
|
|
264
|
+
|
|
265
|
+
return await suiteInstance.pack(true, toHexPublicKeys, contentToSign);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
async packDIDCommV2(params: {
|
|
269
|
+
senderVerificationMethodId: string,
|
|
270
|
+
recipientVerificationMethodIds: string[],
|
|
271
|
+
message: IDIDCommMessage,
|
|
272
|
+
packing: DIDCommMessagePacking
|
|
273
|
+
}): Promise<{ packedMessage: DIDCommPackedMessage }> {
|
|
274
|
+
|
|
275
|
+
// if (!this.config.didResolver) {
|
|
276
|
+
// console.log()
|
|
277
|
+
// }
|
|
278
|
+
|
|
279
|
+
const suiteType = this.suites.get(Suite.DIDCommV2);
|
|
280
|
+
|
|
281
|
+
if (!suiteType) {
|
|
282
|
+
throw new Error("Unsupported Suite");
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
const receiptKeys = await Promise.all(params.recipientVerificationMethodIds.map(async vmId => {
|
|
286
|
+
const didDoc = await this.config.didResolver(vmId.substring(0, vmId.indexOf("#")));
|
|
287
|
+
|
|
288
|
+
const vm = await DIDDocumentUtils.getVerificationMethodById(didDoc, vmId) as VerificationMethodJwk;
|
|
289
|
+
|
|
290
|
+
if (vm) {
|
|
291
|
+
return {
|
|
292
|
+
verificationMethodId: vmId,
|
|
293
|
+
publicKeyHex: BaseConverter.convert(
|
|
294
|
+
vm.publicKeyJwk,
|
|
295
|
+
Base.JWK,
|
|
296
|
+
Base.Hex
|
|
297
|
+
),
|
|
298
|
+
publicKeyCrv: vm.publicKeyJwk && (vm.publicKeyJwk as any).crv,
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}));
|
|
302
|
+
|
|
303
|
+
const senderDoc = await this.config.didResolver(
|
|
304
|
+
params.senderVerificationMethodId.substring(0, params.senderVerificationMethodId.indexOf("#")));
|
|
305
|
+
|
|
306
|
+
const senderPbk = DIDDocumentUtils.getVerificationMethodById(senderDoc, params.senderVerificationMethodId) as VerificationMethodJwk;
|
|
307
|
+
|
|
308
|
+
const senderPublicKeyHex = BaseConverter.convert(
|
|
309
|
+
senderPbk.publicKeyJwk,
|
|
310
|
+
Base.JWK,
|
|
311
|
+
Base.Hex
|
|
312
|
+
);
|
|
313
|
+
|
|
314
|
+
const privateKey = (await this.config.storage.get(
|
|
315
|
+
senderPublicKeyHex
|
|
316
|
+
)) as IDidCommKeyPair;
|
|
317
|
+
|
|
318
|
+
if (!privateKey) {
|
|
319
|
+
throw new Error(`Cannot find private key for public '${senderPublicKeyHex}' key on KMS storage.`)
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
const suiteInstance = new (<any>suiteType)(this.config.didResolver) as IDIDCommV2Suite;
|
|
323
|
+
|
|
324
|
+
suiteInstance.load(privateKey);
|
|
325
|
+
|
|
326
|
+
const packedMessage = await suiteInstance.pack({
|
|
327
|
+
packing: params.packing,
|
|
328
|
+
message: params.message,
|
|
329
|
+
toKeys: receiptKeys,
|
|
330
|
+
senderVerificationMethodId: params.senderVerificationMethodId
|
|
331
|
+
});
|
|
332
|
+
|
|
333
|
+
return { packedMessage: JSON.parse(packedMessage.message) as DIDCommPackedMessage };
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
async packv2(publicKeyJWK: IJWK,
|
|
337
|
+
senderVerificationMethodId: string,
|
|
338
|
+
toHexPublicKeys: string[],
|
|
339
|
+
message: IDIDCommMessage,
|
|
340
|
+
packing: DIDCommMessagePacking
|
|
341
|
+
): Promise<IPackedDIDCommMessage> {
|
|
342
|
+
const suiteType = this.suites.get(Suite.DIDCommV2);
|
|
343
|
+
|
|
344
|
+
if (!suiteType) {
|
|
345
|
+
throw new Error("Unsupported Suite");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
const publicKeyHex = BaseConverter.convert(
|
|
349
|
+
publicKeyJWK,
|
|
350
|
+
Base.JWK,
|
|
351
|
+
Base.Hex
|
|
352
|
+
);
|
|
353
|
+
|
|
354
|
+
const privateKey = (await this.config.storage.get(
|
|
355
|
+
publicKeyHex
|
|
356
|
+
)) as IDidCommKeyPair;
|
|
357
|
+
const suiteInstance = new (<any>suiteType)(this.config.didResolver) as IDIDCommV2Suite;
|
|
358
|
+
|
|
359
|
+
suiteInstance.load(privateKey);
|
|
360
|
+
|
|
361
|
+
return await suiteInstance.pack({
|
|
362
|
+
packing: packing,
|
|
363
|
+
message: message,
|
|
364
|
+
senderVerificationMethodId: senderVerificationMethodId,
|
|
365
|
+
toKeys: toHexPublicKeys.map(x => ({
|
|
366
|
+
publicKeyHex: x,
|
|
367
|
+
verificationMethodId: senderVerificationMethodId
|
|
368
|
+
})),
|
|
369
|
+
});
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
async unpack(publicKeyJWK: IJWK, packedContent: string): Promise<string> {
|
|
373
|
+
const suiteType = this.suites.get(Suite.DIDComm);
|
|
374
|
+
|
|
375
|
+
if (!suiteType) {
|
|
376
|
+
throw new Error("Unsupported Suite");
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
const publicKeyHex = BaseConverter.convert(
|
|
380
|
+
publicKeyJWK,
|
|
381
|
+
Base.JWK,
|
|
382
|
+
Base.Hex
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
const privateKey = (await this.config.storage.get(
|
|
386
|
+
publicKeyHex
|
|
387
|
+
)) as IDidCommKeyPair;
|
|
388
|
+
const suiteInstance = new suiteType() as IDIDCommSuite;
|
|
389
|
+
|
|
390
|
+
suiteInstance.load(privateKey);
|
|
391
|
+
|
|
392
|
+
return await suiteInstance.unpack(packedContent);
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
async unpackv2(publicKeyJWK: IJWK, jwe: any): Promise<string> {
|
|
396
|
+
if (!this.config.didResolver) {
|
|
397
|
+
throw new Error("DID Resolver required on KMSClient instance to Unpack DIDComm v2")
|
|
398
|
+
}
|
|
399
|
+
|
|
400
|
+
const suiteType = this.suites.get(Suite.DIDCommV2);
|
|
401
|
+
|
|
402
|
+
if (!suiteType) {
|
|
403
|
+
throw new Error("Unsupported Suite");
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
const publicKeyHex = BaseConverter.convert(
|
|
407
|
+
publicKeyJWK,
|
|
408
|
+
Base.JWK,
|
|
409
|
+
Base.Hex
|
|
410
|
+
);
|
|
411
|
+
|
|
412
|
+
const privateKey = (await this.config.storage.get(
|
|
413
|
+
publicKeyHex
|
|
414
|
+
)) as IDidCommKeyPair;
|
|
415
|
+
const suiteInstance = new (<any>suiteType)(this.config.didResolver) as IDIDCommV2Suite;
|
|
416
|
+
|
|
417
|
+
suiteInstance.load(privateKey);
|
|
418
|
+
|
|
419
|
+
return await suiteInstance.unpack(jwe);
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
async unpackvDIDCommV2(receiptDID: string,
|
|
423
|
+
packedMessage: any | DIDCommPackedMessage): Promise<{ message: DIDCommMessage, metaData: { packing: DIDCommMessagePacking } }> {
|
|
424
|
+
if (!this.config.didResolver) {
|
|
425
|
+
throw new Error("DID Resolver required on KMSClient instance to Unpack DIDComm v2")
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
let publicKeyHex = null;
|
|
429
|
+
|
|
430
|
+
const dcpm = packedMessage as DIDCommPackedMessage;
|
|
431
|
+
const receiptKid = dcpm.recipients?.find(x => x.header.kid.indexOf(receiptDID) > -1);
|
|
432
|
+
|
|
433
|
+
if (receiptKid != null) {
|
|
434
|
+
const receiptDoc = await this.config.didResolver(receiptDID);
|
|
435
|
+
const pbk = DIDDocumentUtils.getVerificationMethodById(receiptDoc, receiptKid.header.kid) as VerificationMethodJwk;
|
|
436
|
+
publicKeyHex = BaseConverter.convert(pbk.publicKeyJwk, Base.JWK, Base.Hex, pbk.type);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const suiteType = this.suites.get(Suite.DIDCommV2);
|
|
440
|
+
|
|
441
|
+
if (!suiteType) {
|
|
442
|
+
throw new Error("Unsupported Suite");
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
const suiteInstance = new (<any>suiteType)(this.config.didResolver) as IDIDCommV2Suite;
|
|
446
|
+
|
|
447
|
+
if (publicKeyHex) {
|
|
448
|
+
const privateKey = (await this.config.storage.get(
|
|
449
|
+
publicKeyHex
|
|
450
|
+
)) as IDidCommKeyPair;
|
|
451
|
+
|
|
452
|
+
suiteInstance.load(privateKey);
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
const result = await suiteInstance.unpack({ message: packedMessage });
|
|
456
|
+
|
|
457
|
+
return result;
|
|
458
|
+
}
|
|
459
|
+
|
|
460
|
+
async export(publicKeyJWK: IJWK): Promise<IKeyPair> {
|
|
461
|
+
const publicKeyHex = BaseConverter.convert(
|
|
462
|
+
publicKeyJWK,
|
|
463
|
+
Base.JWK,
|
|
464
|
+
Base.Hex
|
|
465
|
+
);
|
|
466
|
+
|
|
467
|
+
const data = await this.config.storage.get(publicKeyHex);
|
|
468
|
+
return data;
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
async import(key: {
|
|
472
|
+
publicKeyHex: string,
|
|
473
|
+
secret: IKeyPair
|
|
474
|
+
}) {
|
|
475
|
+
await this.config.storage.add(key.publicKeyHex, key.secret);
|
|
476
|
+
}
|
|
477
|
+
|
|
478
|
+
async getPublicKeysBySuiteType(suite: Suite): Promise<IJWK[]> {
|
|
479
|
+
const data = await this.config.storage.getAll();
|
|
480
|
+
const returnKeys = new Array<IJWK>();
|
|
481
|
+
|
|
482
|
+
data.forEach((value: IKeyPair, key) => {
|
|
483
|
+
if (data.get(key).suite == suite)
|
|
484
|
+
value.publicKeyJWK ? returnKeys.push(value.publicKeyJWK) :
|
|
485
|
+
returnKeys.push(BaseConverter.convert(key, Base.Hex, Base.JWK, (<IES256kKeyPair>value).curve || value.keyType));
|
|
486
|
+
});
|
|
487
|
+
|
|
488
|
+
return returnKeys;
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
async getAllPublicKeys(): Promise<IJWK[]> {
|
|
492
|
+
const data = await this.config.storage.getAll();
|
|
493
|
+
return Array.from(data.keys()).map((x) =>
|
|
494
|
+
BaseConverter.convert(x, Base.Hex, Base.JWK, (<IES256kKeyPair>data.get(x)).curve || data.get(x).keyType)
|
|
495
|
+
);
|
|
496
|
+
}
|
|
497
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "es5",
|
|
4
|
+
"lib": [
|
|
5
|
+
"es6"
|
|
6
|
+
],
|
|
7
|
+
"types": [
|
|
8
|
+
"node",
|
|
9
|
+
"jest"
|
|
10
|
+
],
|
|
11
|
+
"resolveJsonModule": true,
|
|
12
|
+
"outDir": "dist",
|
|
13
|
+
"module": "commonjs",
|
|
14
|
+
"moduleResolution": "node",
|
|
15
|
+
"sourceMap": true,
|
|
16
|
+
"declaration": true,
|
|
17
|
+
"experimentalDecorators": true,
|
|
18
|
+
"emitDecoratorMetadata": true,
|
|
19
|
+
}
|
|
20
|
+
}
|