import { timeout } from 'rxjs/operators' ; import { Inject , Injectable } from '@nestjs/common' ; import { ClientProxy } from '@nestjs/microservices' ; import { ConfigService } from '@fc/config' ; import { LoggerLevelNames , LoggerService } from '@fc/logger' ; import { CryptoProtocol } from '@fc/microservices' ; import { RabbitmqConfig } from '@fc/rabbitmq' ; import { CryptographyGatewayException , CryptographyInvalidPayloadFormatException , } from '../exceptions' ; import { OverrideCode } from '../helpers' ; @ Injectable ( ) export class CryptoOverrideService { constructor ( private readonly config , private readonly logger , @ Inject ( 'CryptographyBroker' ) private broker ) { this . logger . setContext ( this . constructor . name ) ; } onModuleInit ( ) { see . . see your this . registerOverride ( 'crypto.sign' ) ; this . broker . connect ( ) ; } onModuleDestroy ( ) { this . broker . close ( ) ; } private registerOverride ( name ) { OverrideCode . override ( name , this [ name ] . bind ( this ) ) ; } /**
     * Use our own sign library
     */ private [ 'crypto.sign' ] ( nodeAlg , payload , key ) { this . logger . debug ( 'Run override for crypto.sign' ) ; this . logger . trace ( { nodeAlg , payload , key } ) ; if ( ! ( payload instanceof Uint8Array ) ) { throw new CryptographyInvalidPayloadFormatException ( ) ; } see https see https return this . sign ( key , Buffer . from ( payload ) , nodeAlg ) ; } param { any } _unusedKey param { Buffer } dataBuffer param { string } digest returns { Promise < Buffer > } async sign ( _unusedKey , dataBuffer , digest = 'sha256' ) { return new Promise ( ( resolve , reject ) => { const { payloadEncoding , requestTimeout } = this . config . get < RabbitmqConfig > ( 'CryptographyBroker' ) ; this . logger . debug ( 'CryptoOverrideService.sign()' ) ; this . logger . trace ( { sign : { payloadEncoding , requestTimeout } } ) ; try { const payload = { data : dataBuffer . toString ( payloadEncoding ) , digest , } ; const next = this . signSuccess . bind ( this , resolve , reject ) ; const error = ( error ) : void => { this . logger . trace ( { error } , LoggerLevelNames . WARN ) ; reject ( new CryptographyGatewayException ( ) ) ; } ; this . broker . send ( CryptoProtocol . Commands . SIGN , payload ) . pipe ( timeout ( requestTimeout ) ) . subscribe ( { next , error , } ) ; } catch ( error ) { this . logger . trace ( { error } , LoggerLevelNames . WARN ) ; return reject ( new CryptographyGatewayException ( ) ) ; } } ) ; } private signSuccess ( resolve , reject , data ) { this . logger . debug ( 'CryptoOverrideService.signSuccess' ) ; const { payloadEncoding } = this . config . get < RabbitmqConfig > ( 'CryptographyBroker' ) ; TODO see https if ( data === 'ERROR' ) { this . logger . trace ( { error : { data , payloadEncoding } } , LoggerLevelNames . WARN ) ; reject ( new CryptographyGatewayException ( ) ) ; } this . logger . trace ( { data , payloadEncoding } ) ; resolve ( Buffer . from ( data , payloadEncoding ) ) ; } }