diff --git a/packages/block/src/header.ts b/packages/block/src/header.ts index e708f4f563..94b1c3568e 100644 --- a/packages/block/src/header.ts +++ b/packages/block/src/header.ts @@ -3,6 +3,7 @@ import { Address, BN, bnToHex, + bnToUnpaddedBuffer, ecrecover, ecsign, intToBuffer, @@ -11,7 +12,6 @@ import { rlp, rlphash, toBuffer, - unpadBuffer, zeros, } from 'ethereumjs-util' import { HeaderData, JsonHeader, BlockHeaderBuffer, Blockchain, BlockOptions } from './types' @@ -648,18 +648,18 @@ export class BlockHeader { this.transactionsTrie, this.receiptTrie, this.bloom, - unpadBuffer(toBuffer(this.difficulty)), // we unpadBuffer, because toBuffer(new BN(0)) == - unpadBuffer(toBuffer(this.number)), - unpadBuffer(toBuffer(this.gasLimit)), - unpadBuffer(toBuffer(this.gasUsed)), - unpadBuffer(toBuffer(this.timestamp)), + bnToUnpaddedBuffer(this.difficulty), + bnToUnpaddedBuffer(this.number), + bnToUnpaddedBuffer(this.gasLimit), + bnToUnpaddedBuffer(this.gasUsed), + bnToUnpaddedBuffer(this.timestamp), this.extraData, this.mixHash, this.nonce, ] if (this._common.isActivatedEIP(1559)) { - rawItems.push(unpadBuffer(toBuffer(this.baseFeePerGas))) + rawItems.push(bnToUnpaddedBuffer(this.baseFeePerGas!)) } return rawItems diff --git a/packages/tx/src/eip1559Transaction.ts b/packages/tx/src/eip1559Transaction.ts index a7d73abd2a..aa554a0360 100644 --- a/packages/tx/src/eip1559Transaction.ts +++ b/packages/tx/src/eip1559Transaction.ts @@ -1,5 +1,13 @@ +import { + BN, + bnToHex, + bnToUnpaddedBuffer, + ecrecover, + keccak256, + rlp, + toBuffer, +} from 'ethereumjs-util' import Common from '@ethereumjs/common' -import { BN, bnToHex, bnToRlp, ecrecover, keccak256, rlp, toBuffer } from 'ethereumjs-util' import { BaseTransaction } from './baseTransaction' import { AccessList, @@ -252,18 +260,18 @@ export default class FeeMarketEIP1559Transaction extends BaseTransaction { */ raw(): TxValuesArray { return [ - bnToRlp(this.nonce), - bnToRlp(this.gasPrice), - bnToRlp(this.gasLimit), + bnToUnpaddedBuffer(this.nonce), + bnToUnpaddedBuffer(this.gasPrice), + bnToUnpaddedBuffer(this.gasLimit), this.to !== undefined ? this.to.buf : Buffer.from([]), - bnToRlp(this.value), + bnToUnpaddedBuffer(this.value), this.data, - this.v !== undefined ? bnToRlp(this.v) : Buffer.from([]), - this.r !== undefined ? bnToRlp(this.r) : Buffer.from([]), - this.s !== undefined ? bnToRlp(this.s) : Buffer.from([]), + this.v !== undefined ? bnToUnpaddedBuffer(this.v) : Buffer.from([]), + this.r !== undefined ? bnToUnpaddedBuffer(this.r) : Buffer.from([]), + this.s !== undefined ? bnToUnpaddedBuffer(this.s) : Buffer.from([]), ] } @@ -156,11 +156,11 @@ export default class Transaction extends BaseTransaction { private _getMessageToSign(withEIP155: boolean) { const values = [ - bnToRlp(this.nonce), - bnToRlp(this.gasPrice), - bnToRlp(this.gasLimit), + bnToUnpaddedBuffer(this.nonce), + bnToUnpaddedBuffer(this.gasPrice), + bnToUnpaddedBuffer(this.gasLimit), this.to !== undefined ? this.to.buf : Buffer.from([]), - bnToRlp(this.value), + bnToUnpaddedBuffer(this.value), this.data, ] @@ -244,8 +244,8 @@ export default class Transaction extends BaseTransaction { return ecrecover( msgHash, v!, - bnToRlp(r!), - bnToRlp(s!), + bnToUnpaddedBuffer(r!), + bnToUnpaddedBuffer(s!), this._signedTxImplementsEIP155() ? this.common.chainIdBN() : undefined ) } catch (e) { diff --git a/packages/util/src/account.ts b/packages/util/src/account.ts index a06e4ccb70..13509db99f 100644 --- a/packages/util/src/account.ts +++ b/packages/util/src/account.ts @@ -12,7 +12,7 @@ import { KECCAK256_RLP, KECCAK256_NULL } from './constants' import { zeros, bufferToHex, toBuffer } from './bytes' import { keccak, keccak256, keccakFromString, rlphash } from './hash' import { assertIsString, assertIsHexString, assertIsBuffer } from './helpers' -import { BNLike, BufferLike, bnToRlp, toType, TypeOutput } from './types' +import { BNLike, BufferLike, bnToUnpaddedBuffer, toType, TypeOutput } from './types' export interface AccountData { nonce?: BNLike @@ -91,7 +91,12 @@ export class Account { * Returns a Buffer Array of the raw Buffers for the account, in order. */ raw(): Buffer[] { - return [bnToRlp(this.nonce), bnToRlp(this.balance), this.stateRoot, this.codeHash] + return [ + bnToUnpaddedBuffer(this.nonce), + bnToUnpaddedBuffer(this.balance), + this.stateRoot, + this.codeHash, + ] } /** diff --git a/packages/util/src/types.ts b/packages/util/src/types.ts index 1423325657..782d62b13b 100644 --- a/packages/util/src/types.ts +++ b/packages/util/src/types.ts @@ -55,15 +55,24 @@ export function bnToHex(value: BN): PrefixedHexString { } /** - * Convert value from BN to RLP (unpadded buffer) + * Convert value from BN to an unpadded Buffer + * (useful for RLP transport) * @param value value to convert */ -export function bnToRlp(value: BN): Buffer { +export function bnToUnpaddedBuffer(value: BN): Buffer { // Using `bn.toArrayLike(Buffer)` instead of `bn.toBuffer()` // for compatibility with browserify and similar tools return unpadBuffer(value.toArrayLike(Buffer)) } +/** + * Deprecated alias for bnToUnpaddedBuffer() + * @deprecated + */ +export function bnToRlp(value: BN): Buffer { + return bnToUnpaddedBuffer(value) +} + /** * Type output options */ diff --git a/packages/util/test/types.spec.ts b/packages/util/test/types.spec.ts index 3a8602c17f..d964fa7e34 100644 --- a/packages/util/test/types.spec.ts +++ b/packages/util/test/types.spec.ts @@ -7,6 +7,7 @@ import { bufferToHex, intToHex, bnToHex, + bnToUnpaddedBuffer, toBuffer, } from '../src' @@ -122,3 +123,11 @@ tape('toType', function (t) { }) }) }) + +tape('bnToUnpaddedBuffer', function (t) { + t.test('should equal unpadded buffer value', function (st) { + st.ok(bnToUnpaddedBuffer(new BN(0)).equals(Buffer.from([]))) + st.ok(bnToUnpaddedBuffer(new BN(100)).equals(Buffer.from('64', 'hex'))) + st.end() + }) +})