update sdk

This commit is contained in:
turnoffthiscomputer
2024-08-28 15:46:39 +02:00
parent ef42d4b41a
commit 16c6add44e
4 changed files with 59 additions and 58 deletions

View File

@@ -1,6 +1,6 @@
{
"name": "@proofofpassport/sdk",
"version": "1.6.6",
"version": "1.6.9",
"main": "dist/sdk/src/index.js",
"types": "dist/sdk/src/index.d.ts",
"license": "MIT",
@@ -58,4 +58,4 @@
"common",
"circuits/**/*.json"
]
}
}

View File

@@ -36,14 +36,14 @@ export class OpenPassport1StepVerifier {
this.dev_mode = options.dev_mode || false;
}
async verify(
openPassport1StepInputs: OpenPassport1StepInputs
): Promise<OpenPassportVerifierReport> {
const { signatureAlgorithm, hashFunction } = getSignatureAlgorithm(openPassport1StepInputs.dsc);
const vkey = getVkey(openPassport1StepInputs.circuit, signatureAlgorithm, hashFunction);
const parsedPublicSignals = parsePublicSignals1Step(openPassport1StepInputs.dscProof.publicSignals);
const parsedPublicSignals = parsePublicSignals1Step(
openPassport1StepInputs.dscProof.publicSignals
);
//1. Verify the scope
if (parsedPublicSignals.scope !== this.scope) {
this.report.exposeAttribute('scope', parsedPublicSignals.scope, this.scope);
@@ -120,7 +120,7 @@ export class OpenPassport1StepInputs {
dscProof: {
publicSignals: string[];
proof: string[];
}
};
dsc: string;
circuit: string;

View File

@@ -2,15 +2,17 @@ import QRCode from 'easyqrcodejs';
import { AppType } from '../../common/src/utils/appType';
export class QRCodeGenerator {
static async generateQRCode(appData: AppType, size: number = 256): Promise<HTMLElement> {
static generateQRCode(appData: AppType, size: number = 256): HTMLElement {
const qrData = this.serializeAppType(appData);
const options = {
text: qrData,
width: size,
height: size,
};
const element = document.createElement('div');
new QRCode(element, options);
return element;
}

View File

@@ -5,75 +5,74 @@ import { derToBytes } from '../../common/src/utils/csca';
import forge from 'node-forge';
import { SKI_PEM, SKI_PEM_DEV } from './skiPem';
import {
vkey_prove_rsa_65537_sha1,
vkey_prove_rsa_65537_sha256,
vkey_prove_rsapss_65537_sha256,
vkey_prove_rsa_65537_sha1,
vkey_prove_rsa_65537_sha256,
vkey_prove_rsapss_65537_sha256,
} from '../../common/src/constants/vkey';
import { getCircuitName } from '../../common/src/utils/handleCertificate';
export function getCurrentDateFormatted() {
return getCurrentDateYYMMDD().map((datePart) => BigInt(datePart).toString());
return getCurrentDateYYMMDD().map((datePart) => BigInt(datePart).toString());
}
export function getVkey(circuit: string, signatureAlgorithm: string, hashFunction: string) {
const circuitName = getCircuitName(circuit, signatureAlgorithm, hashFunction);
switch (circuitName) {
case 'prove_rsa_65537_sha256':
return vkey_prove_rsa_65537_sha256;
case 'prove_rsa_65537_sha1':
return vkey_prove_rsa_65537_sha1;
case 'prove_rsapss_65537_sha256':
return vkey_prove_rsapss_65537_sha256;
default:
throw new Error('Invalid signature algorithm or hash function');
}
const circuitName = getCircuitName(circuit, signatureAlgorithm, hashFunction);
switch (circuitName) {
case 'prove_rsa_65537_sha256':
return vkey_prove_rsa_65537_sha256;
case 'prove_rsa_65537_sha1':
return vkey_prove_rsa_65537_sha1;
case 'prove_rsapss_65537_sha256':
return vkey_prove_rsapss_65537_sha256;
default:
throw new Error('Invalid signature algorithm or hash function');
}
}
// OpenPassport2Step
export async function checkMerkleRoot(rpcUrl: string, merkleRoot: number) {
const provider = new ethers.JsonRpcProvider(rpcUrl);
const contract = new ethers.Contract(REGISTER_CONTRACT_ADDRESS, REGISTER_ABI, provider);
return await contract.checkRoot(merkleRoot);
const provider = new ethers.JsonRpcProvider(rpcUrl);
const contract = new ethers.Contract(REGISTER_CONTRACT_ADDRESS, REGISTER_ABI, provider);
return await contract.checkRoot(merkleRoot);
}
// OpenPassport1Step
function getCSCAPem(formattedValueAdjusted: string, dev_mode: boolean): string {
const skiPem = dev_mode ? { ...SKI_PEM, ...SKI_PEM_DEV } : SKI_PEM;
const pem = skiPem[formattedValueAdjusted];
return pem;
const skiPem = dev_mode ? { ...SKI_PEM, ...SKI_PEM_DEV } : SKI_PEM;
const pem = skiPem[formattedValueAdjusted];
return pem;
}
export function verifyDSCValidity(dscCertificate: forge.pki.Certificate, dev_mode: boolean) {
const authorityKeyIdentifierExt = dscCertificate.extensions.find(
(ext) => ext.name === 'authorityKeyIdentifier'
);
const value = authorityKeyIdentifierExt.value;
const byteArray = derToBytes(value);
const formattedValue = byteArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
const formattedValueAdjusted = formattedValue.substring(8); // Remove the first '3016' from the formatted string
const csca_pem = getCSCAPem(formattedValueAdjusted, dev_mode);
if (csca_pem === null || csca_pem === undefined) {
console.error('Error: CSCA PEM not found');
throw new Error('CSCA PEM not found');
const authorityKeyIdentifierExt = dscCertificate.extensions.find(
(ext) => ext.name === 'authorityKeyIdentifier'
);
const value = authorityKeyIdentifierExt.value;
const byteArray = derToBytes(value);
const formattedValue = byteArray.map((byte) => byte.toString(16).padStart(2, '0')).join('');
const formattedValueAdjusted = formattedValue.substring(8); // Remove the first '3016' from the formatted string
const csca_pem = getCSCAPem(formattedValueAdjusted, dev_mode);
if (csca_pem === null || csca_pem === undefined) {
console.error('Error: CSCA PEM not found');
throw new Error('CSCA PEM not found');
}
const csca_certificate = forge.pki.certificateFromPem(csca_pem);
try {
const caStore = forge.pki.createCaStore([csca_certificate]);
const verified = forge.pki.verifyCertificateChain(caStore, [dscCertificate]);
if (!verified) {
throw new Error('DSC certificate verification failed');
}
const csca_certificate = forge.pki.certificateFromPem(csca_pem);
try {
const caStore = forge.pki.createCaStore([csca_certificate]);
const verified = forge.pki.verifyCertificateChain(caStore, [dscCertificate]);
if (!verified) {
throw new Error('DSC certificate verification failed');
}
const currentDate = new Date();
if (
currentDate < dscCertificate.validity.notBefore ||
currentDate > dscCertificate.validity.notAfter
) {
throw new Error('DSC certificate is not within its validity period');
}
return true;
} catch (error) {
console.error('DSC certificate validation error:', error);
return false;
const currentDate = new Date();
if (
currentDate < dscCertificate.validity.notBefore ||
currentDate > dscCertificate.validity.notAfter
) {
throw new Error('DSC certificate is not within its validity period');
}
return true;
} catch (error) {
console.error('DSC certificate validation error:', error);
return false;
}
}