@ledgerhq/coin-algorand
Version:
Ledger Algorand Coin integration
55 lines • 2.67 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.encodeToBroadcast = exports.encodeToSign = exports.buildTransactionPayload = void 0;
const algo_msgpack_with_bigint_1 = require("algo-msgpack-with-bigint");
const algosdk_1 = require("algosdk");
const api_1 = __importDefault(require("./api"));
const tokens_1 = require("./tokens");
const buildTransactionPayload = async (account, transaction) => {
const { amount, recipient, mode, memo, assetId, subAccountId } = transaction;
const subAccount = subAccountId
? account.subAccounts && account.subAccounts.find(t => t.id === subAccountId)
: null;
const note = memo ? new TextEncoder().encode(memo) : undefined;
const params = await api_1.default.getTransactionParams();
let algoTxn;
if (subAccount || (assetId && mode === "optIn")) {
const targetAssetId = subAccount && subAccount.type === "TokenAccount"
? (0, tokens_1.extractTokenId)(subAccount.token.id)
: assetId
? (0, tokens_1.extractTokenId)(assetId)
: "";
if (!targetAssetId) {
throw new Error("Token Asset Id not found");
}
algoTxn = (0, algosdk_1.makeAssetTransferTxnWithSuggestedParams)(account.freshAddress, recipient, undefined, undefined, amount.toNumber(), note, Number(targetAssetId), params, undefined);
}
else {
algoTxn = (0, algosdk_1.makePaymentTxnWithSuggestedParams)(account.freshAddress, recipient, amount.toNumber(), undefined, note, params);
}
// Bit of safety: set tx validity to the next 1000 blocks
algoTxn.firstRound = params.lastRound;
algoTxn.lastRound = params.lastRound + 1000;
// Flaw in the SDK: payload isn't sorted, but it needs to be for msgPack encoding
const sorted = Object.fromEntries(Object.entries(algoTxn.get_obj_for_encoding()).sort());
return sorted;
};
exports.buildTransactionPayload = buildTransactionPayload;
const encodeToSign = (payload) => {
const msgPackEncoded = (0, algo_msgpack_with_bigint_1.encode)(payload);
return Buffer.from(msgPackEncoded).toString("hex");
};
exports.encodeToSign = encodeToSign;
const encodeToBroadcast = (payload, signature) => {
const signedPayload = {
sig: signature,
txn: payload,
};
const msgPackEncoded = (0, algo_msgpack_with_bigint_1.encode)(signedPayload);
return Buffer.from(msgPackEncoded);
};
exports.encodeToBroadcast = encodeToBroadcast;
//# sourceMappingURL=buildTransaction.js.map