2019-08-25 09:39:20 +03:00
|
|
|
"use strict";
|
|
|
|
import { getAddress } from "@ethersproject/address";
|
|
|
|
import { BigNumber } from "@ethersproject/bignumber";
|
2021-03-30 22:22:45 +03:00
|
|
|
import { arrayify, hexConcat, hexDataLength, hexDataSlice, hexlify, hexZeroPad, isBytesLike, splitSignature, stripZeros, } from "@ethersproject/bytes";
|
2019-08-25 09:39:20 +03:00
|
|
|
import { Zero } from "@ethersproject/constants";
|
|
|
|
import { keccak256 } from "@ethersproject/keccak256";
|
|
|
|
import { checkProperties } from "@ethersproject/properties";
|
|
|
|
import * as RLP from "@ethersproject/rlp";
|
|
|
|
import { computePublicKey, recoverPublicKey } from "@ethersproject/signing-key";
|
|
|
|
import { Logger } from "@ethersproject/logger";
|
|
|
|
import { version } from "./_version";
|
|
|
|
const logger = new Logger(version);
|
|
|
|
///////////////////////////////
|
|
|
|
function handleAddress(value) {
|
|
|
|
if (value === "0x") {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return getAddress(value);
|
|
|
|
}
|
|
|
|
function handleNumber(value) {
|
|
|
|
if (value === "0x") {
|
|
|
|
return Zero;
|
|
|
|
}
|
|
|
|
return BigNumber.from(value);
|
|
|
|
}
|
2021-03-30 22:22:45 +03:00
|
|
|
// Legacy Transaction Fields
|
2019-08-25 09:39:20 +03:00
|
|
|
const transactionFields = [
|
2019-09-28 09:36:19 +03:00
|
|
|
{ name: "nonce", maxLength: 32, numeric: true },
|
|
|
|
{ name: "gasPrice", maxLength: 32, numeric: true },
|
|
|
|
{ name: "gasLimit", maxLength: 32, numeric: true },
|
2019-08-25 09:39:20 +03:00
|
|
|
{ name: "to", length: 20 },
|
2019-09-28 09:36:19 +03:00
|
|
|
{ name: "value", maxLength: 32, numeric: true },
|
2019-08-25 09:39:20 +03:00
|
|
|
{ name: "data" },
|
|
|
|
];
|
|
|
|
const allowedTransactionKeys = {
|
|
|
|
chainId: true, data: true, gasLimit: true, gasPrice: true, nonce: true, to: true, value: true
|
|
|
|
};
|
|
|
|
export function computeAddress(key) {
|
2019-11-20 12:57:38 +03:00
|
|
|
const publicKey = computePublicKey(key);
|
2019-08-25 09:39:20 +03:00
|
|
|
return getAddress(hexDataSlice(keccak256(hexDataSlice(publicKey, 1)), 12));
|
|
|
|
}
|
|
|
|
export function recoverAddress(digest, signature) {
|
|
|
|
return computeAddress(recoverPublicKey(arrayify(digest), signature));
|
|
|
|
}
|
2021-03-30 22:22:45 +03:00
|
|
|
function formatNumber(value, name) {
|
|
|
|
const result = stripZeros(BigNumber.from(value).toHexString());
|
|
|
|
if (result.length > 32) {
|
|
|
|
logger.throwArgumentError("invalid length for " + name, ("transaction:" + name), value);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function accessSetify(addr, storageKeys) {
|
|
|
|
return {
|
|
|
|
address: getAddress(addr),
|
|
|
|
storageKeys: (storageKeys || []).map((storageKey, index) => {
|
|
|
|
if (hexDataLength(storageKey) !== 32) {
|
|
|
|
logger.throwArgumentError("invalid access list storageKey", `accessList[${addr}:${index}]`, storageKey);
|
|
|
|
}
|
|
|
|
return storageKey.toLowerCase();
|
|
|
|
})
|
|
|
|
};
|
|
|
|
}
|
|
|
|
export function accessListify(value) {
|
|
|
|
if (Array.isArray(value)) {
|
|
|
|
return value.map((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);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const result = Object.keys(value).map((addr) => {
|
|
|
|
const storageKeys = value[addr].reduce((accum, storageKey) => {
|
|
|
|
accum[storageKey] = true;
|
|
|
|
return accum;
|
|
|
|
}, {});
|
|
|
|
return accessSetify(addr, Object.keys(storageKeys).sort());
|
|
|
|
});
|
|
|
|
result.sort((a, b) => (a.address.localeCompare(b.address)));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
function formatAccessList(value) {
|
|
|
|
return accessListify(value).map((set) => [set.address, set.storageKeys]);
|
|
|
|
}
|
|
|
|
function _serializeEip2930(transaction, signature) {
|
|
|
|
const fields = [
|
|
|
|
formatNumber(transaction.chainId || 0, "chainId"),
|
|
|
|
formatNumber(transaction.nonce || 0, "nonce"),
|
|
|
|
formatNumber(transaction.gasPrice || 0, "gasPrice"),
|
|
|
|
formatNumber(transaction.gasLimit || 0, "gasLimit"),
|
|
|
|
((transaction.to != null) ? getAddress(transaction.to) : "0x"),
|
|
|
|
formatNumber(transaction.value || 0, "value"),
|
|
|
|
(transaction.data || "0x"),
|
|
|
|
(formatAccessList(transaction.accessList || []))
|
|
|
|
];
|
|
|
|
if (signature) {
|
|
|
|
const sig = splitSignature(signature);
|
|
|
|
fields.push(formatNumber(sig.recoveryParam, "recoveryParam"));
|
|
|
|
fields.push(stripZeros(sig.r));
|
|
|
|
fields.push(stripZeros(sig.s));
|
|
|
|
}
|
|
|
|
return hexConcat(["0x01", RLP.encode(fields)]);
|
|
|
|
}
|
|
|
|
// Legacy Transactions and EIP-155
|
|
|
|
function _serialize(transaction, signature) {
|
2019-08-25 09:39:20 +03:00
|
|
|
checkProperties(transaction, allowedTransactionKeys);
|
2019-11-20 12:57:38 +03:00
|
|
|
const raw = [];
|
2019-08-25 09:39:20 +03:00
|
|
|
transactionFields.forEach(function (fieldInfo) {
|
|
|
|
let value = transaction[fieldInfo.name] || ([]);
|
2019-09-28 09:36:19 +03:00
|
|
|
const options = {};
|
|
|
|
if (fieldInfo.numeric) {
|
|
|
|
options.hexPad = "left";
|
|
|
|
}
|
|
|
|
value = arrayify(hexlify(value, options));
|
2019-08-25 09:39:20 +03:00
|
|
|
// Fixed-width field
|
|
|
|
if (fieldInfo.length && value.length !== fieldInfo.length && value.length > 0) {
|
|
|
|
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value);
|
|
|
|
}
|
|
|
|
// Variable-width (with a maximum)
|
|
|
|
if (fieldInfo.maxLength) {
|
|
|
|
value = stripZeros(value);
|
|
|
|
if (value.length > fieldInfo.maxLength) {
|
|
|
|
logger.throwArgumentError("invalid length for " + fieldInfo.name, ("transaction:" + fieldInfo.name), value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
raw.push(hexlify(value));
|
|
|
|
});
|
2020-02-04 09:06:47 +03:00
|
|
|
let 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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (signature && !isBytesLike(signature) && signature.v > 28) {
|
|
|
|
// 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) {
|
2020-09-06 06:35:35 +03:00
|
|
|
raw.push(hexlify(chainId)); // @TODO: hexValue?
|
2019-08-25 09:39:20 +03:00
|
|
|
raw.push("0x");
|
|
|
|
raw.push("0x");
|
|
|
|
}
|
|
|
|
// Requesting an unsigned transation
|
|
|
|
if (!signature) {
|
2020-02-04 09:06:47 +03:00
|
|
|
return RLP.encode(raw);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
// The splitSignature will ensure the transaction has a recoveryParam in the
|
|
|
|
// case that the signTransaction function only adds a v.
|
2019-11-20 12:57:38 +03:00
|
|
|
const sig = splitSignature(signature);
|
2019-08-25 09:39:20 +03:00
|
|
|
// We pushed a chainId and null r, s on for hashing only; remove those
|
|
|
|
let v = 27 + sig.recoveryParam;
|
2020-02-04 09:06:47 +03:00
|
|
|
if (chainId !== 0) {
|
2019-08-25 09:39:20 +03:00
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
|
|
|
raw.pop();
|
2020-02-04 09:06:47 +03: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-08-25 09:39:20 +03:00
|
|
|
}
|
|
|
|
raw.push(hexlify(v));
|
|
|
|
raw.push(stripZeros(arrayify(sig.r)));
|
|
|
|
raw.push(stripZeros(arrayify(sig.s)));
|
|
|
|
return RLP.encode(raw);
|
|
|
|
}
|
2021-03-30 22:22:45 +03:00
|
|
|
export function serialize(transaction, signature) {
|
|
|
|
// Legacy and EIP-155 Transactions
|
|
|
|
if (transaction.type == null) {
|
|
|
|
return _serialize(transaction, signature);
|
|
|
|
}
|
|
|
|
// Typed Transactions (EIP-2718)
|
|
|
|
switch (transaction.type) {
|
|
|
|
case 1:
|
|
|
|
return _serializeEip2930(transaction, signature);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return logger.throwError(`unsupported transaction type: ${transaction.type}`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "serializeTransaction",
|
|
|
|
transactionType: transaction.type
|
|
|
|
});
|
|
|
|
}
|
|
|
|
function _parseEip2930(payload) {
|
|
|
|
const transaction = RLP.decode(payload.slice(1));
|
|
|
|
if (transaction.length !== 8 && transaction.length !== 11) {
|
|
|
|
logger.throwArgumentError("invalid component count for transaction type: 1", "payload", hexlify(payload));
|
|
|
|
}
|
|
|
|
const 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],
|
|
|
|
accessList: accessListify(transaction[7]),
|
|
|
|
};
|
|
|
|
// Unsigned EIP-2930 Transaction
|
|
|
|
if (transaction.length === 8) {
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
const recid = handleNumber(transaction[8]).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", transaction[8]);
|
|
|
|
}
|
|
|
|
tx.r = hexZeroPad(transaction[9], 32);
|
|
|
|
tx.s = hexZeroPad(transaction[10], 32);
|
|
|
|
try {
|
|
|
|
const digest = keccak256(_serializeEip2930(tx));
|
|
|
|
tx.from = recoverAddress(digest, { r: tx.r, s: tx.s, recoveryParam: tx.v });
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
tx.hash = keccak256(payload);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
// Legacy Transactions and EIP-155
|
|
|
|
function _parse(rawTransaction) {
|
2019-11-20 12:57:38 +03:00
|
|
|
const transaction = RLP.decode(rawTransaction);
|
2019-08-25 09:39:20 +03:00
|
|
|
if (transaction.length !== 9 && transaction.length !== 6) {
|
2020-04-24 06:35:39 +03:00
|
|
|
logger.throwArgumentError("invalid raw transaction", "rawTransaction", rawTransaction);
|
2019-08-25 09:39:20 +03:00
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const tx = {
|
2019-08-25 09:39:20 +03:00
|
|
|
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.from(transaction[6]).toNumber();
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
return tx;
|
|
|
|
}
|
|
|
|
tx.r = hexZeroPad(transaction[7], 32);
|
|
|
|
tx.s = hexZeroPad(transaction[8], 32);
|
|
|
|
if (BigNumber.from(tx.r).isZero() && BigNumber.from(tx.s).isZero()) {
|
|
|
|
// EIP-155 unsigned transaction
|
|
|
|
tx.chainId = tx.v;
|
|
|
|
tx.v = 0;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// Signed Tranasaction
|
|
|
|
tx.chainId = Math.floor((tx.v - 35) / 2);
|
|
|
|
if (tx.chainId < 0) {
|
|
|
|
tx.chainId = 0;
|
|
|
|
}
|
|
|
|
let recoveryParam = tx.v - 27;
|
2019-11-20 12:57:38 +03:00
|
|
|
const raw = transaction.slice(0, 6);
|
2019-08-25 09:39:20 +03:00
|
|
|
if (tx.chainId !== 0) {
|
|
|
|
raw.push(hexlify(tx.chainId));
|
|
|
|
raw.push("0x");
|
|
|
|
raw.push("0x");
|
|
|
|
recoveryParam -= tx.chainId * 2 + 8;
|
|
|
|
}
|
2019-11-20 12:57:38 +03:00
|
|
|
const digest = keccak256(RLP.encode(raw));
|
2019-08-25 09:39:20 +03:00
|
|
|
try {
|
|
|
|
tx.from = recoverAddress(digest, { r: hexlify(tx.r), s: hexlify(tx.s), recoveryParam: recoveryParam });
|
|
|
|
}
|
|
|
|
catch (error) {
|
|
|
|
console.log(error);
|
|
|
|
}
|
|
|
|
tx.hash = keccak256(rawTransaction);
|
|
|
|
}
|
2021-03-30 22:22:45 +03:00
|
|
|
tx.type = null;
|
2019-08-25 09:39:20 +03:00
|
|
|
return tx;
|
|
|
|
}
|
2021-03-30 22:22:45 +03:00
|
|
|
export function parse(rawTransaction) {
|
|
|
|
const payload = arrayify(rawTransaction);
|
|
|
|
// Legacy and EIP-155 Transactions
|
|
|
|
if (payload[0] > 0x7f) {
|
|
|
|
return _parse(payload);
|
|
|
|
}
|
|
|
|
// Typed Transaction (EIP-2718)
|
|
|
|
switch (payload[0]) {
|
|
|
|
case 1:
|
|
|
|
return _parseEip2930(payload);
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return logger.throwError(`unsupported transaction type: ${payload[0]}`, Logger.errors.UNSUPPORTED_OPERATION, {
|
|
|
|
operation: "parseTransaction",
|
|
|
|
transactionType: payload[0]
|
|
|
|
});
|
|
|
|
}
|
2020-07-13 15:03:56 +03:00
|
|
|
//# sourceMappingURL=index.js.map
|