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;
}
}
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;
}
}
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;
}
}
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;
}
Aggregations