Skip to content
This repository has been archived by the owner on Dec 10, 2020. It is now read-only.

Update rlp-encoding lib to rlp #94

Merged
merged 2 commits into from
Nov 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions examples/peer-communication.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import * as devp2p from '../src/index'
import { ETH, Peer } from '../src/index'
import Common from '@ethereumjs/common'
import { Transaction } from '@ethereumjs/tx'
import { Block, BlockHeader } from '@ethereumjs/block'
import assert from 'assert'
import { randomBytes } from 'crypto'
import LRUCache from 'lru-cache'
import ms from 'ms'
import chalk from 'chalk'
import assert from 'assert'
import { randomBytes } from 'crypto'
import rlp from 'rlp-encoding'
import * as rlp from 'rlp'
import Common from '@ethereumjs/common'
import { Transaction } from '@ethereumjs/tx'
import { Block, BlockHeader } from '@ethereumjs/block'
import * as devp2p from '../src/index'
import { ETH, Peer } from '../src/index'

const PRIVATE_KEY = randomBytes(32)
const CHAIN_ID = 1
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"node": ">=10.0"
},
"scripts": {
"build": "ethereumjs-config-build",
"build": "ethereumjs-config-ts-build",
"prepublishOnly": "npm run test && npm run build",
"coverage": "nyc --reporter=lcov npm run test",
"docs:build": "typedoc",
Expand All @@ -68,7 +68,7 @@
"keccak": "^3.0.1",
"lru-cache": "^5.1.1",
"ms": "^0.7.1",
"rlp-encoding": "^3.0.0",
"rlp": "^2.2.6",
"secp256k1": "^4.0.2"
},
"devDependencies": {
Expand Down
5 changes: 0 additions & 5 deletions src/@types/rlp-encoding.d.ts

This file was deleted.

7 changes: 3 additions & 4 deletions src/dpt/message.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
/// <reference path="../@types/rlp-encoding.d.ts"/>
import { debug as createDebugLogger } from 'debug'
import ip from 'ip'
import rlp from 'rlp-encoding'
import * as rlp from 'rlp'
import secp256k1 from 'secp256k1'
import { keccak256, int2buffer, buffer2int, assertEq } from '../util'
import { keccak256, int2buffer, buffer2int, assertEq, unstrictDecode } from '../util'

const debug = createDebugLogger('devp2p:dpt:server')

Expand Down Expand Up @@ -193,7 +192,7 @@ export function decode(buffer: Buffer) {
const type = typedata[0]
const typename = types.byType[type]
if (typename === undefined) throw new Error(`Invalid type: ${type}`)
const data = messages[typename].decode(rlp.decode(typedata.slice(1)))
const data = messages[typename].decode(unstrictDecode(typedata.slice(1)))

const sighash = keccak256(typedata)
const signature = buffer.slice(32, 96)
Expand Down
8 changes: 4 additions & 4 deletions src/eth/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events'
import rlp from 'rlp-encoding'
import * as rlp from 'rlp'
import ms from 'ms'
import { int2buffer, buffer2int, assertEq, formatLogId, formatLogData } from '../util'
import { Peer, DISCONNECT_REASONS } from '../rlpx/peer'
Expand Down Expand Up @@ -36,7 +36,7 @@ export class ETH extends EventEmitter {
static eth63 = { name: 'eth', version: 63, length: 17, constructor: ETH }

_handleMessage(code: ETH.MESSAGE_CODES, data: any) {
const payload = rlp.decode(data)
const payload = rlp.decode(data) as unknown
if (code !== ETH.MESSAGE_CODES.STATUS) {
const debugMsg = `Received ${this.getMsgPrefix(code)} message from ${
this._peer._socket.remoteAddress
Expand All @@ -47,7 +47,7 @@ export class ETH extends EventEmitter {
switch (code) {
case ETH.MESSAGE_CODES.STATUS:
assertEq(this._peerStatus, null, 'Uncontrolled status message', debug)
this._peerStatus = payload
this._peerStatus = payload as ETH.StatusMsg
debug(
`Received ${this.getMsgPrefix(code)} message from ${this._peer._socket.remoteAddress}:${
this._peer._socket.remotePort
Expand Down Expand Up @@ -126,7 +126,7 @@ export class ETH extends EventEmitter {
this._peer._socket.remotePort
} (eth${this._version}): ${this._getStatusString(this._status)}`
)
this._send(ETH.MESSAGE_CODES.STATUS, rlp.encode(this._status))
this._send(ETH.MESSAGE_CODES.STATUS, rlp.encode(this._status as any))
this._handleStatus()
}

Expand Down
7 changes: 4 additions & 3 deletions src/rlpx/ecies.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import crypto, { Decipher } from 'crypto'
import { debug as createDebugLogger } from 'debug'
import { publicKeyCreate, ecdh, ecdsaRecover, ecdsaSign } from 'secp256k1'
import rlp from 'rlp-encoding'
import * as rlp from 'rlp'
import { unstrictDecode } from '../util'
import { MAC } from './mac'

import {
Expand Down Expand Up @@ -239,7 +240,7 @@ export class ECIES {
remotePublicKey = id2pk(decrypted.slice(97, 161))
nonce = decrypted.slice(161, 193)
} else {
const decoded = rlp.decode(decrypted)
const decoded = unstrictDecode(decrypted) as Buffer[]

signature = decoded[0].slice(0, 64)
recoveryId = decoded[0][64]
Expand Down Expand Up @@ -317,7 +318,7 @@ export class ECIES {
remoteEphemeralPublicKey = id2pk(decrypted.slice(0, 64))
remoteNonce = decrypted.slice(64, 96)
} else {
const decoded = rlp.decode(decrypted)
const decoded = unstrictDecode(decrypted) as Buffer[]

remoteEphemeralPublicKey = id2pk(decoded[0])
remoteNonce = decoded[1]
Expand Down
4 changes: 2 additions & 2 deletions src/rlpx/peer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { EventEmitter } from 'events'
import rlp from 'rlp-encoding'
import * as rlp from 'rlp'
import * as util from '../util'
import BufferList = require('bl')
import ms from 'ms'
Expand Down Expand Up @@ -218,7 +218,7 @@ export class Peer extends EventEmitter {
]

if (!this._closed) {
if (this._sendMessage(PREFIXES.HELLO, rlp.encode(payload))) {
if (this._sendMessage(PREFIXES.HELLO, rlp.encode(payload as any))) {
this._weHello = payload
}
if (this._hello) {
Expand Down
9 changes: 8 additions & 1 deletion src/util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import assert from 'assert'
import { randomBytes } from 'crypto'
import { privateKeyVerify, publicKeyConvert } from 'secp256k1'
import createKeccakHash from 'keccak'
import assert from 'assert'
import { decode } from 'rlp'

export function keccak256(...buffers: Buffer[]) {
const buffer = Buffer.concat(buffers)
Expand Down Expand Up @@ -106,3 +107,9 @@ export class Deferred<T> {
export function createDeferred<T>(): Deferred<T> {
return new Deferred()
}

export function unstrictDecode(value: Buffer) {
// rlp library throws on remainder.length !== 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this an issue of some kind? If so can you open on rlp?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too sure, I think checking for 0 remainder is just a cautious check. Could we solicit feedback from some other team members?

This little bypass is just a trick in the rlp src that if stream is passed as true, it'll return the decoded data before it does the remainder check 😆 but it is fragile workaround: https://github.com/ethereumjs/rlp/blob/75f398193fef0c2416fd0f68b4e2794273105620/src/index.ts#L71-L76

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, maybe we'll find this out along the way when we have more to do with the devp2p library and questions like this can benefit from the daily practical experience and some grown intuition. Will leave for now with this comment.

// this utility function bypasses that
return (decode(value, true) as any).data
}