net.bither.bitherj.exception.AddressFormatException Java Examples

The following examples show how to use net.bither.bitherj.exception.AddressFormatException. 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: DialogImportBip38KeyText.java    From bither-android with Apache License 2.0 6 votes vote down vote up
@Override
public void onClick(View v) {
    if (v.getId() == R.id.btn_ok) {
        String s = et.getText().toString();
        if (Utils.isEmpty(s)) {
            tvError.setVisibility(View.VISIBLE);
            shake();
            return;
        }
        try {
            if (!Bip38.isBip38PrivateKey(s)) {
                tvError.setVisibility(View.VISIBLE);
                shake();
                return;
            }
            bip38KeyString = et.getText().toString();
            dismiss();

        } catch (AddressFormatException e) {
            tvError.setVisibility(View.VISIBLE);
            e.printStackTrace();
        }
    } else {
        dismiss();
    }
}
 
Example #2
Source File: PrivateKeyUtil.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static String getBIP38PrivateKeyString(Address address, CharSequence password) throws
        AddressFormatException, InterruptedException {
    SecureCharSequence decrypted = getDecryptPrivateKeyString(address.getFullEncryptPrivKey()
            , password);
    String bip38 = Bip38.encryptNoEcMultiply(password, decrypted.toString());
    if (BitherjSettings.DEV_DEBUG) {
        SecureCharSequence d = Bip38.decrypt(bip38, password);
        if (d.equals(decrypted)) {
            log.info("BIP38 right");
        } else {
            throw new RuntimeException("BIP38 wrong " + d.toString() + " , " +
                    "" + decrypted.toString());
        }
    }
    decrypted.wipe();
    return bip38;
}
 
Example #3
Source File: AbstractHDAccountAddressProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public List<byte[]> getPubs(int hdAccountId, AbstractHD.PathType pathType) {
    String sql = "select pub from hd_account_addresses where path_type=? and hd_account_id=?";
    final List<byte[]> adressPubList = new ArrayList<byte[]>();
    this.execQueryLoop(sql, new String[]{Integer.toString(pathType.getValue()), Integer.toString(hdAccountId)}, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            int idColumn = c.getColumnIndex(AbstractDb.HDAccountAddressesColumns.PUB);
            if (idColumn != -1) {
                try {
                    adressPubList.add(Base58.decode(c.getString(idColumn)));
                } catch (AddressFormatException e) {
                    e.printStackTrace();
                }
            }
            return null;
        }
    });
    return adressPubList;
}
 
Example #4
Source File: AbstractAddressProvider.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Override
public List<Address> getAddresses() {
    String sql = "select address,encrypt_private_key,pub_key,is_xrandom,is_trash,is_synced,sort_time " +
            "from addresses  order by sort_time desc";
    final List<Address> addressList = new ArrayList<Address>();
    this.execQueryLoop(sql, null, new Function<ICursor, Void>() {
        @Nullable
        @Override
        public Void apply(@Nullable ICursor c) {
            Address address = null;
            try {
                address = applyAddressCursor(c);
            } catch (AddressFormatException e) {
                e.printStackTrace();
            }
            if (address != null) {
                addressList.add(address);
            }
            return null;
        }
    });
    return addressList;
}
 
Example #5
Source File: ScriptBuilder.java    From bitherj with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a scriptPubKey that encodes payment to the given address.
 */
public static Script createOutputScript(String to) {
    try {
        if (Utils.getAddressHeader(to) == BitherjSettings.p2shHeader) {
            // OP_HASH160 <scriptHash> OP_EQUAL
            return new ScriptBuilder()
                    .op(OP_HASH160)
                    .data(Utils.getAddressHash(to))
                    .op(OP_EQUAL)
                    .build();
        } else if (Utils.getAddressHeader(to) == BitherjSettings.addressHeader)  {
            // OP_DUP OP_HASH160 <pubKeyHash> OP_EQUALVERIFY OP_CHECKSIG
            return new ScriptBuilder()
                    .op(OP_DUP)
                    .op(OP_HASH160)
                    .data(Utils.getAddressHash(to))
                    .op(OP_EQUALVERIFY)
                    .op(OP_CHECKSIG)
                    .build();
        } else {
            return null;
        }
    } catch (AddressFormatException ex) {
        return null;
    }
}
 
Example #6
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");
    }
}
 
Example #7
Source File: BitherSetting.java    From bither-android with Apache License 2.0 6 votes vote down vote up
public boolean checkFormat(String content) {
    switch (this) {
        case Bither:
            //todo checkBitherQrCode
            return true;

        case Bip38:
            boolean check = false;
            try {
                check = net.bither.bitherj.crypto.bip38.Bip38.isBip38PrivateKey(content);
            } catch (AddressFormatException e) {
                e.printStackTrace();
            }
            return check;

    }
    return false;
}
 
Example #8
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 #9
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 #10
Source File: Bip38.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Decrypt a BIP38 formatted private key with a passphrase.
 * <p/>
 * This is a helper function that does everything in one go. You can call the
 * individual functions if you wish to separate it into more phases.
 *
 * @throws InterruptedException
 */
public static SecureCharSequence decrypt(String bip38PrivateKeyString, CharSequence passphrase) throws InterruptedException, AddressFormatException {
    Bip38PrivateKey bip38Key = parseBip38PrivateKey(bip38PrivateKeyString);
    if (bip38Key == null) {
        return null;
    }
    if (bip38Key.ecMultiply) {
        return decryptEcMultiply(bip38Key, passphrase);
    } else {
        byte[] stretcedKeyMaterial = bip38Stretch1(passphrase, bip38Key.salt, SCRYPT_LENGTH);
        return decryptNoEcMultiply(bip38Key, stretcedKeyMaterial);
    }
}
 
Example #11
Source File: Bip38.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Encrypt a SIPA formatted private key with a passphrase using BIP38.
 * <p/>
 * This is a helper function that does everything in one go. You can call the
 * individual functions if you wish to separate it into more phases.
 *
 * @throws InterruptedException
 */
public static String encryptNoEcMultiply(CharSequence passphrase, String base58EncodedPrivateKey) throws InterruptedException, AddressFormatException {
    DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey(base58EncodedPrivateKey);
    ECKey key = dumpedPrivateKey.getKey();
    dumpedPrivateKey.clearPrivateKey();
    byte[] salt = Bip38.calculateScryptSalt(key.toAddress());
    byte[] stretchedKeyMaterial = bip38Stretch1(passphrase, salt, SCRYPT_LENGTH);
    return encryptNoEcMultiply(stretchedKeyMaterial, key, salt);
}
 
Example #12
Source File: Bip38Test.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptCompression1() throws InterruptedException, AddressFormatException {
    String encoded = Bip38.encryptNoEcMultiply("TestingOneTwoThree",
            "L44B5gGEpqEDRS9vVPz7QT35jcBG2r3CZwSwQ4fCewXAhAhqGVpP");
    assertEquals(encoded, "6PYNKZ1EAgYgmQfmNVamxyXVWHzK5s6DGhwP4J5o44cvXdoY7sRzhtpUeo");
    assertTrue(Bip38.isBip38PrivateKey(encoded));
}
 
Example #13
Source File: Utils.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Attempts to parse the given string as arbitrary-length hex or base58 and then return the
 * results, or null if
 * neither parse was successful.
 */
public static byte[] parseAsHexOrBase58(String data) {
    try {
        return Hex.decode(data);
    } catch (Exception e) {
        // Didn't decode as hex, try base58.
        try {
            return Base58.decodeChecked(data);
        } catch (AddressFormatException e1) {
            return null;
        }
    }
}
 
Example #14
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public static Tx applyCursor(ResultSet c) throws AddressFormatException, SQLException {
    Tx txItem = new Tx();
    int idColumn = c.findColumn(AbstractDb.TxsColumns.BLOCK_NO);
    if (idColumn != -1 && c.getObject(idColumn) != null) {
        txItem.setBlockNo(c.getInt(idColumn));
    } else {
        txItem.setBlockNo(Tx.TX_UNCONFIRMED);
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_HASH);
    if (idColumn != -1) {
        txItem.setTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.SOURCE);
    if (idColumn != -1) {
        txItem.setSource(c.getInt(idColumn));
    }
    if (txItem.getSource() >= 1) {
        txItem.setSawByPeerCnt(txItem.getSource() - 1);
        txItem.setSource(1);
    } else {
        txItem.setSawByPeerCnt(0);
        txItem.setSource(0);
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_TIME);
    if (idColumn != -1) {
        txItem.setTxTime(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_VER);
    if (idColumn != -1) {
        txItem.setTxVer(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_LOCKTIME);
    if (idColumn != -1) {
        txItem.setTxLockTime(c.getInt(idColumn));
    }
    return txItem;

}
 
Example #15
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void addOutForTxDetail(String address, HashMap<Sha256Hash, Tx> txDict) throws AddressFormatException, SQLException {
    String sql = "select b.* from addresses_txs a, outs b where a.tx_hash=b.tx_hash and a.address=? "
            + "order by b.tx_hash,b.out_sn";
    PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{address});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        Out out = TxHelper.applyCursorOut(c);
        Tx tx = txDict.get(new Sha256Hash(out.getTxHash()));
        if (tx != null) {
            tx.getOuts().add(out);
        }
    }
    c.close();
    statement.close();
}
 
Example #16
Source File: AbstractHDAccountProvider.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Override
    public byte[] getExternalPub(int hdSeedId) {
        final byte[][] pub = {null};
        String sql = "select external_pub from hd_account where hd_account_id=?";
        this.execQueryOneRecord(sql, new String[]{Integer.toString(hdSeedId)}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                int idColumn = c.getColumnIndex(AbstractDb.HDAccountColumns.EXTERNAL_PUB);
                if (idColumn != -1) {
                    String pubStr = c.getString(idColumn);
                    try {
                        pub[0] = Base58.decode(pubStr);
                    } catch (AddressFormatException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        });
        return pub[0];

//        try {
//            SQLiteDatabase db = this.mDb.getReadableDatabase();
//            Cursor c = db.rawQuery("select external_pub from hd_account where hd_account_id=? "
//                    , new String[]{Integer.toString(hdSeedId)});
//            if (c.moveToNext()) {
//                int idColumn = c.getColumnIndex(AbstractDb.HDAccountColumns.EXTERNAL_PUB);
//                if (idColumn != -1) {
//                    String pubStr = c.getString(idColumn);
//                    pub = Base58.decode(pubStr);
//                }
//            }
//            c.close();
//        } catch (AddressFormatException e) {
//            e.printStackTrace();
//        }
//
//        return pub;
    }
 
Example #17
Source File: AbstractHDAccountProvider.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Override
    public byte[] getInternalPub(int hdSeedId) {
        final byte[][] pub = {null};
        String sql = "select internal_pub from hd_account where hd_account_id=? ";
        this.execQueryOneRecord(sql, new String[]{Integer.toString(hdSeedId)}, new Function<ICursor, Void>() {
            @Nullable
            @Override
            public Void apply(@Nullable ICursor c) {
                int idColumn = c.getColumnIndex(AbstractDb.HDAccountColumns.INTERNAL_PUB);
                if (idColumn != -1) {
                    String pubStr = c.getString(idColumn);
                    try {
                        pub[0] = Base58.decode(pubStr);
                    } catch (AddressFormatException e) {
                        e.printStackTrace();
                    }
                }
                return null;
            }
        });
        return pub[0];
//        try {
//            SQLiteDatabase db = this.mDb.getReadableDatabase();
//            Cursor c = db.rawQuery("select internal_pub from hd_account where hd_account_id=? "
//                    , new String[]{Integer.toString(hdSeedId)});
//            if (c.moveToNext()) {
//                int idColumn = c.getColumnIndex(AbstractDb.HDAccountColumns.INTERNAL_PUB);
//                if (idColumn != -1) {
//                    String pubStr = c.getString(idColumn);
//                    pub = Base58.decode(pubStr);
//                }
//            }
//            c.close();
//        } catch (AddressFormatException e) {
//            e.printStackTrace();
//        }
//
//
//        return pub;
    }
 
Example #18
Source File: Base58.java    From bitherj with Apache License 2.0 5 votes vote down vote up
/**
 * Uses the checksum in the last 4 bytes of the decoded data to verify the rest are correct. The checksum is
 * removed from the returned data.
 *
 * @throws AddressFormatException if the input is not base 58 or the checksum does not validate.
 */
public static byte[] decodeChecked(String input) throws AddressFormatException {
    byte tmp[] = decode(input);
    if (tmp.length < 4)
        throw new AddressFormatException("Input too short");
    byte[] bytes = copyOfRange(tmp, 0, tmp.length - 4);
    byte[] checksum = copyOfRange(tmp, tmp.length - 4, tmp.length);

    tmp = Utils.doubleDigest(bytes);
    byte[] hash = copyOfRange(tmp, 0, 4);
    if (!Arrays.equals(checksum, hash))
        throw new AddressFormatException("Checksum does not validate");

    return bytes;
}
 
Example #19
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 #20
Source File: Bip38Test.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptNoCompression() throws InterruptedException, AddressFormatException {
    String encoded = Bip38.encryptNoEcMultiply("TestingOneTwoThree",
            "5KN7MzqK5wt2TP1fQCYyHBtDrXdJuXbUzm4A9rKAteGu3Qi5CVR");
    assertEquals(encoded, "6PRVWUbkzzsbcVac2qwfssoUJAN1Xhrg6bNk8J7Nzm5H7kxEbn2Nh2ZoGg");
    assertTrue(Bip38.isBip38PrivateKey(encoded));
}
 
Example #21
Source File: PasswordSeed.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public String toPasswordSeedString() {
    try {
        String passwordSeedString = Base58.bas58ToHexWithAddress(this.address) + QRCodeUtil.QR_CODE_SPLIT
                + QRCodeUtil.getNewVersionEncryptPrivKey(this.keyStr);
        return passwordSeedString;
    } catch (AddressFormatException e) {
        throw new RuntimeException("passwordSeed  address is format error ," + this.address);

    }

}
 
Example #22
Source File: Bip38Test.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testEncryptCompression2() throws InterruptedException, AddressFormatException {
    String encoded = Bip38.encryptNoEcMultiply("Satoshi", "KwYgW8gcxj1JWJXhPSu4Fqwzfhp5Yfi42mdYmMa4XqK7NJxXUSK7"
    );
    assertEquals(encoded, "6PYLtMnXvfG3oJde97zRyLYFZCYizPU5T3LwgdYJz1fRhh16bU7u6PPmY7");
    assertTrue(Bip38.isBip38PrivateKey(encoded));
}
 
Example #23
Source File: Bip38Test.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecryptNoCompressionWithEcMultiplyWithLot2() throws InterruptedException, UnsupportedEncodingException, AddressFormatException {
    // "MOLON LABE" using greek characters  = "ΜΟΛΩΝ ΛΑΒΕ"
    String passphrase = "\u039C\u039F\u039B\u03A9\u039D \u039B\u0391\u0392\u0395";
    assertEquals("ce9cce9fce9bcea9ce9d20ce9bce91ce92ce95".toUpperCase(), Utils.bytesToHexString(passphrase.getBytes("UTF-8")));
    SecureCharSequence decoded = Bip38.decrypt("6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH", passphrase);
    assertEquals(decoded.toString(), "5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D");
}
 
Example #24
Source File: Bip38Test.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecryptNoCompressionWithEcMultiplyWithLot2WithBom() throws InterruptedException, UnsupportedEncodingException, AddressFormatException {
    // "MOLON LABE" using greek characters  = "ΜΟΛΩΝ ΛΑΒΕ"
    String passphrase = "\u039C\u039F\u039B\u03A9\u039D \u039B\u0391\u0392\u0395";
    assertEquals("ce9cce9fce9bcea9ce9d20ce9bce91ce92ce95".toUpperCase(), Utils.bytesToHexString(passphrase.getBytes("UTF-8")));
    SecureCharSequence decoded = Bip38.decrypt("\uFEFF6PgGWtx25kUg8QWvwuJAgorN6k9FbE25rv5dMRwu5SKMnfpfVe5mar2ngH", passphrase);
    assertEquals(decoded.toString(), "5KMKKuUmAkiNbA3DazMQiLfDq47qs8MAEThm4yL8R2PhV1ov33D");
}
 
Example #25
Source File: AddressTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testAddress() {
    try {
        DumpedPrivateKey dumpedPrivateKey = new DumpedPrivateKey("L4rK1yDtCWekvXuE6oXD9jCYfFNV2cWRpVuPLBcCU2z8TrisoyY1");
        ECKey ecKey = dumpedPrivateKey.getKey();
        String addressStr = ecKey.toAddress();
        assertEquals(ecKey.toAddress(), "1F3sAm6ZtwLAUnj7d38pGFxtP3RVEvtsbV");
    } catch (AddressFormatException e) {
        e.printStackTrace();
    }
}
 
Example #26
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public static Tx applyCursor(ResultSet c) throws AddressFormatException, SQLException {
    Tx txItem = new Tx();
    int idColumn = c.findColumn(AbstractDb.TxsColumns.BLOCK_NO);
    if (idColumn != -1 && c.getObject(idColumn) != null) {
        txItem.setBlockNo(c.getInt(idColumn));
    } else {
        txItem.setBlockNo(Tx.TX_UNCONFIRMED);
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_HASH);
    if (idColumn != -1) {
        txItem.setTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.SOURCE);
    if (idColumn != -1) {
        txItem.setSource(c.getInt(idColumn));
    }
    if (txItem.getSource() >= 1) {
        txItem.setSawByPeerCnt(txItem.getSource() - 1);
        txItem.setSource(1);
    } else {
        txItem.setSawByPeerCnt(0);
        txItem.setSource(0);
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_TIME);
    if (idColumn != -1) {
        txItem.setTxTime(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_VER);
    if (idColumn != -1) {
        txItem.setTxVer(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.TxsColumns.TX_LOCKTIME);
    if (idColumn != -1) {
        txItem.setTxLockTime(c.getInt(idColumn));
    }
    return txItem;

}
 
Example #27
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 #28
Source File: TxHelper.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public static Out applyCursorOut(ResultSet c) throws AddressFormatException, SQLException {
    Out outItem = new Out();
    int idColumn = c.findColumn(AbstractDb.OutsColumns.TX_HASH);
    if (idColumn != -1) {
        outItem.setTxHash(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.OutsColumns.OUT_SN);
    if (idColumn != -1) {
        outItem.setOutSn(c.getInt(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.OutsColumns.OUT_SCRIPT);
    if (idColumn != -1) {
        outItem.setOutScript(Base58.decode(c.getString(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.OutsColumns.OUT_VALUE);
    if (idColumn != -1) {
        outItem.setOutValue(c.getLong(idColumn));
    }
    idColumn = c.findColumn(AbstractDb.OutsColumns.OUT_STATUS);
    if (idColumn != -1) {
        outItem.setOutStatus(Out.getOutStatus(c.getInt(idColumn)));
    }
    idColumn = c.findColumn(AbstractDb.OutsColumns.OUT_ADDRESS);
    if (idColumn != -1) {
        outItem.setOutAddress(c.getString(idColumn));
    }
    return outItem;
}
 
Example #29
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 #30
Source File: TxProvider.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
private void addOutForTxDetail(String address, HashMap<Sha256Hash, Tx> txDict) throws AddressFormatException, SQLException {
    String sql = "select b.* from addresses_txs a, outs b where a.tx_hash=b.tx_hash and a.address=? "
            + "order by b.tx_hash,b.out_sn";
    PreparedStatement statement = this.mDb.getPreparedStatement(sql, new String[]{address});
    ResultSet c = statement.executeQuery();
    while (c.next()) {
        Out out = TxHelper.applyCursorOut(c);
        Tx tx = txDict.get(new Sha256Hash(out.getTxHash()));
        if (tx != null) {
            tx.getOuts().add(out);
        }
    }
    c.close();
    statement.close();
}