ethereumjs-util
Advanced tools
Comparing version 7.0.5 to 7.0.6
@@ -9,6 +9,49 @@ # Changelog | ||
## [7.0.6] - [UNRELEASED] | ||
## [7.0.6] - 2020-10-07 | ||
[ ADD REFERENCES TO YOUR WORK HERE UPON PRs. PLEASE ADOPT THE VERSION IF YOUR PR REQUIRES. ] | ||
### New `Account` class | ||
This release adds a new `Account` class intended as a modern replacement for `ethereumjs-account`. It has a shape of `Account(nonce?: BN, balance?: BN, stateRoot?: Buffer, codeHash?: Buffer)`. | ||
**Instantiation** | ||
The static factory methods assist in creating an `Account` object from varying data types: `Object: fromAccountData`, `RLP: fromRlpSerializedAccount`, and `Array: fromValuesArray`. | ||
**Methods**: `isEmpty(): boolean`, `isContract(): boolean`, `serialize(): Buffer` | ||
Example usage: | ||
```typescript | ||
import { Account, BN } from 'ethereumjs-util' | ||
const account = new Account( | ||
new BN(0), // nonce, default: 0 | ||
new BN(10).pow(new BN(18)), // balance, default: 0 | ||
undefined, // stateRoot, default: KECCAK256_RLP (hash of RLP of null) | ||
undefined, // codeHash, default: KECCAK256_NULL (hash of null) | ||
) | ||
``` | ||
For more info see the documentation, examples of usage in `test/account.spec.ts` or | ||
PR [#275](https://github.com/ethereumjs/ethereumjs-util/pull/275). | ||
### New export: TypeScript types | ||
A new file with helpful TypeScript types has been added to the exports of this project, | ||
see PR [#275](https://github.com/ethereumjs/ethereumjs-util/pull/275). | ||
In this release it contains `BNLike`, `BufferLike`, and `TransformableToBuffer`. | ||
### Address.toBuffer() | ||
The Address class has as a new method `address.toBuffer()` that will give you a copy of the underlying `address.buf` | ||
(PR [#277](https://github.com/ethereumjs/ethereumjs-util/pull/277)). | ||
### `toBuffer()` now converts TransformableToBuffer | ||
The `toBuffer()` exported function now additionally converts any object with a `toBuffer()` method | ||
(PR [#277](https://github.com/ethereumjs/ethereumjs-util/pull/277)). | ||
[7.0.6]: https://github.com/ethereumjs/ethereumjs-util/compare/v7.0.5...v7.0.6 | ||
## [7.0.5] - 2020-09-09 | ||
@@ -15,0 +58,0 @@ |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import { BNLike, BufferLike } from './types'; | ||
export interface AccountData { | ||
nonce?: BNLike; | ||
balance?: BNLike; | ||
stateRoot?: BufferLike; | ||
codeHash?: BufferLike; | ||
} | ||
export declare class Account { | ||
nonce: BN; | ||
balance: BN; | ||
stateRoot: Buffer; | ||
codeHash: Buffer; | ||
static fromAccountData(accountData: AccountData): Account; | ||
static fromRlpSerializedAccount(serialized: Buffer): Account; | ||
static fromValuesArray(values: Buffer[]): Account; | ||
/** | ||
* This constructor assigns and validates the values. | ||
* Use the static factory methods to assist in creating an Account from varying data types. | ||
*/ | ||
constructor(nonce?: BN, balance?: BN, stateRoot?: Buffer, codeHash?: Buffer); | ||
private _validate; | ||
/** | ||
* Returns the RLP serialization of the account as a `Buffer`. | ||
*/ | ||
serialize(): Buffer; | ||
/** | ||
* Returns a `Boolean` determining if the account is a contract. | ||
*/ | ||
isContract(): boolean; | ||
/** | ||
* Returns a `Boolean` determining if the account is empty. | ||
* For more details about account emptiness see [EIP-161](https://eips.ethereum.org/EIPS/eip-161). | ||
* 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). | ||
*/ | ||
isEmpty(): boolean; | ||
} | ||
/** | ||
* Returns a zero address. | ||
*/ | ||
export declare const zeroAddress: () => string; | ||
/** | ||
* Checks if the address is a valid. Accepts checksummed addresses too. | ||
@@ -11,6 +44,2 @@ */ | ||
/** | ||
* Checks if a given address is a zero address. | ||
*/ | ||
export declare const isZeroAddress: (hexAddress: string) => boolean; | ||
/** | ||
* Returns a checksummed address. | ||
@@ -20,3 +49,3 @@ * | ||
* has the effect of checksummed addresses for one chain having invalid checksums for others. | ||
* For more details, consult EIP-1191. | ||
* For more details see [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191). | ||
* | ||
@@ -79,1 +108,9 @@ * WARNING: Checksums with and without the chainId will differ. As of 2019-06-26, the most commonly | ||
export declare const importPublic: (publicKey: Buffer) => Buffer; | ||
/** | ||
* Returns the zero address. | ||
*/ | ||
export declare const zeroAddress: () => string; | ||
/** | ||
* Checks if a given address is the zero address. | ||
*/ | ||
export declare const isZeroAddress: (hexAddress: string) => boolean; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.importPublic = exports.privateToPublic = exports.privateToAddress = exports.publicToAddress = exports.pubToAddress = exports.isValidPublic = exports.isValidPrivate = exports.generateAddress2 = exports.generateAddress = exports.isValidChecksumAddress = exports.toChecksumAddress = exports.isZeroAddress = exports.isValidAddress = exports.zeroAddress = void 0; | ||
var ethjsUtil = require("ethjs-util"); | ||
var _a = require('ethereum-cryptography/secp256k1'), privateKeyVerify = _a.privateKeyVerify, publicKeyCreate = _a.publicKeyCreate, publicKeyVerify = _a.publicKeyVerify, publicKeyConvert = _a.publicKeyConvert; | ||
exports.isZeroAddress = exports.zeroAddress = exports.importPublic = exports.privateToPublic = exports.privateToAddress = exports.publicToAddress = exports.pubToAddress = exports.isValidPublic = exports.isValidPrivate = exports.generateAddress2 = exports.generateAddress = exports.isValidChecksumAddress = exports.toChecksumAddress = exports.isValidAddress = exports.Account = void 0; | ||
var assert = require("assert"); | ||
var BN = require("bn.js"); | ||
var rlp = require("rlp"); | ||
var ethjs_util_1 = require("ethjs-util"); | ||
var constants_1 = require("./constants"); | ||
var bytes_1 = require("./bytes"); | ||
var hash_1 = require("./hash"); | ||
var helpers_1 = require("./helpers"); | ||
var types_1 = require("./types"); | ||
var _a = require('ethereum-cryptography/secp256k1'), privateKeyVerify = _a.privateKeyVerify, publicKeyCreate = _a.publicKeyCreate, publicKeyVerify = _a.publicKeyVerify, publicKeyConvert = _a.publicKeyConvert; | ||
var Account = /** @class */ (function () { | ||
/** | ||
* This constructor assigns and validates the values. | ||
* Use the static factory methods to assist in creating an Account from varying data types. | ||
*/ | ||
function Account(nonce, balance, stateRoot, codeHash) { | ||
if (nonce === void 0) { nonce = new BN(0); } | ||
if (balance === void 0) { balance = new BN(0); } | ||
if (stateRoot === void 0) { stateRoot = constants_1.KECCAK256_RLP; } | ||
if (codeHash === void 0) { codeHash = constants_1.KECCAK256_NULL; } | ||
this.nonce = nonce; | ||
this.balance = balance; | ||
this.stateRoot = stateRoot; | ||
this.codeHash = codeHash; | ||
this._validate(); | ||
} | ||
Account.fromAccountData = function (accountData) { | ||
var nonce = accountData.nonce, balance = accountData.balance, stateRoot = accountData.stateRoot, codeHash = accountData.codeHash; | ||
return new Account(nonce ? new BN(bytes_1.toBuffer(nonce)) : undefined, balance ? new BN(bytes_1.toBuffer(balance)) : undefined, stateRoot ? bytes_1.toBuffer(stateRoot) : undefined, codeHash ? bytes_1.toBuffer(codeHash) : undefined); | ||
}; | ||
Account.fromRlpSerializedAccount = function (serialized) { | ||
var values = rlp.decode(serialized); | ||
if (!Array.isArray(values)) { | ||
throw new Error('Invalid serialized account input. Must be array'); | ||
} | ||
return this.fromValuesArray(values); | ||
}; | ||
Account.fromValuesArray = function (values) { | ||
var nonce = values[0], balance = values[1], stateRoot = values[2], codeHash = values[3]; | ||
return new Account(nonce ? new BN(nonce) : undefined, balance ? new BN(balance) : undefined, stateRoot, codeHash); | ||
}; | ||
Account.prototype._validate = function () { | ||
if (this.nonce.lt(new BN(0))) { | ||
throw new Error('nonce must be greater than zero'); | ||
} | ||
if (this.balance.lt(new BN(0))) { | ||
throw new Error('balance must be greater than zero'); | ||
} | ||
if (this.stateRoot.length !== 32) { | ||
throw new Error('stateRoot must have a length of 32'); | ||
} | ||
if (this.codeHash.length !== 32) { | ||
throw new Error('codeHash must have a length of 32'); | ||
} | ||
}; | ||
/** | ||
* Returns the RLP serialization of the account as a `Buffer`. | ||
*/ | ||
Account.prototype.serialize = function () { | ||
return rlp.encode([types_1.bnToRlp(this.nonce), types_1.bnToRlp(this.balance), this.stateRoot, this.codeHash]); | ||
}; | ||
/** | ||
* Returns a `Boolean` determining if the account is a contract. | ||
*/ | ||
Account.prototype.isContract = function () { | ||
return !this.codeHash.equals(constants_1.KECCAK256_NULL); | ||
}; | ||
/** | ||
* Returns a `Boolean` determining if the account is empty. | ||
* For more details about account emptiness see [EIP-161](https://eips.ethereum.org/EIPS/eip-161). | ||
* 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). | ||
*/ | ||
Account.prototype.isEmpty = function () { | ||
return (this.balance.isZero() && | ||
this.nonce.isZero() && | ||
this.stateRoot.equals(constants_1.KECCAK256_RLP) && | ||
this.codeHash.equals(constants_1.KECCAK256_NULL)); | ||
}; | ||
return Account; | ||
}()); | ||
exports.Account = Account; | ||
/** | ||
* Returns a zero address. | ||
*/ | ||
exports.zeroAddress = function () { | ||
var addressLength = 20; | ||
var addr = bytes_1.zeros(addressLength); | ||
return bytes_1.bufferToHex(addr); | ||
}; | ||
/** | ||
* Checks if the address is a valid. Accepts checksummed addresses too. | ||
@@ -27,10 +93,2 @@ */ | ||
/** | ||
* Checks if a given address is a zero address. | ||
*/ | ||
exports.isZeroAddress = function (hexAddress) { | ||
helpers_1.assertIsHexString(hexAddress); | ||
var zeroAddr = exports.zeroAddress(); | ||
return zeroAddr === hexAddress; | ||
}; | ||
/** | ||
* Returns a checksummed address. | ||
@@ -40,3 +98,3 @@ * | ||
* has the effect of checksummed addresses for one chain having invalid checksums for others. | ||
* For more details, consult EIP-1191. | ||
* For more details see [EIP-1191](https://eips.ethereum.org/EIPS/eip-1191). | ||
* | ||
@@ -48,3 +106,3 @@ * WARNING: Checksums with and without the chainId will differ. As of 2019-06-26, the most commonly | ||
helpers_1.assertIsHexString(hexAddress); | ||
var address = ethjsUtil.stripHexPrefix(hexAddress).toLowerCase(); | ||
var address = ethjs_util_1.stripHexPrefix(hexAddress).toLowerCase(); | ||
var prefix = eip1191ChainId !== undefined ? eip1191ChainId.toString() + '0x' : ''; | ||
@@ -170,2 +228,18 @@ var hash = hash_1.keccakFromString(prefix + address).toString('hex'); | ||
}; | ||
/** | ||
* Returns the zero address. | ||
*/ | ||
exports.zeroAddress = function () { | ||
var addressLength = 20; | ||
var addr = bytes_1.zeros(addressLength); | ||
return bytes_1.bufferToHex(addr); | ||
}; | ||
/** | ||
* Checks if a given address is the zero address. | ||
*/ | ||
exports.isZeroAddress = function (hexAddress) { | ||
helpers_1.assertIsHexString(hexAddress); | ||
var zeroAddr = exports.zeroAddress(); | ||
return zeroAddr === hexAddress; | ||
}; | ||
//# sourceMappingURL=account.js.map |
@@ -46,2 +46,6 @@ /// <reference types="node" /> | ||
toString(): string; | ||
/** | ||
* Returns Buffer representation of address. | ||
*/ | ||
toBuffer(): Buffer; | ||
} |
@@ -77,2 +77,8 @@ "use strict"; | ||
}; | ||
/** | ||
* Returns Buffer representation of address. | ||
*/ | ||
Address.prototype.toBuffer = function () { | ||
return Buffer.from(this.buf); | ||
}; | ||
return Address; | ||
@@ -79,0 +85,0 @@ }()); |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import { TransformableToArray, TransformableToBuffer } from './types'; | ||
/** | ||
@@ -43,6 +44,7 @@ * Returns a buffer filled with 0s. | ||
/** | ||
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method. | ||
* Attempts to turn a value into a `Buffer`. | ||
* Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method. | ||
* @param v the value | ||
*/ | ||
export declare const toBuffer: (v: any) => Buffer; | ||
export declare const toBuffer: (v: string | number | BN | Buffer | Uint8Array | number[] | TransformableToArray | TransformableToBuffer | null | undefined) => Buffer; | ||
/** | ||
@@ -49,0 +51,0 @@ * Converts a `Buffer` to a `Number`. |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.baToJSON = exports.addHexPrefix = exports.toUnsigned = exports.fromSigned = exports.bufferToHex = exports.bufferToInt = exports.toBuffer = exports.unpadHexString = exports.unpadArray = exports.unpadBuffer = exports.setLengthRight = exports.setLengthLeft = exports.zeros = void 0; | ||
var ethjsUtil = require("ethjs-util"); | ||
var BN = require("bn.js"); | ||
var ethjs_util_1 = require("ethjs-util"); | ||
var helpers_1 = require("./helpers"); | ||
@@ -86,3 +86,3 @@ /** | ||
helpers_1.assertIsHexString(a); | ||
a = ethjsUtil.stripHexPrefix(a); | ||
a = ethjs_util_1.stripHexPrefix(a); | ||
return stripZeros(a); | ||
@@ -104,36 +104,36 @@ }; | ||
/** | ||
* Attempts to turn a value into a `Buffer`. As input it supports `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` method. | ||
* Attempts to turn a value into a `Buffer`. | ||
* Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method. | ||
* @param v the value | ||
*/ | ||
exports.toBuffer = function (v) { | ||
if (!Buffer.isBuffer(v)) { | ||
if (Array.isArray(v) || v instanceof Uint8Array) { | ||
v = Buffer.from(v); | ||
if (v === null || v === undefined) { | ||
return Buffer.allocUnsafe(0); | ||
} | ||
if (Buffer.isBuffer(v)) { | ||
return Buffer.from(v); | ||
} | ||
if (Array.isArray(v) || v instanceof Uint8Array) { | ||
return Buffer.from(v); | ||
} | ||
if (typeof v === 'string') { | ||
if (!ethjs_util_1.isHexString(v)) { | ||
throw new Error("Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: " + v); | ||
} | ||
else if (typeof v === 'string') { | ||
if (ethjsUtil.isHexString(v)) { | ||
v = Buffer.from(ethjsUtil.padToEven(ethjsUtil.stripHexPrefix(v)), 'hex'); | ||
} | ||
else { | ||
throw new Error("Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: " + v); | ||
} | ||
} | ||
else if (typeof v === 'number') { | ||
v = ethjsUtil.intToBuffer(v); | ||
} | ||
else if (v === null || v === undefined) { | ||
v = Buffer.allocUnsafe(0); | ||
} | ||
else if (BN.isBN(v)) { | ||
v = v.toArrayLike(Buffer); | ||
} | ||
else if (v.toArray) { | ||
// converts a BN to a Buffer | ||
v = Buffer.from(v.toArray()); | ||
} | ||
else { | ||
throw new Error('invalid type'); | ||
} | ||
return Buffer.from(ethjs_util_1.padToEven(ethjs_util_1.stripHexPrefix(v)), 'hex'); | ||
} | ||
return v; | ||
if (typeof v === 'number') { | ||
return ethjs_util_1.intToBuffer(v); | ||
} | ||
if (BN.isBN(v)) { | ||
return v.toArrayLike(Buffer); | ||
} | ||
if (v.toArray) { | ||
// converts a BN to a Buffer | ||
return Buffer.from(v.toArray()); | ||
} | ||
if (v.toBuffer) { | ||
return Buffer.from(v.toBuffer()); | ||
} | ||
throw new Error('invalid type'); | ||
}; | ||
@@ -177,3 +177,3 @@ /** | ||
} | ||
return ethjsUtil.isHexPrefixed(str) ? str : '0x' + str; | ||
return ethjs_util_1.isHexPrefixed(str) ? str : '0x' + str; | ||
}; | ||
@@ -180,0 +180,0 @@ /** |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assertIsString = exports.assertIsArray = exports.assertIsBuffer = exports.assertIsHexString = void 0; | ||
var ethjsUtil = require("ethjs-util"); | ||
var ethjs_util_1 = require("ethjs-util"); | ||
/** | ||
@@ -10,3 +10,3 @@ * Throws if a string is not hex prefixed | ||
exports.assertIsHexString = function (input) { | ||
if (!ethjsUtil.isHexString(input)) { | ||
if (!ethjs_util_1.isHexString(input)) { | ||
var msg = "This method only supports 0x-prefixed hex strings but input was: " + input; | ||
@@ -13,0 +13,0 @@ throw new Error(msg); |
@@ -7,3 +7,3 @@ /// <reference path="@types/ethjs-util/index.d.ts" /> | ||
/** | ||
* Public-key cryptography (secp256k1) and addresses | ||
* Account class and helper functions | ||
*/ | ||
@@ -36,4 +36,8 @@ export * from './account'; | ||
/** | ||
* Helpful TypeScript types | ||
*/ | ||
export * from './types'; | ||
/** | ||
* Export ethjs-util methods | ||
*/ | ||
export * from 'ethjs-util'; |
@@ -19,3 +19,3 @@ "use strict"; | ||
/** | ||
* Public-key cryptography (secp256k1) and addresses | ||
* Account class and helper functions | ||
*/ | ||
@@ -48,2 +48,6 @@ __exportStar(require("./account"), exports); | ||
/** | ||
* Helpful TypeScript types | ||
*/ | ||
__exportStar(require("./types"), exports); | ||
/** | ||
* Export ethjs-util methods | ||
@@ -50,0 +54,0 @@ */ |
{ | ||
"name": "ethereumjs-util", | ||
"version": "7.0.5", | ||
"version": "7.0.6", | ||
"description": "a collection of utility functions for Ethereum", | ||
@@ -23,3 +23,3 @@ "main": "dist/index.js", | ||
"test:browser": "karma start karma.conf.js", | ||
"test:node": "nyc --reporter=lcov mocha 'test/*.spec.ts' -- --require ts-node/register", | ||
"test:node": "nyc --reporter=lcov mocha --require ts-node/register 'test/*.spec.ts'", | ||
"tsc": "ethereumjs-config-tsc", | ||
@@ -26,0 +26,0 @@ "tslint": "ethereumjs-config-tslint", |
@@ -35,2 +35,3 @@ # SYNOPSIS | ||
- [account](docs/modules/_account_.md) | ||
- Account class | ||
- Private/public key and address-related functionality (creation, validation, conversion) | ||
@@ -50,2 +51,4 @@ - [address](docs/modules/_address_.md) | ||
- Signing, signature validation, conversion, recovery | ||
- [types](docs/modules/_types_.md) | ||
- Helpful TypeScript types | ||
- [externals](docs/modules/_externals_.md) | ||
@@ -52,0 +55,0 @@ - Helper methods from `ethjs-util` |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
122986
40
1602
99