diff --git a/es/ae/aens.js b/es/ae/aens.js index 91d8ab69b5..7834b2b2ce 100644 --- a/es/ae/aens.js +++ b/es/ae/aens.js @@ -28,7 +28,7 @@ import * as R from 'ramda' import { encodeBase58Check, salt } from '../utils/crypto' -import { commitmentHash, prelimaCommitmentHash, isNameValid, getMinimumNameFee } from '../tx/builder/helpers' +import { commitmentHash, isNameValid, getMinimumNameFee } from '../tx/builder/helpers' import Ae from './' import { CLIENT_TTL, NAME_FEE, NAME_TTL } from '../tx/builder/schema' @@ -169,6 +169,12 @@ async function query (name, opt = {}) { * @return {Promise} the result of the claim */ async function claim (name, salt, options = {}) { + // Todo remove cross compatibility + const { version } = this.getNodeInfo() + const [majorVersion] = version.split('.') + const vsn = +majorVersion === 5 && version !== '5.0.0-rc.1' ? 2 : 1 + options.vsn = options.vsn || vsn + isNameValid(name) const opt = R.merge(this.Ae.defaults, options) @@ -187,7 +193,8 @@ async function claim (name, salt, options = {}) { })) const result = await this.send(claimTx, opt) - if (opt.vsn === 1) { + if (opt.vsn === 1 || name.length - 4 > 12) { + delete opt.vsn const nameInter = this.Chain.defaults.waitMined ? await this.aensQuery(name, opt) : {} return Object.assign(result, nameInter) } @@ -204,18 +211,11 @@ async function claim (name, salt, options = {}) { * @return {Promise} */ async function preclaim (name, options = {}) { - // TODO remove cross compatibility - const { version } = this.getNodeInfo() - const [majorVersion] = version.split('.') - const vsn = +majorVersion === 5 && version !== '5.0.0-rc.1' ? 2 : 1 - isNameValid(name) const opt = R.merge(this.Ae.defaults, options) const _salt = salt() const height = await this.height() - const hash = vsn === 1 - ? await prelimaCommitmentHash(name, _salt) - : await commitmentHash(name, _salt) + const hash = commitmentHash(name, _salt) const preclaimTx = await this.namePreclaimTx(R.merge(opt, { accountId: await this.address(opt), @@ -227,7 +227,7 @@ async function preclaim (name, options = {}) { return Object.freeze({ ...result, height, - claim: options => this.aensClaim(name, _salt, { ...options, onAccount: opt.onAccount, vsn }), + claim: options => this.aensClaim(name, _salt, { ...options, onAccount: opt.onAccount }), salt: _salt, commitmentId: hash }) diff --git a/es/ae/index.js b/es/ae/index.js index 56d111e725..6606e31254 100644 --- a/es/ae/index.js +++ b/es/ae/index.js @@ -30,7 +30,7 @@ import TxBuilder from '../tx/builder' import * as R from 'ramda' import { BigNumber } from 'bignumber.js' import { isAddressValid } from '../utils/crypto' -import { isNameValid } from '../tx/builder/helpers' +import { isNameValid, produceNameId } from '../tx/builder/helpers' /** * Sign and post a transaction to the chain @@ -84,10 +84,12 @@ async function spend (amount, recipientId, options = {}) { async function resolveRecipientName (nameOrAddress, { verify = false }) { if (isAddressValid(nameOrAddress)) return nameOrAddress if (isNameValid(nameOrAddress)) { - const { id, pointers } = await this.getName(nameOrAddress) // Validation - if (verify && !pointers.find(({ id }) => id.split('_')[0] === 'ak')) throw new Error(`Name ${nameOrAddress} do not have pointers for account`) - return id + if (verify) { + const { pointers } = await this.getName(nameOrAddress) + if (!pointers.find(({ id }) => id.split('_')[0] === 'ak')) throw new Error(`Name ${nameOrAddress} do not have pointers for account`) + } + return produceNameId(nameOrAddress) } } diff --git a/es/tx/builder/helpers.js b/es/tx/builder/helpers.js index 7305a6223b..2320e3253c 100644 --- a/es/tx/builder/helpers.js +++ b/es/tx/builder/helpers.js @@ -7,7 +7,7 @@ import { decodeBase64Check, encodeBase58Check, encodeBase64Check, hash, - nameId, + nameId as nameHash, salt } from '../../utils/crypto' import { toBytes } from '../../utils/bytes' @@ -89,19 +89,16 @@ export function formatSalt (salt) { } /** - * Generate the commitment hash by hashing the formatted salt and - * name, base 58 encoding the result and prepending 'cm_' - * + * Encode a domain name + * @function * @alias module:@aeternity/aepp-sdk/es/tx/builder/helpers - * @function prelimaCommitmentHash - * @category async - * @rtype (name: String, salt?: String) => hash: Promise[String] - * @param {String} name - Name to be registered - * @param {Number} salt Random salt - * @return {String} Commitment hash + * @param {String} name Name to encode + * @return {String} `nm_` prefixed encoded domain name */ -export async function prelimaCommitmentHash (name, salt = createSalt()) { - return `cm_${encodeBase58Check(hash(Buffer.concat([nameId(name.toLowerCase()), formatSalt(salt)])))}` +export function produceNameId (name) { + const namespace = R.last(name.split('.')) + if (namespace === 'aet') return encode(hash(name.toLowerCase()), 'nm') + return encode(nameHash(name), 'nm') } /** @@ -116,8 +113,10 @@ export async function prelimaCommitmentHash (name, salt = createSalt()) { * @param {Number} salt Random salt * @return {String} Commitment hash */ -export async function commitmentHash (name, salt = createSalt()) { - return `cm_${encodeBase58Check(hash(Buffer.concat([Buffer.from(name), formatSalt(salt)])))}` +export function commitmentHash (name, salt = createSalt()) { + const namespace = R.last(name.split('.')) + if (namespace === 'aet') return `cm_${encodeBase58Check(hash(Buffer.concat([Buffer.from(name), formatSalt(salt)])))}` + return `cm_${encodeBase58Check(hash(Buffer.concat([nameHash(name.toLowerCase()), formatSalt(salt)])))}` } /** @@ -293,5 +292,6 @@ export default { oracleQueryId, createSalt, buildHash, - isNameValid + isNameValid, + produceNameId } diff --git a/es/tx/builder/schema.js b/es/tx/builder/schema.js index e749eea866..eff1aefe8d 100644 --- a/es/tx/builder/schema.js +++ b/es/tx/builder/schema.js @@ -13,10 +13,12 @@ import BigNumber from 'bignumber.js' export const VSN = 1 export const VSN_2 = 2 +// # TRANSACTION DEFAULT TTL +export const TX_TTL = 0 + // # AENS export const AENS_NAME_DOMAINS = ['aet', 'test'] export const NAME_TTL = 500 -// # Aens // # max number of block into the future that the name is going to be available // # https://github.com/aeternity/protocol/blob/epoch-v0.22.0/AENS.md#update // # https://github.com/aeternity/protocol/blob/44a93d3aab957ca820183c3520b9daf6b0fedff4/AENS.md#aens-entry diff --git a/es/tx/tx.js b/es/tx/tx.js index 30a1dfa8dc..1e4d2a702b 100644 --- a/es/tx/tx.js +++ b/es/tx/tx.js @@ -28,7 +28,7 @@ import ChainNode from '../chain/node' import Tx from './' import { buildTx, calculateFee } from './builder' -import { ABI_VERSIONS, MIN_GAS_PRICE, PROTOCOL_VM_ABI, TX_TYPE, VM_TYPE } from './builder/schema' +import { ABI_VERSIONS, MIN_GAS_PRICE, PROTOCOL_VM_ABI, TX_TYPE, VM_TYPE, TX_TTL } from './builder/schema' import { buildContractId, oracleQueryId } from './builder/helpers' async function spendTx ({ senderId, recipientId, amount, payload = '' }) { @@ -392,7 +392,7 @@ function getVmVersion (txType, { vmVersion, abiVersion, backend } = {}) { * @param {boolean} relative ttl is absolute or relative(default: true(relative)) * @return {number} Absolute Ttl */ -async function calculateTtl (ttl = 0, relative = true) { +async function calculateTtl (ttl = TX_TTL, relative = true) { if (ttl === 0) return 0 if (ttl < 0) throw new Error('ttl must be greater than 0') diff --git a/es/utils/crypto.js b/es/utils/crypto.js index 2f20486e6f..c17559413d 100644 --- a/es/utils/crypto.js +++ b/es/utils/crypto.js @@ -70,12 +70,13 @@ export function formatAddress (format = ADDRESS_FORMAT.api, address) { * Check if address is valid * @rtype (input: String) => valid: Boolean * @param {String} address - Address + * @param {String} prefix Transaction prefix. Default: 'ak' * @return {Boolean} valid */ -export function isAddressValid (address) { +export function isAddressValid (address, prefix = 'ak') { let isValid try { - isValid = decodeBase58Check(assertedType(address, 'ak')).length === 32 + isValid = decodeBase58Check(assertedType(address, prefix)).length === 32 } catch (e) { isValid = false }