@@ -9,36 +9,25 @@ export type Status = {
9
9
signers : { [ signer : string ] : SignerStatus }
10
10
}
11
11
12
- export type SignerStatusPending = {
13
- situation ?: string
12
+ export enum SignerState {
13
+ INITIAL ,
14
+ SIGNING ,
15
+ SIGNED ,
16
+ ERROR
14
17
}
15
18
16
- export type SignerStatusRejected = {
17
- rejected : true
18
- error ?: string
19
- }
20
-
21
- export type SignerStatusSigned = {
22
- signature : ethers . BytesLike
23
- suffix : ethers . BytesLike
24
- }
25
-
26
- export type SignerStatus = SignerStatusPending | SignerStatusRejected | SignerStatusSigned
27
-
28
- export function isSignerStatusRejected ( status : SignerStatus ) : status is SignerStatusRejected {
29
- return ( status as SignerStatusRejected ) . rejected
30
- }
19
+ export type SignerStatus =
20
+ | { state : SignerState . INITIAL }
21
+ | { state : SignerState . SIGNING ; request : Promise < ethers . BytesLike > }
22
+ | { state : SignerState . SIGNED ; signature : ethers . BytesLike ; suffix : ethers . BytesLike }
23
+ | { state : SignerState . ERROR ; error : any }
31
24
32
- export function isSignerStatusSigned ( status : SignerStatus ) : status is SignerStatusSigned {
33
- return ( status as SignerStatusSigned ) . signature !== undefined
25
+ export function isSignerStatusPending (
26
+ status ?: SignerStatus
27
+ ) : status is undefined | { state : SignerState . INITIAL } | { state : SignerState . SIGNING ; request : Promise < ethers . BytesLike > } {
28
+ return status === undefined || status . state === SignerState . INITIAL || status . state === SignerState . SIGNING
34
29
}
35
30
36
- export function isSignerStatusPending ( status : SignerStatus ) : status is SignerStatusPending {
37
- return ! isSignerStatusRejected ( status ) && ! isSignerStatusSigned ( status )
38
- }
39
-
40
- export const InitialSituation = 'Initial'
41
-
42
31
export interface SignatureOrchestrator {
43
32
getSigners ( ) : Promise < string [ ] >
44
33
@@ -189,17 +178,22 @@ export class Orchestrator {
189
178
const accepted = await Promise . allSettled (
190
179
signers . map ( async s => {
191
180
const saddr = await s . getAddress ( )
192
- status . signers [ saddr ] = { situation : InitialSituation }
193
- try {
194
- const signature = await s . sign ( message , metadata ?? { } )
195
- const suffix = s . suffix ( )
196
- status . signers [ saddr ] = { signature, suffix }
197
- onStatusUpdate ( )
198
- return true
199
- } catch ( error ) {
200
- status . signers [ saddr ] = { rejected : true , error }
201
- onStatusUpdate ( )
202
- return false
181
+
182
+ status . signers [ saddr ] = {
183
+ state : SignerState . SIGNING ,
184
+ request : s
185
+ . sign ( message , metadata ?? { } )
186
+ . then ( signature => {
187
+ const suffix = s . suffix ( )
188
+ status . signers [ saddr ] = { state : SignerState . SIGNED , signature, suffix }
189
+ onStatusUpdate ( )
190
+ return signature
191
+ } )
192
+ . catch ( error => {
193
+ status . signers [ saddr ] = { state : SignerState . ERROR , error }
194
+ onStatusUpdate ( )
195
+ throw error
196
+ } )
203
197
}
204
198
} )
205
199
)
@@ -211,11 +205,10 @@ export class Orchestrator {
211
205
if ( promise . status === 'rejected' ) {
212
206
const address = await signer . getAddress ( )
213
207
console . warn ( `signer ${ address } rejected the request: ${ promise . reason } ` )
214
- status . signers [ address ] = { rejected : true , error : `signer ${ address } rejected the request: ${ promise . reason } ` }
215
- } else if ( ! promise . value ) {
216
- const address = await signer . getAddress ( )
217
- console . warn ( `signer ${ address } rejected the request` )
218
- status . signers [ address ] = { rejected : true , error : `signer ${ address } rejected the request` }
208
+ status . signers [ address ] = {
209
+ state : SignerState . ERROR ,
210
+ error : new Error ( `signer ${ address } rejected the request: ${ promise . reason } ` )
211
+ }
219
212
}
220
213
}
221
214
0 commit comments