From 6eea59f8fe64872ae673278dd0212dde12f8c680 Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Tue, 24 Jan 2023 18:44:25 -0330 Subject: [PATCH 01/30] Add `Keyring` type WIP; description to be added later. --- src/encryption.ts | 6 ++ src/keyring.ts | 250 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 256 insertions(+) create mode 100644 src/encryption.ts create mode 100644 src/keyring.ts diff --git a/src/encryption.ts b/src/encryption.ts new file mode 100644 index 000000000..24cf9d3a4 --- /dev/null +++ b/src/encryption.ts @@ -0,0 +1,6 @@ +export type Eip1024EncryptedData = { + version: string; + nonce: string; + ephemPublicKey: string; + ciphertext: string; +}; diff --git a/src/keyring.ts b/src/keyring.ts new file mode 100644 index 000000000..62b907af8 --- /dev/null +++ b/src/keyring.ts @@ -0,0 +1,250 @@ +import type { Transaction } from '@ethereumjs/tx'; + +import type { Eip1024EncryptedData } from './encryption'; +import { Hex } from './hex'; +import { Json } from './json'; + +/** + * A Keyring class. + * + * This type is used to validate the constructor signature and the `type` + * static property on Keyring classes. See the {@link Keyring} type for more + * information. + */ +export type KeyringClass = { + /** + * The Keyring constructor. Takes a single parameter, an "options" object. + * See the documentation for the specific keyring for more information about + * what these options are. + * + * @param options - The constructor options. Differs between keyring + * implementations. + */ + new (options?: Record): Keyring; + + /** + * The name of this type of keyring. This must uniquely identify the + * keyring type. + */ + type: string; +}; + +/** + * A keyring is something that can sign messages. Keyrings are used to add new + * signing strategies; each strategy is a new keyring. + * + * Each keyring manages a collection of key pairs, which we call "accounts". + * Each account is referred to by its "address", which is a unique identifier + * derived from the public key. The address is always a "0x"-prefixed + * hexidecimal string. + * + * The keyring might store the private key for each account as well, but it's + * not guaranteed. Some keyrings delegate signing, so they don't need the + * private key directly. The keyring (and in particular the keyring state) + * should be treated with care though, just in case it does contain sensitive + * material such as a private key. + */ +export type Keyring = { + /** + * The name of this type of keyring. This must match the `type` property of + * the keyring class. + */ + type: string; + + /** + * Get the addresses for all accounts in this keyring. + * + * @returns A list of the account addresses for this keyring + */ + getAccounts(): Promise; + + /** + * Add an account to the keyring. + * + * @param number - The number of accounts to add. Usually defaults to 1. + * @returns A list of the newly added account addresses. + */ + addAccounts(number: number): Promise; + + /** + * Serialize the keyring state as a JSON-serializable object. + * + * @returns A JSON-serializable representation of the keyring state. + */ + serialize(): Promise; + + /** + * Deserialize the given keyring state, overwriting any existing state with + * the serialized state provided. + * + * @param state - A JSON-serializable representation of the keyring state. + */ + deserialize(state: State): Promise; + + /** + * Remove an account from the keyring. + * + * @param address - The address of the account to remove. + */ + removeAccount?(address: Hex): void; + + /** + * Export the private key for one of the keyring accounts. + * + * Some keyrings accept an "options" parameter as well. See the documentation + * for the specific keyring for more information about what these options + * are. For some keyrings, the options parameter is used to allow exporting a + * private key that is derived from the given account, rather than exporting + * that account's private key directly. + * + * @param address - The address of the account to export. + * @param options - Export options; differs between keyrings. + * @returns The non-prefixed, hex-encoded private key that was requested. + */ + exportAccount?( + address: Hex, + options?: Record, + ): Promise; + + /** + * Get the "app key" address for the given account and origin. An app key is + * an application-specific key pair. See {@link https://eips.ethereum.org/EIPS/eip-1775|EIP-1775} + * for more information. The {@link https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Origin|origin} + * is used as the unique identifier for the application, and it's used as + * part of the key derivation process. + * + * @param address - The address of the account the app key is derived from. + * @param origin - The origin of the application. + * @returns The address of the app key for the given account and origin. + */ + getAppKeyAddress?(address: Hex, origin: string): Promise; + + /** + * Sign a transaction. This is equivalent to the `eth_signTransaction` + * Ethereum JSON-RPC method. See the Ethereum JSON-RPC API documentation for + * more details. + * + * Some keyrings accept an "options" parameter as well. See the documentation + * for the specific keyring for more information about what these options + * are. For some keyrings, the options parameter can even change which key is + * used for signing (e.g. signing with app keys). + * + * @param address - The address of the account to use for signing. + * @param transaction - The transaction to sign. + * @param options - Signing options; differs between keyrings. + * @returns The signed tranasction. + */ + signTransaction?( + address: Hex, + transaction: Transaction, + options?: Record, + ): Promise; + + /** + * Sign a message. This is equivalent to an older version of the the + * `eth_sign` Ethereum JSON-RPC method. The message is signed using ECDSA, + * using the curve secp256k1 the Keccak-256 hash function. + * + * For more information about this method and why we still support it, see + * the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}. + * + * Some keyrings accept an "options" parameter as well. See the documentation + * for the specific keyring for more information about what these options + * are. For some keyrings, the options parameter can even change which key is + * used for signing (e.g. signing with app keys). + * + * @param address - The address of the account to use for signing. + * @param message - The message to sign. + * @param options - Signing options; differs between keyrings. + * @returns The signed message. + */ + signMessage?( + address: Hex, + message: string, + options?: Record, + ): Promise; + + /** + * Sign a message. This is equivalent to the `eth_sign` Ethereum JSON-RPC + * method, which is exposed by MetaMask as the method `personal_sign`. See + * the Ethereum JSON-RPC API documentation for more details. + * + * For more information about this method and why we call it `personal_sign`, + * see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}. + * + * Some keyrings accept an "options" parameter as well. See the documentation + * for the specific keyring for more information about what these options + * are. For some keyrings, the options parameter can even change which key is + * used for signing (e.g. signing with app keys). + * + * @param address - The address of the account to use for signing. + * @param message - The message to sign. + * @param options - Signing options; differs between keyrings. + * @returns The signed message. + */ + signPersonalMessage?( + address: Hex, + message: Hex, + options?: { version?: string } & Record, + ): Promise; + + /** + * Sign a message. This is equivalent to the `eth_signTypedData` Ethereum + * JSON-RPC method. See {@link https://github.com/ethereum/EIPs/blob/master/EIPS/eip-712.md|EIP-712} + * for more details. + * + * The "version" option dictates which version of `eth_signTypedData` is + * used. The latest version reflects the specification most closely, whereas + * earlier versions reflect earlier drafts of the specification that are + * still supported for backwards-compatibility reasons. For more information + * about why we support multiple versions, see the {@link https://docs.metamask.io/guide/signing-data.html|MetaMask Docs}. + * + * Some keyrings accept additional options as well. See the documentation for + * the specific keyring for more information about what these options are. + * For some keyrings, the options parameter can even change which key is used + * for signing (e.g. signing with app keys). + * + * @param address - The address of the account to use for signing. + * @param typedData - The data to sign. + * @param options - Signing options; differs between keyrings. + * @returns The signed message. + */ + signTypedData?( + address: Hex, + typedData: Record, + options?: Record, + ): Promise; + + /** + * Get a public key to use for encryption. This is equivalent to the + * ` eth_getEncryptionPublicKey` JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-getencryptionpublickey|MetaMask Docs} + * for more information. + * + * Some keyrings accept an "options" parameter as well. See the documentation + * for the specific keyring for more information about what these options + * are. For some keyrings, the options parameter can even change which key is + * used (e.g. encrypting with app keys). + * + * @param account - The address of the account you want the encryption key for. + * @param options - Options; differs between keyrings. + */ + getEncryptionPublicKey?( + account: Hex, + options?: Record, + ): Promise; + + /** + * Decrypt an encrypted message. This is equivalent to the ` eth_decrypt` + * JSON-RPC method. See the {@link https://docs.metamask.io/guide/rpc-api.html#eth-decrypt|MetaMask Docs} + * for more information. + * + * @param account - The address of the account you want to use to decrypt + * the message. + * @param encryptedData - The encrypted data that you want to decrypt. + * @returns The decrypted data. + */ + decryptMessage?( + account: Hex, + encryptedData: Eip1024EncryptedData, + ): Promise; +}; From 81cdb8e55c82d72a58ff111945c2a8229d2c436e Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Mon, 13 Feb 2023 20:12:03 -0300 Subject: [PATCH 02/30] Update file name "encryption" -> "encrytion-types" --- src/{encryption.ts => encryption-types.ts} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/{encryption.ts => encryption-types.ts} (100%) diff --git a/src/encryption.ts b/src/encryption-types.ts similarity index 100% rename from src/encryption.ts rename to src/encryption-types.ts From a787d08a9cdc1151d43731d6466f14e3f3f62c2e Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Mon, 13 Feb 2023 20:51:29 -0300 Subject: [PATCH 03/30] Add transaction type --- src/keyring.ts | 5 ++- src/transaction-types.ts | 73 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 75 insertions(+), 3 deletions(-) create mode 100644 src/transaction-types.ts diff --git a/src/keyring.ts b/src/keyring.ts index 62b907af8..ead01e4fc 100644 --- a/src/keyring.ts +++ b/src/keyring.ts @@ -1,8 +1,7 @@ -import type { Transaction } from '@ethereumjs/tx'; - -import type { Eip1024EncryptedData } from './encryption'; +import type { Eip1024EncryptedData } from './encryption-types'; import { Hex } from './hex'; import { Json } from './json'; +import type { Transaction } from './transaction-types'; /** * A Keyring class. diff --git a/src/transaction-types.ts b/src/transaction-types.ts new file mode 100644 index 000000000..08043be07 --- /dev/null +++ b/src/transaction-types.ts @@ -0,0 +1,73 @@ +// eslint-disable-next-line import/no-nodejs-modules +import { Buffer } from 'buffer'; + +export type Address = `0x${string}` | Buffer; + +export type Transaction = { + /** + * The transaction's nonce. + */ + nonce?: bigint | string | number | Buffer; + + /** + * The transaction's gas price. + */ + gasPrice?: bigint | string | number | Buffer | null; + + /** + * The transaction's gas limit. + */ + gasLimit?: bigint | string | number | Buffer; + + /** + * The maximum price of the consumed gas + * to be included as a tip to the validator + */ + maxPriorityFeePerGas?: bigint | string | number | Buffer; + + /** + * the maximum fee per unit of gas willing to be paid for the + * transaction (inclusive of _baseFeePerGas_ and _maxPriorityFeePerGas_) + */ + maxFeePerGas?: bigint | string | number | Buffer; + + /** + * The transaction's the address is from. + */ + from?: Address; + + /** + * The transaction's the address is sent to. + */ + to?: Address; + + /** + * The amount of Ether sent. + */ + value?: bigint | string | number | Buffer; + + /** + * This will contain the data of the message or the init of a contract. + */ + data?: Buffer; + + /** + * EC recovery ID. + */ + v?: bigint | string | number | Buffer; + + /** + * EC signature parameter. + */ + r?: bigint | string | number | Buffer; + + /** + * EC signature parameter. + */ + s?: bigint | string | number | Buffer; + + /** + * The transaction type + */ + type?: bigint | string | number | Buffer; +}; From 5d4ac3b9310125097f8a873ea47698b9218dd68e Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Mon, 13 Feb 2023 20:52:58 -0300 Subject: [PATCH 04/30] Disable no-restricted-globals --- src/transaction-types.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 08043be07..3739fdf5d 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,5 +1,4 @@ -// eslint-disable-next-line import/no-nodejs-modules -import { Buffer } from 'buffer'; +/* eslint-disable no-restricted-globals */ export type Address = `0x${string}` | Buffer; From c2426518e9de3f5e005da060f177d8fff6dcd8b3 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:02:23 -0300 Subject: [PATCH 05/30] Update data type to Hex --- src/transaction-types.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 3739fdf5d..0b008bfaf 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,6 +1,7 @@ /* eslint-disable no-restricted-globals */ +import { Hex } from './hex'; -export type Address = `0x${string}` | Buffer; +export type Address = Hex | Buffer; export type Transaction = { /** From 69426091961b24c343ecc4661176907ef2a8e3e0 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Tue, 14 Feb 2023 20:53:50 -0300 Subject: [PATCH 06/30] Proposed tx data types --- src/transaction-types.ts | 93 +++++++++++++++------------------------- 1 file changed, 34 insertions(+), 59 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 0b008bfaf..140531227 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -3,71 +3,46 @@ import { Hex } from './hex'; export type Address = Hex | Buffer; +// https://ethereum.org/en/developers/docs/transactions/ export type Transaction = { - /** - * The transaction's nonce. - */ - nonce?: bigint | string | number | Buffer; - - /** - * The transaction's gas price. - */ + nonce: bigint | string | number | Buffer; + from: Address; + to: Address; + data: Buffer; gasPrice?: bigint | string | number | Buffer | null; - - /** - * The transaction's gas limit. - */ gasLimit?: bigint | string | number | Buffer; - - /** - * The maximum price of the consumed gas - * to be included as a tip to the validator - */ - maxPriorityFeePerGas?: bigint | string | number | Buffer; - - /** - * the maximum fee per unit of gas willing to be paid for the - * transaction (inclusive of _baseFeePerGas_ and _maxPriorityFeePerGas_) - */ - maxFeePerGas?: bigint | string | number | Buffer; - - /** - * The transaction's the address is from. - */ - from?: Address; - - /** - * The transaction's the address is sent to. - */ - to?: Address; - - /** - * The amount of Ether sent. - */ value?: bigint | string | number | Buffer; - - /** - * This will contain the data of the message or the init of a contract. - */ - data?: Buffer; - - /** - * EC recovery ID. - */ v?: bigint | string | number | Buffer; - - /** - * EC signature parameter. - */ r?: bigint | string | number | Buffer; - - /** - * EC signature parameter. - */ s?: bigint | string | number | Buffer; - - /** - * The transaction type - */ type?: bigint | string | number | Buffer; }; + +// https://eips.ethereum.org/EIPS/eip-2930 +export type EIP2930Tx = { + chainId: bigint | string | number | Buffer; + accessList: { address: Hex; storageKeys: Hex[] }[]; +} & Transaction; + +// https://eips.ethereum.org/EIPS/eip-1559 +export type EIP1559Tx = { + gasPrice: never | null; + gas: bigint | string | number | Buffer; + maxPriorityFeePerGas?: bigint | string | number | Buffer; + maxFeePerGas?: bigint | string | number | Buffer; +} & EIP2930Tx; + +// https://eips.ethereum.org/EIPS/eip-4844 +export type EIP4844Tx = { + maxFeePerDataGas: bigint | string | number | Buffer; + blobVersionedHashes: Hex[] | string[] | Buffer[]; + blobs?: Buffer[] | Uint8Array[] | number[][] | number[] | bigint[] | Hex[]; + kzgCommitments?: + | Buffer[] + | Uint8Array[] + | number[][] + | number[] + | bigint[] + | Hex[]; + kzgProof?: Buffer | Uint8Array | number[] | number | bigint | Hex; +} & EIP1559Tx; From 8408cfa3743609dfd87807088c8fa0c03685461f Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:57:23 -0300 Subject: [PATCH 07/30] Update transaction types --- src/transaction-types.ts | 130 +++++++++++++++++++++++++++++---------- 1 file changed, 98 insertions(+), 32 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 140531227..d30a1d828 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,48 +1,114 @@ /* eslint-disable no-restricted-globals */ -import { Hex } from './hex'; +import { Address, Hex } from './hex'; -export type Address = Hex | Buffer; +export type Transaction = (LegacyTx | EIP2930Tx | EIP1559Tx) & Signature; -// https://ethereum.org/en/developers/docs/transactions/ -export type Transaction = { +export type Signature = { + /** + * EC signature parameter + * 32 bytes long sequence. + */ + r?: bigint | string | number | Buffer; + + /** + * EC signature parameter + * Signature proof. + * 32 bytes long sequence + */ + s?: bigint | string | number | Buffer; + + /** + * Recovery identifier. It can be either 0x1b or 0x1c + * 1 byte long sequence + */ + v?: bigint | string | number | Buffer; +}; + +/** + * Ethereum Legacy Transaction (Type-0) + * Reference: https://ethereum.org/en/developers/docs/transactions/ + */ +export type LegacyTx = { + /** + * Sequentially incrementing counter which indicates the transaction + * number from the account + */ nonce: bigint | string | number | Buffer; + /** + * The address of the sender, that will be signing the transaction + */ from: Address; + + /** + * The receiving address. + * If an externally-owned account, the transaction will transfer value. + * If a contract account, the transaction will execute the contract code. + */ to: Address; - data: Buffer; + + /** + * Arbitrary data. + */ + data?: Buffer; + + /** + * Transaction's gas price. + */ gasPrice?: bigint | string | number | Buffer | null; + + /** + * Maximum amount of gas units that this transaction can consume. + */ gasLimit?: bigint | string | number | Buffer; + + /** + * The amount of Ether sent. + */ value?: bigint | string | number | Buffer; - v?: bigint | string | number | Buffer; - r?: bigint | string | number | Buffer; - s?: bigint | string | number | Buffer; + + /** + * Transaction type. + */ type?: bigint | string | number | Buffer; }; -// https://eips.ethereum.org/EIPS/eip-2930 +/** + * EIP-2930 Transaction: Optional Access Lists + * Reference: https://eips.ethereum.org/EIPS/eip-2930 + */ export type EIP2930Tx = { - chainId: bigint | string | number | Buffer; - accessList: { address: Hex; storageKeys: Hex[] }[]; -} & Transaction; + /** + * Transaction chain ID + */ + chainId?: bigint | string | number | Buffer; + + /** + * List of addresses and storage keys that the transaction plans to access + */ + accessList?: + | { address: Hex; storageKeys: Hex[] }[] + | { address: Buffer; storageKeys: Buffer[] }[]; +} & LegacyTx; -// https://eips.ethereum.org/EIPS/eip-1559 +/** + * EIP-1559 Transaction: Fee market change for ETH 1.0 chain (Type-2) + * + * Reference: https://eips.ethereum.org/EIPS/eip-1559 + */ export type EIP1559Tx = { - gasPrice: never | null; - gas: bigint | string | number | Buffer; - maxPriorityFeePerGas?: bigint | string | number | Buffer; - maxFeePerGas?: bigint | string | number | Buffer; -} & EIP2930Tx; + /** + * Maximum fee to give to the miner + */ + maxPriorityFeePerGas: bigint | string | number | Buffer; -// https://eips.ethereum.org/EIPS/eip-4844 -export type EIP4844Tx = { - maxFeePerDataGas: bigint | string | number | Buffer; - blobVersionedHashes: Hex[] | string[] | Buffer[]; - blobs?: Buffer[] | Uint8Array[] | number[][] | number[] | bigint[] | Hex[]; - kzgCommitments?: - | Buffer[] - | Uint8Array[] - | number[][] - | number[] - | bigint[] - | Hex[]; - kzgProof?: Buffer | Uint8Array | number[] | number | bigint | Hex; -} & EIP1559Tx; + /** + * Maximum total fee + */ + maxFeePerGas: bigint | string | number | Buffer; + + /** + * Gas price from {@link LegacyTransaction}. + * Not necessary for EIP-1559 transactions. + */ + gasPrice?: never | null | undefined; +} & EIP2930Tx; From a0eacd56cdf459ec05f9719d55ae1adb169ead71 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:57:45 -0300 Subject: [PATCH 08/30] Move Address data type to Hex file --- src/hex.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/hex.ts b/src/hex.ts index 9cb4d92d7..14096da6a 100644 --- a/src/hex.ts +++ b/src/hex.ts @@ -1,9 +1,12 @@ +/* eslint-disable no-restricted-globals */ import { is, pattern, string, Struct } from 'superstruct'; import { assert } from './assert'; export type Hex = `0x${string}`; +export type Address = Hex | Buffer; + export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu); export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct< Hex, From c699732459cc68d7d36bb5a6929f5c8a49c4e17e Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Thu, 16 Feb 2023 16:59:00 -0300 Subject: [PATCH 09/30] Remove address data type --- src/hex.ts | 2 -- src/transaction-types.ts | 6 +++--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/hex.ts b/src/hex.ts index 14096da6a..d9aa873f0 100644 --- a/src/hex.ts +++ b/src/hex.ts @@ -5,8 +5,6 @@ import { assert } from './assert'; export type Hex = `0x${string}`; -export type Address = Hex | Buffer; - export const HexStruct = pattern(string(), /^(?:0x)?[0-9a-f]+$/iu); export const StrictHexStruct = pattern(string(), /^0x[0-9a-f]+$/iu) as Struct< Hex, diff --git a/src/transaction-types.ts b/src/transaction-types.ts index d30a1d828..3ee0c7a42 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,5 +1,5 @@ /* eslint-disable no-restricted-globals */ -import { Address, Hex } from './hex'; +import { Hex } from './hex'; export type Transaction = (LegacyTx | EIP2930Tx | EIP1559Tx) & Signature; @@ -37,14 +37,14 @@ export type LegacyTx = { /** * The address of the sender, that will be signing the transaction */ - from: Address; + from: Hex | Buffer; /** * The receiving address. * If an externally-owned account, the transaction will transfer value. * If a contract account, the transaction will execute the contract code. */ - to: Address; + to: Hex | Buffer; /** * Arbitrary data. From 305fce79c65b566359c6afe56f2964544972a044 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Thu, 16 Feb 2023 17:09:21 -0300 Subject: [PATCH 10/30] Remove eslint disable comment --- src/hex.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/hex.ts b/src/hex.ts index d9aa873f0..9cb4d92d7 100644 --- a/src/hex.ts +++ b/src/hex.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-restricted-globals */ import { is, pattern, string, Struct } from 'superstruct'; import { assert } from './assert'; From 0c59e3846a5d9bd90da3b8008170df263ed881c6 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 09:08:52 -0300 Subject: [PATCH 11/30] Change data type to Bytes --- src/transaction-types.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 3ee0c7a42..56f092067 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,4 +1,5 @@ /* eslint-disable no-restricted-globals */ +import { Bytes } from './bytes'; import { Hex } from './hex'; export type Transaction = (LegacyTx | EIP2930Tx | EIP1559Tx) & Signature; @@ -8,20 +9,20 @@ export type Signature = { * EC signature parameter * 32 bytes long sequence. */ - r?: bigint | string | number | Buffer; + r?: Bytes; /** * EC signature parameter * Signature proof. * 32 bytes long sequence */ - s?: bigint | string | number | Buffer; + s?: Bytes; /** * Recovery identifier. It can be either 0x1b or 0x1c * 1 byte long sequence */ - v?: bigint | string | number | Buffer; + v?: Bytes; }; /** From e0557ff2caa28552c155b8381d95611054888f34 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 09:09:26 -0300 Subject: [PATCH 12/30] Remove not technically correct comment --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 56f092067..45954ea88 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -26,7 +26,7 @@ export type Signature = { }; /** - * Ethereum Legacy Transaction (Type-0) + * Ethereum Legacy Transaction * Reference: https://ethereum.org/en/developers/docs/transactions/ */ export type LegacyTx = { From 23272708e667c444716e674d8927d00f79b85902 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 09:16:08 -0300 Subject: [PATCH 13/30] Update type names and relations --- src/transaction-types.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 45954ea88..14a3099bc 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -2,7 +2,12 @@ import { Bytes } from './bytes'; import { Hex } from './hex'; -export type Transaction = (LegacyTx | EIP2930Tx | EIP1559Tx) & Signature; +export type Transaction = ( + | LegacyTransaction + | EIP2930Transaction + | EIP1559Transaction +) & + Signature; export type Signature = { /** @@ -29,7 +34,7 @@ export type Signature = { * Ethereum Legacy Transaction * Reference: https://ethereum.org/en/developers/docs/transactions/ */ -export type LegacyTx = { +export type LegacyTransaction = { /** * Sequentially incrementing counter which indicates the transaction * number from the account @@ -77,7 +82,7 @@ export type LegacyTx = { * EIP-2930 Transaction: Optional Access Lists * Reference: https://eips.ethereum.org/EIPS/eip-2930 */ -export type EIP2930Tx = { +export type EIP2930Transaction = { /** * Transaction chain ID */ @@ -89,14 +94,14 @@ export type EIP2930Tx = { accessList?: | { address: Hex; storageKeys: Hex[] }[] | { address: Buffer; storageKeys: Buffer[] }[]; -} & LegacyTx; +} & LegacyTransaction; /** * EIP-1559 Transaction: Fee market change for ETH 1.0 chain (Type-2) * * Reference: https://eips.ethereum.org/EIPS/eip-1559 */ -export type EIP1559Tx = { +export type EIP1559Transaction = { /** * Maximum fee to give to the miner */ @@ -112,4 +117,4 @@ export type EIP1559Tx = { * Not necessary for EIP-1559 transactions. */ gasPrice?: never | null | undefined; -} & EIP2930Tx; +} & LegacyTransaction; From a5904194472e1405136bc2ac1c7c0175ed736e59 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 09:28:44 -0300 Subject: [PATCH 14/30] Rename attribute in Signature from v to yParity --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 14a3099bc..3504392a6 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -27,7 +27,7 @@ export type Signature = { * Recovery identifier. It can be either 0x1b or 0x1c * 1 byte long sequence */ - v?: Bytes; + yParity?: Bytes; }; /** From c134db2b8ec7f69e7265b2f4d56af4c5bdeaafda Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:18:22 -0300 Subject: [PATCH 15/30] Refactor transaction types --- src/transaction-types.ts | 76 +++++++++++++++++++++++----------------- 1 file changed, 44 insertions(+), 32 deletions(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 3504392a6..5435e16e1 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -2,44 +2,44 @@ import { Bytes } from './bytes'; import { Hex } from './hex'; -export type Transaction = ( +export type Transaction = | LegacyTransaction | EIP2930Transaction - | EIP1559Transaction -) & - Signature; + | EIP1559Transaction; + +export type SignedTransaction = Transaction & Signature; export type Signature = { /** * EC signature parameter * 32 bytes long sequence. */ - r?: Bytes; + r: Bytes; /** * EC signature parameter * Signature proof. * 32 bytes long sequence */ - s?: Bytes; + s: Bytes; /** * Recovery identifier. It can be either 0x1b or 0x1c * 1 byte long sequence */ - yParity?: Bytes; + yParity: Bytes; }; /** - * Ethereum Legacy Transaction - * Reference: https://ethereum.org/en/developers/docs/transactions/ + * Base Ethereum Transaction */ -export type LegacyTransaction = { +export type BaseTransaction = { /** * Sequentially incrementing counter which indicates the transaction * number from the account */ nonce: bigint | string | number | Buffer; + /** * The address of the sender, that will be signing the transaction */ @@ -53,55 +53,73 @@ export type LegacyTransaction = { to: Hex | Buffer; /** - * Arbitrary data. + * The amount of Ether sent. */ - data?: Buffer; + value: bigint | string | number | Buffer; /** - * Transaction's gas price. + * Maximum amount of gas units that this transaction can consume. */ - gasPrice?: bigint | string | number | Buffer | null; + gasLimit: bigint | string | number | Buffer; /** - * Maximum amount of gas units that this transaction can consume. + * Arbitrary data. */ - gasLimit?: bigint | string | number | Buffer; + data?: Buffer; +}; +export type TypedTransaction = BaseTransaction & { /** - * The amount of Ether sent. + * Transaction type. */ - value?: bigint | string | number | Buffer; + type: number; +}; +/** + * Ethereum Legacy Transaction + * Reference: https://ethereum.org/en/developers/docs/transactions/ + */ +export type LegacyTransaction = BaseTransaction & { /** - * Transaction type. + * Transaction's gas price. */ - type?: bigint | string | number | Buffer; + gasPrice?: bigint | string | number | Buffer | null; }; /** * EIP-2930 Transaction: Optional Access Lists * Reference: https://eips.ethereum.org/EIPS/eip-2930 */ -export type EIP2930Transaction = { +export type EIP2930Transaction = TypedTransaction & { + /** + * Transaction type. + */ + type: 1; + /** * Transaction chain ID */ - chainId?: bigint | string | number | Buffer; + chainId: bigint | string | number | Buffer; /** * List of addresses and storage keys that the transaction plans to access */ - accessList?: + accessList: | { address: Hex; storageKeys: Hex[] }[] | { address: Buffer; storageKeys: Buffer[] }[]; -} & LegacyTransaction; +}; /** * EIP-1559 Transaction: Fee market change for ETH 1.0 chain (Type-2) * * Reference: https://eips.ethereum.org/EIPS/eip-1559 */ -export type EIP1559Transaction = { +export type EIP1559Transaction = TypedTransaction & { + /** + * Transaction type. + */ + type: 2; + /** * Maximum fee to give to the miner */ @@ -111,10 +129,4 @@ export type EIP1559Transaction = { * Maximum total fee */ maxFeePerGas: bigint | string | number | Buffer; - - /** - * Gas price from {@link LegacyTransaction}. - * Not necessary for EIP-1559 transactions. - */ - gasPrice?: never | null | undefined; -} & LegacyTransaction; +}; From f328f6919185691b4f63d134434706218e84aa4d Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:18:54 -0300 Subject: [PATCH 16/30] Add commentary --- src/transaction-types.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 5435e16e1..09a414385 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -68,6 +68,9 @@ export type BaseTransaction = { data?: Buffer; }; +/** + * Typed Ethereum Transaction + */ export type TypedTransaction = BaseTransaction & { /** * Transaction type. From 33779ddfc63fb7dc3909e9e430f1fe42652dd2ef Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:19:49 -0300 Subject: [PATCH 17/30] Remove optionals --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 09a414385..387979bfa 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -86,7 +86,7 @@ export type LegacyTransaction = BaseTransaction & { /** * Transaction's gas price. */ - gasPrice?: bigint | string | number | Buffer | null; + gasPrice: bigint | string | number | Buffer | null; }; /** From a67a83c32453853a2b3e43e318097367f6085203 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 12:21:39 -0300 Subject: [PATCH 18/30] Update keyring with new types --- src/keyring.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/keyring.ts b/src/keyring.ts index ead01e4fc..d4859e650 100644 --- a/src/keyring.ts +++ b/src/keyring.ts @@ -1,7 +1,7 @@ import type { Eip1024EncryptedData } from './encryption-types'; import { Hex } from './hex'; import { Json } from './json'; -import type { Transaction } from './transaction-types'; +import type { Transaction, SignedTransaction } from './transaction-types'; /** * A Keyring class. @@ -131,13 +131,13 @@ export type Keyring = { * @param address - The address of the account to use for signing. * @param transaction - The transaction to sign. * @param options - Signing options; differs between keyrings. - * @returns The signed tranasction. + * @returns The signed transaction. */ signTransaction?( address: Hex, transaction: Transaction, options?: Record, - ): Promise; + ): Promise; /** * Sign a message. This is equivalent to an older version of the the From 3780038911a5a045143b7c119b0ba20b8f8ee562 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:09 -0300 Subject: [PATCH 19/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 387979bfa..a3e89da12 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -38,7 +38,7 @@ export type BaseTransaction = { * Sequentially incrementing counter which indicates the transaction * number from the account */ - nonce: bigint | string | number | Buffer; + nonce: Bytes; /** * The address of the sender, that will be signing the transaction From 2f383aea21c5902f495f6dd701644edb8c313394 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:18 -0300 Subject: [PATCH 20/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index a3e89da12..f2d875286 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -43,7 +43,7 @@ export type BaseTransaction = { /** * The address of the sender, that will be signing the transaction */ - from: Hex | Buffer; + from: Hex | Uint8Array; /** * The receiving address. From 4e5d65b2868205156864b92a4470ae8f17256c28 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:26 -0300 Subject: [PATCH 21/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index f2d875286..71b099d95 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -50,7 +50,7 @@ export type BaseTransaction = { * If an externally-owned account, the transaction will transfer value. * If a contract account, the transaction will execute the contract code. */ - to: Hex | Buffer; + to: Hex | Uint8Array; /** * The amount of Ether sent. From 2144cd6cd44933a5735448cc965ee87c71576feb Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:34 -0300 Subject: [PATCH 22/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 71b099d95..8a7da4780 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -60,7 +60,7 @@ export type BaseTransaction = { /** * Maximum amount of gas units that this transaction can consume. */ - gasLimit: bigint | string | number | Buffer; + gasLimit: Bytes; /** * Arbitrary data. From 05b677ace27b332e0945e6776aa471e93dd16f09 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:41:50 -0300 Subject: [PATCH 23/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 8a7da4780..d7e82b0fd 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -86,7 +86,7 @@ export type LegacyTransaction = BaseTransaction & { /** * Transaction's gas price. */ - gasPrice: bigint | string | number | Buffer | null; + gasPrice: Bytes | null; }; /** From b5fdde17c85557a38973049cd04ffc559ef93573 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:03 -0300 Subject: [PATCH 24/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index d7e82b0fd..0b59ae7be 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -55,7 +55,7 @@ export type BaseTransaction = { /** * The amount of Ether sent. */ - value: bigint | string | number | Buffer; + value: Bytes; /** * Maximum amount of gas units that this transaction can consume. From 9832f6d580cf1d8d8e0f542655e5490b1bfef9fe Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:16 -0300 Subject: [PATCH 25/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 0b59ae7be..21c957bc7 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -102,7 +102,7 @@ export type EIP2930Transaction = TypedTransaction & { /** * Transaction chain ID */ - chainId: bigint | string | number | Buffer; + chainId: Bytes; /** * List of addresses and storage keys that the transaction plans to access From e16ff86df3b9f77596f6ce5685c0aba2f00b1f0b Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:24 -0300 Subject: [PATCH 26/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 21c957bc7..e822ea3d0 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -109,7 +109,7 @@ export type EIP2930Transaction = TypedTransaction & { */ accessList: | { address: Hex; storageKeys: Hex[] }[] - | { address: Buffer; storageKeys: Buffer[] }[]; + | { address: Uint8Array; storageKeys: Uint8Array[] }[]; }; /** From a10996d6d0b73bca8bbd6a8403522241d9ca4e10 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:31 -0300 Subject: [PATCH 27/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index e822ea3d0..8b9c738ac 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -126,7 +126,7 @@ export type EIP1559Transaction = TypedTransaction & { /** * Maximum fee to give to the miner */ - maxPriorityFeePerGas: bigint | string | number | Buffer; + maxPriorityFeePerGas: Bytes; /** * Maximum total fee From 6f1d5697a6a00f9b8131fa12ca94008ac551f91a Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:41 -0300 Subject: [PATCH 28/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 8b9c738ac..fab7b08a1 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -131,5 +131,5 @@ export type EIP1559Transaction = TypedTransaction & { /** * Maximum total fee */ - maxFeePerGas: bigint | string | number | Buffer; + maxFeePerGas: Bytes; }; From c8cfa1e0022317d811d0b13f8d225fd6777707a9 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 13:42:52 -0300 Subject: [PATCH 29/30] Update data type Co-authored-by: Maarten Zuidhoorn --- src/transaction-types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index fab7b08a1..503b32d02 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -65,7 +65,7 @@ export type BaseTransaction = { /** * Arbitrary data. */ - data?: Buffer; + data?: Bytes; }; /** From 67896a91770067d8e09151cc3ff1e124fe1ddc84 Mon Sep 17 00:00:00 2001 From: Gustavo Antunes <17601467+gantunesr@users.noreply.github.com> Date: Fri, 17 Feb 2023 16:53:33 -0300 Subject: [PATCH 30/30] Remove linter disable comment --- src/transaction-types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/src/transaction-types.ts b/src/transaction-types.ts index 503b32d02..691d6b321 100644 --- a/src/transaction-types.ts +++ b/src/transaction-types.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-restricted-globals */ import { Bytes } from './bytes'; import { Hex } from './hex';