in these two cases the redeem script shows that this is of type P2SH-P2WPKH so we have as witness:
- one signature (fixed 71 bytes if the wallet grinds for low r values)
- length of signature 1 byte
- one compressed public key (33 bytes)
- length of public key 1 byte
- witness stack item count 1 byte
we have as signature script:
- a redeem script of size 23
- push size will be 1
that means each input adds 32(hash)+4(index)+24(sigScr)+4(sequence) = 64 bytes & 71+1+33+1+1=107 as witness
each output that has P2SH (like the first example) adds 8(amount)+23(pubScr)+1(length)=32 bytes
each transaction has some extra bytes: 4(version)+1(inCount)+1(outCount)+4(locktime)=10 & 2(segwit flag)
so the raw bytes or the total size is (2*64)+(2*107)+(2*32)+10+2=418
base size (without witnesses) is (2*64)+(2*32)+10=202
weight is base*3 + totalsize = 1,024
virtual size is weight/4 = 256
fee is 279541 satoshi => 279541/256=1,091.95703125 =~1,092 sat/vbyte
wiggle room
if the wallet doesn't grind for low r and has to add the 0x00 to indicate positive r for high values you can add 1 byte to for high r which makes witness 108 bytes, total size 420, base size same 202, weight 1026, virtual size 257 and fee 1,088 sat/vbyte
the second link is the same with the difference in their output types which are P2WPKH instead of P2SH and each one adds 8(amount)+22(pubscr)+1(length)=31 bytes
that's very precise.
I updated the calculator with your formula and it is now working very well for those 3 address types.
It is working very well now. Let's see those last P2SH transactions that I was having problems. Using blockstream as nc50lc suggested:
- https://blockstream.info/tx/d19e0257c3e37cb805d99a715568137c2553bbb25ee23d66ebcb1f4a2ac7d652
1 P2SH input and 2ouput
Calculates
Transaction size: 247 bytes / 165 vbytes
Transaction fees: 0.00027720 btc(3.03 usd)
Real
TRANSACTION FEES 0.00027556 BTC (166.8 sat/vB)
SIZE 247 B
VIRTUAL SIZE 166 vB - https://blockstream.info/tx/4e313920ff45fb6be3866875c249e502159372afd46c081f320371114045619d
Interesting case: 13 P2SH inputs , 1 P2SH output and 1 P2PKH output. Almost 100%
Calculated
Transaction size: 2299 bytes / 1254 vbytes
Transaction fees: 0.00253308 btc
Real
TRANSACTION FEES 0.00254501 BTC (202.6 sat/vB)
SIZE 2301 B
VIRTUAL SIZE 1257 vB - https://blockstream.info/tx/773f472b3806a9e8b51b9406a9080fb4ae015b28814f5ecb6ce6a3d5a47818e0?expand
Looks like Bech32 outputs and P2SH outputs have both the same size. 100% match:
3 P2SH inputs and 2 Bech32 outputs.
Calculated
Transaction size: 589 bytes / 346 vbytes
Transaction cost: 0.00000346 btc
Real
TRANSACTION FEES 0.00000346 BTC
SIZE 589 B
VIRTUAL SIZE 346 vB
I will try to make a more detailed solution so the user can select type of input during the week.
I wanna still make some prediction using mempool data.
Thank you for your help guys.