From 243beffa4f83c910f5f1c5e0554531e5dcf3ab93 Mon Sep 17 00:00:00 2001 From: Richard Moore Date: Fri, 12 Feb 2021 18:25:25 -0500 Subject: [PATCH] Prevent unconfigured ENS names from making an init tx (#1290). --- packages/abstract-signer/src.ts/index.ts | 11 ++++++++- packages/tests/src.ts/test-providers.ts | 31 ++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) diff --git a/packages/abstract-signer/src.ts/index.ts b/packages/abstract-signer/src.ts/index.ts index 6f018d5521..9a77e64cf9 100644 --- a/packages/abstract-signer/src.ts/index.ts +++ b/packages/abstract-signer/src.ts/index.ts @@ -191,7 +191,16 @@ export abstract class Signer { const tx: Deferrable = await resolveProperties(this.checkTransaction(transaction)) - if (tx.to != null) { tx.to = Promise.resolve(tx.to).then((to) => this.resolveName(to)); } + if (tx.to != null) { + tx.to = Promise.resolve(tx.to).then(async (to) => { + if (to == null) { return null; } + const address = await this.resolveName(to); + if (address == null) { + logger.throwArgumentError("provided ENS name resolves to null", "tx.to", to); + } + return address; + }); + } if (tx.gasPrice == null) { tx.gasPrice = this.getGasPrice(); } if (tx.nonce == null) { tx.nonce = this.getTransactionCount("pending"); } diff --git a/packages/tests/src.ts/test-providers.ts b/packages/tests/src.ts/test-providers.ts index a40066fc74..09bd8c84d2 100644 --- a/packages/tests/src.ts/test-providers.ts +++ b/packages/tests/src.ts/test-providers.ts @@ -1113,3 +1113,34 @@ describe("Test Events", function() { await testBlockEvent(provider); }); }); + +describe("Bad ENS resolution", function() { + const provider = providerFunctions[0].create("ropsten"); + + it("signer has a bad ENS name", async function() { + this.timeout(300000); + + const wallet = new ethers.Wallet(ethers.utils.id("random-wallet"), provider); + + // If "to" is specified as an ENS name, it cannot resolve to null + try { + const tx = await wallet.sendTransaction({ to: "junk", value: 1 }); + console.log("TX", tx); + } catch (error) { + assert.ok(error.argument === "tx.to" && error.value === "junk"); + } + + // But promises that resolve to null are ok + const tos = [ null, Promise.resolve(null) ]; + for (let i = 0; i < tos.length; i++) { + const to = tos[i]; + try { + const tx = await wallet.sendTransaction({ to, value: 1 }); + console.log("TX", tx); + } catch (error) { + assert.ok(error.code === "INSUFFICIENT_FUNDS"); + } + } + }); + +});