2021-07-18 23:03:28 +01:00
|
|
|
/*
|
2026-02-14 02:34:09 +05:00
|
|
|
* Copyright (c) 2026 Kaiy Ragur
|
|
|
|
|
* Original Copyright (c) 2021 Neil Alexander
|
2021-07-18 23:03:28 +01:00
|
|
|
*
|
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
|
|
|
|
|
2021-07-08 23:12:20 +01:00
|
|
|
package utils
|
2021-07-07 18:15:07 +01:00
|
|
|
|
|
|
|
|
import (
|
2021-07-09 00:08:26 +01:00
|
|
|
"crypto/ed25519"
|
2026-02-14 02:34:09 +05:00
|
|
|
"encoding/base32"
|
2021-07-07 18:15:07 +01:00
|
|
|
"fmt"
|
|
|
|
|
"strings"
|
|
|
|
|
)
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func CreateAddress(aPk, dPk ed25519.PublicKey) string {
|
2021-07-09 00:08:26 +01:00
|
|
|
return fmt.Sprintf(
|
|
|
|
|
"%s@%s",
|
2026-02-14 02:34:09 +05:00
|
|
|
EncodeString(aPk), EncodeString(dPk),
|
2021-07-09 00:08:26 +01:00
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-14 02:34:09 +05:00
|
|
|
func EncodeString(k []byte) string {
|
|
|
|
|
return strings.ToLower(base32.HexEncoding.WithPadding(base32.NoPadding).EncodeToString(k))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func DecodeString(s string) ([]byte, error) {
|
|
|
|
|
pk, err := base32.HexEncoding.WithPadding(base32.NoPadding).DecodeString(strings.ToUpper(s))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, fmt.Errorf("hex.DecodeString: %w", err)
|
|
|
|
|
}
|
|
|
|
|
if len(pk) == 0 {
|
|
|
|
|
return nil, fmt.Errorf("utils.DecodeStrings: len %d", len(pk))
|
|
|
|
|
}
|
|
|
|
|
return pk, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ParseAddress(email string) (ed25519.PublicKey, ed25519.PublicKey, error) {
|
2021-07-07 18:15:07 +01:00
|
|
|
at := strings.LastIndex(email, "@")
|
|
|
|
|
if at == 0 {
|
2026-02-14 02:34:09 +05:00
|
|
|
return nil, nil, fmt.Errorf("invalid email address")
|
2021-07-09 00:08:26 +01:00
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
// Domain key
|
|
|
|
|
host := email[at+1:]
|
|
|
|
|
hostPk, err := DecodeString(host)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, fmt.Errorf("invalid email domain: %w", err)
|
|
|
|
|
} else if len(hostPk) != ed25519.PublicKeySize {
|
|
|
|
|
return nil, nil, fmt.Errorf("invalid email domain: invalid keysize")
|
2021-07-09 00:08:26 +01:00
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
// Account key
|
|
|
|
|
user := email[:at]
|
|
|
|
|
userPk, err := DecodeString(user)
|
2021-07-09 00:08:26 +01:00
|
|
|
if err != nil {
|
2026-02-14 02:34:09 +05:00
|
|
|
return nil, nil, fmt.Errorf("invalid email account: %w", err)
|
|
|
|
|
} else if len(userPk) != ed25519.PublicKeySize {
|
|
|
|
|
return nil, nil, fmt.Errorf("invalid email account: invalid keysize")
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|
2026-02-14 02:34:09 +05:00
|
|
|
return hostPk, userPk, nil
|
2021-07-07 18:15:07 +01:00
|
|
|
}
|