@ledgerhq/hw-app-helium
Version:
Ledger Hardware Wallet Helium Application API
171 lines • 6.45 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.serializeUnstakeValidatorV1 = exports.serializeTransferValidatorStakeV1 = exports.serializeStakeValidatorV1 = exports.serializeTokenBurnV1 = exports.serializeSecurityExchangeV1 = exports.serializePaymentV2 = exports.pathToBuffer = void 0;
const transactions_1 = require("@helium/transactions");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
const bip32_path_1 = __importDefault(require("bip32-path"));
const long_1 = __importDefault(require("long"));
const tokenTypeToInt = tokenType => {
if (!tokenType)
return 0;
switch (tokenType) {
default:
case "hnt":
return transactions_1.TokenType.hnt;
case "hst":
return transactions_1.TokenType.hst;
case "mobile":
return transactions_1.TokenType.mobile;
case "iot":
return transactions_1.TokenType.iot;
}
};
const serializePath = (path) => {
const buf = Buffer.alloc(1 + path.length * 4);
buf.writeUInt8(path.length, 0);
for (const [i, num] of path.entries()) {
buf.writeUInt32BE(num, 1 + i * 4);
}
return buf;
};
const pathToBuffer = (originalPath) => {
const path = originalPath
.split("/")
.map(value => (value.endsWith("'") || value.endsWith("h") ? value : value + "'"))
.join("/");
const pathNums = bip32_path_1.default.fromString(path).toPathArray();
return serializePath(pathNums);
};
exports.pathToBuffer = pathToBuffer;
const serializeNumber = (amount) => {
let hex = new bignumber_js_1.default(amount ?? 0).toString(16);
hex = hex.length % 2 ? `0${hex}` : hex;
const len = Math.floor(hex.length / 2);
if (len > 8)
throw "Invalid transaction.";
const u8 = new Uint8Array(8);
for (let i = 0; i < len; i += 1)
u8[len - i - 1] = parseInt(hex.slice(i * 2, i * 2 + 2), 16);
return Buffer.from(u8);
};
const serializeMemo = (memo) => {
const memoBuffer = Buffer.from(memo, "base64");
const memoLong = long_1.default.fromBytes(Array.from(memoBuffer), true, true);
return Buffer.from(memoLong.toBytesLE());
};
const serializePaymentV2 = (txn) => {
if (txn.payments.length > 1)
throw "multiple payments are not supported";
const { amount, memo } = txn.payments[0];
const payee = txn.payments[0].payee;
const tokenType = tokenTypeToInt(txn.payments[0].tokenType);
const txSerialized = Buffer.concat([
serializeNumber(amount),
serializeNumber(txn.fee),
serializeNumber(txn.nonce),
Buffer.from([payee.version]),
Buffer.from([payee.netType | payee.keyType]),
Buffer.from(payee.publicKey),
serializeMemo(memo || ""),
serializeNumber(tokenType),
]);
return Buffer.from(txSerialized);
};
exports.serializePaymentV2 = serializePaymentV2;
const serializeSecurityExchangeV1 = (txn) => {
if (!txn.payee)
throw "Payee required";
const payee = txn.payee;
const txSerialized = Buffer.concat([
serializeNumber(txn.amount),
serializeNumber(txn.fee),
serializeNumber(txn.nonce),
Buffer.from([payee.version]),
Buffer.from([payee.netType | payee.keyType]),
Buffer.from(payee.publicKey),
]);
return Buffer.from(txSerialized);
};
exports.serializeSecurityExchangeV1 = serializeSecurityExchangeV1;
const serializeTokenBurnV1 = (txn) => {
if (!txn.payee)
throw "Payee required";
const payee = txn.payee;
const txSerialized = Buffer.concat([
serializeNumber(txn.amount),
serializeNumber(txn.fee),
serializeNumber(txn.nonce),
serializeMemo(txn.memo),
Buffer.from([payee.version]),
Buffer.from([payee.netType | payee.keyType]),
Buffer.from(payee.publicKey),
]);
return Buffer.from(txSerialized);
};
exports.serializeTokenBurnV1 = serializeTokenBurnV1;
const serializeStakeValidatorV1 = (txn) => {
if (!txn.address)
throw "Address required";
const address = txn.address;
const txSerialized = Buffer.concat([
serializeNumber(txn.stake),
serializeNumber(txn.fee),
Buffer.from([address.version]),
Buffer.from([address.netType | address.keyType]),
Buffer.from(address.publicKey),
]);
return Buffer.from(txSerialized);
};
exports.serializeStakeValidatorV1 = serializeStakeValidatorV1;
const serializeTransferValidatorStakeV1 = (txn) => {
if (!txn.newOwner)
throw "New owner required";
if (!txn.oldOwner)
throw "Old owner required";
if (!txn.newAddress)
throw "New address required";
if (!txn.oldAddress)
throw "Old address required";
const newOwner = txn.newOwner;
const oldOwner = txn.oldOwner;
const newAddress = txn.newAddress;
const oldAddress = txn.oldAddress;
const txSerialized = Buffer.concat([
serializeNumber(txn.stakeAmount),
serializeNumber(txn.paymentAmount),
serializeNumber(txn.fee),
Buffer.from([newOwner.version]),
Buffer.from([newOwner.keyType]),
Buffer.from(newOwner.publicKey),
Buffer.from([oldOwner.version]),
Buffer.from([oldOwner.keyType]),
Buffer.from(oldOwner.publicKey),
Buffer.from([newAddress.version]),
Buffer.from([newAddress.keyType]),
Buffer.from(newAddress.publicKey),
Buffer.from([oldAddress.version]),
Buffer.from([oldAddress.netType | oldAddress.keyType]),
Buffer.from(oldAddress.publicKey),
]);
return Buffer.from(txSerialized);
};
exports.serializeTransferValidatorStakeV1 = serializeTransferValidatorStakeV1;
const serializeUnstakeValidatorV1 = (txn) => {
if (!txn.address)
throw "Address required";
const address = txn.address;
const txSerialized = Buffer.concat([
serializeNumber(txn.stakeAmount),
serializeNumber(txn.stakeReleaseHeight),
serializeNumber(txn.fee),
Buffer.from([address.version]),
Buffer.from([address.netType | address.keyType]),
Buffer.from(address.publicKey),
]);
return Buffer.from(txSerialized);
};
exports.serializeUnstakeValidatorV1 = serializeUnstakeValidatorV1;
//# sourceMappingURL=serialization.js.map