Files

227 lines
6.7 KiB
Ziran

// Legacy daochi-* protocol constants below are released bytes; keep them unchanged.
// Mesh client wire values shared by apps. The caller owns account keys,
// hashing, HTTP transport, and the destination buffers.
#scope_export
Writer :: struct {
output: []u8
cursor: s64
valid: bool
}
#scope_export
WriteByte :: (writer: Writer, byte: u8) -> Writer {
result := writer
if !result.valid || result.cursor < 0 ||
result.output.count < 1 ||
result.cursor >= result.output.count - 1 {
result.valid = false
return result
}
result.output[result.cursor] = byte
result.cursor += 1
result.output[result.cursor] = cast(u8)0
return result
}
#scope_export
WriteText :: (writer: Writer, value: string) -> Writer {
result := writer
if !result.valid || result.cursor < 0 ||
result.cursor >= result.output.count ||
value.count >= result.output.count - result.cursor {
result.valid = false
return result
}
index: s64 = 0
while index < cast(s64)value.count {
result.output[result.cursor] = value[index]
result.cursor += 1
index += 1
}
result.output[result.cursor] = cast(u8)0
return result
}
HexDigit :: (byte: u8) -> bool {
return (byte >= cast(u8)48 && byte <= cast(u8)57) ||
(byte >= cast(u8)65 && byte <= cast(u8)70) ||
(byte >= cast(u8)97 && byte <= cast(u8)102)
}
#scope_export
Hex64 :: (value: string) -> bool {
if value.count != 64 {
return false
}
index: s64 = 0
while index < 64 {
if !HexDigit(value[index]) {
return false
}
index += 1
}
return true
}
HexUpper :: (value: u8) -> u8 {
if value < 10 {
return cast(u8)48 + value
}
return cast(u8)65 + value - cast(u8)10
}
#scope_export
AppendJsonQuoted :: (writer: Writer, value: string) -> Writer {
result := WriteByte(writer, cast(u8)34)
index: s64 = 0
while result.valid && index < cast(s64)value.count {
byte := value[index]
if byte == cast(u8)34 || byte == cast(u8)92 {
result = WriteByte(result, cast(u8)92)
result = WriteByte(result, byte)
} else if byte == cast(u8)8 || byte == cast(u8)12 ||
byte == cast(u8)10 || byte == cast(u8)13 ||
byte == cast(u8)9 {
escape := cast(u8)98
if byte == cast(u8)12 {
escape = cast(u8)102
}
if byte == cast(u8)10 {
escape = cast(u8)110
}
if byte == cast(u8)13 {
escape = cast(u8)114
}
if byte == cast(u8)9 {
escape = cast(u8)116
}
result = WriteByte(result, cast(u8)92)
result = WriteByte(result, escape)
} else if byte < cast(u8)32 {
result = WriteByte(result, cast(u8)92)
result = WriteByte(result, cast(u8)117)
result = WriteByte(result, cast(u8)48)
result = WriteByte(result, cast(u8)48)
result = WriteByte(result, HexUpper(byte / cast(u8)16))
result = WriteByte(result, HexUpper(byte % cast(u8)16))
} else {
result = WriteByte(result, byte)
}
index += 1
}
return WriteByte(result, cast(u8)34)
}
#scope_export
#program_export
BuildChallengePath :: (account_id: string, output: []u8) -> bool {
if output.count == 0 {
return false
}
output[0] = cast(u8)0
if !Hex64(account_id) {
return false
}
writer: Writer = Writer.{output, 0, true}
writer = WriteText(writer, "/api/v1/sync/challenge?user_id=")
writer = WriteText(writer, account_id)
if !writer.valid {
output[0] = cast(u8)0
return false
}
return true
}
WriteLoginBody :: (account_id: string, client_id: string,
public_key: string, restore: bool, output: []u8) -> bool {
if output.count == 0 {
return false
}
output[0] = cast(u8)0
if !Hex64(account_id) || client_id.count == 0 ||
public_key.count == 0 {
return false
}
writer: Writer = Writer.{output, 0, true}
writer = WriteText(writer, "{\"user_id_hash\":")
writer = AppendJsonQuoted(writer, account_id)
writer = WriteText(writer, ",\"client_id\":")
writer = AppendJsonQuoted(writer, client_id)
writer = WriteText(writer, ",\"public_key\":")
writer = AppendJsonQuoted(writer, public_key)
if restore {
writer = WriteText(writer, ",\"restore\":true")
}
writer = WriteByte(writer, cast(u8)125)
if !writer.valid {
output[0] = cast(u8)0
return false
}
return true
}
#scope_export
#program_export
BuildLoginBody :: (account_id: string, client_id: string,
public_key: string, output: []u8) -> bool {
return WriteLoginBody(account_id, client_id, public_key, false, output)
}
// A login that also asks the node to bring back this key's deleted account
// while the node still holds it, before any new data.
#scope_export
#program_export
BuildRestoreLoginBody :: (account_id: string, client_id: string,
public_key: string, output: []u8) -> bool {
return WriteLoginBody(account_id, client_id, public_key, true, output)
}
// The caller hashes the exact body bytes. This function fixes the signed
// message layout and prevents an app from choosing a different path or context.
#scope_export
#program_export
BuildLoginMessageHash :: (body_sha256_hex: string,
nonce_hex: string, output: []u8) -> bool {
if output.count == 0 {
return false
}
output[0] = cast(u8)0
if !Hex64(body_sha256_hex) || !Hex64(nonce_hex) {
return false
}
writer: Writer = Writer.{output, 0, true}
writer = WriteText(writer, "daochi-sync-v1\nPOST\n/api/v1/sync/login\n")
writer = WriteText(writer, body_sha256_hex)
writer = WriteByte(writer, cast(u8)10)
writer = WriteText(writer, nonce_hex)
writer = WriteByte(writer, cast(u8)10)
if !writer.valid {
output[0] = cast(u8)0
return false
}
return true
}
#scope_export
#program_export
BuildDeleteMessageHash :: (body_sha256_hex: string,
nonce_hex: string, output: []u8) -> bool {
if output.count == 0 { return false }
output[0] = cast(u8)0
if !Hex64(body_sha256_hex) || !Hex64(nonce_hex) {
return false
}
writer: Writer = Writer.{output, 0, true}
writer = WriteText(writer, "daochi-sync-v1\nPOST\n/api/v1/account/delete\n")
writer = WriteText(writer, body_sha256_hex)
writer = WriteByte(writer, cast(u8)10)
writer = WriteText(writer, nonce_hex)
writer = WriteByte(writer, cast(u8)10)
if !writer.valid {
output[0] = cast(u8)0
return false
}
return true
}