github.com/hyperledger/aries-framework-go@v0.3.2/pkg/didcomm/protocol/legacyconnection/models.go (about) 1 /* 2 Copyright Avast Software. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package legacyconnection 8 9 import ( 10 "encoding/json" 11 "errors" 12 "fmt" 13 "time" 14 15 "github.com/mitchellh/mapstructure" 16 17 "github.com/hyperledger/aries-framework-go/pkg/didcomm/protocol/decorator" 18 "github.com/hyperledger/aries-framework-go/pkg/doc/did" 19 ) 20 21 // Invitation model 22 // 23 // Invitation defines Connection protocol invitation message 24 // https://github.com/hyperledger/aries-rfcs/tree/main/features/0160-connection-protocol#0-invitation-to-connect 25 type Invitation struct { 26 // the Type of the connection invitation 27 Type string `json:"@type,omitempty"` 28 29 // the ID of the connection invitation 30 ID string `json:"@id,omitempty"` 31 32 // the Label of the connection invitation 33 Label string `json:"label,omitempty"` 34 35 // the RecipientKeys for the connection invitation 36 RecipientKeys []string `json:"recipientKeys,omitempty"` 37 38 // the Service endpoint of the connection invitation 39 ServiceEndpoint string `json:"serviceEndpoint,omitempty"` 40 41 // the RoutingKeys of the connection invitation 42 RoutingKeys []string `json:"routingKeys,omitempty"` 43 44 // the DID of the connection invitation 45 DID string `json:"did,omitempty"` 46 } 47 48 // Request defines a2a Connection request 49 // https://github.com/hyperledger/aries-rfcs/tree/main/features/0160-connection-protocol#1-connection-request 50 type Request struct { 51 Type string `json:"@type,omitempty"` 52 ID string `json:"@id,omitempty"` 53 Label string `json:"label"` 54 Thread *decorator.Thread `json:"~thread,omitempty"` 55 Connection *Connection `json:"connection,omitempty"` 56 } 57 58 // Response defines a2a Connection response 59 // https://github.com/hyperledger/aries-rfcs/tree/main/features/0160-connection-protocol#2-connection-response 60 type Response struct { 61 Type string `json:"@type,omitempty"` 62 ID string `json:"@id,omitempty"` 63 ConnectionSignature *ConnectionSignature `json:"connection~sig,omitempty"` 64 Thread *decorator.Thread `json:"~thread,omitempty"` 65 PleaseAck *PleaseAck `json:"~please_ack,omitempty"` 66 } 67 68 // ConnectionSignature connection signature. 69 type ConnectionSignature struct { 70 Type string `json:"@type,omitempty"` 71 Signature string `json:"signature,omitempty"` 72 SignedData string `json:"sig_data,omitempty"` 73 SignVerKey string `json:"signer,omitempty"` 74 } 75 76 // PleaseAck connection response accepted acknowledgement. 77 type PleaseAck struct { 78 On []string `json:"on,omitempty"` 79 } 80 81 // Connection defines connection body of connection request. 82 type Connection struct { 83 DID string `json:"DID,omitempty"` 84 DIDDoc *did.Doc `json:"DIDDoc,omitempty"` 85 } 86 87 type legacyConnection struct { 88 DID string `json:"DID,omitempty"` 89 DIDDoc *legacyDoc `json:"DIDDoc,omitempty"` 90 } 91 92 type legacyDoc struct { 93 Context interface{} `json:"@context,omitempty"` 94 ID string `json:"id,omitempty"` 95 AlsoKnownAs []interface{} `json:"alsoKnownAs,omitempty"` 96 VerificationMethod []map[string]interface{} `json:"verificationMethod,omitempty"` 97 PublicKey []map[string]interface{} `json:"publicKey,omitempty"` 98 Service []map[string]interface{} `json:"service,omitempty"` 99 Authentication []interface{} `json:"authentication,omitempty"` 100 AssertionMethod []interface{} `json:"assertionMethod,omitempty"` 101 CapabilityDelegation []interface{} `json:"capabilityDelegation,omitempty"` 102 CapabilityInvocation []interface{} `json:"capabilityInvocation,omitempty"` 103 KeyAgreement []interface{} `json:"keyAgreement,omitempty"` 104 Created *time.Time `json:"created,omitempty"` 105 Updated *time.Time `json:"updated,omitempty"` 106 Proof []interface{} `json:"proof,omitempty"` 107 } 108 109 // JSONBytes converts Connection to json bytes. 110 func (con *Connection) toLegacyJSONBytes() ([]byte, error) { 111 if con.DIDDoc == nil { 112 return nil, fmt.Errorf("DIDDoc field cannot be empty") 113 } 114 115 legDoc, err := con.DIDDoc.ToLegacyRawDoc() 116 if err != nil { 117 return nil, fmt.Errorf("converting to Legacy Raw Doc failed: %w", err) 118 } 119 120 connDoc := &legacyDoc{} 121 122 _ = mapstructure.Decode(legDoc, connDoc) //nolint: errcheck 123 124 conRaw := legacyConnection{ 125 DID: con.DID, 126 DIDDoc: connDoc, 127 } 128 129 byteConn, err := json.Marshal(conRaw) 130 if err != nil { 131 return nil, fmt.Errorf("JSON marshalling of connection raw failed: %w", err) 132 } 133 134 return byteConn, nil 135 } 136 137 // ParseConnection creates an instance of Connection by reading a JSON connection from bytes. 138 func parseLegacyJSONBytes(data []byte) (*Connection, error) { 139 connRaw := &legacyConnection{} 140 141 err := json.Unmarshal(data, &connRaw) 142 if err != nil { 143 return nil, fmt.Errorf("JSON umarshalling of connection data bytes failed: %w", err) 144 } else if connRaw.DIDDoc == nil { 145 return nil, errors.New("connection DIDDoc field is missed") 146 } 147 148 docRaw, err := json.Marshal(connRaw.DIDDoc) 149 if err != nil { 150 return nil, fmt.Errorf("JSON marshaling failed: %w", err) 151 } 152 153 doc, err := did.ParseDocument(docRaw) 154 if err != nil { 155 return nil, fmt.Errorf("parcing did document failed: %w", err) 156 } 157 158 return &Connection{ 159 DID: connRaw.DID, 160 DIDDoc: doc, 161 }, nil 162 }