Adapt rlpxConnect to the new auth APIs

This commit is contained in:
Zahary Karadjov 2018-04-04 02:47:31 +03:00
parent 286d537d42
commit e89aab335c
2 changed files with 29 additions and 20 deletions

View File

@ -19,15 +19,15 @@ const
PlainAuthMessageV4Length* = 194 PlainAuthMessageV4Length* = 194
AuthMessageV4Length* = 307 AuthMessageV4Length* = 307
PlainAuthMessageEIP8Length = 169 PlainAuthMessageEIP8Length = 169
PlainAuthMessageMaxEIP8 = PlainAuthMessageEIP8Length + 255 PlainAuthMessageMaxEIP8* = PlainAuthMessageEIP8Length + 255
AuthMessageEIP8Length* = 282 + 2 AuthMessageEIP8Length* = 282 + 2
AuthMessageMaxEIP8* = AuthMessageEIP8Length + 255 AuthMessageMaxEIP8* = AuthMessageEIP8Length + 255
PlainAckMessageV4Length* = 97 PlainAckMessageV4Length* = 97
AckMessageV4Length* = 210 AckMessageV4Length* = 210
PlainAckMessageEIP8Length = 102 PlainAckMessageEIP8Length* = 102
PlainAckMessageMaxEIP8 = PlainAckMessageEIP8Length + 255 PlainAckMessageMaxEIP8* = PlainAckMessageEIP8Length + 255
AckMessageEIP8Length = 215 + 2 AckMessageEIP8Length* = 215 + 2
AckMessageMaxEIP8 = AckMessageEIP8Length + 255 AckMessageMaxEIP8* = AckMessageEIP8Length + 255
type type
AuthMessageV4* = object {.packed.} AuthMessageV4* = object {.packed.}

View File

@ -407,37 +407,46 @@ rlpxProtocol("p2p", 0):
proc pong(peer: Peer) = proc pong(peer: Peer) =
discard discard
proc rlpxConnect*(keys: KeyPair, address: Address): Future[Peer] {.async.} = proc rlpxConnect*(myKeys: KeyPair, remoteKey: PublicKey,
address: Address): Future[Peer] {.async.} =
# TODO: Make sure to close the socket in case of exception # TODO: Make sure to close the socket in case of exception
result.socket = newAsyncSocket() result.socket = newAsyncSocket()
await result.socket.connect($address.ip, address.tcpPort) await result.socket.connect($address.ip, address.tcpPort)
var initiator = newHandshake({Initiator}) const encryptionEnabled = true
initiator.host.seckey = keys.privKey
initiator.host.pubkey = keys.pubKey
var authPlain: PlainAuthMessage
var authCiphertext: AuthMessage
template check(body: untyped) = template check(body: untyped) =
let c = body let c = body
if c != AuthStatus.Success: if c != AuthStatus.Success:
raise newException(Exception, "Error: " & $c) raise newException(Exception, "Error: " & $c)
check authMessage(initiator, keys.pubKey, authPlain) template `^`(arr): auto =
check encryptAuthMessage(authPlain, authCiphertext, keys.pubKey) # passes a stack array with a matching `arrLen`
await result.socket.send(addr authCiphertext[0], sizeof(authCiphertext)) # variable as an open array
arr.toOpenArray(0, `arr Len` - 1)
var authAck: AuthAckMessage var handshake = newHandshake({Initiator})
let receivedBytes = await result.socket.recvInto(addr authAck, sizeof(authAck)) handshake.host.seckey = myKeys.privKey
handshake.host.pubkey = myKeys.pubKey
if receivedBytes != sizeof(AuthAckMessage): var authMsg: array[AuthMessageMaxEIP8, byte]
var authMsgLen = 0
check authMessage(handshake, remoteKey, authMsg, authMsgLen,
encrypt = encryptionEnabled)
await result.socket.send(addr authMsg[0], authMsgLen)
var ackMsg: array[AckMessageMaxEIP8, byte]
let ackMsgLen = handshake.ackSize(encrypt = encryptionEnabled)
let receivedBytes = await result.socket.recvInto(addr ackMsg, ackMsgLen)
if receivedBytes != ackMsgLen:
# XXX: this handling is not perfect, we should probably retry until the # XXX: this handling is not perfect, we should probably retry until the
# correct number of bytes are read! # correct number of bytes are read!
raise newException(MalformedMessageError, "AuthAck message has incorrect size") raise newException(MalformedMessageError, "AuthAck message has incorrect size")
check initiator.decodeAckMessage(authAck) check handshake.decodeAckMessage(^ackMsg)
check initiator.getSecrets(authCiphertext, authAck, result.sessionSecrets) check handshake.getSecrets(^authMsg, ^ackMsg, result.sessionSecrets)
var var
# XXX: TODO # XXX: TODO