go-waku/waku/v2/protocol/noise/qr.go

93 lines
2.4 KiB
Go
Raw Normal View History

2022-12-14 16:22:48 +00:00
package noise
import (
"crypto/ed25519"
"encoding/base64"
2022-12-14 16:22:48 +00:00
"errors"
"strings"
)
type QR struct {
applicationName string
applicationVersion string
shardID string
2022-12-14 22:10:08 +00:00
ephemeralPublicKey ed25519.PublicKey
2022-12-14 16:22:48 +00:00
committedStaticKey []byte
}
func NewQR(applicationName, applicationVersion, shardID string, ephemeralKey ed25519.PublicKey, committedStaticKey []byte) QR {
2022-12-14 16:22:48 +00:00
return QR{
applicationName: applicationName,
applicationVersion: applicationVersion,
shardID: shardID,
2022-12-14 22:10:08 +00:00
ephemeralPublicKey: ephemeralKey,
2022-12-14 16:22:48 +00:00
committedStaticKey: committedStaticKey,
}
}
// Serializes input parameters to a base64 string for exposure through QR code (used by WakuPairing)
func (qr QR) String() string {
return base64.URLEncoding.EncodeToString([]byte(qr.applicationName)) + ":" +
base64.URLEncoding.EncodeToString([]byte(qr.applicationVersion)) + ":" +
base64.URLEncoding.EncodeToString([]byte(qr.shardID)) + ":" +
base64.URLEncoding.EncodeToString(qr.ephemeralPublicKey) + ":" +
base64.URLEncoding.EncodeToString(qr.committedStaticKey[:])
2022-12-14 16:22:48 +00:00
}
func (qr QR) Bytes() []byte {
return []byte(qr.String())
}
func decodeBase64String(inputValue string) ([]byte, error) {
enc := base64.StdEncoding
if strings.ContainsAny(inputValue, "-_") {
enc = base64.URLEncoding
}
if len(inputValue)%4 != 0 {
enc = enc.WithPadding(base64.NoPadding)
}
return enc.DecodeString(inputValue)
}
// StringToQR deserializes input string in base64 to the corresponding (applicationName, applicationVersion, shardId, ephemeralKey, committedStaticKey)
2022-12-14 16:22:48 +00:00
func StringToQR(qrString string) (QR, error) {
values := strings.Split(qrString, ":")
if len(values) != 5 {
return QR{}, errors.New("invalid qr string")
}
applicationName, err := decodeBase64String(values[0])
2022-12-14 16:22:48 +00:00
if err != nil {
return QR{}, err
}
applicationVersion, err := decodeBase64String(values[1])
2022-12-14 16:22:48 +00:00
if err != nil {
return QR{}, err
}
shardID, err := decodeBase64String(values[2])
2022-12-14 16:22:48 +00:00
if err != nil {
return QR{}, err
}
ephemeralKey, err := decodeBase64String(values[3])
2022-12-14 16:22:48 +00:00
if err != nil {
return QR{}, err
}
committedStaticKey, err := decodeBase64String(values[4])
2022-12-14 16:22:48 +00:00
if err != nil {
return QR{}, err
}
return QR{
applicationName: string(applicationName),
applicationVersion: string(applicationVersion),
shardID: string(shardID),
2022-12-14 22:10:08 +00:00
ephemeralPublicKey: ephemeralKey,
2022-12-14 16:22:48 +00:00
committedStaticKey: committedStaticKey,
}, nil
}