Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
vout = int(raw_input("Output index of unspent output: "))
k = getKey("Address of unspent output: ")
inputs.append((txid, vout, k))
amounts.append(int(decimal.Decimal(
raw_input("Amount in unspent output (BCC): ")
) * BCC))
totalAmount = sum(amounts)
print "Total of amounts: %s BCC" % str(decimal.Decimal(totalAmount)/BCC)
fee = int(decimal.Decimal(
raw_input("Transaction fee (BCC): ")
) * BCC)
destAddress = raw_input("Destination address: ")
destHash = base58.decodeBase58Check(destAddress, 0) #PUBKEY_ADDRESS = 0
destAmount = totalAmount - fee
print "Amount sent to destination: %s BCC" % str(decimal.Decimal(destAmount)/BCC)
if destAmount < 0:
print "Negative amount is not allowed"
sys.exit(2)
'''
if destAmount > totalAmount - fee:
print "Not enough funds"
sys.exit(1)
'''
tx = btx.Transaction(
tx_in = [
btx.TxIn(x[0], x[1])
def readPrivateKey(filename):
with open(filename, "rb") as f:
privateKey = f.read()
privateKey = privateKey.split("\n")[0] #first line
privateKey = privateKey.strip() #ignore whitespace
return base58.decodeBase58Check(privateKey, 128) #PRIVKEY = 128
used = [u]
total = u["amount"]
break
if used is None:
used = []
while total < amount:
try:
u = unspent.pop()
except IndexError:
raise Exception("Insufficient funds")
used.append(u)
total += u["amount"]
for u in used:
address = u["address"]
u["privateKey"] = base58.decodeBase58Check(
bitcoind.getPrivateKey(address), 128)
return total, [
(u["txid"], u["vout"], u["scriptPubKey"], u["privateKey"])
for u in used]
changeAmount = totalAmount - destAmount - fee
if changeAmount < 0:
raise Exception("Error: got negative change amount")
elif changeAmount == 0:
print "Note: change amount is zero - no change is sent"
else:
tx.tx_out.append(
btx.TxOut(changeAmount, btx.Script.standardPubKey(changeHash))
)
'''
for i in range(len(inputs)):
#print tx.tx_in[i].previousOutputHash.encode("hex"), tx.tx_in[i].previousOutputIndex
key = inputs[i][2]
address = getAddress(key)
hash = base58.decodeBase58Check(address, 0) #PUBKEY_ADDRESS = 0
scriptPubKey = btx.Script.standardPubKey(hash)
tx.signInput(i, scriptPubKey, [None, key.getPublicKey()], [key], amounts[i])
print "Serialized transaction:"
print tx.serialize().encode("hex")
print "Transaction ID:", tx.getTransactionID()[::-1].encode("hex")
print ' sequenceNumber: 0x%08x' % tx_in.sequenceNumber
print ' script:'
for e in tx_in.scriptSig.elements:
if isinstance(e, str):
s = e.encode("hex")
else:
s = str(e)
print ' ', s
signature, pubKey = tx_in.scriptSig.elements
hashType = ord(signature[-1])
signature = signature[:-1]
k = Key()
k.setPublicKey(pubKey)
address = getAddress(k)
hash = base58.decodeBase58Check(address, 0) #PUBKEY_ADDRESS = 0
scriptPubKey = btx.Script.standardPubKey(hash)
sigHash = tx.getSignatureBodyHash(i, scriptPubKey, hashType, amount=amounts[i])
print ' pubKey: ', pubKey.encode('hex')
print ' signature: ', signature.encode('hex')
print ' hashType: 0x%0x' % hashType
print ' address: ', address
print ' sigHash: ', sigHash.encode('hex')
print ' valid: ', k.verify(sigHash, signature)
print ''
for tx_out in tx.tx_out:
print 'TxOut:'
print ' amount: %s BCC' % str(decimal.Decimal(tx_out.amount)/BCC)