This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
769 additions
and
139 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
const ENSJS = require("ethereum-ens"); | ||
|
||
module.exports = { | ||
convertENSNames: async function({ | ||
ensSettings, | ||
inputArgs, | ||
methodABI, | ||
inputParams, | ||
web3 | ||
}) { | ||
const { registryAddress } = ensSettings; | ||
let args; | ||
if (inputArgs.length && methodABI) { | ||
args = await this.convertENSArgsNames( | ||
inputArgs, | ||
methodABI, | ||
web3, | ||
registryAddress | ||
); | ||
} else { | ||
args = inputArgs; | ||
} | ||
const params = await this.convertENSParamsNames( | ||
inputParams, | ||
web3, | ||
registryAddress | ||
); | ||
return { args, params }; | ||
}, | ||
|
||
getNewENSJS: function({ provider, registryAddress }) { | ||
return new ENSJS(provider, registryAddress); | ||
}, | ||
|
||
resolveNameToAddress: function(name, ensjs) { | ||
return ensjs.resolver(name).addr(); | ||
}, | ||
|
||
convertENSArgsNames: function(inputArgs, methodABI, web3, registryAddress) { | ||
if (methodABI.inputs.length === 0) return inputArgs; | ||
const { isAddress } = web3.utils; | ||
const ensjs = this.getNewENSJS({ | ||
provider: web3.currentProvider, | ||
registryAddress | ||
}); | ||
|
||
const convertedNames = inputArgs.map((argument, index) => { | ||
if (index + 1 > methodABI.inputs.length) { | ||
return argument; | ||
} else if (methodABI.inputs[index].type === "address") { | ||
// Check all address arguments for ENS names | ||
const argIsAddress = isAddress(argument); | ||
if (argIsAddress) return argument; | ||
return this.resolveNameToAddress(argument, ensjs); | ||
} else { | ||
return argument; | ||
} | ||
}); | ||
return Promise.all(convertedNames); | ||
}, | ||
|
||
convertENSParamsNames: async function(params, web3, registryAddress) { | ||
const { isAddress } = web3.utils; | ||
if (params.from && !isAddress(params.from)) { | ||
const ensjs = this.getNewENSJS({ | ||
provider: web3.currentProvider, | ||
registryAddress | ||
}); | ||
const newFrom = await this.resolveNameToAddress(params.from, ensjs); | ||
return Object.assign({}, params, { from: newFrom }); | ||
} else { | ||
return params; | ||
} | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
const ens = require("../../../lib/utils/ens"); | ||
const assert = require("assert"); | ||
const sinon = require("sinon"); | ||
const Web3 = require("web3"); | ||
const methodABI = { | ||
constant: true, | ||
inputs: [ | ||
{ | ||
name: "firstInput", | ||
type: "address" | ||
}, | ||
{ | ||
name: "secondInput", | ||
type: "uint256" | ||
} | ||
], | ||
name: "myMethod", | ||
outputs: [ | ||
{ | ||
name: "", | ||
type: "bool" | ||
} | ||
], | ||
payable: false, | ||
stateMutability: "pure", | ||
type: "function" | ||
}; | ||
const args = ["my.ens.name", 555, { from: "the.other.name" }]; | ||
|
||
describe("convertENSNames", () => { | ||
beforeEach(() => { | ||
const expectedInput1 = "my.ens.name"; | ||
const expectedInput2 = "the.other.name"; | ||
sinon.stub(ens, "getNewENSJS").returns({ | ||
resolver: input => { | ||
if (input === expectedInput1) { | ||
return { addr: () => Promise.resolve("0x123") }; | ||
} | ||
if (input === expectedInput2) { | ||
return { addr: () => Promise.resolve("0x987") }; | ||
} | ||
return Promise.reject("The input was not what was expected"); | ||
} | ||
}); | ||
}); | ||
afterEach(() => { | ||
ens.getNewENSJS.restore(); | ||
}); | ||
|
||
it("converts ens names in address fields to addresses", async () => { | ||
let result = await ens.convertENSNames(args, methodABI, Web3); | ||
assert(result[0] === "0x123"); | ||
}); | ||
it("does not change non-address arguments", async () => { | ||
let result = await ens.convertENSNames(args, methodABI, Web3); | ||
assert(result[1] === 555); | ||
}); | ||
it("converts ens names in the from field of the tx object", async () => { | ||
let result = await ens.convertENSNames(args, methodABI, Web3); | ||
assert(result[2].from === "0x987"); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
|
||
declare module "abi-decoder" { | ||
function addABI(abi: any): void; | ||
function decodeMethod(data: any): any; | ||
function decodeLogs(logs: any[]): any; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.