import { adminApiVersions } from '../config' ; import { AddressInformation } from '../types/database' ; import { Address , VerifiedAddress , VerifiedAddressSignature , } from '../types/protocol' ; import { ParseError , ParseErrorType } from '../utils/errors' ; param maybeAddresses param maybeVerifiedAddresses param maybeIdentityKey param requestVersion returns export default function parseAllAddresses ( maybeAddresses , maybeVerifiedAddresses , maybeIdentityKey , requestVersion ) { const addresses = maybeAddresses ?? [ ] ; const verifiedAddresses = maybeVerifiedAddresses ?? [ ] ; let allAddresses = [ ] ; if ( requestVersion < adminApiVersions [ 1 ] ) { allAddresses = allAddresses . concat ( addresses as AddressInformation [ ] , verifiedAddresses as AddressInformation [ ] ) ; } else if ( requestVersion >= adminApiVersions [ 1 ] ) { const formattedAddresses = ( addresses as Address [ ] ) . map ( ( address ) => { return { paymentNetwork : address . paymentNetwork , ... ( address . environment && { environment : address . environment } ) , details : address . addressDetails , } ; } ) ; const formattedVerifiedAddressesAndKey = parseVerifiedAddresses ( verifiedAddresses as VerifiedAddress [ ] ) ; allAddresses = allAddresses . concat ( formattedAddresses , formattedVerifiedAddressesAndKey [ 0 ] ) ; return [ allAddresses , formattedVerifiedAddressesAndKey [ 1 ] ] ; } return [ allAddresses , maybeIdentityKey ] ; } param verifiedAddresses returns function parseVerifiedAddresses ( verifiedAddresses ) { const identityKeyLabel = 'identityKey' ; const formattedAddresses = [ ] ; let identityKey ; verifiedAddresses . forEach ( ( verifiedAddress ) => { let identityKeySignature ; let identityKeyCount = 0 ; verifiedAddress . signatures . forEach ( ( signaturePayload ) => { let decodedKey ; try { decodedKey = JSON . parse ( Buffer . from ( signaturePayload . protected , 'base64' ) . toString ( ) ) ; } catch ( _err ) { throw new ParseError ( 'Invalid JSON for protected payload (identity key).' , ParseErrorType . InvalidIdentityKey ) ; } if ( ! identityKey && decodedKey . name === identityKeyLabel ) { identityKey = signaturePayload . protected ; identityKeyCount += 1 ; identityKeySignature = signaturePayload . signature ; } else { if ( decodedKey . name === identityKeyLabel ) { identityKeyCount += 1 ; identityKeySignature = signaturePayload . signature ; } if ( identityKey !== signaturePayload . protected && decodedKey . name === identityKeyLabel ) { throw new ParseError ( 'More than one identity key detected. Only one identity key per PayID can be used.' , ParseErrorType . MultipleIdentityKeys ) ; } if ( identityKeyCount > 1 ) { throw new ParseError ( 'More than one identity key detected. Only one identity key per address can be used.' , ParseErrorType . MultipleIdentityKeys ) ; } } } ) ; const jwsPayload = JSON . parse ( verifiedAddress . payload ) ; const databaseAddressPayload = { paymentNetwork : jwsPayload . payIdAddress . paymentNetwork , environment : jwsPayload . payIdAddress . environment , details : { address : jwsPayload . payIdAddress . addressDetails . address , } , identityKeySignature , } ; formattedAddresses . push ( databaseAddressPayload ) ; } ) ; return [ formattedAddresses , identityKey ] ; }