@@ -19,53 +19,48 @@ class RootStoreAddressesPostHandler {
19
19
return
20
20
}
21
21
22
- let promises = [ ]
23
- let resultObj = { }
24
- const getRootStoreFromId = async id => {
25
- const did = await this . getDID ( id )
26
- if ( did ) {
27
- const rootStoreAddress = await this . getRootStore ( did )
28
- if ( rootStoreAddress ) {
29
- resultObj [ id ] = rootStoreAddress
30
- }
22
+ const idDidMap = await this . getDIDs ( body . identities )
23
+ const didRootStoreMap = await this . getRootStores ( Object . values ( idDidMap ) )
24
+
25
+ const rootStoreAddresses = Object . keys ( idDidMap ) . reduce ( ( acc , id ) => {
26
+ if ( didRootStoreMap [ idDidMap [ id ] ] ) {
27
+ acc [ id ] = didRootStoreMap [ idDidMap [ id ] ]
31
28
}
32
- }
33
- body . identities . forEach ( id => {
34
- promises . push ( getRootStoreFromId ( id ) )
35
- } )
36
- await Promise . all ( promises )
37
- cb ( null , { rootStoreAddresses : resultObj } )
29
+ return acc
30
+ } , { } )
31
+ cb ( null , { rootStoreAddresses } )
38
32
}
39
33
40
- async getRootStore ( did ) {
41
- // Get rsAddress for did from db
42
- const rsAddress = await this . addressMgr . get ( did )
43
- if ( ! rsAddress ) {
44
- return null
45
- } else {
46
- return rsAddress . root_store_address
34
+ async getRootStores ( dids ) {
35
+ if ( ! dids || ! dids . length ) {
36
+ return { }
47
37
}
38
+ // Get rsAddress for did from db
39
+ const rsAddressRows = await this . addressMgr . getMultiple ( dids )
40
+ return rsAddressRows . reduce ( ( acc , row ) => {
41
+ acc [ row . did ] = row . root_store_address
42
+ return acc
43
+ } , { } )
48
44
}
49
45
50
- async getDID ( id ) {
51
- // Check if id is an address or a did
52
- let did
53
- if ( id . startsWith ( '0x' ) ) {
54
- const { error } = hexString . validate ( id )
55
- if ( error ) {
56
- return null
57
- }
58
- const didRow = await this . linkMgr . get ( id )
59
- if ( ! didRow ) {
60
- return null
61
- }
62
- did = didRow . did
63
- } else if ( id . startsWith ( 'did:' ) ) {
64
- did = id
65
- } else {
66
- return null
46
+ async getDIDs ( ids ) {
47
+ if ( ! ids || ! ids . length ) {
48
+ return { }
67
49
}
68
- return did
50
+ const { dids, addresses } = ids . reduce ( ( acc , id ) => {
51
+ if ( id . startsWith ( '0x' ) && ! hexString . validate ( id ) . error ) {
52
+ acc . addresses . push ( id )
53
+ } else if ( id . startsWith ( 'did:' ) ) {
54
+ acc . dids . push ( id )
55
+ }
56
+ return acc
57
+ } , { dids : [ ] , addresses : [ ] } )
58
+ const didRows = addresses . length == 0 ? [ ] : await this . linkMgr . getMultiple ( addresses )
59
+
60
+ const results = { }
61
+ didRows . forEach ( row => results [ row . address ] = row . did )
62
+ dids . forEach ( did => results [ did ] = did )
63
+ return results
69
64
}
70
65
}
71
66
module . exports = RootStoreAddressesPostHandler
0 commit comments