2022-12-14 16:22:48 +00:00
|
|
|
package noise
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ed25519"
|
2023-02-20 16:23:25 +00:00
|
|
|
"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 {
|
|
|
|
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 {
|
2023-02-20 16:23:25 +00:00
|
|
|
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())
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2022-12-14 16:22:48 +00:00
|
|
|
// Deserializes input string in base64 to the corresponding (applicationName, applicationVersion, shardId, ephemeralKey, committedStaticKey)
|
|
|
|
func StringToQR(qrString string) (QR, error) {
|
|
|
|
values := strings.Split(qrString, ":")
|
|
|
|
if len(values) != 5 {
|
|
|
|
return QR{}, errors.New("invalid qr string")
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
applicationName, err := decodeBase64String(values[0])
|
2022-12-14 16:22:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return QR{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
applicationVersion, err := decodeBase64String(values[1])
|
2022-12-14 16:22:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return QR{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
shardId, err := decodeBase64String(values[2])
|
2022-12-14 16:22:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return QR{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
ephemeralKey, err := decodeBase64String(values[3])
|
2022-12-14 16:22:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return QR{}, err
|
|
|
|
}
|
|
|
|
|
2023-02-20 16:23:25 +00:00
|
|
|
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
|
|
|
|
}
|