/
did.go
795 lines (638 loc) · 22.4 KB
/
did.go
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
package did
import (
"bytes"
"context"
"crypto/ecdsa"
"crypto/ed25519"
"encoding/json"
"errors"
"fmt"
"math/big"
"sort"
"strconv"
"strings"
"time"
"github.com/MetaBloxIO/did-sdk-go/v2/registry"
"github.com/mr-tron/base58"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/ethclient"
)
var boundedContracts map[string]*BoundedContract
func initBoundedContracts(chainList []string) error {
boundedContracts = make(map[string]*BoundedContract)
for _, chainName := range chainList {
_, ok := ChainName2IdMap[chainName]
if !ok {
return ErrUnknownChainName
}
contractConfig, ok := ChainName2ContractConfigMap[chainName]
if !ok {
return ErrUnknownChainName
}
boundedContract, err := GetRegistryInstance(contractConfig)
if err != nil {
return err
}
boundedContracts[chainName] = boundedContract
}
return nil
}
func GetBoundedContract(chainName string) (*BoundedContract, error) {
boundedContract, ok := boundedContracts[chainName]
if !ok {
return nil, ErrUnknownChainName
}
return boundedContract, nil
}
func GetRegistryInstance(config ContractConfig) (bound *BoundedContract, err error) {
bound = new(BoundedContract)
if !common.IsHexAddress(config.ContractAddr) {
return bound, ErrETHAddress
}
bound.Client, err = ethclient.Dial(config.RpcUrl)
if err != nil {
return bound, err
}
bound.ContractAddr = common.HexToAddress(config.ContractAddr)
bound.Instance, err = registry.NewRegistry(bound.ContractAddr, bound.Client)
if err != nil {
return bound, err
}
bound.ChainID, err = bound.Client.ChainID(context.Background())
if err != nil {
return bound, err
}
bound.ChainName = config.ChainName
return bound, nil
}
func GenerateDIDString(pubKey interface{}, network string) string {
switch pubKey := pubKey.(type) {
case *ecdsa.PublicKey:
if network == "solana" {
return ""
}
return generateEthDIDString(pubKey, network)
case *ed25519.PublicKey:
if network != "solana" {
return ""
}
return generateSolDIDString(pubKey, network)
default:
return ""
}
}
func generateEthDIDString(pubKey *ecdsa.PublicKey, network string) string {
ethAddress := crypto.PubkeyToAddress(*pubKey)
// No network/chainID provided, default on Ethereum Mainnet
if network == "" {
return "did:metablox:" + ethAddress.Hex()
} else {
return "did:metablox:" + network + ":" + ethAddress.Hex()
}
}
func generateSolDIDString(pubKey *ed25519.PublicKey, network string) string {
solAddress := base58.Encode(*pubKey)
return "did:metablox:" + network + ":" + solAddress
}
// TODO: check that this function can be safely removed. The foundation service doesn't need to create new DID documents; however, some other system may want to import this function
func CreateDID(publicKey interface{}, bound BoundedContract) *DIDDocument {
document := new(DIDDocument)
loc, _ := time.LoadLocation("UTC")
document.ID = GenerateDIDString(publicKey, bound.ChainName)
document.Context = make([]string, 0)
document.Context = append(document.Context, ContextSecp256k1)
document.Context = append(document.Context, ContextDID)
document.Created = time.Now().In(loc).Format(time.RFC3339)
document.Updated = document.Created
document.Version = 1
VM := VerificationMethod{}
address := ""
if bound.ChainName == "solana" {
edPubkey := publicKey.(*ed25519.PublicKey)
address = base58.Encode(*edPubkey)
VM.BlockchainAccountId = "solana" + ":" + address
} else {
ecPubkey := publicKey.(*ecdsa.PublicKey)
address = crypto.PubkeyToAddress(*ecPubkey).Hex()
VM.BlockchainAccountId = "eip155:" + bound.ChainID.String() + ":" + address
}
VM.ID = document.ID + "#controller"
VM.Controller = document.ID
VM.MethodType = Secp256k1Key
document.VerificationMethod = append(document.VerificationMethod, VM)
document.Authentication = VM.ID
document.AssertionMethod = VM.ID
return document
}
// TODO: check that this function can be safely removed
func DocumentToJson(document *DIDDocument) ([]byte, error) {
jsonDoc, err := json.Marshal(document)
if err != nil {
return nil, err
}
return jsonDoc, nil
}
// TODO: check that this function can be safely removed
func JsonToDocument(jsonDoc []byte) (*DIDDocument, error) {
document := CreateDIDDocument()
err := json.Unmarshal(jsonDoc, document)
if err != nil {
return nil, err
}
return document, nil
}
// check format of DID string
func IsDIDValid(did []string) bool {
if len(did) != 3 && len(did) != 4 {
fmt.Println("Not exactly 3 or 4 sections in DID")
return false
}
prefix := did[0]
if prefix != "did" {
fmt.Println("First section of DID was '" + prefix + "' instead of 'did'")
return false
}
methodName := did[1]
if methodName != "metablox" {
fmt.Println("Second section of DID was '" + methodName + "'instead of 'metablox'")
return false
}
chainName := ""
identifierSection := ""
if len(did) == 3 {
chainName = "ethereum"
identifierSection = did[2]
} else {
chainName = did[2]
identifierSection = did[3]
}
if !common.IsHexAddress(identifierSection) && chainName != "solana" {
fmt.Println("Identifier section is formatted incorrectly")
return false
}
if len(identifierSection) == 0 {
fmt.Println("Identifier is empty")
return false
}
return true
}
// split did string into 3 sections. First two should be 'did' and 'metablox', last one wil be the identifier
func SplitDIDString(did string) []string {
return strings.Split(did, ":")
}
// splits did and checks that it is formatted correctly
func PrepareDID(did string) ([]string, bool) {
splitString := SplitDIDString(did)
valid := IsDIDValid(splitString)
return splitString, valid
}
func GetChainNameFromDID(did string) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
if len(splitDID) == 3 {
return "ethereum", nil
}
_, ok := ChainName2IdMap[splitDID[2]]
if !ok {
chainID, err := strconv.Atoi(splitDID[2])
if err != nil {
return "", ErrInvalidDID
}
chainName, ok := ChainId2NameMap[chainID]
if !ok {
return "", ErrUnknownChainName
}
return chainName, nil
} else {
return splitDID[2], nil
}
}
func GetDocument(targetAddress string, chainName string) (*DIDDocument, [32]byte, error) {
bound, err := GetBoundedContract(chainName)
if err != nil {
return nil, [32]byte{0}, err
}
txBlk, err := bound.Instance.Changed(nil, common.HexToAddress(targetAddress))
if err != nil {
return nil, [32]byte{0}, err
}
document := new(DIDDocument)
loc, _ := time.LoadLocation("UTC")
//document.ID = "did:metablox:" + "0x" + bound.ChainID.Text(16) + ":" + targetAddress
document.ID = "did:metablox:" + bound.ChainName + ":" + targetAddress
document.Context = make([]string, 0)
document.Context = append(document.Context, ContextSecp256k1)
document.Context = append(document.Context, ContextDID)
document.Created = time.Now().In(loc).Format(time.RFC3339) //todo: need to get this from contract
document.Updated = document.Created //todo: need to get this from contract
document.Version = 1 //todo: need to get this from contract
VM := VerificationMethod{}
VM.ID = document.ID + "#controller"
if bound.ChainName == "solana" {
VM.BlockchainAccountId = "solana" + ":" + targetAddress
VM.MethodType = Ed25519Key
} else {
VM.BlockchainAccountId = "eip155:" + bound.ChainID.String() + ":" + targetAddress
VM.MethodType = Secp256k1Key
}
VM.Controller = document.ID
document.VerificationMethod = append(document.VerificationMethod, VM)
document.Authentication = VM.ID
document.AssertionMethod = VM.ID
/*
contractAbi, err := abi.JSON(strings.NewReader(string(registry.RegistryABI)))
if err != nil {
return nil, [32]byte{0}, err
}
*/
if txBlk.Int64() != big.NewInt(0).Int64() {
// We should check on the Event to rebuild the Doc here
}
placeholderHash := [32]byte{94, 241, 27, 134, 190, 223, 112, 91, 189, 49, 221, 31, 228, 35, 189, 213, 251, 60, 60, 210, 162, 45, 151, 3, 31, 78, 41, 239, 41, 75, 198, 139}
return document, placeholderHash, nil
}
// generate the did document that matches the provided did string. Any errors are returned in the ResolutionMetadata.
// Note that options currently does nothing; including it is a requirement according to W3C specifications, but we don't do anything with it right now
func Resolve(did string, options *ResolutionOptions, bound *BoundedContract) (*ResolutionMetadata, *DIDDocument, *DocumentMetadata) {
splitDID, valid := PrepareDID(did)
if !valid {
return &ResolutionMetadata{Error: "invalid Did"}, nil, &DocumentMetadata{}
}
targetAddress := ""
chainName := ""
if len(splitDID) == 3 {
targetAddress = splitDID[2]
chainName = "ethereum"
} else {
targetAddress = splitDID[3]
chainName = splitDID[2]
}
generatedDocument, _, err := GetDocument(targetAddress, chainName)
if err != nil {
return &ResolutionMetadata{Error: err.Error()}, nil, nil
}
docID, success := PrepareDID(generatedDocument.ID)
if !success {
return &ResolutionMetadata{Error: "document DID is invalid"}, nil, nil
}
if docID[3] != targetAddress { //identifier of the document should match provided did
return &ResolutionMetadata{Error: "generated document DID does not match provided DID"}, nil, nil
}
//compare document hash to the hash value given by contract.GetDocument() to ensure data integrity
/*comparisonHash := sha256.Sum256(ConvertDocToBytes(*generatedDocument)) //disabling this at the moment to avoid needing to update placeholderHash while we're still modfiying document layout
if comparisonHash != generatedHash {
return &ResolutionMetadata{Error: "document failed hash check"}, nil, nil
}*/
return &ResolutionMetadata{}, generatedDocument, nil
}
// generate a did document and return it in a specific data format (currently just JSON)
func ResolveRepresentation(did string, options *RepresentationResolutionOptions, bound *BoundedContract) (*RepresentationResolutionMetadata, []byte, *DocumentMetadata) {
//Should be similar to Resolve, but returns the document in a specific representation format.
//Representation type is included in options and returned in resolution metadata
readOptions := CreateResolutionOptions()
readResolutionMeta, document, readDocumentMeta := Resolve(did, readOptions, bound)
if readResolutionMeta.Error != "" {
return &RepresentationResolutionMetadata{Error: readResolutionMeta.Error}, nil, nil
}
switch options.Accept {
case "application/did+json":
fallthrough
default: //default to JSON format if options.Accept is empty/invalid
byteStream, err := json.Marshal(document)
if err != nil {
return &RepresentationResolutionMetadata{Error: "failed to convert document into JSON"}, nil, nil
}
return &RepresentationResolutionMetadata{ContentType: "application/did+json"}, byteStream, readDocumentMeta
}
}
func ChangeController(did string, newController common.Address, privKey *ecdsa.PrivateKey, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.ChangeController(auth, common.HexToAddress(identifiers), newController)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func ChangeControllerPermit(did string, newController common.Address, privKey *ecdsa.PrivateKey, deadlinne big.Int, signature []byte, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.ChangeControllerPermit(auth, common.HexToAddress(identifiers), newController, &deadlinne, signature)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func AddDelegate(did string, delegate common.Address, delegateType [32]byte, validity big.Int, privKey *ecdsa.PrivateKey, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.AddDelegate(auth, common.HexToAddress(identifiers), delegateType, delegate, &validity)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func AddDelegatePermit(did string, delegate common.Address, delegateType [32]byte, validity big.Int, privKey *ecdsa.PrivateKey, deadline big.Int, signature []byte, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.AddDelegatePermit(auth, common.HexToAddress(identifiers), delegateType, delegate, &validity, &deadline, signature)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func RevokeDelegate(did string, delegate common.Address, delegateType [32]byte, privKey *ecdsa.PrivateKey, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.RevokeDelegate(auth, common.HexToAddress(identifiers), delegateType, delegate)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func RevokeDelegatePermit(did string, delegate common.Address, delegateType [32]byte, privKey *ecdsa.PrivateKey, deadline big.Int, signature []byte, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.RevokeDelegatePermit(auth, common.HexToAddress(identifiers), delegateType, delegate, &deadline, signature)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func ChangeAttribute(did string, attribute [32]byte, attributeValue []byte, validity big.Int, privKey *ecdsa.PrivateKey, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.SetAttribute(auth, common.HexToAddress(identifiers), attribute, attributeValue, &validity)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func ChangeAttributePermit(did string, attribute [32]byte, attributeValue []byte, validity big.Int, privKey *ecdsa.PrivateKey, deadline big.Int, signature []byte, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.SetAttributePermit(auth, common.HexToAddress(identifiers), attribute, attributeValue, &validity, &deadline, signature)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func RevokeAttribute(did string, attribute [32]byte, attributeValue []byte, privKey *ecdsa.PrivateKey, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.RevokeAttribute(auth, common.HexToAddress(identifiers), attribute, attributeValue)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
func RevokeAttributePermit(did string, attribute [32]byte, attributeValue []byte, privKey *ecdsa.PrivateKey, deadline big.Int, signature []byte, bound *BoundedContract) (string, error) {
splitDID, valid := PrepareDID(did)
if !valid {
return "", ErrInvalidDID
}
identifiers := splitDID[len(splitDID)-1]
auth, err := bind.NewKeyedTransactorWithChainID(privKey, bound.ChainID)
if err != nil {
return "", err
}
price, err := bound.Client.SuggestGasPrice(context.Background())
if err != nil {
return "", err
}
auth.GasPrice = price
auth.GasLimit = uint64(300000)
balance, err := bound.Client.BalanceAt(context.Background(), auth.From, nil)
if err != nil {
return "", err
}
if balance.Cmp(new(big.Int).Mul(price, new(big.Int).SetInt64(300000))) < 0 {
return "", errors.New("insufficient balance")
}
tx, err := bound.Instance.RevokeAttributePermit(auth, common.HexToAddress(identifiers), attribute, attributeValue, &deadline, signature)
if err != nil {
return "", err
}
return tx.Hash().Hex(), nil
}
// convert document into byte array so it can be hashed (appears to be unused currently)
func ConvertDocToBytes(doc DIDDocument) []byte {
var convertedBytes []byte
sort.SliceStable(doc.Context, func(i, j int) bool { //have to sort arrays alphabetically before iterating over them to ensure a consistent ordering
return doc.Context[i] < doc.Context[j]
})
for _, item := range doc.Context {
convertedBytes = bytes.Join([][]byte{convertedBytes, []byte(item)}, []byte{})
}
convertedBytes = bytes.Join([][]byte{convertedBytes, []byte(doc.ID), []byte(doc.Created), []byte(doc.Updated), []byte(strconv.Itoa(doc.Version))}, []byte{})
sort.SliceStable(doc.VerificationMethod, func(i, j int) bool {
return doc.VerificationMethod[i].ID < doc.VerificationMethod[j].ID
})
for _, item := range doc.VerificationMethod {
convertedBytes = bytes.Join([][]byte{convertedBytes, ConvertVMToBytes(item)}, []byte{})
}
convertedBytes = bytes.Join([][]byte{convertedBytes, []byte(doc.Authentication)}, []byte{})
sort.SliceStable(doc.Service, func(i, j int) bool {
return doc.Service[i].ID < doc.Service[j].ID
})
for _, item := range doc.Service {
convertedBytes = bytes.Join([][]byte{convertedBytes, ConvertServiceToBytes(item)}, []byte{})
}
return convertedBytes
}
// convert VM to byte array. Used as part of converting document to bytes
func ConvertVMToBytes(vm VerificationMethod) []byte {
var convertedBytes []byte
convertedBytes = bytes.Join([][]byte{convertedBytes, []byte(vm.ID), []byte(vm.MethodType), []byte(vm.Controller), []byte(vm.BlockchainAccountId)}, []byte{})
return convertedBytes
}
// convert service to byte array. Used as part of converting document to bytes
func ConvertServiceToBytes(service Service) []byte {
var convertedBytes []byte
convertedBytes = bytes.Join([][]byte{convertedBytes, []byte(service.ID), []byte(service.Type), []byte(service.ServiceEndpoint)}, []byte{})
return convertedBytes
}