@@ -63,6 +63,7 @@ Object.defineProperty(exports, 'toXOnly', {
6363} ) ;
6464const psbtutils_js_1 = require ( './psbt/psbtutils.cjs' ) ;
6565const witness_js_1 = require ( './psbt/internal/witness.cjs' ) ;
66+ const finalize_js_1 = require ( './psbt/internal/finalize.cjs' ) ;
6667const scriptType_js_1 = require ( './psbt/internal/scriptType.cjs' ) ;
6768const tools = __importStar ( require ( 'uint8array-tools' ) ) ;
6869/**
@@ -366,7 +367,11 @@ class Psbt {
366367 ) ;
367368 throw new Error ( `Cannot finalize input #${ inputIndex } . Not Taproot.` ) ;
368369 }
369- _finalizeInput ( inputIndex , input , finalScriptsFunc = getFinalScripts ) {
370+ _finalizeInput (
371+ inputIndex ,
372+ input ,
373+ finalScriptsFunc = finalize_js_1 . getFinalScripts ,
374+ ) {
370375 const { script, isP2SH, isP2WSH, isSegwit } = getScriptFromInput (
371376 inputIndex ,
372377 input ,
@@ -1039,42 +1044,11 @@ class PsbtTransaction {
10391044 return this . tx . toBuffer ( ) ;
10401045 }
10411046}
1042- function canFinalize ( input , script , scriptType ) {
1043- switch ( scriptType ) {
1044- case 'pubkey' :
1045- case 'pubkeyhash' :
1046- case 'witnesspubkeyhash' :
1047- return hasSigs ( 1 , input . partialSig ) ;
1048- case 'multisig' :
1049- const p2ms = payments . p2ms ( { output : script } ) ;
1050- return hasSigs ( p2ms . m , input . partialSig , p2ms . pubkeys ) ;
1051- default :
1052- return false ;
1053- }
1054- }
10551047function checkCache ( cache ) {
10561048 if ( cache . __UNSAFE_SIGN_NONSEGWIT !== false ) {
10571049 throw new Error ( 'Not BIP174 compliant, can not export' ) ;
10581050 }
10591051}
1060- function hasSigs ( neededSigs , partialSig , pubkeys ) {
1061- if ( ! partialSig ) return false ;
1062- let sigs ;
1063- if ( pubkeys ) {
1064- sigs = pubkeys
1065- . map ( pkey => {
1066- const pubkey = compressPubkey ( pkey ) ;
1067- return partialSig . find (
1068- pSig => tools . compare ( pSig . pubkey , pubkey ) === 0 ,
1069- ) ;
1070- } )
1071- . filter ( v => ! ! v ) ;
1072- } else {
1073- sigs = partialSig ;
1074- }
1075- if ( sigs . length > neededSigs ) throw new Error ( 'Too many signatures' ) ;
1076- return sigs . length === neededSigs ;
1077- }
10781052function isFinalized ( input ) {
10791053 return ! ! input . finalScriptSig || ! ! input . finalScriptWitness ;
10801054}
@@ -1213,61 +1187,6 @@ function getTxCacheValue(key, name, inputs, c) {
12131187 if ( key === '__FEE_RATE' ) return c . __FEE_RATE ;
12141188 else if ( key === '__FEE' ) return c . __FEE ;
12151189}
1216- function getFinalScripts ( inputIndex , input , script , isSegwit , isP2SH , isP2WSH ) {
1217- const scriptType = ( 0 , scriptType_js_1 . classifyScript ) (
1218- script ,
1219- SCRIPT_TYPE_DEPS ,
1220- ) ;
1221- if ( ! canFinalize ( input , script , scriptType ) )
1222- throw new Error ( `Can not finalize input #${ inputIndex } ` ) ;
1223- return prepareFinalScripts (
1224- script ,
1225- scriptType ,
1226- input . partialSig ,
1227- isSegwit ,
1228- isP2SH ,
1229- isP2WSH ,
1230- ) ;
1231- }
1232- function prepareFinalScripts (
1233- script ,
1234- scriptType ,
1235- partialSig ,
1236- isSegwit ,
1237- isP2SH ,
1238- isP2WSH ,
1239- ) {
1240- let finalScriptSig ;
1241- let finalScriptWitness ;
1242- // Wow, the payments API is very handy
1243- const payment = getPayment ( script , scriptType , partialSig ) ;
1244- const p2wsh = ! isP2WSH ? null : payments . p2wsh ( { redeem : payment } ) ;
1245- const p2sh = ! isP2SH ? null : payments . p2sh ( { redeem : p2wsh || payment } ) ;
1246- if ( isSegwit ) {
1247- if ( p2wsh ) {
1248- finalScriptWitness = ( 0 , psbtutils_js_1 . witnessStackToScriptWitness ) (
1249- p2wsh . witness ,
1250- ) ;
1251- } else {
1252- finalScriptWitness = ( 0 , psbtutils_js_1 . witnessStackToScriptWitness ) (
1253- payment . witness ,
1254- ) ;
1255- }
1256- if ( p2sh ) {
1257- finalScriptSig = p2sh . input ;
1258- }
1259- } else {
1260- if ( p2sh ) {
1261- finalScriptSig = p2sh . input ;
1262- } else {
1263- finalScriptSig = payment . input ;
1264- }
1265- }
1266- return {
1267- finalScriptSig,
1268- finalScriptWitness,
1269- } ;
1270- }
12711190function getHashAndSighashType (
12721191 inputs ,
12731192 inputIndex ,
@@ -1472,39 +1391,6 @@ function checkSighashTypeAllowed(sighashType, sighashTypes) {
14721391 ) ;
14731392 }
14741393}
1475- function getPayment ( script , scriptType , partialSig ) {
1476- let payment ;
1477- switch ( scriptType ) {
1478- case 'multisig' :
1479- const sigs = getSortedSigs ( script , partialSig ) ;
1480- payment = payments . p2ms ( {
1481- output : script ,
1482- signatures : sigs ,
1483- } ) ;
1484- break ;
1485- case 'pubkey' :
1486- payment = payments . p2pk ( {
1487- output : script ,
1488- signature : partialSig [ 0 ] . signature ,
1489- } ) ;
1490- break ;
1491- case 'pubkeyhash' :
1492- payment = payments . p2pkh ( {
1493- output : script ,
1494- pubkey : partialSig [ 0 ] . pubkey ,
1495- signature : partialSig [ 0 ] . signature ,
1496- } ) ;
1497- break ;
1498- case 'witnesspubkeyhash' :
1499- payment = payments . p2wpkh ( {
1500- output : script ,
1501- pubkey : partialSig [ 0 ] . pubkey ,
1502- signature : partialSig [ 0 ] . signature ,
1503- } ) ;
1504- break ;
1505- }
1506- return payment ;
1507- }
15081394function getScriptFromInput ( inputIndex , input , cache ) {
15091395 const unsignedTx = cache . __TX ;
15101396 const res = {
@@ -1565,22 +1451,6 @@ function getSignersFromHD(inputIndex, inputs, hdKeyPair) {
15651451 } ) ;
15661452 return signers ;
15671453}
1568- function getSortedSigs ( script , partialSig ) {
1569- const p2ms = payments . p2ms ( { output : script } ) ;
1570- // for each pubkey in order of p2ms script
1571- return p2ms . pubkeys
1572- . map ( pk => {
1573- // filter partialSig array by pubkey being equal
1574- return (
1575- partialSig . filter ( ps => {
1576- return tools . compare ( ps . pubkey , pk ) === 0 ;
1577- } ) [ 0 ] || { }
1578- ) . signature ;
1579- // Any pubkey without a match will return undefined
1580- // this last filter removes all the undefined items in the array.
1581- } )
1582- . filter ( v => ! ! v ) ;
1583- }
15841454function sighashTypeToString ( sighashType ) {
15851455 let text =
15861456 sighashType & transaction_js_1 . Transaction . SIGHASH_ANYONECANPAY
@@ -1731,15 +1601,6 @@ function redeemFromFinalWitnessScript(finalScript) {
17311601 if ( ! sDecomp ) return ;
17321602 return lastItem ;
17331603}
1734- function compressPubkey ( pubkey ) {
1735- if ( pubkey . length === 65 ) {
1736- const parity = pubkey [ 64 ] & 1 ;
1737- const newKey = pubkey . slice ( 0 , 33 ) ;
1738- newKey [ 0 ] = 2 | parity ;
1739- return newKey ;
1740- }
1741- return pubkey . slice ( ) ;
1742- }
17431604function isPubkeyLike ( buf ) {
17441605 return buf . length === 33 && bscript . isCanonicalPubKey ( buf ) ;
17451606}
0 commit comments