@flarenetwork/flare-stake-tool
Version:
Utilities for staking on the Flare network
141 lines • 5.33 kB
JavaScript
;
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.getPublicKey = getPublicKey;
exports.ethereumSignMessage = ethereumSignMessage;
exports.signEvmTransaction = signEvmTransaction;
const connect_web_1 = __importDefault(require("@trezor/connect-web"));
const utils = __importStar(require("../utils"));
const tx_1 = require("@ethereumjs/tx");
let initialized = false;
async function getPublicKey(bip44Path) {
_initialize();
const response = await connect_web_1.default.getPublicKey({ path: bip44Path, showOnTrezor: false });
if (response.success) {
return response.payload.publicKey;
}
else {
throw Error(`Failed to obtain public key from trezor: ${response.payload.error}, code ${response.payload.code}`);
}
}
async function ethereumSignMessage(bip44Path, message) {
_initialize();
const response = await connect_web_1.default.ethereumSignMessage({ path: bip44Path, message: message, hex: false });
if (response.success) {
const signature = utils.toHex(response.payload.signature, true);
return signature;
}
else {
throw new Error(`Failed to sign message on trezor: ${response.payload.error}, code ${response.payload.code}`);
}
}
async function signEvmTransaction(bip44Path, txHex) {
_initialize();
const tx = tx_1.TransactionFactory.fromSerializedData(utils.toBuffer(txHex));
const btx = {
to: tx.to ? tx.to.toString() : "0x0",
value: tx.value.toString(16),
data: utils.toHex(tx.data, true),
nonce: tx.nonce.toString(16),
gasLimit: tx.gasLimit.toString(16),
};
let transaction;
let chainId;
if (tx instanceof tx_1.LegacyTransaction) {
if (tx.v === undefined) {
throw new Error("Legacy transaction must have v field defined");
}
chainId = Number(tx.v);
transaction = {
...btx,
gasPrice: tx.gasPrice.toString(16),
chainId,
};
}
else if (tx instanceof tx_1.FeeMarketEIP1559Transaction) {
chainId = Number(tx.chainId);
transaction = {
...btx,
maxFeePerGas: tx.maxFeePerGas.toString(16),
maxPriorityFeePerGas: tx.maxPriorityFeePerGas.toString(16),
chainId,
};
}
else {
throw new Error("Unsupported EVM transaction type");
}
const response = await connect_web_1.default.ethereumSignTransaction({
path: bip44Path,
transaction,
});
if (response.success) {
const r = Buffer.from(utils.toHex(response.payload.r, false), "hex");
const s = Buffer.from(utils.toHex(response.payload.s, false), "hex");
let recoveryParam = parseInt(utils.toHex(response.payload.v, false), 16);
if (recoveryParam > 28) {
recoveryParam -= 8 + 2 * chainId;
}
if (recoveryParam === 0 || recoveryParam === 1) {
recoveryParam += 27;
}
const v = Buffer.from(recoveryParam.toString(16), "hex");
const signature = utils.toHex(Buffer.concat([r, s, v]), false);
return signature;
}
else {
throw new Error(`Failed to sign EVM transaction on trezor: ${response.payload.error}, code ${response.payload.code}`);
}
}
function _initialize() {
if (!initialized)
connect_web_1.default.init({
lazyLoad: true,
manifest: {
email: "developer@xyz.com",
appUrl: "http://your.application.com",
appName: "flare-stake-tool",
},
})
.then(() => {
initialized = true;
})
.catch((error) => {
throw new Error(`Failed to initialize Trezor Connect: ${error}`);
});
}
//# sourceMappingURL=index.js.map