@@ -8,6 +8,48 @@ const scriptTypes = ["p2sh", "p2shP2wsh", "p2wsh"] as const;
88const scope = [ "external" , "internal" ] as const ;
99const index = [ 0 , 1 , 2 ] ;
1010
11+ /** Get the expected max weight to satisfy the descriptor */
12+ function getExpectedMaxWeightToSatisfy ( scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ) {
13+ switch ( scriptType ) {
14+ case "p2sh" :
15+ return 256 ;
16+ case "p2shP2wsh" :
17+ return 99 ;
18+ case "p2wsh" :
19+ return 64 ;
20+ default :
21+ throw new Error ( "unexpected script type" ) ;
22+ }
23+ }
24+
25+ /** Compute the total size of the input, including overhead */
26+ function getTotalInputSize ( vSize : number ) {
27+ const sizeOpPushData1 = 1 ;
28+ const sizeOpPushData2 = 2 ;
29+ return (
30+ 32 /* txid */ +
31+ 4 /* vout */ +
32+ 4 /* nSequence */ +
33+ ( vSize < 255 ? sizeOpPushData1 : sizeOpPushData2 ) +
34+ vSize
35+ ) ;
36+ }
37+
38+ /** Get the full expected vSize of the input including overhead */
39+ function getExpectedVSize ( scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ) {
40+ // https://github.com/BitGo/BitGoJS/blob/master/modules/unspents/docs/input-costs.md
41+ switch ( scriptType ) {
42+ case "p2sh" :
43+ return 298 ;
44+ case "p2shP2wsh" :
45+ return 140 ;
46+ case "p2wsh" :
47+ return 105 ;
48+ default :
49+ throw new Error ( "unexpected script type" ) ;
50+ }
51+ }
52+
1153function runTest (
1254 scriptType : utxolib . bitgo . outputScripts . ScriptType2Of3 ,
1355 index : number ,
@@ -24,11 +66,16 @@ function runTest(
2466 scriptType ,
2567 ) . scriptPubKey ;
2668
27- it ( "descriptor should have expected format" , function ( ) {
28- const descriptor = Descriptor . fromString (
69+ let descriptor : Descriptor ;
70+
71+ before ( function ( ) {
72+ descriptor = Descriptor . fromString (
2973 getDescriptorForScriptType ( rootWalletKeys , scriptType , scope ) ,
3074 "derivable" ,
3175 ) ;
76+ } ) ;
77+
78+ it ( "descriptor should have expected format" , function ( ) {
3279 const [ x1 , x2 , x3 ] = rootWalletKeys . triple . map ( ( xpub ) => xpub . neutered ( ) . toBase58 ( ) ) ;
3380 if ( scriptType === "p2sh" && scope === "external" ) {
3481 // spot check
@@ -49,16 +96,23 @@ function runTest(
4996 } ) ;
5097
5198 it ( "address should match descriptor" , function ( ) {
52- const scriptFromDescriptor = Buffer . from (
53- Descriptor . fromString (
54- getDescriptorForScriptType ( rootWalletKeys , scriptType , scope ) ,
55- "derivable" ,
56- )
57- . atDerivationIndex ( index )
58- . scriptPubkey ( ) ,
59- ) ;
99+ const scriptFromDescriptor = Buffer . from ( descriptor . atDerivationIndex ( index ) . scriptPubkey ( ) ) ;
60100 assert . deepStrictEqual ( scriptUtxolib . toString ( "hex" ) , scriptFromDescriptor . toString ( "hex" ) ) ;
61101 } ) ;
102+
103+ it ( "should have expected weights" , function ( ) {
104+ assert . ok ( Number . isInteger ( descriptor . maxWeightToSatisfy ( ) ) ) ;
105+ const vSize = Math . ceil ( descriptor . maxWeightToSatisfy ( ) / 4 ) ;
106+ console . log (
107+ scriptType ,
108+ "scriptLength" ,
109+ descriptor . atDerivationIndex ( 0 ) . encode ( ) . length ,
110+ "vSize" ,
111+ vSize ,
112+ ) ;
113+ assert . equal ( vSize , getExpectedMaxWeightToSatisfy ( scriptType ) ) ;
114+ assert . equal ( getTotalInputSize ( vSize ) , getExpectedVSize ( scriptType ) ) ;
115+ } ) ;
62116 } ) ;
63117}
64118
0 commit comments