contract-address.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.getCreate2Address = exports.getCreateAddress = void 0;
  4. const index_js_1 = require("../crypto/index.js");
  5. const index_js_2 = require("../utils/index.js");
  6. const address_js_1 = require("./address.js");
  7. // http://ethereum.stackexchange.com/questions/760/how-is-the-address-of-an-ethereum-contract-computed
  8. /**
  9. * Returns the address that would result from a ``CREATE`` for %%tx%%.
  10. *
  11. * This can be used to compute the address a contract will be
  12. * deployed to by an EOA when sending a deployment transaction (i.e.
  13. * when the ``to`` address is ``null``).
  14. *
  15. * This can also be used to compute the address a contract will be
  16. * deployed to by a contract, by using the contract's address as the
  17. * ``to`` and the contract's nonce.
  18. *
  19. * @example
  20. * from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72";
  21. * nonce = 5;
  22. *
  23. * getCreateAddress({ from, nonce });
  24. * //_result:
  25. */
  26. function getCreateAddress(tx) {
  27. const from = (0, address_js_1.getAddress)(tx.from);
  28. const nonce = (0, index_js_2.getBigInt)(tx.nonce, "tx.nonce");
  29. let nonceHex = nonce.toString(16);
  30. if (nonceHex === "0") {
  31. nonceHex = "0x";
  32. }
  33. else if (nonceHex.length % 2) {
  34. nonceHex = "0x0" + nonceHex;
  35. }
  36. else {
  37. nonceHex = "0x" + nonceHex;
  38. }
  39. return (0, address_js_1.getAddress)((0, index_js_2.dataSlice)((0, index_js_1.keccak256)((0, index_js_2.encodeRlp)([from, nonceHex])), 12));
  40. }
  41. exports.getCreateAddress = getCreateAddress;
  42. /**
  43. * Returns the address that would result from a ``CREATE2`` operation
  44. * with the given %%from%%, %%salt%% and %%initCodeHash%%.
  45. *
  46. * To compute the %%initCodeHash%% from a contract's init code, use
  47. * the [[keccak256]] function.
  48. *
  49. * For a quick overview and example of ``CREATE2``, see [[link-ricmoo-wisps]].
  50. *
  51. * @example
  52. * // The address of the contract
  53. * from = "0x8ba1f109551bD432803012645Ac136ddd64DBA72"
  54. *
  55. * // The salt
  56. * salt = id("HelloWorld")
  57. *
  58. * // The hash of the initCode
  59. * initCode = "0x6394198df16000526103ff60206004601c335afa6040516060f3";
  60. * initCodeHash = keccak256(initCode)
  61. *
  62. * getCreate2Address(from, salt, initCodeHash)
  63. * //_result:
  64. */
  65. function getCreate2Address(_from, _salt, _initCodeHash) {
  66. const from = (0, address_js_1.getAddress)(_from);
  67. const salt = (0, index_js_2.getBytes)(_salt, "salt");
  68. const initCodeHash = (0, index_js_2.getBytes)(_initCodeHash, "initCodeHash");
  69. (0, index_js_2.assertArgument)(salt.length === 32, "salt must be 32 bytes", "salt", _salt);
  70. (0, index_js_2.assertArgument)(initCodeHash.length === 32, "initCodeHash must be 32 bytes", "initCodeHash", _initCodeHash);
  71. return (0, address_js_1.getAddress)((0, index_js_2.dataSlice)((0, index_js_1.keccak256)((0, index_js_2.concat)(["0xff", from, salt, initCodeHash])), 12));
  72. }
  73. exports.getCreate2Address = getCreate2Address;
  74. //# sourceMappingURL=contract-address.js.map