Repository navigation
Expand file tree
/
Copy pathissuer.go
More file actions
987 lines (805 loc) · 22.6 KB
/
Copy pathissuer.go
File metadata and controls
987 lines (805 loc) · 22.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
package tapsdk
import (
"context"
"errors"
"fmt"
"sync"
"time"
)
const (
defaultMintResolveTimeout = 10 * time.Second
defaultMintResolveInterval = 200 * time.Millisecond
defaultMintCancelTimeout = 5 * time.Second
)
var errMintResultNotReady = errors.New("mint result is not visible yet")
// Issuer is the high-level minting surface for SDK business assets.
//
// It creates grouped fungible assets, standalone NFTs, NFT collections, and
// additional supply or collection items while hiding tapd batch mechanics from
// application code. MintClient remains available through Client for callers
// that need direct batch control.
//
// Issuer serializes its own mint calls because tapd has one active mint batch
// per daemon. Issuers returned by the same Wallet share that lock.
type Issuer struct {
client Client
mu *sync.Mutex
}
// NewIssuer creates a high-level issuer backed by the given client.
// Concurrent calls on the returned Issuer are serialized.
func NewIssuer(client Client) *Issuer {
return &Issuer{
client: client,
mu: &sync.Mutex{},
}
}
// NewIssuer returns a high-level issuer backed by the wallet's client.
// Issuers returned by the same Wallet share mint-call serialization.
func (s *Wallet) NewIssuer() *Issuer {
return &Issuer{
client: s.client,
mu: &s.issuerMu,
}
}
// MintOption configures high-level issuer calls.
type MintOption func(*MintOptions)
// MintOptions contains optional parameters for high-level issuer calls.
type MintOptions struct {
// feeRate is the target fee rate for the genesis transaction. Zero uses
// the daemon default.
feeRate FeeRate
// resolveTimeout is how long high-level issuer calls wait for tapd's
// wallet projection to expose the accepted mint result.
resolveTimeout time.Duration
// externalIssuanceKey is the externally managed key descriptor used for
// issuance authorization.
externalIssuanceKey *ExternalKey
// externalIssuanceSigner signs issuance authorization payloads.
externalIssuanceSigner ExternalIssuanceSigner
}
// WithMintFeeRate sets the target fee rate for the genesis transaction. A
// zero value uses tapd's daemon default.
func WithMintFeeRate(feeRate FeeRate) MintOption {
return func(o *MintOptions) {
o.feeRate = feeRate
}
}
// WithMintResolveTimeout sets how long high-level issuer calls wait for tapd's
// wallet projection to expose the accepted mint result. If this timeout is hit,
// the call returns ErrMintResolveTimeout and the mint may still have been
// accepted by tapd.
func WithMintResolveTimeout(timeout time.Duration) MintOption {
return func(o *MintOptions) {
o.resolveTimeout = timeout
}
}
// WithExternalIssuanceKey configures an externally managed issuance key for
// high-level issuer calls. It maps to tapd's external group key at the
// transport boundary while keeping application code on the SDK Asset,
// Collection, and Issuance model.
func WithExternalIssuanceKey(key ExternalKey) MintOption {
return func(o *MintOptions) {
keyCopy := key
o.externalIssuanceKey = &keyCopy
}
}
// WithExternalIssuanceSigner configures the signer used when the issuer needs
// an external issuance authorization signature.
func WithExternalIssuanceSigner(signer ExternalIssuanceSigner) MintOption {
return func(o *MintOptions) {
o.externalIssuanceSigner = signer
}
}
// CreateFungible creates a grouped fungible asset and returns the SDK business
// Asset keyed by its group AssetRef.
//
// If the returned error wraps ErrMintResolveTimeout, tapd may already have
// accepted or finalized the mint. Do not blindly retry; inspect wallet assets,
// issuances, or mint batches first to avoid duplicate issuance.
func (i *Issuer) CreateFungible(ctx context.Context,
spec FungibleAssetSpec,
opts ...MintOption) (*Asset, error) {
unlock := i.lock()
defer unlock()
asset, err := i.createFungible(ctx, spec, opts...)
if err != nil {
return nil, wrapErr("CreateFungible", err)
}
return asset, nil
}
// IssueFungible creates an additional issuance for an existing grouped
// fungible asset.
//
// If the returned error wraps ErrMintResolveTimeout, tapd may already have
// accepted or finalized the mint. Do not blindly retry; inspect wallet assets,
// issuances, or mint batches first to avoid duplicate issuance.
func (i *Issuer) IssueFungible(ctx context.Context,
ref AssetRef, amount uint64,
opts ...MintOption) (*Issuance, error) {
unlock := i.lock()
defer unlock()
issuance, err := i.issueFungible(ctx, ref, amount, opts...)
if err != nil {
return nil, wrapErr("IssueFungible", err)
}
return issuance, nil
}
// CreateNFT creates a standalone NFT and returns it as an Asset keyed by its
// asset-ID AssetRef.
//
// If the returned error wraps ErrMintResolveTimeout, tapd may already have
// accepted or finalized the mint. Do not blindly retry; inspect wallet assets
// or mint batches first to avoid duplicate NFTs.
func (i *Issuer) CreateNFT(ctx context.Context, spec NFTSpec,
opts ...MintOption) (*Asset, error) {
unlock := i.lock()
defer unlock()
asset, err := i.createNFT(ctx, spec, opts...)
if err != nil {
return nil, wrapErr("CreateNFT", err)
}
return asset, nil
}
// CreateCollection creates a new NFT collection by minting the first item in a
// grouped collectible asset. It returns the collection and first item together
// in a CollectionMintResult.
//
// If the returned error wraps ErrMintResolveTimeout, tapd may already have
// accepted or finalized the mint. Do not blindly retry; inspect wallet assets,
// collections, or mint batches first to avoid duplicate NFTs.
func (i *Issuer) CreateCollection(ctx context.Context,
firstItem NFTSpec,
opts ...MintOption) (*CollectionMintResult, error) {
unlock := i.lock()
defer unlock()
result, err := i.createCollection(ctx, firstItem, opts...)
if err != nil {
return nil, wrapErr("CreateCollection", err)
}
return result, nil
}
// MintCollectionItem mints a new NFT item into an existing collection.
//
// If the returned error wraps ErrMintResolveTimeout, tapd may already have
// accepted or finalized the mint. Do not blindly retry; inspect wallet assets,
// collections, or mint batches first to avoid duplicate NFTs.
func (i *Issuer) MintCollectionItem(ctx context.Context,
collectionRef AssetRef, item NFTSpec,
opts ...MintOption) (*Asset, error) {
unlock := i.lock()
defer unlock()
asset, err := i.mintCollectionItem(ctx, collectionRef, item, opts...)
if err != nil {
return nil, wrapErr("MintCollectionItem", err)
}
return asset, nil
}
func (i *Issuer) createFungible(ctx context.Context,
spec FungibleAssetSpec,
opts ...MintOption) (*Asset, error) {
if err := validateFungibleSpec(spec); err != nil {
return nil, err
}
stage := &MintAsset{
AssetVersion: spec.AssetVersion,
AssetType: AssetTypeFungible,
Name: spec.Name,
AssetMeta: spec.AssetMeta,
InitialSupply: spec.Amount,
AllowIssuance: true,
DecimalDisplay: spec.DecimalDisplay,
ScriptKey: spec.ScriptKey,
EnableSupplyCommitments: spec.EnableSupplyCommitments,
}
record, err := i.mintAssetAndResolve(ctx, stage, nil, func(
record *AssetRecord) bool {
return isRecord(record, AssetTypeFungible, spec.Name,
spec.Amount) && record.AssetRef.IsGroupRef()
}, opts)
if err != nil {
return nil, err
}
assets := assetsFromRecords([]*AssetRecord{record})
if len(assets) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
spec.Name)
}
return assets[0], nil
}
func (i *Issuer) issueFungible(ctx context.Context,
ref AssetRef, amount uint64,
opts ...MintOption) (*Issuance, error) {
if err := validateGroupedRef(ref); err != nil {
return nil, err
}
if amount == 0 {
return nil, ErrZeroAmount
}
mintOpts := applyMintOptions(opts)
if err := i.ensureReady(ctx); err != nil {
return nil, err
}
base, err := i.resolveFungibleAsset(ctx, ref)
if err != nil {
return nil, err
}
before, err := i.listMintRecords(ctx, &base.AssetRef)
if err != nil {
return nil, err
}
stage := &MintIssuance{
AssetRef: base.AssetRef,
Name: base.Genesis.Tag,
AssetType: AssetTypeFungible,
Amount: amount,
}
applyExternalIssuanceKeyToIssuance(stage, mintOpts)
err = i.mintAndFinalize(ctx, func() (*MintingBatch,
error) {
return i.client.MintIssuance(ctx,
&MintIssuanceRequest{
Issuance: stage,
ShortResponse: true,
},
)
}, mintOpts, issuanceSigningContext{
operation: IssuanceOperationIssueAsset,
assetRef: base.AssetRef,
})
if err != nil {
return nil, err
}
record, err := i.findNewRecord(ctx, &base.AssetRef, recordIDs(before),
func(record *AssetRecord) bool {
return isRecord(
record, AssetTypeFungible,
base.Genesis.Tag, amount,
)
},
mintOpts.resolveTimeoutOrDefault(),
)
if err != nil {
return nil, err
}
issuances := issuancesFromRecords([]*AssetRecord{record})
if len(issuances) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound, ref)
}
return issuances[0], nil
}
func (i *Issuer) createNFT(ctx context.Context, spec NFTSpec,
opts ...MintOption) (*Asset, error) {
if err := validateNFTSpec(spec); err != nil {
return nil, err
}
stage := mintNFTAsset(spec, false)
record, err := i.mintAssetAndResolve(ctx, stage, nil, func(
record *AssetRecord) bool {
return isRecord(record, AssetTypeNFT, spec.Name, 1) &&
record.AssetRef.IsAssetIDRef()
}, opts)
if err != nil {
return nil, err
}
assets := assetsFromRecords([]*AssetRecord{record})
if len(assets) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
spec.Name)
}
return assets[0], nil
}
func (i *Issuer) createCollection(ctx context.Context,
firstItem NFTSpec,
opts ...MintOption) (*CollectionMintResult, error) {
if err := validateNFTSpec(firstItem); err != nil {
return nil, err
}
stage := mintNFTAsset(firstItem, true)
record, err := i.mintAssetAndResolve(ctx, stage, nil, func(
record *AssetRecord) bool {
return isRecord(
record, AssetTypeNFT, firstItem.Name, 1,
) && record.AssetRef.IsGroupRef()
}, opts)
if err != nil {
return nil, err
}
collections := collectionsFromRecords([]*AssetRecord{record})
if len(collections) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
firstItem.Name)
}
assets := assetsFromRecords([]*AssetRecord{record})
if len(assets) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
firstItem.Name)
}
return &CollectionMintResult{
Collection: collections[0],
FirstItem: assets[0],
}, nil
}
func (i *Issuer) mintCollectionItem(ctx context.Context,
collectionRef AssetRef, item NFTSpec,
opts ...MintOption) (*Asset, error) {
if err := validateGroupedRef(collectionRef); err != nil {
return nil, err
}
if err := validateNFTSpec(item); err != nil {
return nil, err
}
mintOpts := applyMintOptions(opts)
if err := i.ensureReady(ctx); err != nil {
return nil, err
}
collection, err := i.resolveCollection(ctx, collectionRef)
if err != nil {
return nil, err
}
before, err := i.listMintRecords(ctx, &collection.AssetRef)
if err != nil {
return nil, err
}
stage := &MintIssuance{
AssetRef: collection.AssetRef,
Name: item.Name,
AssetType: AssetTypeNFT,
AssetMeta: item.AssetMeta,
Amount: 1,
AssetVersion: item.AssetVersion,
ScriptKey: item.ScriptKey,
}
applyExternalIssuanceKeyToIssuance(stage, mintOpts)
err = i.mintAndFinalize(ctx, func() (*MintingBatch,
error) {
return i.client.MintIssuance(ctx,
&MintIssuanceRequest{
Issuance: stage,
ShortResponse: true,
},
)
}, mintOpts, issuanceSigningContext{
operation: IssuanceOperationMintCollectionItem,
assetRef: collection.AssetRef,
})
if err != nil {
return nil, err
}
record, err := i.findNewRecord(ctx, &collection.AssetRef,
recordIDs(before), func(record *AssetRecord) bool {
return isRecord(
record, AssetTypeNFT, item.Name, 1,
) && record.AssetRef.Equivalent(collection.AssetRef)
},
mintOpts.resolveTimeoutOrDefault(),
)
if err != nil {
return nil, err
}
assets := assetsFromRecords([]*AssetRecord{record})
if len(assets) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
item.Name)
}
return assets[0], nil
}
func validateFungibleSpec(spec FungibleAssetSpec) error {
if spec.Name == "" {
return ErrAssetNameRequired
}
if spec.Amount == 0 {
return ErrZeroAmount
}
return nil
}
func validateNFTSpec(spec NFTSpec) error {
if spec.Name == "" {
return ErrAssetNameRequired
}
return nil
}
func validateGroupedRef(ref AssetRef) error {
if err := ref.Validate(); err != nil {
return err
}
if !ref.IsGroupRef() {
return ErrAssetNotIssuable
}
return nil
}
func mintNFTAsset(spec NFTSpec, collection bool) *MintAsset {
return &MintAsset{
AssetVersion: spec.AssetVersion,
AssetType: AssetTypeNFT,
Name: spec.Name,
AssetMeta: spec.AssetMeta,
InitialSupply: 1,
AllowIssuance: collection,
ScriptKey: spec.ScriptKey,
}
}
func (i *Issuer) lock() func() {
if i == nil || i.mu == nil {
return func() {}
}
i.mu.Lock()
return i.mu.Unlock
}
type recordMatcher func(*AssetRecord) bool
func (i *Issuer) mintAssetAndResolve(ctx context.Context,
stage *MintAsset, ref *AssetRef,
matches recordMatcher,
opts []MintOption) (*AssetRecord, error) {
mintOpts := applyMintOptions(opts)
signingOperation := signingOperationForMintAsset(stage)
if mintOpts.externalIssuanceSigner != nil &&
signingOperation == IssuanceOperationUnknown {
return nil, ErrAssetNotIssuable
}
if err := i.ensureReady(ctx); err != nil {
return nil, err
}
before, err := i.listMintRecords(ctx, ref)
if err != nil {
return nil, err
}
applyExternalIssuanceKeyToAsset(stage, mintOpts)
err = i.mintAndFinalize(ctx, func() (*MintingBatch,
error) {
return i.client.MintAsset(ctx, &MintAssetRequest{
Asset: stage,
ShortResponse: true,
})
}, mintOpts, issuanceSigningContext{
operation: signingOperation,
})
if err != nil {
return nil, err
}
return i.findNewRecord(
ctx, ref, recordIDs(before), matches,
mintOpts.resolveTimeoutOrDefault(),
)
}
func (i *Issuer) mintAndFinalize(ctx context.Context,
stage func() (*MintingBatch, error),
opts *MintOptions, signingContext issuanceSigningContext) error {
if i == nil || i.client == nil {
return fmt.Errorf("issuer client is nil")
}
if opts == nil {
opts = &MintOptions{}
}
if opts.externalIssuanceKey != nil &&
opts.externalIssuanceSigner == nil {
return ErrExternalIssuanceSignerRequired
}
if opts.externalIssuanceSigner != nil &&
opts.externalIssuanceKey == nil {
return ErrExternalIssuanceKeyRequired
}
if _, err := stage(); err != nil {
return err
}
if opts.externalIssuanceSigner != nil {
err := i.signAndFinalizeExternalIssuance(
ctx, opts, signingContext,
)
if err != nil {
i.cancelBatchBestEffort()
}
return err
}
_, err := i.client.FinalizeBatch(ctx,
&FinalizeBatchRequest{
ShortResponse: true,
FeeRate: opts.feeRate,
},
)
if err == nil {
return nil
}
i.cancelBatchBestEffort()
return err
}
type issuanceSigningContext struct {
operation IssuanceOperation
assetRef AssetRef
}
func (i *Issuer) signAndFinalizeExternalIssuance(ctx context.Context,
opts *MintOptions, signingContext issuanceSigningContext) error {
if opts.externalIssuanceKey == nil {
return ErrExternalIssuanceKeyRequired
}
funded, err := i.client.FundBatch(ctx, &FundBatchRequest{
FeeRate: opts.feeRate,
})
if err != nil {
return err
}
requests, err := issuanceSigningRequests(
funded, opts.externalIssuanceKey, signingContext,
)
if err != nil {
return err
}
signedPsbts := make([]string, 0, len(requests))
for _, req := range requests {
signed, err := opts.externalIssuanceSigner.SignIssuance(
ctx, req,
)
if err != nil {
return err
}
if signed.VirtualPSBT == "" {
return ErrExternalIssuanceSignatureRequired
}
signedPsbts = append(signedPsbts, signed.VirtualPSBT)
}
_, err = i.client.SealBatch(ctx, &SealBatchRequest{
ShortResponse: true,
SignedGroupVirtualPSBTs: signedPsbts,
})
if err != nil {
return err
}
_, err = i.client.FinalizeBatch(ctx, &FinalizeBatchRequest{
ShortResponse: true,
})
return err
}
func issuanceSigningRequests(batch *VerboseMintingBatch,
externalKey *ExternalKey,
signingContext issuanceSigningContext) ([]IssuanceSigningRequest, error) {
if batch == nil {
return nil, fmt.Errorf("nil funded minting batch")
}
if externalKey == nil {
return nil, ErrExternalIssuanceKeyRequired
}
requests := make([]IssuanceSigningRequest, 0, len(batch.UnsealedAssets))
for _, unsealed := range batch.UnsealedAssets {
if unsealed.GroupVirtualPSBT == "" {
continue
}
req := IssuanceSigningRequest{
Operation: signingContext.operation,
AssetRef: signingContext.assetRef,
ExternalKey: *externalKey,
VirtualPSBT: unsealed.GroupVirtualPSBT,
VirtualTx: unsealed.GroupVirtualTx,
}
if unsealed.Asset != nil {
req.Name = unsealed.Asset.Name
req.AssetType = unsealed.Asset.AssetType
req.Amount = unsealed.Asset.Amount
req.ScriptKey = unsealed.Asset.ScriptKey
}
if unsealed.GroupKeyRequest != nil {
req.AnchorGenesis = unsealed.GroupKeyRequest.AnchorGenesis
if unsealed.GroupKeyRequest.ExternalKey != nil {
req.ExternalKey = *unsealed.GroupKeyRequest.ExternalKey
}
}
if req.AssetRef.IsZero() && unsealed.GroupVirtualTx != nil &&
unsealed.GroupVirtualTx.TweakedKey != nil {
req.AssetRef = AssetRefFromGroupKey(
*unsealed.GroupVirtualTx.TweakedKey,
)
}
requests = append(requests, req)
}
if len(requests) == 0 {
return nil, ErrExternalIssuanceRequestNotFound
}
return requests, nil
}
func applyExternalIssuanceKeyToAsset(stage *MintAsset, opts *MintOptions) {
if stage == nil || opts == nil || opts.externalIssuanceKey == nil {
return
}
if !stage.AllowIssuance {
return
}
stage.ExternalGroupKey = opts.externalIssuanceKey
}
func applyExternalIssuanceKeyToIssuance(stage *MintIssuance,
opts *MintOptions) {
if stage == nil || opts == nil {
return
}
stage.ExternalGroupKey = opts.externalIssuanceKey
}
func signingOperationForMintAsset(stage *MintAsset) IssuanceOperation {
if stage == nil || !stage.AllowIssuance {
return IssuanceOperationUnknown
}
switch stage.AssetType {
case AssetTypeFungible:
return IssuanceOperationCreateAsset
case AssetTypeNFT:
return IssuanceOperationCreateCollection
default:
return IssuanceOperationUnknown
}
}
func (i *Issuer) cancelBatchBestEffort() {
ctx, cancel := context.WithTimeout(
context.Background(), defaultMintCancelTimeout,
)
defer cancel()
_, _ = i.client.CancelBatch(ctx)
}
func (i *Issuer) ensureReady(ctx context.Context) error {
if i == nil || i.client == nil {
return fmt.Errorf("issuer client is nil")
}
batches, err := i.client.ListBatches(ctx, &ListBatchesRequest{})
if err != nil {
return err
}
for _, batch := range batches {
if batch == nil {
continue
}
if isActiveMintBatch(batch.Batch.State) {
return fmt.Errorf("%w: %s is in state %s",
ErrMintBatchActive, batch.Batch.BatchKey,
batch.Batch.State)
}
}
return nil
}
func isActiveMintBatch(state BatchState) bool {
switch state {
case BatchStatePending,
BatchStateFrozen,
BatchStateCommitted,
BatchStateBroadcast:
return true
default:
return false
}
}
func (i *Issuer) resolveFungibleAsset(ctx context.Context,
ref AssetRef) (*AssetRecord, error) {
records, err := i.listMintRecords(ctx, &ref)
if err != nil {
return nil, err
}
for _, record := range records {
if record == nil {
continue
}
if record.Genesis.Type != AssetTypeFungible {
return nil, fmt.Errorf("%w: expected fungible asset",
ErrWrongAssetType)
}
if !record.AssetRef.IsGroupRef() {
return nil, ErrAssetNotIssuable
}
return record, nil
}
return nil, fmt.Errorf("%w: %s", ErrAssetUnknown, ref)
}
func (i *Issuer) resolveCollection(ctx context.Context,
ref AssetRef) (*Collection, error) {
records, err := i.listMintRecords(ctx, &ref)
if err != nil {
return nil, err
}
for _, record := range records {
if record == nil {
continue
}
if record.Genesis.Type != AssetTypeNFT {
return nil, fmt.Errorf("%w: expected NFT collection",
ErrWrongAssetType)
}
if !record.AssetRef.IsGroupRef() {
return nil, fmt.Errorf("%w: expected collection ref",
ErrWrongAssetType)
}
collections := collectionsFromRecords(
[]*AssetRecord{record},
)
if len(collections) != 1 {
return nil, fmt.Errorf("%w: %s", ErrMintResultNotFound,
ref)
}
return collections[0], nil
}
return nil, fmt.Errorf("%w: %s", ErrAssetUnknown, ref)
}
func (i *Issuer) listMintRecords(ctx context.Context,
ref *AssetRef) ([]*AssetRecord, error) {
if i == nil || i.client == nil {
return nil, fmt.Errorf("issuer client is nil")
}
req := &ListAssetsRequest{
IncludeUnconfirmedMints: true,
AssetRef: ref,
}
return i.client.ListAssetRecords(ctx, req)
}
func (i *Issuer) findNewRecord(ctx context.Context,
ref *AssetRef, before map[AssetID]struct{},
matches recordMatcher, timeout time.Duration) (*AssetRecord,
error) {
deadline := time.NewTimer(timeout)
defer deadline.Stop()
ticker := time.NewTicker(defaultMintResolveInterval)
defer ticker.Stop()
for {
record, err := i.findNewRecordOnce(ctx, ref, before, matches)
if err == nil {
return record, nil
}
if !errors.Is(err, errMintResultNotReady) {
return nil, err
}
select {
case <-ctx.Done():
return nil, ctx.Err()
case <-deadline.C:
return nil, fmt.Errorf("%w: %v",
ErrMintResolveTimeout, err)
case <-ticker.C:
}
}
}
func (i *Issuer) findNewRecordOnce(ctx context.Context,
ref *AssetRef, before map[AssetID]struct{},
matches recordMatcher) (*AssetRecord, error) {
records, err := i.listMintRecords(ctx, ref)
if err != nil {
return nil, err
}
for _, record := range records {
if record == nil {
continue
}
if _, ok := before[record.Genesis.IssuanceID]; ok {
continue
}
if matches == nil || matches(record) {
return record, nil
}
}
return nil, errMintResultNotReady
}
func recordIDs(records []*AssetRecord) map[AssetID]struct{} {
ids := make(map[AssetID]struct{}, len(records))
for _, record := range records {
if record == nil {
continue
}
ids[record.Genesis.IssuanceID] = struct{}{}
}
return ids
}
func isRecord(record *AssetRecord, assetType AssetType,
name string, amount uint64) bool {
if record == nil {
return false
}
return record.Genesis.Type == assetType &&
record.Genesis.Tag == name &&
record.Amount == amount
}
func applyMintOptions(opts []MintOption) *MintOptions {
o := &MintOptions{}
for _, opt := range opts {
if opt == nil {
continue
}
opt(o)
}
return o
}
func (o *MintOptions) resolveTimeoutOrDefault() time.Duration {
if o == nil || o.resolveTimeout <= 0 {
return defaultMintResolveTimeout
}
return o.resolveTimeout
}