/* * Copyright (c) 2026 Kaiy Ragur * Original Copyright (c) 2021 Neil Alexander * * 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/. */ package utils import ( "crypto/ed25519" "encoding/base32" "fmt" "strings" ) func CreateAddress(aPk, dPk ed25519.PublicKey) string { return fmt.Sprintf( "%s@%s", EncodeString(aPk), EncodeString(dPk), ) } 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) { at := strings.LastIndex(email, "@") if at == 0 { return nil, nil, fmt.Errorf("invalid email address") } // 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") } // Account key user := email[:at] userPk, err := DecodeString(user) if err != nil { 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") } return hostPk, userPk, nil }