def getinfo(args): for filename in args: print "----------------" print "Filename: ", filename privateKey = readPrivateKey(filename) k = Key() k.setPrivateKey(privateKey) print "Public key: ", k.getPublicKey().encode("hex") print "Address: ", getAddress(k)
def makekey(args): k = Key() k.makeNewKey(compressed=True) privateKey = k.getPrivateKey() privateKey = base58.encodeBase58Check(privateKey, 128) #PRIVKEY = 128 address = getAddress(k) with open(address, "wb") as f: f.write(privateKey + "\n") print "Saved as ", address
def spend(args): #Load the keys keys = [] for filename in args: privateKey = readPrivateKey(filename) k = Key() k.setPrivateKey(privateKey) keys.append(k) def getKey(question): for i in range(len(keys)): print i+1, getAddress(keys[i]) i = int(raw_input(question)) - 1 return keys[i] #Ask for input information: inputs = [] totalAmount = 0 while True: txid = raw_input("Transaction ID of unspent output (Enter to stop): ") txid = txid.strip() if txid == "": break txid = binascii.unhexlify(txid)[::-1] vout = int(raw_input("Output index of unspent output: ")) k = getKey("Address of unspent output: ") inputs.append((txid, vout, k)) totalAmount += int(decimal.Decimal( raw_input("Amount in unspent output (mBTC): ") ) * mBTC) print "Total of amounts: %s mBTC" % str(decimal.Decimal(totalAmount)/mBTC) fee = int(0.01 * mBTC) print "Transaction fee is set to: %s mBTC" % str(decimal.Decimal(fee)/mBTC) destAddress = raw_input("Destination address: ") destHash = base58.decodeBase58Check(destAddress, 0) #PUBKEY_ADDRESS = 0 destAmount = int(decimal.Decimal( raw_input("Amount to send to destination (mBTC): ") ) * mBTC) if destAmount < 0: print "Negative amount is not allowed" sys.exit(2) if destAmount > totalAmount - fee: print "Not enough funds" sys.exit(1) changeKey = getKey("Address to send change amount to: ") changeAddress = getAddress(changeKey) changeHash = base58.decodeBase58Check(changeAddress, 0) #PUBKEY_ADDRESS = 0 tx = btx.Transaction( tx_in = [ btx.TxIn(x[0], x[1]) for x in inputs ], tx_out = [ btx.TxOut(destAmount, btx.Script.standardPubKey(destHash)) ] ) 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]) print "Serialized transaction:" print tx.serialize().encode("hex") print "Transaction ID:", tx.getTransactionID()[::-1].encode("hex")
def spend(args): #Load the keys keys = [] for filename in args: privateKey = readPrivateKey(filename) k = Key() k.setPrivateKey(privateKey) keys.append(k) def getKey(question): for i in range(len(keys)): print i + 1, getAddress(keys[i]) i = int(raw_input(question)) - 1 return keys[i] #Ask for input information: inputs = [] totalAmount = 0 while True: txid = raw_input("Transaction ID of unspent output (Enter to stop): ") txid = txid.strip() if txid == "": break txid = binascii.unhexlify(txid)[::-1] vout = int(raw_input("Output index of unspent output: ")) k = getKey("Address of unspent output: ") inputs.append((txid, vout, k)) totalAmount += int( decimal.Decimal(raw_input("Amount in unspent output (mBTC): ")) * mBTC) print "Total of amounts: %s mBTC" % str( decimal.Decimal(totalAmount) / mBTC) fee = int(0.01 * mBTC) print "Transaction fee is set to: %s mBTC" % str( decimal.Decimal(fee) / mBTC) destAddress = raw_input("Destination address: ") destHash = base58.decodeBase58Check(destAddress, 0) #PUBKEY_ADDRESS = 0 destAmount = int( decimal.Decimal(raw_input("Amount to send to destination (mBTC): ")) * mBTC) if destAmount < 0: print "Negative amount is not allowed" sys.exit(2) if destAmount > totalAmount - fee: print "Not enough funds" sys.exit(1) changeKey = getKey("Address to send change amount to: ") changeAddress = getAddress(changeKey) changeHash = base58.decodeBase58Check(changeAddress, 0) #PUBKEY_ADDRESS = 0 tx = btx.Transaction( tx_in=[btx.TxIn(x[0], x[1]) for x in inputs], tx_out=[btx.TxOut(destAmount, btx.Script.standardPubKey(destHash))]) 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]) print "Serialized transaction:" print tx.serialize().encode("hex") print "Transaction ID:", tx.getTransactionID()[::-1].encode("hex")
d = bitcoind.Bitcoind(s) #(these addresses are mine - thanks for donating :-P) keyHash1 = binascii.unhexlify("fd5627c5eff58991dec54877272e82f758ea8b65") keyHash2 = binascii.unhexlify("ab22c699d3e72f2c1e4896508bf9d8d7910104d0") address1 = base58.encodeBase58Check(keyHash1, 0) address2 = base58.encodeBase58Check(keyHash2, 0) print address1 print address2 #Note: this will fail, unless you change the above addresses to some of your own privKey1 = base58.decodeBase58Check(d.getPrivateKey(address1), 128) privKey2 = base58.decodeBase58Check(d.getPrivateKey(address2), 128) key1 = Key() key1.setPrivateKey(privKey1) key2 = Key() key2.setPrivateKey(privKey2) print key1.getPublicKey().encode("hex") print key2.getPublicKey().encode("hex") print base58.encodeBase58Check(RIPEMD160(SHA256(key1.getPublicKey())), 0) print base58.encodeBase58Check(RIPEMD160(SHA256(key2.getPublicKey())), 0) amount = int(100000 * float(raw_input("Amount to be transferred (mBTC): "))) fee = 10000 #0.1 mBTC outputHash = binascii.unhexlify(raw_input("Input hash (empty: create multisig): "))[::-1]