428 lines
14 KiB
Go
428 lines
14 KiB
Go
// Code generated by zi2go from node_identity.zi. DO NOT EDIT.
|
|
package main
|
|
|
|
import (
|
|
hex "encoding/hex"
|
|
fmt "fmt"
|
|
sort "sort"
|
|
strconv "strconv"
|
|
strings "strings"
|
|
)
|
|
|
|
// #import protocol
|
|
// #import go_types
|
|
type NodeIdentity struct {
|
|
ID string
|
|
PublicKey PublicKey
|
|
PrivateKey PrivateKey
|
|
}
|
|
|
|
type NodeIdentityResult struct {
|
|
Value NodeIdentity
|
|
Error Error
|
|
}
|
|
|
|
type PrivateKeyResult struct {
|
|
Value PrivateKey
|
|
Error Error
|
|
}
|
|
|
|
type PublicKeyResult struct {
|
|
Value PublicKey
|
|
Error Error
|
|
}
|
|
|
|
type PairingInvite struct {
|
|
Version int "json:\"version\""
|
|
InviteID string "json:\"invite_id\""
|
|
NodeID string "json:\"node_id\""
|
|
PublicKey string "json:\"public_key\""
|
|
DisplayName string "json:\"display_name\""
|
|
Addresses []string "json:\"addresses\""
|
|
SpaceID string "json:\"space_id,omitempty\""
|
|
ExpiresAt int64 "json:\"expires_at\""
|
|
Nonce string "json:\"nonce\""
|
|
Policy NodeSyncPolicy "json:\"policy\""
|
|
Signature string "json:\"signature\""
|
|
}
|
|
|
|
type PairingAcceptance struct {
|
|
Version int "json:\"version\""
|
|
InviteID string "json:\"invite_id\""
|
|
NodeID string "json:\"node_id\""
|
|
PublicKey string "json:\"public_key\""
|
|
DisplayName string "json:\"display_name\""
|
|
Addresses []string "json:\"addresses\""
|
|
AcceptedAt int64 "json:\"accepted_at\""
|
|
Nonce string "json:\"nonce\""
|
|
Signature string "json:\"signature\""
|
|
}
|
|
|
|
type ServiceRecord struct {
|
|
Service string "json:\"service\""
|
|
Endpoints []string "json:\"endpoints\""
|
|
Capabilities []string "json:\"capabilities,omitempty\""
|
|
}
|
|
|
|
type NameClaim struct {
|
|
Version int "json:\"version\""
|
|
SpaceID string "json:\"space_id\""
|
|
Name string "json:\"name\""
|
|
NodeID string "json:\"node_id\""
|
|
Sequence int64 "json:\"sequence\""
|
|
ExpiresAt int64 "json:\"expires_at\""
|
|
Services []ServiceRecord "json:\"services,omitempty\""
|
|
Signature string "json:\"signature\""
|
|
}
|
|
|
|
type MeshTrustSpace struct {
|
|
SpaceID string "json:\"space_id\""
|
|
DisplayName string "json:\"display_name\""
|
|
AuthorityPublicKey string "json:\"authority_public_key\""
|
|
}
|
|
|
|
type HexResult struct {
|
|
Value []uint8
|
|
Error Error
|
|
}
|
|
|
|
func NodeIdentity_DecodeHex(Value string) HexResult {
|
|
var result HexResult
|
|
result.Value, result.Error = hex.DecodeString(Value)
|
|
return result
|
|
}
|
|
|
|
const MaximumInviteLifetimeSeconds = 86400
|
|
|
|
func NodeIdentity_Concat(left string, right string) string {
|
|
parts := [2]string{left, right}
|
|
_ = parts
|
|
return strings.Join(parts[0:2:2], "")
|
|
}
|
|
|
|
func NodeIdentity_CopyBytes(value []uint8) []uint8 {
|
|
output := make([]uint8, int(int(int64(len(value)))))
|
|
var index int64 = 0
|
|
for index < int64(len(value)) {
|
|
output[index] = value[index]
|
|
index++
|
|
}
|
|
return output
|
|
}
|
|
|
|
func NodeIdentity_SortedAddresses(addresses []string) string {
|
|
values := make([]string, int(int(int64(len(addresses)))))
|
|
var index int64 = 0
|
|
for index < int64(len(addresses)) {
|
|
values[index] = addresses[index]
|
|
index++
|
|
}
|
|
sort.Strings(values)
|
|
return strings.Join(values, ",")
|
|
}
|
|
|
|
func NodeIdentity_SignedMessage(key PrivateKey, message []uint8) string {
|
|
signature := Ed25519Go_Sign(key, message)
|
|
var value_0 string = TextGo_FromBytes(signature)
|
|
encoded := Codec_EncodeBase64(value_0, true, false)
|
|
return encoded.Value
|
|
}
|
|
|
|
func NodeIdentity_New(privateKey PrivateKey) NodeIdentityResult {
|
|
var result NodeIdentityResult = NodeIdentityResult{}
|
|
privateBytes := []uint8(privateKey)
|
|
if int64(len(privateBytes)) == 0 {
|
|
generated := Ed25519Go_GenerateKey()
|
|
if generated.Error != nil {
|
|
result.Error = generated.Error
|
|
return result
|
|
}
|
|
privateKey = generated.PrivateKey
|
|
privateBytes = []uint8(privateKey)
|
|
}
|
|
if int64(len(privateBytes)) != 64 {
|
|
var value_0 Error = ErrorsGo_New("invalid Ed25519 node identity key")
|
|
result.Error = value_0
|
|
return result
|
|
}
|
|
if 32 < 0 || 64 < 32 || 64 > int64(len(privateBytes)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
publicBytes := NodeIdentity_CopyBytes(privateBytes[32:64:64])
|
|
var value_2 string = Signing_SHA256Hex(publicBytes)
|
|
result.Value.ID = value_2
|
|
result.Value.PublicKey = PublicKey(publicBytes)
|
|
var value_3 []uint8 = NodeIdentity_CopyBytes(privateBytes)
|
|
result.Value.PrivateKey = PrivateKey(value_3)
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_LoadOrCreateKey(rawPath string) PrivateKeyResult {
|
|
var result PrivateKeyResult = PrivateKeyResult{}
|
|
path := strings.TrimSpace(rawPath)
|
|
if path == "" {
|
|
var value_0 Error = ErrorsGo_New("empty node identity key path")
|
|
result.Error = value_0
|
|
return result
|
|
}
|
|
read := FileGo_ReadFile(path)
|
|
if read.Error == nil {
|
|
decoded := NodeIdentity_DecodeHex(strings.TrimSpace(TextGo_FromBytes(read.Value)))
|
|
if decoded.Error != nil {
|
|
var value_1 Error = fmt.Errorf("decode %s: %w", path, decoded.Error)
|
|
result.Error = value_1
|
|
return result
|
|
}
|
|
if int64(len(decoded.Value)) == 32 {
|
|
var value_2 PrivateKey = Ed25519Go_NewKeyFromSeed(decoded.Value)
|
|
result.Value = value_2
|
|
return result
|
|
}
|
|
if int64(len(decoded.Value)) != 64 {
|
|
var value_3 Error = ErrorsGo_New(NodeIdentity_Concat(path, " has invalid key length"))
|
|
result.Error = value_3
|
|
return result
|
|
}
|
|
result.Value = PrivateKey(decoded.Value)
|
|
return result
|
|
}
|
|
var value_4 bool = ErrorsGo_Is(read.Error, FileGo_NotExist())
|
|
if !value_4 {
|
|
result.Error = read.Error
|
|
return result
|
|
}
|
|
var value_5 Error = FileGo_MkdirAll(FileGo_Dir(path), FileMode(448))
|
|
result.Error = value_5
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
generated := Ed25519Go_GenerateKey()
|
|
if generated.Error != nil {
|
|
result.Error = generated.Error
|
|
return result
|
|
}
|
|
temporary := NodeIdentity_Concat(path, ".tmp")
|
|
encoded := NodeIdentity_Concat(hex.EncodeToString([]uint8(generated.PrivateKey)), "\n")
|
|
var value_6 Error = FileGo_WriteFile(temporary, TextGo_ToBytes(encoded), FileMode(384))
|
|
result.Error = value_6
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
var value_7 Error = FileGo_Rename(temporary, path)
|
|
result.Error = value_7
|
|
if result.Error != nil {
|
|
FileGo_Remove(temporary)
|
|
return result
|
|
}
|
|
result.Value = generated.PrivateKey
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_InviteMessage(invite PairingInvite) []uint8 {
|
|
policy := JsonGo_Marshal(invite.Policy)
|
|
var value_0 string = strconv.FormatInt(int64(int64(invite.Version)), int(int(10)))
|
|
var value_1 string = NodeIdentity_SortedAddresses(invite.Addresses)
|
|
var value_2 string = strconv.FormatInt(int64(invite.ExpiresAt), int(int(10)))
|
|
var value_3 string = Signing_SHA256Hex(policy.Value)
|
|
var value_4 [12]string = [12]string{"daochi-pairing-invite-v1", value_0, invite.InviteID, invite.NodeID, invite.PublicKey, invite.DisplayName, value_1, invite.SpaceID, value_2, invite.Nonce, value_3, ""}
|
|
parts := value_4
|
|
_ = parts
|
|
return TextGo_ToBytes(strings.Join(parts[0:12:12], "\n"))
|
|
}
|
|
|
|
func NodeIdentity_SignInvite(node NodeIdentity, invite *PairingInvite) {
|
|
var value_0 string = NodeIdentity_SignedMessage(node.PrivateKey, NodeIdentity_InviteMessage(*(invite)))
|
|
invite.Signature = value_0
|
|
}
|
|
|
|
func NodeIdentity_AcceptanceMessage(invite PairingInvite, acceptance PairingAcceptance) []uint8 {
|
|
var value_0 string = strconv.FormatInt(int64(int64(acceptance.Version)), int(int(10)))
|
|
var value_1 string = NodeIdentity_SortedAddresses(acceptance.Addresses)
|
|
var value_2 string = strconv.FormatInt(int64(acceptance.AcceptedAt), int(int(10)))
|
|
var value_3 string = Signing_SHA256Hex(TextGo_ToBytes(invite.Signature))
|
|
var value_4 [11]string = [11]string{"daochi-pairing-invite-v1-acceptance", value_0, acceptance.InviteID, acceptance.NodeID, acceptance.PublicKey, acceptance.DisplayName, value_1, value_2, acceptance.Nonce, value_3, ""}
|
|
parts := value_4
|
|
_ = parts
|
|
return TextGo_ToBytes(strings.Join(parts[0:11:11], "\n"))
|
|
}
|
|
|
|
func NodeIdentity_SignAcceptance(node NodeIdentity, invite PairingInvite, acceptance *PairingAcceptance) {
|
|
var value_0 string = NodeIdentity_SignedMessage(node.PrivateKey, NodeIdentity_AcceptanceMessage(invite, *(acceptance)))
|
|
acceptance.Signature = value_0
|
|
}
|
|
|
|
func NodeIdentity_ValidateAddresses(addresses []string) Error {
|
|
if int64(len(addresses)) == 0 {
|
|
return ErrorsGo_New("at least one reachable address is required")
|
|
}
|
|
for _, address := range addresses {
|
|
parsed := UrlGo_Parse(address)
|
|
var value_0 bool = parsed.Error != nil
|
|
var value_1 bool = value_0
|
|
if !value_1 {
|
|
var value_2 bool = UrlGo_Host(parsed.Value) == ""
|
|
value_1 = value_2
|
|
}
|
|
var value_3 bool = value_1
|
|
var value_4 bool = value_3
|
|
if !value_4 {
|
|
var value_5 bool = UrlGo_Scheme(parsed.Value) != "http"
|
|
var value_6 bool = value_5
|
|
if value_6 {
|
|
var value_7 bool = UrlGo_Scheme(parsed.Value) != "https"
|
|
value_6 = value_7
|
|
}
|
|
value_4 = value_6
|
|
}
|
|
if value_4 {
|
|
return ErrorsGo_New("invalid pairing address")
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func NodeIdentity_ValidateAcceptance(invite PairingInvite, acceptance PairingAcceptance, now Time) PublicKeyResult {
|
|
var result PublicKeyResult = PublicKeyResult{}
|
|
var value_0 bool = acceptance.Version != 1 || acceptance.InviteID != invite.InviteID
|
|
if !value_0 {
|
|
var value_1 bool = Identity_ValidUserID(acceptance.NodeID)
|
|
value_0 = !value_1
|
|
}
|
|
if value_0 || acceptance.Nonce == "" {
|
|
var value_2 Error = ErrorsGo_New("invalid pairing acceptance")
|
|
result.Error = value_2
|
|
return result
|
|
}
|
|
var window int64 = 300000000000
|
|
var value_3 bool = acceptance.AcceptedAt < TimeGo_Unix(TimeGo_Add(now, Duration(0-window)))
|
|
var value_4 bool = value_3
|
|
if !value_4 {
|
|
var value_5 bool = acceptance.AcceptedAt > TimeGo_Unix(TimeGo_Add(now, Duration(window)))
|
|
value_4 = value_5
|
|
}
|
|
if value_4 {
|
|
var value_6 Error = ErrorsGo_New("pairing acceptance time is outside the allowed window")
|
|
result.Error = value_6
|
|
return result
|
|
}
|
|
var value_7 Error = NodeIdentity_ValidateAddresses(acceptance.Addresses)
|
|
result.Error = value_7
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
publicBytes := NodeIdentity_DecodeHex(acceptance.PublicKey)
|
|
if publicBytes.Error != nil || int64(len(publicBytes.Value)) != 32 {
|
|
var value_8 Error = ErrorsGo_New("invalid pairing acceptance public key")
|
|
result.Error = value_8
|
|
return result
|
|
}
|
|
if Signing_SHA256Hex(publicBytes.Value) != acceptance.NodeID {
|
|
var value_9 Error = ErrorsGo_New("pairing acceptance node ID does not match public key")
|
|
result.Error = value_9
|
|
return result
|
|
}
|
|
signature := Codec_DecodeBase64(acceptance.Signature, true, false)
|
|
key := PublicKey(publicBytes.Value)
|
|
var value_10 bool = signature.Error != ""
|
|
if !value_10 {
|
|
var value_11 bool = Ed25519Go_Verify(key, NodeIdentity_AcceptanceMessage(invite, acceptance), TextGo_ToBytes(signature.Value))
|
|
value_10 = !value_11
|
|
}
|
|
if value_10 {
|
|
var value_12 Error = ErrorsGo_New("invalid pairing acceptance signature")
|
|
result.Error = value_12
|
|
return result
|
|
}
|
|
result.Value = key
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_ValidateInvite(invite PairingInvite, now Time) PublicKeyResult {
|
|
var result PublicKeyResult = PublicKeyResult{}
|
|
var value_0 bool = invite.Version != 1
|
|
if !value_0 {
|
|
var value_1 bool = Identity_ValidUserID(invite.NodeID)
|
|
value_0 = !value_1
|
|
}
|
|
if value_0 || invite.InviteID == "" || invite.Nonce == "" {
|
|
var value_2 Error = ErrorsGo_New("invalid pairing invite")
|
|
result.Error = value_2
|
|
return result
|
|
}
|
|
lifetime := Duration(86400000000000)
|
|
var value_3 bool = invite.ExpiresAt <= TimeGo_Unix(now)
|
|
var value_4 bool = value_3
|
|
if !value_4 {
|
|
var value_5 bool = invite.ExpiresAt > TimeGo_Unix(TimeGo_Add(now, lifetime))
|
|
value_4 = value_5
|
|
}
|
|
if value_4 {
|
|
var value_6 Error = ErrorsGo_New("pairing invite expired or too far in the future")
|
|
result.Error = value_6
|
|
return result
|
|
}
|
|
publicBytes := NodeIdentity_DecodeHex(invite.PublicKey)
|
|
if publicBytes.Error != nil || int64(len(publicBytes.Value)) != 32 {
|
|
var value_7 Error = ErrorsGo_New("invalid pairing public key")
|
|
result.Error = value_7
|
|
return result
|
|
}
|
|
if Signing_SHA256Hex(publicBytes.Value) != invite.NodeID {
|
|
var value_8 Error = ErrorsGo_New("pairing node ID does not match public key")
|
|
result.Error = value_8
|
|
return result
|
|
}
|
|
signature := Codec_DecodeBase64(invite.Signature, true, false)
|
|
key := PublicKey(publicBytes.Value)
|
|
var value_9 bool = signature.Error != ""
|
|
if !value_9 {
|
|
var value_10 bool = Ed25519Go_Verify(key, NodeIdentity_InviteMessage(invite), TextGo_ToBytes(signature.Value))
|
|
value_9 = !value_10
|
|
}
|
|
if value_9 {
|
|
var value_11 Error = ErrorsGo_New("invalid pairing signature")
|
|
result.Error = value_11
|
|
return result
|
|
}
|
|
var value_12 Error = NodeIdentity_ValidateAddresses(invite.Addresses)
|
|
result.Error = value_12
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
result.Value = key
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_NormalizeName(name string) string {
|
|
return strings.ToLower(strings.TrimSpace(name))
|
|
}
|
|
|
|
func NodeIdentity_ValidName(name string) bool {
|
|
if int64(len(name)) == 0 || int64(len(name)) > 63 {
|
|
return false
|
|
}
|
|
var index int64 = 0
|
|
for index < int64(len(name)) {
|
|
value := name[index]
|
|
if (value < 97 || value > 122) && (value < 48 || value > 57) {
|
|
if value != 45 || index == 0 || index == int64(len(name))-1 {
|
|
return false
|
|
}
|
|
}
|
|
index++
|
|
}
|
|
return true
|
|
}
|
|
|
|
func NodeIdentity_NameClaimMessage(claim NameClaim) []uint8 {
|
|
services := JsonGo_Marshal(claim.Services)
|
|
var value_0 string = strconv.FormatInt(int64(int64(claim.Version)), int(int(10)))
|
|
var value_1 string = strconv.FormatInt(int64(claim.Sequence), int(int(10)))
|
|
var value_2 string = strconv.FormatInt(int64(claim.ExpiresAt), int(int(10)))
|
|
var value_3 string = Signing_SHA256Hex(services.Value)
|
|
var value_4 [9]string = [9]string{"daochi-name-claim-v1", value_0, claim.SpaceID, claim.Name, claim.NodeID, value_1, value_2, value_3, ""}
|
|
parts := value_4
|
|
_ = parts
|
|
return TextGo_ToBytes(strings.Join(parts[0:9:9], "\n"))
|
|
}
|