@@ -57,6 +57,8 @@ import type { TPublicKeyEd25519 } from '@cheqd/did-provider-cheqd';
5757import { toTPublicKeyEd25519 } from '../helpers.js' ;
5858import type { APIServiceOptions } from '../../types/admin.js' ;
5959import { SupportedKeyTypes } from '@veramo/utils' ;
60+ import { PaymentAccountEntity } from '../../database/entities/payment.account.entity.js' ;
61+ import { LocalStore } from '../../database/cache/store.js' ;
6062
6163dotenv . config ( ) ;
6264
@@ -89,36 +91,32 @@ export class PostgresIdentityService extends DefaultIdentityService {
8991
9092 async createCheqdProvider (
9193 customer : CustomerEntity ,
92- namespace : CheqdNetwork
94+ namespace : CheqdNetwork ,
95+ paymentAccounts : PaymentAccountEntity [ ]
9396 ) : Promise < CheqdDIDProvider | undefined > {
9497 let rpcUrl = '' ;
9598 if ( namespace === CheqdNetwork . Mainnet ) {
9699 rpcUrl = MAINNET_RPC_URL || DefaultRPCUrls . mainnet ;
97100 } else {
98101 rpcUrl = TESTNET_RPC_URL || DefaultRPCUrls . testnet ;
99102 }
100- const paymentAccount = await PaymentAccountService . instance . find ( {
101- namespace : namespace ,
102- customer : customer ,
103- } ) ;
104- if ( paymentAccount . length > 1 ) {
105- throw new Error ( `More than one payment account for ${ namespace } found` ) ;
103+ const paymentAccount = paymentAccounts . find ( ( acc ) => acc . namespace === namespace ) ;
104+ if ( paymentAccount === undefined ) {
105+ return undefined ;
106106 }
107- if ( paymentAccount . length === 1 ) {
108- const privateKey = ( await this . getPrivateKey ( paymentAccount [ 0 ] . key . kid ) ) ?. privateKeyHex ;
109107
110- if ( ! privateKey ) {
111- throw new Error ( `No keys is initialized` ) ;
112- }
108+ const privateKey = ( await this . getPrivateKey ( paymentAccount . key . kid ) ) ?. privateKeyHex ;
113109
114- return new CheqdDIDProvider ( {
115- defaultKms : 'postgres' ,
116- cosmosPayerSeed : privateKey ,
117- networkType : namespace ,
118- rpcUrl : rpcUrl ,
119- } ) ;
110+ if ( ! privateKey ) {
111+ throw new Error ( `No keys is initialized` ) ;
120112 }
121- return undefined ;
113+
114+ return new CheqdDIDProvider ( {
115+ defaultKms : 'postgres' ,
116+ cosmosPayerSeed : privateKey ,
117+ networkType : namespace ,
118+ rpcUrl : rpcUrl ,
119+ } ) ;
122120 }
123121
124122 async createAgent ( customer : CustomerEntity ) : Promise < VeramoAgent > {
@@ -133,10 +131,22 @@ export class PostgresIdentityService extends DefaultIdentityService {
133131 }
134132 const dbConnection = Connection . instance . dbConnection ;
135133
134+ const cachedAccounts = LocalStore . instance . getCustomerAccounts ( customer . customerId ) ;
135+ let paymentAccounts : PaymentAccountEntity [ ] ;
136+ if ( cachedAccounts ?. length == 2 ) {
137+ paymentAccounts = cachedAccounts ;
138+ } else {
139+ paymentAccounts = await PaymentAccountService . instance . find ( { customer } , [ 'key' ] ) ;
140+
141+ if ( paymentAccounts . length > 0 ) {
142+ LocalStore . instance . setCustomerAccounts ( customer . customerId , paymentAccounts ) ;
143+ }
144+ }
145+
136146 // One customer may / may not have one Mainnet paymentAccount
137- const providerMainnet = await this . createCheqdProvider ( customer , CheqdNetwork . Mainnet ) ;
147+ const providerMainnet = await this . createCheqdProvider ( customer , CheqdNetwork . Mainnet , paymentAccounts ) ;
138148 // One customer may / may not have one Testnet paymentAccount
139- const providerTestnet = await this . createCheqdProvider ( customer , CheqdNetwork . Testnet ) ;
149+ const providerTestnet = await this . createCheqdProvider ( customer , CheqdNetwork . Testnet , paymentAccounts ) ;
140150 // did:key provider
141151 providers [ 'did:key' ] = new KeyDIDProvider ( { defaultKms : 'postgres' } ) ;
142152 if ( providerMainnet ) {
0 commit comments