@@ -173,10 +173,73 @@ describe("Dimensions", function () {
173173
174174 it ( "should throw when address is provided without network" , function ( ) {
175175 assert . throws ( ( ) => {
176- // @ts -expect-error - testing runtime error
176+ // String matches { length: number } but implementation detects string and throws
177177 Dimensions . fromOutput ( "bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4" ) ;
178178 } , / n e t w o r k i s r e q u i r e d / ) ;
179179 } ) ;
180+
181+ it ( "should create dimensions from script length only" , function ( ) {
182+ // Compare with actual script
183+ const script = Buffer . alloc ( 23 ) ;
184+ const fromScript = Dimensions . fromOutput ( script ) ;
185+ const fromLength = Dimensions . fromOutput ( { length : 23 } ) ;
186+
187+ assert . strictEqual ( fromLength . getWeight ( ) , fromScript . getWeight ( ) ) ;
188+ assert . strictEqual ( fromLength . getVSize ( ) , fromScript . getVSize ( ) ) ;
189+ assert . strictEqual ( fromLength . getOutputWeight ( ) , fromScript . getOutputWeight ( ) ) ;
190+ } ) ;
191+
192+ it ( "should calculate correct weight for different script lengths" , function ( ) {
193+ // p2pkh: 25 bytes -> weight = 4 * (8 + 1 + 25) = 136
194+ const p2pkh = Dimensions . fromOutput ( { length : 25 } ) ;
195+ assert . strictEqual ( p2pkh . getOutputWeight ( ) , 136 ) ;
196+
197+ // p2wpkh: 22 bytes -> weight = 4 * (8 + 1 + 22) = 124
198+ const p2wpkh = Dimensions . fromOutput ( { length : 22 } ) ;
199+ assert . strictEqual ( p2wpkh . getOutputWeight ( ) , 124 ) ;
200+
201+ // p2tr: 34 bytes -> weight = 4 * (8 + 1 + 34) = 172
202+ const p2tr = Dimensions . fromOutput ( { length : 34 } ) ;
203+ assert . strictEqual ( p2tr . getOutputWeight ( ) , 172 ) ;
204+ } ) ;
205+
206+ it ( "should create dimensions from script type" , function ( ) {
207+ // p2sh/p2shP2wsh: 23 bytes -> weight = 4 * (8 + 1 + 23) = 128
208+ const p2sh = Dimensions . fromOutput ( { scriptType : "p2sh" } ) ;
209+ assert . strictEqual ( p2sh . getOutputWeight ( ) , 128 ) ;
210+
211+ const p2shP2wsh = Dimensions . fromOutput ( { scriptType : "p2shP2wsh" } ) ;
212+ assert . strictEqual ( p2shP2wsh . getOutputWeight ( ) , 128 ) ;
213+
214+ // p2wsh: 34 bytes -> weight = 4 * (8 + 1 + 34) = 172
215+ const p2wsh = Dimensions . fromOutput ( { scriptType : "p2wsh" } ) ;
216+ assert . strictEqual ( p2wsh . getOutputWeight ( ) , 172 ) ;
217+
218+ // p2tr/p2trLegacy: 34 bytes -> weight = 4 * (8 + 1 + 34) = 172
219+ const p2tr = Dimensions . fromOutput ( { scriptType : "p2tr" } ) ;
220+ assert . strictEqual ( p2tr . getOutputWeight ( ) , 172 ) ;
221+
222+ const p2trLegacy = Dimensions . fromOutput ( { scriptType : "p2trLegacy" } ) ;
223+ assert . strictEqual ( p2trLegacy . getOutputWeight ( ) , 172 ) ;
224+
225+ // p2trMusig2: 34 bytes -> weight = 4 * (8 + 1 + 34) = 172
226+ const p2trMusig2 = Dimensions . fromOutput ( { scriptType : "p2trMusig2" } ) ;
227+ assert . strictEqual ( p2trMusig2 . getOutputWeight ( ) , 172 ) ;
228+ } ) ;
229+
230+ it ( "scriptType should match equivalent length" , function ( ) {
231+ // p2sh = 23 bytes
232+ assert . strictEqual (
233+ Dimensions . fromOutput ( { scriptType : "p2sh" } ) . getOutputWeight ( ) ,
234+ Dimensions . fromOutput ( { length : 23 } ) . getOutputWeight ( ) ,
235+ ) ;
236+
237+ // p2wsh = 34 bytes
238+ assert . strictEqual (
239+ Dimensions . fromOutput ( { scriptType : "p2wsh" } ) . getOutputWeight ( ) ,
240+ Dimensions . fromOutput ( { length : 34 } ) . getOutputWeight ( ) ,
241+ ) ;
242+ } ) ;
180243 } ) ;
181244
182245 describe ( "plus" , function ( ) {
0 commit comments