Skip to content

Commit

Permalink
refactor!: drop ensureNameValid
Browse files Browse the repository at this point in the history
the TypeScript check would be used later instead

BREAKING CHANGE: removed `ensureNameValid`
Use a TypeScript check instead.
  • Loading branch information
davidyuk committed May 26, 2022
1 parent 2346dcf commit d0d1258
Show file tree
Hide file tree
Showing 7 changed files with 6 additions and 60 deletions.
1 change: 0 additions & 1 deletion docs/guides/error-handling.md
Expand Up @@ -11,7 +11,6 @@ BaseError
│ ArgumentCountMismatchError
│ InsufficientBalanceError
│ InvalidDenominationError
│ InvalidNameError
│ MissingParamError
│ NoSerializerFoundError
│ RequestTimedOutError
Expand Down
8 changes: 1 addition & 7 deletions src/ae/aens.js
Expand Up @@ -27,7 +27,7 @@
*/

import { salt } from '../utils/crypto'
import { commitmentHash, ensureNameValid, isAuctionName } from '../tx/builder/helpers'
import { commitmentHash, isAuctionName } from '../tx/builder/helpers'
import Ae from './'
import { CLIENT_TTL, NAME_TTL, TX_TYPE } from '../tx/builder/schema'
import { ArgumentError } from '../utils/errors'
Expand Down Expand Up @@ -55,7 +55,6 @@ import { ArgumentError } from '../utils/errors'
* await nameObject.revoke({ fee, ttl, nonce })
*/
async function revoke (name, options = {}) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }

const nameRevokeTx = await this.buildTx(TX_TYPE.nameRevoke, {
Expand Down Expand Up @@ -99,7 +98,6 @@ async function revoke (name, options = {}) {
* await nameObject.update(pointers, { nameTtl, ttl, fee, nonce, clientTtl })
*/
async function update (name, pointers = {}, options = {}) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }
const allPointers = {
...options.extendPointers && Object.fromEntries(
Expand Down Expand Up @@ -143,7 +141,6 @@ async function update (name, pointers = {}, options = {}) {
* await nameObject.transfer(recipientPub, { ttl, fee, nonce })
*/
async function transfer (name, account, options = {}) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }

const nameTransferTx = await this.buildTx(TX_TYPE.nameTransfer, {
Expand Down Expand Up @@ -179,7 +176,6 @@ async function transfer (name, account, options = {}) {
* }
*/
async function query (name, opt = {}) {
ensureNameValid(name)
const o = await this.getName(name)

return Object.freeze(Object.assign(o, {
Expand Down Expand Up @@ -234,7 +230,6 @@ async function query (name, opt = {}) {
* await sdkInstance.aensClaim(name, salt, { ttl, fee, nonce, nameFee })
*/
async function claim (name, salt, options) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }

const claimTx = await this.buildTx(TX_TYPE.nameClaim, {
Expand Down Expand Up @@ -279,7 +274,6 @@ async function claim (name, salt, options) {
* }
*/
async function preclaim (name, options = {}) {
ensureNameValid(name)
const opt = { ...this.Ae.defaults, ...options }
const _salt = salt()
const height = await this.height()
Expand Down
2 changes: 1 addition & 1 deletion src/chain.ts
Expand Up @@ -445,7 +445,7 @@ export async function resolveName (
decode(nameOrId)
return nameOrId as EncodedData<'ak'>
} catch (error) {}
if (isNameValid(nameOrId)) {
if (isNameValid(nameOrId) === true) {
if (verify || resolveByNode) {
const name = await onNode.api.getNameEntryByName(nameOrId)
const pointer = name.pointers.find(pointer => pointer.key === key)
Expand Down
3 changes: 1 addition & 2 deletions src/tx/builder/field-types.js
@@ -1,5 +1,5 @@
import {
writeId, readId, isNameValid, produceNameId, ensureNameValid, getMinimumNameFee, readInt, writeInt
writeId, readId, isNameValid, produceNameId, getMinimumNameFee, readInt, writeInt
} from './helpers'
import { InsufficientNameFeeError, IllegalArgumentError } from '../../utils/errors'
import { MIN_GAS_PRICE } from './constants'
Expand All @@ -16,7 +16,6 @@ export class Field {

export class Name extends Field {
static serialize (value) {
ensureNameValid(value)
return Buffer.from(value)
}

Expand Down
27 changes: 2 additions & 25 deletions src/tx/builder/helpers.js
Expand Up @@ -16,7 +16,6 @@ import { ceil } from '../../utils/bignumber'
import {
TagNotFoundError,
PrefixNotFoundError,
InvalidNameError,
IllegalBidFeeError,
NoDefaultAensPointerError,
ArgumentError
Expand Down Expand Up @@ -84,7 +83,6 @@ export function formatSalt (salt) {
* @return {String} `nm_` prefixed encoded AENS name
*/
export function produceNameId (name) {
ensureNameValid(name)
return encode(hash(name.toLowerCase()), 'nm')
}

Expand All @@ -101,7 +99,6 @@ export function produceNameId (name) {
* @return {String} Commitment hash
*/
export function commitmentHash (name, salt = createSalt()) {
ensureNameValid(name)
return encode(hash(concatBuffers([Buffer.from(name.toLowerCase()), formatSalt(salt)])), 'cm')
}

Expand Down Expand Up @@ -192,19 +189,6 @@ export function readPointers (pointers) {

const AENS_SUFFIX = '.chain'

/**
* Ensure that AENS name is valid
* @function
* @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers
* @param {string} name
* @return void
* @throws Error
*/
export function ensureNameValid (name) {
if (!name || typeof name !== 'string') throw new InvalidNameError('Name must be a string')
if (!name.endsWith(AENS_SUFFIX)) throw new InvalidNameError(`Name should end with ${AENS_SUFFIX}: ${name}`)
}

/**
* Is AENS name valid
* @function
Expand All @@ -213,12 +197,8 @@ export function ensureNameValid (name) {
* @return Boolean
*/
export function isNameValid (name) {
try {
ensureNameValid(name)
return true
} catch (error) {
return false
}
// TODO: probably there are stronger requirements
return name && typeof name === 'string' && name.endsWith(AENS_SUFFIX)
}

/**
Expand All @@ -241,7 +221,6 @@ export function getDefaultPointerKey (identifier) {
* @return {String} the minimum fee for the AENS name auction
*/
export function getMinimumNameFee (name) {
ensureNameValid(name)
const nameLength = name.length - AENS_SUFFIX.length
return NAME_BID_RANGES[Math.min(nameLength, NAME_MAX_LENGTH_FEE)]
}
Expand Down Expand Up @@ -273,7 +252,6 @@ export function computeBidFee (name, startFee, increment = NAME_FEE_BID_INCREMEN
* @return {String} Auction end height
*/
export function computeAuctionEndBlock (name, claimHeight) {
ensureNameValid(name)
const length = name.length - AENS_SUFFIX.length
const h = (length <= 4 && NAME_BID_TIMEOUTS[4]) ||
(length <= 8 && NAME_BID_TIMEOUTS[8]) ||
Expand All @@ -290,6 +268,5 @@ export function computeAuctionEndBlock (name, claimHeight) {
* @return {Boolean}
*/
export function isAuctionName (name) {
ensureNameValid(name)
return name.length < 13 + AENS_SUFFIX.length
}
7 changes: 0 additions & 7 deletions src/utils/errors.ts
Expand Up @@ -120,13 +120,6 @@ export class InvalidDenominationError extends BaseError {
}
}

export class InvalidNameError extends BaseError {
constructor (message: string) {
super(message)
this.name = 'InvalidNameError'
}
}

export class MissingParamError extends BaseError {
constructor (message: string) {
super(message)
Expand Down
18 changes: 1 addition & 17 deletions test/unit/tx.js
Expand Up @@ -26,7 +26,6 @@ import {
encode,
getDefaultPointerKey,
commitmentHash,
ensureNameValid,
getMinimumNameFee,
isNameValid,
produceNameId
Expand All @@ -35,10 +34,7 @@ import BigNumber from 'bignumber.js'
import { toBytes } from '../../src/utils/bytes'
import { buildTx, unpackTx } from '../../src/tx/builder'
import { NAME_BID_RANGES } from '../../src/tx/builder/schema'
import {
InvalidNameError,
SchemaNotFoundError
} from '../../src/utils/errors'
import { SchemaNotFoundError } from '../../src/utils/errors'

describe('Tx', function () {
it('reproducible commitment hashes can be generated', async () => {
Expand Down Expand Up @@ -73,18 +69,6 @@ describe('Tx', function () {
produceNameId('asdas.chain').should.be.equal('nm_2DMazuJNrGkQYve9eMttgdteaigeeuBk3fmRYSThJZ2NpX3r8R')
})

describe('ensureNameValid', () => {
it('validates type', () => {
expect(() => ensureNameValid({})).to.throw(InvalidNameError, 'Name must be a string')
})

it('validates domain', () => {
expect(() => ensureNameValid('asdasdasd.unknown')).to.throw(InvalidNameError, 'Name should end with .chain:')
})

it('don\'t throws exception', () => ensureNameValid('asdasdasd.chain'))
})

describe('getMinimumNameFee', () => {
it('returns correct name fees', () => {
for (let i = 1; i <= Object.keys(NAME_BID_RANGES).length; i++) {
Expand Down

0 comments on commit d0d1258

Please sign in to comment.