Skip to content

Commit a495ed4

Browse files
committed
Added secp1256k provider
1 parent 13e0db0 commit a495ed4

30 files changed

+200
-243
lines changed

.eslintrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"parser": "@typescript-eslint/parser",
44
"plugins": ["@typescript-eslint", "prettier"],
55
"parserOptions": {
6-
"project": ["tsconfig.json"]
6+
"project": ["tsconfig.json", "tsconfig.test.json"]
77
},
88
"extends": [
99
"eslint:recommended",

package-lock.json

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/iden3comm/handlers/auth.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { byteDecoder, byteEncoder } from '../utils/index';
21
import { MediaType } from '../constants';
32
import { CircuitId } from '../../circuits/models';
43
import { IProofService } from '../../proof/proof-service';
@@ -19,6 +18,7 @@ import { proving } from '@iden3/js-jwz';
1918
import * as uuid from 'uuid';
2019
import { ICredentialWallet } from '../../credentials';
2120
import { W3CCredential } from '../../verifiable';
21+
import { byteDecoder, byteEncoder } from '../../utils';
2222

2323
/**
2424
* ZKP request and credential that satisfies the zkp query conditions

src/iden3comm/handlers/fetch.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import { byteDecoder, byteEncoder } from '../utils/index';
21
import { MediaType } from '../constants';
32
import { PROTOCOL_MESSAGE_TYPE } from '../constants';
43

@@ -14,6 +13,7 @@ import { proving } from '@iden3/js-jwz';
1413
import * as uuid from 'uuid';
1514
import { W3CCredential } from '../../verifiable';
1615
import axios from 'axios';
16+
import { byteDecoder, byteEncoder } from '../../utils';
1717

1818
/**
1919
* Interface that allows the processing of the credential offer in the raw format for given identifier

src/iden3comm/packageManager.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import { BasicMessage, IPackageManager, IPacker, PackerParams } from './types';
22
import { bytesToHeaderStub } from './utils/envelope';
33
import { base64 } from 'rfc4648';
4-
import { byteEncoder, byteDecoder } from './utils';
54
import { MediaType } from './constants';
5+
import { byteDecoder, byteEncoder } from '../utils';
66

77
/**
88
* Basic package manager for iden3 communication protocol

src/iden3comm/packers/jws.ts

+33-75
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,11 @@
11
import { BasicMessage, IPacker, PackerParams } from '../types';
2-
import { byteDecoder, byteEncoder } from '../utils';
32
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';
247
import { Resolvable, VerificationMethod } from 'did-resolver';
8+
import { byteDecoder, byteEncoder, bytesToHex, encodeBase64url } from '../../utils';
259
export type SignerFn = (vm: VerificationMethod, data: Uint8Array) => Signer;
2610

2711
/**
@@ -33,18 +17,6 @@ export type SignerFn = (vm: VerificationMethod, data: Uint8Array) => Signer;
3317
* @implements implements IPacker interface
3418
*/
3519
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-
4820
// readonly vmPubkeyExtractorHandlerMap = {
4921
// ES256K: getPubKeyHexFromVm,
5022
// 'ES256K-R': getPubKeyHexFromVm
@@ -56,7 +28,7 @@ export class JWSPacker implements IPacker {
5628
// };
5729

5830
constructor(
59-
private readonly _keyStore: AbstractPrivateKeyStore,
31+
private readonly _kms: KMS,
6032
private readonly _documentResolver: Resolvable = { resolve: resolveDIDDocument }
6133
) {}
6234
/**
@@ -75,6 +47,9 @@ export class JWSPacker implements IPacker {
7547
signer?: SignerFn;
7648
}
7749
): Promise<Uint8Array> {
50+
if (!params.alg) {
51+
throw new Error('Missing algorithm');
52+
}
7853
const message = JSON.parse(byteDecoder.decode(payload));
7954

8055
const from = message.from ?? '';
@@ -125,53 +100,38 @@ export class JWSPacker implements IPacker {
125100

126101
const kid = vm.id;
127102

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;
129110
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();
136113
} 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);
146115

147-
const signerAlg = this.signerAlgs[params.alg];
116+
if (!publicKeyBytes) {
117+
throw new Error('No public key found');
118+
}
148119

149-
if (!signerAlg) {
150-
throw new Error(`Unsupported algorithm ${params.alg}`);
120+
if (!kmsKeyType) {
121+
throw new Error('No KMS key type found');
151122
}
152123

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);
164130
}
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);
173131

174-
return byteEncoder.encode(jwt);
132+
// const signature = encodeBase64url(signatureHex);
133+
134+
return byteEncoder.encode(signingInput + '.' + signatureHex);
175135
}
176136

177137
/**
@@ -183,7 +143,6 @@ export class JWSPacker implements IPacker {
183143
async unpack(envelope: Uint8Array): Promise<BasicMessage> {
184144
const jwt = byteDecoder.decode(envelope);
185145
const decoded = decodeJWT(jwt);
186-
console.log('decoded', decoded);
187146

188147
const verificationResponse = await verifyJWT(jwt, {
189148
resolver: this._documentResolver
@@ -218,7 +177,6 @@ export class JWSPacker implements IPacker {
218177
// byteEncoder.encode(`${headerStr}.${msgStr}`),
219178
// fromBase64(signature64)
220179
// );
221-
console.log(verificationResponse);
222180
return {
223181
id: decoded.payload.id,
224182
typ: MediaType.SignedMessage

src/iden3comm/packers/plain.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
import { PlainPackerParams } from './../types/packer';
2-
import { byteEncoder } from './../utils/index';
32
import { BasicMessage, IPacker } from '../types';
4-
import { byteDecoder } from '../utils';
53
import { MediaType } from '../constants';
4+
import { byteDecoder, byteEncoder } from '../../utils';
65

76
/**
87
* Plain packer just serializes bytes to JSON and adds media type

src/iden3comm/packers/zkp.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ import {
1818
ErrStateVerificationFailed,
1919
ErrUnknownCircuitID
2020
} from '../errors';
21-
import { byteDecoder, byteEncoder } from '../utils';
2221
import { MediaType } from '../constants';
22+
import { byteDecoder, byteEncoder } from '../../utils';
2323

2424
const { getProvingMethod } = proving;
2525

@@ -180,7 +180,6 @@ const verifySender = (token: Token, msg: BasicMessage): void => {
180180
};
181181

182182
const verifyAuthV2Sender = (from: string, pubSignals: Array<string>): boolean => {
183-
const byteEncoder = new TextEncoder();
184183
const authSignals = new AuthV2PubSignals();
185184

186185
const pubSig = authSignals.pubSignalsUnmarshal(byteEncoder.encode(JSON.stringify(pubSignals)));

src/iden3comm/utils/bytes.ts

-21
This file was deleted.

0 commit comments

Comments
 (0)