@ledgerhq/hw-app-btc
Version:
Ledger Hardware Wallet Bitcoin Application API
59 lines • 2.12 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.createWalletPolicy = createWalletPolicy;
exports.createProgressCallback = createProgressCallback;
exports.finalizePsbtAndExtract = finalizePsbtAndExtract;
const policy_1 = require("../newops/policy");
const psbtExtractor_1 = require("../newops/psbtExtractor");
const psbtFinalizer_1 = require("../newops/psbtFinalizer");
/**
* Creates a WalletPolicy for the given account.
*/
function createWalletPolicy(masterFp, accountPath, accountXpub, accountType) {
const key = (0, policy_1.createKey)(masterFp, accountPath, accountXpub);
return new policy_1.WalletPolicy(accountType.getDescriptorTemplate(), key);
}
/**
* Creates a progress callback that notifies onDeviceStreaming and onDeviceSignatureGranted.
*/
function createProgressCallback(inputCount, options) {
let notifyCount = 0;
let firstSigned = false;
const progress = () => {
if (!options.onDeviceStreaming || inputCount <= 0)
return;
options.onDeviceStreaming({
total: 2 * inputCount,
index: notifyCount,
progress: ++notifyCount / (2 * inputCount),
});
};
if (options.onDeviceSignatureRequested)
options.onDeviceSignatureRequested();
return () => {
if (!firstSigned) {
firstSigned = true;
if (options.onDeviceSignatureGranted)
options.onDeviceSignatureGranted();
}
progress();
};
}
/**
* Optionally finalizes the PSBT and, when finalizing, extracts the transaction.
* When not finalizing, only the serialized PSBT (with partial signatures) is
* returned; extraction is not performed because FINAL_SCRIPTWITNESS is not set.
*/
function finalizePsbtAndExtract(psbt, shouldFinalize) {
if (shouldFinalize) {
(0, psbtFinalizer_1.finalize)(psbt);
return {
psbt: psbt.serialize(),
tx: (0, psbtExtractor_1.extract)(psbt).toString("hex"),
};
}
return {
psbt: psbt.serialize(),
};
}
//# sourceMappingURL=signAndFinalize.js.map