Java Code Examples for net.bither.bitherj.utils.Utils#hexStringToByteArray()

The following examples show how to use net.bither.bitherj.utils.Utils#hexStringToByteArray() . 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: QRCodeEnodeUtil.java    From bitherj with Apache License 2.0 6 votes vote down vote up
public static List<Address> formatPublicString(String content) {
    String[] strs = QRCodeUtil.splitString(content);
    ArrayList<Address> wallets = new ArrayList<Address>();
    for (String str : strs) {
        boolean isXRandom = false;
        if (str.indexOf(QRCodeUtil.XRANDOM_FLAG) == 0) {
            isXRandom = true;
            str = str.substring(1);
        }
        byte[] pub = Utils.hexStringToByteArray(str);

        org.spongycastle.math.ec.ECPoint ecPoint = ECKey.checkPoint(pub);
        if (ecPoint != null && ecPoint.isValid()) {
            String addString = Utils.toAddress(Utils.sha256hash160(pub));
            Address address = new Address(addString, pub, null, false, isXRandom);
            wallets.add(address);
        }
    }
    return wallets;

}
 
Example 2
Source File: ECKeyTest.java    From bitherj with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetPubs() {
    List<byte[]> pubKeyS = Arrays.asList(Utils.hexStringToByteArray("02d8ed584a211a9195f0d580617c60398f82f58dcd5b104249737762656e62d52e"),
            Utils.hexStringToByteArray("026fd10f953a13ac14041460cd01eab3d665d140d1c978a01db4bc669bab9a77db"), Utils.hexStringToByteArray("03b5eceb6f5a9a12b8b7fe23ae6297bfdb46aeab39ab0ee89efd4068d251667ae0"));
    byte[] params = Utils.hexStringToByteArray("522102d8ed584a211a9195f0d580617c60398f82f58dcd5b104249737762656e62d52e21026fd10f953a13ac14041460cd01eab3d665d140d1c978a01db4bc669bab9a77db2103b5eceb6f5a9a12b8b7fe23ae6297bfdb46aeab39ab0ee89efd4068d251667ae053ae");
    Tx tx = new Tx(Utils.hexStringToByteArray("010000000196d607b6c1647a1cccd9db40e918627e4d5e190ba56a5663ccef5e1a8ecada0700000000fdfd0000473044022041b2a5f1965b060bbf484218ac1e3b7ced9ec908078ca4565f9c4eef4f0dfec902206c3f3a1320dac89fd1e58627a1208510328d618365dd033d90ce5230c40884fa01483045022100abd2696052faa6e707e02402b7c654d0ab3be7d1d7f14af541abf99eaf16e1fd02205da8e8e2c4a9795046aa4c380e6f1c005e6619b16f7182df37b54405eb28aedc014c69522102d8ed584a211a9195f0d580617c60398f82f58dcd5b104249737762656e62d52e21026fd10f953a13ac14041460cd01eab3d665d140d1c978a01db4bc669bab9a77db2103b5eceb6f5a9a12b8b7fe23ae6297bfdb46aeab39ab0ee89efd4068d251667ae053aeffffffff0128230000000000001976a914f307ea0809f5c60d42482e57dfdd78ed53df580688ac00000000"));
    List<byte[]> signPubs = tx.getIns().get(0).getP2SHPubKeys();
    for (byte[] signs : signPubs) {
        boolean isPub = false;
        for (byte[] pubs : pubKeyS) {
            isPub = Arrays.equals(signs, pubs);
            if (isPub) {
                break;
            }
        }
        System.out.println("pub:" + Utils.bytesToHexString(signs));
        assertTrue(Utils.bytesToHexString(signs), isPub);
    }

}
 
Example 3
Source File: HDMKeychainRecoveryUtil.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public void setColdRoot(String coldRootString) {
    String result = coldRootString;
    coldRoot = Utils.hexStringToByteArray(result);
    try {
        lock.lock();
        coldRootCondition.signal();
    } finally {
        lock.unlock();
    }

}
 
Example 4
Source File: HDMKeychainRecoveryUtil.java    From bither-android with Apache License 2.0 5 votes vote down vote up
public boolean onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == ColdRootRequestCode) {
        if (resultCode == Activity.RESULT_OK) {
            String result = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
            if (result.length() % 2 == 0) {
                coldRoot = Utils.hexStringToByteArray(result);
            } else {
                coldRoot = null;
            }
        } else {
            coldRoot = null;
        }
        try {
            lock.lock();
            coldRootCondition.signal();
        } finally {
            lock.unlock();
        }
        return true;
    }
    if (requestCode == ServerQRCodeRequestCode) {
        if (resultCode == Activity.RESULT_OK) {
            hdmBidSignature = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
        }
        try {
            lock.lock();
            hdmIdCondiction.signal();
        } finally {
            lock.unlock();
        }
        return true;
    }
    return false;
}
 
Example 5
Source File: EncryptedData.java    From bitherj with Apache License 2.0 5 votes vote down vote up
public EncryptedData(String str) {
    String[] strs = QRCodeUtil.splitOfPasswordSeed(str);
    if (strs.length != 3) {
        log.error("decryption: EncryptedData format error");
    }
    initialisationVector = Utils.hexStringToByteArray
            (strs[1]);
    encryptedData = Utils.hexStringToByteArray(strs[0]);
    byte[] saltQRCodes = Utils.hexStringToByteArray(strs[2]);
    saltForQRCode = new SaltForQRCode(saltQRCodes);
}
 
Example 6
Source File: ScriptTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
    public void testScriptSig() throws Exception {
        byte[] sigProgBytes = Utils.hexStringToByteArray(sigProg);
        Script script = new Script(sigProgBytes);
        // Test we can extract the from address.
        byte[] hash160 = Utils.sha256hash160(script.getPubKey());
//        Address a = new Address(params, hash160);
        String address = this.toAddress(hash160, 111);
        assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", address);
    }
 
Example 7
Source File: ScriptTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
    public void testScriptPubKey() throws Exception {
        // Check we can extract the to address
        byte[] pubkeyBytes = Utils.hexStringToByteArray(pubkeyProg);
        Script pubkey = new Script(pubkeyBytes);
        assertEquals("DUP HASH160 PUSHDATA(20)[33e81a941e64cda12c6a299ed322ddbdd03f8d0e] EQUALVERIFY CHECKSIG", pubkey.toString());
//        Address toAddr = new Address(params, pubkey.getPubKeyHash());
        String toAddr = this.toAddress(pubkey.getPubKeyHash(), 111);
        assertEquals("mkFQohBpy2HDXrCwyMrYL5RtfrmeiuuPY2", toAddr);
    }
 
Example 8
Source File: TxTest.java    From bitherj with Apache License 2.0 5 votes vote down vote up
@Test
public void testConstructor() {
    byte[] rawTx = Utils.hexStringToByteArray("0100000001bdc0141fe3e5c2223a6d26a95acbf791042d93f9d9b8b38f133bf7adb5c1e293010000006a47304402202214770c0f5a9261190337273219a108132a4bc987c745db8dd6daded34b0dcb0220573de1d973166024b8342d6b6fef2a864a06cceee6aee13a910e5d8df465ed2a01210382b259804ad8d88b96a23222e24dd5a130d39588e78960c9e9b48a5b49943649ffffffff02a0860100000000001976a91479a7bf0bba8359561d4dab457042d7b632d5e64188ac605b0300000000001976a914b036c529faeca8040232cc4bd5918e709e90c4ff88ac00000000");
    Tx tx = new Tx(rawTx);
    byte[] txBytes = tx.bitcoinSerialize();
    assertTrue(Arrays.equals(rawTx, txBytes));
    byte[] exceptTxHash = Utils.reverseBytes(Utils.hexStringToByteArray("584985ca8a9ed57987da36ea3d13fe05a7c498f2098ddeb6c8d0f3214067640c"));
    byte[] txHash = tx.getTxHash();
    for (Out out : tx.getOuts()) {
        String outAddress = out.getOutAddress();
    }
    assertTrue(Arrays.equals(exceptTxHash, txHash));
}
 
Example 9
Source File: HDMKeychainRecoveryUtil.java    From bither-desktop-java with Apache License 2.0 5 votes vote down vote up
public void setColdRoot(String coldRootString) {
    String result = coldRootString;
    coldRoot = Utils.hexStringToByteArray(result);
    try {
        lock.lock();
        coldRootCondition.signal();
    } finally {
        lock.unlock();
    }

}
 
Example 10
Source File: EnterpriseHDMKeychainAddNewAddressActivity.java    From bither-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (AddPubCode == requestCode) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
            if (Utils.isEmpty(result)) {
                return;
            }
            LogUtil.d("AddPub", "pub : " + result);
            byte[] pub = null;
            try {
                if (result.length() == 130) {
                    pub = Utils.hexStringToByteArray(result);
                    if (HDKeyDerivation.createMasterPubKeyFromExtendedBytes(Arrays.copyOf
                            (pub, pub.length)) == null) {
                        pub = null;
                    }
                }
            } catch (Exception e) {
                pub = null;
                e.printStackTrace();
            }
            if (pub == null) {
                DropdownMessage.showDropdownMessage(this, R.string
                        .enterprise_hdm_keychain_collect_pub_error);
                return;
            }
            pubs.add(pub);
            for (int i = 0;
                 i < pubs.size() && i < llPubs.getChildCount();
                 i++) {
                View v = llPubs.getChildAt(i);
                v.setBackgroundColor(getResources().getColor(R.color.blue_dark));
            }
            if (pubs.size() < pubCount()) {
                ibtnAddPub.setVisibility(View.VISIBLE);
                btnCollectFinish.setEnabled(false);
            } else {
                ibtnAddPub.setVisibility(View.GONE);
                btnCollectFinish.setEnabled(true);
            }
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example 11
Source File: AddEnterpriseHDMKeychainActivity.java    From bither-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (AddPubCode == requestCode) {
        if (resultCode == RESULT_OK) {
            String result = data.getStringExtra(ScanActivity.INTENT_EXTRA_RESULT);
            if (Utils.isEmpty(result)) {
                return;
            }
            if (!result.startsWith(EnterpriseHDMSeed.XPubPrefix)) {
                DropdownMessage.showDropdownMessage(this, R.string
                        .enterprise_hdm_keychain_collect_pub_error);
                return;
            }
            result = result.substring(EnterpriseHDMSeed.XPubPrefix.length());
            LogUtil.d("AddPub", "pub : " + result);
            byte[] pub = null;
            try {
                if (result.length() == 130) {
                    pub = Utils.hexStringToByteArray(result);
                    if (HDKeyDerivation.createMasterPubKeyFromExtendedBytes(Arrays.copyOf
                            (pub, pub.length)) == null) {
                        pub = null;
                    }
                }
            } catch (Exception e) {
                pub = null;
                e.printStackTrace();
            }
            if (pub == null) {
                DropdownMessage.showDropdownMessage(this, R.string
                        .enterprise_hdm_keychain_collect_pub_error);
                return;
            }
            pubs.add(pub);
            for (int i = 0;
                 i < pubs.size() && i < llPubs.getChildCount();
                 i++) {
                View v = llPubs.getChildAt(i);
                v.setBackgroundColor(getResources().getColor(R.color.blue_dark));
            }
            if (pubs.size() < pubCount()) {
                ibtnAddPub.setVisibility(View.VISIBLE);
                btnCollectFinish.setEnabled(false);
            } else {
                ibtnAddPub.setVisibility(View.GONE);
                btnCollectFinish.setEnabled(true);
            }
        }
        return;
    }
    super.onActivityResult(requestCode, resultCode, data);
}
 
Example 12
Source File: ScriptTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test
public void testIp() throws Exception {
    byte[] bytes = Utils.hexStringToByteArray("41043e96222332ea7848323c08116dddafbfa917b8e37f0bdf63841628267148588a09a43540942d58d49717ad3fabfe14978cf4f0a8b84d2435dad16e9aa4d7f935ac");
    Script s = new Script(bytes);
    assertTrue(s.isSentToRawPubKey());
}
 
Example 13
Source File: MnemonicCodeTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test(expected = MnemonicException.MnemonicLengthException.class)
public void testBadEntropyLength() throws Exception {
    byte[] entropy = Utils.hexStringToByteArray("7f7f7f7f7f7f7f7f7f7f7f7f7f7f");
    mc.toMnemonic(entropy);
}
 
Example 14
Source File: MultisigTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddress() {
    String pubHot = "026e3f39cd82606a3aa0d9c8194cf516b98ee51c1107e6c7f334cde22b5059e928";
    String pubCold = "034d490441de1cc4a8f1e7192083583c16e513b3b550c8410500db7853fd1fa5fe";
    String pubRemote = "0255b72bc52dfa0ffc40742b1a3eb01858714341c1f72bc1f8fdc731098323e96e";
    String address = "3K2Cbzxfoxey8cbq1w2YutLzhvxByxvNxy";

    ECKey keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    ECKey keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    ECKey keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    List<byte[]> pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    Script script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    String multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "033dc6dcf7d90cb8f4ee3adbc87bf55c700d6c32a74800af6de6e1af57f46bfc41";
    pubCold = "025ed1f76ae3fc0cb84782131594020e885a060daf9f55c199fdb299e7169779b9";
    pubRemote = "0378b509c95fd7aa30dc82c4bbe8b84dcb8bb7d7224d891cce7ccf454c79527b5d";
    address = "3ELN8yYSGoz4fTy8HSbfgLRoDWBU6p9zev";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);

    pubHot = "03d29143a6b76d393075d620df9cf80bbb5eaceb2e2b57e5cc4704a6eb3c125a8d";
    pubCold = "03f7d2d484d903fa498d6069009e77ed9ad0842947a7a58441f9406a4728ae2240";
    pubRemote = "02a5fc2584b879fa5a7b04e67d7ab8abb3b08d7981f9f24b03e9353355162c2e04";
    address = "34RgHSRfg3P7FSk3YBbcWnHaMWxapMtrWf";

    keyHot = new ECKey(null, Utils.hexStringToByteArray(pubHot));
    keyCold = new ECKey(null, Utils.hexStringToByteArray(pubCold));
    keyRemote = new ECKey(null, Utils.hexStringToByteArray(pubRemote));
    pubKeyList = new ArrayList<byte[]>();
    pubKeyList.add(keyHot.getPubKey());
    pubKeyList.add(keyCold.getPubKey());
    pubKeyList.add(keyRemote.getPubKey());

    script = ScriptBuilder.createMultiSigOutputScript(2, pubKeyList);
    multisigAddress = Utils.toP2SHAddress(Utils.sha256hash160(script.getProgram()));

    assertEquals(address, multisigAddress);
}
 
Example 15
Source File: MultisigAddressTest.java    From bitherj with Apache License 2.0 4 votes vote down vote up
TestCase(HDMAddress.Pubs pubs, String address, String script) {
    this.pubs = pubs;
    this.address = address;
    this.script = Utils.hexStringToByteArray(script);
}