import { AddressInformation } from '../types/database' ; import { ParsedAcceptHeader } from '../types/headers' ; import { AddressDetailsType , PaymentInformation } from '../types/protocol' ; param addresses param verifiedAddresses param identityKey param version param payId param memoFn returns export function formatPaymentInfo ( addresses , verifiedAddresses , identityKey , version , payId , memoFn ? ) { const paymentInformation = { payId , version , addresses : addresses . map ( ( address ) => { return { paymentNetwork : address . paymentNetwork , ... ( address . environment && { environment : address . environment } ) , addressDetailsType : getAddressDetailsType ( address , version ) , addressDetails : address . details , } ; } ) , verifiedAddresses : verifiedAddresses . map ( ( address ) => { return { signatures : [ { name : 'identityKey' , protected : identityKey ?? '' , signature : address . identityKeySignature ?? '' , } , ] , payload : JSON . stringify ( { payId , payIdAddress : { paymentNetwork : address . paymentNetwork , ... ( address . environment && { environment : address . environment } ) , addressDetailsType : getAddressDetailsType ( address , version ) , addressDetails : address . details , } , } ) , } ; } ) , } ; return { ... paymentInformation , ... ( memoFn ?. ( paymentInformation ) && { memo : memoFn ( paymentInformation ) } ) , } ; } param allAddresses param allVerifiedAddresses param sortedParsedAcceptHeaders returns export function getPreferredAddressHeaderPair ( allAddresses , allVerifiedAddresses , sortedParsedAcceptHeaders ) { if ( allAddresses . length === 0 && allVerifiedAddresses . length === 0 ) { return [ undefined , [ ] , [ ] ] ; } for ( const acceptHeader of sortedParsedAcceptHeaders ) { if ( acceptHeader . paymentNetwork === 'PAYID' ) { return [ acceptHeader , allAddresses , allVerifiedAddresses ] ; } const foundAddress = allAddresses . find ( ( address ) => address . paymentNetwork === acceptHeader . paymentNetwork && ( address . environment ?? undefined ) === acceptHeader . environment ) ; const foundVerifiedAddress = allVerifiedAddresses . find ( ( address ) => address . paymentNetwork === acceptHeader . paymentNetwork && ( address . environment ?? undefined ) === acceptHeader . environment ) ; if ( foundAddress || foundVerifiedAddress ) { return [ acceptHeader , foundAddress ? [ foundAddress ] : [ ] , foundVerifiedAddress ? [ foundVerifiedAddress ] : [ ] , ] ; } } return [ undefined , [ ] , [ ] ] ; } param address param version returns export function getAddressDetailsType ( address , version ) { if ( address . paymentNetwork === 'ACH' ) { if ( version === '1.0' ) { return AddressDetailsType . AchAddress ; } return AddressDetailsType . FiatAddress ; } return AddressDetailsType . CryptoAddress ; }