Skip to content
This repository was archived by the owner on Jun 17, 2021. It is now read-only.

Commit dd2882d

Browse files
authored
Merge pull request #279 from ethereumjs/fix-account-is-empty-and-release
Update Account.isEmpty to be EIP-161 compliant / New v7.0.7 Release
2 parents c1787c1 + a702514 commit dd2882d

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
66
(modification: no type change headlines) and this project adheres to
77
[Semantic Versioning](http://semver.org/spec/v2.0.0.html).
88

9+
## [7.0.7] - 2020-10-15
10+
11+
- Removed `stateRoot` check for `Account.isEmpty()` to make emptiness check `EIP-161` compliant, PR [#279](https://github.com/ethereumjs/ethereumjs-util/pull/279)
12+
- Added type `AddressLike` and helper `bnToHex()`, PR [#279](https://github.com/ethereumjs/ethereumjs-util/pull/279)
13+
- Added `account.raw()` which returns a Buffer Array of the raw Buffers for the account in order, PR [#279](https://github.com/ethereumjs/ethereumjs-util/pull/279)
14+
15+
[7.0.7]: https://github.com/ethereumjs/ethereumjs-util/compare/v7.0.6...v7.0.7
16+
917
## [7.0.6] - 2020-10-07
1018

1119
### New `Account` class

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ethereumjs-util",
3-
"version": "7.0.6",
3+
"version": "7.0.7",
44
"description": "a collection of utility functions for Ethereum",
55
"main": "dist/index.js",
66
"types": "./dist/index.d.ts",

src/account.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,18 @@ export class Account {
9393
}
9494
}
9595

96+
/**
97+
* Returns a Buffer Array of the raw Buffers for the account, in order.
98+
*/
99+
raw(): Buffer[] {
100+
return [bnToRlp(this.nonce), bnToRlp(this.balance), this.stateRoot, this.codeHash]
101+
}
102+
96103
/**
97104
* Returns the RLP serialization of the account as a `Buffer`.
98105
*/
99106
serialize(): Buffer {
100-
return rlp.encode([bnToRlp(this.nonce), bnToRlp(this.balance), this.stateRoot, this.codeHash])
107+
return rlp.encode(this.raw())
101108
}
102109

103110
/**
@@ -108,17 +115,12 @@ export class Account {
108115
}
109116

110117
/**
111-
* Returns a `Boolean` determining if the account is empty.
112-
* For more details about account emptiness see [EIP-161](https://eips.ethereum.org/EIPS/eip-161).
113-
* Note: The stateRoot is also checked to be empty since in Frontier it was possible to create a contract with no code where nonce remained 0 but some values were written to storage in the constructor (thus stateRoot is not KECCAK256_RLP).
118+
* Returns a `Boolean` determining if the account is empty complying to the definition of
119+
* account emptiness in [EIP-161](https://eips.ethereum.org/EIPS/eip-161):
120+
* "An account is considered empty when it has no code and zero nonce and zero balance."
114121
*/
115122
isEmpty(): boolean {
116-
return (
117-
this.balance.isZero() &&
118-
this.nonce.isZero() &&
119-
this.stateRoot.equals(KECCAK256_RLP) &&
120-
this.codeHash.equals(KECCAK256_NULL)
121-
)
123+
return this.balance.isZero() && this.nonce.isZero() && this.codeHash.equals(KECCAK256_NULL)
122124
}
123125
}
124126

src/types.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as BN from 'bn.js'
2+
import { Address } from './address'
23
import { unpadBuffer } from './bytes'
34

45
/*
@@ -23,6 +24,12 @@ export type BufferLike =
2324
*/
2425
export type PrefixedHexString = string
2526

27+
/**
28+
* A type that represents an Address-like value.
29+
* To convert to address, use `new Address(toBuffer(value))`
30+
*/
31+
export type AddressLike = Address | Buffer | string
32+
2633
/*
2734
* A type that represents an object that has a `toArray()` method.
2835
*/
@@ -39,6 +46,13 @@ export interface TransformableToBuffer {
3946
toArray?(): Uint8Array
4047
}
4148

49+
/**
50+
* Convert BN to 0x-prefixed hex string.
51+
*/
52+
export function bnToHex(value: BN): PrefixedHexString {
53+
return `0x${value.toString(16)}`
54+
}
55+
4256
/**
4357
* Convert value from BN to RLP (unpadded buffer)
4458
* @param value value to convert

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy