UNPKG

@ledgerhq/hw-app-btc

Version:
75 lines 3.63 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getDerivationAccessors = getDerivationAccessors; exports.checkElementBip32Derivation = checkElementBip32Derivation; exports.extractAccountPath = extractAccountPath; exports.checkBip32Derivation = checkBip32Derivation; exports.checkOutputBip32Derivation = checkOutputBip32Derivation; const psbtv2_1 = require("@ledgerhq/psbtv2"); /** * Returns accessors for BIP32 derivation operations based on element type. */ function getDerivationAccessors(psbt, type) { if (type === "input") { return { getKeyDatas: (i, kt) => psbt.getInputKeyDatas(i, kt), getBip32Derivation: (i, pk) => psbt.getInputBip32Derivation(i, pk), getTapBip32Derivation: (i, pk) => psbt.getInputTapBip32Derivation(i, pk), setBip32Derivation: (i, pk, fp, p) => psbt.setInputBip32Derivation(i, pk, fp, p), setTapBip32Derivation: (i, pk, h, fp, p) => psbt.setInputTapBip32Derivation(i, pk, h, fp, p), bip32KeyType: psbtv2_1.psbtIn.BIP32_DERIVATION, tapBip32KeyType: psbtv2_1.psbtIn.TAP_BIP32_DERIVATION, }; } return { getKeyDatas: (i, kt) => psbt.getOutputKeyDatas(i, kt), getBip32Derivation: (i, pk) => psbt.getOutputBip32Derivation(i, pk), getTapBip32Derivation: (i, pk) => psbt.getOutputTapBip32Derivation(i, pk), setBip32Derivation: (i, pk, fp, p) => psbt.setOutputBip32Derivation(i, pk, fp, p), setTapBip32Derivation: (i, pk, h, fp, p) => psbt.setOutputTapBip32Derivation(i, pk, h, fp, p), bip32KeyType: psbtv2_1.psbtOut.BIP_32_DERIVATION, tapBip32KeyType: psbtv2_1.psbtOut.TAP_BIP32_DERIVATION, }; } /** * Generic method to check BIP32 derivation for either an input or output. */ function checkElementBip32Derivation(accessors, elementIndex, masterFp) { const keyDatas = accessors.getKeyDatas(elementIndex, accessors.bip32KeyType); for (const pubkey of keyDatas) { const derivationInfo = accessors.getBip32Derivation(elementIndex, pubkey); if (derivationInfo?.masterFingerprint.equals(masterFp)) { return extractAccountPath(derivationInfo.path); } } const tapKeyDatas = accessors.getKeyDatas(elementIndex, accessors.tapBip32KeyType); for (const pubkey of tapKeyDatas) { const derivationInfo = accessors.getTapBip32Derivation(elementIndex, pubkey); if (derivationInfo?.masterFingerprint.equals(masterFp)) { return extractAccountPath(derivationInfo.path); } } return { belongsToSigner: false, accountPath: [] }; } /** * Returns belongsToSigner: true because this function is only called after * a master fingerprint match against the connected signer (hardware wallet). * This is unrelated to the BIP44 "internal chain" (change = 1) concept. */ function extractAccountPath(fullPath) { const accountPath = fullPath.length >= 2 ? fullPath.slice(0, -2) : []; return { belongsToSigner: true, accountPath }; } function checkBip32Derivation(psbt, inputIndex, masterFp) { const accessors = getDerivationAccessors(psbt, "input"); return checkElementBip32Derivation(accessors, inputIndex, masterFp); } /** * Checks if an output has a valid BIP32 derivation with the correct master fingerprint. */ function checkOutputBip32Derivation(psbt, outputIndex, masterFp) { const accessors = getDerivationAccessors(psbt, "output"); const result = checkElementBip32Derivation(accessors, outputIndex, masterFp); return result.belongsToSigner; } //# sourceMappingURL=derivationAccessors.js.map