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
2023-09-11 14:24:05 +00:00
shardID string
2022-12-14 22:10:08 +00:00
ephemeralPublicKey ed25519 . PublicKey
2022-12-14 16:22:48 +00:00
committedStaticKey [ ] byte
}
2023-09-11 14:24:05 +00:00
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 ,
2023-09-11 14:24:05 +00:00
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 ) ) + ":" +
2023-09-11 14:24:05 +00:00
base64 . URLEncoding . EncodeToString ( [ ] byte ( qr . shardID ) ) + ":" +
2023-02-20 16:23:25 +00:00
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 )
}
2023-09-11 14:24:05 +00:00
// 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" )
}
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-09-11 14:24:05 +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 ) ,
2023-09-11 14:24:05 +00:00
shardID : string ( shardID ) ,
2022-12-14 22:10:08 +00:00
ephemeralPublicKey : ephemeralKey ,
2022-12-14 16:22:48 +00:00
committedStaticKey : committedStaticKey ,
} , nil
}