github.com/mysteriumnetwork/node@v0.0.0-20240516044423-365054f76801/p2p/contact.go (about) 1 /* 2 * Copyright (C) 2020 The "MysteriumNetwork/node" Authors. 3 * 4 * This program is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * This program is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 18 package p2p 19 20 import ( 21 "encoding/json" 22 "errors" 23 "fmt" 24 25 "github.com/mysteriumnetwork/node/market" 26 ) 27 28 // ErrContactNotFound represents that no p2p contact is found. 29 var ErrContactNotFound = errors.New("p2p contact not found") 30 31 const ( 32 // ContactTypeV1 is p2p contact type. 33 ContactTypeV1 = "nats/p2p/v1" 34 ) 35 36 // ContactDefinition represents p2p contact which contains NATS broker addresses for connection. 37 type ContactDefinition struct { 38 BrokerAddresses []string `json:"broker_addresses"` 39 } 40 41 // ParseContact tries to parse p2p contact from given contacts list. 42 func ParseContact(contacts market.ContactList) (ContactDefinition, error) { 43 for _, c := range contacts { 44 if c.Type == ContactTypeV1 { 45 def, ok := c.Definition.(ContactDefinition) 46 if !ok { 47 return ContactDefinition{}, fmt.Errorf("invalid p2p contact definition: %#v", c.Definition) 48 } 49 return def, nil 50 } 51 } 52 return ContactDefinition{}, ErrContactNotFound 53 } 54 55 // RegisterContactUnserializer registers global proposal contact unserializer. 56 func RegisterContactUnserializer() { 57 market.RegisterContactUnserializer( 58 ContactTypeV1, 59 func(rawDefinition *json.RawMessage) (market.ContactDefinition, error) { 60 var contact ContactDefinition 61 err := json.Unmarshal(*rawDefinition, &contact) 62 return contact, err 63 }, 64 ) 65 }