Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
filename = os.path.basename(sys.argv[i])
message = '\n'.join([
'Content-type: {}{}'.format(mime_type, '; charset={}'.format(encoding) if encoding is not None else ''),
'Content-length: {}'.format(len(data)),
'Content-disposition: attachment; filename={}'.format(filename),
])
message = message.encode('utf8') + b'\n\n' + data
i += 1
# Get coins for input
print('*** Step 1. We need Bitcoins in order to send a message. Give me a Bitcoin private key (it starts with a 5...) to use as an input for the message transaction.')
bitcoin_private_key = input('...Enter Bitcoin private key: ')
# Decode private key, show bitcoin address associated with key
private_key = base58.decode_to_bytes(bitcoin_private_key)[-36:-4]
public_key = addressgen.get_public_key(private_key)
bitcoin_input_address = addressgen.generate_address(public_key, version=0)
print('...The Bitcoin address associated with that private key is: {}'.format(bitcoin_input_address))
# Lookup the unspent outputs associated with the given input...
print('...Looking up unspent outputs on blockchain.info...')
unspent_outputs = filter_unspent_outputs(lookup_unspent_outputs([bitcoin_input_address])[bitcoin_input_address])
# Show the inputs to the user, and ask him which he'd like to use as input.
print('\n*** Step 2. You need to select an input:')
for k, u in enumerate(unspent_outputs):
print('...{}: txid={} n={} value={} confirmations={}'.format(k+1, u['tx_hash'], u['tx_output_n'], Bitcoin.format_money(u['value']), u['confirmations']))
selected_inputs = [int(x.strip())-1 for x in input('Enter inputs (if more than one, separated by commas): ').split(',') if len(x) > 0]
if not all(x >= 0 and x < len(unspent_outputs) for x in selected_inputs):
raise Exception("Invalid input provided")
total_input_amount = sum(unspent_outputs[k]['value'] for k in selected_inputs)
def extractAddressFromInputScript(self):
self.address = None
self.signed = False
if len(self.scriptSig.program) == 4 and len(self.scriptSig.program[1]) in (71, 72, 73):
if len(self.scriptSig.program[3]) in (33,65):
self.address = base58.decode_to_bytes(addressgen.generate_address(self.scriptSig.program[3]))[-24:-4]
# TODO - we have to run the script to see if the signature is valid for the input, but for now we just accept if it has non-zero length
self.signed = True
self.signed_hash_type = self.scriptSig.program[1][-1]
def extractAddressFromOutputScript(self):
self.address = None
if len(self.scriptPubKey.program) == 6 and \
self.scriptPubKey.program[0][0] == OP_DUP and self.scriptPubKey.program[1][0] == OP_HASH160 and \
self.scriptPubKey.program[2][0] == 20 and \
self.scriptPubKey.program[4][0] == OP_EQUALVERIFY and self.scriptPubKey.program[5][0] == OP_CHECKSIG:
self.address = self.scriptPubKey.program[3]
assert len(self.address) == 20
elif len(self.scriptPubKey.program) == 3 and \
self.scriptPubKey.program[2][0] == OP_CHECKSIG:
if self.scriptPubKey.program[2][0] in (0x04, 0x03, 0x02):
self.address = base58.decode_to_bytes(addressgen.generate_address(self.scriptPubKey.program[1]))[-24:-4]
elif self.scriptPubKey.program[-1][0] == OP_CHECKMULTISIG:
nreq = self.scriptPubKey.program[0][0] - OP_1 + 1
nkeys = self.scriptPubKey.program[-2][0] - OP_1 + 1
if nreq >= 1 and nreq <= nkeys:
pubkeys = [self.scriptPubKey.program[2 + i * 2] for i in range(nkeys)]
self.multisig = (pubkeys, nreq)
unspent = defaultdict(list)
for u in result['unspent_outputs']:
program_bytes = Bitcoin.hexstring_to_bytes(u['script'], reverse=False)
scriptPubKey, _ = script.Script.unserialize(program_bytes, len(program_bytes))
address = None
# Try to extract the address from the scriptpubkey program
if len(scriptPubKey.program) == 6 and \
scriptPubKey.program[0][0] == script.OP_DUP and scriptPubKey.program[1][0] == script.OP_HASH160 and \
scriptPubKey.program[2][0] == 20 and \
scriptPubKey.program[4][0] == script.OP_EQUALVERIFY and scriptPubKey.program[5][0] == script.OP_CHECKSIG:
address = scriptPubKey.program[3]
elif len(scriptPubKey.program) == 3 and scriptPubKey.program[2][0] == script.OP_CHECKSIG:
if scriptPubKey.program[2][0] in (0x04, 0x03, 0x02):
address = base58.decode_to_bytes(addressgen.generate_address(scriptPubKey.program[1], version=0))[-24:-4]
if address is not None:
i = 0
while address[i] == 0:
i += 1
address = '1' + ('1' * i) + addressgen.base58_check(address, version=0)
unspent[address].append(u)
return unspent
def __init__(self, address=None, amount=0):
self.amount = amount
self.address = base58.decode_to_bytes(address)[-24:-4] if address is not None else None
self.multisig = None
self.scriptPubKey = None