Eip8 auth
This commit is contained in:
parent
d22c8251dc
commit
74f0b705f0
|
@ -334,14 +334,16 @@ proc decodeAuthMessageV4(h: var Handshake, m: openarray[byte]): AuthStatus =
|
||||||
h.remoteHPubkey = pubkey
|
h.remoteHPubkey = pubkey
|
||||||
result = Success
|
result = Success
|
||||||
|
|
||||||
|
proc expectedAuthMsgLenEip8*(input: openarray[byte]): uint16 {.inline.} =
|
||||||
|
bigEndian16(addr result, unsafeAddr input[0])
|
||||||
|
|
||||||
proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthStatus =
|
proc decodeAuthMessageEip8(h: var Handshake, m: openarray[byte]): AuthStatus =
|
||||||
## Decodes EIP-8 AuthMessage.
|
## Decodes EIP-8 AuthMessage.
|
||||||
var
|
var
|
||||||
pubkey: PublicKey
|
pubkey: PublicKey
|
||||||
nonce: Nonce
|
nonce: Nonce
|
||||||
size: uint16
|
|
||||||
secret: SharedSecret
|
secret: SharedSecret
|
||||||
bigEndian16(addr size, unsafeAddr m[0])
|
let size = expectedAuthMsgLenEip8(m)
|
||||||
if 2 + int(size) > len(m):
|
if 2 + int(size) > len(m):
|
||||||
return(IncompleteError)
|
return(IncompleteError)
|
||||||
var buffer = newSeq[byte](eciesDecryptedLength(int(size)))
|
var buffer = newSeq[byte](eciesDecryptedLength(int(size)))
|
||||||
|
@ -434,9 +436,10 @@ proc decodeAuthMessage*(h: var Handshake, input: openarray[byte]): AuthStatus =
|
||||||
if len(input) < AuthMessageV4Length:
|
if len(input) < AuthMessageV4Length:
|
||||||
result = IncompleteError
|
result = IncompleteError
|
||||||
elif len(input) == AuthMessageV4Length:
|
elif len(input) == AuthMessageV4Length:
|
||||||
let res = h.decodeAuthMessageV4(input)
|
var res = h.decodeAuthMessageV4(input)
|
||||||
if res != Success:
|
if res != Success:
|
||||||
if h.decodeAuthMessageEip8(input) != Success:
|
res = h.decodeAuthMessageEip8(input)
|
||||||
|
if res != Success:
|
||||||
result = res
|
result = res
|
||||||
else:
|
else:
|
||||||
h.flags.incl(EIP8)
|
h.flags.incl(EIP8)
|
||||||
|
|
|
@ -358,7 +358,6 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
||||||
error("The only type that can be defined inside a RLPx protocol is the protocol's State type.")
|
error("The only type that can be defined inside a RLPx protocol is the protocol's State type.")
|
||||||
|
|
||||||
of nnkProcDef:
|
of nnkProcDef:
|
||||||
inc nextId
|
|
||||||
let
|
let
|
||||||
msgIdent = n.name.ident
|
msgIdent = n.name.ident
|
||||||
msgName = $msgIdent
|
msgName = $msgIdent
|
||||||
|
@ -464,6 +463,7 @@ macro rlpxProtocol*(protoIdentifier: untyped,
|
||||||
newStrLitNode($n.name),
|
newStrLitNode($n.name),
|
||||||
thunkName)
|
thunkName)
|
||||||
|
|
||||||
|
inc nextId
|
||||||
else:
|
else:
|
||||||
error("illegal syntax in a RLPx protocol definition", n)
|
error("illegal syntax in a RLPx protocol definition", n)
|
||||||
|
|
||||||
|
@ -574,11 +574,20 @@ proc rlpxConnectIncoming*(myKeys: KeyPair, listenPort: Port, address: IpAddress,
|
||||||
var handshake = newHandshake({Responder})
|
var handshake = newHandshake({Responder})
|
||||||
handshake.host = myKeys
|
handshake.host = myKeys
|
||||||
|
|
||||||
var authMsg: array[1024, byte]
|
var authMsg: array[4086, byte]
|
||||||
var authMsgLen = AuthMessageV4Length
|
var authMsgLen = AuthMessageV4Length
|
||||||
# TODO: Handle both auth methods
|
|
||||||
await s.fullRecvInto(addr authMsg[0], authMsgLen)
|
await s.fullRecvInto(addr authMsg[0], authMsgLen)
|
||||||
check handshake.decodeAuthMessage(^authMsg)
|
var ret = handshake.decodeAuthMessage(^authMsg)
|
||||||
|
if ret == AuthStatus.IncompleteError: # Eip8 auth message is likely
|
||||||
|
let expectedLen = expectedAuthMsgLenEip8(authMsg).int + 2
|
||||||
|
if expectedLen > authMsg.len:
|
||||||
|
raise newException(Exception, "Auth message too big: " & $authMsgLen)
|
||||||
|
await s.fullRecvInto(addr authMsg[authMsgLen], expectedLen - authMsgLen)
|
||||||
|
authMsgLen = expectedLen
|
||||||
|
ret = handshake.decodeAuthMessage(^authMsg)
|
||||||
|
|
||||||
|
check ret
|
||||||
|
|
||||||
var ackMsg: array[AckMessageMaxEIP8, byte]
|
var ackMsg: array[AckMessageMaxEIP8, byte]
|
||||||
var ackMsgLen: int
|
var ackMsgLen: int
|
||||||
|
|
Loading…
Reference in New Issue