-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
42 lines (34 loc) · 889 Bytes
/
main.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
//go:build js && wasm
package main
import (
"syscall/js"
"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)
func main() {
wait := make(chan struct{}, 0)
js.Global().Set("wgCtrl", js.ValueOf(map[string]interface{}{
"genKeyPair": js.FuncOf(genKeyPair),
"genPreKey": js.FuncOf(genPreKey),
}))
<-wait
}
func genPreKey(this js.Value, args []js.Value) any {
preKey, _ := wgtypes.GenerateKey()
return js.ValueOf(preKey.String())
}
func genKeyPair(this js.Value, args []js.Value) any {
if len(args) > 0 {
key, err := wgtypes.ParseKey(args[0].String())
if err != nil {
return js.ValueOf(
map[string]any{
"error": err.Error(),
},
)
}
return js.ValueOf([]any{key.String(), key.PublicKey().String()})
}
privateKey, _ := wgtypes.GeneratePrivateKey()
publicKey := privateKey.PublicKey()
return js.ValueOf([]any{privateKey.String(), publicKey.String()})
}