Skip to content

Commit

Permalink
fix iden3comm folder
Browse files Browse the repository at this point in the history
  • Loading branch information
volodymyr-basiuk committed Jul 13, 2023
1 parent 10eca58 commit 605be1d
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 22 deletions.
19 changes: 19 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"@microsoft/api-extractor": "^7.34.4",
"@types/chai": "^4.3.4",
"@types/chai-as-promised": "^7.1.5",
"@types/elliptic": "^6.4.14",
"@types/jsonld": "^1.4.8",
"@types/mocha": "^10.0.1",
"@types/node": "^18.16.19",
Expand Down
1 change: 1 addition & 0 deletions src/iden3comm/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export const ErrSenderNotUsedTokenCreation = 'sender of message is not used for
export const ErrPackedWithUnsupportedCircuit = 'message was packed with unsupported circuit';
export const ErrProofIsInvalid = 'message proof is invalid';
export const ErrStateVerificationFailed = 'message state verification failed';
export const ErrNoProvingMethodAlg = 'unknown proving method algorithm';
4 changes: 2 additions & 2 deletions src/iden3comm/handlers/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,14 @@ export class AuthHandler implements IAuthHandler {
type: PROTOCOL_MESSAGE_TYPE.AUTHORIZATION_RESPONSE_MESSAGE_TYPE,
thid: authRequest.thid ?? guid,
body: {
message: authRequest.body.message,
message: authRequest?.body?.message,
scope: []
},
from: userGenesisDID.string(),
to: authRequest.from
};

for (const r of zkpRequestsWithCreds) {
for (const r of zkpRequestsWithCreds || []) {
const zkpRes: ZeroKnowledgeProofResponse = await this._proofService.generateProof(
r.req,
userGenesisDID,
Expand Down
15 changes: 9 additions & 6 deletions src/iden3comm/handlers/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ export class FetchHandler implements IFetchHandler {
}
const credentials: W3CCredential[] = [];

for (let index = 0; index < offerMessage.body.credentials.length; index++) {
const credentialInfo = offerMessage.body.credentials[index];
for (let index = 0; index < (offerMessage?.body?.credentials?.length ?? 0); index++) {
const credentialInfo = offerMessage?.body?.credentials[index];

const guid = uuid.v4();
const fetchRequest: MessageFetchRequestMessage = {
Expand All @@ -107,7 +107,7 @@ export class FetchHandler implements IFetchHandler {
type: PROTOCOL_MESSAGE_TYPE.CREDENTIAL_FETCH_REQUEST_MESSAGE_TYPE,
thid: offerMessage.thid ?? guid,
body: {
id: credentialInfo.id
id: credentialInfo?.id || ''
},
from: did.string(),
to: offerMessage.from
Expand All @@ -119,6 +119,9 @@ export class FetchHandler implements IFetchHandler {
);
let message: { body: { credential: W3CCredential } };
try {
if (!offerMessage?.body?.url) {
throw new Error(`could not fetch W3C credential, body url is missing`);
}
const resp = await fetch(offerMessage.body.url, {
method: 'post',
headers: {
Expand All @@ -127,13 +130,13 @@ export class FetchHandler implements IFetchHandler {
body: token
});
if (resp.status !== 200) {
throw new Error(`could not fetch W3C credential, ${credentialInfo.id}`);
throw new Error(`could not fetch W3C credential, ${credentialInfo?.id}`);
}
message = await resp.json();
credentials.push(message.body.credential);
} catch (e) {
} catch (e: any) {
throw new Error(
`could not fetch W3C credential, ${credentialInfo.id}, error: ${e.message ?? e}`
`could not fetch W3C credential, ${credentialInfo?.id}, error: ${e.message ?? e}`
);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/iden3comm/packers/jws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class JWSPacker implements IPacker {
throw new Error('Missing sender DID');
}

const vmTypes: string[] = SUPPORTED_PUBLIC_KEY_TYPES[params.alg];
const vmTypes: string[] = SUPPORTED_PUBLIC_KEY_TYPES[params.alg as keyof typeof SUPPORTED_PUBLIC_KEY_TYPES];
if (!vmTypes?.length) {
throw new Error(`No supported verification methods for algorithm ${params.alg}`);
}
Expand Down
30 changes: 20 additions & 10 deletions src/iden3comm/packers/zkp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { AuthV2PubSignals, CircuitId } from '../../circuits/index';
import { DID, Id } from '@iden3/js-iden3-core';
import { bytesToProtocolMessage } from '../utils/envelope';
import {
ErrNoProvingMethodAlg,
ErrPackedWithUnsupportedCircuit,
ErrProofIsInvalid,
ErrSenderNotUsedTokenCreation,
Expand Down Expand Up @@ -110,19 +111,28 @@ export class ZKPPacker implements IPacker {
*/
async pack(payload: Uint8Array, params: ZKPPackerParams): Promise<Uint8Array> {
const provingMethod = await getProvingMethod(params.provingMethodAlg);
const { provingKey, wasm, dataPreparer } = this.provingParamsMap.get(
const provingParams = this.provingParamsMap.get(
params.provingMethodAlg.toString()
);

if (!provingParams) {
throw new Error(ErrNoProvingMethodAlg);
}

const token = new Token(
provingMethod,
byteDecoder.decode(payload),
(hash: Uint8Array, circuitID: CircuitId) => {
return dataPreparer.prepare(hash, params.senderDID, params.profileNonce, circuitID);
(hash: Uint8Array, circuitID: string) => {
return provingParams?.dataPreparer?.prepare(
hash,
params.senderDID,
params.profileNonce,
CircuitId[circuitID as keyof typeof CircuitId]
);
}
);
token.setHeader(Header.Type, MediaType.ZKPMessage);
const tokenStr = await token.prove(provingKey, wasm);
const tokenStr = await token.prove(provingParams.provingKey, provingParams.wasm);
return byteEncoder.encode(tokenStr);
}

Expand All @@ -135,18 +145,18 @@ export class ZKPPacker implements IPacker {
async unpack(envelope: Uint8Array): Promise<BasicMessage> {
const token = await Token.parse(byteDecoder.decode(envelope));
const provingMethodAlg = new ProvingMethodAlg(token.alg, token.circuitId);
const { key: verificationKey, verificationFn } = this.verificationParamsMap.get(
const verificationParams = this.verificationParamsMap.get(
provingMethodAlg.toString()
);
if (!verificationKey) {
if (!verificationParams?.key) {
throw new Error(ErrPackedWithUnsupportedCircuit);
}
const isValid = await token.verify(verificationKey);
const isValid = await token.verify(verificationParams?.key);
if (!isValid) {
throw new Error(ErrProofIsInvalid);
}

const verificationResult = await verificationFn.verify(
const verificationResult = await verificationParams?.verificationFn?.verify(
token.circuitId,
token.zkProof.pub_signals
);
Expand All @@ -170,7 +180,7 @@ export class ZKPPacker implements IPacker {
const verifySender = (token: Token, msg: BasicMessage): void => {
switch (token.circuitId) {
case CircuitId.AuthV2:
if (!verifyAuthV2Sender(msg.from, token.zkProof.pub_signals)) {
if (!msg.from || !verifyAuthV2Sender(msg.from, token.zkProof.pub_signals)) {
throw new Error(ErrSenderNotUsedTokenCreation);
}
break;
Expand All @@ -183,7 +193,7 @@ const verifyAuthV2Sender = (from: string, pubSignals: Array<string>): boolean =>
const authSignals = new AuthV2PubSignals();

const pubSig = authSignals.pubSignalsUnmarshal(byteEncoder.encode(JSON.stringify(pubSignals)));
return checkSender(from, pubSig.userID);
return pubSig.userID ? checkSender(from, pubSig.userID) : false;
};

const checkSender = (from: string, id: Id): boolean => {
Expand Down
6 changes: 3 additions & 3 deletions src/iden3comm/utils/did.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const resolveDIDDocument = async (
const response = await fetch(`${UNIVERSAL_RESOLVER_URL}/${didUrl}`);
const data = await response.json();
return data as DIDResolutionResult;
} catch (error) {
} catch (error: any) {
throw new Error(`Can't resolve did document: ${error.message}`);
}
};
Expand Down Expand Up @@ -52,9 +52,9 @@ const secp256k1 = new elliptic.ec('secp256k1');

export const extractPublicKeyBytes = (
vm: VerificationMethod
): { publicKeyBytes: Uint8Array; kmsKeyType?: KmsKeyType } => {
): { publicKeyBytes: Uint8Array | null; kmsKeyType?: KmsKeyType } => {
const isSupportedVmType = Object.keys(SUPPORTED_PUBLIC_KEY_TYPES).some((key) =>
SUPPORTED_PUBLIC_KEY_TYPES[key].includes(vm.type)
SUPPORTED_PUBLIC_KEY_TYPES[key as keyof typeof SUPPORTED_PUBLIC_KEY_TYPES].includes(vm.type)
);
if (vm.publicKeyBase58 && isSupportedVmType) {
return { publicKeyBytes: base58ToBytes(vm.publicKeyBase58), kmsKeyType: KmsKeyType.Secp256k1 };
Expand Down

0 comments on commit 605be1d

Please sign in to comment.