micro-eth-signer
Advanced tools
Comparing version 0.2.1 to 0.2.2
@@ -22,5 +22,7 @@ /*! micro-eth-signer - MIT License (c) Paul Miller (paulmillr.com) */ | ||
export declare type Field = typeof FIELDS[number] | typeof FIELDS2930[number] | typeof FIELDS1559[number] | 'address' | 'storageKey'; | ||
export declare type RawTxLegacy = [string, string, string, string, string, string, string, string, string]; | ||
export declare type RawTx2930 = [string, string, string, string, string, string, [string, string[]][], string, string, string]; | ||
export declare type RawTx1559 = [string, string, string, string, string, string, string, [string, string[]][], string, string, string]; | ||
declare type str = string; | ||
export declare type AccessList = [str, str[]][]; | ||
export declare type RawTxLegacy = [str, str, str, str, str, str, str, str, str]; | ||
export declare type RawTx2930 = [str, str, str, str, str, str, AccessList, str, str, str]; | ||
export declare type RawTx1559 = [str, str, str, str, str, str, str, AccessList, str, str, str]; | ||
export declare type RawTx = RawTxLegacy | RawTx2930 | RawTx1559; | ||
@@ -37,3 +39,3 @@ export declare type RawTxMap = { | ||
data: string; | ||
accessList?: [string, string[]][]; | ||
accessList?: AccessList; | ||
yParity?: string; | ||
@@ -64,2 +66,6 @@ v?: string; | ||
get sender(): string; | ||
get gasPrice(): bigint; | ||
get maxFeePerGas(): bigint; | ||
get maxPriorityFeePerGas(): bigint; | ||
get gasLimit(): bigint; | ||
get amount(): bigint; | ||
@@ -66,0 +72,0 @@ get fee(): bigint; |
51
index.js
@@ -51,6 +51,4 @@ "use strict"; | ||
} | ||
function numberToHex(num, padToBytes = 0) { | ||
const hex = num.toString(16); | ||
const p1 = hex.length & 1 ? `0${hex}` : hex; | ||
return p1.padStart(padToBytes * 2, '0'); | ||
function numberToHex(num) { | ||
return padHex(num.toString(16)); | ||
} | ||
@@ -64,6 +62,10 @@ function hexToNumber(hex) { | ||
const FIELDS = ['nonce', 'gasPrice', 'gasLimit', 'to', 'value', 'data', 'v', 'r', 's']; | ||
const FIELDS2930 = ['chainId', 'nonce', 'gasPrice', 'gasLimit', 'to', 'value', 'data', 'accessList', | ||
'yParity', 'r', 's']; | ||
const FIELDS1559 = ['chainId', 'nonce', 'maxPriorityFeePerGas', 'maxFeePerGas', 'gasLimit', 'to', 'value', | ||
'data', 'accessList', 'yParity', 'r', 's']; | ||
const FIELDS2930 = [ | ||
'chainId', 'nonce', 'gasPrice', 'gasLimit', | ||
'to', 'value', 'data', 'accessList', 'yParity', 'r', 's' | ||
]; | ||
const FIELDS1559 = [ | ||
'chainId', 'nonce', 'maxPriorityFeePerGas', 'maxFeePerGas', 'gasLimit', | ||
'to', 'value', 'data', 'accessList', 'yParity', 'r', 's' | ||
]; | ||
const TypeToFields = { | ||
@@ -74,4 +76,6 @@ legacy: FIELDS, | ||
}; | ||
const FIELD_NUMBER = new Set(['chainId', 'nonce', 'gasPrice', 'maxPriorityFeePerGas', 'maxFeePerGas', | ||
'gasLimit', 'value', 'v', 'yParity', 'r', 's']); | ||
const FIELD_NUMBER = new Set([ | ||
'chainId', 'nonce', 'gasPrice', 'maxPriorityFeePerGas', 'maxFeePerGas', | ||
'gasLimit', 'value', 'v', 'yParity', 'r', 's' | ||
]); | ||
const FIELD_DATA = new Set(['data', 'to', 'storageKey', 'address']); | ||
@@ -123,3 +127,3 @@ function normalizeField(field, value) { | ||
if (typeof access !== 'object' || | ||
access === null || | ||
access == null || | ||
!access.address || | ||
@@ -137,3 +141,3 @@ !Array.isArray(access.storageKeys)) | ||
else { | ||
if (typeof value !== 'object' || value === null || value instanceof Uint8Array) | ||
if (typeof value !== 'object' || value == null || value instanceof Uint8Array) | ||
throw new TypeError(`Invalid type for field ${field}`); | ||
@@ -336,2 +340,20 @@ for (let k in value) { | ||
} | ||
get gasPrice() { | ||
if (this.type === 'eip1559') | ||
throw new Error('Field only available for "legacy" transactions'); | ||
return BigInt(this.raw.gasPrice); | ||
} | ||
get maxFeePerGas() { | ||
if (this.type !== 'eip1559') | ||
throw new Error('Field only available for "eip1559" transactions'); | ||
return BigInt(this.raw.maxFeePerGas); | ||
} | ||
get maxPriorityFeePerGas() { | ||
if (this.type !== 'eip1559') | ||
throw new Error('Field only available for "eip1559" transactions'); | ||
return BigInt(this.raw.maxPriorityFeePerGas); | ||
} | ||
get gasLimit() { | ||
return BigInt(this.raw.gasLimit); | ||
} | ||
get amount() { | ||
@@ -341,5 +363,4 @@ return BigInt(this.raw.value); | ||
get fee() { | ||
if (this.type === 'eip1559') | ||
return BigInt(this.raw.maxFeePerGas) * BigInt(this.raw.gasLimit); | ||
return BigInt(this.raw.gasPrice) * BigInt(this.raw.gasLimit); | ||
const price = this.type === 'eip1559' ? this.maxFeePerGas : this.gasPrice; | ||
return price * this.gasLimit; | ||
} | ||
@@ -346,0 +367,0 @@ get upfrontCost() { |
{ | ||
"name": "micro-eth-signer", | ||
"version": "0.2.1", | ||
"version": "0.2.2", | ||
"description": "Create, sign and validate Ethereum transactions & addresses with minimum deps. Supports London & Berlin txs", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -111,3 +111,3 @@ # micro-eth-signer | ||
- `new Transaction(serialized[, chain, hardfork])` - creates transaction from Raw TX string. | ||
- `new Transaction(serialized[, chain, hardfork, type])` - creates transaction from Raw TX string. | ||
- `chain`: optional argument (default is `mainnet`; `ropsten`, `rinkeby`, `goerli`, `kovan` etc) | ||
@@ -117,3 +117,4 @@ - `hardfork`: optional argument (default is `london`). The only place we're checking for `hardfork` | ||
you'll probably won't need them | ||
- `type`: optional argument (default is `legacy`). Can be either `legacy`, `eip2930`, or `eip1559` (Berlin and London style transactions with access lists and maxFeePerGas/maxPriorityFeePerGas) | ||
- `type`: optional argument (default is `legacy`). Can be either `legacy`, `eip2930`, or `eip1559` | ||
(Berlin and London style transactions with access lists and `maxFeePerGas`/`maxPriorityFeePerGas`) | ||
- `new Transaction(rawTx[, chain, hardfork, type])` - creates transaction from Raw TX data. | ||
@@ -138,2 +139,4 @@ - `rawTx` must have fields `to`, `value`, `nonce`, `gasLimit` | ||
- `isSigned: boolean` - whether tx is signed with private key | ||
- `gasPrice: bigint` - legacy wei/gas | ||
- `maxFeePerGas: bigint`, `maxPriorityFeePerGas: bigint` - eip1559 wei/gas | ||
- `amount: bigint` - amount (aka `value`) in wei | ||
@@ -140,0 +143,0 @@ - `fee: bigint` - fee in wei (`maxFeePerGas` * `gasLimit` or `gasPrice` * `gasLimit`) |
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
29287
509
153