forked from hyperledger-labs/go-perun
-
Notifications
You must be signed in to change notification settings - Fork 2
/
address.go
104 lines (89 loc) · 2.77 KB
/
address.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
// Copyright 2019 - See NOTICE file for copyright holders.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package wire
import (
stdio "io"
"perun.network/go-perun/wallet"
"perun.network/go-perun/wire/perunio"
)
var (
_ perunio.Serializer = (*Addresses)(nil)
_ perunio.Serializer = (*AddressesWithLen)(nil)
)
// Address is a Perun node's network address, which is used as a permanent
// identity within the Perun peer-to-peer network. For now, it is based on type
// wallet.Address.
type Address interface {
wallet.Address
}
// Addresses is a helper type for encoding and decoding address slices in
// situations where the length of the slice is known.
type Addresses []Address
// AddressesWithLen is a helper type for encoding and decoding address slices
// of unknown length.
type AddressesWithLen []Address
// NewAddress returns a variable of type Address, which can be used
// for unmarshalling an address from its binary representation.
func NewAddress() Address {
return wallet.NewAddress()
}
// Encode encodes wire addresses.
func (a Addresses) Encode(w stdio.Writer) error {
return wallet.Addresses(asWalletAddresses(a)).Encode(w)
}
// Encode encodes wire addresses with length.
func (a AddressesWithLen) Encode(w stdio.Writer) error {
return wallet.AddressesWithLen(asWalletAddresses(a)).Encode(w)
}
// asWalletAddresses converts wire addresses to wallet addresses.
func asWalletAddresses(a []Address) []wallet.Address {
b := make([]wallet.Address, len(a))
for i, x := range a {
b[i] = x
}
return b
}
// Decode decodes wallet addresses.
func (a Addresses) Decode(r stdio.Reader) error {
b := wallet.Addresses(make([]wallet.Address, len(a)))
if err := b.Decode(r); err != nil {
return err
}
for i, x := range b {
a[i] = x
}
return nil
}
// Decode decodes a wallet address slice of unknown length.
func (a *AddressesWithLen) Decode(r stdio.Reader) error {
var b wallet.AddressesWithLen
if err := b.Decode(r); err != nil {
return err
}
*a = make(AddressesWithLen, len(b))
for i, x := range b {
(*a)[i] = x
}
return nil
}
// IndexOfAddr returns the index of the given address in the address slice,
// or -1 if it is not part of the slice.
func IndexOfAddr(addrs []Address, addr Address) int {
for i, a := range addrs {
if addr.Equal(a) {
return i
}
}
return -1
}