Skip to content

Commit

Permalink
Merge pull request tahowallet#1304 from tallycash/resolver-revolver
Browse files Browse the repository at this point in the history
Resolver Revolver: Introduce NameResolver and refactor NameService to use it
  • Loading branch information
0xDaedalus authored Apr 11, 2022
2 parents 36cf5d9 + f86594e commit 05537c5
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 161 deletions.
22 changes: 13 additions & 9 deletions background/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,15 +756,20 @@ export default class Main extends BaseService<never> {
async connectNameService(): Promise<void> {
this.nameService.emitter.on(
"resolvedName",
async ({ from: { addressNetwork }, resolved: { name } }) => {
this.store.dispatch(updateENSName({ ...addressNetwork, name }))
async ({
from: { addressOnNetwork },
resolved: {
nameOnNetwork: { name },
},
}) => {
this.store.dispatch(updateENSName({ ...addressOnNetwork, name }))
}
)
this.nameService.emitter.on(
"resolvedAvatar",
async ({ from: { addressNetwork }, resolved: { avatar } }) => {
async ({ from: { addressOnNetwork }, resolved: { avatar } }) => {
this.store.dispatch(
updateENSAvatar({ ...addressNetwork, avatar: avatar.toString() })
updateENSAvatar({ ...addressOnNetwork, avatar: avatar.toString() })
)
}
)
Expand Down Expand Up @@ -1158,12 +1163,11 @@ export default class Main extends BaseService<never> {
this.telemetryService.connectReduxStore(this.store)
}

async resolveNameOnNetwork({
name,
network,
}: NameOnNetwork): Promise<string | undefined> {
async resolveNameOnNetwork(
nameOnNetwork: NameOnNetwork
): Promise<AddressOnNetwork | undefined> {
try {
return await this.nameService.lookUpEthereumAddress(name /* , network */)
return await this.nameService.lookUpEthereumAddress(nameOnNetwork)
} catch (error) {
logger.info("Error looking up Ethereum address: ", error)
return undefined
Expand Down
33 changes: 19 additions & 14 deletions background/services/enrichment/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ export default class EnrichmentService extends BaseService<Events> {
transaction.input === "0x" ||
typeof transaction.input === "undefined"
) {
const toName = await this.nameService.lookUpName(transaction.to, ETHEREUM)
const { name: toName } = (await this.nameService.lookUpName({
address: transaction.to,
network,
})) ?? { name: undefined }

// This is _almost certainly_ not a contract interaction, move on. Note that
// a simple ETH send to a contract address can still effectively be a
Expand Down Expand Up @@ -174,10 +177,10 @@ export default class EnrichmentService extends BaseService<Events> {
erc20Tx &&
(erc20Tx.name === "transfer" || erc20Tx.name === "transferFrom")
) {
const toName = await this.nameService.lookUpName(
erc20Tx.args.to,
ETHEREUM
)
const { name: toName } = (await this.nameService.lookUpName({
address: erc20Tx.args.to,
network,
})) ?? { name: undefined }

// We have an ERC-20 transfer
txAnnotation = {
Expand All @@ -200,10 +203,10 @@ export default class EnrichmentService extends BaseService<Events> {
erc20Tx &&
erc20Tx.name === "approve"
) {
const spenderName = await this.nameService.lookUpName(
erc20Tx.args.spender,
ETHEREUM
)
const { name: spenderName } = (await this.nameService.lookUpName({
address: erc20Tx.args.spender,
network,
})) ?? { name: undefined }

txAnnotation = {
timestamp: resolvedTime,
Expand All @@ -220,10 +223,10 @@ export default class EnrichmentService extends BaseService<Events> {
),
}
} else {
const toName = await this.nameService.lookUpName(
transaction.to,
ETHEREUM
)
const { name: toName } = (await this.nameService.lookUpName({
address: transaction.to,
network,
})) ?? { name: undefined }

// Fall back on a standard contract interaction.
txAnnotation = {
Expand Down Expand Up @@ -258,7 +261,9 @@ export default class EnrichmentService extends BaseService<Events> {
async (address) =>
[
normalizeEVMAddress(address),
await this.nameService.lookUpName(address, ETHEREUM),
(
await this.nameService.lookUpName({ address, network })
)?.name,
] as const
)
)
Expand Down
17 changes: 13 additions & 4 deletions background/services/enrichment/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export async function enrichEIP2612SignTypedDataRequest(
): Promise<EIP2612SignTypedDataAnnotation> {
const { message, domain } = typedData
const { value, owner, spender, nonce } = message

// If we have a corresponding asset - use known decimals to display a human-friendly
// amount e.g. 10 USDC. Otherwise just display the value e.g. 10000000
const formattedValue = asset
Expand All @@ -48,10 +49,18 @@ export async function enrichEIP2612SignTypedDataRequest(
// We only need to add the token if we're not able to properly format the value above
const token = formattedValue === `${value}` ? domain.name : null

const [ownerName, spenderName] = await Promise.all([
await nameService.lookUpName(owner, ETHEREUM, false),
await nameService.lookUpName(spender, ETHEREUM, false),
])
const [ownerName, spenderName] = (
await Promise.all([
await nameService.lookUpName(
{ address: owner, network: ETHEREUM },
false
),
await nameService.lookUpName(
{ address: spender, network: ETHEREUM },
false
),
])
).map((nameOnNetwork) => nameOnNetwork?.name)

return {
type: "EIP-2612",
Expand Down
Loading

0 comments on commit 05537c5

Please sign in to comment.