1
1
import { BasicMessage , IPacker , PackerParams } from '../types' ;
2
- import { byteDecoder , byteEncoder } from '../utils' ;
3
2
import { MediaType , SUPPORTED_PUBLIC_KEY_TYPES } from '../constants' ;
4
- import {
5
- bytesToHex ,
6
- encodeBase64url ,
7
- extractPublicKeyBytes ,
8
- getDIDComponentById ,
9
- hexToBytes ,
10
- resolveDIDDocument
11
- } from '../utils/did' ;
12
- import { AbstractPrivateKeyStore , keyPath , KmsKeyType } from '../../kms/' ;
13
-
14
- import {
15
- ES256KSigner ,
16
- ES256Signer ,
17
- EdDSASigner ,
18
- JWTHeader ,
19
- Signer ,
20
- createJWT ,
21
- decodeJWT ,
22
- verifyJWT
23
- } from 'did-jwt' ;
3
+ import { extractPublicKeyBytes , getDIDComponentById , resolveDIDDocument } from '../utils/did' ;
4
+ import { keyPath , KMS } from '../../kms/' ;
5
+
6
+ import { Signer , decodeJWT , verifyJWT } from 'did-jwt' ;
24
7
import { Resolvable , VerificationMethod } from 'did-resolver' ;
8
+ import { byteDecoder , byteEncoder , bytesToHex , encodeBase64url } from '../../utils' ;
25
9
export type SignerFn = ( vm : VerificationMethod , data : Uint8Array ) => Signer ;
26
10
27
11
/**
@@ -33,18 +17,6 @@ export type SignerFn = (vm: VerificationMethod, data: Uint8Array) => Signer;
33
17
* @implements implements IPacker interface
34
18
*/
35
19
export class JWSPacker implements IPacker {
36
- readonly signerAlgs : { [ k : string ] : ( sk : Uint8Array ) => Signer } = {
37
- ES256 : ( sk : Uint8Array ) => ES256Signer ( sk ) ,
38
- ES256K : ( sk : Uint8Array ) => ES256KSigner ( sk ) ,
39
- 'ES256K-R' : ( sk : Uint8Array ) => ES256KSigner ( sk , true ) ,
40
- Ed25519 : ( sk : Uint8Array ) => EdDSASigner ( sk ) ,
41
- EdDSA : ( sk : Uint8Array ) => EdDSASigner ( sk )
42
- } ;
43
- readonly algToProviderKeyType = {
44
- ES256K : KmsKeyType . Secp256k1 ,
45
- 'ES256-R' : KmsKeyType . Secp256k1
46
- } ;
47
-
48
20
// readonly vmPubkeyExtractorHandlerMap = {
49
21
// ES256K: getPubKeyHexFromVm,
50
22
// 'ES256K-R': getPubKeyHexFromVm
@@ -56,7 +28,7 @@ export class JWSPacker implements IPacker {
56
28
// };
57
29
58
30
constructor (
59
- private readonly _keyStore : AbstractPrivateKeyStore ,
31
+ private readonly _kms : KMS ,
60
32
private readonly _documentResolver : Resolvable = { resolve : resolveDIDDocument }
61
33
) { }
62
34
/**
@@ -75,6 +47,9 @@ export class JWSPacker implements IPacker {
75
47
signer ?: SignerFn ;
76
48
}
77
49
) : Promise < Uint8Array > {
50
+ if ( ! params . alg ) {
51
+ throw new Error ( 'Missing algorithm' ) ;
52
+ }
78
53
const message = JSON . parse ( byteDecoder . decode ( payload ) ) ;
79
54
80
55
const from = message . from ?? '' ;
@@ -125,53 +100,38 @@ export class JWSPacker implements IPacker {
125
100
126
101
const kid = vm . id ;
127
102
128
- let signer : Signer ;
103
+ const headerObj = { alg : params . alg , kid, typ : MediaType . SignedMessage } ;
104
+ const header = encodeBase64url ( JSON . stringify ( headerObj ) ) ;
105
+ const msg = encodeBase64url ( JSON . stringify ( message ) ) ;
106
+ // construct signing input and obtain signature
107
+ const signingInput = header + '.' + msg ;
108
+ const signingInputBytes = byteEncoder . encode ( signingInput ) ;
109
+ let signatureHex : string ;
129
110
if ( params . signer ) {
130
- const headerObj = { alg : params . alg , kid, typ : MediaType . SignedMessage } ;
131
- const header = encodeBase64url ( JSON . stringify ( headerObj ) ) ;
132
- const msg = encodeBase64url ( JSON . stringify ( message ) ) ;
133
- // construct signing input and obtain signature
134
- const signingInput = byteEncoder . encode ( `${ header } .${ msg } ` ) ;
135
- signer = params . signer ( vm , signingInput ) ;
111
+ const signerFn = params . signer ( vm , signingInputBytes ) ;
112
+ signatureHex = ( await signerFn ( signingInput ) ) . toString ( ) ;
136
113
} else {
137
- const keyType = this . algToProviderKeyType [ params . alg ] ;
138
- if ( ! keyType ) {
139
- throw new Error ( `Unsupported algorithm ${ params . alg } ` ) ;
140
- }
141
- // console.log('pk', bytesToHex(extractPublicKeyBytes(vm)));
142
-
143
- const sk = await this . _keyStore . get ( {
144
- alias : keyPath ( keyType , bytesToHex ( extractPublicKeyBytes ( vm ) ) )
145
- } ) ;
114
+ const { publicKeyBytes, kmsKeyType } = extractPublicKeyBytes ( vm ) ;
146
115
147
- const signerAlg = this . signerAlgs [ params . alg ] ;
116
+ if ( ! publicKeyBytes ) {
117
+ throw new Error ( 'No public key found' ) ;
118
+ }
148
119
149
- if ( ! signerAlg ) {
150
- throw new Error ( `Unsupported algorithm ${ params . alg } ` ) ;
120
+ if ( ! kmsKeyType ) {
121
+ throw new Error ( 'No KMS key type found' ) ;
151
122
}
152
123
153
- signer = signerAlg ( hexToBytes ( sk ) ) ;
154
-
155
- // const type = this.algToProviderKeyType[params.alg];
156
- // if (!type) {
157
- // throw new Error(`Unsupported algorithm ${params.alg}`);
158
- // }
159
- // const pkFn = this.vmPubkeyExtractorHandlerMap[params.alg];
160
- // if (!pkFn) {
161
- // throw new Error(`Unsupported detect public key fetcher ${params.alg}`);
162
- // }
163
- // signature = await this._kms.sign({ type, id: keyPath(type, pkFn(vm)) }, signingInput);
124
+ const signatureBytes = await this . _kms . sign (
125
+ { type : kmsKeyType , id : keyPath ( kmsKeyType , bytesToHex ( publicKeyBytes ) ) } ,
126
+ signingInputBytes
127
+ ) ;
128
+
129
+ signatureHex = byteDecoder . decode ( signatureBytes ) ;
164
130
}
165
- // const signatureBase64 = toBase64(BytesHelper.bytesToHex(signature));
166
- // const tokenStr = `${header}.${msg}.${signatureBase64}`;
167
- // console.log('tokenStr', tokenStr);
168
- const jwt = await createJWT ( message , { issuer : params . issuer , signer } , {
169
- alg : params . alg ,
170
- kid,
171
- typ : MediaType . SignedMessage
172
- } as unknown as JWTHeader ) ;
173
131
174
- return byteEncoder . encode ( jwt ) ;
132
+ // const signature = encodeBase64url(signatureHex);
133
+
134
+ return byteEncoder . encode ( signingInput + '.' + signatureHex ) ;
175
135
}
176
136
177
137
/**
@@ -183,7 +143,6 @@ export class JWSPacker implements IPacker {
183
143
async unpack ( envelope : Uint8Array ) : Promise < BasicMessage > {
184
144
const jwt = byteDecoder . decode ( envelope ) ;
185
145
const decoded = decodeJWT ( jwt ) ;
186
- console . log ( 'decoded' , decoded ) ;
187
146
188
147
const verificationResponse = await verifyJWT ( jwt , {
189
148
resolver : this . _documentResolver
@@ -218,7 +177,6 @@ export class JWSPacker implements IPacker {
218
177
// byteEncoder.encode(`${headerStr}.${msgStr}`),
219
178
// fromBase64(signature64)
220
179
// );
221
- console . log ( verificationResponse ) ;
222
180
return {
223
181
id : decoded . payload . id ,
224
182
typ : MediaType . SignedMessage
0 commit comments