UNPKG

@ledgerhq/hw-app-helium

Version:
158 lines 5.45 kB
import { TokenType, } from "@helium/transactions"; import BigNumber from "bignumber.js"; import BIPPath from "bip32-path"; import JSLong from "long"; const tokenTypeToInt = tokenType => { if (!tokenType) return 0; switch (tokenType) { default: case "hnt": return TokenType.hnt; case "hst": return TokenType.hst; case "mobile": return TokenType.mobile; case "iot": return 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; }; export const pathToBuffer = (originalPath) => { const path = originalPath .split("/") .map(value => (value.endsWith("'") || value.endsWith("h") ? value : value + "'")) .join("/"); const pathNums = BIPPath.fromString(path).toPathArray(); return serializePath(pathNums); }; const serializeNumber = (amount) => { let hex = new BigNumber(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 = JSLong.fromBytes(Array.from(memoBuffer), true, true); return Buffer.from(memoLong.toBytesLE()); }; export 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); }; export 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); }; export 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); }; export 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); }; export 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); }; export 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); }; //# sourceMappingURL=serialization.js.map