accounts.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generateAccount = generateAccount;
  4. exports.generateRandom = generateRandom;
  5. exports.generateAccountWithMnemonic = generateAccountWithMnemonic;
  6. const bytes_js_1 = require("./bytes.js");
  7. const crypto_js_1 = require("./crypto.js");
  8. const ethersUtils_js_1 = require("./ethersUtils.js");
  9. const address_js_1 = require("./address.js");
  10. const INVALID_TRON_PATH_ERROR_MSG = 'Invalid tron path provided';
  11. function generateAccount() {
  12. const priKeyBytes = (0, crypto_js_1.genPriKey)();
  13. const pubKeyBytes = (0, crypto_js_1.getPubKeyFromPriKey)(priKeyBytes);
  14. const addressBytes = (0, crypto_js_1.getAddressFromPriKey)(priKeyBytes);
  15. const privateKey = (0, bytes_js_1.byteArray2hexStr)(priKeyBytes);
  16. const publicKey = (0, bytes_js_1.byteArray2hexStr)(pubKeyBytes);
  17. return {
  18. privateKey,
  19. publicKey,
  20. address: {
  21. base58: (0, crypto_js_1.getBase58CheckAddress)(addressBytes),
  22. hex: (0, bytes_js_1.byteArray2hexStr)(addressBytes),
  23. },
  24. };
  25. }
  26. function generateRandom(password = '', path = address_js_1.TRON_BIP39_PATH_INDEX_0, wordlist) {
  27. const account = ethersUtils_js_1.ethersHDNodeWallet.createRandom(password, path, wordlist);
  28. const result = {
  29. mnemonic: account.mnemonic,
  30. privateKey: account.privateKey,
  31. publicKey: account.signingKey.publicKey,
  32. address: (0, crypto_js_1.pkToAddress)(account.privateKey.replace(/^0x/, '')),
  33. path: account.path,
  34. };
  35. return result;
  36. }
  37. function generateAccountWithMnemonic(mnemonic, path = address_js_1.TRON_BIP39_PATH_INDEX_0, password = '', wordlist = null) {
  38. // eslint-disable-next-line no-useless-escape
  39. if (!String(path).match(/^m\/44\'\/195\'/)) {
  40. throw new Error(INVALID_TRON_PATH_ERROR_MSG);
  41. }
  42. const account = ethersUtils_js_1.ethersHDNodeWallet.fromMnemonic(ethersUtils_js_1.Mnemonic.fromPhrase(mnemonic, password, wordlist), path);
  43. const result = {
  44. mnemonic: account.mnemonic,
  45. privateKey: account.privateKey,
  46. publicKey: account.signingKey.publicKey,
  47. address: (0, crypto_js_1.pkToAddress)(account.privateKey.replace(/^0x/, '')),
  48. };
  49. return result;
  50. }
  51. //# sourceMappingURL=accounts.js.map