20
20
// limitations under the License.
21
21
22
22
use std:: cmp:: Ordering ;
23
- use std:: vec;
24
23
25
24
use amplify:: ByteArray ;
26
25
use bp:: dbc:: opret:: OpretProof ;
27
26
use bp:: dbc:: tapret:: TapretProof ;
28
27
use bp:: dbc:: { anchor, Anchor } ;
29
28
use bp:: { Tx , Txid } ;
30
- use commit_verify:: { mpc, CommitId } ;
31
- use rgb:: validation:: { DbcProof , EAnchor } ;
32
- use rgb:: {
33
- BundleDisclosure , BundleId , ContractId , DiscloseHash , Operation , Transition , TransitionBundle ,
34
- XChain , XWitnessId ,
35
- } ;
29
+ use commit_verify:: mpc;
30
+ use rgb:: validation:: DbcProof ;
31
+ use rgb:: { BundleId , DiscloseHash , TransitionBundle , XChain , XWitnessId } ;
36
32
use strict_encoding:: StrictDumb ;
37
33
38
- use crate :: { BundleExt , MergeReveal , MergeRevealError , RevealError , LIB_NAME_RGB_STD } ;
34
+ use crate :: { MergeReveal , MergeRevealError , LIB_NAME_RGB_STD } ;
39
35
40
36
#[ derive( Clone , Eq , PartialEq , Debug ) ]
41
37
#[ derive( StrictType , StrictDumb , StrictEncode , StrictDecode ) ]
@@ -153,34 +149,6 @@ impl PubWitness {
153
149
}
154
150
}
155
151
156
- #[ derive( Clone , Eq , PartialEq , Debug ) ]
157
- #[ derive( StrictType , StrictDumb , StrictEncode , StrictDecode ) ]
158
- #[ strict_type( lib = LIB_NAME_RGB_STD ) ]
159
- pub ( crate ) struct AnchoredBundleDisclosure {
160
- pub anchor : EAnchor ,
161
- pub bundle : BundleDisclosure ,
162
- }
163
-
164
- impl AnchoredBundleDisclosure {
165
- pub fn new ( anchor : EAnchor , bundle : & TransitionBundle ) -> Self {
166
- Self {
167
- anchor,
168
- bundle : bundle. disclose ( ) ,
169
- }
170
- }
171
- }
172
-
173
- #[ derive( Clone , Eq , PartialEq , Debug ) ]
174
- #[ derive( StrictType , StrictDumb , StrictEncode , StrictDecode ) ]
175
- #[ strict_type( lib = LIB_NAME_RGB_STD ) ]
176
- #[ derive( CommitEncode ) ]
177
- #[ commit_encode( strategy = strict, id = DiscloseHash ) ]
178
- pub ( crate ) struct BundledWitnessDisclosure {
179
- pub pub_witness : XPubWitness ,
180
- pub first : AnchoredBundleDisclosure ,
181
- pub second : Option < AnchoredBundleDisclosure > ,
182
- }
183
-
184
152
#[ derive( Clone , Eq , Debug ) ]
185
153
#[ derive( StrictType , StrictDumb , StrictEncode , StrictDecode ) ]
186
154
#[ strict_type( lib = LIB_NAME_RGB_STD ) ]
@@ -189,50 +157,37 @@ pub(crate) struct BundledWitnessDisclosure {
189
157
derive( Serialize , Deserialize ) ,
190
158
serde( crate = "serde_crate" , rename_all = "camelCase" )
191
159
) ]
192
- pub struct BundledWitness < P : mpc:: Proof + StrictDumb = mpc:: MerkleProof > {
160
+ #[ derive( CommitEncode ) ]
161
+ #[ commit_encode( strategy = strict, id = DiscloseHash ) ]
162
+ pub struct WitnessBundle < P : mpc:: Proof + StrictDumb = mpc:: MerkleProof > {
193
163
pub pub_witness : XPubWitness ,
194
- pub anchored_bundles : AnchoredBundles < P > ,
164
+ pub anchor : Anchor < P , DbcProof > ,
165
+ pub bundle : TransitionBundle ,
195
166
}
196
167
197
- impl < P : mpc:: Proof + StrictDumb > PartialEq for BundledWitness < P > {
168
+ impl < P : mpc:: Proof + StrictDumb > PartialEq for WitnessBundle < P > {
198
169
fn eq ( & self , other : & Self ) -> bool { self . pub_witness == other. pub_witness }
199
170
}
200
171
201
- impl < P : mpc:: Proof + StrictDumb > Ord for BundledWitness < P > {
172
+ impl < P : mpc:: Proof + StrictDumb > Ord for WitnessBundle < P > {
202
173
fn cmp ( & self , other : & Self ) -> Ordering { self . pub_witness . cmp ( & other. pub_witness ) }
203
174
}
204
175
205
- impl < P : mpc:: Proof + StrictDumb > PartialOrd for BundledWitness < P > {
176
+ impl < P : mpc:: Proof + StrictDumb > PartialOrd for WitnessBundle < P > {
206
177
fn partial_cmp ( & self , other : & Self ) -> Option < Ordering > { Some ( self . cmp ( other) ) }
207
178
}
208
179
209
- impl < P : mpc:: Proof + StrictDumb > BundledWitness < P > {
210
- pub fn bundles ( & self ) -> vec:: IntoIter < & TransitionBundle > { self . anchored_bundles . bundles ( ) }
211
- }
212
-
213
- impl BundledWitness < mpc:: MerkleProof > {
180
+ impl WitnessBundle < mpc:: MerkleProof > {
214
181
pub fn witness_id ( & self ) -> XWitnessId { self . pub_witness . to_witness_id ( ) }
215
-
216
- pub ( crate ) fn disclose ( & self ) -> BundledWitnessDisclosure {
217
- let mut pairs = self . anchored_bundles . pairs ( ) ;
218
- let ( a1, b1) = pairs. next ( ) . expect ( "there always at least one bundle" ) ;
219
- let second = pairs
220
- . next ( )
221
- . map ( |( a, b) | AnchoredBundleDisclosure :: new ( a, b) ) ;
222
- BundledWitnessDisclosure {
223
- pub_witness : self . pub_witness . clone ( ) ,
224
- first : AnchoredBundleDisclosure :: new ( a1, b1) ,
225
- second,
226
- }
227
- }
228
-
229
- pub fn disclose_hash ( & self ) -> DiscloseHash { self . disclose ( ) . commit_id ( ) }
230
182
}
231
183
232
- impl BundledWitness {
184
+ impl WitnessBundle {
233
185
pub fn merge_reveal ( mut self , other : Self ) -> Result < Self , MergeRevealError > {
234
186
self . pub_witness = self . pub_witness . merge_reveal ( other. pub_witness ) ?;
235
- self . anchored_bundles = self . anchored_bundles . merge_reveal ( other. anchored_bundles ) ?;
187
+ if self . anchor != other. anchor {
188
+ return Err ( MergeRevealError :: AnchorsNonEqual ( self . bundle . bundle_id ( ) ) ) ;
189
+ }
190
+ self . bundle = self . bundle . merge_reveal ( other. bundle ) ?;
236
191
Ok ( self )
237
192
}
238
193
}
@@ -311,247 +266,3 @@ impl AnchorSet {
311
266
}
312
267
}
313
268
}
314
-
315
- #[ derive( Clone , PartialEq , Eq , Debug ) ]
316
- #[ derive( StrictType , StrictEncode , StrictDecode ) ]
317
- #[ strict_type( lib = LIB_NAME_RGB_STD , tags = custom) ]
318
- #[ cfg_attr(
319
- feature = "serde" ,
320
- derive( Serialize , Deserialize ) ,
321
- serde( crate = "serde_crate" , rename_all = "camelCase" )
322
- ) ]
323
- pub enum AnchoredBundles < P : mpc:: Proof + StrictDumb = mpc:: MerkleProof > {
324
- #[ strict_type( tag = 0x01 ) ]
325
- Tapret ( Anchor < P , TapretProof > , TransitionBundle ) ,
326
- #[ strict_type( tag = 0x02 ) ]
327
- Opret ( Anchor < P , OpretProof > , TransitionBundle ) ,
328
- #[ strict_type( tag = 0x03 ) ]
329
- Double {
330
- tapret_anchor : Anchor < P , TapretProof > ,
331
- tapret_bundle : TransitionBundle ,
332
- opret_anchor : Anchor < P , OpretProof > ,
333
- opret_bundle : TransitionBundle ,
334
- } ,
335
- }
336
-
337
- impl < P : mpc:: Proof + StrictDumb > StrictDumb for AnchoredBundles < P > {
338
- fn strict_dumb ( ) -> Self { Self :: Opret ( strict_dumb ! ( ) , strict_dumb ! ( ) ) }
339
- }
340
-
341
- impl < P : mpc:: Proof + StrictDumb > AnchoredBundles < P > {
342
- pub fn with ( anchor : EAnchor < P > , bundle : TransitionBundle ) -> Self {
343
- match anchor. dbc_proof {
344
- DbcProof :: Tapret ( tapret) => Self :: Tapret ( Anchor :: new ( anchor. mpc_proof , tapret) , bundle) ,
345
- DbcProof :: Opret ( opret) => Self :: Opret ( Anchor :: new ( anchor. mpc_proof , opret) , bundle) ,
346
- }
347
- }
348
-
349
- pub fn has_tapret ( & self ) -> bool { matches ! ( self , Self :: Tapret ( ..) | Self :: Double { .. } ) }
350
-
351
- pub fn has_opret ( & self ) -> bool { matches ! ( self , Self :: Opret ( ..) | Self :: Double { .. } ) }
352
-
353
- pub fn pairs ( & self ) -> vec:: IntoIter < ( EAnchor < P > , & TransitionBundle ) >
354
- where P : Clone {
355
- match self {
356
- AnchoredBundles :: Tapret ( anchor, bundle) => {
357
- let anchor = anchor. clone ( ) ;
358
- vec ! [ ( EAnchor :: new( anchor. mpc_proof, anchor. dbc_proof. into( ) ) , bundle) ]
359
- }
360
- AnchoredBundles :: Opret ( anchor, bundle) => {
361
- let anchor = anchor. clone ( ) ;
362
- vec ! [ ( EAnchor :: new( anchor. mpc_proof, anchor. dbc_proof. into( ) ) , bundle) ]
363
- }
364
- AnchoredBundles :: Double {
365
- tapret_anchor,
366
- tapret_bundle,
367
- opret_anchor,
368
- opret_bundle,
369
- } => {
370
- let tapret_anchor = tapret_anchor. clone ( ) ;
371
- let opret_anchor = opret_anchor. clone ( ) ;
372
- vec ! [
373
- (
374
- EAnchor :: new( tapret_anchor. mpc_proof, tapret_anchor. dbc_proof. into( ) ) ,
375
- tapret_bundle,
376
- ) ,
377
- (
378
- EAnchor :: new( opret_anchor. mpc_proof, opret_anchor. dbc_proof. into( ) ) ,
379
- opret_bundle,
380
- ) ,
381
- ]
382
- }
383
- }
384
- . into_iter ( )
385
- }
386
-
387
- pub fn bundles ( & self ) -> vec:: IntoIter < & TransitionBundle > {
388
- match self {
389
- AnchoredBundles :: Tapret ( _, bundle) | AnchoredBundles :: Opret ( _, bundle) => vec ! [ bundle] ,
390
- AnchoredBundles :: Double {
391
- tapret_bundle,
392
- opret_bundle,
393
- ..
394
- } => vec ! [ tapret_bundle, opret_bundle] ,
395
- }
396
- . into_iter ( )
397
- }
398
-
399
- pub fn bundles_mut ( & mut self ) -> vec:: IntoIter < & mut TransitionBundle > {
400
- match self {
401
- AnchoredBundles :: Tapret ( _, bundle) | AnchoredBundles :: Opret ( _, bundle) => vec ! [ bundle] ,
402
- AnchoredBundles :: Double {
403
- tapret_bundle,
404
- opret_bundle,
405
- ..
406
- } => vec ! [ tapret_bundle, opret_bundle] ,
407
- }
408
- . into_iter ( )
409
- }
410
-
411
- /// Ensures that the transition is revealed inside the anchored bundle.
412
- ///
413
- /// # Returns
414
- ///
415
- /// `true` if the transition was previously concealed; `false` if it was
416
- /// already revealed; error if the transition is unrelated to the bundle.
417
- pub fn reveal_transition ( & mut self , mut transition : Transition ) -> Result < bool , RevealError > {
418
- for bundle in self . bundles_mut ( ) {
419
- match bundle. reveal_transition ( transition) {
420
- Ok ( known) => return Ok ( known) ,
421
- Err ( RevealError :: UnrelatedTransition ( _, t) ) => transition = t,
422
- }
423
- }
424
- Err ( RevealError :: UnrelatedTransition ( transition. id ( ) , transition) )
425
- }
426
- }
427
-
428
- impl AnchoredBundles {
429
- pub fn to_anchor_set (
430
- & self ,
431
- contract_id : ContractId ,
432
- bundle_id : BundleId ,
433
- ) -> Result < AnchorSet , mpc:: InvalidProof > {
434
- let proto = mpc:: ProtocolId :: from_byte_array ( contract_id. to_byte_array ( ) ) ;
435
- let msg = mpc:: Message :: from_byte_array ( bundle_id. to_byte_array ( ) ) ;
436
- match self . clone ( ) {
437
- Self :: Tapret ( anchor, _) => anchor. to_merkle_block ( proto, msg) . map ( AnchorSet :: Tapret ) ,
438
- Self :: Opret ( anchor, _) => anchor. to_merkle_block ( proto, msg) . map ( AnchorSet :: Opret ) ,
439
- Self :: Double {
440
- tapret_anchor,
441
- opret_anchor,
442
- ..
443
- } => Ok ( AnchorSet :: Double {
444
- tapret : tapret_anchor. to_merkle_block ( proto, msg) ?,
445
- opret : opret_anchor. to_merkle_block ( proto, msg) ?,
446
- } ) ,
447
- }
448
- }
449
- }
450
-
451
- impl AnchoredBundles {
452
- pub fn merge_reveal ( self , other : Self ) -> Result < Self , MergeRevealError > {
453
- match ( self , other) {
454
- ( AnchoredBundles :: Tapret ( anchor, bundle1) , AnchoredBundles :: Tapret ( a, bundle2) )
455
- if a == anchor =>
456
- {
457
- Ok ( AnchoredBundles :: Tapret ( anchor, bundle1. merge_reveal ( bundle2) ?) )
458
- }
459
-
460
- ( AnchoredBundles :: Opret ( anchor, bundle1) , AnchoredBundles :: Opret ( a, bundle2) )
461
- if a == anchor =>
462
- {
463
- Ok ( AnchoredBundles :: Opret ( anchor, bundle1. merge_reveal ( bundle2) ?) )
464
- }
465
-
466
- (
467
- AnchoredBundles :: Tapret ( tapret_anchor, tapret_bundle) ,
468
- AnchoredBundles :: Opret ( opret_anchor, opret_bundle) ,
469
- )
470
- | (
471
- AnchoredBundles :: Opret ( opret_anchor, opret_bundle) ,
472
- AnchoredBundles :: Tapret ( tapret_anchor, tapret_bundle) ,
473
- ) => Ok ( AnchoredBundles :: Double {
474
- tapret_anchor,
475
- tapret_bundle,
476
- opret_anchor,
477
- opret_bundle,
478
- } ) ,
479
-
480
- (
481
- AnchoredBundles :: Double {
482
- tapret_anchor,
483
- tapret_bundle,
484
- opret_anchor,
485
- opret_bundle,
486
- } ,
487
- AnchoredBundles :: Tapret ( t, bundle) ,
488
- )
489
- | (
490
- AnchoredBundles :: Tapret ( t, bundle) ,
491
- AnchoredBundles :: Double {
492
- tapret_anchor,
493
- tapret_bundle,
494
- opret_anchor,
495
- opret_bundle,
496
- } ,
497
- ) if tapret_anchor == t => Ok ( AnchoredBundles :: Double {
498
- tapret_anchor,
499
- opret_anchor,
500
- tapret_bundle : tapret_bundle. merge_reveal ( bundle) ?,
501
- opret_bundle,
502
- } ) ,
503
-
504
- (
505
- AnchoredBundles :: Double {
506
- tapret_anchor,
507
- tapret_bundle,
508
- opret_anchor,
509
- opret_bundle,
510
- } ,
511
- AnchoredBundles :: Opret ( o, bundle) ,
512
- )
513
- | (
514
- AnchoredBundles :: Opret ( o, bundle) ,
515
- AnchoredBundles :: Double {
516
- tapret_anchor,
517
- tapret_bundle,
518
- opret_anchor,
519
- opret_bundle,
520
- } ,
521
- ) if opret_anchor == o => Ok ( AnchoredBundles :: Double {
522
- tapret_anchor,
523
- opret_anchor,
524
- tapret_bundle : tapret_bundle. merge_reveal ( bundle) ?,
525
- opret_bundle,
526
- } ) ,
527
-
528
- (
529
- AnchoredBundles :: Double {
530
- tapret_anchor,
531
- tapret_bundle : tapret1,
532
- opret_anchor,
533
- opret_bundle : opret1,
534
- } ,
535
- AnchoredBundles :: Double {
536
- tapret_anchor : t,
537
- opret_anchor : o,
538
- tapret_bundle : tapret2,
539
- opret_bundle : opret2,
540
- ..
541
- } ,
542
- ) if tapret_anchor == t && opret_anchor == o => Ok ( AnchoredBundles :: Double {
543
- tapret_anchor,
544
- opret_anchor,
545
- tapret_bundle : tapret1. merge_reveal ( tapret2) ?,
546
- opret_bundle : opret1. merge_reveal ( opret2) ?,
547
- } ) ,
548
-
549
- ( me, _) => Err ( MergeRevealError :: AnchorsNonEqual (
550
- me. bundles ( )
551
- . next ( )
552
- . expect ( "at least one bundle" )
553
- . bundle_id ( ) ,
554
- ) ) ,
555
- }
556
- }
557
- }
0 commit comments