net.bither.bitherj.core.In Java Examples

The following examples show how to use net.bither.bitherj.core.In. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public List<String> getInAddresses(Tx tx) {
    final List<String> result = new ArrayList<String>();
    String sql = "select out_address from outs where tx_hash=? and out_sn=?";
    IDb db = this.getReadDb();
    for (In inItem : tx.getIns()) {
        this.execQueryOneRecord(db, sql, new String[]{Base58.encode(inItem.getPrevTxHash())
                , Integer.toString(inItem.getPrevOutSn())}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                if (!c.isNull(0)) {
                    result.add(c.getString(0));
                }
                return null;
            }
        });
    }
    return result;
}
 
Example #2
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public void completeInSignature(final List<In> ins) {
    try {
        this.mDb.getConn().setAutoCommit(false);
        String sql = "update ins set in_signature=? where tx_hash=? and in_sn=? and ifnull(in_signature,'')=''";
        for (In in : ins) {
            PreparedStatement preparedStatement = this.mDb.getConn().prepareStatement(sql);
            preparedStatement.setString(1, Base58.encode(in.getInSignature()));
            preparedStatement.setString(2, Base58.encode(in.getTxHash()));
            preparedStatement.setInt(3, in.getInSn());
            preparedStatement.executeUpdate();
            preparedStatement.close();
        }
        this.mDb.getConn().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #3
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public List<String> getInAddresses(Tx tx) {
    List<String> result = new ArrayList<String>();
    String sql = "select out_address from outs where tx_hash=? and out_sn=?";
    ResultSet c;
    try {
        for (In inItem : tx.getIns()) {
            PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(inItem.getPrevTxHash()), Integer.toString(inItem.getPrevOutSn())});
            c = statement.executeQuery();
            if (c.next()) {
                int column = c.findColumn("out_address");
                if (column != -1) {
                    result.add(c.getString(column));
                }
            }
            c.close();
            statement.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example #4
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public boolean isTxDoubleSpendWithConfirmedTx(Tx tx) {
    String sql = "select count(0) cnt from ins a, txs b where a.tx_hash=b.tx_hash and" +
            " b.block_no is not null and a.prev_tx_hash=? and a.prev_out_sn=?";
    ResultSet rs;
    try {
        for (In inItem : tx.getIns()) {
            PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(inItem.getPrevTxHash()), Integer.toString(inItem.getPrevOutSn())});
            rs = statement.executeQuery();
            if (rs.next()) {
                int columnIndex = rs.findColumn("cnt");
                if (columnIndex != -1 && rs.getInt(columnIndex) > 0) {
                    rs.close();
                    statement.close();
                    return true;
                }
            }
            rs.close();
            statement.close();

        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #5
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static void addInsAndOuts(TxDBHelper mDb, Tx txItem) throws AddressFormatException, SQLException {
    String txHashStr = Base58.encode(txItem.getTxHash());
    txItem.setOuts(new ArrayList<Out>());
    txItem.setIns(new ArrayList<In>());
    String sql = "select * from ins where tx_hash=? order by in_sn";
    PreparedStatement statement = mDb.getPreparedStatement(sql, new String[]{txHashStr});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        In inItem = TxHelper.applyCursorIn(c);
        inItem.setTx(txItem);
        txItem.getIns().add(inItem);
    }
    c.close();
    statement.close();

    sql = "select * from outs where tx_hash=? order by out_sn";
    statement = mDb.getPreparedStatement(sql, new String[]{txHashStr});
    c = statement.executeQuery();
    while (c.next()) {
        Out outItem = TxHelper.applyCursorOut(c);
        outItem.setTx(txItem);
        txItem.getOuts().add(outItem);
    }
    c.close();
    statement.close();
}
 
Example #6
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public boolean isTxDoubleSpendWithConfirmedTx(Tx tx) {
    String sql = "select count(0) from ins a, txs b where a.tx_hash=b.tx_hash and" +
            " b.block_no is not null and a.prev_tx_hash=? and a.prev_out_sn=?";
    IDb db = this.getReadDb();
    for (In inItem : tx.getIns()) {
        final int[] cnt = {0};
        this.execQueryOneRecord(db, sql, new String[]{Base58.encode(inItem.getPrevTxHash()), Integer.toString(inItem.getPrevOutSn())}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                cnt[0] = c.getInt(0);
                return null;
            }
        });
        if (cnt[0] > 0) {
            return true;
        }
    }
    return false;
}
 
Example #7
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
private void addInForTxDetail(IDb db, String address, final HashMap<Sha256Hash, Tx> txDict) {
    String sql = "select b.* from addresses_txs a, ins b where a.tx_hash=b.tx_hash and a.address=? "
            + "order by b.tx_hash ,b.in_sn";
    this.execQueryLoop(db, sql, new String[]{address}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            In inItem = applyCursorIn(c);
            Tx tx = txDict.get(new Sha256Hash(inItem.getTxHash()));
            if (tx != null) {
                tx.getIns().add(inItem);
            }
            return null;
        }
    });
}
 
Example #8
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public List<Tx> getTxAndDetailByAddress(String address) {
    final List<Tx> txItemList = new ArrayList<Tx>();
    final HashMap<Sha256Hash, Tx> txDict = new HashMap<Sha256Hash, Tx>();
    String sql = "select b.* from addresses_txs a, txs b" +
            " where a.tx_hash=b.tx_hash and a.address=? order by ifnull(b.block_no,4294967295) desc";
    IDb db = this.getReadDb();
    this.execQueryLoop(db, sql, new String[]{address}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            Tx txItem = applyCursor(c);
            txItem.setIns(new ArrayList<In>());
            txItem.setOuts(new ArrayList<Out>());
            txItemList.add(txItem);
            txDict.put(new Sha256Hash(txItem.getTxHash()), txItem);
            return null;
        }
    });
    addInForTxDetail(db, address, txDict);
    addOutForTxDetail(db, address, txDict);

    return txItemList;
}
 
Example #9
Source File: AbstractHDAccountAddressProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public List<HDAccount.HDAccountAddress> getSigningAddressesForInputs(int hdAccountId, List<In> inList) {
    final List<HDAccount.HDAccountAddress> hdAccountAddressList =
            new ArrayList<HDAccount.HDAccountAddress>();
    for (In in : inList) {
        String sql = "select a.address,a.path_type,a.address_index,a.is_synced,a.hd_account_id" +
                " from hd_account_addresses a ,outs b" +
                " where a.address=b.out_address" +
                " and b.tx_hash=? and b.out_sn=? and a.hd_account_id=?";
        OutPoint outPoint = in.getOutpoint();
        this.execQueryOneRecord(sql, new String[]{Base58.encode(in.getPrevTxHash()), Integer.toString
                (outPoint.getOutSn()), Integer.toString(hdAccountId)}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                hdAccountAddressList.add(formatAddress(c));
                return null;
            }
        });
    }
    return hdAccountAddressList;
}
 
Example #10
Source File: TxProvider.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void insertInToDb(IDb db, In in) {
    AndroidDb mdb = (AndroidDb)db;
    ContentValues cv = new ContentValues();
    cv.put(AbstractDb.InsColumns.TX_HASH, Base58.encode(in.getTxHash()));
    cv.put(AbstractDb.InsColumns.IN_SN, in.getInSn());
    cv.put(AbstractDb.InsColumns.PREV_TX_HASH, Base58.encode(in.getPrevTxHash()));
    cv.put(AbstractDb.InsColumns.PREV_OUT_SN, in.getPrevOutSn());
    if (in.getInSignature() != null) {
        cv.put(AbstractDb.InsColumns.IN_SIGNATURE, Base58.encode(in.getInSignature()));
    } else {
        cv.putNull(AbstractDb.InsColumns.IN_SIGNATURE);
    }
    cv.put(AbstractDb.InsColumns.IN_SEQUENCE, in.getInSequence());
    mdb.getSQLiteDatabase().insert(AbstractDb.Tables.INS, null, cv);
}
 
Example #11
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public void completeInSignature(final List<In> ins) {
    try {
        this.mDb.getConn().setAutoCommit(false);
        String sql = "update ins set in_signature=? where tx_hash=? and in_sn=? and ifnull(in_signature,'')=''";
        for (In in : ins) {
            PreparedStatement preparedStatement = this.mDb.getConn().prepareStatement(sql);
            preparedStatement.setString(1, Base58.encode(in.getInSignature()));
            preparedStatement.setString(2, Base58.encode(in.getTxHash()));
            preparedStatement.setInt(3, in.getInSn());
            preparedStatement.executeUpdate();
            preparedStatement.close();
        }
        this.mDb.getConn().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #12
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public List<String> getInAddresses(Tx tx) {
    List<String> result = new ArrayList<String>();
    String sql = "select out_address from outs where tx_hash=? and out_sn=?";
    ResultSet c;
    try {
        for (In inItem : tx.getIns()) {
            PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(inItem.getPrevTxHash()), Integer.toString(inItem.getPrevOutSn())});
            c = statement.executeQuery();
            if (c.next()) {
                int column = c.findColumn("out_address");
                if (column != -1) {
                    result.add(c.getString(column));
                }
            }
            c.close();
            statement.close();
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return result;
}
 
Example #13
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public boolean isTxDoubleSpendWithConfirmedTx(Tx tx) {
    String sql = "select count(0) cnt from ins a, txs b where a.tx_hash=b.tx_hash and" +
            " b.block_no is not null and a.prev_tx_hash=? and a.prev_out_sn=?";
    ResultSet rs;
    try {
        for (In inItem : tx.getIns()) {
            PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(inItem.getPrevTxHash()), Integer.toString(inItem.getPrevOutSn())});
            rs = statement.executeQuery();
            if (rs.next()) {
                int columnIndex = rs.findColumn("cnt");
                if (columnIndex != -1 && rs.getInt(columnIndex) > 0) {
                    rs.close();
                    statement.close();
                    return true;
                }
            }
            rs.close();
            statement.close();

        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return false;
}
 
Example #14
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public static void addInsAndOuts(TxDBHelper mDb, Tx txItem) throws AddressFormatException, SQLException {
    String txHashStr = Base58.encode(txItem.getTxHash());
    txItem.setOuts(new ArrayList<Out>());
    txItem.setIns(new ArrayList<In>());
    String sql = "select * from ins where tx_hash=? order by in_sn";
    PreparedStatement statement = mDb.getPreparedStatement(sql, new String[]{txHashStr});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        In inItem = TxHelper.applyCursorIn(c);
        inItem.setTx(txItem);
        txItem.getIns().add(inItem);
    }
    c.close();
    statement.close();

    sql = "select * from outs where tx_hash=? order by out_sn";
    statement = mDb.getPreparedStatement(sql, new String[]{txHashStr});
    c = statement.executeQuery();
    while (c.next()) {
        Out outItem = TxHelper.applyCursorOut(c);
        outItem.setTx(txItem);
        txItem.getOuts().add(outItem);
    }
    c.close();
    statement.close();
}
 
Example #15
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public static In applyCursorIn(ResultSet c) throws AddressFormatException, SQLException {
    In inItem = new In();
    int idColumn = c.findColumn(AbstractDb.InsColumns.TX_HASH);
    if (idColumn != -1) {
        inItem.setTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SN);
    if (idColumn != -1) {
        inItem.setInSn(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.PREV_TX_HASH);
    if (idColumn != -1) {
        inItem.setPrevTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.PREV_OUT_SN);
    if (idColumn != -1) {
        inItem.setPrevOutSn(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SIGNATURE);
    if (idColumn != -1) {
        String inSignature = c.getString(idColumn);
        if (!Utils.isEmpty(inSignature)) {
            inItem.setInSignature(Base58.decode(c.getString(idColumn)));
        }
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SEQUENCE);
    if (idColumn != -1) {
        inItem.setInSequence(c.getInt(idColumn));
    }
    return inItem;
}
 
Example #16
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public void completeInSignature(List<In> ins) {
    IDb db = this.getWriteDb();
    db.beginTransaction();
    String sql = "update ins set in_signature=? where tx_hash=? and in_sn=? and ifnull(in_signature,'')=''";
    for (In in : ins) {
        this.execUpdate(db, sql, new String[]{Base58.encode(in.getInSignature())
                , Base58.encode(in.getTxHash()), Integer.toString(in.getInSn())});
    }
    db.endTransaction();
}
 
Example #17
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public byte[] isIdentify(Tx tx) {
    HashSet<String> result = new HashSet<String>();

    for (In in : tx.getIns()) {
        String queryPrevTxHashSql = "select tx_hash from ins where prev_tx_hash=? and prev_out_sn=?";
        final HashSet<String> each = new HashSet<String>();
        this.execQueryOneRecord(this.getReadDb(), queryPrevTxHashSql, new String[]{Base58.encode(in.getPrevTxHash())
                , Integer.toString(in.getPrevOutSn())}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                each.add(c.getString(0));
                return null;
            }
        });
        each.remove(Base58.encode(tx.getTxHash()));
        result.retainAll(each);
        if (result.size() == 0) {
            break;
        }
    }
    if (result.size() == 0) {
        return new byte[0];
    } else {
        try {
            return Base58.decode((String) result.toArray()[0]);
        } catch (AddressFormatException e) {
            e.printStackTrace();
            return new byte[0];
        }
    }
}
 
Example #18
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public static In applyCursorIn(ResultSet c) throws AddressFormatException, SQLException {
    In inItem = new In();
    int idColumn = c.findColumn(AbstractDb.InsColumns.TX_HASH);
    if (idColumn != -1) {
        inItem.setTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SN);
    if (idColumn != -1) {
        inItem.setInSn(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.PREV_TX_HASH);
    if (idColumn != -1) {
        inItem.setPrevTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.PREV_OUT_SN);
    if (idColumn != -1) {
        inItem.setPrevOutSn(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SIGNATURE);
    if (idColumn != -1) {
        String inSignature = c.getString(idColumn);
        if (!Utils.isEmpty(inSignature)) {
            inItem.setInSignature(Base58.decode(c.getString(idColumn)));
        }
    }
    idColumn = c.findColumn(AbstractDb.InsColumns.IN_SEQUENCE);
    if (idColumn != -1) {
        inItem.setInSequence(c.getInt(idColumn));
    }
    return inItem;
}
 
Example #19
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void addInForTxDetail(String address, HashMap<Sha256Hash, Tx> txDict) throws AddressFormatException, SQLException {
    String sql = "select b.* from addresses_txs a, ins b where a.tx_hash=b.tx_hash and a.address=? "
            + "order by b.tx_hash ,b.in_sn";
    PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{address});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        In inItem = TxHelper.applyCursorIn(c);
        Tx tx = txDict.get(new Sha256Hash(inItem.getTxHash()));
        if (tx != null) {
            tx.getIns().add(inItem);
        }
    }
    c.close();
    statement.close();
}
 
Example #20
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void addInForTxDetail(String address, HashMap<Sha256Hash, Tx> txDict) throws AddressFormatException, SQLException {
    String sql = "select b.* from addresses_txs a, ins b where a.tx_hash=b.tx_hash and a.address=? "
            + "order by b.tx_hash ,b.in_sn";
    PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{address});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        In inItem = TxHelper.applyCursorIn(c);
        Tx tx = txDict.get(new Sha256Hash(inItem.getTxHash()));
        if (tx != null) {
            tx.getIns().add(inItem);
        }
    }
    c.close();
    statement.close();
}
 
Example #21
Source File: IDesktopTxProvider.java    From bitherj with Apache License 2.0 votes vote down vote up
List<DesktopHDMAddress> getSigningAddressesForInputs(DesktopHDMKeychain keychain, List<In> inList); 
Example #22
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 votes vote down vote up
protected abstract void insertInToDb(IDb db, In in); 
Example #23
Source File: IHDAccountAddressProvider.java    From bitherj with Apache License 2.0 votes vote down vote up
List<HDAccount.HDAccountAddress> getSigningAddressesForInputs(int hdAccountId, List<In> inList); 
Example #24
Source File: ITxProvider.java    From bitherj with Apache License 2.0 votes vote down vote up
void completeInSignature(List<In> ins);