@@ -1409,12 +1409,13 @@ class CoinSelector {
14091409 /**
14101410 * Create a coin selector.
14111411 * @constructor
1412- * @param {TX } tx
1412+ * @param {MTX } tx
14131413 * @param {Object? } options
14141414 */
14151415
14161416 constructor ( tx , options ) {
14171417 this . tx = tx . clone ( ) ;
1418+ this . view = tx . view ;
14181419 this . coins = [ ] ;
14191420 this . outputValue = 0 ;
14201421 this . index = 0 ;
@@ -1538,11 +1539,13 @@ class CoinSelector {
15381539
15391540 if ( options . inputs ) {
15401541 assert ( Array . isArray ( options . inputs ) ) ;
1542+
1543+ const lastIndex = this . inputs . size ;
15411544 for ( let i = 0 ; i < options . inputs . length ; i ++ ) {
15421545 const prevout = options . inputs [ i ] ;
15431546 assert ( prevout && typeof prevout === 'object' ) ;
15441547 const { hash, index} = prevout ;
1545- this . inputs . set ( Outpoint . toKey ( hash , index ) , i ) ;
1548+ this . inputs . set ( Outpoint . toKey ( hash , index ) , lastIndex + i ) ;
15461549 }
15471550 }
15481551
@@ -1676,31 +1679,7 @@ class CoinSelector {
16761679
16771680 fund ( ) {
16781681 // Ensure all preferred inputs first.
1679- if ( this . inputs . size > 0 ) {
1680- const coins = [ ] ;
1681-
1682- for ( let i = 0 ; i < this . inputs . size ; i ++ )
1683- coins . push ( null ) ;
1684-
1685- for ( const coin of this . coins ) {
1686- const { hash, index} = coin ;
1687- const key = Outpoint . toKey ( hash , index ) ;
1688- const i = this . inputs . get ( key ) ;
1689-
1690- if ( i != null ) {
1691- coins [ i ] = coin ;
1692- this . inputs . delete ( key ) ;
1693- }
1694- }
1695-
1696- if ( this . inputs . size > 0 )
1697- throw new Error ( 'Could not resolve preferred inputs.' ) ;
1698-
1699- for ( const coin of coins ) {
1700- this . tx . addCoin ( coin ) ;
1701- this . chosen . push ( coin ) ;
1702- }
1703- }
1682+ this . resolveInputCoins ( ) ;
17041683
17051684 if ( this . isFull ( ) )
17061685 return ;
@@ -1803,6 +1782,56 @@ class CoinSelector {
18031782 this . fee = this . hardFee ;
18041783 this . fund ( ) ;
18051784 }
1785+
1786+ resolveInputCoins ( ) {
1787+ if ( this . inputs . size === 0 )
1788+ return ;
1789+
1790+ const coins = [ ] ;
1791+
1792+ for ( let i = 0 ; i < this . inputs . size ; i ++ ) {
1793+ coins . push ( null ) ;
1794+ }
1795+
1796+ // first resolve from coinview if possible.
1797+ for ( const key of this . inputs . keys ( ) ) {
1798+ const prevout = Outpoint . fromKey ( key ) ;
1799+
1800+ if ( this . view . hasEntry ( prevout ) ) {
1801+ const coinEntry = this . view . getEntry ( prevout ) ;
1802+ const i = this . inputs . get ( key ) ;
1803+
1804+ if ( i != null ) {
1805+ assert ( ! coins [ i ] ) ;
1806+ coins [ i ] = coinEntry . toCoin ( prevout ) ;
1807+ this . inputs . delete ( key ) ;
1808+ }
1809+ }
1810+ }
1811+
1812+ // Now try to resolve from the passed coins array.
1813+ if ( this . inputs . size > 0 ) {
1814+ for ( const coin of this . coins ) {
1815+ const { hash, index} = coin ;
1816+ const key = Outpoint . toKey ( hash , index ) ;
1817+ const i = this . inputs . get ( key ) ;
1818+
1819+ if ( i != null ) {
1820+ assert ( ! coins [ i ] ) ;
1821+ coins [ i ] = coin ;
1822+ this . inputs . delete ( key ) ;
1823+ }
1824+ }
1825+ }
1826+
1827+ if ( this . inputs . size > 0 )
1828+ throw new Error ( 'Could not resolve preferred inputs.' ) ;
1829+
1830+ for ( const coin of coins ) {
1831+ this . tx . addCoin ( coin ) ;
1832+ this . chosen . push ( coin ) ;
1833+ }
1834+ }
18061835}
18071836
18081837/**
0 commit comments