def dictionary(): for i in _27_digit_prime: p = str(i) + str(prime) print( str(p) + " " + str(len(p)) + b.privtoaddr(int(p)) + "\n" + p[::-1] + " " + str(len(p)) + " " + b.privtoaddr(int(p[::-1])))
def send(fromprivkey, toaddr, value): transaction_fee = 20000 print "Sending:", fromprivkey, toaddr, value tx = bitcoin.mksend(bitcoin.history(bitcoin.privtoaddr(fromprivkey)), [toaddr + ":" + str(value)], bitcoin.privtoaddr(fromprivkey), transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def templateReplaceOne(s, Jorig): # FIXME: This is kind of ugly code.. match = re.match(r"\<bitcoin-address\:([a-z]+)\>", s) if match: res = bitcoin.privtoaddr(bitcoin.sha256(match.group(1))) #logging.info("Replace %s with %s" % (match.group(0), res)) return res match = re.match(r"\<hash-of\:([^>]+)\>", s) if match: fn = match.group(1) res = bitcoin.sha256(open(fn).read()) #logging.info("Replace %s with %s" % (match.group(0), res)) return res match = re.match(r"\<signature:([a-z]+)\>", s) if match: msg = "VOTE-FORMAT-VERSION-1 %s %s %s %s %s" % ( templateReplaceOne(Jorig["handle"], Jorig), templateReplaceOne(Jorig["addr"], Jorig), templateReplaceOne(Jorig["ballot_option"], Jorig), templateReplaceOne(Jorig["proposal"], Jorig), templateReplaceOne(Jorig["proposal_meta"], Jorig)) privkey = bitcoin.sha256(match.group(1)) res = bitcoin.ecdsa_sign(msg, privkey) #logging.info("Replace %s with %s" % (match.group(0), res)) return res else: return s
def templateReplaceOne(s, Jorig): # FIXME: This is kind of ugly code.. match=re.match(r"\<bitcoin-address\:([a-z]+)\>", s) if match: res=bitcoin.privtoaddr( bitcoin.sha256(match.group(1))) #logging.info("Replace %s with %s" % (match.group(0), res)) return res match=re.match(r"\<hash-of\:([^>]+)\>", s) if match: fn=match.group(1) res=bitcoin.sha256(open(fn).read()) #logging.info("Replace %s with %s" % (match.group(0), res)) return res match=re.match(r"\<signature:([a-z]+)\>", s) if match: msg="VOTE-FORMAT-VERSION-1 %s %s %s %s %s" % ( templateReplaceOne(Jorig["handle"], Jorig), templateReplaceOne(Jorig["addr"], Jorig), templateReplaceOne(Jorig["ballot_option"], Jorig), templateReplaceOne(Jorig["proposal"], Jorig), templateReplaceOne(Jorig["proposal_meta"], Jorig) ) privkey=bitcoin.sha256(match.group(1)) res=bitcoin.ecdsa_sign(msg, privkey) #logging.info("Replace %s with %s" % (match.group(0), res)) return res else: return s
def _derive_address(self, change_or_deposit, index, master_key): xpriv = bip32_ckd( bip32_ckd(self.get_bip44_master(master_key), change_or_deposit), index) priv = bip32_extract_key(xpriv) address = privtoaddr(priv, self.get_address_byte()) return address, priv
def get(self): if self.request.get('id'): id = int(self.request.get('id')) forwarder = Forwarder.get_by_id(id, parent=forwarders_key()) if forwarder.addressType == 'PrivKey' and forwarder.privateKey != '': forwarder.address = bitcoin.privtoaddr(forwarder.privateKey) forwarder.put() elif forwarder.addressType == 'BIP44': parameters = Parameters.get_or_insert('DefaultConfig') if parameters and (parameters.walletseed != None and parameters.walletseed != ""): seed = parameters.walletseed xpub = BIP44Tools.getTrezorXPUBKeys(seed)[0] if forwarder.walletIndex == 0: forwarder.walletIndex = getNextIndex() forwarder.address = BIP44Tools.getAddressesFromXPUB(xpub, forwarder.walletIndex + 1)[forwarder.walletIndex] forwarder.put() self.redirect('/admin?')
def get(self): if self.request.get('id'): id = int(self.request.get('id')) forwarder = Forwarder.get_by_id(id, parent=forwarders_key()) if forwarder.addressType == 'PrivKey' and forwarder.privateKey != '': forwarder.address = bitcoin.privtoaddr(forwarder.privateKey) forwarder.put() elif forwarder.addressType == 'BIP44': parameters = Parameters.get_or_insert('DefaultConfig') if parameters and (parameters.walletseed != None and parameters.walletseed != ""): seed = parameters.walletseed xpub = BIP44Tools.getTrezorXPUBKeys(seed)[0] if forwarder.walletIndex == 0: forwarder.walletIndex = getNextIndex() forwarder.address = BIP44Tools.getAddressesFromXPUB( xpub, forwarder.walletIndex + 1)[forwarder.walletIndex] forwarder.put() self.redirect('/admin?')
def send_whole_wallet(fromprivkey, toaddr): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) balance = sum(transaction['value'] for transaction in bitcoin.unspent(fromaddress)) assert balance >= transaction_fee tx = bitcoin.mktx(bitcoin.history(fromaddress), [{'value': balance - transaction_fee, 'address': toaddr}]) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def _get_wallet_sha256(): for i in w: HEX=btc.encode_privkey(btc.sha256(str(i)),"hex") addr=btc.privtoaddr(HEX) if(addr==match): print(WIF) else: print(addr)
def read_wallet_file_data(self, filename): self.path = None self.index_cache = [[0, 0]] * self.max_mix_depth path = os.path.join('wallets', filename) if not os.path.isfile(path): if get_network() == 'testnet': log.debug('filename interpreted as seed, only available in ' 'testnet because this probably has lower entropy') return filename else: raise IOError('wallet file not found') self.path = path fd = open(path, 'r') walletfile = fd.read() fd.close() walletdata = json.loads(walletfile) if walletdata['network'] != get_network(): print ('wallet network(%s) does not match ' 'joinmarket configured network(%s)' % ( walletdata['network'], get_network())) sys.exit(0) if 'index_cache' in walletdata: self.index_cache = walletdata['index_cache'] decrypted = False while not decrypted: password = getpass('Enter wallet decryption passphrase: ') password_key = btc.bin_dbl_sha256(password) encrypted_seed = walletdata['encrypted_seed'] try: decrypted_seed = decryptData( password_key, encrypted_seed.decode('hex')).encode('hex') # there is a small probability of getting a valid PKCS7 # padding by chance from a wrong password; sanity check the # seed length if len(decrypted_seed) == 32: decrypted = True else: raise ValueError except ValueError: print('Incorrect password') decrypted = False if self.storepassword: self.password_key = password_key self.walletdata = walletdata if 'imported_keys' in walletdata: for epk_m in walletdata['imported_keys']: privkey = decryptData( password_key, epk_m['encrypted_privkey'].decode( 'hex')).encode('hex') privkey = btc.encode_privkey(privkey, 'hex_compressed') if epk_m['mixdepth'] not in self.imported_privkeys: self.imported_privkeys[epk_m['mixdepth']] = [] self.addr_cache[btc.privtoaddr( privkey, get_p2pk_vbyte())] = (epk_m['mixdepth'], -1, len(self.imported_privkeys[epk_m['mixdepth']])) self.imported_privkeys[epk_m['mixdepth']].append(privkey) return decrypted_seed
def send(fromprivkey, toaddr, value): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) tx = bitcoin.mksend(bitcoin.history(fromaddress), [{ 'value': value, 'address': toaddr }], fromaddress, transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def test_p2p_broadcast(setup_tx_notify): #listen up kids, dont do this to generate private #keys that hold real money, or else you'll be robbed src_privkey = random.getrandbits(256) src_privkey = btc.encode(src_privkey, 16, 64) + '01' src_addr = btc.privtoaddr(src_privkey, magicbyte=get_p2pk_vbyte()) dst_addr = btc.pubtoaddr('03' + btc.encode(random.getrandbits(256), 16), get_p2pk_vbyte()) jm_single().bc_interface.rpc('importaddress', [src_addr, "", False]) jm_single().bc_interface.rpc('importaddress', [dst_addr, "", False]) jm_single().bc_interface.rpc('generatetoaddress', [1, src_addr]) jm_single().bc_interface.rpc('generate', [101]) src_utxos = jm_single().bc_interface.rpc('listunspent', [0, 500, [src_addr]]) inputs = [{ 'output': src_utxos[0]['txid'] + ':' + str(src_utxos[0]['vout']) }] miner_fee = 10000 outs = [{ 'address': dst_addr, 'value': int(src_utxos[0]['amount'] * 1e8) - miner_fee }] tx = btc.mktx(inputs, outs) tx = btc.sign(tx, 0, src_privkey) bad_tx = random.getrandbits(len(tx) * 4) bad_tx = btc.encode(bad_tx, 16, len(tx)) utxo_before = jm_single().bc_interface.rpc('listunspent', [0, 500, [dst_addr]]) #jm_single().bc_interface.rpc('sendrawtransaction', [tx]) pushed = tor_broadcast_tx(tx, None, 'regtest', remote_hostport=('localhost', 18444)) assert pushed pushed = tor_broadcast_tx(tx, None, 'regtest', remote_hostport=('localhost', 18444)) assert not pushed #node should already have the same tx, reject pushed = tor_broadcast_tx(bad_tx, None, 'regtest', remote_hostport=('localhost', 18444)) assert not pushed #bad tx should be rejected jm_single().bc_interface.rpc('generate', [1]) utxo_after = jm_single().bc_interface.rpc('listunspent', [0, 500, [dst_addr]]) return len(utxo_after) - 1 == len(utxo_before)
def getPrivKey(xpriv, i): privkeys = {} priv0 = bitcoin.bip32_ckd(xpriv, 0) privateKey = bitcoin.bip32_ckd(priv0, i) wifKey = bitcoin.encode_privkey(bitcoin.bip32_extract_key(privateKey), 'wif_compressed') address_fromPriv = bitcoin.privtoaddr(wifKey) privkeys[address_fromPriv] = wifKey return privkeys
def make_peer_registration(pk, domain): timestamp = datetime.datetime.now().isoformat() address = privtoaddr(pk) to_sign = "%s%s%s" % (domain, address, timestamp) return { 'domain': domain, 'payout_address': address, 'timestamp': timestamp, 'signature': ecdsa_sign(to_sign, pk) }
def get_keychain(username, n=0): user = user_model.query.filter_by(username=username).first() username = user.username email = user.email SECRET = app.config['SECRET_KEY'] privkey = sha3_hex(username + SECRET + email) address = privtoaddr(privkey, 0x58) return [privkey, address]
def send_whole_wallet(fromprivkey, toaddr): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) balance = sum(transaction['value'] for transaction in bitcoin.unspent(fromaddress)) assert balance >= transaction_fee tx = bitcoin.mktx(bitcoin.history(fromaddress), [{ 'value': balance - transaction_fee, 'address': toaddr }]) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def spend(self, owner): assert isinstance(owner, (str, bytes)) assert len(owner) == 20 # Sign it, so it can be accepted by an address messageHash = utils.sha3(owner + self._pubkey) V, R, S = b.ecdsa_raw_sign(messageHash, self._seckey) recoveredPublicKey = b.ecdsa_raw_recover(messageHash, (V, R, S)) assert b.pubtoaddr(recoveredPublicKey) == b.privtoaddr(self._seckey) # Correctly encoded return ", ".join([ "\"0x%s\"" % (hexlify(self._pubkey), ), "%d" % (V, ), "\"0x%064X\"" % (R, ), "\"0x%064X\"" % (S, ), ])
def test__get_message_prefix(): test_addr = bitcoin.privtoaddr(bitcoin.random_key()) prefix = messaging._get_message_prefix(test_addr) assert prefix == 'a'
def sync_addresses(self, wallet): from joinmarket.wallet import BitcoinCoreWallet if isinstance(wallet, BitcoinCoreWallet): return log.debug('requesting wallet history') wallet_name = self.get_wallet_name(wallet) #TODO It is worth considering making this user configurable: addr_req_count = 20 wallet_addr_list = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: #If we have an index-cache available, we can use it #to decide how much to import (note that this list #*always* starts from index 0 on each branch). #In cases where the Bitcoin Core instance is fresh, #this will allow the entire import+rescan to occur #in 2 steps only. if wallet.index_cache != [[0, 0]] * wallet.max_mix_depth: #Need to request N*addr_req_count where N is least s.t. #N*addr_req_count > index_cache val. This is so that the batching #process in the main loop *always* has already imported enough #addresses to complete. req_count = int(wallet.index_cache[mix_depth][forchange] / addr_req_count) + 1 req_count *= addr_req_count else: #If we have *nothing* - no index_cache, and no info #in Core wallet (imports), we revert to a batching mode #with a default size. #In this scenario it could require several restarts *and* #rescans; perhaps user should set addr_req_count high #(see above TODO) req_count = addr_req_count wallet_addr_list += [wallet.get_new_addr(mix_depth, forchange) for _ in range(req_count)] #Indices are reset here so that the next algorithm step starts #from the beginning of each branch wallet.index[mix_depth][forchange] = 0 # makes more sense to add these in an account called "joinmarket-imported" but its much # simpler to add to the same account here for privkey_list in wallet.imported_privkeys.values(): for privkey in privkey_list: imported_addr = btc.privtoaddr(privkey, magicbyte=get_p2pk_vbyte()) wallet_addr_list.append(imported_addr) imported_addr_list = self.rpc('getaddressesbyaccount', [wallet_name]) if not set(wallet_addr_list).issubset(set(imported_addr_list)): self.add_watchonly_addresses(wallet_addr_list, wallet_name) return buf = self.rpc('listtransactions', [wallet_name, 1000, 0, True]) txs = buf # If the buffer's full, check for more, until it ain't while len(buf) == 1000: buf = self.rpc('listtransactions', [wallet_name, 1000, len(txs), True]) txs += buf # TODO check whether used_addr_list can be a set, may be faster (if # its a hashset) and allows using issubset() here and setdiff() for # finding which addresses need importing # TODO also check the fastest way to build up python lists, i suspect # using += is slow used_addr_list = [tx['address'] for tx in txs if tx['category'] == 'receive'] too_few_addr_mix_change = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: unused_addr_count = 0 last_used_addr = '' breakloop = False while not breakloop: if unused_addr_count >= wallet.gaplimit and \ is_index_ahead_of_cache(wallet, mix_depth, forchange): break mix_change_addrs = [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count) ] for mc_addr in mix_change_addrs: if mc_addr not in imported_addr_list: too_few_addr_mix_change.append((mix_depth, forchange )) breakloop = True break if mc_addr in used_addr_list: last_used_addr = mc_addr unused_addr_count = 0 else: unused_addr_count += 1 #index setting here depends on whether we broke out of the loop #early; if we did, it means we need to prepare the index #at the level of the last used address or zero so as to not #miss any imports in add_watchonly_addresses. #If we didn't, we need to respect the index_cache to avoid #potential address reuse. if breakloop: if last_used_addr == '': wallet.index[mix_depth][forchange] = 0 else: wallet.index[mix_depth][forchange] = \ wallet.addr_cache[last_used_addr][2] + 1 else: if last_used_addr == '': next_avail_idx = max([wallet.index_cache[mix_depth][ forchange], 0]) else: next_avail_idx = max([wallet.addr_cache[last_used_addr][ 2] + 1, wallet.index_cache[mix_depth][forchange]]) wallet.index[mix_depth][forchange] = next_avail_idx wallet_addr_list = [] if len(too_few_addr_mix_change) > 0: indices = [wallet.index[mc[0]][mc[1]] for mc in too_few_addr_mix_change] log.debug('too few addresses in ' + str(too_few_addr_mix_change) + ' at ' + str(indices)) for mix_depth, forchange in too_few_addr_mix_change: wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count * 3) ] self.add_watchonly_addresses(wallet_addr_list, wallet_name) return self.wallet_synced = True
def get_addr(self, mixing_depth, forchange, i): return btc.privtoaddr( self.get_key(mixing_depth, forchange, i), get_p2pk_vbyte())
privkey = btc.wif_compressed_privkey( wallet.get_key(m, forchange, k), get_p2pk_vbyte()) else: privkey = btc.encode_privkey(wallet.get_key(m, forchange, k), 'wif_compressed', get_p2pk_vbyte()) else: privkey = '' if (method == 'displayall' or balance > 0 or (used == ' new' and forchange == 0)): cus_print(' m/0/%d/%d/%03d %-35s%s %.8f btc %s' % (m, forchange, k, addr, used, balance / 1e8, privkey)) if m in wallet.imported_privkeys: cus_print(' import addresses') for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, magicbyte=get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] used = (' used' if balance > 0.0 else 'empty') balance_depth += balance if options.showprivkey: if btc.secp_present: wip_privkey = btc.wif_compressed_privkey( privkey, get_p2pk_vbyte()) else: wip_privkey = btc.encode_privkey(privkey, 'wif_compressed', get_p2pk_vbyte()) else: wip_privkey = ''
def read_wallet_file_data(self, filename, pwd=None): self.path = None self.index_cache = [[0, 0]] * self.max_mix_depth path = os.path.join('wallets', filename) if not os.path.isfile(path): if get_network() == 'testnet': log.debug('filename interpreted as seed, only available in ' 'testnet because this probably has lower entropy') return filename else: raise IOError('wallet file not found') self.path = path fd = open(path, 'r') walletfile = fd.read() fd.close() walletdata = json.loads(walletfile) if walletdata['network'] != get_network(): print ('wallet network(%s) does not match ' 'joinmarket configured network(%s)' % ( walletdata['network'], get_network())) sys.exit(0) if 'index_cache' in walletdata: self.index_cache = walletdata['index_cache'] if self.max_mix_depth > len(self.index_cache): #This can happen e.g. in tumbler when we need more mixdepths #than currently exist. Since we have no info for those extra #depths, we must default to (0,0) (but sync should find used #adddresses). self.index_cache += [[0,0]] * ( self.max_mix_depth - len(self.index_cache)) decrypted = False trieddefault = False while not decrypted: if pwd: password = pwd else: if not trieddefault: password = '' trieddefault = True else: password = getpass('Enter wallet decryption passphrase: ') password_key = btc.bin_dbl_sha256(password) encrypted_seed = walletdata['encrypted_seed'] try: decrypted_seed = decryptData( password_key, encrypted_seed.decode('hex')).encode('hex') # there is a small probability of getting a valid PKCS7 # padding by chance from a wrong password; sanity check the # seed length if len(decrypted_seed) == 32: decrypted = True else: raise ValueError except ValueError: if not trieddefault: print('Incorrect password') if pwd: raise decrypted = False if self.storepassword: self.password_key = password_key self.walletdata = walletdata if 'imported_keys' in walletdata: for epk_m in walletdata['imported_keys']: privkey = decryptData( password_key, epk_m['encrypted_privkey'].decode( 'hex')).encode('hex') #Imported keys are stored as 32 byte strings only, so the #second version below is sufficient, really. if len(privkey) != 64: raise Exception( "Unexpected privkey format; already compressed?:" + privkey) privkey += "01" if epk_m['mixdepth'] not in self.imported_privkeys: self.imported_privkeys[epk_m['mixdepth']] = [] self.addr_cache[btc.privtoaddr( privkey, magicbyte=get_p2pk_vbyte())] = (epk_m['mixdepth'], -1, len(self.imported_privkeys[epk_m['mixdepth']])) self.imported_privkeys[epk_m['mixdepth']].append(privkey) return decrypted_seed
for k in range(wallet.index[m][forchange] + options.gaplimit): addr = wallet.get_addr(m, forchange, k) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] balance_depth += balance used = ('used' if k < wallet.index[m][forchange] else ' new') privkey = btc.encode_privkey(wallet.get_key(m, forchange, k), 'wif_compressed', get_p2pk_vbyte()) if options.showprivkey else '' if method == 'displayall' or balance > 0 or (used == ' new' and forchange==0): printd(' m/0/%d/%d/%03d %-35s%s %.8f btc %s' % (m, forchange, k, addr, used, balance/1e8, privkey)) if m in wallet.imported_privkeys: printd(' import addresses') for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, common.get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] used = (' used' if balance > 0.0 else 'empty') balance_depth += balance wip_privkey = btc.encode_privkey(privkey, 'wif_compressed', get_addr_vbyte()) if options.showprivkey else '' printd(' '*13 + '%-35s%s %.8f btc %s' % (addr, used, balance/1e8, wip_privkey)) total_balance += balance_depth print('for mixdepth=%d balance=%.8fbtc' % (m, balance_depth/1e8)) print 'total balance = %.8fbtc' % (total_balance/1e8) elif method == 'generate' or method == 'recover': if method == 'generate': seed = btc.sha256(os.urandom(64))[:32]
def sign(args): data=stdin.readline()[:-1] # remove newline privkey=bitcoin.sha256(args.handle) print bitcoin.ecdsa_sign(data, privkey) print print bitcoin.privtoaddr(privkey)
def main(): parser = OptionParser( usage= 'usage: %prog [options] [auth utxo] [cjamount] [cjaddr] [changeaddr] [utxos..]', description= 'Creates an unsigned coinjoin transaction. Outputs a partially signed transaction ' + 'hex string. The user must sign their inputs independently and broadcast them. The JoinMarket' + ' protocol requires the taker to have a single p2pk UTXO input to use to authenticate the ' + ' encrypted messages. For this reason you must pass auth utxo and the corresponding private key' ) #for cjamount=0 do a sweep, and ignore change address parser.add_option( '-f', '--txfee', action='store', type='int', dest='txfee', default=10000, help='miner fee contribution, in satoshis, default=10000') parser.add_option( '-w', '--wait-time', action='store', type='float', dest='waittime', help='wait time in seconds to allow orders to arrive, default=5', default=5) parser.add_option('-N', '--makercount', action='store', type='int', dest='makercount', help='how many makers to coinjoin with, default=2', default=2) parser.add_option( '-C', '--choose-cheapest', action='store_true', dest='choosecheapest', default=False, help='override weightened offers picking and choose cheapest') parser.add_option( '-P', '--pick-orders', action='store_true', dest='pickorders', default=False, help='manually pick which orders to take. doesn\'t work while sweeping.' ) parser.add_option('--yes', action='store_true', dest='answeryes', default=False, help='answer yes to everything') #TODO implement #parser.add_option('-n', '--no-network', action='store_true', dest='nonetwork', default=False, # help='dont query the blockchain interface, instead user must supply value of UTXOs on ' + # ' command line in the format txid:output/value-in-satoshi') (options, args) = parser.parse_args() if len(args) < 3: parser.error('Needs a wallet, amount and destination address') sys.exit(0) auth_utxo = args[0] cjamount = int(args[1]) destaddr = args[2] changeaddr = args[3] cold_utxos = args[4:] common.load_program_config() addr_valid1, errormsg1 = validate_address(destaddr) #if amount = 0 dont bother checking changeaddr so user can write any junk if cjamount != 0: addr_valid2, errormsg2 = validate_address(changeaddr) else: addr_valid2 = True if not addr_valid1 or not addr_valid2: if not addr_valid1: print 'ERROR: Address invalid. ' + errormsg1 else: print 'ERROR: Address invalid. ' + errormsg2 return all_utxos = [auth_utxo] + cold_utxos query_result = common.bc_interface.query_utxo_set(all_utxos) if None in query_result: print query_result utxo_data = {} for utxo, data in zip(all_utxos, query_result): utxo_data[utxo] = {'address': data['address'], 'value': data['value']} auth_privkey = raw_input('input private key for ' + utxo_data[auth_utxo]['address'] + ' :') if utxo_data[auth_utxo]['address'] != btc.privtoaddr( auth_privkey, common.get_p2pk_vbyte()): print 'ERROR: privkey does not match auth utxo' return chooseOrdersFunc = None if options.pickorders and amount != 0: #cant use for sweeping chooseOrdersFunc = pick_order elif options.choosecheapest: chooseOrdersFunc = cheapest_order_choose else: #choose randomly (weighted) chooseOrdersFunc = weighted_order_choose common.nickname = random_nick() debug('starting sendpayment') class UnsignedTXWallet(common.AbstractWallet): def get_key_from_addr(self, addr): debug('getting privkey of ' + addr) if btc.privtoaddr(auth_privkey, common.get_p2pk_vbyte()) != addr: raise RuntimeError('privkey doesnt match given address') return auth_privkey wallet = UnsignedTXWallet() irc = IRCMessageChannel(common.nickname) taker = CreateUnsignedTx(irc, wallet, auth_utxo, cjamount, destaddr, changeaddr, utxo_data, options, chooseOrdersFunc) try: debug('starting irc') irc.run() except: debug('CRASHING, DUMPING EVERYTHING') debug_dump_object(wallet, ['addr_cache', 'keys', 'wallet_name', 'seed']) debug_dump_object(taker) import traceback debug(traceback.format_exc())
import bitcoin from bitcoin.deterministic import bip32_harden as h mnemonic = 'saddle observe obtain scare burger nerve electric alone minute east walnut motor omit coyote time' seed = bitcoin.mnemonic_to_seed(mnemonic) mpriv = bitcoin.bip32_master_key(seed) accountroot = mpriv accountroot = bitcoin.bip32_ckd(accountroot, h(44)) accountroot = bitcoin.bip32_ckd(accountroot, h(0)) accountroot = bitcoin.bip32_ckd(accountroot, h(0)) for i in range(19): dkey = bitcoin.bip32_descend(accountroot, 0, i) print(bitcoin.privtoaddr(dkey))
balance += addrvalue["value"] balance_depth += balance used = "used" if k < wallet.index[m][forchange] else " new" privkey = ( btc.encode_privkey(wallet.get_key(m, forchange, k), "wif_compressed", get_p2pk_vbyte()) if options.showprivkey else "" ) if method == "displayall" or balance > 0 or (used == " new" and forchange == 0): cus_print( " m/0/%d/%d/%03d %-35s%s %.8f btc %s" % (m, forchange, k, addr, used, balance / 1e8, privkey) ) if m in wallet.imported_privkeys: cus_print(" import addresses") for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue["address"]: balance += addrvalue["value"] used = " used" if balance > 0.0 else "empty" balance_depth += balance wip_privkey = ( btc.encode_privkey(privkey, "wif_compressed", get_p2pk_vbyte()) if options.showprivkey else "" ) cus_print(" " * 13 + "%-35s%s %.8f btc %s" % (addr, used, balance / 1e8, wip_privkey)) total_balance += balance_depth print("for mixdepth=%d balance=%.8fbtc" % (m, balance_depth / 1e8)) print("total balance = %.8fbtc" % (total_balance / 1e8)) elif method == "generate" or method == "recover": if method == "generate":
def sync_addresses(self, wallet): if isinstance(wallet, common.BitcoinCoreWallet): return common.debug('requesting wallet history') wallet_name = self.get_wallet_name(wallet) addr_req_count = 20 wallet_addr_list = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for i in range(addr_req_count) ] wallet.index[mix_depth][forchange] = 0 #makes more sense to add these in an account called "joinmarket-imported" but its much # simpler to add to the same account here for privkey_list in wallet.imported_privkeys.values(): for privkey in privkey_list: imported_addr = btc.privtoaddr(privkey, common.get_p2pk_vbyte()) wallet_addr_list.append(imported_addr) imported_addr_list = self.rpc('getaddressesbyaccount', [wallet_name]) if not set(wallet_addr_list).issubset(set(imported_addr_list)): self.add_watchonly_addresses(wallet_addr_list, wallet_name) return buf = self.rpc('listtransactions', [wallet_name, 1000, 0, True]) txs = buf # If the buffer's full, check for more, until it ain't while len(buf) == 1000: buf = self.rpc( 'listtransactions', [wallet_name, 1000, len(txs), True]) txs += buf #TODO check whether used_addr_list can be a set, may be faster (if its a hashset) and allows # using issubset() here and setdiff() for finding which addresses need importing #TODO also check the fastest way to build up python lists, i suspect using += is slow used_addr_list = [ tx['address'] for tx in txs if tx['category'] == 'receive' ] too_few_addr_mix_change = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: unused_addr_count = 0 last_used_addr = '' breakloop = False while not breakloop: if unused_addr_count >= wallet.gaplimit and\ is_index_ahead_of_cache(wallet, mix_depth, forchange): break mix_change_addrs = [ wallet.get_new_addr(mix_depth, forchange) for i in range(addr_req_count) ] for mc_addr in mix_change_addrs: if mc_addr not in imported_addr_list: too_few_addr_mix_change.append( (mix_depth, forchange)) breakloop = True break if mc_addr in used_addr_list: last_used_addr = mc_addr unused_addr_count = 0 else: unused_addr_count += 1 if last_used_addr == '': wallet.index[mix_depth][forchange] = 0 else: wallet.index[mix_depth][ forchange] = wallet.addr_cache[last_used_addr][2] + 1 wallet_addr_list = [] if len(too_few_addr_mix_change) > 0: common.debug('too few addresses in ' + str(too_few_addr_mix_change)) for mix_depth, forchange in too_few_addr_mix_change: wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for i in range(addr_req_count * 3) ] self.add_watchonly_addresses(wallet_addr_list, wallet_name) return self.wallet_synced = True
def send(fromprivkey, toaddr, value): transaction_fee = 20000 # .0002 BTC fromaddress = bitcoin.privtoaddr(fromprivkey) tx = bitcoin.mksend(bitcoin.history(fromaddress), [{'value': value, 'address': toaddr}], fromaddress, transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def read_wallet_file_data(self, filename, pwd=None): self.path = None self.index_cache = [[0, 0]] * self.max_mix_depth path = os.path.join('wallets', filename) if not os.path.isfile(path): if get_network() == 'testnet': log.debug('filename interpreted as seed, only available in ' 'testnet because this probably has lower entropy') return filename else: raise IOError('wallet file not found') self.path = path fd = open(path, 'r') walletfile = fd.read() fd.close() walletdata = json.loads(walletfile) if walletdata['network'] != get_network(): print('wallet network(%s) does not match ' 'joinmarket configured network(%s)' % (walletdata['network'], get_network())) sys.exit(0) if 'index_cache' in walletdata: self.index_cache = walletdata['index_cache'] if self.max_mix_depth > len(self.index_cache): #This can happen e.g. in tumbler when we need more mixdepths #than currently exist. Since we have no info for those extra #depths, we must default to (0,0) (but sync should find used #adddresses). self.index_cache += [[0, 0]] * (self.max_mix_depth - len(self.index_cache)) decrypted = False while not decrypted: if pwd: password = pwd else: password = getpass('Enter wallet decryption passphrase: ') password_key = btc.bin_dbl_sha256(password) encrypted_seed = walletdata['encrypted_seed'] try: decrypted_seed = decryptData( password_key, encrypted_seed.decode('hex')).encode('hex') # there is a small probability of getting a valid PKCS7 # padding by chance from a wrong password; sanity check the # seed length if len(decrypted_seed) == 32: decrypted = True else: raise ValueError except ValueError: print('Incorrect password') if pwd: raise decrypted = False if self.storepassword: self.password_key = password_key self.walletdata = walletdata if 'imported_keys' in walletdata: for epk_m in walletdata['imported_keys']: privkey = decryptData( password_key, epk_m['encrypted_privkey'].decode('hex')).encode('hex') #Imported keys are stored as 32 byte strings only, so the #second version below is sufficient, really. if len(privkey) != 64: raise Exception( "Unexpected privkey format; already compressed?:" + privkey) privkey += "01" if epk_m['mixdepth'] not in self.imported_privkeys: self.imported_privkeys[epk_m['mixdepth']] = [] self.addr_cache[btc.privtoaddr( privkey, magicbyte=get_p2pk_vbyte())] = ( epk_m['mixdepth'], -1, len(self.imported_privkeys[epk_m['mixdepth']])) self.imported_privkeys[epk_m['mixdepth']].append(privkey) return decrypted_seed
import bitcoin from bitcoin.deterministic import bip32_harden as h mnemonic='saddle observe obtain scare burger nerve electric alone minute east walnut motor omit coyote time' seed=bitcoin.mnemonic_to_seed(mnemonic) mpriv=bitcoin.bip32_master_key(seed) accountroot=mpriv accountroot=bitcoin.bip32_ckd(accountroot,h(44)) accountroot=bitcoin.bip32_ckd(accountroot,h(0)) accountroot=bitcoin.bip32_ckd(accountroot,h(0)) for i in range(19): dkey=bitcoin.bip32_descend(accountroot,0,i) print(bitcoin.privtoaddr(dkey))
def forward(self, forwarder): data = {} parameters = Parameters.get_or_insert('DefaultConfig') url = parameters.blockchaindataURL + '/utxos?confirmations=' + str(REQUIRED_CONFIRMATIONS) + '&addresses=' + str(forwarder.address) try: ret = urllib2.urlopen(urllib2.Request(url)) data = json.loads(ret.read()) except: data = {} if 'success' in data and data['success'] == 1 and 'UTXOs' in data: if len(data['UTXOs']) > 0: self.response.write("<br>Found UTXO(s) for: " + str(forwarder.name) + " on " + str(forwarder.address)) logging.info("Found UTXO(s) for: " + str(forwarder.name) + " on " + str(forwarder.address)) for UTXO in data['UTXOs']: self.response.write('<br>Forwarding ' + str(UTXO['value']) + ' from tx ' + UTXO['output']) logging.info('Forwarding ' + str(UTXO['value']) + ' from tx ' + UTXO['output']) to_addresses = [] amounts = [] data = {} url = parameters.blockchaindataURL + '/primeInputAddress?txid=' + UTXO['output'].split(":")[0] try: ret = urllib2.urlopen(urllib2.Request(url)) data = json.loads(ret.read()) except: data = {} if 'success' in data and data['success'] == 1: primeInputAddress = data['PrimeInputAddress'] else: continue url = parameters.blocklinkerURL + '/?address=' + str(forwarder.address) + '&xpub=' + forwarder.xpub + '&metric=LAL&format=json' data = {} try: ret = urllib2.urlopen(urllib2.Request(url)) data = json.loads(ret.read()) except: data = {} if 'success' in data and data['success'] == 1 : LAL = data['LAL'] for i in range(0, len(LAL)): if LAL[i][0] == primeInputAddress: to_addresses.append(LAL[i][1]) amounts.append(UTXO['value']) success = False privKeys = {} if forwarder.addressType == 'PrivKey': address = bitcoin.privtoaddr(forwarder.privateKey) privKeys = {address: forwarder.privateKey} elif forwarder.addressType == 'BIP44': parameters = Parameters.get_or_insert('DefaultConfig') if parameters and parameters.walletseed != '' and parameters.walletseed != None: seed = parameters.walletseed xprivKeys = BIP44Tools.getTrezorXPRIVKeys(seed, "", 1) privKeys = BIP44Tools.getPrivKey(xprivKeys[0], forwarder.walletIndex) if len(amounts) > 0 and forwarder.minimumAmount > 0 and amounts[0] < forwarder.minimumAmount: self.response.write("<br>" + str(amounts[0]) + " is below minimum of " + str(forwarder.minimumAmount) + "! returning btc to sender") logging.warning(str(amounts[0]) + " is below minimum of " + str(forwarder.minimumAmount) + "! returning btc to sender") to_addresses = [primeInputAddress] #if there is enough btc, subtract network fee, otherwise log a warning if amounts[0] > TRANSACTION_FEE: #subtract network fee in satoshis from first amount amounts[0] = amounts[0] - TRANSACTION_FEE outputs = [] outputs.append({'address': to_addresses[0], 'value': amounts[0]}) self.response.write("<br>Returning " + str(amounts[0]) + " to " + to_addresses[0]) logging.info("Returning " + str(amounts[0]) + " to " + to_addresses[0]) success = self.sendCustomTransaction(privKeys, [UTXO], outputs, TRANSACTION_FEE) else: self.response.write("<br>Insufficient amount to send, please remove UTXO manually as soon as possible.") logging.warning("Insufficient amount to send, please remove UTXO manually as soon as possible.") elif len(to_addresses) > 0: if forwarder.feePercent > 0.0 and forwarder.feeAddress != '': fee = int(amounts[0] * forwarder.feePercent/100) amounts = [amounts[0] - fee, fee] to_addresses.append(forwarder.feeAddress) self.response.write("<br>Forwarding Fee: " + str(amounts[1]) + " -> " + str(to_addresses[1])) logging.info("Forwarding Fee: " + str(amounts[1]) + " -> " + str(to_addresses[1])) if forwarder.confirmAmount > 0: amounts[0] -= forwarder.confirmAmount amounts.append(forwarder.confirmAmount) to_addresses.append(primeInputAddress) self.response.write("<br>Origin: " + str(forwarder.confirmAmount) + " -> " + primeInputAddress) logging.info("Origin: " + str(forwarder.confirmAmount) + " -> " + primeInputAddress) #subtract transaction fee in satoshis from first amount amounts[0] = amounts[0] - TRANSACTION_FEE self.response.write("<br>Destination: " + str(amounts[0]) + " -> " + to_addresses[0]) logging.info("Destination: " + str(amounts[0]) + " -> " + to_addresses[0]) if amounts[0] > 0: outputs = [] for i in range(0, len(amounts)): outputs.append({'address': to_addresses[i], 'value': amounts[i]}) success = self.sendCustomTransaction(privKeys, [UTXO], outputs, TRANSACTION_FEE) else: self.response.write("Not enough balance left to send Transaction") logging.error("Not enough balance left to send Transaction") if success == True: self.response.write("<br>Success<br><br>" ) logging.info("Success") else: self.response.write("<br>Failed to send transaction<br><br>" ) logging.error("Failed to send transaction") else: self.response.write("<br>Failed to retrieve data from blocklinker<br><br>" ) logging.error("Failed to retrieve data from blocklinker" )
def _derive_address(self, change_or_deposit, index, master_key): xpriv = bip32_ckd(bip32_ckd(self.get_bip44_master( master_key), change_or_deposit), index) priv = bip32_extract_key(xpriv) address = privtoaddr(priv, self.get_address_byte()) return address, priv
def send(fromprivkey, toaddr, value): transaction_fee = 20000 print "Sending:", fromprivkey, toaddr, value tx = bitcoin.mksend(bitcoin.history(bitcoin.privtoaddr(fromprivkey)), [toaddr+":"+str(value)], bitcoin.privtoaddr(fromprivkey), transaction_fee) signed_tx = bitcoin.sign(tx, 0, fromprivkey) bitcoin.pushtx(signed_tx)
def nextWord(currSeed, nextPos): global seedCount global f if nextPos < len(sys.argv) - 1: if len(seedStruct[nextPos]) > 1: for x in range(seedStruct[nextPos]['min'], seedStruct[nextPos]['max'] + 1): #print("WordLength: " + str(x) + "------------------------------") for theWord in words[x]: currSeed += theWord + " " nextWord(currSeed, nextPos + 1) currSeed = currSeed.strip().rsplit(' ', 1)[0] + " " else: currSeed += seedStruct[nextPos]['word'] + " " nextWord(currSeed, nextPos + 1) currSeed = currSeed.strip().rsplit(' ', 1)[0] + " " else: if len(seedStruct[nextPos]) > 1: for x in range(seedStruct[nextPos]['min'], seedStruct[nextPos]['max'] + 1): #print("WordLength: " + str(x) + "------------------------------") for theWord in words[x]: currSeed += theWord else: currSeed += seedStruct[nextPos]['word'] seedCount += 1 #print("Seed:" + currSeed) try: entropy = binascii.hexlify(m.to_entropy(currSeed)) hexSeed = binascii.hexlify(m.to_seed(currSeed)) print("Found valid seed!") #print(hexSeed) print("Seed: " + currSeed) output = open('keys_' + str(seedCount) + '.txt', 'w') output.write('Seed: ' + currSeed + '\n') xprv = bitcoin.bip32_master_key(binascii.unhexlify(hexSeed)) #xprvReceive = bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 2**31),0) #m/0'/0 #xprvChange = bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 2**31),1) #m/0'/1 #m/44'/0'/0'/0 - Receive xprvReceive = bitcoin.bip32_ckd( bitcoin.bip32_ckd( bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 44 + 2**31), 2**31), 2**31), 0) #m/44'/0'/0'/1 - Change xprvChange = bitcoin.bip32_ckd( bitcoin.bip32_ckd( bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 44 + 2**31), 2**31), 2**31), 1) rcvAddr = [] chgAddr = [] rcvPrivKey = [] chgPrivKey = [] sys.stdout.write("Generating Seed Keys/Addresses.") for x in range(0, 100): if x % 10 == 0: sys.stdout.write(".") childprivReceive = bitcoin.bip32_ckd(xprvReceive, x) childprivChange = bitcoin.bip32_ckd(xprvChange, x) pkeyReceive = bitcoin.bip32_extract_key(childprivReceive) pkeyChange = bitcoin.bip32_extract_key(childprivChange) rcvAddr.append(bitcoin.privtoaddr(pkeyReceive)) chgAddr.append(bitcoin.privtoaddr(pkeyChange)) rcvPrivKey.append( bitcoin.encode_privkey(pkeyReceive, 'wif_compressed')) chgPrivKey.append( bitcoin.encode_privkey(pkeyChange, 'wif_compressed')) for x in range(0, 100): output.write(rcvPrivKey[x] + '\n') output.write(chgPrivKey[x] + '\n') output.write( '------ END KEYS ------------------------------------\n') for x in range(0, 100): output.write(rcvAddr[x] + '\n') output.write(chgAddr[x] + '\n') output.write( '------ END ADDRESSES -------------------------------\n') output.close() print("Dumped!") except ValueError: sys.stdout.write('.') if seedCount % 1000000 == 0: print(str(seedCount) + ' Seeds ' + str(datetime.now())) f.write((str(seedCount) + ' Seeds ' + str(datetime.now())) + '\n') currSeed = currSeed.strip().rsplit(' ', 1)[0] + " "
balance_depth += balance used = ('used' if k < wallet.index[m][forchange] else ' new') if options.showprivkey: privkey = btc.wif_compressed_privkey( wallet.get_key(m, forchange, k), get_p2pk_vbyte()) else: privkey = '' if (method == 'displayall' or balance > 0 or (used == ' new' and forchange == 0)): cus_print( ' m/0/%d/%d/%03d %-35s%s %.8f btc %s' % (m, forchange, k, addr, used, balance / 1e8, privkey)) if m in wallet.imported_privkeys: cus_print(' import addresses') for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, magicbyte=get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] used = (' used' if balance > 0.0 else 'empty') balance_depth += balance if options.showprivkey: wip_privkey = btc.wif_compressed_privkey( privkey, get_p2pk_vbyte()) else: wip_privkey = '' cus_print(' ' * 13 + '%-35s%s %.8f btc %s' % (addr, used, balance / 1e8, wip_privkey)) total_balance += balance_depth print('for mixdepth=%d balance=%.8fbtc' % (m, balance_depth / 1e8))
import bitcoin import hashlib auguri = 'A Mirko e Rossana i nostri più sinceri auguri, felici di vivere questo giorno con voi!' for x in range(0, 20170729): auguri = hashlib.sha256(auguri.encode()).hexdigest() print(auguri) print(bitcoin.privtoaddr(auguri))
if addr == addrvalue['address']: balance += addrvalue['value'] balance_depth += balance used = ('used' if k < wallet.index[m][forchange] else ' new') privkey = btc.encode_privkey( wallet.get_key(m, forchange, k), 'wif_compressed', get_p2pk_vbyte()) if options.showprivkey else '' if (method == 'displayall' or balance > 0 or (used == ' new' and forchange == 0)): cus_print(' m/0/%d/%d/%03d %-35s%s %.8f btc %s' % (m, forchange, k, addr, used, balance / 1e8, privkey)) if m in wallet.imported_privkeys: cus_print(' import addresses') for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] used = (' used' if balance > 0.0 else 'empty') balance_depth += balance wip_privkey = btc.encode_privkey( privkey, 'wif_compressed', get_p2pk_vbyte()) if options.showprivkey else '' cus_print(' ' * 13 + '%-35s%s %.8f btc %s' % ( addr, used, balance / 1e8, wip_privkey)) total_balance += balance_depth print('for mixdepth=%d balance=%.8fbtc' % (m, balance_depth / 1e8)) print('total balance = %.8fbtc' % (total_balance / 1e8)) elif method == 'generate' or method == 'recover':
if addr == addrvalue['address']: balance += addrvalue['value'] balance_depth += balance used = ('used' if k < wallet.index[m][forchange] else ' new') privkey = btc.encode_privkey( wallet.get_key(m, forchange, k), 'wif_compressed', get_p2pk_vbyte()) if options.showprivkey else '' if method == 'displayall' or balance > 0 or (used == ' new' and forchange == 0): printd( ' m/0/%d/%d/%03d %-35s%s %.8f btc %s' % (m, forchange, k, addr, used, balance / 1e8, privkey)) if m in wallet.imported_privkeys: printd(' import addresses') for privkey in wallet.imported_privkeys[m]: addr = btc.privtoaddr(privkey, common.get_p2pk_vbyte()) balance = 0.0 for addrvalue in wallet.unspent.values(): if addr == addrvalue['address']: balance += addrvalue['value'] used = (' used' if balance > 0.0 else 'empty') balance_depth += balance wip_privkey = btc.encode_privkey( privkey, 'wif_compressed', get_p2pk_vbyte()) if options.showprivkey else '' printd(' ' * 13 + '%-35s%s %.8f btc %s' % (addr, used, balance / 1e8, wip_privkey)) total_balance += balance_depth print('for mixdepth=%d balance=%.8fbtc' % (m, balance_depth / 1e8)) print 'total balance = %.8fbtc' % (total_balance / 1e8) elif method == 'generate' or method == 'recover':
def sync_addresses(self, wallet): from joinmarket.wallet import BitcoinCoreWallet if isinstance(wallet, BitcoinCoreWallet): return log.debug('requesting wallet history') wallet_name = self.get_wallet_name(wallet) #TODO It is worth considering making this user configurable: addr_req_count = 20 wallet_addr_list = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: #If we have an index-cache available, we can use it #to decide how much to import (note that this list #*always* starts from index 0 on each branch). #In cases where the Bitcoin Core instance is fresh, #this will allow the entire import+rescan to occur #in 2 steps only. if wallet.index_cache != [[0, 0]] * wallet.max_mix_depth: #Need to request N*addr_req_count where N is least s.t. #N*addr_req_count > index_cache val. This is so that the batching #process in the main loop *always* has already imported enough #addresses to complete. req_count = int(wallet.index_cache[mix_depth][forchange] / addr_req_count) + 1 req_count *= addr_req_count else: #If we have *nothing* - no index_cache, and no info #in Core wallet (imports), we revert to a batching mode #with a default size. #In this scenario it could require several restarts *and* #rescans; perhaps user should set addr_req_count high #(see above TODO) req_count = addr_req_count wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for _ in range(req_count) ] #Indices are reset here so that the next algorithm step starts #from the beginning of each branch wallet.index[mix_depth][forchange] = 0 # makes more sense to add these in an account called "joinmarket-imported" but its much # simpler to add to the same account here for privkey_list in wallet.imported_privkeys.values(): for privkey in privkey_list: imported_addr = btc.privtoaddr(privkey, magicbyte=get_p2pk_vbyte()) wallet_addr_list.append(imported_addr) imported_addr_list = self.rpc('getaddressesbyaccount', [wallet_name]) if not set(wallet_addr_list).issubset(set(imported_addr_list)): self.add_watchonly_addresses(wallet_addr_list, wallet_name) return buf = self.rpc('listtransactions', [wallet_name, 1000, 0, True]) txs = buf # If the buffer's full, check for more, until it ain't while len(buf) == 1000: buf = self.rpc( 'listtransactions', [wallet_name, 1000, len(txs), True]) txs += buf # TODO check whether used_addr_list can be a set, may be faster (if # its a hashset) and allows using issubset() here and setdiff() for # finding which addresses need importing # TODO also check the fastest way to build up python lists, i suspect # using += is slow used_addr_list = [ tx['address'] for tx in txs if tx['category'] == 'receive' ] too_few_addr_mix_change = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: unused_addr_count = 0 last_used_addr = '' breakloop = False while not breakloop: if unused_addr_count >= wallet.gaplimit and \ is_index_ahead_of_cache(wallet, mix_depth, forchange): break mix_change_addrs = [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count) ] for mc_addr in mix_change_addrs: if mc_addr not in imported_addr_list: too_few_addr_mix_change.append( (mix_depth, forchange)) breakloop = True break if mc_addr in used_addr_list: last_used_addr = mc_addr unused_addr_count = 0 else: unused_addr_count += 1 #index setting here depends on whether we broke out of the loop #early; if we did, it means we need to prepare the index #at the level of the last used address or zero so as to not #miss any imports in add_watchonly_addresses. #If we didn't, we need to respect the index_cache to avoid #potential address reuse. if breakloop: if last_used_addr == '': wallet.index[mix_depth][forchange] = 0 else: wallet.index[mix_depth][forchange] = \ wallet.addr_cache[last_used_addr][2] + 1 else: if last_used_addr == '': next_avail_idx = max( [wallet.index_cache[mix_depth][forchange], 0]) else: next_avail_idx = max([ wallet.addr_cache[last_used_addr][2] + 1, wallet.index_cache[mix_depth][forchange] ]) wallet.index[mix_depth][forchange] = next_avail_idx wallet_addr_list = [] if len(too_few_addr_mix_change) > 0: indices = [ wallet.index[mc[0]][mc[1]] for mc in too_few_addr_mix_change ] log.debug('too few addresses in ' + str(too_few_addr_mix_change) + ' at ' + str(indices)) for mix_depth, forchange in too_few_addr_mix_change: wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count * 3) ] self.add_watchonly_addresses(wallet_addr_list, wallet_name) return self.wallet_synced = True
def post(self): error = '' forwarderID = 0 response = {'success': 0} parameters = Parameters.get_or_insert('DefaultConfig') if 'API-Key' in self.request.headers: APIkey = self.request.headers['API-Key'] if APIkey != parameters.APIkey and parameters.APIkey != '': error = 'Incorrect APIkey' response = {'success': 0, 'error': error} else: error = 'No APIkey supplied' response = {'success': 0, 'error': error} if 'API-Sign' in self.request.headers: signature = str(self.request.headers['API-Sign']) postdata = self.request.body message = hashlib.sha256(postdata).digest() if signature != base64.b64encode(hmac.new(base64.b64decode(parameters.APIsecret), message, hashlib.sha512).digest()): error = 'Invalid signature' response = {'success': 0, 'error': error} else: error = 'No signature supplied' response = {'success': 0, 'error': error} if self.request.get('ID') and error == '': try: forwarderID = int(self.request.get('ID')) except ValueError: error = 'ID must be an integer' response = {'success': 0, 'error': error} if error == '': forwarder = Forwarder(parent=forwarders_key()) if forwarderID != 0: forwarder = Forwarder.get_by_id(forwarderID, parent=forwarders_key()) if self.request.get('Name'): if len(self.request.get('Name')) > 0: forwarder.name = self.request.get('Name') else: error = 'Name cannot be empty' if self.request.get('Description'): forwarder.description = self.request.get('Description') if self.request.get('Creator'): forwarder.creator = self.request.get('Creator') if self.request.get('CreatorEmail'): if validEmail(self.request.get('CreatorEmail')): forwarder.creatorEmail = self.request.get('CreatorEmail') else: error = 'Invalid email address' if self.request.get('XPUB'): if validXPUB(self.request.get('XPUB')): forwarder.xpub = self.request.get('XPUB') else: error = 'Invalid XPUB key' if len(forwarder.xpub) == 0: error = 'XPUB key cannot be empty' if self.request.get('MinimumAmount'): amount = -1 try: amount = int(self.request.get('MinimumAmount')) except ValueError: error = 'MinimumAmount must be a positive integer or equal to 0 (in Satoshis)' if amount >= 0: forwarder.minimumAmount = amount else: error = 'MinimumAmount must be a positive integer or equal to 0 (in Satoshis)' if self.request.get('Youtube'): forwarder.youtube = self.request.get('Youtube') if self.request.get('Visibility'): if self.request.get('Visibility') in ['Public', 'Private']: forwarder.visibility = self.request.get('Visibility') else: error = 'Visibility must be Public or Private' if self.request.get('Status'): if self.request.get('Status') in ['Pending', 'Active', 'Disabled']: forwarder.status = self.request.get('Status') else: error = 'Status must be Pending, Active or Disabled' if self.request.get('FeePercent'): percentage = -1 try: percentage = float(self.request.get('FeePercent')) except ValueError: error = 'Incorrect Fee percentage' if percentage >= 0: forwarder.feePercent = percentage else: error = 'FeePercent must be greater than or equal to 0' if self.request.get('FeeAddress'): forwarder.feeAddress = self.request.get('FeeAddress') if self.request.get('ConfirmAmount'): amount = -1 try: amount = int(self.request.get('ConfirmAmount')) except ValueError: error = 'ConfirmAmount must be a positive integer or equal to 0 (in Satoshis)' if amount >= 0: forwarder.confirmAmount = amount else: error = 'ConfirmAmount must be greater than or equal to 0 (in Satoshis)' if self.request.get('AddressType'): if self.request.get('AddressType') in ['PrivKey', 'BIP44']: forwarder.addressType = self.request.get('AddressType') else: error = 'AddressType must be BIP44 or PrivKey' if self.request.get('WalletIndex'): index = -1 try: index = int(self.request.get('WalletIndex')) except ValueError: error = 'WalletIndex must be a positive integer' if index >= 0: forwarder.walletIndex = index else: error = 'Wallet index must be greater than or equal to 0' if self.request.get('PrivateKey'): forwarder.privateKey = self.request.get('PrivateKey') if error == '': forwarder.put() if forwarder.addressType == 'PrivKey' and forwarder.privateKey != '': newAddress = bitcoin.privtoaddr(forwarder.privateKey) if forwarder.address != newAddress: forwarder.address = newAddress forwarder.put() elif forwarder.addressType == 'BIP44': if parameters and (parameters.walletseed != None and parameters.walletseed != ""): if forwarder.walletIndex == 0: forwarder.walletIndex = getNextIndex() #bugfix: when creating the first forwarder, set index to 1 if forwarder.walletIndex == 0: forwarder.walletIndex = 1 seed = parameters.walletseed xpub = BIP44Tools.getTrezorXPUBKeys(seed)[0] forwarder.address = BIP44Tools.getAddressesFromXPUB(xpub, forwarder.walletIndex + 1)[forwarder.walletIndex] forwarder.put() response['success'] = 1 tmpForwarder = {} tmpForwarder['ID'] = forwarder.key.id() tmpForwarder['Name'] = forwarder.name tmpForwarder['Address'] = forwarder.address tmpForwarder['Description'] = forwarder.description tmpForwarder['Creator'] = forwarder.creator tmpForwarder['CreatorEmail'] = forwarder.creatorEmail tmpForwarder['Youtube'] = forwarder.youtube tmpForwarder['Status'] = forwarder.status tmpForwarder['ConfirmAmount'] = forwarder.confirmAmount tmpForwarder['FeeAddress'] = forwarder.feeAddress tmpForwarder['FeePercent'] = forwarder.feePercent tmpForwarder['MinimumAmount'] = forwarder.minimumAmount tmpForwarder['XPUB'] = forwarder.xpub tmpForwarder['Visibility'] = forwarder.visibility tmpForwarder['Date'] = int(time.mktime(forwarder.date.timetuple())) response['forwarder'] = tmpForwarder self.response.write(json.dumps(response)) else: response['success'] = 0 response['error'] = error self.response.write(json.dumps(response)) else: self.response.write(json.dumps(response)) else: self.response.write(json.dumps(response))
def get_key_from_addr(self, addr): debug('getting privkey of ' + addr) if btc.privtoaddr(auth_privkey, common.get_p2pk_vbyte()) != addr: raise RuntimeError('privkey doesnt match given address') return auth_privkey
return int2bytes(int.from_bytes(bytes.fromhex(str), enc1), enc2).hex() #1 prime = 957496696762772407663 #2 second = b58decode_int('SatoshiNakamoto', alphabet=BITCOIN_ALPHABET) #124728751148945267645137860 hex2 = (str('%x' % second)) #672c5725fa8fc1aa52c3c4 little_big2 = convert_hex(hex2, 'big', 'little') #c4c352aac18ffa25572c67 second_sol = int(little_big2, 16) #237871847045914904726285415 #3 third = b58decode_int('Phemex', alphabet=RIPPLE_ALPHABET) #14899878097 hex3 = (str('%010x' % third)) #035f251581 little_big3 = convert_hex(hex3, 'big', 'little') #d1181a7803 third_sol = int(little_big3, 16) #554405551875 #END big = prime * second_sol * third_sol #126272244427365764086102017718794198001099243823071433146875 hex = (str('%064x' % big) ) #00000000000000141dc7bec50472bb381be8e18f6d6b397773d71fc5d91d41fb uncompressed = bitcoin.privtoaddr(hex) #1LPmwxe59KD6oEJGYinx7Li1oCSRPCSNDY WIFC = bitcoin.encode_privkey( hex, 'wif_compressed') #KwDiBf89QgGfm2CrqioD77Q1g7urAhFcGUyUCQP3GdGAwCQRszmY compressed = bitcoin.privtoaddr(WIFC) #1h8BNZkhsPiu6EKazP19WkGxDw3jHf9aT print('COMPRESSED WIF:', WIFC) print('UNCOMPRESSED ADDRESS:', uncompressed) print('COMPRESSED ADDRESS:', compressed)
def sync_addresses(self, wallet): from joinmarket.wallet import BitcoinCoreWallet if isinstance(wallet, BitcoinCoreWallet): return log.debug('requesting wallet history') wallet_name = self.get_wallet_name(wallet) addr_req_count = 20 wallet_addr_list = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: wallet_addr_list += [wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count)] wallet.index[mix_depth][forchange] = 0 # makes more sense to add these in an account called "joinmarket-imported" but its much # simpler to add to the same account here for privkey_list in wallet.imported_privkeys.values(): for privkey in privkey_list: imported_addr = btc.privtoaddr(privkey, get_p2pk_vbyte()) wallet_addr_list.append(imported_addr) imported_addr_list = self.rpc('getaddressesbyaccount', [wallet_name]) if not set(wallet_addr_list).issubset(set(imported_addr_list)): self.add_watchonly_addresses(wallet_addr_list, wallet_name) return buf = self.rpc('listtransactions', [wallet_name, 1000, 0, True]) txs = buf # If the buffer's full, check for more, until it ain't while len(buf) == 1000: buf = self.rpc('listtransactions', [wallet_name, 1000, len(txs), True]) txs += buf # TODO check whether used_addr_list can be a set, may be faster (if # its a hashset) and allows using issubset() here and setdiff() for # finding which addresses need importing # TODO also check the fastest way to build up python lists, i suspect # using += is slow used_addr_list = [tx['address'] for tx in txs if tx['category'] == 'receive'] too_few_addr_mix_change = [] for mix_depth in range(wallet.max_mix_depth): for forchange in [0, 1]: unused_addr_count = 0 last_used_addr = '' breakloop = False while not breakloop: if unused_addr_count >= wallet.gaplimit and \ is_index_ahead_of_cache(wallet, mix_depth, forchange): break mix_change_addrs = [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count)] for mc_addr in mix_change_addrs: if mc_addr not in imported_addr_list: too_few_addr_mix_change.append( (mix_depth, forchange)) breakloop = True break if mc_addr in used_addr_list: last_used_addr = mc_addr unused_addr_count = 0 else: unused_addr_count += 1 if last_used_addr == '': wallet.index[mix_depth][forchange] = 0 else: wallet.index[mix_depth][forchange] = \ wallet.addr_cache[last_used_addr][2] + 1 wallet_addr_list = [] if len(too_few_addr_mix_change) > 0: log.debug('too few addresses in ' + str(too_few_addr_mix_change)) for mix_depth, forchange in too_few_addr_mix_change: wallet_addr_list += [ wallet.get_new_addr(mix_depth, forchange) for _ in range(addr_req_count * 3)] self.add_watchonly_addresses(wallet_addr_list, wallet_name) return self.wallet_synced = True
# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import bitcoin import datetime as dt start = dt.datetime.now() print start gens = {} for e in range(1000): key = bitcoin.random_key() gens[bitcoin.privtoaddr(key)] = key with open("generadas.txt", "w") as oF: for e in gens.keys(): oF.write(e + "\t" + gens[e]) end = dt.datetime.now() print end
def get_addr(self, mixing_depth, forchange, i): return btc.privtoaddr(self.get_key(mixing_depth, forchange, i), get_p2pk_vbyte())
def main(): parser = OptionParser( usage='usage: %prog [options] [auth utxo] [cjamount] [cjaddr] [' 'changeaddr] [utxos..]', description=('Creates an unsigned coinjoin transaction. Outputs ' 'a partially signed transaction hex string. The user ' 'must sign their inputs independently and broadcast ' 'them. The JoinMarket protocol requires the taker to ' 'have a single p2pk UTXO input to use to ' 'authenticate the encrypted messages. For this ' 'reason you must pass auth utxo and the ' 'corresponding private key')) # for cjamount=0 do a sweep, and ignore change address parser.add_option( '-f', '--txfee', action='store', type='int', dest='txfee', default=10000, help='total miner fee in satoshis, default=10000') parser.add_option( '-w', '--wait-time', action='store', type='float', dest='waittime', help='wait time in seconds to allow orders to arrive, default=5', default=5) parser.add_option( '-N', '--makercount', action='store', type='int', dest='makercount', help='how many makers to coinjoin with, default=2', default=2) parser.add_option( '-C', '--choose-cheapest', action='store_true', dest='choosecheapest', default=False, help='override weightened offers picking and choose cheapest') parser.add_option( '-P', '--pick-orders', action='store_true', dest='pickorders', default=False, help= 'manually pick which orders to take. doesn\'t work while sweeping.') parser.add_option( '--yes', action='store_true', dest='answeryes', default=False, help='answer yes to everything') # TODO implement parser.add_option('-n', '--no-network', # action='store_true', dest='nonetwork', default=False, help='dont query # the blockchain interface, instead user must supply value of UTXOs on ' # + ' command line in the format txid:output/value-in-satoshi') (options, args) = parser.parse_args() if len(args) < 3: parser.error('Needs a wallet, amount and destination address') sys.exit(0) auth_utxo = args[0] cjamount = int(args[1]) destaddr = args[2] changeaddr = args[3] cold_utxos = args[4:] load_program_config() addr_valid1, errormsg1 = validate_address(destaddr) errormsg2 = None # if amount = 0 dont bother checking changeaddr so user can write any junk if cjamount != 0: addr_valid2, errormsg2 = validate_address(changeaddr) else: addr_valid2 = True if not addr_valid1 or not addr_valid2: if not addr_valid1: print 'ERROR: Address invalid. ' + errormsg1 else: print 'ERROR: Address invalid. ' + errormsg2 return all_utxos = [auth_utxo] + cold_utxos query_result = jm_single().bc_interface.query_utxo_set(all_utxos) if None in query_result: print query_result utxo_data = {} for utxo, data in zip(all_utxos, query_result): utxo_data[utxo] = {'address': data['address'], 'value': data['value']} auth_privkey = raw_input('input private key for ' + utxo_data[auth_utxo][ 'address'] + ' :') if utxo_data[auth_utxo]['address'] != btc.privtoaddr( auth_privkey, get_p2pk_vbyte()): print 'ERROR: privkey does not match auth utxo' return if options.pickorders and cjamount != 0: # cant use for sweeping chooseOrdersFunc = pick_order elif options.choosecheapest: chooseOrdersFunc = cheapest_order_choose else: # choose randomly (weighted) chooseOrdersFunc = weighted_order_choose jm_single().nickname = random_nick() log.debug('starting sendpayment') class UnsignedTXWallet(AbstractWallet): def get_key_from_addr(self, addr): log.debug('getting privkey of ' + addr) if btc.privtoaddr(auth_privkey, get_p2pk_vbyte()) != addr: raise RuntimeError('privkey doesnt match given address') return auth_privkey wallet = UnsignedTXWallet() irc = IRCMessageChannel(jm_single().nickname) taker = CreateUnsignedTx(irc, wallet, auth_utxo, cjamount, destaddr, changeaddr, utxo_data, options, chooseOrdersFunc) try: log.debug('starting irc') irc.run() except: log.debug('CRASHING, DUMPING EVERYTHING') debug_dump_object(wallet, ['addr_cache', 'keys', 'wallet_name', 'seed']) debug_dump_object(taker) import traceback log.debug(traceback.format_exc())
def load_wallet(wallet_file, get_password_fn): """load and if necessary decrypt a bitcoinj wallet file :param wallet_file: an open bitcoinj wallet file :type wallet_file: file :param get_password_fn: a callback returning a password that's called iff one is required :type get_password_fn: function :return: the Wallet protobuf message or None if no password was entered when required :rtype: wallet_pb2.Wallet """ wallet_file.seek(0) magic_bytes = wallet_file.read(12) wallet_file.seek(0, os.SEEK_END) wallet_size = wallet_file.tell() wallet_file.seek(0) if magic_bytes[2:6] != b"org." and wallet_size % 16 == 0: import pylibscrypt takes_long = not pylibscrypt._done # if a binary library wasn't found, this'll take a while ciphertext = wallet_file.read() assert len(ciphertext) % 16 == 0 password = get_password_fn(takes_long) if not password: return None # Derive the encryption key salt = '\x35\x51\x03\x80\x75\xa3\xb0\xc5' key = pylibscrypt.scrypt(password.encode('utf_16_be'), salt, olen=32) # Decrypt the wallet ( v0.5.0+ ) try: plaintext = aes256_cbc_decrypt(ciphertext[16:], key, ciphertext[:16]) if plaintext[2:6] != b"org.": raise ValueError('incorrect password') except ValueError as e: if e.args[0] == 'incorrect password': # Decrypt the wallet ( < v0.5.0 ) iv = '\xa3\x44\x39\x1f\x53\x83\x11\xb3\x29\x54\x86\x16\xc4\x89\x72\x3e' plaintext = aes256_cbc_decrypt(ciphertext, key, iv) global multibit_hd_password multibit_hd_password = password # Else it's not whole-file encrypted else: password = None plaintext = wallet_file.read() # Parse the wallet protobuf pb_wallet = wallet_pb2.Wallet() try: pb_wallet.ParseFromString(plaintext) except Exception as e: msg = 'not a wallet file: ' + str(e) if password: msg = "incorrect password (or " + msg + ")" raise ValueError(msg) f = open('parsed_wallet.txt','w') f.write(pb_wallet.__str__()) f.close() foundAddr = [] for trans in pb_wallet.transaction: if trans.pool == 4: print("--------------------------------------------------------------------------------") print("TXID: " + binascii.hexlify(trans.hash)) for out in trans.transaction_output: print("") faddr = bitcoin.bin_to_b58check(bitcoin.deserialize_script(out.script_bytes)[2]) print("Addr: " + faddr) foundAddr.append(faddr) print("Amt: " + str(out.value * 0.00000001) + " BTC") print("") print("--------------------------------------------------------------------------------") seed = None sys.stdout.write('Finding Seed....') salt = pb_wallet.encryption_parameters.salt dkey = pylibscrypt.scrypt(password.encode('utf_16_be'), salt, olen=32) for wkey in pb_wallet.key: if wkey.type == 3: seed = aes256_cbc_decrypt(wkey.encrypted_deterministic_seed.encrypted_private_key, dkey, wkey.encrypted_deterministic_seed.initialisation_vector) break if not seed: print("No DETERMINISTIC_MNEMONIC seed found!") return None else: print("Done!") xprv = bitcoin.bip32_master_key(seed) xprvReceive = bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 2**31),0) #m/0'/0 xprvChange = bitcoin.bip32_ckd(bitcoin.bip32_ckd(xprv, 2**31),1) #m/0'/1 rcvAddr = [] chgAddr = [] rcvPrivKey = [] chgPrivKey = [] sys.stdout.write("Generating Addresses/Keys.") for x in range(0,1000): if x % 10 == 0: sys.stdout.write(".") childprivReceive = bitcoin.bip32_ckd(xprvReceive, x) childprivChange = bitcoin.bip32_ckd(xprvChange, x) pkeyReceive = bitcoin.bip32_extract_key(childprivReceive) pkeyChange = bitcoin.bip32_extract_key(childprivChange) #addressReceive = privtoaddr(pkeyReceive) #addressChange = privtoaddr(pkeyChange) rcvAddr.append(bitcoin.privtoaddr(pkeyReceive)) chgAddr.append(bitcoin.privtoaddr(pkeyChange)) rcvPrivKey.append(bitcoin.encode_privkey(pkeyReceive, 'wif_compressed')) chgPrivKey.append(bitcoin.encode_privkey(pkeyChange, 'wif_compressed')) print("Done!") print("--------------------------------------------------------------------------------") for addy in foundAddr: if addy in rcvAddr: print("") print("Found Address: " + addy) print("PrivateKey: " + rcvPrivKey[rcvAddr.index(addy)]) elif addy in chgAddr: print("") print("Found Change Address: " + addy) print("PrivateKey: " + chgPrivKey[chgAddr.index(addy)]) else: print("") print("Address not found: " + addy) print("") print("--------------------------------------------------------------------------------") return pb_wallet
def get_key_from_addr(self, addr): log.debug('getting privkey of ' + addr) if btc.privtoaddr(auth_privkey, get_p2pk_vbyte()) != addr: raise RuntimeError('privkey doesnt match given address') return auth_privkey