-
Notifications
You must be signed in to change notification settings - Fork 23
/
ssi_types.go
163 lines (136 loc) · 3.78 KB
/
ssi_types.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
package types
import (
fmt "fmt"
"strings"
proto "github.com/gogo/protobuf/proto"
)
type (
SsiMsg interface {
proto.Message
GetSignBytes() []byte
}
Signer struct {
Signer string
Authentication []string
AssertionMethod []string
VerificationMethod []*VerificationMethod
KeyAgreement []string
CapabilityInvocation []string
CapabilityDelegation []string
}
ValidDid struct {
DidId string
IsValid bool
}
ExtendedVerificationMethod struct {
Id string
Type string
Controller string
PublicKeyMultibase string
BlockchainAccountId string
Signature string
ClientSpec *ClientSpec
}
)
func CreateExtendedVerificationMethod(vm *VerificationMethod, signInfo *SignInfo) *ExtendedVerificationMethod {
extendedVm := &ExtendedVerificationMethod{
Id: vm.Id,
Type: vm.Type,
Controller: vm.Controller,
PublicKeyMultibase: vm.PublicKeyMultibase,
BlockchainAccountId: vm.BlockchainAccountId,
Signature: signInfo.Signature,
}
if signInfo.ClientSpec != nil {
extendedVm.ClientSpec = signInfo.ClientSpec
}
return extendedVm
}
// Struct catering to supported Client Spec's required inputs
type ClientSpecOpts struct {
ClientSpecType string
SSIDoc SsiMsg
SignerAddress string
}
// Cosmos ADR SignDoc Struct Definitions
type (
Val struct {
Data string `json:"data"`
Signer string `json:"signer"`
}
Msg struct {
Type string `json:"type"`
Value Val `json:"value"`
}
Fees struct {
Amount []string `json:"amount"`
Gas string `json:"gas"`
}
SignDoc struct {
AccountNumber string `json:"account_number"`
ChainId string `json:"chain_id"`
Fee Fees `json:"fee"`
Memo string `json:"memo"`
Msgs []Msg `json:"msgs"`
Sequence string `json:"sequence"`
}
)
// Handle Proof Struct of SSI Docs
type SSIProofInterface interface {
GetProofValue() string
GetType() string
GetVerificationMethod() string
}
// CAIP-10 Blockchain Account Id
type BlockchainId struct {
CAIP10Prefix string
ChainId string
BlockchainAddress string
}
func NewBlockchainId(blockchainAccountId string) (*BlockchainId, error) {
segments := strings.Split(blockchainAccountId, ":")
// Validate blockchainAccountId segments
if len(segments) != 3 {
return nil, fmt.Errorf(
"invalid CAIP-10 format for blockchainAccountId '%v'. Please refer: https://github.com/ChainAgnostic/CAIPs/blob/master/CAIPs/caip-10.md",
blockchainAccountId,
)
}
return &BlockchainId{
CAIP10Prefix: segments[0],
ChainId: segments[1],
BlockchainAddress: segments[2],
}, nil
}
func (bid *BlockchainId) ValidateSupportedCAIP10Prefix() error {
if !FindInSlice(SupportedCAIP10Prefixes, bid.CAIP10Prefix) {
return fmt.Errorf(
"unsupported CAIP-10 prefix: '%v', supported CAIP-10 prefixes are %v",
bid.CAIP10Prefix,
SupportedCAIP10Prefixes,
)
}
return nil
}
func (bid *BlockchainId) ValidateSupportChainId() error {
supportedChainIds := SupportedCAIP10PrefixChainIdsMap[bid.CAIP10Prefix]
if !FindInSlice(supportedChainIds, bid.ChainId) {
return fmt.Errorf(
"unsupported CAIP-10 chain-id: '%v', supported CAIP-10 chain-ids are %v",
bid.ChainId,
supportedChainIds,
)
}
return nil
}
func (bid *BlockchainId) ValidateSupportedBech32Prefix() error {
extractedBech32Prefix := strings.Split(bid.BlockchainAddress, "1")[0]
supportedBech32Prefix, supported := CosmosCAIP10ChainIdBech32PrefixMap[bid.ChainId]
if !supported {
return fmt.Errorf("chain-id %v is not supported", bid.ChainId)
}
if supportedBech32Prefix != extractedBech32Prefix {
return fmt.Errorf("invalid bech32 prefix for blockchain address: %v", bid.BlockchainAddress)
}
return nil
}