442 lines
14 KiB
Go
442 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 std_go_types
|
|
type NodeIdentity struct {
|
|
ID string
|
|
PublicKey PublicKey
|
|
PrivateKey PrivateKey
|
|
}
|
|
|
|
type NodeIdentityResult struct {
|
|
Value NodeIdentity
|
|
Error Error
|
|
}
|
|
|
|
type zir_e94080a3b50d4b7c_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 zir_91a8a532591b3577_HexResult struct {
|
|
Value []uint8
|
|
Error Error
|
|
}
|
|
|
|
func NodeIdentity_DecodeHex(Value string) zir_91a8a532591b3577_HexResult {
|
|
var result zir_91a8a532591b3577_HexResult
|
|
result.Value, result.Error = hex.DecodeString(Value)
|
|
return result
|
|
}
|
|
|
|
const MaximumInviteLifetimeSeconds = 86400
|
|
|
|
func NodeIdentity_Concat(left string, right string) string {
|
|
var value_0 [2]string
|
|
value_0[0] = left
|
|
value_0[1] = right
|
|
parts := value_0
|
|
_ = 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 := StdEd25519Go_Sign(key, message)
|
|
var value_0 string = StdTextGo_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 := StdEd25519Go_GenerateKey()
|
|
if generated.Error != nil {
|
|
result.Error = generated.Error
|
|
return result
|
|
}
|
|
privateKey = generated.PrivateKey
|
|
privateBytes = []uint8(privateKey)
|
|
}
|
|
if int64(len(privateBytes)) != 64 {
|
|
result.Error = StdErrorsGo_New("invalid Ed25519 node identity key")
|
|
return result
|
|
}
|
|
if int64(32) < 0 || int64(64) < int64(32) || int64(64) > int64(len(privateBytes)) {
|
|
panic("slice range out of bounds")
|
|
}
|
|
publicBytes := NodeIdentity_CopyBytes(privateBytes[32:64:64])
|
|
result.Value.ID = Signing_SHA256Hex(publicBytes)
|
|
result.Value.PublicKey = PublicKey(publicBytes)
|
|
var value_1 []uint8 = NodeIdentity_CopyBytes(privateBytes)
|
|
result.Value.PrivateKey = PrivateKey(value_1)
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_LoadOrCreateKey(rawPath string) zir_e94080a3b50d4b7c_PrivateKeyResult {
|
|
var result zir_e94080a3b50d4b7c_PrivateKeyResult = zir_e94080a3b50d4b7c_PrivateKeyResult{}
|
|
path := strings.TrimSpace(rawPath)
|
|
if path == "" {
|
|
result.Error = StdErrorsGo_New("empty node identity key path")
|
|
return result
|
|
}
|
|
read := StdFileGo_ReadFile(path)
|
|
if read.Error == nil {
|
|
decoded := NodeIdentity_DecodeHex(strings.TrimSpace(StdTextGo_FromBytes(read.Value)))
|
|
if decoded.Error != nil {
|
|
result.Error = fmt.Errorf("decode %s: %w", path, decoded.Error)
|
|
return result
|
|
}
|
|
if int64(len(decoded.Value)) == 32 {
|
|
result.Value = StdEd25519Go_NewKeyFromSeed(decoded.Value)
|
|
return result
|
|
}
|
|
if int64(len(decoded.Value)) != 64 {
|
|
result.Error = StdErrorsGo_New(NodeIdentity_Concat(path, " has invalid key length"))
|
|
return result
|
|
}
|
|
result.Value = PrivateKey(decoded.Value)
|
|
return result
|
|
}
|
|
var value_0 bool = StdErrorsGo_Is(read.Error, StdFileGo_NotExist())
|
|
if !value_0 {
|
|
result.Error = read.Error
|
|
return result
|
|
}
|
|
result.Error = StdFileGo_MkdirAll(StdFileGo_Dir(path), FileMode(448))
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
generated := StdEd25519Go_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")
|
|
result.Error = StdFileGo_WriteFile(temporary, StdTextGo_ToBytes(encoded), FileMode(384))
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
result.Error = StdFileGo_Rename(temporary, path)
|
|
if result.Error != nil {
|
|
StdFileGo_Remove(temporary)
|
|
return result
|
|
}
|
|
result.Value = generated.PrivateKey
|
|
return result
|
|
}
|
|
|
|
func NodeIdentity_InviteMessage(invite PairingInvite) []uint8 {
|
|
policy := StdJsonGo_Marshal(invite.Policy)
|
|
var value_0 [12]string
|
|
value_0[0] = "daochi-pairing-invite-v1"
|
|
var value_1 string = strconv.FormatInt(int64(int64(invite.Version)), int(int(10)))
|
|
value_0[1] = value_1
|
|
value_0[2] = invite.InviteID
|
|
value_0[3] = invite.NodeID
|
|
value_0[4] = invite.PublicKey
|
|
value_0[5] = invite.DisplayName
|
|
var value_2 string = NodeIdentity_SortedAddresses(invite.Addresses)
|
|
value_0[6] = value_2
|
|
value_0[7] = invite.SpaceID
|
|
var value_3 string = strconv.FormatInt(int64(invite.ExpiresAt), int(int(10)))
|
|
value_0[8] = value_3
|
|
value_0[9] = invite.Nonce
|
|
var value_4 string = Signing_SHA256Hex(policy.Value)
|
|
value_0[10] = value_4
|
|
value_0[11] = ""
|
|
parts := value_0
|
|
_ = parts
|
|
return StdTextGo_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 [11]string
|
|
value_0[0] = "daochi-pairing-invite-v1-acceptance"
|
|
var value_1 string = strconv.FormatInt(int64(int64(acceptance.Version)), int(int(10)))
|
|
value_0[1] = value_1
|
|
value_0[2] = acceptance.InviteID
|
|
value_0[3] = acceptance.NodeID
|
|
value_0[4] = acceptance.PublicKey
|
|
value_0[5] = acceptance.DisplayName
|
|
var value_2 string = NodeIdentity_SortedAddresses(acceptance.Addresses)
|
|
value_0[6] = value_2
|
|
var value_3 string = strconv.FormatInt(int64(acceptance.AcceptedAt), int(int(10)))
|
|
value_0[7] = value_3
|
|
value_0[8] = acceptance.Nonce
|
|
var value_4 string = Signing_SHA256Hex(StdTextGo_ToBytes(invite.Signature))
|
|
value_0[9] = value_4
|
|
value_0[10] = ""
|
|
parts := value_0
|
|
_ = parts
|
|
return StdTextGo_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 StdErrorsGo_New("at least one reachable address is required")
|
|
}
|
|
for _, address := range addresses {
|
|
parsed := StdUrlGo_Parse(address)
|
|
var value_0 bool = parsed.Error != nil
|
|
var value_1 bool = value_0
|
|
if !value_1 {
|
|
var value_2 bool = StdUrlGo_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 = StdUrlGo_Scheme(parsed.Value) != "http"
|
|
var value_6 bool = value_5
|
|
if value_6 {
|
|
var value_7 bool = StdUrlGo_Scheme(parsed.Value) != "https"
|
|
value_6 = value_7
|
|
}
|
|
value_4 = value_6
|
|
}
|
|
if value_4 {
|
|
return StdErrorsGo_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 == "" {
|
|
result.Error = StdErrorsGo_New("invalid pairing acceptance")
|
|
return result
|
|
}
|
|
var window int64 = 300000000000
|
|
var value_2 bool = acceptance.AcceptedAt < StdTimeGo_Unix(StdTimeGo_Add(now, Duration(0-window)))
|
|
var value_3 bool = value_2
|
|
if !value_3 {
|
|
var value_4 bool = acceptance.AcceptedAt > StdTimeGo_Unix(StdTimeGo_Add(now, Duration(window)))
|
|
value_3 = value_4
|
|
}
|
|
if value_3 {
|
|
result.Error = StdErrorsGo_New("pairing acceptance time is outside the allowed window")
|
|
return result
|
|
}
|
|
result.Error = NodeIdentity_ValidateAddresses(acceptance.Addresses)
|
|
if result.Error != nil {
|
|
return result
|
|
}
|
|
publicBytes := NodeIdentity_DecodeHex(acceptance.PublicKey)
|
|
if publicBytes.Error != nil || int64(len(publicBytes.Value)) != 32 {
|
|
result.Error = StdErrorsGo_New("invalid pairing acceptance public key")
|
|
return result
|
|
}
|
|
if Signing_SHA256Hex(publicBytes.Value) != acceptance.NodeID {
|
|
result.Error = StdErrorsGo_New("pairing acceptance node ID does not match public key")
|
|
return result
|
|
}
|
|
signature := Codec_DecodeBase64(acceptance.Signature, true, false)
|
|
key := PublicKey(publicBytes.Value)
|
|
var value_5 bool = signature.Error != ""
|
|
if !value_5 {
|
|
var value_6 bool = StdEd25519Go_Verify(key, NodeIdentity_AcceptanceMessage(invite, acceptance), StdTextGo_ToBytes(signature.Value))
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 {
|
|
result.Error = StdErrorsGo_New("invalid pairing acceptance signature")
|
|
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 == "" {
|
|
result.Error = StdErrorsGo_New("invalid pairing invite")
|
|
return result
|
|
}
|
|
lifetime := Duration(86400000000000)
|
|
var value_2 bool = invite.ExpiresAt <= StdTimeGo_Unix(now)
|
|
var value_3 bool = value_2
|
|
if !value_3 {
|
|
var value_4 bool = invite.ExpiresAt > StdTimeGo_Unix(StdTimeGo_Add(now, lifetime))
|
|
value_3 = value_4
|
|
}
|
|
if value_3 {
|
|
result.Error = StdErrorsGo_New("pairing invite expired or too far in the future")
|
|
return result
|
|
}
|
|
publicBytes := NodeIdentity_DecodeHex(invite.PublicKey)
|
|
if publicBytes.Error != nil || int64(len(publicBytes.Value)) != 32 {
|
|
result.Error = StdErrorsGo_New("invalid pairing public key")
|
|
return result
|
|
}
|
|
if Signing_SHA256Hex(publicBytes.Value) != invite.NodeID {
|
|
result.Error = StdErrorsGo_New("pairing node ID does not match public key")
|
|
return result
|
|
}
|
|
signature := Codec_DecodeBase64(invite.Signature, true, false)
|
|
key := PublicKey(publicBytes.Value)
|
|
var value_5 bool = signature.Error != ""
|
|
if !value_5 {
|
|
var value_6 bool = StdEd25519Go_Verify(key, NodeIdentity_InviteMessage(invite), StdTextGo_ToBytes(signature.Value))
|
|
value_5 = !value_6
|
|
}
|
|
if value_5 {
|
|
result.Error = StdErrorsGo_New("invalid pairing signature")
|
|
return result
|
|
}
|
|
result.Error = NodeIdentity_ValidateAddresses(invite.Addresses)
|
|
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 := StdJsonGo_Marshal(claim.Services)
|
|
var value_0 [9]string
|
|
value_0[0] = "daochi-name-claim-v1"
|
|
var value_1 string = strconv.FormatInt(int64(int64(claim.Version)), int(int(10)))
|
|
value_0[1] = value_1
|
|
value_0[2] = claim.SpaceID
|
|
value_0[3] = claim.Name
|
|
value_0[4] = claim.NodeID
|
|
var value_2 string = strconv.FormatInt(int64(claim.Sequence), int(int(10)))
|
|
value_0[5] = value_2
|
|
var value_3 string = strconv.FormatInt(int64(claim.ExpiresAt), int(int(10)))
|
|
value_0[6] = value_3
|
|
var value_4 string = Signing_SHA256Hex(services.Value)
|
|
value_0[7] = value_4
|
|
value_0[8] = ""
|
|
parts := value_0
|
|
_ = parts
|
|
return StdTextGo_ToBytes(strings.Join(parts[0:9:9], "\n"))
|
|
}
|