module import * as forge from 'node-forge' ; import * as jsigs from 'jsonld-signatures' ; import { RSAKeyPair } from 'crypto-ld' ; import { createDocumentLoader , strictExpansionMap } from '../src' ; import { RsaKeyPair , PrivateKeyPem , PublicKeyMeta } from '../../types' ; const { RsaSignature2018 } = jsigs . suites ; const { AssertionProofPurpose } = jsigs . purposes ; function param options param { number } options . bits returns { object } export const generateRsaKeyPair = ( { bits = 2048 } = { } ) : RsaKeyPair => { const keypair = forge . pki . rsa . generateKeyPair ( { bits } ) ; const publicKeyPem = forge . pki . publicKeyToPem ( keypair . publicKey ) ; const privateKeyPem = forge . pki . privateKeyToPem ( keypair . privateKey ) ; return { publicKeyPem , privateKeyPem } ; } ; function param { object } document param { PublicKeyMeta } publicKey param { string } privateKeyPem param options param { object } options . documentLoader returns { Promise < object > } export const signRsaSignature = async ( document , publicKey , privateKeyPem , { documentLoader = createDocumentLoader ( ) , } = { } ) => { const publicKeyMetadata = { '@context' : jsigs . SECURITY_CONTEXT_URL , type : 'RsaVerificationKey2018' , ... publicKey } ; const key = new RSAKeyPair ( { ... publicKeyMetadata , privateKeyPem } ) ; const signed = await jsigs . sign ( document , { suite : new RsaSignature2018 ( { key } ) , purpose : new AssertionProofPurpose ( ) , documentLoader , strictExpansionMap } ) ; return signed ; } ; function param { object } document param options param { object } options . documentLoader returns { Promise < boolean > } export const verifyRsaSignature = async ( document , { documentLoader = createDocumentLoader ( ) , } = { } ) : Promise < boolean > => { const result = await jsigs . verify ( document , { suite : new RsaSignature2018 ( ) , purpose : new AssertionProofPurpose ( ) , expansionMap : false , documentLoader } ) ; return result . verified ; } ;