Merge pull request #190 from status-im/endians

fix endian conversion issues
This commit is contained in:
Kim De Mey 2020-03-06 09:58:54 +01:00 committed by GitHub
commit baa5394d7f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 33 additions and 24 deletions

View File

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

View File

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

View File

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

View File

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

View File

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