net.bither.bitherj.utils.Base58 Java Examples

The following examples show how to use net.bither.bitherj.utils.Base58. 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: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public List<Long> txInValues(byte[] txHash) {
    List<Long> inValues = new ArrayList<Long>();
    try {
        String sql = "select b.out_value " +
                "from ins a left outer join outs b on a.prev_tx_hash=b.tx_hash and a.prev_out_sn=b.out_sn " +
                "where a.tx_hash=?";
        PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(txHash)});
        ResultSet c = statement.executeQuery();
        while (c.next()) {
            int idColumn = c.findColumn("out_value");
            if (idColumn != -1) {
                inValues.add(c.getLong(idColumn));
            } else {
                inValues.add(null);
            }
        }
        c.close();
        statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return inValues;
}
 
Example #2
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 #3
Source File: BlockProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public boolean blockExists(byte[] blockHash) {
    String sql = "select count(0) cnt from blocks where block_hash=?";

    int cnt = 0;
    try {
        PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(blockHash)});
        ResultSet c = statement.executeQuery();
        if (c.next()) {
            int idColumn = c.findColumn("cnt");
            if (idColumn != -1) {
                cnt = c.getInt(idColumn);
            }
        }
        c.close();
        statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return cnt > 0;
}
 
Example #4
Source File: AddressProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
@Override
public void addAddress(final Address address) {
    try {

        this.mDb.getConn().setAutoCommit(false);
        String[] params = new String[]{address.getAddress(), address.hasPrivKey() ? address.getEncryptPrivKeyOfDb() : null, Base58.encode(address.getPubKey()),
                Integer.toString(address.isFromXRandom() ? 1 : 0), Integer.toString(address.isSyncComplete() ? 1 : 0), Integer.toString(address.isTrashed() ? 1 : 0), Long.toString(address.getSortTime())};
        PreparedStatement stmt = this.mDb.getConn().prepareStatement(insertAddressSql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                stmt.setString(i + 1, params[i]);
            }
        }
        stmt.executeUpdate();
        if (address.hasPrivKey()) {
            if (!hasPasswordSeed(this.mDb.getConn())) {
                PasswordSeed passwordSeed = new PasswordSeed(address.getAddress(), address.getFullEncryptPrivKeyOfDb());
                addPasswordSeed(this.mDb.getConn(), passwordSeed);
            }
        }
        this.mDb.getConn().commit();
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example #5
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public void insertTx(IDb db, Tx txItem) {
    final int[] cnt = {0};
    String existSql = "select count(0) cnt from txs where tx_hash=?";
    this.execQueryOneRecord(db, existSql, new String[]{Base58.encode(txItem.getTxHash())}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            int idColumn = c.getColumnIndex("cnt");
            if (idColumn != -1) {
                cnt[0] = c.getInt(idColumn);
            }
            return null;
        }
    });

    if (cnt[0] == 0) {
        this.insertTxToDb(db, txItem);
    }

}
 
Example #6
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 #7
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
@Override
public long sentFromAddress(byte[] txHash, String address) {
    String sql = "select  sum(o.out_value) out_value from ins i,outs o where" +
            " i.tx_hash=? and o.tx_hash=i.prev_tx_hash and i.prev_out_sn=o.out_sn and o.out_address=?";
    long sum = 0;
    try {
        PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(txHash),
                address});
        ResultSet cursor = statement.executeQuery();
        if (cursor.next()) {
            int idColumn = cursor.findColumn(AbstractDb.OutsColumns.OUT_VALUE);
            if (idColumn != -1) {
                sum = cursor.getLong(idColumn);
            }
        }
        cursor.close();
        statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return sum;
}
 
Example #8
Source File: DesktopHDMKeychain.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public ArrayList<byte[]> signWithCold(List<byte[]> unsignedHashes,
                                      CharSequence password,
                                      List<PathTypeIndex> pathTypeIndexList) {


    ArrayList<byte[]> sigs = new ArrayList<byte[]>();
    for (int i = 0;
         i < unsignedHashes.size();
         i++) {
        PathTypeIndex pathTypeIndex = pathTypeIndexList.get(i);
        DeterministicKey key;
        if (pathTypeIndex.pathType == PathType.EXTERNAL_ROOT_PATH) {
            key = getExternalKey(pathTypeIndex.index, password);
            System.out.println("pub:" + Base58.encode(key.getPubKey()));
        } else {
            key = getInternalKey(pathTypeIndex.index, password);
        }
        ECKey.ECDSASignature signed = key.sign(unsignedHashes.get(i));
        sigs.add(signed.encodeToDER());
        key.wipe();
    }

    return sigs;
}
 
Example #9
Source File: HDAccountProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
@Override
public long sentFromAccount(int hdAccountId, byte[] txHash) {
    String sql = "select  sum(o.out_value) out_value from ins i,outs o where" +
            " i.tx_hash=? and o.tx_hash=i.prev_tx_hash and i.prev_out_sn=o.out_sn and o.hd_account_id=?";
    long sum = 0;

    ResultSet cursor;

    try {
        PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(txHash),
                Integer.toString(hdAccountId)});
        cursor = statement.executeQuery();
        if (cursor.next()) {
            int idColumn = cursor.findColumn(AbstractDb.OutsColumns.OUT_VALUE);
            if (idColumn != -1) {
                sum = cursor.getLong(idColumn);
            }
        }
        cursor.close();
        statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return sum;
}
 
Example #10
Source File: AddressProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
@Override
public void addAddress(final Address address) {
    try {

        this.mDb.getConn().setAutoCommit(false);
        String[] params = new String[]{address.getAddress(), address.hasPrivKey() ? address.getEncryptPrivKeyOfDb() : null, Base58.encode(address.getPubKey()),
                Integer.toString(address.isFromXRandom() ? 1 : 0), Integer.toString(address.isSyncComplete() ? 1 : 0), Integer.toString(address.isTrashed() ? 1 : 0), Long.toString(address.getSortTime())};
        PreparedStatement stmt = this.mDb.getConn().prepareStatement(insertAddressSql);
        if (params != null) {
            for (int i = 0; i < params.length; i++) {
                stmt.setString(i + 1, params[i]);
            }
        }
        stmt.executeUpdate();
        if (address.hasPrivKey()) {
            if (!hasPasswordSeed(this.mDb.getConn())) {
                PasswordSeed passwordSeed = new PasswordSeed(address.getAddress(), address.getFullEncryptPrivKeyOfDb());
                addPasswordSeed(this.mDb.getConn(), passwordSeed);
            }
        }
        this.mDb.getConn().commit();
        stmt.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }
}
 
Example #11
Source File: HDAccountProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private void addAddress(Connection conn, HDAccount.HDAccountAddress accountAddress) throws SQLException {
    String sql = "insert into hd_account_addresses(path_type,address_index" +
            ",is_issued,address,pub,is_synced) " +
            " values(?,?,?,?,?,?)";

    String[] params = new String[]{Integer.toString(accountAddress.getPathType().getValue())
            , Integer.toString(accountAddress.getIndex())
            , Integer.toString(accountAddress.isIssued() ? 1 : 0)
            , accountAddress.getAddress()
            , Base58.encode(accountAddress.getPub())
            , Integer.toString(accountAddress.isSyncedComplete() ? 1 : 0)
    };
    PreparedStatement stmt = conn.prepareStatement(sql);
    if (params != null) {
        for (int i = 0; i < params.length; i++) {
            stmt.setString(i + 1, params[i]);
        }
    }
    stmt.executeUpdate();
    stmt.close();
}
 
Example #12
Source File: HDAccountProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
private void addAddress(Connection conn, HDAccount.HDAccountAddress accountAddress) throws SQLException {
    String sql = "insert into hd_account_addresses(path_type,address_index" +
            ",is_issued,address,pub,is_synced) " +
            " values(?,?,?,?,?,?)";

    String[] params = new String[]{Integer.toString(accountAddress.getPathType().getValue())
            , Integer.toString(accountAddress.getIndex())
            , Integer.toString(accountAddress.isIssued() ? 1 : 0)
            , accountAddress.getAddress()
            , Base58.encode(accountAddress.getPub())
            , Integer.toString(accountAddress.isSyncedComplete() ? 1 : 0)
    };
    PreparedStatement stmt = conn.prepareStatement(sql);
    if (params != null) {
        for (int i = 0; i < params.length; i++) {
            stmt.setString(i + 1, params[i]);
        }
    }
    stmt.executeUpdate();
    stmt.close();
}
 
Example #13
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public long sentFromAddress(byte[] txHash, String address) {
    String sql = "select  sum(o.out_value) out_value from ins i,outs o where" +
            " i.tx_hash=? and o.tx_hash=i.prev_tx_hash and i.prev_out_sn=o.out_sn and o.out_address=?";
    final long[] sum = {0};
    this.execQueryOneRecord(sql, new String[]{Base58.encode(txHash), address}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            int idColumn = c.getColumnIndex(AbstractDb.OutsColumns.OUT_VALUE);
            if (idColumn != -1) {
                sum[0] = c.getLong(idColumn);
            }
            return null;
        }
    });
    return sum[0];
}
 
Example #14
Source File: AbstractTxProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public Tx getTxDetailByTxHash(byte[] txHash) {
    final Tx[] txItem = {null};
    final boolean[] txExists = {false};
    String txHashStr = Base58.encode(txHash);
    String sql = "select * from txs where tx_hash=?";
    IDb db = this.getReadDb();
    this.execQueryOneRecord(db, sql, new String[]{txHashStr}, new Function<ICursor, Void>() {
        @Nullable

        @Override
        public Void apply(@Nullable ICursor c) {
            txItem[0] = applyCursor(c);
            txExists[0] = true;
            return null;
        }
    });
    if (txExists[0]) {
        addInsAndOuts(db, txItem[0]);
    }
    return txItem[0];
}
 
Example #15
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public void remove(byte[] txHash) {
    String txHashStr = Base58.encode(txHash);
    List<String> txHashes = new ArrayList<String>();
    final List<String> needRemoveTxHashes = new ArrayList<String>();
    txHashes.add(txHashStr);
    while (txHashes.size() > 0) {
        String thisHash = txHashes.get(0);
        txHashes.remove(0);
        needRemoveTxHashes.add(thisHash);
        List<String> temp = getRelayTx(thisHash);
        txHashes.addAll(temp);
    }
    try {
        this.mDb.getConn().setAutoCommit(false);
        for (String str : needRemoveTxHashes) {
            removeSingleTx(this.mDb.getConn(), str);
        }
        this.mDb.getConn().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #16
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
public void remove(byte[] txHash) {
    String txHashStr = Base58.encode(txHash);
    List<String> txHashes = new ArrayList<String>();
    final List<String> needRemoveTxHashes = new ArrayList<String>();
    txHashes.add(txHashStr);
    while (txHashes.size() > 0) {
        String thisHash = txHashes.get(0);
        txHashes.remove(0);
        needRemoveTxHashes.add(thisHash);
        List<String> temp = getRelayTx(thisHash);
        txHashes.addAll(temp);
    }
    try {
        this.mDb.getConn().setAutoCommit(false);
        for (String str : needRemoveTxHashes) {
            removeSingleTx(this.mDb.getConn(), str);
        }
        this.mDb.getConn().commit();
    } catch (SQLException e) {
        e.printStackTrace();
    }

}
 
Example #17
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 #18
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 #19
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 #20
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 6 votes vote down vote up
@Override
public long sentFromAddress(byte[] txHash, String address) {
    String sql = "select  sum(o.out_value) out_value from ins i,outs o where" +
            " i.tx_hash=? and o.tx_hash=i.prev_tx_hash and i.prev_out_sn=o.out_sn and o.out_address=?";
    long sum = 0;
    try {
        PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{Base58.encode(txHash),
                address});
        ResultSet cursor = statement.executeQuery();
        if (cursor.next()) {
            int idColumn = cursor.findColumn(AbstractDb.OutsColumns.OUT_VALUE);
            if (idColumn != -1) {
                sum = cursor.getLong(idColumn);
            }
        }
        cursor.close();
        statement.close();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    return sum;
}
 
Example #21
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 #22
Source File: TxProvider.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void insertTxToDb(IDb db, Tx tx) {
    AndroidDb mdb = (AndroidDb)db;
    ContentValues cv = new ContentValues();
    if (tx.getBlockNo() != Tx.TX_UNCONFIRMED) {
        cv.put(AbstractDb.TxsColumns.BLOCK_NO, tx.getBlockNo());
    } else {
        cv.putNull(AbstractDb.TxsColumns.BLOCK_NO);
    }
    cv.put(AbstractDb.TxsColumns.TX_HASH, Base58.encode(tx.getTxHash()));
    cv.put(AbstractDb.TxsColumns.SOURCE, tx.getSource());
    cv.put(AbstractDb.TxsColumns.TX_TIME, tx.getTxTime());
    cv.put(AbstractDb.TxsColumns.TX_VER, tx.getTxVer());
    cv.put(AbstractDb.TxsColumns.TX_LOCKTIME, tx.getTxLockTime());
    mdb.getSQLiteDatabase().insert(AbstractDb.Tables.TXS, null, cv);
}
 
Example #23
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 #24
Source File: TxProvider.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
protected void insertOutToDb(IDb db, Out out) {
    AndroidDb mdb = (AndroidDb)db;
    ContentValues cv = new ContentValues();
    cv.put(AbstractDb.OutsColumns.TX_HASH, Base58.encode(out.getTxHash()));
    cv.put(AbstractDb.OutsColumns.OUT_SN, out.getOutSn());
    cv.put(AbstractDb.OutsColumns.OUT_SCRIPT, Base58.encode(out.getOutScript()));
    cv.put(AbstractDb.OutsColumns.OUT_VALUE, out.getOutValue());
    cv.put(AbstractDb.OutsColumns.OUT_STATUS, out.getOutStatus().getValue());
    if (!Utils.isEmpty(out.getOutAddress())) {
        cv.put(AbstractDb.OutsColumns.OUT_ADDRESS, out.getOutAddress());
    } else {
        cv.putNull(AbstractDb.OutsColumns.OUT_ADDRESS);
    }
    //support hd
    if (out.getHDAccountId() != -1) {
        cv.put(AbstractDb.OutsColumns.HD_ACCOUNT_ID,
                out.getHDAccountId());
    } else {
        cv.putNull(AbstractDb.OutsColumns.HD_ACCOUNT_ID);
    }
    mdb.getSQLiteDatabase().insert(AbstractDb.Tables.OUTS, null, cv);
}
 
Example #25
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 #26
Source File: AbstractHDAccountAddressProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public long sentFromAccount(int hdAccountId, byte[] txHash) {
    String sql = "select  sum(o.out_value) out_value from ins i,outs o where" +
            " i.tx_hash=? and o.tx_hash=i.prev_tx_hash and i.prev_out_sn=o.out_sn and o" +
            ".hd_account_id=?";
    final long[] sum = {0};
    this.execQueryOneRecord(sql, new String[]{Base58.encode(txHash), Integer.toString(hdAccountId)}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            int idColumn = c.getColumnIndex(AbstractDb.OutsColumns.OUT_VALUE);
            if (idColumn != -1) {
                sum[0] = c.getLong(idColumn);
            }
            return null;
        }
    });
    return sum[0];
}
 
Example #27
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 #28
Source File: AbstractAddressProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public void setHDMPubsRemote(int hdSeedId, int index, byte[] remote) {
    String sql = "select count(0) from hdm_addresses " +
            "where hd_seed_id=? and hd_seed_index=? and pub_key_remote is null";
    final boolean[] isExist = {true};
    this.execQueryOneRecord(sql, new String[]{Integer.toString(hdSeedId), Integer.toString(index)}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            isExist[0] = c.getInt(0) > 0;
            return null;
        }
    });
    if (isExist[0]) {
        sql = "update hdm_addresses set pub_key_remote=? where hd_seed_id=? and hd_seed_index=?";
        this.execUpdate(sql, new String[]{Base58.encode(remote), Integer.toString(hdSeedId), Integer.toString(index)});
    }
}
 
Example #29
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 #30
Source File: DumpedPrivateKey.java    From bitherj with Apache License 2.0 6 votes vote down vote up
/**
 * Parses the given private key as created by the "dumpprivkey" Bitcoin C++ RPC.
 *
 * @param encoded The base58 encoded string.
 * @throws net.bither.bitherj.exception.AddressFormatException If the string is invalid or the header byte doesn't match the network params.
 */
public DumpedPrivateKey(String encoded) throws AddressFormatException {
    //todo string encoded
    byte[] tmp = Base58.decodeChecked(encoded);
    version = tmp[0] & 0xFF;
    bytes = new byte[tmp.length - 1];
    System.arraycopy(tmp, 1, bytes, 0, tmp.length - 1);

    if (version != BitherjSettings.dumpedPrivateKeyHeader)
        throw new AddressFormatException("Mismatched version number, trying to cross networks? " + version +
                " vs " + BitherjSettings.dumpedPrivateKeyHeader);
    if (bytes.length == 33 && bytes[32] == 1) {
        compressed = true;
        bytes = Arrays.copyOf(bytes, 32);  // Chop off the additional marker byte.
    } else if (bytes.length == 32) {
        compressed = false;
    } else {
        throw new AddressFormatException("Wrong number of bytes for a private key, not 32 or 33");
    }
}