Skip to content

Commit

Permalink
refactor(resolveName): don't replace name hash with address in verify
Browse files Browse the repository at this point in the history
  • Loading branch information
davidyuk committed Jun 24, 2021
1 parent 30926ab commit b8611fc
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
19 changes: 9 additions & 10 deletions src/chain/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,31 +221,30 @@ async function getName (name) {
* @param {String} nameOrId
* @param {String} prefix
* @param {Object} [options]
* @param {Boolean} [options.verify] Enables resolving by node, needed for compatibility with `verify` option of other methods
* @param {Boolean} [options.resolveByNode] Enables pointer resolving using node (isn't more durable to resolve it on the node side?)
* @param {Boolean} [options.verify] To ensure that name exist and have a corresponding pointer
* @param {Boolean} [options.resolveByNode] Enables pointer resolving using node // TODO: avoid that to don't trust to current api gateway
* @return {String} Address or AENS name hash
*/
async function resolveName (nameOrId, prefix, { verify, resolveByNode } = {}) {
if (!nameOrId || typeof nameOrId !== 'string') {
throw new Error('Invalid name or address. Should be a string')
throw new Error(`Name or address should be a string: ${nameOrId}`)
}
const prefixes = Object.keys(NAME_ID_KEY)
if (!prefixes.includes(prefix)) {
throw new Error(`Invalid prefix ${prefix}. Should be one of [${prefixes}]`)
throw new Error(`Invalid prefix ${prefix}, should be one of [${prefixes}]`)
}
if (assertedType(nameOrId, prefix, true)) return nameOrId

if (isNameValid(nameOrId)) {
if (resolveByNode || verify) {
if (verify || resolveByNode) {
const name = await this.getName(nameOrId).catch(_ => null)
if (!name) throw new Error('Name not found')
if (!name) throw new Error(`Name not found: ${nameOrId}`)
const pointer = name.pointers.find(({ id }) => id.split('_')[0] === prefix)
if (!pointer) throw new Error(`Name ${nameOrId} do not have pointers for ${prefix}`)
return pointer.id
if (!pointer) throw new Error(`Name ${nameOrId} don't have pointers for ${prefix}`)
if (resolveByNode) return pointer.id
}
return produceNameId(nameOrId)
}
throw new Error('Invalid name or address')
throw new Error(`Invalid name or address: ${nameOrId}`)
}

/**
Expand Down
1 change: 0 additions & 1 deletion src/contract/aci/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ export default async function getContractInstance (source, { aci, contractAddres
skipTransformDecoded: false,
callStatic: false,
waitMined: true,
verify: false,
filesystem
}
const instance = {
Expand Down
2 changes: 1 addition & 1 deletion test/integration/aens.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ describe('Aens', function () {
try {
await aens.spend(100, name, { onAccount, verify: true })
} catch (e) {
e.message.should.be.equal(`Name ${name} do not have pointers for ak`)
e.message.should.be.equal(`Name ${name} don't have pointers for ak`)
}
})
it('Call contract using AENS name', async () => {
Expand Down

0 comments on commit b8611fc

Please sign in to comment.