@ledgerhq/coin-cardano
Version:
Ledger Cardano Coin integration
65 lines • 2.02 kB
JavaScript
import invariant from "invariant";
import flatMap from "lodash/flatMap";
import { getAccountCurrency } from "@ledgerhq/coin-framework/account/index";
const options = [
{
name: "mode",
type: String,
desc: "mode of transaction: send, delegate, undelegate",
},
{
name: "token",
type: String,
desc: "uses subAccount(TokenAccount) of the account",
},
{
name: "poolId",
type: String,
desc: "Cardano stake pool id to delegate",
},
];
function inferAccounts(account, opts) {
invariant(account.currency.family === "cardano", "cardano family");
if (!opts.token) {
const accounts = [account];
return accounts;
}
return opts.token.map((token) => {
const subAccounts = account.subAccounts || [];
const subAccount = subAccounts.find(a => {
const currency = getAccountCurrency(a);
return token.toLowerCase() === currency.id.toLowerCase();
});
if (!subAccount) {
throw new Error("token account '" +
token +
"' not found. Available: " +
subAccounts.map(t => t.id).join("\n"));
}
return subAccount;
});
}
function inferTransactions(transactions, opts) {
return flatMap(transactions, ({ transaction, account }) => {
if (transaction.family !== "cardano") {
throw new Error("transaction is not of type cardano");
}
if (account.type === "Account" && !account.cardanoResources) {
throw new Error("unactivated account");
}
return {
...transaction,
mode: opts.mode || "send",
subAccountId: account.type === "TokenAccount" ? account.id : undefined,
poolId: opts.poolId || "",
};
});
}
export default function makeCliTools() {
return {
options,
inferAccounts,
inferTransactions,
};
}
//# sourceMappingURL=cli-transaction.js.map