<commit_before>/*
* Copyright 2014 http://Bither.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.bither.bitherj.core;


import net.bither.bitherj.AbstractApp;
import net.bither.bitherj.BitherjSettings;
import net.bither.bitherj.db.AbstractDb;
import net.bither.bitherj.script.Script;
import net.bither.bitherj.utils.Sha256Hash;
import net.bither.bitherj.utils.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AddressManager implements HDMKeychain.HDMAddressChangeDelegate {

    private static final Logger log = LoggerFactory.getLogger(AddressManager.class);
    private final byte[] lock = new byte[0];
    private static AddressManager uniqueInstance = new AddressManager();

    protected List<Address> privKeyAddresses = new ArrayList<Address>();
    protected List<Address> watchOnlyAddresses = new ArrayList<Address>();
    protected List<Address> trashAddresses = new ArrayList<Address>();
    protected HashSet<String> addressHashSet = new HashSet<String>();
    protected HDMKeychain hdmKeychain;
    protected HDAccount hdAccount;

    private AddressManager() {
        synchronized (lock) {
            initAddress();
            initHDMKeychain();
            initHDAccount();
            initAliasAndVanityLen();
            AbstractApp.addressIsReady = true;
            AbstractApp.notificationService.sendBroadcastAddressLoadCompleteState();
        }
    }

    public static AddressManager getInstance() {
        return uniqueInstance;
    }

    private void initAliasAndVanityLen() {
        Map<String, String> addressAlias = AbstractDb.addressProvider.getAliases();
        Map<String, Integer> vanityAddresses = AbstractDb.addressProvider.getVanitylens();
        if (addressAlias.size() == 0 && vanityAddresses.size() == 0) {
            return;
        }
        for (Address address : privKeyAddresses) {
            String addressStr = address.getAddress();
            if (addressAlias.containsKey(addressStr)) {
                String alias = addressAlias.get(addressStr);
                address.setAlias(alias);
            }
            if (vanityAddresses.containsKey(addressStr)) {
                int vanityLen = vanityAddresses.get(addressStr);
                address.setVanityLen(vanityLen);
            }
        }
        for (Address address : watchOnlyAddresses) {
            String addressStr = address.getAddress();
            if (addressAlias.containsKey(addressStr)) {
                String alias = addressAlias.get(addressStr);
                address.setAlias(alias);
            }
            if (vanityAddresses.containsKey(addressStr)) {
                int vanityLen = vanityAddresses.get(addressStr);
                address.setVanityLen(vanityLen);
            }
        }
        if (hdmKeychain != null) {
            for (HDMAddress address : hdmKeychain.getAllCompletedAddresses()) {
                if (addressAlias.containsKey(address.getAddress())) {
                    String alias = addressAlias.get(address.getAddress());
                    address.setAlias(alias);
                }
            }
        }
    }

    private void initAddress() {
        List<Address> addressList = AbstractDb.addressProvider.getAddresses();
        for (Address address : addressList) {

            if (address.hasPrivKey()) {
                if (address.isTrashed()) {
                    this.trashAddresses.add(address);
                } else {
                    this.privKeyAddresses.add(address);
                    this.addressHashSet.add(address.getAddress());
                }
            } else {
                this.watchOnlyAddresses.add(address);
                this.addressHashSet.add(address.getAddress());
            }

        }
    }

    private void initHDAccount() {
        List<Integer> seeds = AbstractDb.addressProvider.getHDAccountSeeds();
        if (seeds.size() > 0) {
            hdAccount = new HDAccount(seeds.get(0));
        }
    }

    public boolean registerTx(Tx tx, Tx.TxNotificationType txNotificationType) {
        if (AbstractDb.txProvider.isTxDoubleSpendWithConfirmedTx(tx)) {
            // double spend with confirmed tx
            return false;
        }

        boolean isRegister = false;
        Tx compressedTx = compressTx(tx);
        HashSet<String> needNotifyAddressHashSet = new HashSet<String>();
        HashSet<String> needNotifyHDAccountHS = new HashSet<String>();
        List<HDAccount.HDAccountAddress> relatedAddresses = new ArrayList<HDAccount.HDAccountAddress>();
        HashSet<String> relatedAddressesHS = new HashSet<String>();

        if (hdAccount != null) {
            relatedAddresses = hdAccount.getRelatedAddressesForTx(compressedTx);
        }

        for (HDAccount.HDAccountAddress hdAccountAddress : relatedAddresses) {
            relatedAddressesHS.add(hdAccountAddress.getAddress());
        }


        for (Out out : compressedTx.getOuts()) {
            String outAddress = out.getOutAddress();
            if (addressHashSet.contains(outAddress)) {
                needNotifyAddressHashSet.add(outAddress);
            }

            if (relatedAddressesHS.contains(outAddress)) {
                needNotifyHDAccountHS.add(outAddress);
            }

        }

        Tx txInDb = AbstractDb.txProvider.getTxDetailByTxHash(tx.getTxHash());
        if (txInDb != null) {
            for (Out out : txInDb.getOuts()) {
                String outAddress = out.getOutAddress();
                if (needNotifyAddressHashSet.contains(outAddress)) {
                    needNotifyAddressHashSet.remove(outAddress);
                }

                if (needNotifyHDAccountHS.contains(outAddress)) {
                    needNotifyHDAccountHS.remove(outAddress);
                }

            }
            isRegister = true;
        } else {
            List<String> inAddresses = AbstractDb.txProvider.getInAddresses(compressedTx);
            for (String address : inAddresses) {
                if (addressHashSet.contains(address)) {<commit_msg>get in addresses<commit_after>/*
* Copyright 2014 http://Bither.net
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package net.bither.bitherj.core;


import net.bither.bitherj.AbstractApp;
import net.bither.bitherj.BitherjSettings;
import net.bither.bitherj.db.AbstractDb;
import net.bither.bitherj.script.Script;
import net.bither.bitherj.utils.Sha256Hash;
import net.bither.bitherj.utils.Utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class AddressManager implements HDMKeychain.HDMAddressChangeDelegate {

    private static final Logger log = LoggerFactory.getLogger(AddressManager.class);
    private final byte[] lock = new byte[0];
    private static AddressManager uniqueInstance = new AddressManager();

    protected List<Address> privKeyAddresses = new ArrayList<Address>();
    protected List<Address> watchOnlyAddresses = new ArrayList<Address>();
    protected List<Address> trashAddresses = new ArrayList<Address>();
    protected HashSet<String> addressHashSet = new HashSet<String>();
    protected HDMKeychain hdmKeychain;
    protected HDAccount hdAccount;

    private AddressManager() {
        synchronized (lock) {
            initAddress();
            initHDMKeychain();
            initHDAccount();
            initAliasAndVanityLen();
            AbstractApp.addressIsReady = true;
            AbstractApp.notificationService.sendBroadcastAddressLoadCompleteState();
        }
    }

    public static AddressManager getInstance() {
        return uniqueInstance;
    }

    private void initAliasAndVanityLen() {
        Map<String, String> addressAlias = AbstractDb.addressProvider.getAliases();
        Map<String, Integer> vanityAddresses = AbstractDb.addressProvider.getVanitylens();
        if (addressAlias.size() == 0 && vanityAddresses.size() == 0) {
            return;
        }
        for (Address address : privKeyAddresses) {
            String addressStr = address.getAddress();
            if (addressAlias.containsKey(addressStr)) {
                String alias = addressAlias.get(addressStr);
                address.setAlias(alias);
            }
            if (vanityAddresses.containsKey(addressStr)) {
                int vanityLen = vanityAddresses.get(addressStr);
                address.setVanityLen(vanityLen);
            }
        }
        for (Address address : watchOnlyAddresses) {
            String addressStr = address.getAddress();
            if (addressAlias.containsKey(addressStr)) {
                String alias = addressAlias.get(addressStr);
                address.setAlias(alias);
            }
            if (vanityAddresses.containsKey(addressStr)) {
                int vanityLen = vanityAddresses.get(addressStr);
                address.setVanityLen(vanityLen);
            }
        }
        if (hdmKeychain != null) {
            for (HDMAddress address : hdmKeychain.getAllCompletedAddresses()) {
                if (addressAlias.containsKey(address.getAddress())) {
                    String alias = addressAlias.get(address.getAddress());
                    address.setAlias(alias);
                }
            }
        }
    }

    private void initAddress() {
        List<Address> addressList = AbstractDb.addressProvider.getAddresses();
        for (Address address : addressList) {

            if (address.hasPrivKey()) {
                if (address.isTrashed()) {
                    this.trashAddresses.add(address);
                } else {
                    this.privKeyAddresses.add(address);
                    this.addressHashSet.add(address.getAddress());
                }
            } else {
                this.watchOnlyAddresses.add(address);
                this.addressHashSet.add(address.getAddress());
            }

        }
    }

    private void initHDAccount() {
        List<Integer> seeds = AbstractDb.addressProvider.getHDAccountSeeds();
        if (seeds.size() > 0) {
            hdAccount = new HDAccount(seeds.get(0));
        }
    }

    public boolean registerTx(Tx tx, Tx.TxNotificationType txNotificationType) {
        if (AbstractDb.txProvider.isTxDoubleSpendWithConfirmedTx(tx)) {
            // double spend with confirmed tx
            return false;
        }

        boolean isRegister = false;
        Tx compressedTx = compressTx(tx);
        HashSet<String> needNotifyAddressHashSet = new HashSet<String>();
        HashSet<String> needNotifyHDAccountHS = new HashSet<String>();
        List<HDAccount.HDAccountAddress> relatedAddresses = new ArrayList<HDAccount.HDAccountAddress>();
        HashSet<String> relatedAddressesHS = new HashSet<String>();

        if (hdAccount != null) {
            relatedAddresses = hdAccount.getRelatedAddressesForTx(compressedTx);
        }

        for (HDAccount.HDAccountAddress hdAccountAddress : relatedAddresses) {
            relatedAddressesHS.add(hdAccountAddress.getAddress());
        }


        for (Out out : compressedTx.getOuts()) {
            String outAddress = out.getOutAddress();
            if (addressHashSet.contains(outAddress)) {
                needNotifyAddressHashSet.add(outAddress);
            }

            if (relatedAddressesHS.contains(outAddress)) {
                needNotifyHDAccountHS.add(outAddress);
            }

        }

        Tx txInDb = AbstractDb.txProvider.getTxDetailByTxHash(tx.getTxHash());
        if (txInDb != null) {
            for (Out out : txInDb.getOuts()) {
                String outAddress = out.getOutAddress();
                if (needNotifyAddressHashSet.contains(outAddress)) {
                    needNotifyAddressHashSet.remove(outAddress);
                }

                if (needNotifyHDAccountHS.contains(outAddress)) {
                    needNotifyHDAccountHS.remove(outAddress);
                }

            }
            isRegister = true;
        } else {
            List<String> inAddresses = compressedTx.getInAddresses();
            for (String address : inAddresses) {
                if (addressHashSet.contains(address)) {
{'fraction_non_alphanumeric': 0.09380122187453435, 'fraction_numerical': 0.002682163611980331, 'mean_word_length': 2.367536377320622, 'pattern_counts': {'":': 0, '<': 45, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2142856', 'n_tokens_mistral': 3655, 'n_tokens_neox': 3501, 'n_words': 746}