UNPKG

@tronlink/core

Version:

The library serves as a core module within TronLink Extension, which provides low-level wallet functionality for both Tron and Ethereum networks, primary features includes account generation and transaction signing

209 lines 7.74 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.TronWallet = void 0; // @ts-ignore const bip32 = __importStar(require("bip32")); // @ts-ignore const bip39_1 = __importDefault(require("bip39")); // @ts-ignore const tronweb_1 = require("tronweb"); const base_wallet_1 = require("../base_wallet"); const constants_1 = require("../base_wallet/constants"); const error_1 = require("../base_wallet/error"); const generateTronWeb_1 = require("./generateTronWeb"); const utils_1 = require("../utils"); class TronWallet extends base_wallet_1.BaseWallet { getCoinType() { return constants_1.CoinType.TRON; } derivePrivateKey(params) { const seed = bip39_1.default.mnemonicToSeed(params.mnemonic); // @ts-ignore const node = bip32.fromSeed(seed); const child = node.derivePath(params.path); const privateKey = child.privateKey.toString('hex'); return privateKey; } getAddressBy(params) { // @ts-ignore const address = tronweb_1.TronWeb.address.fromPrivateKey(params.privateKey); return address; } validateAddress(params) { if (!params.address) { return false; } // @ts-ignore return tronweb_1.TronWeb.isAddress(params.address); } async sign(params) { try { (0, utils_1.checkSignParams)(params); const { data, options } = params; const { isMultiSign, isSignMessageV2 } = options || { isMultiSign: false, isSignMessageV2: false, }; if (typeof data === 'string' || (data && data.constructor && data.constructor.name === 'String')) { if (isSignMessageV2) { return this.signMessageV2(params); } else { return this.signMessage(params); } } else if (typeof data === 'object' && data.txID) { if (isMultiSign) { return this.multiSign(params); } else { return this.signTransaction(params); } } else if (typeof data === 'object' && data.domain && data.types && data.message) { return this.signTypedData(params); } else { throw new error_1.InvalidParameterError(); } } catch (error) { throw new error_1.SignError(error.message); } } async signMessage(params) { try { (0, utils_1.checkSignParams)(params); const { privateKey, data: message } = params; return generateTronWeb_1.defaultTronWeb.trx.sign(message, privateKey); } catch (error) { throw new error_1.SignError(error.message); } } async verifyMessage(params) { try { const { data: message, signature, address } = params; if (!message || !signature) { throw new error_1.InvalidParameterError(); } return generateTronWeb_1.defaultTronWeb.trx.verifyMessage(message, signature, address); } catch (error) { throw new error_1.VerifySignError(error.message); } } async signTransaction(params) { try { (0, utils_1.checkSignParams)(params); const { privateKey, data: transaction } = params; return generateTronWeb_1.defaultTronWeb.trx.sign(transaction, privateKey); } catch (error) { throw new error_1.SignError(error.message); } } async signTypedData(params) { try { (0, utils_1.checkSignParams)(params); const { privateKey, data } = params; if (typeof data !== 'object') { throw new error_1.InvalidParameterError(); } const { domain, types, message } = data; return generateTronWeb_1.defaultTronWeb.trx._signTypedData(domain, types, message, privateKey); } catch (error) { throw new error_1.SignError(error.message); } } async verifyTypedData(params) { try { const { data, signature, address } = params; if (!data || !signature || !address || typeof data !== 'object') { throw new error_1.InvalidParameterError(); } const { domain, types, message } = data; return generateTronWeb_1.defaultTronWeb.trx.verifyTypedData(domain, types, message, signature, address); } catch (error) { throw new error_1.VerifySignError(error.message); } } async multiSign(params) { try { (0, utils_1.checkSignParams)(params); const { privateKey, data: transaction, options } = params; if (!options) { throw new error_1.InvalidParameterError(); } const { permissionId, nodeInfo } = options; const tronWeb = (0, generateTronWeb_1.generateTronWeb)(nodeInfo); return await tronWeb.trx.multiSign(transaction, privateKey, permissionId); } catch (error) { throw new error_1.SignError(error.message); } } async signMessageV2(params) { try { (0, utils_1.checkSignParams)(params); const { privateKey, data: message } = params; return generateTronWeb_1.defaultTronWeb.trx.signMessageV2(message, privateKey); } catch (error) { throw new error_1.SignError(error.message); } } async verifyMessageV2(params) { try { const { data: message, signature } = params; if (!message || !signature) { throw new error_1.InvalidParameterError(); } return generateTronWeb_1.defaultTronWeb.trx.verifyMessageV2(message, signature); } catch (error) { throw new error_1.VerifySignError(error.message); } } } exports.TronWallet = TronWallet; //# sourceMappingURL=TronWallet.js.map