~ehmry/nim_tox

40f503bf7e59a2f50d58ba7b4d1664e7509b277d — Emery Hemingway 2 years ago b1dbeca
Address utilities
2 files changed, 16 insertions(+), 1 deletions(-)

M src/toxcore.nim
M tests/test1.nim
M src/toxcore.nim => src/toxcore.nim +15 -1
@@ 99,7 99,7 @@ assert(TOX_MAX_HOSTNAME_LENGTH == (int)max_hostname_length())
template toxArrayType(name, N: untyped) =
  ## Generate a array type definition with hex conversions.
  type name* = object
    bytes: array[N, uint8]
    bytes*: array[N, uint8]
  func `$`*(x: name): string =
    result = newStringOfCap(N*2)
    for b in x.bytes:


@@ 117,6 117,20 @@ toxArrayType(FileId, TOX_FILE_ID_LENGTH)
toxArrayType(ConferenceId, TOX_CONFERENCE_ID_SIZE)
toxArrayType(ConferenceUid, TOX_CONFERENCE_UID_SIZE)

func publicKey*(id: Address): Publickey =
  ## Extract the public key from a Tox address.
  for i in 0..<TOX_PUBLIC_KEY_SIZE:
    result.bytes[i] = id.bytes[i]

func isValid*(id: Address): bool =
  ## Verify a Tox address using the footer checksum.
  var even, odd: uint8
  for i in countup(0, id.bytes.high, 2):
    even = even xor id.bytes[i]
  for i in countup(1, id.bytes.high, 2):
    odd = odd xor id.bytes[i]
  (even or odd) == 0

type
  Core* = distinct pointer
    ## Opaque C type wrapped by the `Tox` object.

M tests/test1.nim => tests/test1.nim +1 -0
@@ 6,4 6,5 @@ test "basic":
  var options = newOptions()
  default(options)
  let tox = newTox(options)
  check(tox.address.isValid)
  close tox