Comments (9)
The fees are not explicitly set in a transaction. They must be implicitly set based on this formula: fee = input_amount - target amount - change_amount
Just reduce the target_amount or change_amount in your code to automatically include a fee. I calculated it by doing this:
input_amount = 0.0005690
target_amount = input_amount * 0.6
change_amount = input_amount * 0.36
This is a rather generous fee. Apparently I overpaid by 912%!
from programmingbitcoin.
How can we get fees in there? I've broadcasted the last transaction but it's having difficulty confirming. See this bitcoin testnet transaction: https://live.blockcypher.com/btc-testnet/tx/2946d9b34eb63575898fc881ec0e157154f9afd24ed3d371f83616f097c9043e/
No fees. Would be nice to get them in there. I'll play around with it and post updates here.
from programmingbitcoin.
UPDATE:
I've been able to resolve the FALSE output. Let's look at the "priv" variable.
I changed priv = PrivateKey(secret=secret)
to
priv = PrivateKey(secret)
from programmingbitcoin.
The fees are not explicitly set in a transaction. They must be implicitly set based on this formula:
fee = input_amount - target amount - change_amount
Just reduce the target_amount or change_amount in your code to automatically include a fee. I calculated it by doing this:
input_amount = 0.0005690 target_amount = input_amount * 0.6 change_amount = input_amount * 0.36
This is a rather generous fee. Apparently I overpaid by 912%!
@alexbarron Thanks so much for sharing this! I'm just about wrapped up with Ch 12 and Ch 13. Once I wrap those up, I'll revisit Ch 4 and implement this again. Cheers!
from programmingbitcoin.
Hello all,
I tried to run Example 4 but I was not able to broadcast the transaction on https://live.blockcypher.com/btc/pushtx/
gives me an error
Error validating transaction: witness script detected in tx without witness data.
My code:
from ecc import PrivateKey
from helper import decode_base58, SIGHASH_ALL
from script import p2pkh_script, Script
from tx import TxIn, TxOut, Tx
prev_tx = bytes.fromhex('c4c34a8d9a705a1a7e4bd753314e25697f82622f5cbf25ea4dbb2b6f48ce719a')
prev_index = 1
target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv'
change_address = 'mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG'
input_amount = 0.0002
target_amount = input_amount * 0.6
change_amount = input_amount * 0.36
#secret = 8675309
priv = PrivateKey(secret)
tx_ins = []
tx_ins.append(TxIn(prev_tx, prev_index))
tx_outs = []
h160 = decode_base58(target_address)
script_pubkey = p2pkh_script(h160)
target_satoshis = int(target_amount100000000)
tx_outs.append(TxOut(target_satoshis, script_pubkey))
h160 = decode_base58(change_address)
script_pubkey = p2pkh_script(h160)
change_satoshis = int(change_amount100000000)
tx_outs.append(TxOut(change_satoshis, script_pubkey))
tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True)
print(tx_obj.sign_input(0, priv))
print(tx_obj.serialize().hex())
print(secret)
I created secret as
from ecc import PrivateKey
from helper import hash256, little_endian_to_int
secret = little_endian_to_int(hash256(b'Hashir secret'))
private_key = PrivateKey(secret)
print(private_key.point.address(testnet=True))
print(secret)
Notes:
I sent some coins 0.0006 to my address
mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG
I use the prev transaction from here: https://live.blockcypher.com/btc-testnet/address/mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG/
but I am unable to broadcast the transaction
from programmingbitcoin.
@asteryeyo @vvronskyFX
FYI, I was getting a similar error and finally got to the bottom of it.
In my case the issue was that in Chapter 4 Exercise 9 where you are asked to create an address the book does not specify compressed vs. uncompressed format. I chose uncompressed, which led to pain, as in the Chapter 7, the Tx.sign_input method assumes compressed when creating the script_sig.
def sign_input(self, input_index, private_key):
# get the signature hash (z)
# get der signature of z from private key
# append the SIGHASH_ALL to der (use SIGHASH_ALL.to_bytes(1, 'big'))
# calculate the sec
# initialize a new script with [sig, sec] as the cmds
# change input's script_sig to new script
# return whether sig is valid using self.verify_input
#raise NotImplementedErro
z=self.sig_hash(input_index)
sig=private_key.sign(z).der()
sig+=SIGHASH_ALL.to_bytes(1, 'big')
sec = private_key.point.sec(compressed=False) #<-- this fixed it for me
script=Script([sig, sec])
self.tx_ins[input_index].script_sig=script
return self.verify_input(input_index)
from programmingbitcoin.
I'm working through this exercise and it's been some time. I want to move forwards to Ch 8 but I really want to get this solution to work.
First thing that stands out here is the the instruction to "Create a testnet transaction that sends 60% of a single UTXO to
mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv
The remaining amount minus fees should go back to your own change address. This should be a one-input, two-output transaction."However, in the solution, the
target_address
is'miKegze5FQNCnGw6PKyqUbYUeBa4x2hFeM'
Next is we need to include a fee in there but I don't see how I can set up a fee.
Next is the fact that we want 60% of the UTXO to be sent to the target address. I just put in a
target_amount
of0.0002
All in all, i get a Hex code but when i run
>>> print(tx_obj.sign_input(0, priv))
I get aFALSE
output.Here's the solution screenshot:
Here's my code within Exercise 4:
I couldn't find any solution online. Has anyone passed this exercise here?
response regarding the fees: i constructed my transaction with zero fees and after all the signing , i calculated the serialized txn hash . i then set my fees to 1*transaction length, that is paying 1 Satoshi per byte
from programmingbitcoin.
i meant the serialized txn length not length of its hash
from programmingbitcoin.
Hello all, I tried to run Example 4 but I was not able to broadcast the transaction on https://live.blockcypher.com/btc/pushtx/ gives me an error Error validating transaction: witness script detected in tx without witness data.
My code:
from ecc import PrivateKey from helper import decode_base58, SIGHASH_ALL from script import p2pkh_script, Script from tx import TxIn, TxOut, Tx prev_tx = bytes.fromhex('c4c34a8d9a705a1a7e4bd753314e25697f82622f5cbf25ea4dbb2b6f48ce719a') prev_index = 1 target_address = 'mwJn1YPMq7y5F8J3LkC5Hxg9PHyZ5K4cFv' change_address = 'mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG' input_amount = 0.0002 target_amount = input_amount * 0.6 change_amount = input_amount * 0.36 #secret = 8675309 priv = PrivateKey(secret) tx_ins = [] tx_ins.append(TxIn(prev_tx, prev_index)) tx_outs = [] h160 = decode_base58(target_address) script_pubkey = p2pkh_script(h160) target_satoshis = int(target_amount_100000000) tx_outs.append(TxOut(target_satoshis, script_pubkey)) h160 = decode_base58(change_address) script_pubkey = p2pkh_script(h160) change_satoshis = int(change_amount_100000000) tx_outs.append(TxOut(change_satoshis, script_pubkey)) tx_obj = Tx(1, tx_ins, tx_outs, 0, testnet=True) print(tx_obj.sign_input(0, priv)) print(tx_obj.serialize().hex()) print(secret)
I created secret as from ecc import PrivateKey from helper import hash256, little_endian_to_int secret = little_endian_to_int(hash256(b'Hashir secret')) private_key = PrivateKey(secret) print(private_key.point.address(testnet=True)) print(secret)
Notes: I sent some coins 0.0006 to my address mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG I use the prev transaction from here: https://live.blockcypher.com/btc-testnet/address/mtjfgktYZdoEt8XgU5gM7BRx4yrHsqY1yG/ but I am unable to broadcast the transaction
some one apparently has 345 tBTC. Looks like you have the wrong index. prev_index has to be a 0 not 1
from programmingbitcoin.
Related Issues (20)
- Chapter 4 Exercise 9 Code returns invalid testnet address HOT 3
- Chapter 11: Exercise 6 & 7 HOT 2
- Openssl removed ripemd160 support. Code using hashlib's 'ripemd160' algorithm may fail. HOT 4
- mainnet.programmingbitcoin.com not responding for getheaders message HOT 1
- Chapter 7: help me understand decode_base58 HOT 1
- rmul method inside FieldElement Class to be added before completing ex 4 chapter 3 HOT 1
- Chapter 3, Exercise 3 self.assertEqual() error HOT 1
- Chapter 3, Explanation of why doubling SHA-256 is more secure seems wrongs
- "virtualenv -p python3 .venv" does not work
- How to setup environment in Google Colab
- How to generate P2TR address? HOT 1
- op_checkmultisig: Last signature is not verified HOT 3
- Chapter 5: Varint sidebar example
- Chapter 4 return value for decode_base58 function HOT 1
- Chapter 3 sign method s or N - s works as part of the sig? HOT 1
- Chapter 5: In function encode_varint - Why use < 0xfd instead of < 0x100 for one byte strings, similar to the larger options? HOT 1
- Invalid ScriptSig found on mainnet HOT 1
- Chapter 4 - checksum should be double hashed? HOT 1
- Can not visit the link https://faucet.programmingbitcoin.com HOT 4
- Multiplying two points
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from programmingbitcoin.