|
| 1 | +// Copyright © 2025 Attestant Limited. |
| 2 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +// you may not use this file except in compliance with the License. |
| 4 | +// You may obtain a copy of the License at |
| 5 | +// |
| 6 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | +// |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | + |
| 14 | +package v1 |
| 15 | + |
| 16 | +import ( |
| 17 | + "encoding/json" |
| 18 | + "fmt" |
| 19 | + |
| 20 | + "github.com/pkg/errors" |
| 21 | +) |
| 22 | + |
| 23 | +// NodeIdentity contains the node identity information. |
| 24 | +type NodeIdentity struct { |
| 25 | + PeerID string `json:"peer_id"` |
| 26 | + Enr string `json:"enr"` |
| 27 | + P2PAddresses []string `json:"p2p_addresses"` |
| 28 | + DiscoveryAddresses []string `json:"discovery_addresses"` |
| 29 | + Metadata map[string]string `json:"metadata"` |
| 30 | +} |
| 31 | + |
| 32 | +type nodeIdentityJSON struct { |
| 33 | + PeerID string `json:"peer_id"` |
| 34 | + Enr string `json:"enr"` |
| 35 | + P2PAddresses []string `json:"p2p_addresses"` |
| 36 | + DiscoveryAddresses []string `json:"discovery_addresses"` |
| 37 | + Metadata map[string]string `json:"metadata"` |
| 38 | +} |
| 39 | + |
| 40 | +// MarshalJSON implements json.Marshaler. |
| 41 | +func (n *NodeIdentity) MarshalJSON() ([]byte, error) { |
| 42 | + return json.Marshal(&nodeIdentityJSON{ |
| 43 | + PeerID: n.PeerID, |
| 44 | + Enr: n.Enr, |
| 45 | + P2PAddresses: n.P2PAddresses, |
| 46 | + DiscoveryAddresses: n.DiscoveryAddresses, |
| 47 | + Metadata: n.Metadata, |
| 48 | + }) |
| 49 | +} |
| 50 | + |
| 51 | +// UnmarshalJSON implements json.Unmarshaler. |
| 52 | +func (n *NodeIdentity) UnmarshalJSON(input []byte) error { |
| 53 | + var nodeIdentityJSON nodeIdentityJSON |
| 54 | + |
| 55 | + if err := json.Unmarshal(input, &nodeIdentityJSON); err != nil { |
| 56 | + return errors.Wrap(err, "invalid JSON") |
| 57 | + } |
| 58 | + |
| 59 | + n.PeerID = nodeIdentityJSON.PeerID |
| 60 | + n.Enr = nodeIdentityJSON.Enr |
| 61 | + n.P2PAddresses = nodeIdentityJSON.P2PAddresses |
| 62 | + n.DiscoveryAddresses = nodeIdentityJSON.DiscoveryAddresses |
| 63 | + n.Metadata = nodeIdentityJSON.Metadata |
| 64 | + |
| 65 | + return nil |
| 66 | +} |
| 67 | + |
| 68 | +func (n *NodeIdentity) String() string { |
| 69 | + data, err := json.Marshal(n) |
| 70 | + if err != nil { |
| 71 | + return fmt.Sprintf("ERR: %v", err) |
| 72 | + } |
| 73 | + |
| 74 | + return string(data) |
| 75 | +} |
0 commit comments