Skip to content

Commit 42a1e35

Browse files
committed
Split ST and public key logic RIVM-106
1 parent 3ec7952 commit 42a1e35

File tree

17 files changed

+133
-102
lines changed

17 files changed

+133
-102
lines changed

cmd/genkeys/main.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
/*
2-
32
This file generates crypto keys.
43
It prints out a new set of keys each time if finds a "better" one.
54
By default, "better" means a higher NodeID (-> higher IP address).
@@ -8,7 +7,6 @@ This is because the IP address format can compress leading 1s in the address, to
87
If run with the "-sig" flag, it generates signing keys instead.
98
A "better" signing key means one with a higher TreeID.
109
This only matters if it's high enough to make you the root of the tree.
11-
1210
*/
1311
package main
1412

@@ -22,6 +20,7 @@ import (
2220

2321
"github.com/gologme/log"
2422

23+
iwt "github.com/Arceliar/ironwood/types"
2524
c "github.com/RiV-chain/RiV-mesh/src/core"
2625
)
2726

@@ -46,7 +45,7 @@ func main() {
4645
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
4746
logger := log.New(os.Stdout, "", log.Flags())
4847
core, _ := c.New(newKey.priv, logger, nil)
49-
addr := core.AddrForKey(newKey.pub)
48+
addr := core.AddrForDomain(iwt.Domain("example"))
5049
fmt.Println("IP:", net.IP(addr[:]).String())
5150
}
5251
}

cmd/mesh/main.go

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package main
22

33
import (
4-
"crypto/ed25519"
54
"encoding/hex"
65
"encoding/json"
76
"flag"
@@ -31,6 +30,8 @@ import (
3130
"github.com/RiV-chain/RiV-mesh/src/restapi"
3231
"github.com/RiV-chain/RiV-mesh/src/tun"
3332
"github.com/RiV-chain/RiV-mesh/src/version"
33+
34+
iwt "github.com/Arceliar/ironwood/types"
3435
)
3536

3637
type node struct {
@@ -203,23 +204,23 @@ func run(args rivArgs, sigCh chan os.Signal) {
203204
}
204205
n := &node{}
205206
// Have we been asked for the node address yet? If so, print it and then stop.
206-
getNodeKey := func() ed25519.PublicKey {
207-
if pubkey, err := hex.DecodeString(cfg.PrivateKey); err == nil {
208-
return ed25519.PrivateKey(pubkey).Public().(ed25519.PublicKey)
207+
getDomain := func() iwt.Domain {
208+
if d, err := hex.DecodeString(cfg.Domain); err == nil {
209+
return d
209210
}
210211
return nil
211212
}
212213
switch {
213214
case args.getaddr:
214-
if key := getNodeKey(); key != nil {
215-
addr := n.core.AddrForKey(key)
215+
if domain := getDomain(); domain != nil {
216+
addr := n.core.AddrForDomain(domain)
216217
ip := net.IP(addr[:])
217218
fmt.Println(ip.String())
218219
}
219220
return
220221
case args.getsnet:
221-
if key := getNodeKey(); key != nil {
222-
snet := n.core.SubnetForKey(key)
222+
if domain := getDomain(); domain != nil {
223+
snet := n.core.SubnetForDomain(domain)
223224
ipnet := net.IPNet{
224225
IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0),
225226
Mask: net.CIDRMask(len(snet)*8, 128),
@@ -324,7 +325,7 @@ func run(args rivArgs, sigCh chan os.Signal) {
324325
// This is just logged to stdout for the user.
325326
address := n.core.Address()
326327
subnet := n.core.Subnet()
327-
public := n.core.GetSelf().Key
328+
public := n.core.GetSelf().Domain
328329
logger.Infof("Your public key is %s", hex.EncodeToString(public[:]))
329330
logger.Infof("Your IPv6 address is %s", address.String())
330331
logger.Infof("Your IPv6 subnet is %s", subnet.String())

contrib/mobile/mobile.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ func (m *Mesh) GetSubnetString() string {
216216

217217
// GetPublicKeyString gets the node's public key in hex form
218218
func (m *Mesh) GetPublicKeyString() string {
219-
return hex.EncodeToString(m.core.GetSelf().Key)
219+
return hex.EncodeToString(m.core.GetSelf().Domain)
220220
}
221221

222222
// GetCoordsString gets the node's coordinates
@@ -230,7 +230,7 @@ func (m *Mesh) GetPeersJSON() (result string) {
230230
IP string
231231
}{}
232232
for _, v := range m.core.GetPeers() {
233-
a := m.core.AddrForKey(v.Key)
233+
a := m.core.AddrForDomain(v.Domain)
234234
ip := net.IP(a[:]).String()
235235
peers = append(peers, struct {
236236
core.PeerInfo

go.mod

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ module github.com/RiV-chain/RiV-mesh
22

33
go 1.18
44

5+
replace github.com/Arceliar/ironwood => github.com/RiV-chain/ironwood v0.0.0-20230806102314-bd0750e3fe00
6+
57
require (
6-
github.com/Arceliar/ironwood v0.0.0-20221115123222-ec61cea2f439
8+
github.com/Arceliar/ironwood v0.0.0-20230805085300-86206813435f
79
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979
810
github.com/getlantern/multipath v0.0.0-20220920195041-55195f38df73
911
github.com/gologme/log v1.2.0
@@ -41,7 +43,7 @@ require (
4143
github.com/getlantern/hidden v0.0.0-20190325191715-f02dbb02be55 // indirect
4244
github.com/getlantern/ops v0.0.0-20190325191751-d70cb0d6f85f // indirect
4345
github.com/go-stack/stack v1.8.0 // indirect
44-
github.com/google/uuid v1.1.2 // indirect
46+
github.com/google/uuid v1.3.0
4547
github.com/libp2p/go-buffer-pool v0.0.2 // indirect
4648
github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c // indirect
4749
github.com/rivo/uniseg v0.3.4 // indirect

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
gerace.dev/zipfs v0.2.0 h1:3U1GsasLdxGVhf7lCYbccsH4mpuSpMpLOy37GmfT+KY=
22
gerace.dev/zipfs v0.2.0/go.mod h1:L6cxA6m8uVfWDawsBHnki/J6Gos5uSlu/6RJZhafrfQ=
3-
github.com/Arceliar/ironwood v0.0.0-20221115123222-ec61cea2f439 h1:eOW6/XIs06TnUn9GPCnfv71CQZw8edP3u3mH3lZt6iM=
4-
github.com/Arceliar/ironwood v0.0.0-20221115123222-ec61cea2f439/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk=
53
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979 h1:WndgpSW13S32VLQ3ugUxx2EnnWmgba1kCqPkd4Gk1yQ=
64
github.com/Arceliar/phony v0.0.0-20210209235338-dde1a8dca979/go.mod h1:6Lkn+/zJilRMsKmbmG1RPoamiArC6HS73xbwRyp3UyI=
5+
github.com/RiV-chain/ironwood v0.0.0-20230806102314-bd0750e3fe00 h1:XeRRkIRhDN0zITv6yx9GkqZ0PQ9WXIfL2dINlhgk+UQ=
6+
github.com/RiV-chain/ironwood v0.0.0-20230806102314-bd0750e3fe00/go.mod h1:RP72rucOFm5udrnEzTmIWLRVGQiV/fSUAQXJ0RST/nk=
77
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
88
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
99
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -33,6 +33,8 @@ github.com/gologme/log v1.2.0 h1:Ya5Ip/KD6FX7uH0S31QO87nCCSucKtF44TLbTtO7V4c=
3333
github.com/gologme/log v1.2.0/go.mod h1:gq31gQ8wEHkR+WekdWsqDuf8pXTUZA9BnnzTuPz1Y9U=
3434
github.com/google/uuid v1.1.2 h1:EVhdT+1Kseyi1/pUmXKaFxYsDNy9RQYkMWRH68J/W7Y=
3535
github.com/google/uuid v1.1.2/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
36+
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
37+
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
3638
github.com/hashicorp/go-syslog v1.0.0 h1:KaodqZuhUoZereWVIYmpUgZysurB1kBLX2j0MwMrUAE=
3739
github.com/hashicorp/go-syslog v1.0.0/go.mod h1:qPfqrKkXGihmCqbJM2mZgkZGvKG1dFdvsLplgctolz4=
3840
github.com/hjson/hjson-go v3.1.0+incompatible h1:DY/9yE8ey8Zv22bY+mHV1uk2yRy0h8tKhZ77hEdi0Aw=

src/config/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type NodeConfig struct {
3535
AllowedPublicKeys []string `comment:"List of peer public keys to allow incoming peering connections\nfrom. If left empty/undefined then all connections will be allowed\nby default. This does not affect outgoing peerings, nor does it\naffect link-local peers discovered via multicast."`
3636
PublicKey string `comment:"Your public key. Your peers may ask you for this to put\ninto their AllowedPublicKeys configuration."`
3737
PrivateKey string `comment:"Your private key. DO NOT share this with anyone!"`
38+
Domain string `comment:"Your domain. Should be registered with consensus algo."`
3839
IfName string `comment:"Local network interface name for TUN adapter, or \"auto\" to select\nan interface automatically, or \"none\" to run without TUN."`
3940
IfMTU uint64 `comment:"Maximum Transmission Unit (MTU) size for your local TUN interface.\nDefault is the largest supported size for your platform. The lowest\npossible value is 1280."`
4041
NodeInfoPrivacy bool `comment:"By default, nodeinfo contains some defaults including the platform,\narchitecture and RiV-mesh version. These can help when surveying\nthe network and diagnosing network routing problems. Enabling\nnodeinfo privacy prevents this, so that only items specified in\n\"NodeInfo\" are sent back if specified."`

src/core/address.go

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package core
55
import (
66
"crypto/ed25519"
77
"encoding/hex"
8+
9+
iwt "github.com/Arceliar/ironwood/types"
810
)
911

1012
// Address represents an IPv6 address in the mesh address range.
@@ -50,23 +52,23 @@ func (c *Core) IsValidSubnet(s Subnet) bool {
5052
return s[l-1] == prefix[l-1]|0x01
5153
}
5254

53-
// AddrForKey takes an ed25519.PublicKey as an argument and returns an *Address.
54-
// This function returns nil if the key length is not ed25519.PublicKeySize.
55+
// AddrForDomain takes a Domain as an argument and returns an *Address.
56+
// This function returns nil if the Domain length is greater ed25519.PublicKeySize.
5557
// This address begins with the contents of GetPrefix(), with the last bit set to 0 to indicate an address.
5658
// The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the public key.
5759
// The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the address.
58-
func (c *Core) AddrForKey(publicKey ed25519.PublicKey) *Address {
60+
func (c *Core) AddrForDomain(domain iwt.Domain) *Address {
5961
// 128 bit address
6062
// Begins with prefix
6163
// Next bit is a 0
6264
// Next 7 bits, interpreted as a uint, are # of leading 1s in the NodeID
6365
// Leading 1s and first leading 0 of the NodeID are truncated off
6466
// The rest is appended to the IPv6 address (truncated to 128 bits total)
65-
if len(publicKey) != ed25519.PublicKeySize {
67+
if len(domain) > ed25519.PublicKeySize {
6668
return nil
6769
}
6870
var buf [ed25519.PublicKeySize]byte
69-
copy(buf[:], publicKey)
71+
copy(buf[:], domain)
7072
for idx := range buf {
7173
buf[idx] = ^buf[idx]
7274
}
@@ -100,29 +102,28 @@ func (c *Core) AddrForKey(publicKey ed25519.PublicKey) *Address {
100102
return &addr
101103
}
102104

103-
// SubnetForKey takes an ed25519.PublicKey as an argument and returns a *Subnet.
104-
// This function returns nil if the key length is not ed25519.PublicKeySize.
105+
// SubnetForDomain takes a Domain as an argument and returns a *Subnet.
106+
// This function returns nil if the Domain length is greater ed25519.PublicKeySize.
105107
// The subnet begins with the address prefix, with the last bit set to 1 to indicate a prefix.
106108
// The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the key.
107109
// The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the subnet.
108-
func (c *Core) SubnetForKey(publicKey ed25519.PublicKey) *Subnet {
110+
func (c *Core) SubnetForDomain(domain iwt.Domain) *Subnet {
109111
// Exactly as the address version, with two exceptions:
110112
// 1) The first bit after the fixed prefix is a 1 instead of a 0
111113
// 2) It's truncated to a subnet prefix length instead of 128 bits
112-
addr := c.AddrForKey(publicKey)
114+
addr := c.AddrForDomain(domain)
113115
if addr == nil {
114116
return nil
115117
}
116118
var snet Subnet
117119
copy(snet[:], addr[:])
118-
prefix := c.GetPrefix() // nolint:staticcheck
119-
snet[len(prefix)-1] |= 0x01
120+
snet[len(c.GetPrefix())-1] |= 0x01
120121
return &snet
121122
}
122123

123-
// GetKet returns the partial ed25519.PublicKey for the Address.
124+
// GetKet returns the partial Domain for the Address.
124125
// This is used for key lookup.
125-
func (c *Core) GetAddressKey(a Address) ed25519.PublicKey {
126+
func (c *Core) GetAddressKey(a Address) iwt.Domain {
126127
var key [ed25519.PublicKeySize]byte
127128
prefix := c.GetPrefix() // nolint:staticcheck
128129
ones := int(a[len(prefix)])
@@ -145,12 +146,12 @@ func (c *Core) GetAddressKey(a Address) ed25519.PublicKey {
145146
for idx := range key {
146147
key[idx] = ^key[idx]
147148
}
148-
return ed25519.PublicKey(key[:])
149+
return iwt.Domain(key[:])
149150
}
150151

151-
// GetKet returns the partial ed25519.PublicKey for the Subnet.
152+
// GetKet returns the partial Domain for the Subnet.
152153
// This is used for key lookup.
153-
func (c *Core) GetSubnetKey(s Subnet) ed25519.PublicKey {
154+
func (c *Core) GetSubnetKey(s Subnet) iwt.Domain {
154155
var addr Address
155156
copy(addr[:], s[:])
156157
return c.GetAddressKey(addr)

src/core/address_test.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"crypto/ed25519"
66
"math/rand"
77
"testing"
8+
9+
iwt "github.com/Arceliar/ironwood/types"
810
)
911

1012
func (c *Core) TestAddress_Address_IsValid(t *testing.T) {
@@ -54,7 +56,7 @@ func (c *Core) TestAddress_Subnet_IsValid(t *testing.T) {
5456
}
5557

5658
func (c *Core) TestAddress_AddrForKey(t *testing.T) {
57-
publicKey := ed25519.PublicKey{
59+
publicKey := iwt.Domain{
5860
189, 186, 207, 216, 34, 64, 222, 61, 205, 18, 57, 36, 203, 181, 82, 86,
5961
251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251,
6062
}
@@ -63,20 +65,20 @@ func (c *Core) TestAddress_AddrForKey(t *testing.T) {
6365
0xfc, 0, 132, 138, 96, 79, 187, 126, 67, 132, 101, 219, 141, 182, 104, 149,
6466
}
6567

66-
if *c.AddrForKey(publicKey) != expectedAddress {
68+
if *c.AddrForDomain(publicKey) != expectedAddress {
6769
t.Fatal("invalid address returned")
6870
}
6971
}
7072

7173
func (c *Core) TestAddress_SubnetForKey(t *testing.T) {
72-
publicKey := ed25519.PublicKey{
74+
publicKey := iwt.Domain{
7375
189, 186, 207, 216, 34, 64, 222, 61, 205, 18, 57, 36, 203, 181, 82, 86,
7476
251, 141, 171, 8, 170, 152, 227, 5, 82, 138, 184, 79, 65, 158, 110, 251,
7577
}
7678

7779
expectedSubnet := Subnet{0xfd, 0, 132, 138, 96, 79, 187, 126}
7880

79-
if *c.SubnetForKey(publicKey) != expectedSubnet {
81+
if *c.SubnetForDomain(publicKey) != expectedSubnet {
8082
t.Fatal("invalid subnet returned")
8183
}
8284
}

0 commit comments

Comments
 (0)