Search in sources :

Example 1 with ScriptException

use of org.bitcoinj.script.ScriptException in project samourai-wallet-android by Samourai-Wallet.

the class SendFactory method signTransaction.

public Transaction signTransaction(Transaction unsignedTx) {
    HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
    for (TransactionInput input : unsignedTx.getInputs()) {
        try {
            byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
            String address = new Script(scriptBytes).getToAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
            // Log.i("address from script", address);
            ECKey ecKey = null;
            ecKey = getPrivKey(address);
            if (ecKey != null) {
                keyBag.put(input.getOutpoint().toString(), ecKey);
            } else {
                throw new RuntimeException("ECKey error: cannot process private key");
            // Log.i("ECKey error", "cannot process private key");
            }
        } catch (ScriptException se) {
            ;
        } catch (Exception e) {
            ;
        }
    }
    Transaction signedTx = signTransaction(unsignedTx, keyBag);
    if (signedTx == null) {
        return null;
    } else {
        String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
        if (hexString.length() > (100 * 1024)) {
            Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
        // Log.i("SendFactory", "Transaction length too long");
        }
        return signedTx;
    }
}
Also used : Script(org.bitcoinj.script.Script) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) TransactionInput(org.bitcoinj.core.TransactionInput) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 2 with ScriptException

use of org.bitcoinj.script.ScriptException in project samourai-wallet-android by Samourai-Wallet.

the class SendFactory method signTransactionForSweep.

public Transaction signTransactionForSweep(Transaction unsignedTx, PrivKeyReader privKeyReader) {
    HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
    for (TransactionInput input : unsignedTx.getInputs()) {
        try {
            byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
            // String address = new BitcoinScript(scriptBytes).getAddress().toString();
            String address = new Script(scriptBytes).getToAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
            // Log.i("address from script", address);
            ECKey ecKey = null;
            try {
                DumpedPrivateKey pk = new DumpedPrivateKey(SamouraiWallet.getInstance().getCurrentNetworkParams(), privKeyReader.getKey().getPrivateKeyAsWiF(SamouraiWallet.getInstance().getCurrentNetworkParams()));
                ecKey = pk.getKey();
            // Log.i("SendFactory", "ECKey address:" + ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
            } catch (AddressFormatException afe) {
                afe.printStackTrace();
                continue;
            }
            if (ecKey != null) {
                keyBag.put(input.getOutpoint().toString(), ecKey);
            } else {
                Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
            // Log.i("ECKey error", "cannot process private key");
            }
        } catch (ScriptException se) {
            ;
        } catch (Exception e) {
            ;
        }
    }
    Transaction signedTx = signTransaction(unsignedTx, keyBag);
    if (signedTx == null) {
        return null;
    } else {
        String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
        if (hexString.length() > (100 * 1024)) {
            Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
        // Log.i("SendFactory", "Transaction length too long");
        }
        return signedTx;
    }
}
Also used : Script(org.bitcoinj.script.Script) AddressFormatException(org.bitcoinj.core.AddressFormatException) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) TransactionInput(org.bitcoinj.core.TransactionInput) MnemonicException(org.bitcoinj.crypto.MnemonicException) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) IOException(java.io.IOException)

Example 3 with ScriptException

use of org.bitcoinj.script.ScriptException in project sentinel-android by Samourai-Wallet.

the class SendFactory method signTransactionForSweep.

public Transaction signTransactionForSweep(Transaction unsignedTx, PrivKeyReader privKeyReader) {
    HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
    for (TransactionInput input : unsignedTx.getInputs()) {
        try {
            byte[] scriptBytes = input.getOutpoint().getConnectedPubKeyScript();
            // String address = new BitcoinScript(scriptBytes).getAddress().toString();
            String address = new Script(scriptBytes).getToAddress(MainNetParams.get()).toString();
            // Log.i("address from script", address);
            ECKey ecKey = null;
            try {
                DumpedPrivateKey pk = new DumpedPrivateKey(MainNetParams.get(), privKeyReader.getKey().getPrivateKeyAsWiF(MainNetParams.get()));
                ecKey = pk.getKey();
            // Log.i("SendFactory", "ECKey address:" + ecKey.toAddress(MainNetParams.get()).toString());
            } catch (AddressFormatException afe) {
                afe.printStackTrace();
                continue;
            }
            if (ecKey != null) {
                keyBag.put(input.getOutpoint().toString(), ecKey);
            } else {
                Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
            // Log.i("ECKey error", "cannot process private key");
            }
        } catch (ScriptException se) {
            ;
        } catch (Exception e) {
            ;
        }
    }
    Transaction signedTx = signTransaction(unsignedTx, keyBag);
    if (signedTx == null) {
        return null;
    } else {
        String hexString = new String(Hex.encode(signedTx.bitcoinSerialize()));
        if (hexString.length() > (100 * 1024)) {
            Toast.makeText(context, R.string.tx_length_error, Toast.LENGTH_SHORT).show();
        // Log.i("SendFactory", "Transaction length too long");
        }
        return signedTx;
    }
}
Also used : Script(org.bitcoinj.script.Script) AddressFormatException(org.bitcoinj.core.AddressFormatException) ScriptException(org.bitcoinj.script.ScriptException) Transaction(org.bitcoinj.core.Transaction) HashMap(java.util.HashMap) ECKey(org.bitcoinj.core.ECKey) DumpedPrivateKey(org.bitcoinj.core.DumpedPrivateKey) TransactionInput(org.bitcoinj.core.TransactionInput) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException)

Example 4 with ScriptException

use of org.bitcoinj.script.ScriptException in project sentinel-android by Samourai-Wallet.

the class SendFactory method signTransaction.

private synchronized Transaction signTransaction(Transaction transaction, HashMap<String, ECKey> keyBag) throws ScriptException {
    List<TransactionInput> inputs = transaction.getInputs();
    TransactionInput input = null;
    TransactionOutput connectedOutput = null;
    byte[] connectedPubKeyScript = null;
    TransactionSignature sig = null;
    Script scriptPubKey = null;
    ECKey key = null;
    for (int i = 0; i < inputs.size(); i++) {
        input = inputs.get(i);
        key = keyBag.get(input.getOutpoint().toString());
        connectedPubKeyScript = input.getOutpoint().getConnectedPubKeyScript();
        connectedOutput = input.getOutpoint().getConnectedOutput();
        scriptPubKey = connectedOutput.getScriptPubKey();
        String script = Hex.toHexString(connectedPubKeyScript);
        String address = null;
        if (Bech32Util.getInstance().isBech32Script(script)) {
            try {
                address = Bech32Util.getInstance().getAddressFromScript(script);
            } catch (Exception e) {
                ;
            }
        } else {
            address = new Script(connectedPubKeyScript).getToAddress(MainNetParams.get()).toString();
        }
        if (FormatsUtil.getInstance().isValidBech32(address) || Address.fromBase58(MainNetParams.get(), address).isP2SHAddress()) {
            final P2SH_P2WPKH p2shp2wpkh = new P2SH_P2WPKH(key.getPubKey(), MainNetParams.get());
            System.out.println("pubKey:" + Hex.toHexString(key.getPubKey()));
            // final Script scriptPubKey = p2shp2wpkh.segWitOutputScript();
            // System.out.println("scriptPubKey:" + Hex.toHexString(scriptPubKey.getProgram()));
            System.out.println("to address from script:" + scriptPubKey.getToAddress(MainNetParams.get()).toString());
            final Script redeemScript = p2shp2wpkh.segWitRedeemScript();
            System.out.println("redeem script:" + Hex.toHexString(redeemScript.getProgram()));
            final Script scriptCode = redeemScript.scriptCode();
            System.out.println("script code:" + Hex.toHexString(scriptCode.getProgram()));
            sig = transaction.calculateWitnessSignature(i, key, scriptCode, connectedOutput.getValue(), Transaction.SigHash.ALL, false);
            final TransactionWitness witness = new TransactionWitness(2);
            witness.setPush(0, sig.encodeToBitcoin());
            witness.setPush(1, key.getPubKey());
            transaction.setWitness(i, witness);
            if (!FormatsUtil.getInstance().isValidBech32(address) && Address.fromBase58(MainNetParams.get(), address).isP2SHAddress()) {
                final ScriptBuilder sigScript = new ScriptBuilder();
                sigScript.data(redeemScript.getProgram());
                transaction.getInput(i).setScriptSig(sigScript.build());
                transaction.getInput(i).getScriptSig().correctlySpends(transaction, i, scriptPubKey, connectedOutput.getValue(), Script.ALL_VERIFY_FLAGS);
            }
        } else {
            if (key != null && key.hasPrivKey() || key.isEncrypted()) {
                sig = transaction.calculateSignature(i, key, connectedPubKeyScript, Transaction.SigHash.ALL, false);
            } else {
                // watch only ?
                sig = TransactionSignature.dummy();
            }
            if (scriptPubKey.isSentToAddress()) {
                input.setScriptSig(ScriptBuilder.createInputScript(sig, key));
            } else if (scriptPubKey.isSentToRawPubKey()) {
                input.setScriptSig(ScriptBuilder.createInputScript(sig));
            } else {
                throw new RuntimeException("Unknown script type: " + scriptPubKey);
            }
        }
    }
    return transaction;
}
Also used : Script(org.bitcoinj.script.Script) TransactionOutput(org.bitcoinj.core.TransactionOutput) TransactionWitness(org.bitcoinj.core.TransactionWitness) TransactionSignature(org.bitcoinj.crypto.TransactionSignature) ECKey(org.bitcoinj.core.ECKey) ScriptBuilder(org.bitcoinj.script.ScriptBuilder) TransactionInput(org.bitcoinj.core.TransactionInput) ScriptException(org.bitcoinj.script.ScriptException) AddressFormatException(org.bitcoinj.core.AddressFormatException) P2SH_P2WPKH(com.samourai.sentinel.segwit.P2SH_P2WPKH)

Aggregations

AddressFormatException (org.bitcoinj.core.AddressFormatException)4 ECKey (org.bitcoinj.core.ECKey)4 TransactionInput (org.bitcoinj.core.TransactionInput)4 Script (org.bitcoinj.script.Script)4 ScriptException (org.bitcoinj.script.ScriptException)4 HashMap (java.util.HashMap)3 Transaction (org.bitcoinj.core.Transaction)3 IOException (java.io.IOException)2 DumpedPrivateKey (org.bitcoinj.core.DumpedPrivateKey)2 MnemonicException (org.bitcoinj.crypto.MnemonicException)2 P2SH_P2WPKH (com.samourai.sentinel.segwit.P2SH_P2WPKH)1 TransactionOutput (org.bitcoinj.core.TransactionOutput)1 TransactionWitness (org.bitcoinj.core.TransactionWitness)1 TransactionSignature (org.bitcoinj.crypto.TransactionSignature)1 ScriptBuilder (org.bitcoinj.script.ScriptBuilder)1