ethereumjs-util
Advanced tools
Comparing version 7.0.8 to 7.0.9
@@ -9,2 +9,26 @@ # Changelog | ||
## [7.0.9] - 2021-03-04 | ||
This release adds support for very high `chainId` numbers exceeding `MAX_SAFE_INTEGER` (an example is the chain ID `34180983699157880` used for the ephemeral Yolov3 testnet preparing for the `berlin` hardfork, but high chain IDs might be used for things like private test networks and the like as well). | ||
Function signatures for methods in `address` and `signature` are therefore expanded to allow for a `BNLike` input type (`BN | PrefixedHexString | number | Buffer`) for chain ID related parameters. | ||
All function signatures are still taking in a `number` input for backwards-compatibility reasons. If you use one of the following functions to implement generic use cases in your library where the chain ID is not yet known it is recommended to updated to one of the other input types (with plain `Buffer` likely be the most future-proof). Note that on some functions this changes the return value as well. | ||
- `account`: `toChecksumAddresss(hexAddress: string, eip1191ChainId?: number): string` | ||
- -> `toChecksumAddress = function(hexAddress: string, eip1191ChainId?: BNLike): string` | ||
- `account`: `isValidChecksumAddress(hexAddress: string, eip1191ChainId?: number)` | ||
- -> `isValidChecksumAddress(hexAddress: string, eip1191ChainId?: BNLike)` | ||
- `signature`: `ecsign(msgHash: Buffer, privateKey: Buffer, chainId?: number): ECDSASignature` | ||
- -> `ecsign(msgHash: Buffer, privateKey: Buffer, chainId?: number): ECDSASignature` (return value stays the same on `number` input) | ||
- -> `ecsign(msgHash: Buffer, privateKey: Buffer, chainId: BNLike): ECDSASignatureBuffer` (changed return value for other type inputs) | ||
- `signature`: `ecrecover(msgHash: Buffer, v: number, r: Buffer, s: Buffer, chainId?: number): Buffer` | ||
- -> `ecrecover(msgHash: Buffer, v: BNLike, r: Buffer, s: Buffer, chainId?: BNLike): Buffer` | ||
- `signature`: `toRpcSig(v: number, r: Buffer, s: Buffer, chainId?: number): string` | ||
- -> `toRpcSig(v: BNLike, r: Buffer, s: Buffer, chainId?: BNLike): string` | ||
- `signature`: `isValidSignature(v: number, r: Buffer, s: Buffer, homesteadOrLater: boolean = true, chainId?: number)` | ||
- -> `isValidSignature(v: BNLike, r: Buffer, s: Buffer, homesteadOrLater: boolean = true, chainId?: BNLike)` | ||
[7.0.9]: https://github.com/ethereumjs/ethereumjs-util/compare/v7.0.8...v7.0.9 | ||
## [7.0.8] - 2021-02-01 | ||
@@ -11,0 +35,0 @@ |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import BN from 'bn.js'; | ||
import { BNLike, BufferLike } from './types'; | ||
@@ -57,3 +57,3 @@ export interface AccountData { | ||
*/ | ||
export declare const toChecksumAddress: (hexAddress: string, eip1191ChainId?: number | undefined) => string; | ||
export declare const toChecksumAddress: (hexAddress: string, eip1191ChainId?: string | number | Buffer | BN | undefined) => string; | ||
/** | ||
@@ -64,3 +64,3 @@ * Checks if the address is a valid checksummed address. | ||
*/ | ||
export declare const isValidChecksumAddress: (hexAddress: string, eip1191ChainId?: number | undefined) => boolean; | ||
export declare const isValidChecksumAddress: (hexAddress: string, eip1191ChainId?: string | number | Buffer | BN | undefined) => boolean; | ||
/** | ||
@@ -99,11 +99,11 @@ * Generates an address of a newly created contract. | ||
/** | ||
* Returns the ethereum address of a given private key. | ||
* Returns the ethereum public key of a given private key. | ||
* @param privateKey A private key must be 256 bits wide | ||
*/ | ||
export declare const privateToAddress: (privateKey: Buffer) => Buffer; | ||
export declare const privateToPublic: (privateKey: Buffer) => Buffer; | ||
/** | ||
* Returns the ethereum public key of a given private key. | ||
* Returns the ethereum address of a given private key. | ||
* @param privateKey A private key must be 256 bits wide | ||
*/ | ||
export declare const privateToPublic: (privateKey: Buffer) => Buffer; | ||
export declare const privateToAddress: (privateKey: Buffer) => Buffer; | ||
/** | ||
@@ -110,0 +110,0 @@ * Converts a public key to the Ethereum format. |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
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 () { | ||
exports.isZeroAddress = exports.zeroAddress = exports.importPublic = exports.privateToAddress = exports.privateToPublic = exports.publicToAddress = exports.pubToAddress = exports.isValidPublic = exports.isValidPrivate = exports.generateAddress2 = exports.generateAddress = exports.isValidChecksumAddress = exports.toChecksumAddress = exports.isValidAddress = exports.Account = void 0; | ||
const assert_1 = __importDefault(require("assert")); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const rlp = __importStar(require("rlp")); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const constants_1 = require("./constants"); | ||
const bytes_1 = require("./bytes"); | ||
const hash_1 = require("./hash"); | ||
const helpers_1 = require("./helpers"); | ||
const types_1 = require("./types"); | ||
const { privateKeyVerify, publicKeyCreate, publicKeyVerify, publicKeyConvert } = require('ethereum-cryptography/secp256k1'); | ||
class Account { | ||
/** | ||
@@ -19,7 +41,3 @@ * This constructor assigns and validates the values. | ||
*/ | ||
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; } | ||
constructor(nonce = new bn_js_1.default(0), balance = new bn_js_1.default(0), stateRoot = constants_1.KECCAK256_RLP, codeHash = constants_1.KECCAK256_NULL) { | ||
this.nonce = nonce; | ||
@@ -31,8 +49,8 @@ this.balance = balance; | ||
} | ||
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); | ||
static fromAccountData(accountData) { | ||
const { nonce, balance, stateRoot, codeHash } = accountData; | ||
return new Account(nonce ? new bn_js_1.default(bytes_1.toBuffer(nonce)) : undefined, balance ? new bn_js_1.default(bytes_1.toBuffer(balance)) : undefined, stateRoot ? bytes_1.toBuffer(stateRoot) : undefined, codeHash ? bytes_1.toBuffer(codeHash) : undefined); | ||
} | ||
static fromRlpSerializedAccount(serialized) { | ||
const values = rlp.decode(serialized); | ||
if (!Array.isArray(values)) { | ||
@@ -42,12 +60,12 @@ 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))) { | ||
} | ||
static fromValuesArray(values) { | ||
const [nonce, balance, stateRoot, codeHash] = values; | ||
return new Account(new bn_js_1.default(nonce), new bn_js_1.default(balance), stateRoot, codeHash); | ||
} | ||
_validate() { | ||
if (this.nonce.lt(new bn_js_1.default(0))) { | ||
throw new Error('nonce must be greater than zero'); | ||
} | ||
if (this.balance.lt(new BN(0))) { | ||
if (this.balance.lt(new bn_js_1.default(0))) { | ||
throw new Error('balance must be greater than zero'); | ||
@@ -61,21 +79,21 @@ } | ||
} | ||
}; | ||
} | ||
/** | ||
* Returns a Buffer Array of the raw Buffers for the account, in order. | ||
*/ | ||
Account.prototype.raw = function () { | ||
raw() { | ||
return [types_1.bnToRlp(this.nonce), types_1.bnToRlp(this.balance), this.stateRoot, this.codeHash]; | ||
}; | ||
} | ||
/** | ||
* Returns the RLP serialization of the account as a `Buffer`. | ||
*/ | ||
Account.prototype.serialize = function () { | ||
serialize() { | ||
return rlp.encode(this.raw()); | ||
}; | ||
} | ||
/** | ||
* Returns a `Boolean` determining if the account is a contract. | ||
*/ | ||
Account.prototype.isContract = function () { | ||
isContract() { | ||
return !this.codeHash.equals(constants_1.KECCAK256_NULL); | ||
}; | ||
} | ||
/** | ||
@@ -86,7 +104,6 @@ * Returns a `Boolean` determining if the account is empty complying to the definition of | ||
*/ | ||
Account.prototype.isEmpty = function () { | ||
isEmpty() { | ||
return this.balance.isZero() && this.nonce.isZero() && this.codeHash.equals(constants_1.KECCAK256_NULL); | ||
}; | ||
return Account; | ||
}()); | ||
} | ||
} | ||
exports.Account = Account; | ||
@@ -112,7 +129,11 @@ /** | ||
helpers_1.assertIsHexString(hexAddress); | ||
var address = ethjs_util_1.stripHexPrefix(hexAddress).toLowerCase(); | ||
var prefix = eip1191ChainId !== undefined ? eip1191ChainId.toString() + '0x' : ''; | ||
var hash = hash_1.keccakFromString(prefix + address).toString('hex'); | ||
var ret = '0x'; | ||
for (var i = 0; i < address.length; i++) { | ||
const address = ethjs_util_1.stripHexPrefix(hexAddress).toLowerCase(); | ||
let prefix = ''; | ||
if (eip1191ChainId) { | ||
const chainId = types_1.toType(eip1191ChainId, types_1.TypeOutput.BN); | ||
prefix = chainId.toString() + '0x'; | ||
} | ||
const hash = hash_1.keccakFromString(prefix + address).toString('hex'); | ||
let ret = '0x'; | ||
for (let i = 0; i < address.length; i++) { | ||
if (parseInt(hash[i], 16) >= 8) { | ||
@@ -143,3 +164,3 @@ ret += address[i].toUpperCase(); | ||
helpers_1.assertIsBuffer(nonce); | ||
var nonceBN = new BN(nonce); | ||
const nonceBN = new bn_js_1.default(nonce); | ||
if (nonceBN.isZero()) { | ||
@@ -163,5 +184,5 @@ // in RLP we want to encode null in the case of zero nonce | ||
helpers_1.assertIsBuffer(initCode); | ||
assert(from.length === 20); | ||
assert(salt.length === 32); | ||
var address = hash_1.keccak256(Buffer.concat([Buffer.from('ff', 'hex'), from, salt, hash_1.keccak256(initCode)])); | ||
assert_1.default(from.length === 20); | ||
assert_1.default(salt.length === 32); | ||
const address = hash_1.keccak256(Buffer.concat([Buffer.from('ff', 'hex'), from, salt, hash_1.keccak256(initCode)])); | ||
return address.slice(-20); | ||
@@ -181,4 +202,3 @@ }; | ||
*/ | ||
exports.isValidPublic = function (publicKey, sanitize) { | ||
if (sanitize === void 0) { sanitize = false; } | ||
exports.isValidPublic = function (publicKey, sanitize = false) { | ||
helpers_1.assertIsBuffer(publicKey); | ||
@@ -200,4 +220,3 @@ if (publicKey.length === 64) { | ||
*/ | ||
exports.pubToAddress = function (pubKey, sanitize) { | ||
if (sanitize === void 0) { sanitize = false; } | ||
exports.pubToAddress = function (pubKey, sanitize = false) { | ||
helpers_1.assertIsBuffer(pubKey); | ||
@@ -207,3 +226,3 @@ if (sanitize && pubKey.length !== 64) { | ||
} | ||
assert(pubKey.length === 64); | ||
assert_1.default(pubKey.length === 64); | ||
// Only take the lower 160bits of the hash | ||
@@ -214,9 +233,2 @@ return hash_1.keccak(pubKey).slice(-20); | ||
/** | ||
* Returns the ethereum address of a given private key. | ||
* @param privateKey A private key must be 256 bits wide | ||
*/ | ||
exports.privateToAddress = function (privateKey) { | ||
return exports.publicToAddress(exports.privateToPublic(privateKey)); | ||
}; | ||
/** | ||
* Returns the ethereum public key of a given private key. | ||
@@ -231,2 +243,9 @@ * @param privateKey A private key must be 256 bits wide | ||
/** | ||
* Returns the ethereum address of a given private key. | ||
* @param privateKey A private key must be 256 bits wide | ||
*/ | ||
exports.privateToAddress = function (privateKey) { | ||
return exports.publicToAddress(exports.privateToPublic(privateKey)); | ||
}; | ||
/** | ||
* Converts a public key to the Ethereum format. | ||
@@ -245,4 +264,4 @@ */ | ||
exports.zeroAddress = function () { | ||
var addressLength = 20; | ||
var addr = bytes_1.zeros(addressLength); | ||
const addressLength = 20; | ||
const addr = bytes_1.zeros(addressLength); | ||
return bytes_1.bufferToHex(addr); | ||
@@ -255,5 +274,5 @@ }; | ||
helpers_1.assertIsHexString(hexAddress); | ||
var zeroAddr = exports.zeroAddress(); | ||
const zeroAddr = exports.zeroAddress(); | ||
return zeroAddr === hexAddress; | ||
}; | ||
//# sourceMappingURL=account.js.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Address = void 0; | ||
var assert = require('assert'); | ||
var BN = require("bn.js"); | ||
var bytes_1 = require("./bytes"); | ||
var account_1 = require("./account"); | ||
var Address = /** @class */ (function () { | ||
function Address(buf) { | ||
const assert = require('assert'); | ||
const BN = require("bn.js"); | ||
const bytes_1 = require("./bytes"); | ||
const account_1 = require("./account"); | ||
class Address { | ||
constructor(buf) { | ||
assert(buf.length === 20, 'Invalid address length'); | ||
@@ -16,5 +16,5 @@ this.buf = buf; | ||
*/ | ||
Address.zero = function () { | ||
static zero() { | ||
return new Address(bytes_1.zeros(20)); | ||
}; | ||
} | ||
/** | ||
@@ -24,6 +24,6 @@ * Returns an Address object from a hex-encoded string. | ||
*/ | ||
Address.fromString = function (str) { | ||
static fromString(str) { | ||
assert(account_1.isValidAddress(str), 'Invalid address'); | ||
return new Address(bytes_1.toBuffer(str)); | ||
}; | ||
} | ||
/** | ||
@@ -33,7 +33,7 @@ * Returns an address for a given public key. | ||
*/ | ||
Address.fromPublicKey = function (pubKey) { | ||
static fromPublicKey(pubKey) { | ||
assert(Buffer.isBuffer(pubKey), 'Public key should be Buffer'); | ||
var buf = account_1.pubToAddress(pubKey); | ||
const buf = account_1.pubToAddress(pubKey); | ||
return new Address(buf); | ||
}; | ||
} | ||
/** | ||
@@ -43,7 +43,7 @@ * Returns an address for a given private key. | ||
*/ | ||
Address.fromPrivateKey = function (privateKey) { | ||
static fromPrivateKey(privateKey) { | ||
assert(Buffer.isBuffer(privateKey), 'Private key should be Buffer'); | ||
var buf = account_1.privateToAddress(privateKey); | ||
const buf = account_1.privateToAddress(privateKey); | ||
return new Address(buf); | ||
}; | ||
} | ||
/** | ||
@@ -54,6 +54,6 @@ * Generates an address for a newly created contract. | ||
*/ | ||
Address.generate = function (from, nonce) { | ||
static generate(from, nonce) { | ||
assert(BN.isBN(nonce)); | ||
return new Address(account_1.generateAddress(from.buf, nonce.toArrayLike(Buffer))); | ||
}; | ||
} | ||
/** | ||
@@ -65,34 +65,33 @@ * Generates an address for a contract created using CREATE2. | ||
*/ | ||
Address.generate2 = function (from, salt, initCode) { | ||
static generate2(from, salt, initCode) { | ||
assert(Buffer.isBuffer(salt)); | ||
assert(Buffer.isBuffer(initCode)); | ||
return new Address(account_1.generateAddress2(from.buf, salt, initCode)); | ||
}; | ||
} | ||
/** | ||
* Is address equal to another. | ||
*/ | ||
Address.prototype.equals = function (address) { | ||
equals(address) { | ||
return this.buf.equals(address.buf); | ||
}; | ||
} | ||
/** | ||
* Is address zero. | ||
*/ | ||
Address.prototype.isZero = function () { | ||
isZero() { | ||
return this.equals(Address.zero()); | ||
}; | ||
} | ||
/** | ||
* Returns hex encoding of address. | ||
*/ | ||
Address.prototype.toString = function () { | ||
toString() { | ||
return '0x' + this.buf.toString('hex'); | ||
}; | ||
} | ||
/** | ||
* Returns Buffer representation of address. | ||
*/ | ||
Address.prototype.toBuffer = function () { | ||
toBuffer() { | ||
return Buffer.from(this.buf); | ||
}; | ||
return Address; | ||
}()); | ||
} | ||
} | ||
exports.Address = Address; | ||
//# sourceMappingURL=address.js.map |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import { TransformableToArray, TransformableToBuffer } from './types'; | ||
import BN from 'bn.js'; | ||
import { PrefixedHexString, TransformableToArray, TransformableToBuffer } from './types'; | ||
/** | ||
@@ -43,2 +43,3 @@ * Returns a buffer filled with 0s. | ||
export declare const unpadHexString: (a: string) => string; | ||
export declare type ToBufferInputTypes = PrefixedHexString | number | BN | Buffer | Uint8Array | number[] | TransformableToArray | TransformableToBuffer | null | undefined; | ||
/** | ||
@@ -49,3 +50,3 @@ * Attempts to turn a value into a `Buffer`. | ||
*/ | ||
export declare const toBuffer: (v: string | number | BN | Buffer | Uint8Array | number[] | TransformableToArray | TransformableToBuffer | null | undefined) => Buffer; | ||
export declare const toBuffer: (v: ToBufferInputTypes) => Buffer; | ||
/** | ||
@@ -52,0 +53,0 @@ * Converts a `Buffer` to a `Number`. |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
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 BN = require("bn.js"); | ||
var ethjs_util_1 = require("ethjs-util"); | ||
var helpers_1 = require("./helpers"); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const helpers_1 = require("./helpers"); | ||
/** | ||
@@ -15,2 +18,27 @@ * Returns a buffer filled with 0s. | ||
/** | ||
* Pads a `Buffer` with zeros till it has `length` bytes. | ||
* Truncates the beginning or end of input if its length exceeds `length`. | ||
* @param msg the value to pad (Buffer) | ||
* @param length the number of bytes the output should be | ||
* @param right whether to start padding form the left or right | ||
* @return (Buffer) | ||
*/ | ||
const setLength = function (msg, length, right) { | ||
const buf = exports.zeros(length); | ||
if (right) { | ||
if (msg.length < length) { | ||
msg.copy(buf); | ||
return buf; | ||
} | ||
return msg.slice(0, length); | ||
} | ||
else { | ||
if (msg.length < length) { | ||
msg.copy(buf, length - msg.length); | ||
return buf; | ||
} | ||
return msg.slice(-length); | ||
} | ||
}; | ||
/** | ||
* Left Pads a `Buffer` with leading zeros till it has `length` bytes. | ||
@@ -38,25 +66,13 @@ * Or it truncates the beginning if it exceeds. | ||
/** | ||
* Pads a `Buffer` with zeros till it has `length` bytes. | ||
* Truncates the beginning or end of input if its length exceeds `length`. | ||
* @param msg the value to pad (Buffer) | ||
* @param length the number of bytes the output should be | ||
* @param right whether to start padding form the left or right | ||
* @return (Buffer) | ||
* Trims leading zeros from a `Buffer`, `String` or `Number[]`. | ||
* @param a (Buffer|Array|String) | ||
* @return (Buffer|Array|String) | ||
*/ | ||
var setLength = function (msg, length, right) { | ||
var buf = exports.zeros(length); | ||
if (right) { | ||
if (msg.length < length) { | ||
msg.copy(buf); | ||
return buf; | ||
} | ||
return msg.slice(0, length); | ||
const stripZeros = function (a) { | ||
let first = a[0]; | ||
while (a.length > 0 && first.toString() === '0') { | ||
a = a.slice(1); | ||
first = a[0]; | ||
} | ||
else { | ||
if (msg.length < length) { | ||
msg.copy(buf, length - msg.length); | ||
return buf; | ||
} | ||
return msg.slice(-length); | ||
} | ||
return a; | ||
}; | ||
@@ -92,15 +108,2 @@ /** | ||
/** | ||
* Trims leading zeros from a `Buffer`, `String` or `Number[]`. | ||
* @param a (Buffer|Array|String) | ||
* @return (Buffer|Array|String) | ||
*/ | ||
var stripZeros = function (a) { | ||
var first = a[0]; | ||
while (a.length > 0 && first.toString() === '0') { | ||
a = a.slice(1); | ||
first = a[0]; | ||
} | ||
return a; | ||
}; | ||
/** | ||
* Attempts to turn a value into a `Buffer`. | ||
@@ -122,3 +125,3 @@ * Inputs supported: `Buffer`, `String`, `Number`, null/undefined, `BN` and other objects with a `toArray()` or `toBuffer()` method. | ||
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); | ||
throw new Error(`Cannot convert string to buffer. toBuffer only supports 0x-prefixed hex strings and this string was given: ${v}`); | ||
} | ||
@@ -130,3 +133,3 @@ return Buffer.from(ethjs_util_1.padToEven(ethjs_util_1.stripHexPrefix(v)), 'hex'); | ||
} | ||
if (BN.isBN(v)) { | ||
if (bn_js_1.default.isBN(v)) { | ||
return v.toArrayLike(Buffer); | ||
@@ -149,3 +152,3 @@ } | ||
exports.bufferToInt = function (buf) { | ||
return new BN(exports.toBuffer(buf)).toNumber(); | ||
return new bn_js_1.default(exports.toBuffer(buf)).toNumber(); | ||
}; | ||
@@ -165,3 +168,3 @@ /** | ||
exports.fromSigned = function (num) { | ||
return new BN(num).fromTwos(256); | ||
return new bn_js_1.default(num).fromTwos(256); | ||
}; | ||
@@ -191,7 +194,7 @@ /** | ||
if (Buffer.isBuffer(ba)) { | ||
return "0x" + ba.toString('hex'); | ||
return `0x${ba.toString('hex')}`; | ||
} | ||
else if (ba instanceof Array) { | ||
var array = []; | ||
for (var i = 0; i < ba.length; i++) { | ||
const array = []; | ||
for (let i = 0; i < ba.length; i++) { | ||
array.push(exports.baToJSON(ba[i])); | ||
@@ -198,0 +201,0 @@ } |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import BN from 'bn.js'; | ||
/** | ||
@@ -4,0 +4,0 @@ * The max integer that this VM can handle |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.KECCAK256_RLP = exports.KECCAK256_RLP_S = exports.KECCAK256_RLP_ARRAY = exports.KECCAK256_RLP_ARRAY_S = exports.KECCAK256_NULL = exports.KECCAK256_NULL_S = exports.TWO_POW256 = exports.MAX_INTEGER = void 0; | ||
var Buffer = require('buffer').Buffer; | ||
var BN = require("bn.js"); | ||
const Buffer = require('buffer').Buffer; | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
/** | ||
* The max integer that this VM can handle | ||
*/ | ||
exports.MAX_INTEGER = new BN('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16); | ||
exports.MAX_INTEGER = new bn_js_1.default('ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff', 16); | ||
/** | ||
* 2^256 | ||
*/ | ||
exports.TWO_POW256 = new BN('10000000000000000000000000000000000000000000000000000000000000000', 16); | ||
exports.TWO_POW256 = new bn_js_1.default('10000000000000000000000000000000000000000000000000000000000000000', 16); | ||
/** | ||
@@ -15,0 +18,0 @@ * Keccak-256 hash of null |
@@ -12,6 +12,6 @@ "use strict"; | ||
// is released and adopted here. | ||
var BN = require("bn.js"); | ||
const BN = require("bn.js"); | ||
exports.BN = BN; | ||
var rlp = require("rlp"); | ||
const rlp = require("rlp"); | ||
exports.rlp = rlp; | ||
//# sourceMappingURL=externals.js.map |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.rlphash = exports.ripemd160FromArray = exports.ripemd160FromString = exports.ripemd160 = exports.sha256FromArray = exports.sha256FromString = exports.sha256 = exports.keccakFromArray = exports.keccakFromHexString = exports.keccakFromString = exports.keccak256 = exports.keccak = void 0; | ||
var _a = require('ethereum-cryptography/keccak'), keccak224 = _a.keccak224, keccak384 = _a.keccak384, k256 = _a.keccak256, keccak512 = _a.keccak512; | ||
var createHash = require('create-hash'); | ||
var rlp = require("rlp"); | ||
var bytes_1 = require("./bytes"); | ||
var helpers_1 = require("./helpers"); | ||
const { keccak224, keccak384, keccak256: k256, keccak512 } = require('ethereum-cryptography/keccak'); | ||
const createHash = require('create-hash'); | ||
const rlp = __importStar(require("rlp")); | ||
const bytes_1 = require("./bytes"); | ||
const helpers_1 = require("./helpers"); | ||
/** | ||
@@ -14,4 +33,3 @@ * Creates Keccak hash of a Buffer input | ||
*/ | ||
exports.keccak = function (a, bits) { | ||
if (bits === void 0) { bits = 256; } | ||
exports.keccak = function (a, bits = 256) { | ||
helpers_1.assertIsBuffer(a); | ||
@@ -32,3 +50,3 @@ switch (bits) { | ||
default: { | ||
throw new Error("Invald algorithm: keccak" + bits); | ||
throw new Error(`Invald algorithm: keccak${bits}`); | ||
} | ||
@@ -49,6 +67,5 @@ } | ||
*/ | ||
exports.keccakFromString = function (a, bits) { | ||
if (bits === void 0) { bits = 256; } | ||
exports.keccakFromString = function (a, bits = 256) { | ||
helpers_1.assertIsString(a); | ||
var buf = Buffer.from(a, 'utf8'); | ||
const buf = Buffer.from(a, 'utf8'); | ||
return exports.keccak(buf, bits); | ||
@@ -61,4 +78,3 @@ }; | ||
*/ | ||
exports.keccakFromHexString = function (a, bits) { | ||
if (bits === void 0) { bits = 256; } | ||
exports.keccakFromHexString = function (a, bits = 256) { | ||
helpers_1.assertIsHexString(a); | ||
@@ -72,4 +88,3 @@ return exports.keccak(bytes_1.toBuffer(a), bits); | ||
*/ | ||
exports.keccakFromArray = function (a, bits) { | ||
if (bits === void 0) { bits = 256; } | ||
exports.keccakFromArray = function (a, bits = 256) { | ||
helpers_1.assertIsArray(a); | ||
@@ -79,2 +94,12 @@ return exports.keccak(bytes_1.toBuffer(a), bits); | ||
/** | ||
* Creates SHA256 hash of an input. | ||
* @param a The input data (Buffer|Array|String) | ||
*/ | ||
const _sha256 = function (a) { | ||
a = bytes_1.toBuffer(a); | ||
return createHash('sha256') | ||
.update(a) | ||
.digest(); | ||
}; | ||
/** | ||
* Creates SHA256 hash of a Buffer input. | ||
@@ -104,10 +129,17 @@ * @param a The input data (Buffer) | ||
/** | ||
* Creates SHA256 hash of an input. | ||
* @param a The input data (Buffer|Array|String) | ||
* Creates RIPEMD160 hash of the input. | ||
* @param a The input data (Buffer|Array|String|Number) | ||
* @param padded Whether it should be padded to 256 bits or not | ||
*/ | ||
var _sha256 = function (a) { | ||
const _ripemd160 = function (a, padded) { | ||
a = bytes_1.toBuffer(a); | ||
return createHash('sha256') | ||
const hash = createHash('rmd160') | ||
.update(a) | ||
.digest(); | ||
if (padded === true) { | ||
return bytes_1.setLengthLeft(hash, 32); | ||
} | ||
else { | ||
return hash; | ||
} | ||
}; | ||
@@ -142,19 +174,2 @@ /** | ||
/** | ||
* Creates RIPEMD160 hash of the input. | ||
* @param a The input data (Buffer|Array|String|Number) | ||
* @param padded Whether it should be padded to 256 bits or not | ||
*/ | ||
var _ripemd160 = function (a, padded) { | ||
a = bytes_1.toBuffer(a); | ||
var hash = createHash('rmd160') | ||
.update(a) | ||
.digest(); | ||
if (padded === true) { | ||
return bytes_1.setLengthLeft(hash, 32); | ||
} | ||
else { | ||
return hash; | ||
} | ||
}; | ||
/** | ||
* Creates SHA-3 hash of the RLP encoded version of the input. | ||
@@ -161,0 +176,0 @@ * @param a The input data |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.assertIsString = exports.assertIsArray = exports.assertIsBuffer = exports.assertIsHexString = void 0; | ||
var ethjs_util_1 = require("ethjs-util"); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
/** | ||
@@ -11,3 +11,3 @@ * Throws if a string is not hex prefixed | ||
if (!ethjs_util_1.isHexString(input)) { | ||
var msg = "This method only supports 0x-prefixed hex strings but input was: " + input; | ||
const msg = `This method only supports 0x-prefixed hex strings but input was: ${input}`; | ||
throw new Error(msg); | ||
@@ -22,3 +22,3 @@ } | ||
if (!Buffer.isBuffer(input)) { | ||
var msg = "This method only supports Buffer but input was: " + input; | ||
const msg = `This method only supports Buffer but input was: ${input}`; | ||
throw new Error(msg); | ||
@@ -33,3 +33,3 @@ } | ||
if (!Array.isArray(input)) { | ||
var msg = "This method only supports number arrays but input was: " + input; | ||
const msg = `This method only supports number arrays but input was: ${input}`; | ||
throw new Error(msg); | ||
@@ -44,3 +44,3 @@ } | ||
if (typeof input !== 'string') { | ||
var msg = "This method only supports strings but input was: " + input; | ||
const msg = `This method only supports strings but input was: ${input}`; | ||
throw new Error(msg); | ||
@@ -47,0 +47,0 @@ } |
"use strict"; | ||
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } }); | ||
}) : (function(o, m, k, k2) { | ||
if (k2 === undefined) k2 = k; | ||
o[k2] = m[k]; | ||
})); | ||
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { | ||
Object.defineProperty(o, "default", { enumerable: true, value: v }); | ||
}) : function(o, v) { | ||
o["default"] = v; | ||
}); | ||
var __importStar = (this && this.__importStar) || function (mod) { | ||
if (mod && mod.__esModule) return mod; | ||
var result = {}; | ||
if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); | ||
__setModuleDefault(result, mod); | ||
return result; | ||
}; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.defineProperties = void 0; | ||
var ethjsUtil = require("ethjs-util"); | ||
var assert = require("assert"); | ||
var rlp = require("rlp"); | ||
var bytes_1 = require("./bytes"); | ||
const assert_1 = __importDefault(require("assert")); | ||
const ethjsUtil = __importStar(require("ethjs-util")); | ||
const rlp = __importStar(require("rlp")); | ||
const bytes_1 = require("./bytes"); | ||
/** | ||
@@ -23,10 +45,9 @@ * Defines properties on a `Object`. It make the assumption that underlying data is binary. | ||
// attach the `toJSON` | ||
self.toJSON = function (label) { | ||
if (label === void 0) { label = false; } | ||
self.toJSON = function (label = false) { | ||
if (label) { | ||
var obj_1 = {}; | ||
self._fields.forEach(function (field) { | ||
obj_1[field] = "0x" + self[field].toString('hex'); | ||
const obj = {}; | ||
self._fields.forEach((field) => { | ||
obj[field] = `0x${self[field].toString('hex')}`; | ||
}); | ||
return obj_1; | ||
return obj; | ||
} | ||
@@ -38,3 +59,3 @@ return bytes_1.baToJSON(self.raw); | ||
}; | ||
fields.forEach(function (field, i) { | ||
fields.forEach((field, i) => { | ||
self._fields.push(field.name); | ||
@@ -51,6 +72,6 @@ function getter() { | ||
v = bytes_1.unpadBuffer(v); | ||
assert(field.length >= v.length, "The field " + field.name + " must not have more " + field.length + " bytes"); | ||
assert_1.default(field.length >= v.length, `The field ${field.name} must not have more ${field.length} bytes`); | ||
} | ||
else if (!(field.allowZero && v.length === 0) && field.length) { | ||
assert(field.length === v.length, "The field " + field.name + " must have byte length of " + field.length); | ||
assert_1.default(field.length === v.length, `The field ${field.name} must have byte length of ${field.length}`); | ||
} | ||
@@ -63,3 +84,3 @@ self.raw[i] = v; | ||
get: getter, | ||
set: setter, | ||
set: setter | ||
}); | ||
@@ -75,3 +96,3 @@ if (field.default) { | ||
set: setter, | ||
get: getter, | ||
get: getter | ||
}); | ||
@@ -93,3 +114,3 @@ } | ||
// make sure all the items are buffers | ||
data.forEach(function (d, i) { | ||
data.forEach((d, i) => { | ||
self[self._fields[i]] = bytes_1.toBuffer(d); | ||
@@ -99,7 +120,7 @@ }); | ||
else if (typeof data === 'object') { | ||
var keys_1 = Object.keys(data); | ||
fields.forEach(function (field) { | ||
if (keys_1.indexOf(field.name) !== -1) | ||
const keys = Object.keys(data); | ||
fields.forEach((field) => { | ||
if (keys.indexOf(field.name) !== -1) | ||
self[field.name] = data[field.name]; | ||
if (keys_1.indexOf(field.alias) !== -1) | ||
if (keys.indexOf(field.alias) !== -1) | ||
self[field.alias] = data[field.alias]; | ||
@@ -106,0 +127,0 @@ }); |
/// <reference types="node" /> | ||
import BN from 'bn.js'; | ||
import { BNLike } from './types'; | ||
export interface ECDSASignature { | ||
@@ -7,6 +9,12 @@ v: number; | ||
} | ||
export interface ECDSASignatureBuffer { | ||
v: Buffer; | ||
r: Buffer; | ||
s: Buffer; | ||
} | ||
/** | ||
* Returns the ECDSA signature of a message hash. | ||
*/ | ||
export declare const ecsign: (msgHash: Buffer, privateKey: Buffer, chainId?: number | undefined) => ECDSASignature; | ||
export declare function ecsign(msgHash: Buffer, privateKey: Buffer, chainId?: number): ECDSASignature; | ||
export declare function ecsign(msgHash: Buffer, privateKey: Buffer, chainId: BNLike): ECDSASignatureBuffer; | ||
/** | ||
@@ -16,3 +24,3 @@ * ECDSA public key recovery from signature. | ||
*/ | ||
export declare const ecrecover: (msgHash: Buffer, v: number, r: Buffer, s: Buffer, chainId?: number | undefined) => Buffer; | ||
export declare const ecrecover: (msgHash: Buffer, v: BNLike, r: Buffer, s: Buffer, chainId?: string | number | Buffer | BN | undefined) => Buffer; | ||
/** | ||
@@ -22,3 +30,3 @@ * Convert signature parameters into the format of `eth_sign` RPC method. | ||
*/ | ||
export declare const toRpcSig: (v: number, r: Buffer, s: Buffer, chainId?: number | undefined) => string; | ||
export declare const toRpcSig: (v: BNLike, r: Buffer, s: Buffer, chainId?: string | number | Buffer | BN | undefined) => string; | ||
/** | ||
@@ -33,3 +41,3 @@ * Convert signature format of the `eth_sign` RPC method to signature parameters | ||
*/ | ||
export declare const isValidSignature: (v: number, r: Buffer, s: Buffer, homesteadOrLater?: boolean, chainId?: number | undefined) => boolean; | ||
export declare const isValidSignature: (v: BNLike, r: Buffer, s: Buffer, homesteadOrLater?: boolean, chainId?: string | number | Buffer | BN | undefined) => boolean; | ||
/** | ||
@@ -36,0 +44,0 @@ * Returns the keccak-256 hash of `message`, prefixed with the header used by the `eth_sign` RPC call. |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.hashPersonalMessage = exports.isValidSignature = exports.fromRpcSig = exports.toRpcSig = exports.ecrecover = exports.ecsign = void 0; | ||
var _a = require('ethereum-cryptography/secp256k1'), ecdsaSign = _a.ecdsaSign, ecdsaRecover = _a.ecdsaRecover, publicKeyConvert = _a.publicKeyConvert; | ||
var BN = require("bn.js"); | ||
var bytes_1 = require("./bytes"); | ||
var hash_1 = require("./hash"); | ||
var helpers_1 = require("./helpers"); | ||
const secp256k1_1 = require("ethereum-cryptography/secp256k1"); | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const bytes_1 = require("./bytes"); | ||
const hash_1 = require("./hash"); | ||
const helpers_1 = require("./helpers"); | ||
const types_1 = require("./types"); | ||
function ecsign(msgHash, privateKey, chainId) { | ||
const { signature, recid: recovery } = secp256k1_1.ecdsaSign(msgHash, privateKey); | ||
const r = Buffer.from(signature.slice(0, 32)); | ||
const s = Buffer.from(signature.slice(32, 64)); | ||
if (!chainId || typeof chainId === 'number') { | ||
// return legacy type ECDSASignature (deprecated in favor of ECDSASignatureBuffer to handle large chainIds) | ||
if (chainId && !Number.isSafeInteger(chainId)) { | ||
throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'); | ||
} | ||
const v = chainId ? recovery + (chainId * 2 + 35) : recovery + 27; | ||
return { r, s, v }; | ||
} | ||
const chainIdBN = types_1.toType(chainId, types_1.TypeOutput.BN); | ||
const v = chainIdBN | ||
.muln(2) | ||
.addn(35) | ||
.addn(recovery) | ||
.toArrayLike(Buffer); | ||
return { r, s, v }; | ||
} | ||
exports.ecsign = ecsign; | ||
function calculateSigRecovery(v, chainId) { | ||
const vBN = types_1.toType(v, types_1.TypeOutput.BN); | ||
if (!chainId) { | ||
return vBN.subn(27); | ||
} | ||
const chainIdBN = types_1.toType(chainId, types_1.TypeOutput.BN); | ||
return vBN.sub(chainIdBN.muln(2).addn(35)); | ||
} | ||
function isValidSigRecovery(recovery) { | ||
const rec = new bn_js_1.default(recovery); | ||
return rec.eqn(0) || rec.eqn(1); | ||
} | ||
/** | ||
* Returns the ECDSA signature of a message hash. | ||
*/ | ||
exports.ecsign = function (msgHash, privateKey, chainId) { | ||
var sig = ecdsaSign(msgHash, privateKey); | ||
var recovery = sig.recid; | ||
var ret = { | ||
r: Buffer.from(sig.signature.slice(0, 32)), | ||
s: Buffer.from(sig.signature.slice(32, 64)), | ||
v: chainId ? recovery + (chainId * 2 + 35) : recovery + 27, | ||
}; | ||
return ret; | ||
}; | ||
/** | ||
* ECDSA public key recovery from signature. | ||
@@ -27,9 +51,9 @@ * @returns Recovered public key | ||
exports.ecrecover = function (msgHash, v, r, s, chainId) { | ||
var signature = Buffer.concat([bytes_1.setLengthLeft(r, 32), bytes_1.setLengthLeft(s, 32)], 64); | ||
var recovery = calculateSigRecovery(v, chainId); | ||
const signature = Buffer.concat([bytes_1.setLengthLeft(r, 32), bytes_1.setLengthLeft(s, 32)], 64); | ||
const recovery = calculateSigRecovery(v, chainId); | ||
if (!isValidSigRecovery(recovery)) { | ||
throw new Error('Invalid signature v value'); | ||
} | ||
var senderPubKey = ecdsaRecover(signature, recovery, msgHash); | ||
return Buffer.from(publicKeyConvert(senderPubKey, false).slice(1)); | ||
const senderPubKey = secp256k1_1.ecdsaRecover(signature, recovery.toNumber(), msgHash); | ||
return Buffer.from(secp256k1_1.publicKeyConvert(senderPubKey, false).slice(1)); | ||
}; | ||
@@ -41,3 +65,3 @@ /** | ||
exports.toRpcSig = function (v, r, s, chainId) { | ||
var recovery = calculateSigRecovery(v, chainId); | ||
const recovery = calculateSigRecovery(v, chainId); | ||
if (!isValidSigRecovery(recovery)) { | ||
@@ -54,7 +78,7 @@ throw new Error('Invalid signature v value'); | ||
exports.fromRpcSig = function (sig) { | ||
var buf = bytes_1.toBuffer(sig); | ||
const buf = bytes_1.toBuffer(sig); | ||
if (buf.length < 65) { | ||
throw new Error('Invalid signature length'); | ||
} | ||
var v = bytes_1.bufferToInt(buf.slice(64)); | ||
let v = bytes_1.bufferToInt(buf.slice(64)); | ||
// support both versions of `eth_sign` responses | ||
@@ -67,3 +91,3 @@ if (v < 27) { | ||
r: buf.slice(0, 32), | ||
s: buf.slice(32, 64), | ||
s: buf.slice(32, 64) | ||
}; | ||
@@ -75,6 +99,5 @@ }; | ||
*/ | ||
exports.isValidSignature = function (v, r, s, homesteadOrLater, chainId) { | ||
if (homesteadOrLater === void 0) { homesteadOrLater = true; } | ||
var SECP256K1_N_DIV_2 = new BN('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16); | ||
var SECP256K1_N = new BN('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16); | ||
exports.isValidSignature = function (v, r, s, homesteadOrLater = true, chainId) { | ||
const SECP256K1_N_DIV_2 = new bn_js_1.default('7fffffffffffffffffffffffffffffff5d576e7357a4501ddfe92f46681b20a0', 16); | ||
const SECP256K1_N = new bn_js_1.default('fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141', 16); | ||
if (r.length !== 32 || s.length !== 32) { | ||
@@ -86,4 +109,4 @@ return false; | ||
} | ||
var rBN = new BN(r); | ||
var sBN = new BN(s); | ||
const rBN = new bn_js_1.default(r); | ||
const sBN = new bn_js_1.default(s); | ||
if (rBN.isZero() || rBN.gt(SECP256K1_N) || sBN.isZero() || sBN.gt(SECP256K1_N)) { | ||
@@ -105,11 +128,5 @@ return false; | ||
helpers_1.assertIsBuffer(message); | ||
var prefix = Buffer.from("\u0019Ethereum Signed Message:\n" + message.length.toString(), 'utf-8'); | ||
const prefix = Buffer.from(`\u0019Ethereum Signed Message:\n${message.length.toString()}`, 'utf-8'); | ||
return hash_1.keccak(Buffer.concat([prefix, message])); | ||
}; | ||
function calculateSigRecovery(v, chainId) { | ||
return chainId ? v - (2 * chainId + 35) : v - 27; | ||
} | ||
function isValidSigRecovery(recovery) { | ||
return recovery === 0 || recovery === 1; | ||
} | ||
//# sourceMappingURL=signature.js.map |
/// <reference types="node" /> | ||
import * as BN from 'bn.js'; | ||
import BN from 'bn.js'; | ||
import { Address } from './address'; | ||
export declare type BNLike = BN | string | number; | ||
import { ToBufferInputTypes } from './bytes'; | ||
export declare type BNLike = BN | PrefixedHexString | number | Buffer; | ||
export declare type BufferLike = Buffer | Uint8Array | number[] | number | BN | TransformableToBuffer | PrefixedHexString; | ||
@@ -11,3 +12,3 @@ export declare type PrefixedHexString = string; | ||
*/ | ||
export declare type AddressLike = Address | Buffer | string; | ||
export declare type AddressLike = Address | Buffer | PrefixedHexString; | ||
export interface TransformableToArray { | ||
@@ -30,1 +31,22 @@ toArray(): Uint8Array; | ||
export declare function bnToRlp(value: BN): Buffer; | ||
/** | ||
* Type output options | ||
*/ | ||
export declare enum TypeOutput { | ||
Number = 0, | ||
BN = 1, | ||
Buffer = 2, | ||
PrefixedHexString = 3 | ||
} | ||
export declare type TypeOutputReturnType = { | ||
[TypeOutput.Number]: number; | ||
[TypeOutput.BN]: BN; | ||
[TypeOutput.Buffer]: Buffer; | ||
[TypeOutput.PrefixedHexString]: PrefixedHexString; | ||
}; | ||
/** | ||
* Convert an input to a specified type | ||
* @param input value to convert | ||
* @param outputType type to output | ||
*/ | ||
export declare function toType<T extends TypeOutput>(input: ToBufferInputTypes, outputType: T): TypeOutputReturnType[T]; |
"use strict"; | ||
var __importDefault = (this && this.__importDefault) || function (mod) { | ||
return (mod && mod.__esModule) ? mod : { "default": mod }; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.bnToRlp = exports.bnToHex = void 0; | ||
var bytes_1 = require("./bytes"); | ||
exports.toType = exports.TypeOutput = exports.bnToRlp = exports.bnToHex = void 0; | ||
const bn_js_1 = __importDefault(require("bn.js")); | ||
const ethjs_util_1 = require("ethjs-util"); | ||
const bytes_1 = require("./bytes"); | ||
/** | ||
@@ -9,3 +14,3 @@ * Convert BN to 0x-prefixed hex string. | ||
function bnToHex(value) { | ||
return "0x" + value.toString(16); | ||
return `0x${value.toString(16)}`; | ||
} | ||
@@ -23,2 +28,45 @@ exports.bnToHex = bnToHex; | ||
exports.bnToRlp = bnToRlp; | ||
/** | ||
* Type output options | ||
*/ | ||
var TypeOutput; | ||
(function (TypeOutput) { | ||
TypeOutput[TypeOutput["Number"] = 0] = "Number"; | ||
TypeOutput[TypeOutput["BN"] = 1] = "BN"; | ||
TypeOutput[TypeOutput["Buffer"] = 2] = "Buffer"; | ||
TypeOutput[TypeOutput["PrefixedHexString"] = 3] = "PrefixedHexString"; | ||
})(TypeOutput = exports.TypeOutput || (exports.TypeOutput = {})); | ||
/** | ||
* Convert an input to a specified type | ||
* @param input value to convert | ||
* @param outputType type to output | ||
*/ | ||
function toType(input, outputType) { | ||
if (typeof input === 'string' && !ethjs_util_1.isHexString(input)) { | ||
throw new Error(`A string must be provided with a 0x-prefix, given: ${input}`); | ||
} | ||
else if (typeof input === 'number' && !Number.isSafeInteger(input)) { | ||
throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative input type)'); | ||
} | ||
input = bytes_1.toBuffer(input); | ||
if (outputType === TypeOutput.Buffer) { | ||
return input; | ||
} | ||
else if (outputType === TypeOutput.BN) { | ||
return new bn_js_1.default(input); | ||
} | ||
else if (outputType === TypeOutput.Number) { | ||
const bn = new bn_js_1.default(input); | ||
const max = new bn_js_1.default(Number.MAX_SAFE_INTEGER.toString()); | ||
if (bn.gt(max)) { | ||
throw new Error('The provided number is greater than MAX_SAFE_INTEGER (please use an alternative output type)'); | ||
} | ||
return bn.toNumber(); | ||
} | ||
else { | ||
// outputType === TypeOutput.PrefixedHexString | ||
return `0x${input.toString('hex')}`; | ||
} | ||
} | ||
exports.toType = toType; | ||
//# sourceMappingURL=types.js.map |
{ | ||
"name": "ethereumjs-util", | ||
"version": "7.0.8", | ||
"version": "7.0.9", | ||
"description": "a collection of utility functions for Ethereum", | ||
@@ -14,15 +14,11 @@ "main": "dist/index.js", | ||
"scripts": { | ||
"build": "ethereumjs-config-build", | ||
"build": "ethereumjs-config-ts-build", | ||
"prepublishOnly": "npm run test && npm run build", | ||
"docs:build": "npx typedoc --options typedoc.js", | ||
"format": "ethereumjs-config-format", | ||
"format:fix": "ethereumjs-config-format-fix", | ||
"lint": "ethereumjs-config-lint", | ||
"lint:fix": "ethereumjs-config-lint-fix", | ||
"test": "npm run lint && npm run test:node && npm run test:browser", | ||
"test": "npm run test:node && npm run test:browser", | ||
"test:browser": "karma start karma.conf.js", | ||
"test:node": "nyc --reporter=lcov mocha --require ts-node/register 'test/*.spec.ts'", | ||
"tsc": "ethereumjs-config-tsc", | ||
"tslint": "ethereumjs-config-tslint", | ||
"tslint:fix": "ethereumjs-config-tslint-fix" | ||
"tsc": "ethereumjs-config-tsc" | ||
}, | ||
@@ -93,3 +89,3 @@ "husky": { | ||
"dependencies": { | ||
"@types/bn.js": "^4.11.3", | ||
"@types/bn.js": "^5.1.0", | ||
"bn.js": "^5.1.2", | ||
@@ -102,6 +98,7 @@ "create-hash": "^1.1.2", | ||
"devDependencies": { | ||
"@ethereumjs/config-prettier": "^1.1.0", | ||
"@ethereumjs/config-tsc": "^1.1.0", | ||
"@ethereumjs/config-tslint": "^1.1.0", | ||
"@types/mocha": "^5.2.7", | ||
"@ethereumjs/config-coverage": "^2.0.0", | ||
"@ethereumjs/config-typescript": "^2.0.0", | ||
"@ethereumjs/eslint-config-defaults": "^2.0.0", | ||
"@types/assert": "^1.5.4", | ||
"@types/mocha": "^8.2.0", | ||
"@types/node": "^11.9.0", | ||
@@ -115,12 +112,10 @@ "@types/secp256k1": "^4.0.1", | ||
"karma-typescript": "^4.1.1", | ||
"mocha": "^6.0.0", | ||
"mocha": "^8.2.1", | ||
"nyc": "^15.0.0", | ||
"prettier": "^1.15.3", | ||
"ts-node": "^8.6.2", | ||
"tslint": "^5.12.0", | ||
"typedoc": "next", | ||
"typedoc-plugin-markdown": "^2.2.16", | ||
"typescript": "^3.8.3", | ||
"typestrict": "^1.0.2" | ||
"typescript": "^3.8.3" | ||
} | ||
} |
# SYNOPSIS | ||
[![NPM Package](https://img.shields.io/npm/v/ethereumjs-util.svg)](https://www.npmjs.org/package/ethereumjs-util) | ||
[![Actions Status](https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg)](https://github.com/ethereumjs/ethereumjs-util/actions) | ||
[![Coverage Status](https://img.shields.io/coveralls/ethereumjs/ethereumjs-util.svg)](https://coveralls.io/r/ethereumjs/ethereumjs-util) | ||
[![NPM Status][npm-badge]][npm-link] | ||
[![Actions Status][actions-badge]][actions-link] | ||
[![Coverage Status][coverage-badge]][coverage-link] | ||
[![Discord][discord-badge]][discord-link] | ||
@@ -74,3 +74,3 @@ | ||
Import can be done directly by function name analogous to the build-in function import: | ||
They can be imported by name: | ||
@@ -98,3 +98,9 @@ ```js | ||
[npm-badge]: https://img.shields.io/npm/v/ethereumjs-util.svg | ||
[npm-link]: https://www.npmjs.org/package/ethereumjs-util | ||
[actions-badge]: https://github.com/ethereumjs/ethereumjs-util/workflows/Build/badge.svg | ||
[actions-link]: https://github.com/ethereumjs/ethereumjs-util/actions | ||
[coverage-badge]: https://codecov.io/gh/ethereumjs/ethereumjs-util/branch/master/graph/badge.svg | ||
[coverage-link]: https://codecov.io/gh/ethereumjs/ethereumjs-util | ||
[discord-badge]: https://img.shields.io/static/v1?logo=discord&label=discord&message=Join&color=blue | ||
[discord-link]: https://discord.gg/TNwARpR |
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
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
134754
20
1791
105
+ Added@types/bn.js@5.1.6(transitive)
- Removed@types/bn.js@4.11.6(transitive)
Updated@types/bn.js@^5.1.0