ethers.js/packages/transactions/lib/index.js

412 lines
16 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
2021-03-07 18:24:04 -05:00
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;
});
2019-05-14 18:48:48 -04:00
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
2021-03-07 18:24:04 -05:00
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
2019-05-14 18:48:48 -04:00
return result;
};
Object.defineProperty(exports, "__esModule", { value: true });
2021-06-24 02:13:06 -04:00
exports.parse = exports.serialize = exports.accessListify = exports.recoverAddress = exports.computeAddress = exports.TransactionTypes = void 0;
2019-05-14 18:48:48 -04:00
var address_1 = require("@ethersproject/address");
var bignumber_1 = require("@ethersproject/bignumber");
var bytes_1 = require("@ethersproject/bytes");
var constants_1 = require("@ethersproject/constants");
var keccak256_1 = require("@ethersproject/keccak256");
var properties_1 = require("@ethersproject/properties");
var RLP = __importStar(require("@ethersproject/rlp"));
var signing_key_1 = require("@ethersproject/signing-key");
2019-08-02 02:10:58 -04:00
var logger_1 = require("@ethersproject/logger");
var _version_1 = require("./_version");
var logger = new logger_1.Logger(_version_1.version);
2021-06-24 02:13:06 -04:00
var TransactionTypes;
(function (TransactionTypes) {
TransactionTypes[TransactionTypes["legacy"] = 0] = "legacy";
TransactionTypes[TransactionTypes["eip2930"] = 1] = "eip2930";
TransactionTypes[TransactionTypes["eip1559"] = 2] = "eip1559";
})(TransactionTypes = exports.TransactionTypes || (exports.TransactionTypes = {}));
;
2019-05-14 18:48:48 -04:00
///////////////////////////////
function handleAddress(value) {
if (value === "0x") {
return null;
}
2021-10-16 02:29:27 -04:00
return (0, address_1.getAddress)(value);
2019-05-14 18:48:48 -04:00
}
function handleNumber(value) {
if (value === "0x") {
return constants_1.Zero;
}
return bignumber_1.BigNumber.from(value);
}
2021-03-30 15:22:45 -04:00
// Legacy Transaction Fields
2019-05-14 18:48:48 -04:00
var transactionFields = [
2019-09-28 02:36:19 -04:00
{ name: "nonce", maxLength: 32, numeric: true },
{ name: "gasPrice", maxLength: 32, numeric: true },
{ name: "gasLimit", maxLength: 32, numeric: true },
2019-05-14 18:48:48 -04:00
{ name: "to", length: 20 },
2019-09-28 02:36:19 -04:00
{ name: "value", maxLength: 32, numeric: true },
2019-05-14 18:48:48 -04:00
{ name: "data" },
];
var allowedTransactionKeys = {
2021-06-24 02:13:06 -04:00
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, type: true, value: true
2019-05-14 18:48:48 -04:00
};
function computeAddress(key) {
2021-10-16 02:29:27 -04:00
var publicKey = (0, signing_key_1.computePublicKey)(key);
return (0, address_1.getAddress)((0, bytes_1.hexDataSlice)((0, keccak256_1.keccak256)((0, bytes_1.hexDataSlice)(publicKey, 1)), 12));
2019-05-14 18:48:48 -04:00
}
exports.computeAddress = computeAddress;
function recoverAddress(digest, signature) {
2021-10-16 02:29:27 -04:00
return computeAddress((0, signing_key_1.recoverPublicKey)((0, bytes_1.arrayify)(digest), signature));
2019-05-14 18:48:48 -04:00
}
exports.recoverAddress = recoverAddress;
2021-03-30 15:22:45 -04:00
function formatNumber(value, name) {
2021-10-16 02:29:27 -04:00
var result = (0, bytes_1.stripZeros)(bignumber_1.BigNumber.from(value).toHexString());
2021-03-30 15:22:45 -04:00
if (result.length > 32) {
logger.throwArgumentError("invalid length for " + name, ("transaction:" + name), value);
}
return result;
}
function accessSetify(addr, storageKeys) {
return {
2021-10-16 02:29:27 -04:00
address: (0, address_1.getAddress)(addr),
2021-03-30 15:22:45 -04:00
storageKeys: (storageKeys || []).map(function (storageKey, index) {
2021-10-16 02:29:27 -04:00
if ((0, bytes_1.hexDataLength)(storageKey) !== 32) {
2021-03-30 15:22:45 -04:00
logger.throwArgumentError("invalid access list storageKey", "accessList[" + addr + ":" + index + "]", storageKey);
}
return storageKey.toLowerCase();
})
};
}
function accessListify(value) {
if (Array.isArray(value)) {
return value.map(function (set, index) {
if (Array.isArray(set)) {
if (set.length > 2) {
logger.throwArgumentError("access list expected to be [ address, storageKeys[] ]", "value[" + index + "]", set);
}
return accessSetify(set[0], set[1]);
}
return accessSetify(set.address, set.storageKeys);
});
}
var result = Object.keys(value).map(function (addr) {
var storageKeys = value[addr].reduce(function (accum, storageKey) {
accum[storageKey] = true;
return accum;
}, {});
return accessSetify(addr, Object.keys(storageKeys).sort());
});
result.sort(function (a, b) { return (a.address.localeCompare(b.address)); });
return result;
}
exports.accessListify = accessListify;
function formatAccessList(value) {
return accessListify(value).map(function (set) { return [set.address, set.storageKeys]; });
}
2021-06-24 02:13:06 -04:00
function _serializeEip1559(transaction, signature) {
// If there is an explicit gasPrice, make sure it matches the
// EIP-1559 fees; otherwise they may not understand what they
// think they are setting in terms of fee.
if (transaction.gasPrice != null) {
var gasPrice = bignumber_1.BigNumber.from(transaction.gasPrice);
var maxFeePerGas = bignumber_1.BigNumber.from(transaction.maxFeePerGas || 0);
if (!gasPrice.eq(maxFeePerGas)) {
logger.throwArgumentError("mismatch EIP-1559 gasPrice != maxFeePerGas", "tx", {
2021-10-16 02:29:27 -04:00
gasPrice: gasPrice,
maxFeePerGas: maxFeePerGas
2021-06-24 02:13:06 -04:00
});
}
}
var fields = [
formatNumber(transaction.chainId || 0, "chainId"),
formatNumber(transaction.nonce || 0, "nonce"),
formatNumber(transaction.maxPriorityFeePerGas || 0, "maxPriorityFeePerGas"),
formatNumber(transaction.maxFeePerGas || 0, "maxFeePerGas"),
formatNumber(transaction.gasLimit || 0, "gasLimit"),
2021-10-16 02:29:27 -04:00
((transaction.to != null) ? (0, address_1.getAddress)(transaction.to) : "0x"),
2021-06-24 02:13:06 -04:00
formatNumber(transaction.value || 0, "value"),
(transaction.data || "0x"),
(formatAccessList(transaction.accessList || []))
];
if (signature) {
2021-10-16 02:29:27 -04:00
var sig = (0, bytes_1.splitSignature)(signature);
2021-06-24 02:13:06 -04:00
fields.push(formatNumber(sig.recoveryParam, "recoveryParam"));
2021-10-16 02:29:27 -04:00
fields.push((0, bytes_1.stripZeros)(sig.r));
fields.push((0, bytes_1.stripZeros)(sig.s));
2021-06-24 02:13:06 -04:00
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexConcat)(["0x02", RLP.encode(fields)]);
2021-06-24 02:13:06 -04:00
}
2021-03-30 15:22:45 -04:00
function _serializeEip2930(transaction, signature) {
var fields = [
formatNumber(transaction.chainId || 0, "chainId"),
formatNumber(transaction.nonce || 0, "nonce"),
formatNumber(transaction.gasPrice || 0, "gasPrice"),
formatNumber(transaction.gasLimit || 0, "gasLimit"),
2021-10-16 02:29:27 -04:00
((transaction.to != null) ? (0, address_1.getAddress)(transaction.to) : "0x"),
2021-03-30 15:22:45 -04:00
formatNumber(transaction.value || 0, "value"),
(transaction.data || "0x"),
(formatAccessList(transaction.accessList || []))
];
if (signature) {
2021-10-16 02:29:27 -04:00
var sig = (0, bytes_1.splitSignature)(signature);
2021-03-30 15:22:45 -04:00
fields.push(formatNumber(sig.recoveryParam, "recoveryParam"));
2021-10-16 02:29:27 -04:00
fields.push((0, bytes_1.stripZeros)(sig.r));
fields.push((0, bytes_1.stripZeros)(sig.s));
2021-03-30 15:22:45 -04:00
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexConcat)(["0x01", RLP.encode(fields)]);
2021-03-30 15:22:45 -04:00
}
// Legacy Transactions and EIP-155
function _serialize(transaction, signature) {
2021-10-16 02:29:27 -04:00
(0, properties_1.checkProperties)(transaction, allowedTransactionKeys);
2019-05-14 18:48:48 -04:00
var raw = [];
transactionFields.forEach(function (fieldInfo) {
var value = transaction[fieldInfo.name] || ([]);
2019-09-28 02:36:19 -04:00
var options = {};
if (fieldInfo.numeric) {
options.hexPad = "left";
}
2021-10-16 02:29:27 -04:00
value = (0, bytes_1.arrayify)((0, bytes_1.hexlify)(value, options));
2019-05-14 18:48:48 -04:00
// Fixed-width field
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value);
2019-05-14 18:48:48 -04:00
}
// Variable-width (with a maximum)
if (fieldInfo.maxLength) {
2021-10-16 02:29:27 -04:00
value = (0, bytes_1.stripZeros)(value);
2019-05-14 18:48:48 -04:00
if (value.length > fieldInfo.maxLength) {
2019-08-02 02:10:58 -04:00
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value);
2019-05-14 18:48:48 -04:00
}
}
2021-10-16 02:29:27 -04:00
raw.push((0, bytes_1.hexlify)(value));
2019-05-14 18:48:48 -04:00
});
2020-02-04 01:06:47 -05:00
var chainId = 0;
if (transaction.chainId != null) {
// A chainId was provided; if non-zero we'll use EIP-155
chainId = transaction.chainId;
if (typeof (chainId) !== "number") {
logger.throwArgumentError("invalid transaction.chainId", "transaction", transaction);
}
}
2021-10-16 02:29:27 -04:00
else if (signature && !(0, bytes_1.isBytesLike)(signature) && signature.v > 28) {
2020-02-04 01:06:47 -05:00
// No chainId provided, but the signature is signing with EIP-155; derive chainId
chainId = Math.floor((signature.v - 35) / 2);
}
// We have an EIP-155 transaction (chainId was specified and non-zero)
if (chainId !== 0) {
2021-10-16 02:29:27 -04:00
raw.push((0, bytes_1.hexlify)(chainId)); // @TODO: hexValue?
2019-05-14 18:48:48 -04:00
raw.push("0x");
raw.push("0x");
}
2021-10-16 02:29:27 -04:00
// Requesting an unsigned transaction
2019-05-14 18:48:48 -04:00
if (!signature) {
2020-02-04 01:06:47 -05:00
return RLP.encode(raw);
2019-05-14 18:48:48 -04:00
}
// The splitSignature will ensure the transaction has a recoveryParam in the
// case that the signTransaction function only adds a v.
2021-10-16 02:29:27 -04:00
var sig = (0, bytes_1.splitSignature)(signature);
2019-05-14 18:48:48 -04:00
// We pushed a chainId and null r, s on for hashing only; remove those
var v = 27 + sig.recoveryParam;
2020-02-04 01:06:47 -05:00
if (chainId !== 0) {
2019-05-14 18:48:48 -04:00
raw.pop();
raw.pop();
raw.pop();
2020-02-04 01:06:47 -05:00
v += chainId * 2 + 8;
// If an EIP-155 v (directly or indirectly; maybe _vs) was provided, check it!
if (sig.v > 28 && sig.v !== v) {
logger.throwArgumentError("transaction.chainId/signature.v mismatch", "signature", signature);
}
}
else if (sig.v !== v) {
logger.throwArgumentError("transaction.chainId/signature.v mismatch", "signature", signature);
2019-05-14 18:48:48 -04:00
}
2021-10-16 02:29:27 -04:00
raw.push((0, bytes_1.hexlify)(v));
raw.push((0, bytes_1.stripZeros)((0, bytes_1.arrayify)(sig.r)));
raw.push((0, bytes_1.stripZeros)((0, bytes_1.arrayify)(sig.s)));
2019-05-14 18:48:48 -04:00
return RLP.encode(raw);
}
2021-03-30 15:22:45 -04:00
function serialize(transaction, signature) {
// Legacy and EIP-155 Transactions
2021-06-24 02:13:06 -04:00
if (transaction.type == null || transaction.type === 0) {
2021-04-17 22:41:09 -04:00
if (transaction.accessList != null) {
logger.throwArgumentError("untyped transactions do not support accessList; include type: 1", "transaction", transaction);
}
2021-03-30 15:22:45 -04:00
return _serialize(transaction, signature);
}
// Typed Transactions (EIP-2718)
switch (transaction.type) {
case 1:
return _serializeEip2930(transaction, signature);
2021-06-24 02:13:06 -04:00
case 2:
return _serializeEip1559(transaction, signature);
2021-03-30 15:22:45 -04:00
default:
break;
}
return logger.throwError("unsupported transaction type: " + transaction.type, logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "serializeTransaction",
transactionType: transaction.type
});
}
2019-05-14 18:48:48 -04:00
exports.serialize = serialize;
2021-06-24 02:13:06 -04:00
function _parseEipSignature(tx, fields, serialize) {
try {
var recid = handleNumber(fields[0]).toNumber();
if (recid !== 0 && recid !== 1) {
throw new Error("bad recid");
}
tx.v = recid;
}
catch (error) {
logger.throwArgumentError("invalid v for transaction type: 1", "v", fields[0]);
}
2021-10-16 02:29:27 -04:00
tx.r = (0, bytes_1.hexZeroPad)(fields[1], 32);
tx.s = (0, bytes_1.hexZeroPad)(fields[2], 32);
2021-06-24 02:13:06 -04:00
try {
2021-10-16 02:29:27 -04:00
var digest = (0, keccak256_1.keccak256)(serialize(tx));
2021-06-24 02:13:06 -04:00
tx.from = recoverAddress(digest, { r: tx.r, s: tx.s, recoveryParam: tx.v });
}
2022-05-20 19:13:03 -04:00
catch (error) { }
2021-06-24 02:13:06 -04:00
}
function _parseEip1559(payload) {
var transaction = RLP.decode(payload.slice(1));
if (transaction.length !== 9 && transaction.length !== 12) {
2021-10-16 02:29:27 -04:00
logger.throwArgumentError("invalid component count for transaction type: 2", "payload", (0, bytes_1.hexlify)(payload));
2021-06-24 02:13:06 -04:00
}
var maxPriorityFeePerGas = handleNumber(transaction[2]);
var maxFeePerGas = handleNumber(transaction[3]);
var tx = {
type: 2,
chainId: handleNumber(transaction[0]).toNumber(),
nonce: handleNumber(transaction[1]).toNumber(),
maxPriorityFeePerGas: maxPriorityFeePerGas,
maxFeePerGas: maxFeePerGas,
2021-06-26 01:55:19 -04:00
gasPrice: null,
2021-06-24 02:13:06 -04:00
gasLimit: handleNumber(transaction[4]),
to: handleAddress(transaction[5]),
value: handleNumber(transaction[6]),
data: transaction[7],
accessList: accessListify(transaction[8]),
};
// Unsigned EIP-1559 Transaction
if (transaction.length === 9) {
return tx;
}
2021-10-16 02:29:27 -04:00
tx.hash = (0, keccak256_1.keccak256)(payload);
2021-06-24 02:13:06 -04:00
_parseEipSignature(tx, transaction.slice(9), _serializeEip1559);
return tx;
}
2021-03-30 15:22:45 -04:00
function _parseEip2930(payload) {
var transaction = RLP.decode(payload.slice(1));
if (transaction.length !== 8 && transaction.length !== 11) {
2021-10-16 02:29:27 -04:00
logger.throwArgumentError("invalid component count for transaction type: 1", "payload", (0, bytes_1.hexlify)(payload));
2021-03-30 15:22:45 -04:00
}
var tx = {
type: 1,
chainId: handleNumber(transaction[0]).toNumber(),
nonce: handleNumber(transaction[1]).toNumber(),
gasPrice: handleNumber(transaction[2]),
gasLimit: handleNumber(transaction[3]),
to: handleAddress(transaction[4]),
value: handleNumber(transaction[5]),
data: transaction[6],
2021-06-24 02:13:06 -04:00
accessList: accessListify(transaction[7])
2021-03-30 15:22:45 -04:00
};
// Unsigned EIP-2930 Transaction
if (transaction.length === 8) {
return tx;
}
2021-10-16 02:29:27 -04:00
tx.hash = (0, keccak256_1.keccak256)(payload);
2021-06-24 02:13:06 -04:00
_parseEipSignature(tx, transaction.slice(8), _serializeEip2930);
2021-03-30 15:22:45 -04:00
return tx;
}
// Legacy Transactions and EIP-155
function _parse(rawTransaction) {
2019-05-14 18:48:48 -04:00
var transaction = RLP.decode(rawTransaction);
if (transaction.length !== 9 && transaction.length !== 6) {
2020-04-23 23:35:39 -04:00
logger.throwArgumentError("invalid raw transaction", "rawTransaction", rawTransaction);
2019-05-14 18:48:48 -04:00
}
var tx = {
nonce: handleNumber(transaction[0]).toNumber(),
gasPrice: handleNumber(transaction[1]),
gasLimit: handleNumber(transaction[2]),
to: handleAddress(transaction[3]),
value: handleNumber(transaction[4]),
data: transaction[5],
chainId: 0
};
// Legacy unsigned transaction
if (transaction.length === 6) {
return tx;
}
try {
tx.v = bignumber_1.BigNumber.from(transaction[6]).toNumber();
}
catch (error) {
2022-05-20 19:13:03 -04:00
// @TODO: What makes snese to do? The v is too big
2019-05-14 18:48:48 -04:00
return tx;
}
2021-10-16 02:29:27 -04:00
tx.r = (0, bytes_1.hexZeroPad)(transaction[7], 32);
tx.s = (0, bytes_1.hexZeroPad)(transaction[8], 32);
2019-05-14 18:48:48 -04:00
if (bignumber_1.BigNumber.from(tx.r).isZero() && bignumber_1.BigNumber.from(tx.s).isZero()) {
// EIP-155 unsigned transaction
tx.chainId = tx.v;
tx.v = 0;
}
else {
2021-10-16 02:29:27 -04:00
// Signed Transaction
2019-05-14 18:48:48 -04:00
tx.chainId = Math.floor((tx.v - 35) / 2);
if (tx.chainId < 0) {
tx.chainId = 0;
}
var recoveryParam = tx.v - 27;
var raw = transaction.slice(0, 6);
if (tx.chainId !== 0) {
2021-10-16 02:29:27 -04:00
raw.push((0, bytes_1.hexlify)(tx.chainId));
2019-05-14 18:48:48 -04:00
raw.push("0x");
raw.push("0x");
recoveryParam -= tx.chainId * 2 + 8;
}
2021-10-16 02:29:27 -04:00
var digest = (0, keccak256_1.keccak256)(RLP.encode(raw));
2019-05-14 18:48:48 -04:00
try {
2021-10-16 02:29:27 -04:00
tx.from = recoverAddress(digest, { r: (0, bytes_1.hexlify)(tx.r), s: (0, bytes_1.hexlify)(tx.s), recoveryParam: recoveryParam });
2019-05-14 18:48:48 -04:00
}
2022-05-20 19:13:03 -04:00
catch (error) { }
2021-10-16 02:29:27 -04:00
tx.hash = (0, keccak256_1.keccak256)(rawTransaction);
2019-05-14 18:48:48 -04:00
}
2021-03-30 15:22:45 -04:00
tx.type = null;
2019-05-14 18:48:48 -04:00
return tx;
}
2021-03-30 15:22:45 -04:00
function parse(rawTransaction) {
2021-10-16 02:29:27 -04:00
var payload = (0, bytes_1.arrayify)(rawTransaction);
2021-03-30 15:22:45 -04:00
// Legacy and EIP-155 Transactions
if (payload[0] > 0x7f) {
return _parse(payload);
}
// Typed Transaction (EIP-2718)
switch (payload[0]) {
case 1:
return _parseEip2930(payload);
2021-06-24 02:13:06 -04:00
case 2:
return _parseEip1559(payload);
2021-03-30 15:22:45 -04:00
default:
break;
}
return logger.throwError("unsupported transaction type: " + payload[0], logger_1.Logger.errors.UNSUPPORTED_OPERATION, {
operation: "parseTransaction",
transactionType: payload[0]
});
}
2019-05-14 18:48:48 -04:00
exports.parse = parse;
2020-07-13 08:03:56 -04:00
//# sourceMappingURL=index.js.map