package main

import (
	"golang.zx2c4.com/wireguard/wgctrl/wgtypes"
)

var wgKeys = KVSecrets[string]{"wireguard"}

func wgKey(path string) (key string, err error) {
	return wgKeys.GetOrCreate(path, func() (key string, err error) {
		k, err := wgtypes.GeneratePrivateKey()
		if err != nil {
			return
		}
		key = k.String()
		return
	})
}

func wgPSKey(path string) (key string, err error) {
	return wgKeys.GetOrCreate(path, func() (key string, err error) {
		k, err := wgtypes.GenerateKey()
		if err != nil {
			return
		}
		key = k.String()
		return
	})
}

func wgPubKey(path string) (pubkey string, err error) {
	key, err := wgKey(path)
	if err != nil {
		return
	}

	k, err := wgtypes.ParseKey(key)
	if err != nil {
		return
	}

	pubkey = k.PublicKey().String()
	return
}
