Skip to content

Commit 8116479

Browse files
authored
feat: add tunnelsdk methods to get Key from Noise*Key (#4)
1 parent 5ea153d commit 8116479

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

tunnelsdk/tunnel.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,35 @@ func ParsePrivateKey(key string) (Key, error) {
7272
}, nil
7373
}
7474

75+
// ParsePublicKey parses a public key generated using key.String().
76+
func ParsePublicKey(key string) (Key, error) {
77+
k, err := wgtypes.ParseKey(key)
78+
if err != nil {
79+
return Key{}, err
80+
}
81+
82+
return Key{
83+
k: k,
84+
isPrivate: false,
85+
}, nil
86+
}
87+
88+
// FromNoisePrivateKey converts a device.NoisePrivateKey to a Key.
89+
func FromNoisePrivateKey(k device.NoisePrivateKey) Key {
90+
return Key{
91+
k: wgtypes.Key(k),
92+
isPrivate: true,
93+
}
94+
}
95+
96+
// FromNoisePublicKey converts a device.NoisePublicKey to a Key.
97+
func FromNoisePublicKey(k device.NoisePublicKey) Key {
98+
return Key{
99+
k: wgtypes.Key(k),
100+
isPrivate: false,
101+
}
102+
}
103+
75104
// IsZero returns true if the Key is the zero value.
76105
func (k Key) IsZero() bool {
77106
return k.k == wgtypes.Key{}

0 commit comments

Comments
 (0)