fix endian conversion issues

This commit is contained in:
Jacek Sieka 2020-03-05 01:25:21 +01:00
parent bc6c981916
commit a899e09f68
No known key found for this signature in database
GPG Key ID: A1B09461ABB656B8
5 changed files with 33 additions and 24 deletions

View File

@ -1,5 +1,5 @@
import
endians, options, times, chronicles,
stew/endians2, options, times, chronicles,
stint, nimcrypto/[keccak, hash], eth/rlp, eth/trie/[trie_defs, db]
export
@ -202,11 +202,11 @@ else:
template u256*(n: BlockNumber): UInt256 =
n
proc toBlockNonce*(n: uint64): BlockNonce =
bigEndian64(addr result[0], unsafeAddr n)
func toBlockNonce*(n: uint64): BlockNonce =
n.toBytesBE()
proc toUint*(n: BlockNonce): uint64 =
bigEndian64(addr result, unsafeAddr n[0])
func toUint*(n: BlockNonce): uint64 =
uint64.fromBytesBE(n)
proc newAccount*(nonce: AccountNonce = 0, balance: UInt256 = 0.u256): Account =
result.nonce = nonce

View File

@ -16,9 +16,7 @@
## - ``FreeBSD``, ``OpenBSD``, ``NetBSD``,
## ``DragonflyBSD`` - using `uuid_create()`.
{.deadCodeElim:on.}
import nimcrypto/utils, endians
import nimcrypto/utils, stew/endians2
type
UUIDException = object of CatchableError
@ -38,9 +36,14 @@ proc uuidFromString*(s: string): UUID =
if s[i] notin {'A'..'F', '0'..'9', 'a'..'f', '-'}:
raiseInvalidUuid()
var d = fromHex(stripSpaces(s))
bigEndian32(addr result.data[0], addr d[0])
bigEndian16(addr result.data[4], addr d[4])
bigEndian16(addr result.data[6], addr d[6])
var
a = uint32.fromBytesBE(d.toOpenArray(0, 3))
b = uint16.fromBytesBE(d.toOpenArray(4, 5))
c = uint16.fromBytesBE(d.toOpenArray(6, 7))
copyMem(addr result.data[0], addr a, 4)
copyMem(addr result.data[4], addr b, 2)
copyMem(addr result.data[6], addr c, 2)
copyMem(addr result.data[8], addr d[8], 8)
proc uuidToString*(u: UUID, lowercase: bool = false): string =
@ -49,9 +52,14 @@ proc uuidToString*(u: UUID, lowercase: bool = false): string =
## of output string.
result = newStringOfCap(38)
var d: array[8, byte]
bigEndian32(addr d[0], unsafeAddr u.data[0])
bigEndian16(addr d[4], unsafeAddr u.data[4])
bigEndian16(addr d[6], unsafeAddr u.data[6])
var
a = uint32.fromBytesBE(u.data.toOpenArray(0, 3))
b = uint16.fromBytesBE(u.data.toOpenArray(4, 5))
c = uint16.fromBytesBE(u.data.toOpenArray(6, 7))
copyMem(addr d[0], addr a, 4)
copyMem(addr d[4], addr b, 2)
copyMem(addr d[6], addr c, 2)
result.add(toHex(toOpenArray(d, 0, 3), lowercase))
result.add("-")
result.add(toHex(toOpenArray(d, 4, 5), lowercase))

View File

@ -10,9 +10,9 @@
## This module implements Ethereum authentication
import endians
import eth/[keys, rlp], nimcrypto
import ecies
import stew/[byteutils, endians2]
const
SupportedRlpxVersion* = 4
@ -192,7 +192,8 @@ proc authMessageEIP8(h: var Handshake,
copyMem(addr buffer[0], addr payload[0], len(payload))
if len(output) < fullsize:
return(BufferOverrun)
bigEndian16(addr output, addr wosize)
let wosizeBE = uint16(wosize).toBytesBE()
output[0..<2] = wosizeBE
if eciesEncrypt(toa(buffer, 0, len(payload) + int(padsize)),
toa(output, 2, wosize), pubkey,
toa(output, 0, 2)) != EciesStatus.Success:
@ -264,7 +265,7 @@ proc ackMessageEIP8(h: var Handshake,
if encrypt:
if len(output) < fullsize:
return(BufferOverrun)
bigEndian16(addr output, addr wosize)
output[0..<2] = uint16(wosize).toBytesBE()
if eciesEncrypt(toa(buffer, 0, len(payload) + int(padsize)),
toa(output, 2, wosize), h.remoteHPubkey,
toa(output, 0, 2)) != EciesStatus.Success:
@ -342,9 +343,8 @@ proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthStatus =
pubkey: PublicKey
nonce: Nonce
secret: SharedSecret
size: uint16
bigEndian16(addr size, unsafeAddr m[0])
let size = uint16.fromBytesBE(m)
h.expectedLength = int(size) + 2
if h.expectedLength > len(m):
return(IncompleteError)
@ -389,8 +389,8 @@ proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthStatus =
proc decodeAckMessageEip8*(h: var Handshake, m: openarray[byte]): AuthStatus =
## Decodes EIP-8 AckMessage.
var size: uint16
bigEndian16(addr size, unsafeAddr m[0])
let size = uint16.fromBytesBE(m)
h.expectedLength = 2 + int(size)
if h.expectedLength > len(m):
return(IncompleteError)

View File

@ -1,5 +1,5 @@
import
std/[net, endians, hashes], nimcrypto, stint, chronicles,
std/[net, hashes], nimcrypto, stint, chronicles,
types, enr, eth/keys, ../enode
type

View File

@ -1,6 +1,7 @@
import
std/[tables, sets, endians, options, math, random],
json_serialization/std/net, stew/byteutils, chronicles, chronos, stint,
std/[tables, sets, options, math, random],
json_serialization/std/net,
stew/[byteutils, endians2], chronicles, chronos, stint,
eth/[rlp, keys], ../enode, types, encoding, node, routing_table, enr
import nimcrypto except toHex