import { RawSignedModel } from './RawSignedModel' ; import { IPrivateKey , ICardCrypto } from '../types' ; import { IExtraData } from './ICard' ; import { SelfSigner } from './constants' ; /**
 * Parameters for the card signature generation.
 */ export interface IRawSignParams { /**
     * The card to generate the signature for in the form of
     *  RawSignedModel object.
      readonly model : RawSignedModel ; /**
     * The private key to use to generate the signature.
     */ readonly signerPrivateKey : IPrivateKey ; /**
     * Custom attributes to associate with the signature. If provided, these
     * will also be signed.
     */ readonly extraFields ? : IExtraData ; /**
     * Identifier of the signature. Default is "self".
     */ readonly signer ? : string ; } hidden interface IFinalSignParams { readonly model : RawSignedModel ; readonly signerPrivateKey : IPrivateKey ; readonly extraSnapshot ? : string ; readonly signer : string ; } /**
 * Class responsible for generating signatures of the cards.
 */ export class ModelSigner { param { ICardCrypto } crypto - Object implementing the
     *  ICardCrypto interface.
      public constructor ( private readonly crypto ) { } param { IRawSignParams } rawParams public sign ( rawParams ) { const { model , signerPrivateKey , signer , extraSnapshot } = this . prepareParams ( rawParams ) ; const signedSnapshot = extraSnapshot != null ? model . contentSnapshot + extraSnapshot : model . contentSnapshot ; const signature = this . crypto . generateSignature ( { value : signedSnapshot , encoding : 'utf8' } , signerPrivateKey ) ; model . signatures . push ( { signer , signature : signature . toString ( 'base64' ) , snapshot : extraSnapshot } ) ; } private prepareParams ( { model , signerPrivateKey , extraFields , signer } ) { signer = signer || SelfSigner ; let extraSnapshot ; if ( extraFields != null ) { extraSnapshot = JSON . stringify ( extraFields ) ; } const final = { model , signerPrivateKey , signer , extraSnapshot } ; this . validate ( final ) ; return final ; } private validate ( { model , signerPrivateKey , signer } ) { if ( model == null ) { throw new Error ( "Model is empty" ) ; } if ( signerPrivateKey == null ) { throw new Error ( "`signerPrivateKey` property is mandatory" ) ; } if ( model . signatures != null && model . signatures . some ( s => s . signer == signer ) ) { throw new Error ( "The model already has this signature." ) ; } } }