format.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. /**
  2. * @_ignore
  3. */
  4. import { getAddress, getCreateAddress } from "../address/index.js";
  5. import { Signature } from "../crypto/index.js";
  6. import { accessListify } from "../transaction/index.js";
  7. import { getBigInt, getNumber, hexlify, isHexString, zeroPadValue, assert, assertArgument } from "../utils/index.js";
  8. const BN_0 = BigInt(0);
  9. export function allowNull(format, nullValue) {
  10. return (function (value) {
  11. if (value == null) {
  12. return nullValue;
  13. }
  14. return format(value);
  15. });
  16. }
  17. export function arrayOf(format, allowNull) {
  18. return ((array) => {
  19. if (allowNull && array == null) {
  20. return null;
  21. }
  22. if (!Array.isArray(array)) {
  23. throw new Error("not an array");
  24. }
  25. return array.map((i) => format(i));
  26. });
  27. }
  28. // Requires an object which matches a fleet of other formatters
  29. // Any FormatFunc may return `undefined` to have the value omitted
  30. // from the result object. Calls preserve `this`.
  31. export function object(format, altNames) {
  32. return ((value) => {
  33. const result = {};
  34. for (const key in format) {
  35. let srcKey = key;
  36. if (altNames && key in altNames && !(srcKey in value)) {
  37. for (const altKey of altNames[key]) {
  38. if (altKey in value) {
  39. srcKey = altKey;
  40. break;
  41. }
  42. }
  43. }
  44. try {
  45. const nv = format[key](value[srcKey]);
  46. if (nv !== undefined) {
  47. result[key] = nv;
  48. }
  49. }
  50. catch (error) {
  51. const message = (error instanceof Error) ? error.message : "not-an-error";
  52. assert(false, `invalid value for value.${key} (${message})`, "BAD_DATA", { value });
  53. }
  54. }
  55. return result;
  56. });
  57. }
  58. export function formatBoolean(value) {
  59. switch (value) {
  60. case true:
  61. case "true":
  62. return true;
  63. case false:
  64. case "false":
  65. return false;
  66. }
  67. assertArgument(false, `invalid boolean; ${JSON.stringify(value)}`, "value", value);
  68. }
  69. export function formatData(value) {
  70. assertArgument(isHexString(value, true), "invalid data", "value", value);
  71. return value;
  72. }
  73. export function formatHash(value) {
  74. assertArgument(isHexString(value, 32), "invalid hash", "value", value);
  75. return value;
  76. }
  77. export function formatUint256(value) {
  78. if (!isHexString(value)) {
  79. throw new Error("invalid uint256");
  80. }
  81. return zeroPadValue(value, 32);
  82. }
  83. const _formatLog = object({
  84. address: getAddress,
  85. blockHash: formatHash,
  86. blockNumber: getNumber,
  87. data: formatData,
  88. index: getNumber,
  89. removed: allowNull(formatBoolean, false),
  90. topics: arrayOf(formatHash),
  91. transactionHash: formatHash,
  92. transactionIndex: getNumber,
  93. }, {
  94. index: ["logIndex"]
  95. });
  96. export function formatLog(value) {
  97. return _formatLog(value);
  98. }
  99. const _formatBlock = object({
  100. hash: allowNull(formatHash),
  101. parentHash: formatHash,
  102. parentBeaconBlockRoot: allowNull(formatHash, null),
  103. number: getNumber,
  104. timestamp: getNumber,
  105. nonce: allowNull(formatData),
  106. difficulty: getBigInt,
  107. gasLimit: getBigInt,
  108. gasUsed: getBigInt,
  109. stateRoot: allowNull(formatHash, null),
  110. receiptsRoot: allowNull(formatHash, null),
  111. blobGasUsed: allowNull(getBigInt, null),
  112. excessBlobGas: allowNull(getBigInt, null),
  113. miner: allowNull(getAddress),
  114. prevRandao: allowNull(formatHash, null),
  115. extraData: formatData,
  116. baseFeePerGas: allowNull(getBigInt)
  117. }, {
  118. prevRandao: ["mixHash"]
  119. });
  120. export function formatBlock(value) {
  121. const result = _formatBlock(value);
  122. result.transactions = value.transactions.map((tx) => {
  123. if (typeof (tx) === "string") {
  124. return tx;
  125. }
  126. return formatTransactionResponse(tx);
  127. });
  128. return result;
  129. }
  130. const _formatReceiptLog = object({
  131. transactionIndex: getNumber,
  132. blockNumber: getNumber,
  133. transactionHash: formatHash,
  134. address: getAddress,
  135. topics: arrayOf(formatHash),
  136. data: formatData,
  137. index: getNumber,
  138. blockHash: formatHash,
  139. }, {
  140. index: ["logIndex"]
  141. });
  142. export function formatReceiptLog(value) {
  143. return _formatReceiptLog(value);
  144. }
  145. const _formatTransactionReceipt = object({
  146. to: allowNull(getAddress, null),
  147. from: allowNull(getAddress, null),
  148. contractAddress: allowNull(getAddress, null),
  149. // should be allowNull(hash), but broken-EIP-658 support is handled in receipt
  150. index: getNumber,
  151. root: allowNull(hexlify),
  152. gasUsed: getBigInt,
  153. blobGasUsed: allowNull(getBigInt, null),
  154. logsBloom: allowNull(formatData),
  155. blockHash: formatHash,
  156. hash: formatHash,
  157. logs: arrayOf(formatReceiptLog),
  158. blockNumber: getNumber,
  159. //confirmations: allowNull(getNumber, null),
  160. cumulativeGasUsed: getBigInt,
  161. effectiveGasPrice: allowNull(getBigInt),
  162. blobGasPrice: allowNull(getBigInt, null),
  163. status: allowNull(getNumber),
  164. type: allowNull(getNumber, 0)
  165. }, {
  166. effectiveGasPrice: ["gasPrice"],
  167. hash: ["transactionHash"],
  168. index: ["transactionIndex"],
  169. });
  170. export function formatTransactionReceipt(value) {
  171. return _formatTransactionReceipt(value);
  172. }
  173. export function formatTransactionResponse(value) {
  174. // Some clients (TestRPC) do strange things like return 0x0 for the
  175. // 0 address; correct this to be a real address
  176. if (value.to && getBigInt(value.to) === BN_0) {
  177. value.to = "0x0000000000000000000000000000000000000000";
  178. }
  179. const result = object({
  180. hash: formatHash,
  181. // Some nodes do not return this, usually test nodes (like Ganache)
  182. index: allowNull(getNumber, undefined),
  183. type: (value) => {
  184. if (value === "0x" || value == null) {
  185. return 0;
  186. }
  187. return getNumber(value);
  188. },
  189. accessList: allowNull(accessListify, null),
  190. blobVersionedHashes: allowNull(arrayOf(formatHash, true), null),
  191. blockHash: allowNull(formatHash, null),
  192. blockNumber: allowNull(getNumber, null),
  193. transactionIndex: allowNull(getNumber, null),
  194. from: getAddress,
  195. // either (gasPrice) or (maxPriorityFeePerGas + maxFeePerGas) must be set
  196. gasPrice: allowNull(getBigInt),
  197. maxPriorityFeePerGas: allowNull(getBigInt),
  198. maxFeePerGas: allowNull(getBigInt),
  199. maxFeePerBlobGas: allowNull(getBigInt, null),
  200. gasLimit: getBigInt,
  201. to: allowNull(getAddress, null),
  202. value: getBigInt,
  203. nonce: getNumber,
  204. data: formatData,
  205. creates: allowNull(getAddress, null),
  206. chainId: allowNull(getBigInt, null)
  207. }, {
  208. data: ["input"],
  209. gasLimit: ["gas"],
  210. index: ["transactionIndex"]
  211. })(value);
  212. // If to and creates are empty, populate the creates from the value
  213. if (result.to == null && result.creates == null) {
  214. result.creates = getCreateAddress(result);
  215. }
  216. // @TODO: Check fee data
  217. // Add an access list to supported transaction types
  218. if ((value.type === 1 || value.type === 2) && value.accessList == null) {
  219. result.accessList = [];
  220. }
  221. // Compute the signature
  222. if (value.signature) {
  223. result.signature = Signature.from(value.signature);
  224. }
  225. else {
  226. result.signature = Signature.from(value);
  227. }
  228. // Some backends omit ChainId on legacy transactions, but we can compute it
  229. if (result.chainId == null) {
  230. const chainId = result.signature.legacyChainId;
  231. if (chainId != null) {
  232. result.chainId = chainId;
  233. }
  234. }
  235. // @TODO: check chainID
  236. /*
  237. if (value.chainId != null) {
  238. let chainId = value.chainId;
  239. if (isHexString(chainId)) {
  240. chainId = BigNumber.from(chainId).toNumber();
  241. }
  242. result.chainId = chainId;
  243. } else {
  244. let chainId = value.networkId;
  245. // geth-etc returns chainId
  246. if (chainId == null && result.v == null) {
  247. chainId = value.chainId;
  248. }
  249. if (isHexString(chainId)) {
  250. chainId = BigNumber.from(chainId).toNumber();
  251. }
  252. if (typeof(chainId) !== "number" && result.v != null) {
  253. chainId = (result.v - 35) / 2;
  254. if (chainId < 0) { chainId = 0; }
  255. chainId = parseInt(chainId);
  256. }
  257. if (typeof(chainId) !== "number") { chainId = 0; }
  258. result.chainId = chainId;
  259. }
  260. */
  261. // 0x0000... should actually be null
  262. if (result.blockHash && getBigInt(result.blockHash) === BN_0) {
  263. result.blockHash = null;
  264. }
  265. return result;
  266. }
  267. //# sourceMappingURL=format.js.map