@@ -13,12 +13,8 @@ use super::{
13
13
Action , IdentityUpdate , MemberIdentifier , Signature , SignatureError ,
14
14
} ;
15
15
16
- #[ derive( Error , Debug ) ]
17
- pub enum IdentityBuilderError {
18
- #[ error( "Missing signer" ) ]
19
- MissingSigner ,
20
- }
21
-
16
+ /// The SignatureField is used to map the signatures from a [SignatureRequest] back to the correct
17
+ /// field in an [IdentityUpdate]. It is used in the `pending_signatures` map in a [PendingIdentityAction]
22
18
#[ derive( Clone , PartialEq , Hash , Eq ) ]
23
19
enum SignatureField {
24
20
InitialAddress ,
@@ -33,13 +29,17 @@ pub struct PendingIdentityAction {
33
29
pending_signatures : HashMap < SignatureField , MemberIdentifier > ,
34
30
}
35
31
36
- pub struct IdentityUpdateBuilder {
32
+ /// The SignatureRequestBuilder is used to collect all of the actions in
33
+ /// an IdentityUpdate, but without the signatures.
34
+ /// It outputs a SignatureRequest, which can then collect the relevant signatures and be turned into
35
+ /// an IdentityUpdate.
36
+ pub struct SignatureRequestBuilder {
37
37
inbox_id : String ,
38
38
client_timestamp_ns : u64 ,
39
39
actions : Vec < PendingIdentityAction > ,
40
40
}
41
41
42
- impl IdentityUpdateBuilder {
42
+ impl SignatureRequestBuilder {
43
43
/// Create a new IdentityUpdateBuilder for the given `inbox_id`
44
44
pub fn new ( inbox_id : String ) -> Self {
45
45
Self {
@@ -76,7 +76,6 @@ impl IdentityUpdateBuilder {
76
76
self . actions . push ( PendingIdentityAction {
77
77
unsigned_action : UnsignedAction :: AddAssociation ( UnsignedAddAssociation {
78
78
new_member_identifier : new_member_identifier. clone ( ) ,
79
- inbox_id : self . inbox_id . clone ( ) ,
80
79
} ) ,
81
80
pending_signatures : HashMap :: from ( [
82
81
(
@@ -101,7 +100,6 @@ impl IdentityUpdateBuilder {
101
100
recovery_address_identifier. clone ( ) ,
102
101
) ] ) ,
103
102
unsigned_action : UnsignedAction :: RevokeAssociation ( UnsignedRevokeAssociation {
104
- inbox_id : self . inbox_id . clone ( ) ,
105
103
revoked_member,
106
104
} ) ,
107
105
} ) ;
@@ -120,24 +118,32 @@ impl IdentityUpdateBuilder {
120
118
recovery_address_identifier. clone ( ) ,
121
119
) ] ) ,
122
120
unsigned_action : UnsignedAction :: ChangeRecoveryAddress ( UnsignedChangeRecoveryAddress {
123
- inbox_id : self . inbox_id . clone ( ) ,
124
121
new_recovery_address,
125
122
} ) ,
126
123
} ) ;
127
124
128
125
self
129
126
}
130
127
131
- pub fn to_signature_request ( self ) -> SignatureRequest {
128
+ pub fn build ( self ) -> SignatureRequest {
132
129
let unsigned_actions: Vec < UnsignedAction > = self
133
130
. actions
134
131
. iter ( )
135
132
. map ( |pending_action| pending_action. unsigned_action . clone ( ) )
136
133
. collect ( ) ;
137
134
138
- let signature_text = get_signature_text ( unsigned_actions, self . client_timestamp_ns ) ;
135
+ let signature_text = get_signature_text (
136
+ unsigned_actions,
137
+ self . inbox_id . clone ( ) ,
138
+ self . client_timestamp_ns ,
139
+ ) ;
139
140
140
- SignatureRequest :: new ( self . actions , signature_text, self . client_timestamp_ns )
141
+ SignatureRequest :: new (
142
+ self . actions ,
143
+ signature_text,
144
+ self . inbox_id ,
145
+ self . client_timestamp_ns ,
146
+ )
141
147
}
142
148
}
143
149
@@ -161,15 +167,18 @@ pub struct SignatureRequest {
161
167
signature_text : String ,
162
168
signatures : HashMap < MemberIdentifier , Box < dyn Signature > > ,
163
169
client_timestamp_ns : u64 ,
170
+ inbox_id : String ,
164
171
}
165
172
166
173
impl SignatureRequest {
167
174
pub fn new (
168
175
pending_actions : Vec < PendingIdentityAction > ,
169
176
signature_text : String ,
177
+ inbox_id : String ,
170
178
client_timestamp_ns : u64 ,
171
179
) -> Self {
172
180
Self {
181
+ inbox_id,
173
182
pending_actions,
174
183
signature_text,
175
184
signatures : HashMap :: new ( ) ,
@@ -220,7 +229,7 @@ impl SignatureRequest {
220
229
self . signature_text . clone ( )
221
230
}
222
231
223
- pub fn build_identity_update ( & self ) -> Result < IdentityUpdate , SignatureRequestError > {
232
+ pub fn build_identity_update ( self ) -> Result < IdentityUpdate , SignatureRequestError > {
224
233
if !self . is_ready ( ) {
225
234
return Err ( SignatureRequestError :: MissingSigner ) ;
226
235
}
@@ -232,7 +241,11 @@ impl SignatureRequest {
232
241
. map ( |pending_action| build_action ( pending_action, & self . signatures ) )
233
242
. collect :: < Result < Vec < Action > , SignatureRequestError > > ( ) ?;
234
243
235
- Ok ( IdentityUpdate :: new ( actions, self . client_timestamp_ns ) )
244
+ Ok ( IdentityUpdate :: new (
245
+ actions,
246
+ self . inbox_id ,
247
+ self . client_timestamp_ns ,
248
+ ) )
236
249
}
237
250
}
238
251
@@ -317,10 +330,15 @@ fn build_action(
317
330
}
318
331
}
319
332
320
- fn get_signature_text ( actions : Vec < UnsignedAction > , client_timestamp_ns : u64 ) -> String {
333
+ fn get_signature_text (
334
+ actions : Vec < UnsignedAction > ,
335
+ inbox_id : String ,
336
+ client_timestamp_ns : u64 ,
337
+ ) -> String {
321
338
let identity_update = UnsignedIdentityUpdate {
322
339
client_timestamp_ns,
323
340
actions,
341
+ inbox_id,
324
342
} ;
325
343
326
344
identity_update. signature_text ( )
@@ -337,7 +355,7 @@ mod tests {
337
355
338
356
use super :: * ;
339
357
340
- // Helper function to add all the missing signatures
358
+ // Helper function to add all the missing signatures, since we don't have real signers available
341
359
fn add_missing_signatures_to_request ( signature_request : & mut SignatureRequest ) {
342
360
let missing_signatures = signature_request. missing_signatures ( ) ;
343
361
for member_identifier in missing_signatures {
@@ -362,9 +380,9 @@ mod tests {
362
380
let account_address = "account_address" . to_string ( ) ;
363
381
let nonce = 0 ;
364
382
let inbox_id = generate_inbox_id ( & account_address, & nonce) ;
365
- let mut signature_request = IdentityUpdateBuilder :: new ( inbox_id)
383
+ let mut signature_request = SignatureRequestBuilder :: new ( inbox_id)
366
384
. create_inbox ( account_address. into ( ) , nonce)
367
- . to_signature_request ( ) ;
385
+ . build ( ) ;
368
386
369
387
add_missing_signatures_to_request ( & mut signature_request) ;
370
388
@@ -383,10 +401,10 @@ mod tests {
383
401
let existing_member_identifier: MemberIdentifier = account_address. into ( ) ;
384
402
let new_member_identifier: MemberIdentifier = rand_vec ( ) . into ( ) ;
385
403
386
- let mut signature_request = IdentityUpdateBuilder :: new ( inbox_id)
404
+ let mut signature_request = SignatureRequestBuilder :: new ( inbox_id)
387
405
. create_inbox ( existing_member_identifier. clone ( ) , nonce)
388
406
. add_association ( new_member_identifier, existing_member_identifier)
389
- . to_signature_request ( ) ;
407
+ . build ( ) ;
390
408
391
409
add_missing_signatures_to_request ( & mut signature_request) ;
392
410
@@ -405,10 +423,10 @@ mod tests {
405
423
let inbox_id = generate_inbox_id ( & account_address, & nonce) ;
406
424
let existing_member_identifier: MemberIdentifier = account_address. clone ( ) . into ( ) ;
407
425
408
- let mut signature_request = IdentityUpdateBuilder :: new ( inbox_id)
426
+ let mut signature_request = SignatureRequestBuilder :: new ( inbox_id)
409
427
. create_inbox ( existing_member_identifier. clone ( ) , nonce)
410
428
. revoke_association ( existing_member_identifier. clone ( ) , account_address. into ( ) )
411
- . to_signature_request ( ) ;
429
+ . build ( ) ;
412
430
413
431
add_missing_signatures_to_request ( & mut signature_request) ;
414
432
@@ -426,9 +444,9 @@ mod tests {
426
444
let account_address = "account_address" . to_string ( ) ;
427
445
let nonce = 0 ;
428
446
let inbox_id = generate_inbox_id ( & account_address, & nonce) ;
429
- let mut signature_request = IdentityUpdateBuilder :: new ( inbox_id)
447
+ let mut signature_request = SignatureRequestBuilder :: new ( inbox_id)
430
448
. create_inbox ( account_address. into ( ) , nonce)
431
- . to_signature_request ( ) ;
449
+ . build ( ) ;
432
450
433
451
let attempt_to_add_random_member = signature_request. add_signature (
434
452
MockSignature :: new_boxed ( true , rand_string ( ) . into ( ) , SignatureKind :: Erc191 , None ) ,
0 commit comments