'use strict' ; import * as fs from 'fs-extra' ; const { Certificate } = require ( '@fidm/x509' ) ; const ENCODING = 'utf8' ; export interface Attribute { name : string ; value : string ; ecert : boolean ; } export class FabricCertificate { public static validateCertificate ( cert ) { try { Certificate . fromPEM ( cert ) ; } catch ( error ) { throw new Error ( `Invalid certificate: ${ error . message }` ) ; } } public static validatePrivateKey ( key ) { if ( ! key . includes ( '-----BEGIN PRIVATE KEY-----' ) || ! key . includes ( '-----END PRIVATE KEY-----' ) ) { throw new Error ( 'Invalid private key' ) ; } } public static loadFileFromDisk ( path ) { return fs . readFileSync ( path , ENCODING ) ; } private certificate : any ; constructor ( cert ) { this . certificate = Certificate . fromPEM ( cert ) ; } public getCommonName ( ) { return this . certificate . subject . commonName ; } public getAttributes ( ) { const magicObject = this . certificate . extensions . find ( ( attrObj ) => { return attrObj . id === '1.2.3.4.5.6.7.8.1' ; } ) ; if ( magicObject ) { const attrString = magicObject . value . toString ( 'utf8' ) ; const attrObject = JSON . parse ( attrString ) ; return attrObject . attrs ; } else { return { } ; } } }