3

In eosjs@16.0.9 I have a Eos.modules.format.encodeName()

Is there something like that in eosjs@20.0.0 or what can I use to replace it?

1

2 Answers 2

0

This module is often used in conjunction with get_table_rows(). If this is the case, you no longer need to encode names when making requests to this method. Simply pass the key as part of the lower_bound parameter.

For example you may make a request like this:

const resp = await rpc.get_table_rows({
  json: true,              // Get the response as json
  code: 'eosio',     // Contract that we target
  scope: 'eosio',         // Account that owns the data
  table: 'deposits',        // Table name
  lower_bound: 'ACCOUNT_NAME_HERE',   
  limit: 1               // Maximum number of rows 
});
1
  • 1
    I still need to fetch all the entries for an account name of secondary index, so this doesn't help. Aug 14, 2019 at 14:04
0

I was looking for the same, so I just took the code from eosjs 16.0.9

Just put it in your project, and use it. The logic hasn't changed and won't ever change.

/**
  Encode a name (a base32 string) to a number.

  For performance reasons, the blockchain uses the numerical encoding of strings
  for very common types like account names.

  @see types.hpp string_to_name

  @arg {string} name - A string to encode, up to 12 characters long.
  @arg {string} [littleEndian = true] - Little or Bigendian encoding

  @return {string<uint64>} - compressed string (from name arg).  A string is
    always used because a number could exceed JavaScript's 52 bit limit.
*/
function encodeName(name, littleEndian = true) {
  if(typeof name !== 'string')
    throw new TypeError('name parameter is a required string')

  if(name.length > 12)
    throw new TypeError('A name can be up to 12 characters long')

  let bitstr = ''
  for(let i = 0; i <= 12; i++) { // process all 64 bits (even if name is short)
    const c = i < name.length ? charidx(name[i]) : 0
    const bitlen = i < 12 ? 5 : 4
    let bits = Number(c).toString(2)
    if(bits.length > bitlen) {
      throw new TypeError('Invalid name ' + name)
    }
    bits = '0'.repeat(bitlen - bits.length) + bits
    bitstr += bits
  }

  const value = Long.fromString(bitstr, true, 2)

  // convert to LITTLE_ENDIAN
  let leHex = ''
  const bytes = littleEndian ? value.toBytesLE() : value.toBytesBE()
  for(const b of bytes) {
    const n = Number(b).toString(16)
    leHex += (n.length === 1 ? '0' : '') + n
  }

  const ulName = Long.fromString(leHex, true, 16).toString()

  // console.log('encodeName', name, value.toString(), ulName.toString(), JSON.stringify(bitstr.split(/(.....)/).slice(1)))

  return ulName.toString()
}

/**
  @arg {Long|String|number} value uint64
  @arg {string} [littleEndian = true] - Little or Bigendian encoding

  @return {string}
*/
function decodeName(value, littleEndian = true) {
  value = ULong(value)

  // convert from LITTLE_ENDIAN
  let beHex = ''
  const bytes = littleEndian ? value.toBytesLE() : value.toBytesBE()
  for(const b of bytes) {
    const n = Number(b).toString(16)
    beHex += (n.length === 1 ? '0' : '') + n
  }
  beHex += '0'.repeat(16 - beHex.length)

  const fiveBits = Long.fromNumber(0x1f, true)
  const fourBits = Long.fromNumber(0x0f, true)
  const beValue = Long.fromString(beHex, true, 16)

  let str = ''
  let tmp = beValue

  for(let i = 0; i <= 12; i++) {
    const c = charmap[tmp.and(i === 0 ? fourBits : fiveBits)]
    str = c + str
    tmp = tmp.shiftRight(i === 0 ? 4 : 5)
  }
  str = str.replace(/\.+$/, '') // remove trailing dots (all of them)

  // console.log('decodeName', str, beValue.toString(), value.toString(), JSON.stringify(beValue.toString(2).split(/(.....)/).slice(1)))

  return str
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.