UNPKG

@flarenetwork/flare-stake-tool

Version:
153 lines 5.88 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.isEthereumApp = isEthereumApp; exports.getPublicKey = getPublicKey; exports.getCAddress = getCAddress; exports.getPAddress = getPAddress; exports.signPersonalMessage = signPersonalMessage; exports.signEvmTransaction = signEvmTransaction; const hw_app_eth_1 = __importStar(require("@ledgerhq/hw-app-eth")); const hw_transport_node_hid_1 = __importDefault(require("@ledgerhq/hw-transport-node-hid")); const pubk = __importStar(require("../flare/pubk")); const utils = __importStar(require("../utils")); async function isEthereumApp() { let eth = false; await _connect(async (app) => { try { const info = await app.getAppConfiguration(); console.log(info); eth = true; } catch (e) { console.log(e); } }); return eth; } async function getPublicKey(bip44Path) { let response; await _connect(async (app) => { response = await app.getAddress(bip44Path); }); if (!response || !response.publicKey) { throw new Error("Failed to obtain public key from ledger"); } return pubk.normalizePublicKey(response.publicKey); } async function getCAddress(bip44Path, display) { let response; await _connect(async (app) => { response = await app.getAddress(bip44Path, display); }); if (!response || !response.address) { throw new Error("Failed to obtain C-chain address from ledger"); } return response.address; } async function getPAddress(bip44Path, hrp, display) { let response; await _connect(async (app) => { response = await app.getAddress(bip44Path, display); }); if (!response || !response.publicKey) { throw new Error("Failed to obtain P-chain address from ledger"); } return pubk.publicKeyToPAddress(hrp, response.publicKey); } async function signPersonalMessage(bip44Path, message) { const messageHex = utils.toHex(Buffer.from(message, "utf-8"), false); let response; await _connect(async (app) => { response = await app.signPersonalMessage(bip44Path, messageHex); }); if (!response) { throw new Error("Failed to sign ETH personal message"); } if (!response.r || !response.s || !response.v) { throw new Error("Failed to get signature from ledger device"); } const r = Buffer.from(utils.toHex(response.r, false), "hex"); const s = Buffer.from(utils.toHex(response.s, false), "hex"); const v = Buffer.from(response.v.toString(16), "hex"); const signature = utils.toHex(Buffer.concat([r, s, v]), false); return signature; } async function signEvmTransaction(bip44Path, txHex) { const rawTx = utils.toHex(txHex, false); const resolution = await hw_app_eth_1.ledgerService.resolveTransaction(rawTx, {}, {}); let response; await _connect(async (app) => { response = await app.signTransaction(bip44Path, rawTx, resolution); }); if (!response) { throw new Error("Failed to sign EVM transaction on ledger"); } if (!response.r || !response.s || !response.v) { throw new Error("Failed to get signature from ledger device"); } const r = Buffer.from(utils.toHex(response.r, false), "hex"); const s = Buffer.from(utils.toHex(response.s, false), "hex"); let recoveryParam = parseInt(utils.toHex(response.v, false), 16); if (recoveryParam === 0 || recoveryParam === 1) { recoveryParam += 27; } else if (recoveryParam > 28) { recoveryParam = recoveryParam % 2 === 1 ? 27 : 28; } const v = Buffer.from(recoveryParam.toString(16), "hex"); const signature = utils.toHex(Buffer.concat([r, s, v]), false); return signature; } async function _connect(execute) { let eth; try { const transport = await hw_transport_node_hid_1.default.open(undefined); // Two ledger packages have slightly different Transport type definitions // (abortTimeoutMs optional vs explicitly undefined-able under exactOptionalPropertyTypes). // The runtime contract is identical. eth = new hw_app_eth_1.default(transport); await execute(eth); } finally { if (eth && eth.transport) { await eth.transport.close(); } } } //# sourceMappingURL=ethereum.js.map