module worldsibu import * as uuid from 'uuid/v1' ; import * as selfsigned from 'selfsigned' ; import { pki , asn1 , md } from 'node-forge' ; import { Chaincode , IConfig } from '@worldsibu/convector-core-chaincode' ; import { ChaincodeMockStub , Transform } from '@theledger/fabric-mock-stub' ; import { ControllerAdapter , ClientResponseError } from '@worldsibu/convector-core' ; import { Transform as _Transform } from '@theledger/fabric-chaincode-utils' ; _Transform . isObject = data => data !== null && typeof data === 'object' ; export interface CertificateProps { commonName : string ; countryName ? : string ; localityName ? : string ; stateOrProvinceName ? : string ; organizationName ? : string ; organizationalUnitName ? : string ; emailAddress ? : string ; } export class MockControllerAdapter implements ControllerAdapter { public users = { } ; public stub = new ChaincodeMockStub ( 'Participant' , new Chaincode ( ) ) ; public async init ( config ) { await this . stub . mockInit ( uuid ( ) , [ ] ) ; await this . stub . mockInvoke ( uuid ( ) , [ 'initControllers' , JSON . stringify ( config ) ] ) ; } public async getById < T > ( id ) { const response = await this . stub . getState ( id ) ; return Transform . bufferToObject ( response ) as any ; } public async invoke ( controller , name , config = { } , ... args ) { this . stub [ 'usercert' ] = config . user ? this . users [ config . user ] || config . user : this . stub [ 'usercert' ] ; const transientMap = Object . keys ( config . transient || { } ) . reduce ( ( map , k ) => { const v = config . transient [ k ] ; map . set ( k , Buffer . from ( typeof v === 'string' ? v : JSON . stringify ( v ) ) ) ; return map ; } , new Map < string , Buffer > ( ) ) ; const response = await this . stub . mockInvoke ( uuid ( ) , [ `${ controller }_${ name }` , ... args . map ( arg => typeof arg === 'object' ? JSON . stringify ( arg ) : arg === undefined ? undefined : arg . toString ( ) ) ] , transientMap ) ; if ( response . status === 500 ) { let err = response . message . toString ( ) ; try { err = JSON . parse ( JSON . parse ( err ) ) ; } catch ( e ) { try { err = JSON . parse ( err ) ; } catch ( e ) { } } throw new ClientResponseError ( [ { error : err , response } ] ) ; } return { ... response , result : Transform . bufferToObject ( response . payload ) , } ; } addUser ( name , certificate ? ) ; addUser ( props ) ; addUser ( props , certificate ? ) { let name ; if ( typeof props === 'string' ) { name = props ; props = { commonName : props } ; if ( certificate ) { this . users [ name ] = certificate ; return ; } } else { name = props . commonName ; } const attrsFormatted = Object . keys ( props ) . map ( k => ( { name : k , value : props [ k ] } ) ) ; const pems = selfsigned . generate ( attrsFormatted ) ; this . users [ name ] = pems . cert ; } getUserFingerprint ( user ) { const cert = pki . certificateFromPem ( this . users [ user ] ) ; return md . sha1 . create ( ) . update ( asn1 . toDer ( pki . certificateToAsn1 ( cert ) ) . getBytes ( ) ) . digest ( ) . toHex ( ) . toUpperCase ( ) . replace ( /(.{2})(?=.)/g , '$1:' ) ; } }