├── .gitignore ├── .gitpod.yml ├── .prettierrc ├── .travis.yml ├── CHANGELOG.md ├── README.md ├── __tests__ ├── char_to_symbol.test.js ├── nameToUint64.test.js ├── symbol.test.js └── uint64ToName.test.js ├── codecov.yml ├── docs └── index.html ├── jest.config.js ├── package.json ├── src ├── char_to_symbol.js ├── index.js ├── nameToUint64.js ├── symbol.js └── uint64ToName.js ├── tsconfig.json ├── webpack.config.js └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | .vscode/ 3 | node_modules/ 4 | 5 | coverage/ 6 | 7 | lib/ 8 | dist/ 9 | 10 | *.log 11 | -------------------------------------------------------------------------------- /.gitpod.yml: -------------------------------------------------------------------------------- 1 | tasks: 2 | - init: yarn install 3 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "tabWidth": 2, 3 | "printWidth": 100, 4 | "singleQuote": true, 5 | "trailingComma": "all" 6 | } -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - stable 4 | - '10' 5 | cache: 6 | yarn: true 7 | install: 8 | - yarn 9 | - npm install -g codecov 10 | script: 11 | - yarn test --coverage 12 | - codecov 13 | deploy: 14 | provider: npm 15 | email: manh.vv.htth@gmail.com 16 | api_key: 17 | secure: FaLNssDeyTNbi3CwJu84WpXD/Y6y2G6IlFLx37Me4DEvGj2NEp/QogjjCvAy4uSZUOLR/arl0ZQ82POq8hjvjuyE/mw838/kvYDL5ilkqpgl+r8UEFUFoD3SekLkn2um1uwaM2GnqavDf3DAQyhLFBc70B+puXpGp5P+xoMTbxog3zbQUeFXzT+mYDneVqm2Y6wb5xmN2L6p9cJjFg0JD+AYMMR4XBxS+pfSp1rYUOpiwim4slTsgxi7moRTMCs0ar++/18uLBlCrvJHsTKENRek4IXa2KZHoIRwS9LdeOFATb9JKISjPqilPw5pmR6YASsvvSTGqlINjrLN8aeQFVSi7AV6w4LejaMsGcq8LO8LPhsrcpEOsgR1TL4iMuh802IM+MnrmstHHXaC5jKImG9YgwSpO8wm7YPloTZyn0iQcaFZcEIq2wbE7V1p33p9y9FyFW17eGu3Qs7dj9GrnA/7eBwV5npd134LKT5uZkQPJeXVnDGIigiiEsTcfy8MOM6tusHSXltrE+4xdUrDH7K2OCSQD7Qv0htwi/vbLIde0hFibbf9ZBd+v8jZxRTYtqqW/FSlKz01O+75JmLuvNL/QOEv/4C7sTNslmXFMABdeAZ89RM0AlsTN29GuT0m2irq4fA307KmXQPzA2YclRlHdfNijh/dhy2Uz1GyKOs= 18 | on: 19 | tags: true 20 | repo: manh-vv/eosjs-name 21 | branch: master 22 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines. 4 | 5 | ## [2.3.0](https://github.com/manh-vv/eosjs-name/compare/v2.2.1...v2.3.0) (2023-01-27) 6 | 7 | 8 | ### Features 9 | 10 | * [#29](https://github.com/manh-vv/eosjs-name/issues/29) support typescript ([ebeedc3](https://github.com/manh-vv/eosjs-name/commit/ebeedc3365402a69fb83c3bf6f9bbb8800141a0f)) 11 | 12 | ### [2.2.1](https://github.com/manh-vv/eosjs-name/compare/v2.2.0...v2.2.1) (2021-01-04) 13 | 14 | 15 | ### Bug Fixes 16 | 17 | * :green_heart: fix public for including the lib for front-end side ([132070a](https://github.com/manh-vv/eosjs-name/commit/132070ab35fa92ec789194d92c4151397c658e4b)) 18 | 19 | ## [2.2.0](https://github.com/manh-vv/eosjs-name/compare/v2.1.1...v2.2.0) (2021-01-04) 20 | 21 | 22 | ### Features 23 | 24 | * :hammer: add build for front-end side ([76cc1da](https://github.com/manh-vv/eosjs-name/commit/76cc1daf5f8b57ff344bcf9c26991fd4668406f1)) 25 | 26 | ### [2.1.1](https://github.com/manh-vv/eosjs-name/compare/v2.1.0...v2.1.1) (2020-10-31) 27 | 28 | ## [2.1.0](https://github.com/manh-vv/eosjs-name/compare/v1.1.1...v2.1.0) (2020-10-27) 29 | 30 | 31 | ### Features 32 | 33 | * :sparkles: convert number to symbol name ([16eac27](https://github.com/manh-vv/eosjs-name/commit/16eac27f9d8c150093ffc1b48adbe4cef3e9dbd4)) 34 | 35 | ## [2.0.0](https://github.com/manh-vv/eosjs-name/compare/v1.1.1...v2.0.0) (2020-10-26) 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Eosio name to uint64 2 | 3 | [![NPM](https://img.shields.io/npm/v/eosjs-account-name.svg)](https://www.npmjs.org/package/eosjs-account-name) 4 | [![Build Status](https://travis-ci.org/manh-vv/eosjs-name.svg?branch=master)](https://travis-ci.org/manh-vv/eosjs-name) 5 | [![codecov](https://codecov.io/gh/manh-vv/eosjs-name/branch/master/graph/badge.svg?token=WLVN5CZT6Q)](https://codecov.io/gh/manh-vv/eosjs-name) 6 | 7 | [![Gitpod ready-to-code](https://img.shields.io/badge/Gitpod-ready--to--code-blue?logo=gitpod)](https://gitpod.io/#https://github.com/manh-vv/eosjs-name) 8 | 9 | ```sh 10 | npm i eosjs-account-name 11 | ``` 12 | 13 | ```sh 14 | yarn add eosjs-account-name 15 | ``` 16 | 17 | ```html 18 | 19 | 20 | ``` 21 | 22 | Example of browser is in [index.html](./docs/index.html). 23 | 24 | ## From name to uint64 25 | 26 | Here is [how eosio account name is computed to uint64](https://github.com/EOSIO/eos/blob/master/libraries/chain/include/eosio/chain/name.hpp#L21). 27 | 28 | ```text 29 | "eosio", 30 | "eosio.msig", 31 | "eosio.token", 32 | 33 | ---- 6138663577826885632 34 | ---- 6138663587900751872 35 | ---- 6138663591592764928 36 | ``` 37 | 38 | ## From uint64 to name 39 | 40 | Source https://github.com/EOSIO/eos/blob/master/libraries/chain/name.cpp#L19 41 | 42 | ## Example 43 | 44 | Try on run-kit https://npm.runkit.com/eosjs-account-name 45 | 46 | ```javascript 47 | const eosjsAccountName = require('eosjs-account-name'); 48 | const n = eosjsAccountName.nameToUint64('eosio'); 49 | 50 | console.log('eosio to uint64: ' + n); 51 | 52 | console.log('uint64 to name: ' + eosjsAccountName.uint64ToName(n)); 53 | ``` 54 | 55 | ### Parse symbol name 56 | 57 | [symbol.test.js](./__tests__/symbol.test.js) 58 | 59 | ```javascript 60 | const { symbol, nameToUint64 } = require('eosjs-account-name'); 61 | 62 | /** 63 | * cleos -u https://eos.greymass.com get scope eosio.token -t stat 64 | */ 65 | const name = '........ehbo5'; 66 | const uint64 = nameToUint64(name); 67 | const symbolName = symbol.toName(uint64); 68 | 69 | // expect(symbolName).toEqual('EOS'); 70 | ``` 71 | 72 | ## Note on random eosio name 73 | 74 | In case you want to generate a random name, I suggest you use [`nanoid`](https://zelark.github.io/nano-id-cc/). 75 | 76 | ```javascript 77 | const generate = require('nanoid/generate'); 78 | const alphabet = '.12345abcdefghijklmnopqrstuvwxyz'; 79 | generate(alphabet, 12); //=> "nc4zs1yyg.jx" 80 | ``` 81 | -------------------------------------------------------------------------------- /__tests__/char_to_symbol.test.js: -------------------------------------------------------------------------------- 1 | const { char_to_symbol } = require('../dist'); 2 | 3 | const charMap = '.12345abcdefghijklmnopqrstuvwxyz'; 4 | 5 | describe('test char to symbol', () => { 6 | it('should convert char to symbol', () => { 7 | for (const c of charMap) { 8 | expect(char_to_symbol(c)).toEqual(expect.any(Number)); 9 | } 10 | 11 | expect(char_to_symbol(charMap[0])).toEqual(0); 12 | expect(char_to_symbol(charMap[5])).toEqual(5); 13 | expect(char_to_symbol(charMap[15])).toEqual(15); 14 | expect(char_to_symbol(charMap[25])).toEqual(25); 15 | expect(char_to_symbol(charMap[31])).toEqual(31); 16 | }); 17 | }); 18 | -------------------------------------------------------------------------------- /__tests__/nameToUint64.test.js: -------------------------------------------------------------------------------- 1 | const { nameToUint64 } = require('../dist'); 2 | 3 | describe('test name to uint64', () => { 4 | it('should transfrom `eosio` to `6138663577826885632`', () => { 5 | const name = 'eosio'; 6 | const uint64 = nameToUint64(name); 7 | expect(uint64).toBe('6138663577826885632'); 8 | }); 9 | 10 | it('should transfrom `eosio.msig` to `6138663587900751872`', () => { 11 | const name = 'eosio.msig'; 12 | const uint64 = nameToUint64(name); 13 | expect(uint64).toBe('6138663587900751872'); 14 | }); 15 | 16 | it('should transfrom `eosio.token` to `6138663591592764928`', () => { 17 | const name = 'eosio.token'; 18 | const uint64 = nameToUint64(name); 19 | expect(uint64).toBe('6138663591592764928'); 20 | }); 21 | 22 | it('should transfrom `namewith13cha` to `11071165353165377750`', () => { 23 | const name = 'namewith13cha'; 24 | const uint64 = nameToUint64(name); 25 | expect(uint64).toBe('11071165353165377750'); 26 | }); 27 | }); 28 | -------------------------------------------------------------------------------- /__tests__/symbol.test.js: -------------------------------------------------------------------------------- 1 | const { symbol, nameToUint64 } = require('../dist'); 2 | 3 | describe('test symbol name', () => { 4 | it('EOS symbol', () => { 5 | /** 6 | * cleos -u https://eos.greymass.com get scope eosio.token -t stat 7 | */ 8 | const name = '........ehbo5'; 9 | const uint64 = nameToUint64(name); 10 | expect(symbol.toName(uint64)).toEqual('EOS'); 11 | }); 12 | 13 | it('parse symbol name', () => { 14 | /** 15 | * cleos -u https://api.canfoundation.io get scope canpasstoken -t stat 16 | */ 17 | const data = [ 18 | { 19 | scope: '.11oen2oeh2p4', 20 | }, 21 | { 22 | scope: '.1b5glegch2og', 23 | }, 24 | { 25 | scope: '.1f4en23cpa54', 26 | }, 27 | ]; 28 | 29 | const rs = ['TESTLEC', 'LECLEVN', 'TLECLEV']; 30 | 31 | data 32 | .map(({ scope }) => nameToUint64(scope)) 33 | .map((n) => symbol.toName(n)) 34 | .forEach((sym, i) => { 35 | expect(sym).toEqual(rs[i]); 36 | }); 37 | }); 38 | }); 39 | -------------------------------------------------------------------------------- /__tests__/uint64ToName.test.js: -------------------------------------------------------------------------------- 1 | const { uint64ToName } = require('../dist'); 2 | 3 | describe('test uint64 to name', () => { 4 | it('should transfrom `6138663577826885632` to `eosio`', () => { 5 | const uint64 = '6138663577826885632'; 6 | const name = uint64ToName(uint64); 7 | expect(name).toBe('eosio'); 8 | }); 9 | 10 | it('should transfrom `6138663587900751872` to `eosio.msig`', () => { 11 | const uint64 = '6138663587900751872'; 12 | const name = uint64ToName(uint64); 13 | expect(name).toBe('eosio.msig'); 14 | }); 15 | 16 | it('should transfrom `6138663591592764928` to `eosio.token`', () => { 17 | const uint64 = '6138663591592764928'; 18 | const name = uint64ToName(uint64); 19 | expect(name).toBe('eosio.token'); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /codecov.yml: -------------------------------------------------------------------------------- 1 | codecov: 2 | require_ci_to_pass: yes 3 | 4 | coverage: 5 | precision: 2 6 | round: down 7 | range: "98...100" 8 | 9 | parsers: 10 | gcov: 11 | branch_detection: 12 | conditional: yes 13 | loop: yes 14 | method: no 15 | macro: no 16 | 17 | comment: 18 | layout: "reach,diff,flags,files,footer" 19 | behavior: default 20 | require_changes: no 21 | -------------------------------------------------------------------------------- /docs/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | demo eosjs-name 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 24 | 25 | 26 | 27 |
28 |
29 |
30 |

Hello eosjsName

31 | 32 |

Import the script

33 |
34 | 35 |

After import, the library name is eosjsName.

36 | 37 |

Convert eosio to uint64

38 | 39 |

uint64 value of eosio is:

40 |
41 | 42 |

Convert uint64 to eosio

43 |

eosio name value of 6138663577826885632 is:

44 |
45 | 46 |

The library also support to get representation of symbol

47 |

48 | Suppose that we want to know what is value of ........ehbo5 in the following query: 49 |

 50 | 
 51 |         
 52 |           cleos -u https://eos.greymass.com get scope eosio.token -t stat
 53 |           {
 54 |             "rows": [{
 55 |                 "code": "eosio.token",
 56 |                 "scope": "........ehbo5",
 57 |                 "table": "stat",
 58 |                 "payer": "eosio.token",
 59 |                 "count": 1
 60 |               }
 61 |             ],
 62 |             "more": "12345zzxxxxx"
 63 |           }
 64 |         
 65 |       
66 |

67 |

Its value is:

68 | 69 |
 70 |       
 71 |     
72 |
73 |
74 |
75 | 76 | 79 | 80 | 85 | 86 | 91 | 92 | 113 | 114 | 115 | -------------------------------------------------------------------------------- /jest.config.js: -------------------------------------------------------------------------------- 1 | // For a detailed explanation regarding each configuration property, visit: 2 | // https://jestjs.io/docs/en/configuration.html 3 | 4 | module.exports = { 5 | // All imported modules in your tests should be mocked automatically 6 | // automock: false, 7 | 8 | // Stop running tests after `n` failures 9 | // bail: 0, 10 | 11 | // Respect "browser" field in package.json when resolving modules 12 | // browser: false, 13 | 14 | // The directory where Jest should store its cached dependency information 15 | // cacheDirectory: "/private/var/folders/g4/y8vxt9510yj_lbl5fjc6bd2m0000gn/T/jest_dx", 16 | 17 | // Automatically clear mock calls and instances between every test 18 | clearMocks: true, 19 | 20 | // Indicates whether the coverage information should be collected while executing the test 21 | // collectCoverage: false, 22 | 23 | // An array of glob patterns indicating a set of files for which coverage information should be collected 24 | collectCoverageFrom: ['./dist/*.js'], 25 | 26 | // The directory where Jest should output its coverage files 27 | coverageDirectory: 'coverage', 28 | 29 | // An array of regexp pattern strings used to skip coverage collection 30 | coveragePathIgnorePatterns: ['/node_modules/', '/lib/', '/_tests_/'], 31 | 32 | // A list of reporter names that Jest uses when writing coverage reports 33 | // coverageReporters: [ 34 | // "json", 35 | // "text", 36 | // "lcov", 37 | // "clover" 38 | // ], 39 | 40 | // An object that configures minimum threshold enforcement for coverage results 41 | // coverageThreshold: null, 42 | 43 | // A path to a custom dependency extractor 44 | // dependencyExtractor: null, 45 | 46 | // Make calling deprecated APIs throw helpful error messages 47 | // errorOnDeprecated: false, 48 | 49 | // Force coverage collection from ignored files using an array of glob patterns 50 | // forceCoverageMatch: [], 51 | 52 | // A path to a module which exports an async function that is triggered once before all test suites 53 | // globalSetup: null, 54 | 55 | // A path to a module which exports an async function that is triggered once after all test suites 56 | // globalTeardown: null, 57 | 58 | // A set of global variables that need to be available in all test environments 59 | // globals: {}, 60 | 61 | // The maximum amount of workers used to run your tests. Can be specified as % or a number. E.g. maxWorkers: 10% will use 10% of your CPU amount + 1 as the maximum worker number. maxWorkers: 2 will use a maximum of 2 workers. 62 | // maxWorkers: "50%", 63 | 64 | // An array of directory names to be searched recursively up from the requiring module's location 65 | // moduleDirectories: [ 66 | // "node_modules" 67 | // ], 68 | 69 | // An array of file extensions your modules use 70 | // moduleFileExtensions: [ 71 | // "js", 72 | // "json", 73 | // "jsx", 74 | // "ts", 75 | // "tsx", 76 | // "node" 77 | // ], 78 | 79 | // A map from regular expressions to module names that allow to stub out resources with a single module 80 | // moduleNameMapper: {}, 81 | 82 | // An array of regexp pattern strings, matched against all module paths before considered 'visible' to the module loader 83 | // modulePathIgnorePatterns: [], 84 | 85 | // Activates notifications for test results 86 | // notify: false, 87 | 88 | // An enum that specifies notification mode. Requires { notify: true } 89 | // notifyMode: "failure-change", 90 | 91 | // A preset that is used as a base for Jest's configuration 92 | // preset: null, 93 | 94 | // Run tests from one or more projects 95 | // projects: null, 96 | 97 | // Use this configuration option to add custom reporters to Jest 98 | // reporters: undefined, 99 | 100 | // Automatically reset mock state between every test 101 | // resetMocks: false, 102 | 103 | // Reset the module registry before running each individual test 104 | // resetModules: false, 105 | 106 | // A path to a custom resolver 107 | // resolver: null, 108 | 109 | // Automatically restore mock state between every test 110 | // restoreMocks: false, 111 | 112 | // The root directory that Jest should scan for tests and modules within 113 | // rootDir: null, 114 | 115 | // A list of paths to directories that Jest should use to search for files in 116 | // roots: [ 117 | // "" 118 | // ], 119 | 120 | // Allows you to use a custom runner instead of Jest's default test runner 121 | // runner: "jest-runner", 122 | 123 | // The paths to modules that run some code to configure or set up the testing environment before each test 124 | // setupFiles: [], 125 | 126 | // A list of paths to modules that run some code to configure or set up the testing framework before each test 127 | // setupFilesAfterEnv: [], 128 | 129 | // A list of paths to snapshot serializer modules Jest should use for snapshot testing 130 | // snapshotSerializers: [], 131 | 132 | // The test environment that will be used for testing 133 | testEnvironment: 'node', 134 | 135 | // Options that will be passed to the testEnvironment 136 | // testEnvironmentOptions: {}, 137 | 138 | // Adds a location field to test results 139 | // testLocationInResults: false, 140 | 141 | // The glob patterns Jest uses to detect test files 142 | // testMatch: [ 143 | // "**/__tests__/**/*.[jt]s?(x)", 144 | // "**/?(*.)+(spec|test).[tj]s?(x)" 145 | // ], 146 | 147 | // An array of regexp pattern strings that are matched against all test paths, matched tests are skipped 148 | // testPathIgnorePatterns: [ 149 | // "/node_modules/" 150 | // ], 151 | 152 | // The regexp pattern or array of patterns that Jest uses to detect test files 153 | // testRegex: [], 154 | 155 | // This option allows the use of a custom results processor 156 | // testResultsProcessor: null, 157 | 158 | // This option allows use of a custom test runner 159 | // testRunner: "jasmine2", 160 | 161 | // This option sets the URL for the jsdom environment. It is reflected in properties such as location.href 162 | // testURL: "http://localhost", 163 | 164 | // Setting this value to "fake" allows the use of fake timers for functions such as "setTimeout" 165 | // timers: "real", 166 | 167 | // A map from regular expressions to paths to transformers 168 | // transform: null, 169 | 170 | // An array of regexp pattern strings that are matched against all source file paths, matched files will skip transformation 171 | // transformIgnorePatterns: [ 172 | // "/node_modules/" 173 | // ], 174 | 175 | // An array of regexp pattern strings that are matched against all modules before the module loader will automatically return a mock for them 176 | // unmockedModulePathPatterns: undefined, 177 | 178 | // Indicates whether each individual test should be reported during the run 179 | // verbose: null, 180 | 181 | // An array of regexp patterns that are matched against all source file paths before re-running tests in watch mode 182 | // watchPathIgnorePatterns: [], 183 | 184 | // Whether to use watchman for file crawling 185 | // watchman: true, 186 | }; 187 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "eosjs-account-name", 3 | "version": "2.3.0", 4 | "description": "eos name in string to uint64 and vice versa", 5 | "main": "dist/index.js", 6 | "files": [ 7 | "dist", 8 | "lib" 9 | ], 10 | "types": "dist/index.d.ts", 11 | "author": "manh.vv.htth@gmail.com", 12 | "license": "MIT", 13 | "repository": "https://github.com/manh-vv/eosjs-name.git", 14 | "keywords": [ 15 | "eosio account name", 16 | "eosio name", 17 | "eos name", 18 | "account_name", 19 | "symbol name" 20 | ], 21 | "dependencies": {}, 22 | "devDependencies": { 23 | "husky": "^4.3.0", 24 | "jest": "^26.6.1", 25 | "lint-staged": "^10.4.2", 26 | "prettier": "^2.1.2", 27 | "standard-version": "^9.0.0", 28 | "ts-loader": "^9.4.2", 29 | "typescript": "^4.9.4", 30 | "webpack": "^5.11.1", 31 | "webpack-cli": "^4.3.1" 32 | }, 33 | "scripts": { 34 | "build-tsc": "yarn tsc", 35 | "build": "yarn build-tsc && yarn webpack", 36 | "prepare": "yarn build", 37 | "release": "standard-version", 38 | "pretest": "yarn build-tsc", 39 | "test": "jest" 40 | }, 41 | "lint-staged": { 42 | "*.{js,json}": [ 43 | "prettier --write" 44 | ] 45 | }, 46 | "husky": { 47 | "hooks": { 48 | "pre-commit": "lint-staged" 49 | } 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/char_to_symbol.js: -------------------------------------------------------------------------------- 1 | /** 2 | * Convert a charactor to a preset charactor 3 | * @param {String|Number} c 4 | * @returns 5 | */ 6 | exports.char_to_symbol = (c) => { 7 | if (typeof c == 'string') c = c.charCodeAt(0); 8 | 9 | if (c >= 'a'.charCodeAt(0) && c <= 'z'.charCodeAt(0)) { 10 | return c - 'a'.charCodeAt(0) + 6; 11 | } 12 | 13 | if (c >= '1'.charCodeAt(0) && c <= '5'.charCodeAt(0)) { 14 | return c - '1'.charCodeAt(0) + 1; 15 | } 16 | 17 | return 0; 18 | }; 19 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | const { char_to_symbol } = require('./char_to_symbol'); 2 | const { nameToUint64 } = require('./nameToUint64'); 3 | const { uint64ToName } = require('./uint64ToName'); 4 | const symbol = require('./symbol'); 5 | 6 | module.exports = { 7 | char_to_symbol, 8 | nameToUint64, 9 | uint64ToName, 10 | symbol, 11 | }; 12 | -------------------------------------------------------------------------------- /src/nameToUint64.js: -------------------------------------------------------------------------------- 1 | const { char_to_symbol } = require('./char_to_symbol'); 2 | 3 | /** 4 | * Convert string to create ID in EOS smart contract. 5 | * @param {String} s 6 | * @return string to uint64 is not fit to js number, the return value is string format 7 | */ 8 | exports.nameToUint64 = (s) => { 9 | let n = 0n; 10 | 11 | let i = 0; 12 | for (; i < 12 && s[i]; i++) { 13 | n |= BigInt(char_to_symbol(s.charCodeAt(i)) & 0x1f) << BigInt(64 - 5 * (i + 1)); 14 | } 15 | 16 | if (i == 12) { 17 | n |= BigInt(char_to_symbol(s.charCodeAt(i)) & 0x0f); 18 | } 19 | 20 | return n.toString(); 21 | }; 22 | -------------------------------------------------------------------------------- /src/symbol.js: -------------------------------------------------------------------------------- 1 | /** 2 | * https://github.com/EOSIO/eos/blob/0d87dff8bee56179aa01472dd00a089b2aa7b9fa/libraries/chain/include/eosio/chain/symbol.hpp#L104 3 | * @param {String} value is string reprenting the value of a uint64 4 | */ 5 | exports.toName = (value) => { 6 | let v = BigInt.asUintN(64, value); 7 | 8 | let result = ''; 9 | 10 | while (v > 0) { 11 | const c = v & BigInt(0xff); 12 | result += String.fromCharCode(Number(c.toString())); 13 | v >>= 8n; 14 | } 15 | 16 | return result; 17 | }; 18 | -------------------------------------------------------------------------------- /src/uint64ToName.js: -------------------------------------------------------------------------------- 1 | const charMap = '.12345abcdefghijklmnopqrstuvwxyz'; 2 | 3 | /** 4 | * Convert from given Uint64 to a EOS's name format. 5 | * @param {String} value A string represents uint64 6 | * @returns 7 | */ 8 | exports.uint64ToName = (value) => { 9 | const str = []; 10 | 11 | let tmp = BigInt.asUintN(64, value); 12 | for (let i = 0; i <= 12; ++i) { 13 | const idx = tmp & BigInt(i === 0 ? 0x0f : 0x1f); 14 | 15 | str[12 - i] = charMap[Number(idx.toString())]; 16 | tmp = tmp >> BigInt(i === 0 ? 4 : 5); 17 | } 18 | 19 | return str.join('').replace(/\.+$/g, ''); 20 | }; 21 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "declaration": true, 4 | "outDir": "./dist/", 5 | "sourceMap": true, 6 | "noImplicitAny": true, 7 | "module": "commonjs", 8 | "target": "es5", 9 | "allowJs": true, 10 | "moduleResolution": "node" 11 | }, 12 | "include": ["src"] 13 | } 14 | -------------------------------------------------------------------------------- /webpack.config.js: -------------------------------------------------------------------------------- 1 | const path = require('path'); 2 | 3 | module.exports = { 4 | mode: 'production', 5 | entry: './src/index.js', 6 | output: { 7 | path: path.resolve(__dirname, 'lib'), 8 | filename: 'index.js', 9 | library: 'eosjsName', 10 | libraryTarget: 'umd', 11 | }, 12 | target: 'web', 13 | module: { 14 | rules: [ 15 | { 16 | test: /\.tsx?$/, 17 | use: 'ts-loader', 18 | exclude: /node_modules/, 19 | }, 20 | ], 21 | }, 22 | resolve: { 23 | extensions: ['.tsx', '.ts', '.js'], 24 | }, 25 | }; 26 | --------------------------------------------------------------------------------