@ledgerhq/coin-multiversx
Version:
Ledger MultiversX Coin integration
77 lines • 2.76 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.extractTokenId = exports.addPrefixToken = exports.computeDelegationBalance = exports.isAmountSpentFromBalance = exports.isSelfTransaction = exports.isValidAddress = void 0;
const bech32_1 = require("bech32");
const bignumber_js_1 = __importDefault(require("bignumber.js"));
/**
* The human-readable-part of the bech32 addresses.
*/
const HRP = "erd";
/**
* The length (in bytes) of a public key (from which a bech32 address can be obtained).
*/
const PUBKEY_LENGTH = 32;
function fromBech32(value) {
let decoded;
try {
decoded = (0, bech32_1.decode)(value);
}
catch {
throw new Error("Erd address can't be created");
}
const prefix = decoded.prefix;
if (prefix !== HRP) {
throw new Error("Bad HRP");
}
const pubkey = Buffer.from((0, bech32_1.fromWords)(decoded.words));
if (pubkey.length !== PUBKEY_LENGTH) {
throw new Error("Erd address can't be created");
}
return value;
}
/**
* Returns true if address is a valid bech32
*
* @param {string} address
*/
const isValidAddress = (address) => {
try {
fromBech32(address);
return true;
}
catch {
return false;
}
};
exports.isValidAddress = isValidAddress;
const isSelfTransaction = (a, t) => {
return t.recipient === a.freshAddress;
};
exports.isSelfTransaction = isSelfTransaction;
// For some transaction modes the amount doesn't belong to the account's balance
const isAmountSpentFromBalance = (mode) => {
return ["send", "delegate"].includes(mode);
};
exports.isAmountSpentFromBalance = isAmountSpentFromBalance;
const computeDelegationBalance = (delegations) => {
let totalDelegationBalance = new bignumber_js_1.default(0);
for (const delegation of delegations) {
let delegationBalance = new bignumber_js_1.default(delegation.userActiveStake).plus(new bignumber_js_1.default(delegation.claimableRewards));
for (const undelegation of delegation.userUndelegatedList) {
delegationBalance = delegationBalance.plus(new bignumber_js_1.default(undelegation.amount));
}
totalDelegationBalance = totalDelegationBalance.plus(delegationBalance);
}
return totalDelegationBalance;
};
exports.computeDelegationBalance = computeDelegationBalance;
const addPrefixToken = (tokenId) => `multiversx/esdt/${tokenId}`;
exports.addPrefixToken = addPrefixToken;
const extractTokenId = (tokenId) => {
return tokenId.split("/")[2];
};
exports.extractTokenId = extractTokenId;
//# sourceMappingURL=logic.js.map