Title: bitcoin transactions size and multiple output... Post by: hamdi369 on December 16, 2015, 12:12:08 PM Hi
I have a little problem to determine a bitcoin transaction size... I read this topic https://bitcointalk.org/index.php?topic=1000686.0 ,and this too : http://bitcoin.stackexchange.com/questions/1195/how-to-calculate-transaction-size-before-sending but it seem not work for my case... I create a transaction that have always min 2 outputs ( without the change address it will be 3) I understand this : in*180 + out*34 + 10 plus or minus 'in' but I try with different type of transactions : in the below exemple the transaction is sending bitcoin to 2 different address (with sendmany) -- first exemple from 1 address with 5 inputs -> size : 977 (5*180) + (2*34) +10 +-5 = 900 + 68 + 10 +-5 = 978+-5 973 < size (977 ) < 983 it's okay! :) -- second exemple from 5 differents address with in each 1 input -> size : 1157 it appaers that for the formula work it could just be : (5*180) + (7*34) + 10 +- 5*2 = 900 + 238 + 10 +- 10 = 1148 +- 10 1138 < size 1157 < 1158 is that right? if yes, why here 7 outputs? why *2 ?? -- third exemple from 1 address with 1 input -> size : 439 it appaers that for the formula work it could just be : (2*180) + (2*34) + 10 +- 1 = 360 + 68 + 10 +- 1 = 360 + 68 + 10 +- 1 = 438 = 437 < size 439 < 439 why here this 2 when there is just one input? could anyone help for explain me why this differences? Thank you for your help :) Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 16, 2015, 02:26:55 PM You'll have to provide actual examples. Show what the resulting actual transactions are so we can see where you made your error.
I think that formula only works for pay-to-pubkey-hash addresses, addresses that start with a 1 (on mainnet) or with an m or n (on testnet). If you are using inputs that were sent to pay-to-script-hash addresses, addresses that start with a 3 (on mainnet) or with 2 (on testnet), then the calculation will be different. Also note that the formula that you've indicated is for inputs that were received at addresses computed from uncompressed keys. Using compressed key addresses reduces the size of each input by about 32 bytes. If those inputs were received at addresses computed from compressed keys, then I think the formula is: Code: in*148 + out*34 + 10 plus or minus 'in' Title: Re: bitcoin transactions size and multiple output... Post by: hamdi369 on December 16, 2015, 03:26:28 PM thank you for your answer :) , I work with testcoins
In the exemple1, I send 0.064 from mhrUjndTCeu6yicvwrsy68GxtQrGKCcwVr (that have exatly 0.064) to 2 address: (no fees, no change address...) 0.06336 for mhtqrsyrT2TKE5ZVCKTVGZM63L8L9W4jq6 0.00064 for n1ZkUoqCMEFXA5KboJHNuSaNDL7iP2LiWd the transaction hex decode : Code: { --------------------------------------------------------------------------------------------- In the exemple2, I send 0.067 from 5 adress with 1 input in each from : mhmXbBpwznLvtdtMe4pXomUW6pB1Z1Tjx9 0.014 mpUdk8WCCv7X2Na9VNc1B7mPouDHdp3qNC 0.014 miA5yJeoqRsn7FSjnu88rB49niqVmFKvjK 0.013 n2R4xyw5Hws3KcWA932nTKXYbnVx8RWt8K 0.013 mshVq1kSb9cYeMswVQ4bo63U1fLfqYQk9w 0.013 ----> total 0.067 to 2 address (no fees, no change address exatly like exemple1) 0.06633 for mhtqrsyrT2TKE5ZVCKTVGZM63L8L9W4jq6 0.00067 for n1ZkUoqCMEFXA5KboJHNuSaNDL7iP2LiWd the decode transaction hex : Code: { ------------------------------------------------------------------------------------------------- In the exemple3, I send 0.011 from 1 adress with 1 input from : n12oErzdFKYWtyVrymmyPezjby1pdJ3tSK 0.011 ----> total 0.011 to 2 address (no fees, no change address exatly like exemple1 and 2) 0.01089 for mhtqrsyrT2TKE5ZVCKTVGZM63L8L9W4jq6 0.00011 for n1ZkUoqCMEFXA5KboJHNuSaNDL7iP2LiWd the decode transaction hex : Code: { Title: Re: bitcoin transactions size and multiple output... Post by: achow101 on December 16, 2015, 03:37:00 PM Example 1 uses inputs that use uncompressed keys, so they are longer and fit the formula that you used
Examples 2 and 3 both have some inputs that use compressed keys so those inputs are shorter. You will need to use their shorter size when adding calculating those inputs. Their size is 148 bytes, 32 bytes shorter than the inputs that use uncompressed keys. Title: Re: bitcoin transactions size and multiple output... Post by: hamdi369 on December 16, 2015, 05:50:00 PM thank you knightdk for your answer but how do you know if the key is compressed or not ?
and what is then the formula for exemple2 and 3? thank you for answer Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 16, 2015, 07:59:09 PM In the exemple2, I send 0.067 from 5 adress with 1 input in each from : to 2 address (no fees, no change address exatly like exemple1) 0.06633 for mhtqrsyrT2TKE5ZVCKTVGZM63L8L9W4jq6 0.00067 for n1ZkUoqCMEFXA5KboJHNuSaNDL7iP2LiWd You are mistaken. I've looked at the transaction you posted. It appears to have 6 inputs, not 5 inputs:
For a total of 0.08482863 BTC in value supplied to the transaction It also appears to have 3 outputs, not 2:
For a total of 0.0845239 BTC in value spent from the transaction Leaving 0.00030473 BTC as a transaction fee One of your inputs (the one from transaction 605ad738e124aa9a6fe2e1a1e9873309c72e02e6b2d676ed0a237304ad6e644f) was received at a compressed key address. So, that's a minimum size of: (5*180)+(1*148)+(3*34)+10-6 = 1154 And a maximum size of: (5*180)+(1*148)+(3*34)+10+6 = 1166 The size you have is: 1157 1154 < 1157 < 1166 I haven't looked at Example 3 yet, but I suspect that you probably made similar errors. Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 16, 2015, 08:27:34 PM thank you knightdk for your answer but how do you know if the key is compressed or not ? If the wallet created the address, then it will know what type of address it created. If you wrote your own software for generating addresses, then you should know what type of address you generated. If you can access the private key for an address, then you can tell if it is a compressed key address by the first byte in the private key. A private key in WIF for an uncompressed key address on testnet will start with a 9. A private key in WIF for a compressed key address on testnet will start with a c. If you don't have access to the private key, but you have access to a transaction where the the output has been spent (and if it was a pay-to-pubkey-hash output) , then you can tell by looking at the scriptSig (also known as Txin-script). The first byte of the scriptSig indicates the length (in bytes) of the signature, next is the signature, then the next byte after the signature is a byte that indicates the length of the public key. If the length of the public key is 65 bytes (41 in hex), then the input was previously received at an uncompressed key address. If the length of the public key is 33 bytes (21 in hex), then the input was previously received at a compressed key address. Title: Re: bitcoin transactions size and multiple output... Post by: achow101 on December 16, 2015, 08:30:02 PM thank you knightdk for your answer but how do you know if the key is compressed or not ? If the wallet created the address, then it will know what type of address it created. If you wrote your own software for generating addresses, then you should know what type of address you generated. If you can access the private key for an address, then you can tell if it is a compressed key address by the first byte in the private key. A private key in WIF for an uncompressed key address on testnet will start with a 9. A private key in WIF for a compressed key address on testnet will start with a c. If you don't have access to the private key, but you have access to a transaction where the the output has been spent (and if it was a pay-to-pubkey-hash output) , then you can tell by looking at the scriptSig (also known as Txin-script). The first byte of the scriptSig indicates the length (in bytes) of the signature, next is the signature, then the next byte after the signature is a byte that indicates the length of the public key. If the length of the public key is 65 bytes (41 in hex), then the input was previously received at an uncompressed key address. If the length of the public key is 33 bytes (21 in hex), then the input was previously received at a compressed key address. Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 16, 2015, 08:36:14 PM Also, compressed public keys will begin with a 02 or 03 byte while uncompressed public keys begin with a 04 byte. True, but if you've gotten that far, then you must already have gotten past the key size. Title: Re: bitcoin transactions size and multiple output... Post by: achow101 on December 16, 2015, 09:23:51 PM Also, compressed public keys will begin with a 02 or 03 byte while uncompressed public keys begin with a 04 byte. True, but if you've gotten that far, then you must already have gotten past the key size. Title: Re: bitcoin transactions size and multiple output... Post by: hamdi369 on December 17, 2015, 05:39:41 AM Thank you Knightdk and DannyHamilton for your answer.
It's really clear now. I will study it for know the size of a transaction before create it. But know what I can't understand, is why are this additional input and outputs? I make these three transactions with RPC calls to bitcoind server, using sendmany method. In these three exmples, I always give to server just 2 outputs (receiving address), from an account, and transfert exactly the amount of bitcoin from the account (without fee), but know it appaears that there is more input and outputs as I expected... Do you know why? Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 17, 2015, 12:06:48 PM But know what I can't understand, is why are this additional input and outputs? I make these three transactions with RPC calls to bitcoind server, using sendmany method. In these three exmples, I always give to server just 2 outputs (receiving address), from an account, and transfert exactly the amount of bitcoin from the account (without fee), but know it appaears that there is more input and outputs as I expected... Do you know why? Sendmany does not allow you to choose which outputs the wallet will use. If you want to specify exactly which outputs you want the wallet to use, then you need to use createrawtransaction. "Addresses" and "accounts" are not the same thing. Bitcoin Core has a built in accounting system that doesn't work the way most people seem to think that it would work. Think of it more like a bank where the "account name" in Bitcoin Core is like our account number at the bank, and the "bitcoin address" is like our receipt number on our bank transaction. It is possible for receipt numbers and account numbers to have the same value occasionally, but that doesn't mean they are the same thing. If the bank is using their interface properly, then we should get a new receipt number for every transaction we do, but our account numbers stay the same. If I make five transactions depositing a $20 bill into the bank each time, the bank will have received five outputs that they will store in the safe. If you deposit ten $10 bills into the bank, the bank will have a total of fifteen outputs that they will store in the safe (five from me and ten from you). Now, if you come back and withdraw $100, the bank won't keep track of which of those paper notes are yours and which are mine, they'll just go grab some of the $20 and some of the $10 to give you, and then they'll adjust the values in your account. Just like you can overdraw your account at a bank, it is possible for you to overdraw your account in Bitcoin Core. If you have 0.5 BTC in account A in Bitcoin Core, and 0.75 BTC in account B, then Bitcoin Core will allow you to use sendmany to send 1.0 BTC from account A. The balance on account A will then be -0.5 BTC and the balance on account B will still be 0.75 BTC. I don't think that sendmany will allow you to set a transaction fee (or lack of fee) either. It will just use whatever the default fee settings are for the wallet. Title: Re: bitcoin transactions size and multiple output... Post by: hamdi369 on December 17, 2015, 07:20:12 PM Thank you DannyHamilton for your clear explanation :) , with your help, now I understand good that SendMany is not what I need because it works with accounts ans not with address :)
I would like to test CreateRawTransaction https://bitcoin.org/en/developer-reference#createrawtransaction, for exemple, if I want to spend the 0.04999 of this address : (that I have the private keys of course...) https://blockchain.info/fr/address/1g89wGhWxuQGGqGxVG6dZMzTTSVMWqWj6 to an empty new address php code Code: $bitcoin->createrawtransaction( I can't find where I can get the output index vout ? bitcoin devlopper reference quote : https://bitcoin.org/en/developer-reference#createrawtransaction Quote The createrawtransaction RPC creates an unsigned serialized transaction that spends a previous output to a new output with a P2PKH or P2SH address. The transaction is not stored in the wallet or transmitted to the network. after done with CreateRawTransaction, is the next steps : > SignRawTransaction > SendRawTransaction or just > SendRawTransaction ? I thank you for your help and hope I will too help another newbie in future, like you help me Title: Re: bitcoin transactions size and multiple output... Post by: DannyHamilton on December 17, 2015, 07:57:52 PM - snip - for exemple, if I want to spend the 0.04999 of this address : (that I have the private keys of course...) https://blockchain.info/address/1g89wGhWxuQGGqGxVG6dZMzTTSVMWqWj6 to an empty new address You'll notice that this address has received only 1 transaction. The transaction ID where the bitcoins were received (where the output was created) was: 8dc0aa6a5022868490245fd4b10c47bccdaa0358cb24ed5ad3d09c99fac39f0e If you look at that transaction you can see the outputs: https://blockchain.info/rawtx/8dc0aa6a5022868490245fd4b10c47bccdaa0358cb24ed5ad3d09c99fac39f0e?format=json Code: { Notice from that JSON output that there are two outputs ("out" array with two array elements). The first output of the array is the one sent to "addr" 1g89wGhWxuQGGqGxVG6dZMzTTSVMWqWj6. The index is indicated in the blockchain.info web interface in the array element with the "n" label. So in this case, it is array element 0. { "spent":false, "tx_index":114922761, "type":0, "addr":"1g89wGhWxuQGGqGxVG6dZMzTTSVMWqWj6", "value":4999000, "n":0, "script":"76a914076631c59ba595332917d7b6705006345233e46388ac" }, How you determine this in your software will depend what interface you are using to access the data. There are several services with their own API that you could use, or you could just use the Bitcoin Core interface. If you are using Bitcoin Core, then you can use "listunspent" to get the list of all unspent outputs that the wallet has full control over. You could also use "getrawtransaction" and/or "decoderawtransaction" to get information about specific transactions if you know the transactionID. after done with CreateRawTransaction, is the next steps : > SignRawTransaction > SendRawTransaction or just > SendRawTransaction ? An unsigned transaction is not a valid transaction. All nodes will reject it, and miners will not confirm it. Therefore, you will need to use "signrawtransaction" before you can "sendrawtransaction" Title: Re: bitcoin transactions size and multiple output... Post by: hamdi369 on December 18, 2015, 06:53:14 AM DannyHamilton, Thank you :)
I am very grateful for your assistance. Quote If you are using Bitcoin Core, then you can use "listunspent" to get the list of all unspent outputs that the wallet has full control over. You could also use "getrawtransaction" and/or "decoderawtransaction" to get information about specific transactions if you know the transactionID. Now (7.30 AM France), my Bitcoin Core deamon is downloading the blockchain ( 345379 of 388985 blocks downloaded...), transaction is Quote "block_height":387184 so I have to wait that it download that block to test it :)I will have to study now, - how to determine size of transaction before create it (to determine the appropriate fees) - create it with CreateRawTransaction - sign it with SignRawTransaction - send it with SendRawTransaction Thank you for you help ;) |