Search in sources :

Example 1 with HD_Address

use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.

the class APIFactory method lockXPUB.

public synchronized JSONObject lockXPUB(String xpub, boolean bip49) {
    String _url = SamouraiWallet.getInstance().isTestNet() ? WebUtil.SAMOURAI_API2_TESTNET : WebUtil.SAMOURAI_API2;
    JSONObject jsonObject = null;
    try {
        String response = null;
        ECKey ecKey = null;
        if (AddressFactory.getInstance(context).xpub2account().get(xpub) != null || xpub.equals(BIP49Util.getInstance(context).getWallet().getAccount(0).ypubstr())) {
            HD_Address addr = null;
            if (bip49) {
                addr = BIP49Util.getInstance(context).getWallet().getAccountAt(0).getChange().getAddressAt(0);
            } else {
                addr = HD_WalletFactory.getInstance(context).get().getAccount(0).getChain(AddressFactory.CHANGE_CHAIN).getAddressAt(0);
            }
            ecKey = addr.getECKey();
            if (ecKey != null && ecKey.hasPrivKey()) {
                String sig = ecKey.signMessage("lock");
                String address = null;
                if (bip49) {
                    SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
                    address = segwitAddress.getAddressAsString();
                } else {
                    address = ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
                }
                if (!TorUtil.getInstance(context).statusFromBroadcast()) {
                    StringBuilder args = new StringBuilder();
                    args.append("address=");
                    args.append(address);
                    args.append("&signature=");
                    args.append(Uri.encode(sig));
                    args.append("&message=");
                    args.append("lock");
                    // Log.i("APIFactory", "lock XPUB:" + args.toString());
                    response = WebUtil.getInstance(context).postURL(_url + "xpub/" + xpub + "/lock/", args.toString());
                // Log.i("APIFactory", "lock XPUB response:" + response);
                } else {
                    HashMap<String, String> args = new HashMap<String, String>();
                    args.put("address", address);
                    args.put("signature", Uri.encode(sig));
                    args.put("message", "lock");
                    // Log.i("APIFactory", "lock XPUB:" + args.toString());
                    response = WebUtil.getInstance(context).tor_postURL(_url + "xpub" + xpub + "/lock/", args);
                // Log.i("APIFactory", "lock XPUB response:" + response);
                }
                try {
                    jsonObject = new JSONObject(response);
                    if (jsonObject.has("status") && jsonObject.getString("status").equals("ok")) {
                        if (bip49) {
                            PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB49LOCK, true);
                        } else {
                            PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB44LOCK, true);
                        }
                    }
                } catch (JSONException je) {
                    je.printStackTrace();
                    jsonObject = null;
                }
            }
        }
    } catch (Exception e) {
        jsonObject = null;
        e.printStackTrace();
    }
    return jsonObject;
}
Also used : JSONObject(org.json.JSONObject) HD_Address(com.samourai.wallet.hd.HD_Address) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress) HashMap(java.util.HashMap) JSONException(org.json.JSONException) ECKey(org.bitcoinj.core.ECKey) JSONException(org.json.JSONException) AddressFormatException(org.bitcoinj.core.AddressFormatException) MnemonicException(org.bitcoinj.crypto.MnemonicException) IOException(java.io.IOException)

Example 2 with HD_Address

use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.

the class RicochetMeta method getHopTx.

private Transaction getHopTx(String prevTxHash, int prevTxN, String scriptPubKey, int prevIndex, long spendAmount, String destination) {
    Transaction tx = null;
    HD_Address address = null;
    try {
        address = HD_WalletFactory.getInstance(context).get().getAccountAt(RICOCHET_ACCOUNT).getChain(0).getAddressAt(prevIndex);
        ECKey ecKey = address.getECKey();
        // Log.d("RicochetMeta", "getHopTx address:" + ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
        byte[] hashBytes = Hex.decode(prevTxHash);
        Sha256Hash txHash = new Sha256Hash(hashBytes);
        TransactionOutPoint outpoint = new TransactionOutPoint(SamouraiWallet.getInstance().getCurrentNetworkParams(), prevTxN, txHash);
        TransactionInput input = new TransactionInput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Hex.decode(scriptPubKey), outpoint);
        Script outputScript = null;
        TransactionOutput output = null;
        if (destination.toLowerCase().startsWith("tb") || destination.toLowerCase().startsWith("bc")) {
            byte[] bScriptPubKey = null;
            try {
                Pair<Byte, byte[]> pair = Bech32Segwit.decode(SamouraiWallet.getInstance().isTestNet() ? "tb" : "bc", destination);
                bScriptPubKey = Bech32Segwit.getScriptPubkey(pair.getLeft(), pair.getRight());
            } catch (Exception e) {
                return null;
            }
            output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), bScriptPubKey);
        } else {
            outputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), destination));
            output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), outputScript.getProgram());
        }
        tx = new Transaction(SamouraiWallet.getInstance().getCurrentNetworkParams());
        tx.addInput(input);
        tx.addOutput(output);
        TransactionSignature sig = tx.calculateSignature(0, ecKey, Hex.decode(scriptPubKey), Transaction.SigHash.ALL, false);
        tx.getInput(0).setScriptSig(ScriptBuilder.createInputScript(sig, ecKey));
    } catch (IOException ioe) {
        return null;
    } catch (MnemonicException.MnemonicLengthException mle) {
        return null;
    }
    return tx;
}
Also used : Script(org.bitcoinj.script.Script) TransactionOutput(org.bitcoinj.core.TransactionOutput) HD_Address(com.samourai.wallet.hd.HD_Address) Sha256Hash(org.bitcoinj.core.Sha256Hash) ECKey(org.bitcoinj.core.ECKey) TransactionSignature(org.bitcoinj.crypto.TransactionSignature) IOException(java.io.IOException) TransactionInput(org.bitcoinj.core.TransactionInput) MnemonicException(org.bitcoinj.crypto.MnemonicException) JSONException(org.json.JSONException) IOException(java.io.IOException) MnemonicException(org.bitcoinj.crypto.MnemonicException) Transaction(org.bitcoinj.core.Transaction) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 3 with HD_Address

use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.

the class SendFactory method getPrivKey.

public static ECKey getPrivKey(String address) {
    ECKey ecKey = null;
    try {
        String path = APIFactory.getInstance(context).getUnspentPaths().get(address);
        // Log.d("APIFactory", "address:" + path);
        if (path != null) {
            String[] s = path.split("/");
            if (Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
                // Log.d("APIFactory", "address type:" + "bip49");
                HD_Address addr = BIP49Util.getInstance(context).getWallet().getAccount(0).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
                ecKey = addr.getECKey();
            } else {
                // Log.d("APIFactory", "address type:" + "bip44");
                int account_no = APIFactory.getInstance(context).getUnspentAccounts().get(address);
                HD_Address hd_address = AddressFactory.getInstance(context).get(account_no, Integer.parseInt(s[1]), Integer.parseInt(s[2]));
                String strPrivKey = hd_address.getPrivateKeyString();
                DumpedPrivateKey pk = new DumpedPrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), strPrivKey);
                ecKey = pk.getKey();
            }
        } else {
            // Log.d("APIFactory", "address type:" + "bip47");
            String pcode = BIP47Meta.getInstance().getPCode4Addr(address);
            int idx = BIP47Meta.getInstance().getIdx4Addr(address);
            PaymentAddress addr = BIP47Util.getInstance(context).getReceiveAddress(new PaymentCode(pcode), idx);
            ecKey = addr.getReceiveECKey();
        }
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
    return ecKey;
}
Also used : PaymentCode(com.samourai.wallet.bip47.rpc.PaymentCode) HD_Address(com.samourai.wallet.hd.HD_Address) ECKey(org.bitcoinj.core.ECKey) PaymentAddress(com.samourai.wallet.bip47.rpc.PaymentAddress) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 4 with HD_Address

use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.

the class BIP84Util method getAddressAt.

public SegwitAddress getAddressAt(int chain, int idx) {
    HD_Address addr = getWallet().getAccount(0).getChain(chain).getAddressAt(idx);
    SegwitAddress segwitAddress = new SegwitAddress(addr.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
    return segwitAddress;
}
Also used : HD_Address(com.samourai.wallet.hd.HD_Address)

Example 5 with HD_Address

use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.

the class AddressFactory method getBIP49.

public SegwitAddress getBIP49(int chain) {
    int idx = 0;
    HD_Address addr = null;
    SegwitAddress p2shp2wpkh = null;
    // try	{
    HD_Wallet hdw = BIP49Util.getInstance(context).getWallet();
    if (hdw != null) {
        idx = BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddrIdx();
        addr = BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddressAt(idx);
        p2shp2wpkh = new SegwitAddress(addr.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
        if (chain == RECEIVE_CHAIN && canIncBIP49ReceiveAddress(idx)) {
            BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).incAddrIdx();
        // PayloadUtil.getInstance(context).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(context).getGUID() + AccessFactory.getInstance(context).getPIN()));
        }
    }
    return p2shp2wpkh;
}
Also used : HD_Wallet(com.samourai.wallet.hd.HD_Wallet) HD_Address(com.samourai.wallet.hd.HD_Address) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress)

Aggregations

HD_Address (com.samourai.wallet.hd.HD_Address)17 ECKey (org.bitcoinj.core.ECKey)11 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)10 IOException (java.io.IOException)9 MnemonicException (org.bitcoinj.crypto.MnemonicException)9 JSONException (org.json.JSONException)7 HashMap (java.util.HashMap)5 AddressFormatException (org.bitcoinj.core.AddressFormatException)5 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)4 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)4 JSONObject (org.json.JSONObject)4 PaymentAddress (com.samourai.wallet.bip47.rpc.PaymentAddress)3 PaymentCode (com.samourai.wallet.bip47.rpc.PaymentCode)3 HD_Wallet (com.samourai.wallet.hd.HD_Wallet)3 Transaction (org.bitcoinj.core.Transaction)3 TransactionInput (org.bitcoinj.core.TransactionInput)3 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)3 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)3 Script (org.bitcoinj.script.Script)3 ScriptException (org.bitcoinj.script.ScriptException)3