@ledgerhq/coin-canton
Version:
82 lines • 3.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.buildSignOperation = void 0;
const errors_1 = require("@ledgerhq/ledger-wallet-framework/errors");
const account_1 = require("@ledgerhq/ledger-wallet-framework/account");
const operation_1 = require("@ledgerhq/ledger-wallet-framework/operation");
const rxjs_1 = require("rxjs");
const common_logic_1 = require("../common-logic");
const sign_1 = require("../common-logic/transaction/sign");
const buildSignOperation = (signerContext) => ({ account, deviceId, transaction }) => new rxjs_1.Observable(o => {
async function main() {
const { fee } = transaction;
if (!fee)
throw new errors_1.FeeNotLoaded();
try {
// o observables allows to define steps of the signing process with the device
o.next({
type: "device-signature-requested",
});
let transactionHash = "";
const signature = await signerContext(deviceId, async (signer) => {
const { id, freshAddressPath: derivationPath, xpub } = account;
const address = xpub ?? (0, account_1.decodeAccountId)(id).xpubOrAddress;
const params = {
recipient: transaction.recipient,
amount: transaction.amount,
expireInSeconds: transaction.expireInSeconds ?? 24 * 60 * 60, // Default to 1 day
tokenId: transaction.tokenId,
};
if (transaction.instrumentAdmin) {
params.instrumentAdmin = transaction.instrumentAdmin;
}
if (transaction.memo) {
params.memo = transaction.memo;
}
const { nativeTransaction, serializedTransaction, hash } = await (0, common_logic_1.craftTransaction)(account.currency, {
address,
}, params);
transactionHash = hash || "";
const signatureResult = await (0, sign_1.signTransaction)(signer, derivationPath, nativeTransaction);
return (0, common_logic_1.combine)(serializedTransaction, `${signatureResult.signature}__PARTY__${address}`);
});
o.next({
type: "device-signature-granted",
});
// We create an optimistic operation here, the framework will then replace this transaction with the one returned by the indexer
const hash = transactionHash;
const operation = {
id: (0, operation_1.encodeOperationId)(account.id, hash, "OUT"),
hash,
accountId: account.id,
type: "OUT",
value: transaction.amount,
fee,
blockHash: null,
blockHeight: null,
senders: [account.freshAddress],
recipients: [transaction.recipient],
date: new Date(),
extra: {},
};
o.next({
type: "signed",
signedOperation: {
operation,
signature,
},
});
}
catch (e) {
if (e instanceof Error) {
const errorMessage = e?.data?.resultMessage ||
e.message;
throw new Error(errorMessage);
}
throw e;
}
}
main().then(() => o.complete(), e => o.error(e));
});
exports.buildSignOperation = buildSignOperation;
//# sourceMappingURL=signOperation.js.map