275 lines
8.9 KiB
Ziran
275 lines
8.9 KiB
Ziran
// Legacy daochi-* protocol constants below are released bytes; keep them unchanged.
|
|
// Protocol v6 device registration and signed sync transaction wire formats.
|
|
#import "mesh_client/wire"
|
|
|
|
DeviceRegistration :: struct {
|
|
account_id: string
|
|
app_id: string
|
|
device_key_id: string
|
|
client_id: string
|
|
public_key_hex: string
|
|
nonce: string
|
|
expires_at: s64
|
|
}
|
|
|
|
SyncTransaction :: struct {
|
|
account_id: string
|
|
app_id: string
|
|
device_key_id: string
|
|
tx_id: string
|
|
nonce: string
|
|
body_sha256_hex: string
|
|
expires_at: s64
|
|
}
|
|
|
|
PlainText :: (value: string) -> bool {
|
|
if value.count == 0 {
|
|
return false
|
|
}
|
|
index: s64 = 0
|
|
while index < cast(s64)value.count {
|
|
byte := value[index]
|
|
if byte < cast(u8)33 || byte >= cast(u8)127 ||
|
|
byte == cast(u8)34 || byte == cast(u8)92 {
|
|
return false
|
|
}
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
HexText :: (value: string, length: s64) -> bool {
|
|
if cast(s64)value.count != length {
|
|
return false
|
|
}
|
|
index: s64 = 0
|
|
while index < length {
|
|
byte := value[index]
|
|
if !((byte >= cast(u8)48 && byte <= cast(u8)57) ||
|
|
(byte >= cast(u8)65 && byte <= cast(u8)70) ||
|
|
(byte >= cast(u8)97 && byte <= cast(u8)102)) {
|
|
return false
|
|
}
|
|
index += 1
|
|
}
|
|
return true
|
|
}
|
|
|
|
WriteDecimal :: (writer: Writer, value: s64) -> Writer {
|
|
result := writer
|
|
if value < 0 {
|
|
result.valid = false
|
|
return result
|
|
}
|
|
digits: [20]u8
|
|
count: s64 = 0
|
|
remaining := value
|
|
if remaining == 0 {
|
|
digits[0] = cast(u8)48
|
|
count = 1
|
|
}
|
|
while remaining > 0 {
|
|
digits[count] = cast(u8)48 + cast(u8)(remaining % 10)
|
|
count += 1
|
|
remaining /= 10
|
|
}
|
|
while count > 0 {
|
|
count -= 1
|
|
result = WriteByte(result, digits[count])
|
|
}
|
|
return result
|
|
}
|
|
|
|
RegistrationValid :: (request: DeviceRegistration) -> bool {
|
|
return Hex64(request.account_id) &&
|
|
PlainText(request.app_id) &&
|
|
Hex64(request.device_key_id) &&
|
|
PlainText(request.client_id) &&
|
|
Hex64(request.public_key_hex) &&
|
|
Hex64(request.nonce) && request.expires_at > 0
|
|
}
|
|
|
|
TransactionValid :: (transaction: SyncTransaction) -> bool {
|
|
return Hex64(transaction.account_id) &&
|
|
PlainText(transaction.app_id) &&
|
|
Hex64(transaction.device_key_id) &&
|
|
Hex64(transaction.tx_id) &&
|
|
Hex64(transaction.nonce) &&
|
|
Hex64(transaction.body_sha256_hex) &&
|
|
transaction.expires_at > 0
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildDeviceRegistrationMessage :: (request: DeviceRegistration,
|
|
output: []u8) -> bool {
|
|
if output.count == 0 {
|
|
return false
|
|
}
|
|
output[0] = cast(u8)0
|
|
if !RegistrationValid(request) {
|
|
return false
|
|
}
|
|
writer: Writer = Writer.{output, 0, true}
|
|
writer = WriteText(writer, "daochi-device-registration-v1\n")
|
|
writer = WriteText(writer, request.account_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, request.app_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, request.device_key_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, request.client_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, request.public_key_hex)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, request.nonce)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteDecimal(writer, request.expires_at)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
if !writer.valid {
|
|
output[0] = cast(u8)0
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildDeviceRegistrationBody :: (request: DeviceRegistration,
|
|
account_signature_hex: string, output: []u8) -> bool {
|
|
if output.count == 0 {
|
|
return false
|
|
}
|
|
output[0] = cast(u8)0
|
|
if !RegistrationValid(request) ||
|
|
!HexText(account_signature_hex, 4840) {
|
|
return false
|
|
}
|
|
writer: Writer = Writer.{output, 0, true}
|
|
writer = WriteText(writer, "{\"app_id\":")
|
|
writer = AppendJsonQuoted(writer, request.app_id)
|
|
writer = WriteText(writer, ",\"device_key_id\":")
|
|
writer = AppendJsonQuoted(writer, request.device_key_id)
|
|
writer = WriteText(writer, ",\"client_id\":")
|
|
writer = AppendJsonQuoted(writer, request.client_id)
|
|
writer = WriteText(writer, ",\"public_key\":")
|
|
writer = AppendJsonQuoted(writer, request.public_key_hex)
|
|
writer = WriteText(writer, ",\"nonce\":")
|
|
writer = AppendJsonQuoted(writer, request.nonce)
|
|
writer = WriteText(writer, ",\"expires_at\":")
|
|
writer = WriteDecimal(writer, request.expires_at)
|
|
writer = WriteText(writer, ",\"signature\":")
|
|
writer = AppendJsonQuoted(writer, account_signature_hex)
|
|
writer = WriteByte(writer, cast(u8)125)
|
|
if !writer.valid {
|
|
output[0] = cast(u8)0
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildSyncTransactionMessage :: (transaction: SyncTransaction,
|
|
output: []u8) -> bool {
|
|
return BuildTransactionMessage(transaction, "POST", "/api/v1/sync", output)
|
|
}
|
|
|
|
RequestValid :: (method: string, path: string) -> bool {
|
|
return (method == "GET" || method == "HEAD" || method == "POST" || method == "PUT" ||
|
|
method == "PATCH" || method == "DELETE" || method == "OPTIONS") &&
|
|
path.count > 0 && path.count <= 2048 && path[0] == 47 && PlainText(path)
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildTransactionMessage :: (transaction: SyncTransaction, method: string, path: string,
|
|
output: []u8) -> bool {
|
|
if output.count == 0 {
|
|
return false
|
|
}
|
|
output[0] = cast(u8)0
|
|
if !TransactionValid(transaction) || !RequestValid(method, path) {
|
|
return false
|
|
}
|
|
writer: Writer = Writer.{output, 0, true}
|
|
writer = WriteText(writer, "daochi-tx-v1\n6\n")
|
|
writer = WriteText(writer, transaction.tx_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, transaction.account_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, transaction.app_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, transaction.device_key_id)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, method)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, path)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, transaction.body_sha256_hex)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteText(writer, transaction.nonce)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
writer = WriteDecimal(writer, transaction.expires_at)
|
|
writer = WriteByte(writer, cast(u8)10)
|
|
if !writer.valid {
|
|
output[0] = cast(u8)0
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildSyncTransactionHeader :: (transaction: SyncTransaction,
|
|
account_signature_hex: string, device_signature_hex: string,
|
|
output: []u8) -> bool {
|
|
return BuildTransactionHeader(transaction, "POST", "/api/v1/sync",
|
|
account_signature_hex, device_signature_hex, output)
|
|
}
|
|
|
|
#scope_export
|
|
#program_export
|
|
BuildTransactionHeader :: (transaction: SyncTransaction, method: string, path: string,
|
|
account_signature_hex: string, device_signature_hex: string, output: []u8) -> bool {
|
|
if output.count == 0 {
|
|
return false
|
|
}
|
|
output[0] = cast(u8)0
|
|
if !TransactionValid(transaction) || !RequestValid(method, path) ||
|
|
!HexText(account_signature_hex, 4840) ||
|
|
!HexText(device_signature_hex, 128) {
|
|
return false
|
|
}
|
|
writer: Writer = Writer.{output, 0, true}
|
|
writer = WriteText(writer, "{\"protocol_version\":6,\"tx_id\":")
|
|
writer = AppendJsonQuoted(writer, transaction.tx_id)
|
|
writer = WriteText(writer, ",\"account_id\":")
|
|
writer = AppendJsonQuoted(writer, transaction.account_id)
|
|
writer = WriteText(writer, ",\"app_id\":")
|
|
writer = AppendJsonQuoted(writer, transaction.app_id)
|
|
writer = WriteText(writer, ",\"device_key_id\":")
|
|
writer = AppendJsonQuoted(writer, transaction.device_key_id)
|
|
writer = WriteText(writer, ",\"method\":")
|
|
writer = AppendJsonQuoted(writer, method)
|
|
writer = WriteText(writer, ",\"path\":")
|
|
writer = AppendJsonQuoted(writer, path)
|
|
writer = WriteText(writer, ",\"body_sha256\":")
|
|
writer = AppendJsonQuoted(writer, transaction.body_sha256_hex)
|
|
writer = WriteText(writer, ",\"nonce\":")
|
|
writer = AppendJsonQuoted(writer, transaction.nonce)
|
|
writer = WriteText(writer, ",\"expires_at\":")
|
|
writer = WriteDecimal(writer, transaction.expires_at)
|
|
writer = WriteText(writer,
|
|
",\"signature_context\":\"daochi-tx-v1\",\"signature\":")
|
|
writer = AppendJsonQuoted(writer, account_signature_hex)
|
|
writer = WriteText(writer, ",\"device_signature\":")
|
|
writer = AppendJsonQuoted(writer, device_signature_hex)
|
|
writer = WriteByte(writer, cast(u8)125)
|
|
if !writer.valid {
|
|
output[0] = cast(u8)0
|
|
return false
|
|
}
|
|
return true
|
|
}
|