Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #! /usr/bin/python
- import sys
- sys.path.append('..')
- sys.path.append('.')
- from armoryengine import *
- from sys import argv
- from string import Template
- import os
- import httplib2
- import time
- import getpass
- import json
- def chooseWallet():
- wlts = [ wlt for wlt in os.listdir(ARMORY_HOME_DIR) if wlt.endswith('.wallet') and not wlt.endswith('_backup.wallet') ]
- if len(wlts) == 1:
- return wlts[0]
- else:
- for i, wlt in enumerate(wlts):
- print '%d) %s' % (i, wlt)
- index = int(raw_input('Open which wallet? [%d-%d] ' % (0, len(wlts)-1)))
- return wlts[index]
- wltfile = chooseWallet()
- if wltfile:
- wltfile = os.path.join(ARMORY_HOME_DIR, wltfile)
- else:
- print 'No wallet file found in Armory home directory'
- wlt = PyBtcWallet().readWalletFile(wltfile)
- TheBDM.Reset()
- TheBDM.setBlocking(newTimeout=60)
- TheBDM.registerWallet(wlt.cppWallet)
- try:
- TheBDM.setOnlineMode(goOnline=True)
- except KeyboardInterrupt:
- exit(1)
- print 'Spendable balance: %s BTC' % coin2str(wlt.getBalance('Spendable'))
- total_amt = str2coin(raw_input('Total amount to anonymize? '))
- min_amt = str2coin(raw_input('Minimum amount per address? '))
- max_amt = str2coin(raw_input('Maximum amount per address? '))
- collected = 0
- fee = MIN_TX_FEE
- http = httplib2.Http()
- url = Template('https://blockchain.info/api/receive?method=create&address=$address&anonymous=true')
- logfile = os.path.join(ARMORY_HOME_DIR, 'anonymize.log')
- log = open(logfile, 'w')
- def getForwardingAddress(my_address):
- response = http.request(url.substitute(address=my_address), 'GET')
- if response[0].status != 200:
- raise Exception('Invalid response: ' + response[0].reason)
- else:
- log.write(response.__str__() + '\n')
- return json.loads(response[1])['input_address']
- recipValuePairs = []
- while collected + fee < total_amt:
- amt = random.randint(min_amt, max_amt)
- if amt > total_amt - collected - fee:
- amt = total_amt - collected - fee
- collected += amt
- my_addr = wlt.getNextUnusedAddress()
- fwd_addr = getForwardingAddress(my_addr.getAddrStr())
- wlt.setComment(my_addr.getAddr160(), str('fwd: ' + fwd_addr))
- recip160 = addrStr_to_hash160(fwd_addr)
- recipValuePairs.append( (recip160, amt) )
- log.close()
- utxoList = wlt.getTxOutList()
- utxoSelect = PySelectCoins(utxoList, collected, fee)
- totalTxSelect = sum([u.getValue() for u in utxoSelect])
- totalChange = totalTxSelect - (collected + fee)
- if totalChange > 0:
- change160 = wlt.getNextUnusedAddress().getAddr160()
- recipValuePairs.append( [change160, totalChange])
- random.shuffle(recipValuePairs)
- txdp = PyTxDistProposal().createFromTxOutSelection(utxoSelect, recipValuePairs)
- print '********************************************************************************'
- print 'PLEASE VERIFY THE TRANSACTION DETAILS'
- print '********************************************************************************'
- btcIn = sum(txdp.inputValues)
- btcOut = sum([o.value for o in txdp.pytxObj.outputs])
- btcFee = btcIn - btcOut
- print ' INPUTS: (%s)' % coin2str(btcIn).strip()
- for i,a160 in enumerate(txdp.inAddr20Lists):
- print ' %s -->\t%s' % (hash160_to_addrStr(a160[0]), coin2str(txdp.inputValues[i]))
- print ' OUTPUTS: (%s)' % coin2str(btcOut).strip()
- for i,txout in enumerate(txdp.pytxObj.outputs):
- a160 = TxOutScriptExtractAddr160(txout.binScript)
- if wlt.hasAddr(a160):
- print ' %s (change) <--\t%s' % (hash160_to_addrStr(a160), coin2str(txout.value))
- else:
- print ' %s <--\t%s' % (hash160_to_addrStr(a160), coin2str(txout.value))
- print ' %s <--\t%s' % ('Fee'.ljust(34), coin2str(btcFee))
- confirm = raw_input('\nDoes this look right? [y/N]: ')
- if not confirm.lower().startswith('y'):
- print 'User did not approve of the transaction. Aborting.'
- exit(0)
- dpid = txdp.uniqueB58
- txdpfile = os.path.join(ARMORY_HOME_DIR, "armory_%s_.unsigned.tx" % dpid)
- # If the wallet is watching only, save the unsigned tx
- if wlt.watchingOnly:
- print 'Wallet is watching only, saving unsigned tx to %s' % txdpfile
- toSave = txdpfile
- try:
- theFile = open(toSave, 'w')
- theFile.write(txdp.serializeAscii())
- theFile.close()
- except IOError:
- LOGEXCEPT('Failed to save file: %s', toSave)
- pass
- exit(0)
- # If the wallet is encrypted, get the passphrase
- elif wlt.useEncryption:
- print 'Please enter your passphrase to unlock your wallet: '
- for ntries in range(3):
- passwd = SecureBinaryData(getpass.getpass('Wallet Passphrase: '))
- if wlt.verifyPassphrase(passwd):
- break;
- print 'Passphrase was incorrect!'
- if ntries==2:
- print 'Wallet could not be unlocked. Aborting.'
- exit(0)
- print 'Correct Passphrase. Unlocking wallet...'
- wlt.unlock(securePassphrase=passwd)
- passwd.destroy()
- try:
- wlt.signTxDistProposal(txdp)
- except WalletLockError:
- print 'Error signing transaction. Wallet is somehow still locked'
- raise
- except:
- print 'Error signing transaction. Unknown reason.'
- raise
- if not txdp.checkTxHasEnoughSignatures():
- print 'Error signing transaction. Most likely this is not the correct wallet.'
- exit(0)
- outfilename = '.'.join(txdpfile.split('.')[:-2] + ['signed.tx'] )
- outfile = open(outfilename, 'w')
- outfile.write(txdp.serializeAscii())
- outfile.close()
- print '\nSigning was successful! The signed transaction is located:'
- print '\t', outfilename, '\n'
- print ''
- print 'The *.signed.tx file can now be broadcast from any computer running '
- print 'Armory in online mode. Click "Offline Transactions" and "Broadcast".'
- print ''
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement