Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util: rename bnToRlp to bnToUnpaddedBuffer #1293

Merged
merged 6 commits into from
Jun 11, 2021
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
14 changes: 7 additions & 7 deletions packages/block/src/header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Address,
BN,
bnToHex,
bnToUnpaddedBuffer,
ecrecover,
ecsign,
intToBuffer,
Expand All @@ -11,7 +12,6 @@ import {
rlp,
rlphash,
toBuffer,
unpadBuffer,
zeros,
} from 'ethereumjs-util'
import { HeaderData, JsonHeader, BlockHeaderBuffer, Blockchain, BlockOptions } from './types'
Expand Down Expand Up @@ -648,18 +648,18 @@ export class BlockHeader {
this.transactionsTrie,
this.receiptTrie,
this.bloom,
unpadBuffer(toBuffer(this.difficulty)), // we unpadBuffer, because toBuffer(new BN(0)) == <Buffer 00>
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
Expand Down
32 changes: 20 additions & 12 deletions packages/tx/src/eip1559Transaction.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -252,18 +260,18 @@ export default class FeeMarketEIP1559Transaction extends BaseTransaction<FeeMark
*/
raw(): FeeMarketEIP1559ValuesArray {
return [
bnToRlp(this.chainId),
bnToRlp(this.nonce),
bnToRlp(this.maxPriorityFeePerGas),
bnToRlp(this.maxFeePerGas),
bnToRlp(this.gasLimit),
bnToUnpaddedBuffer(this.chainId),
bnToUnpaddedBuffer(this.nonce),
bnToUnpaddedBuffer(this.maxPriorityFeePerGas),
bnToUnpaddedBuffer(this.maxFeePerGas),
bnToUnpaddedBuffer(this.gasLimit),
this.to !== undefined ? this.to.buf : Buffer.from([]),
bnToRlp(this.value),
bnToUnpaddedBuffer(this.value),
this.data,
this.accessList,
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([]),
]
}

Expand Down Expand Up @@ -351,8 +359,8 @@ export default class FeeMarketEIP1559Transaction extends BaseTransaction<FeeMark
return ecrecover(
msgHash,
v!.addn(27), // Recover the 27 which was stripped from ecsign
bnToRlp(r!),
bnToRlp(s!)
bnToUnpaddedBuffer(r!),
bnToUnpaddedBuffer(s!)
)
} catch (e) {
throw new Error('Invalid Signature')
Expand Down
30 changes: 19 additions & 11 deletions packages/tx/src/eip2930Transaction.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -223,17 +231,17 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
*/
raw(): AccessListEIP2930ValuesArray {
return [
bnToRlp(this.chainId),
bnToRlp(this.nonce),
bnToRlp(this.gasPrice),
bnToRlp(this.gasLimit),
bnToUnpaddedBuffer(this.chainId),
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.accessList,
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([]),
]
}

Expand Down Expand Up @@ -319,8 +327,8 @@ export default class AccessListEIP2930Transaction extends BaseTransaction<Access
return ecrecover(
msgHash,
yParity!.addn(27), // Recover the 27 which was stripped from ecsign
bnToRlp(r!),
bnToRlp(s!)
bnToUnpaddedBuffer(r!),
bnToUnpaddedBuffer(s!)
)
} catch (e) {
throw new Error('Invalid Signature')
Expand Down
28 changes: 14 additions & 14 deletions packages/tx/src/legacyTransaction.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
BN,
bnToHex,
bnToRlp,
bnToUnpaddedBuffer,
ecrecover,
rlp,
rlphash,
Expand Down Expand Up @@ -125,15 +125,15 @@ export default class Transaction extends BaseTransaction<Transaction> {
*/
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([]),
]
}

Expand All @@ -156,11 +156,11 @@ export default class Transaction extends BaseTransaction<Transaction> {

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,
]

Expand Down Expand Up @@ -244,8 +244,8 @@ export default class Transaction extends BaseTransaction<Transaction> {
return ecrecover(
msgHash,
v!,
bnToRlp(r!),
bnToRlp(s!),
bnToUnpaddedBuffer(r!),
bnToUnpaddedBuffer(s!),
this._signedTxImplementsEIP155() ? this.common.chainIdBN() : undefined
)
} catch (e) {
Expand Down
9 changes: 7 additions & 2 deletions packages/util/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
]
}

/**
Expand Down
13 changes: 11 additions & 2 deletions packages/util/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
9 changes: 9 additions & 0 deletions packages/util/test/types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
bufferToHex,
intToHex,
bnToHex,
bnToUnpaddedBuffer,
toBuffer,
} from '../src'

Expand Down Expand Up @@ -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()
})
})