@ledgerhq/hw-app-btc
Version:
Ledger Hardware Wallet Bitcoin Application API
54 lines • 1.83 kB
JavaScript
import { createKey, WalletPolicy } from "../newops/policy";
import { extract } from "../newops/psbtExtractor";
import { finalize } from "../newops/psbtFinalizer";
/**
* Creates a WalletPolicy for the given account.
*/
export function createWalletPolicy(masterFp, accountPath, accountXpub, accountType) {
const key = createKey(masterFp, accountPath, accountXpub);
return new WalletPolicy(accountType.getDescriptorTemplate(), key);
}
/**
* Creates a progress callback that notifies onDeviceStreaming and onDeviceSignatureGranted.
*/
export 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.
*/
export function finalizePsbtAndExtract(psbt, shouldFinalize) {
if (shouldFinalize) {
finalize(psbt);
return {
psbt: psbt.serialize(),
tx: extract(psbt).toString("hex"),
};
}
return {
psbt: psbt.serialize(),
};
}
//# sourceMappingURL=signAndFinalize.js.map