1
- import { Ability , Discardable , Initialisable , Interaction , Question , QuestionAdapter } from "@serenity-js/core" ;
2
- import SDK from "@atala/prism-wallet-sdk" ;
3
- import { Message } from "@atala/prism-wallet-sdk/build/typings/domain" ;
4
- import axios from "axios" ;
5
- import { CloudAgentConfiguration } from "../configuration/CloudAgentConfiguration" ;
6
- import { Utils } from "../Utils" ;
7
- import { InMemoryStore } from "../configuration/InMemoryStore" ;
1
+ import { Ability , Discardable , Initialisable , Interaction , Question , QuestionAdapter } from "@serenity-js/core"
2
+ import SDK from "@atala/prism-wallet-sdk"
3
+ import { Message } from "@atala/prism-wallet-sdk/build/typings/domain"
4
+ import axios from "axios"
5
+ import { CloudAgentConfiguration } from "../configuration/CloudAgentConfiguration"
6
+ import { Utils } from "../Utils"
7
+ import { InMemoryStore } from "../configuration/InMemoryStore"
8
8
9
- const { Agent, Apollo, Domain, ListenerKey, } = SDK ;
9
+ const { Agent, Apollo, Domain, ListenerKey, } = SDK
10
10
11
11
export class WalletSdk extends Ability implements Initialisable , Discardable {
12
- sdk ! : SDK . Agent ;
13
- messages : MessageQueue = new MessageQueue ( ) ;
12
+ sdk ! : SDK . Agent
13
+ messages : MessageQueue = new MessageQueue ( )
14
14
15
15
static async withANewInstance ( ) : Promise < Ability > {
16
16
const instance : SDK . Agent = await Utils . retry ( 2 , async ( ) => {
17
- return await WalletSdkBuilder . createInstance ( ) ;
18
- } ) ;
19
- return new WalletSdk ( instance ) ;
17
+ return await WalletSdkBuilder . createInstance ( )
18
+ } )
19
+ return new WalletSdk ( instance )
20
20
}
21
21
22
22
constructor ( sdk : SDK . Agent ) {
23
- super ( ) ;
24
- this . sdk = sdk ;
23
+ super ( )
24
+ this . sdk = sdk
25
25
}
26
26
27
27
static credentialOfferStackSize ( ) : QuestionAdapter < number > {
28
28
return Question . about ( "credential offer stack" , actor => {
29
- return WalletSdk . as ( actor ) . messages . credentialOfferStack . length ;
30
- } ) ;
29
+ return WalletSdk . as ( actor ) . messages . credentialOfferStack . length
30
+ } )
31
31
}
32
32
33
33
static issuedCredentialStackSize ( ) : QuestionAdapter < number > {
34
34
return Question . about ( "issued credential stack" , actor => {
35
- return WalletSdk . as ( actor ) . messages . issuedCredentialStack . length ;
36
- } ) ;
35
+ return WalletSdk . as ( actor ) . messages . issuedCredentialStack . length
36
+ } )
37
37
}
38
38
39
39
static proofOfRequestStackSize ( ) : QuestionAdapter < number > {
40
40
return Question . about ( "proof of request stack" , actor => {
41
- return WalletSdk . as ( actor ) . messages . proofRequestStack . length ;
42
- } ) ;
41
+ return WalletSdk . as ( actor ) . messages . proofRequestStack . length
42
+ } )
43
43
}
44
44
45
45
static execute ( callback : ( sdk : SDK . Agent , messages : {
@@ -52,106 +52,106 @@ export class WalletSdk extends Ability implements Initialisable, Discardable {
52
52
credentialOfferStack : WalletSdk . as ( actor ) . messages . credentialOfferStack ,
53
53
issuedCredentialStack : WalletSdk . as ( actor ) . messages . issuedCredentialStack ,
54
54
proofRequestStack : WalletSdk . as ( actor ) . messages . proofRequestStack
55
- } ) ;
56
- } ) ;
55
+ } )
56
+ } )
57
57
}
58
58
59
59
async discard ( ) : Promise < void > {
60
- await this . sdk . stop ( ) ;
60
+ await this . sdk . stop ( )
61
61
}
62
62
63
63
async initialise ( ) : Promise < void > {
64
64
this . sdk . addListener (
65
65
ListenerKey . MESSAGE , ( messages : SDK . Domain . Message [ ] ) => {
66
66
for ( const message of messages ) {
67
- this . messages . enqueue ( message ) ;
67
+ this . messages . enqueue ( message )
68
68
}
69
69
}
70
- ) ;
70
+ )
71
71
72
- await this . sdk . start ( ) ;
72
+ await this . sdk . start ( )
73
73
}
74
74
75
75
isInitialised ( ) : boolean {
76
- return this . sdk . state != "stopped" ;
76
+ return this . sdk . state != "stopped"
77
77
}
78
78
}
79
79
80
80
class WalletSdkBuilder {
81
81
private static async getMediatorDidThroughOob ( ) : Promise < string > {
82
- const response = await axios . get ( CloudAgentConfiguration . mediatorOobUrl ) ;
83
- const encodedData = response . data . split ( "?_oob=" ) [ 1 ] ;
84
- const oobData = JSON . parse ( Buffer . from ( encodedData , "base64" ) . toString ( ) ) ;
85
- return oobData . from ;
82
+ const response = await axios . get ( CloudAgentConfiguration . mediatorOobUrl )
83
+ const encodedData = response . data . split ( "?_oob=" ) [ 1 ]
84
+ const oobData = JSON . parse ( Buffer . from ( encodedData , "base64" ) . toString ( ) )
85
+ return oobData . from
86
86
}
87
87
88
88
static async createInstance ( ) {
89
- const apollo = new Apollo ( ) ;
90
- const store = new InMemoryStore ( ) ;
91
- const pluto = new SDK . Pluto ( store , apollo ) ;
92
- const mediatorDID = Domain . DID . fromString ( await WalletSdkBuilder . getMediatorDidThroughOob ( ) ) ;
89
+ const apollo = new Apollo ( )
90
+ const store = new InMemoryStore ( )
91
+ const pluto = new SDK . Pluto ( store , apollo )
92
+ const mediatorDID = Domain . DID . fromString ( await WalletSdkBuilder . getMediatorDidThroughOob ( ) )
93
93
94
- return Agent . initialize ( { apollo, pluto, mediatorDID } ) ;
94
+ return Agent . initialize ( { apollo, pluto, mediatorDID } )
95
95
}
96
96
}
97
97
98
98
/**
99
99
* Helper class for message queueing processor
100
100
*/
101
101
class MessageQueue {
102
- private processingId : NodeJS . Timeout | null = null ;
103
- private queue : Message [ ] = [ ] ;
102
+ private processingId : NodeJS . Timeout | null = null
103
+ private queue : Message [ ] = [ ]
104
104
105
- credentialOfferStack : Message [ ] = [ ] ;
106
- proofRequestStack : Message [ ] = [ ] ;
107
- issuedCredentialStack : Message [ ] = [ ] ;
108
- receivedMessages : string [ ] = [ ] ;
105
+ credentialOfferStack : Message [ ] = [ ]
106
+ proofRequestStack : Message [ ] = [ ]
107
+ issuedCredentialStack : Message [ ] = [ ]
108
+ receivedMessages : string [ ] = [ ]
109
109
110
110
enqueue ( message : Message ) {
111
- this . queue . push ( message ) ;
111
+ this . queue . push ( message )
112
112
113
113
// auto start processing messages
114
114
if ( ! this . processingId ) {
115
- this . processMessages ( ) ;
115
+ this . processMessages ( )
116
116
}
117
117
}
118
118
119
119
dequeue ( ) : Message {
120
- return this . queue . shift ( ) ! ;
120
+ return this . queue . shift ( ) !
121
121
}
122
122
123
123
// Check if the queue is empty
124
124
isEmpty ( ) : boolean {
125
- return this . queue . length === 0 ;
125
+ return this . queue . length === 0
126
126
}
127
127
128
128
// Get the number of messages in the queue
129
129
size ( ) : number {
130
- return this . queue . length ;
130
+ return this . queue . length
131
131
}
132
132
133
133
processMessages ( ) {
134
134
this . processingId = setInterval ( ( ) => {
135
135
if ( ! this . isEmpty ( ) ) {
136
- const message : Message = this . dequeue ( ) ;
136
+ const message : Message = this . dequeue ( )
137
137
// checks if sdk already received message
138
138
if ( this . receivedMessages . includes ( message . id ) ) {
139
- return ;
139
+ return
140
140
}
141
141
142
- this . receivedMessages . push ( message . id ) ;
142
+ this . receivedMessages . push ( message . id )
143
143
144
144
if ( message . piuri . includes ( "/offer-credential" ) ) {
145
- this . credentialOfferStack . push ( message ) ;
145
+ this . credentialOfferStack . push ( message )
146
146
} else if ( message . piuri . includes ( "/present-proof" ) ) {
147
- this . proofRequestStack . push ( message ) ;
147
+ this . proofRequestStack . push ( message )
148
148
} else if ( message . piuri . includes ( "/issue-credential" ) ) {
149
- this . issuedCredentialStack . push ( message ) ;
149
+ this . issuedCredentialStack . push ( message )
150
150
}
151
151
} else {
152
- clearInterval ( this . processingId ! ) ;
153
- this . processingId = null ;
152
+ clearInterval ( this . processingId ! )
153
+ this . processingId = null
154
154
}
155
- } , 50 ) ;
155
+ } , 50 )
156
156
}
157
157
}
0 commit comments