1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-23 03:36:18 +02:00
metamask-extension/app/scripts/lib/ens-ipfs/resolver.js
Mark Stacey 14d85b1332
Make JSDoc formatting more consistent (#9796)
A few inconsistencies in JSDoc formatting have been fixed throughout
the project. Many issues remain; these were just the few things that
were easy to fix with a regular expression.

The changes include:

* Using lower-case for primitive types, but capitalizing non-primitive
 types
* Separating the parameter identifier and the description with a dash
* Omitting a dash between the return type and the return description
* Ensuring the parameter type is first and the identifier is second (in
 a few places it was backwards)
* Using square brackets to denote when a parameter is optional, rather
 than putting "(optional)" in the parameter description
* Including a type and identifier with every parameter
* Fixing inconsistent spacing, except where it's used for alignment
* Remove incorrectly formatted `@deprecated` tags that reference non-
 existent properties
* Remove lone comment block without accompanying function

Additionally, one parameter was renamed for clarity.
2020-11-10 14:00:41 -03:30

88 lines
2.8 KiB
JavaScript

import namehash from 'eth-ens-namehash'
import Eth from 'ethjs-query'
import EthContract from 'ethjs-contract'
import contentHash from 'content-hash'
import registryAbi from './contracts/registry'
import resolverAbi from './contracts/resolver'
export default async function resolveEnsToIpfsContentId({ provider, name }) {
const eth = new Eth(provider)
const hash = namehash.hash(name)
const contract = new EthContract(eth)
// lookup registry
const chainId = Number.parseInt(await eth.net_version(), 10)
const registryAddress = getRegistryForChainId(chainId)
if (!registryAddress) {
throw new Error(
`EnsIpfsResolver - no known ens-ipfs registry for chainId "${chainId}"`,
)
}
const Registry = contract(registryAbi).at(registryAddress)
// lookup resolver
const resolverLookupResult = await Registry.resolver(hash)
const resolverAddress = resolverLookupResult[0]
if (hexValueIsEmpty(resolverAddress)) {
throw new Error(`EnsIpfsResolver - no resolver found for name "${name}"`)
}
const Resolver = contract(resolverAbi).at(resolverAddress)
const isEIP1577Compliant = await Resolver.supportsInterface('0xbc1c58d1')
const isLegacyResolver = await Resolver.supportsInterface('0xd8389dc5')
if (isEIP1577Compliant[0]) {
const contentLookupResult = await Resolver.contenthash(hash)
const rawContentHash = contentLookupResult[0]
let decodedContentHash = contentHash.decode(rawContentHash)
const type = contentHash.getCodec(rawContentHash)
if (type === 'ipfs-ns' || type === 'ipns-ns') {
decodedContentHash = contentHash.helpers.cidV0ToV1Base32(
decodedContentHash,
)
}
return { type, hash: decodedContentHash }
}
if (isLegacyResolver[0]) {
// lookup content id
const contentLookupResult = await Resolver.content(hash)
const content = contentLookupResult[0]
if (hexValueIsEmpty(content)) {
throw new Error(
`EnsIpfsResolver - no content ID found for name "${name}"`,
)
}
return { type: 'swarm-ns', hash: content.slice(2) }
}
throw new Error(
`EnsIpfsResolver - the resolver for name "${name}" is not standard, it should either supports contenthash() or content()`,
)
}
function hexValueIsEmpty(value) {
return [
undefined,
null,
'0x',
'0x0',
'0x0000000000000000000000000000000000000000000000000000000000000000',
].includes(value)
}
/**
* Returns the registry address for the given chain ID
* @param {number} chainId - the chain ID
* @returns {string|null} the registry address if known, null otherwise
*/
function getRegistryForChainId(chainId) {
switch (chainId) {
case 1:
case 3:
case 4:
case 5:
// Mainnet, Ropsten, Rinkeby, and Goerli, respectively, use the same address
return '0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e'
default:
return null
}
}