provider-browser.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.BrowserProvider = void 0;
  4. const index_js_1 = require("../utils/index.js");
  5. const provider_jsonrpc_js_1 = require("./provider-jsonrpc.js");
  6. ;
  7. /**
  8. * A **BrowserProvider** is intended to wrap an injected provider which
  9. * adheres to the [[link-eip-1193]] standard, which most (if not all)
  10. * currently do.
  11. */
  12. class BrowserProvider extends provider_jsonrpc_js_1.JsonRpcApiPollingProvider {
  13. #request;
  14. /**
  15. * Connect to the %%ethereum%% provider, optionally forcing the
  16. * %%network%%.
  17. */
  18. constructor(ethereum, network, _options) {
  19. // Copy the options
  20. const options = Object.assign({}, ((_options != null) ? _options : {}), { batchMaxCount: 1 });
  21. (0, index_js_1.assertArgument)(ethereum && ethereum.request, "invalid EIP-1193 provider", "ethereum", ethereum);
  22. super(network, options);
  23. this.#request = async (method, params) => {
  24. const payload = { method, params };
  25. this.emit("debug", { action: "sendEip1193Request", payload });
  26. try {
  27. const result = await ethereum.request(payload);
  28. this.emit("debug", { action: "receiveEip1193Result", result });
  29. return result;
  30. }
  31. catch (e) {
  32. const error = new Error(e.message);
  33. error.code = e.code;
  34. error.data = e.data;
  35. error.payload = payload;
  36. this.emit("debug", { action: "receiveEip1193Error", error });
  37. throw error;
  38. }
  39. };
  40. }
  41. async send(method, params) {
  42. await this._start();
  43. return await super.send(method, params);
  44. }
  45. async _send(payload) {
  46. (0, index_js_1.assertArgument)(!Array.isArray(payload), "EIP-1193 does not support batch request", "payload", payload);
  47. try {
  48. const result = await this.#request(payload.method, payload.params || []);
  49. return [{ id: payload.id, result }];
  50. }
  51. catch (e) {
  52. return [{
  53. id: payload.id,
  54. error: { code: e.code, data: e.data, message: e.message }
  55. }];
  56. }
  57. }
  58. getRpcError(payload, error) {
  59. error = JSON.parse(JSON.stringify(error));
  60. // EIP-1193 gives us some machine-readable error codes, so rewrite
  61. // them into
  62. switch (error.error.code || -1) {
  63. case 4001:
  64. error.error.message = `ethers-user-denied: ${error.error.message}`;
  65. break;
  66. case 4200:
  67. error.error.message = `ethers-unsupported: ${error.error.message}`;
  68. break;
  69. }
  70. return super.getRpcError(payload, error);
  71. }
  72. /**
  73. * Resolves to ``true`` if the provider manages the %%address%%.
  74. */
  75. async hasSigner(address) {
  76. if (address == null) {
  77. address = 0;
  78. }
  79. const accounts = await this.send("eth_accounts", []);
  80. if (typeof (address) === "number") {
  81. return (accounts.length > address);
  82. }
  83. address = address.toLowerCase();
  84. return accounts.filter((a) => (a.toLowerCase() === address)).length !== 0;
  85. }
  86. async getSigner(address) {
  87. if (address == null) {
  88. address = 0;
  89. }
  90. if (!(await this.hasSigner(address))) {
  91. try {
  92. //const resp =
  93. await this.#request("eth_requestAccounts", []);
  94. //console.log("RESP", resp);
  95. }
  96. catch (error) {
  97. const payload = error.payload;
  98. throw this.getRpcError(payload, { id: payload.id, error });
  99. }
  100. }
  101. return await super.getSigner(address);
  102. }
  103. }
  104. exports.BrowserProvider = BrowserProvider;
  105. //# sourceMappingURL=provider-browser.js.map