use of org.bitcoinj.core.TransactionWitness 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;
}
use of org.bitcoinj.core.TransactionWitness in project samourai-wallet-android by Samourai-Wallet.
the class RBFTask method signTx.
private Transaction signTx(Transaction tx) {
HashMap<String, ECKey> keyBag = new HashMap<String, ECKey>();
HashMap<String, ECKey> keyBag49 = new HashMap<String, ECKey>();
HashMap<String, ECKey> keyBag84 = new HashMap<String, ECKey>();
HashMap<String, String> keys = rbf.getKeyBag();
for (String outpoint : keys.keySet()) {
ECKey ecKey = null;
String[] s = keys.get(outpoint).split("/");
Log.i("RBF", "path length:" + s.length);
if (s.length == 4) {
if (s[3].equals("84")) {
HD_Address addr = BIP84Util.getInstance(activity).getWallet().getAccount(0).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
ecKey = addr.getECKey();
} else {
HD_Address addr = BIP49Util.getInstance(activity).getWallet().getAccount(0).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
ecKey = addr.getECKey();
}
} else if (s.length == 3) {
HD_Address hd_address = AddressFactory.getInstance(activity).get(0, 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 if (s.length == 2) {
try {
PaymentAddress address = BIP47Util.getInstance(activity).getReceiveAddress(new PaymentCode(s[0]), Integer.parseInt(s[1]));
ecKey = address.getReceiveECKey();
} catch (Exception e) {
;
}
} else {
;
}
Log.i("RBF", "outpoint:" + outpoint);
Log.i("RBF", "path:" + keys.get(outpoint));
if (ecKey != null) {
if (s.length == 4) {
if (s[3].equals("84")) {
keyBag84.put(outpoint, ecKey);
} else {
keyBag49.put(outpoint, ecKey);
}
} else {
keyBag.put(outpoint, ecKey);
}
} else {
throw new RuntimeException("ECKey error: cannot process private key");
// Log.i("ECKey error", "cannot process private key");
}
}
List<TransactionInput> inputs = tx.getInputs();
for (int i = 0; i < inputs.size(); i++) {
ECKey ecKey = null;
String address = null;
if (inputs.get(i).getValue() != null || keyBag49.containsKey(inputs.get(i).getOutpoint().toString()) || keyBag84.containsKey(inputs.get(i).getOutpoint().toString())) {
if (keyBag84.containsKey(inputs.get(i).getOutpoint().toString())) {
ecKey = keyBag84.get(inputs.get(i).getOutpoint().toString());
SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
address = segwitAddress.getBech32AsString();
} else {
ecKey = keyBag49.get(inputs.get(i).getOutpoint().toString());
SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
address = segwitAddress.getAddressAsString();
}
} else {
ecKey = keyBag.get(inputs.get(i).getOutpoint().toString());
address = ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
}
Log.d("RBF", "pubKey:" + Hex.toHexString(ecKey.getPubKey()));
Log.d("RBF", "address:" + address);
if (inputs.get(i).getValue() != null || keyBag49.containsKey(inputs.get(i).getOutpoint().toString()) || keyBag84.containsKey(inputs.get(i).getOutpoint().toString())) {
final SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
Script scriptPubKey = segwitAddress.segWitOutputScript();
final Script redeemScript = segwitAddress.segWitRedeemScript();
System.out.println("redeem script:" + Hex.toHexString(redeemScript.getProgram()));
final Script scriptCode = redeemScript.scriptCode();
System.out.println("script code:" + Hex.toHexString(scriptCode.getProgram()));
TransactionSignature sig = tx.calculateWitnessSignature(i, ecKey, scriptCode, Coin.valueOf(input_values.get(inputs.get(i).getOutpoint().toString())), Transaction.SigHash.ALL, false);
final TransactionWitness witness = new TransactionWitness(2);
witness.setPush(0, sig.encodeToBitcoin());
witness.setPush(1, ecKey.getPubKey());
tx.setWitness(i, witness);
if (!FormatsUtil.getInstance().isValidBech32(address) && Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
final ScriptBuilder sigScript = new ScriptBuilder();
sigScript.data(redeemScript.getProgram());
tx.getInput(i).setScriptSig(sigScript.build());
tx.getInput(i).getScriptSig().correctlySpends(tx, i, scriptPubKey, Coin.valueOf(input_values.get(inputs.get(i).getOutpoint().toString())), Script.ALL_VERIFY_FLAGS);
}
} else {
Log.i("RBF", "sign outpoint:" + inputs.get(i).getOutpoint().toString());
Log.i("RBF", "ECKey address from keyBag:" + ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
Log.i("RBF", "script:" + ScriptBuilder.createOutputScript(ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams())));
Log.i("RBF", "script:" + Hex.toHexString(ScriptBuilder.createOutputScript(ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams())).getProgram()));
TransactionSignature sig = tx.calculateSignature(i, ecKey, ScriptBuilder.createOutputScript(ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams())).getProgram(), Transaction.SigHash.ALL, false);
tx.getInput(i).setScriptSig(ScriptBuilder.createInputScript(sig, ecKey));
}
}
return tx;
}
use of org.bitcoinj.core.TransactionWitness in project samourai-wallet-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(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
}
if (FormatsUtil.getInstance().isValidBech32(address) || Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
final SegwitAddress segwitAddress = new SegwitAddress(key.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
// 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(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
final Script redeemScript = segwitAddress.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(SamouraiWallet.getInstance().getCurrentNetworkParams(), 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;
}
use of org.bitcoinj.core.TransactionWitness in project samourai-wallet-android by Samourai-Wallet.
the class RicochetMeta method getHopTx.
private Transaction getHopTx(String prevTxHash, int prevTxN, int prevIndex, long prevSpendAmount, long spendAmount, String destination, Pair<String, Long> samouraiFeePair, long nTimeLock) {
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 {
Script outputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), destination));
output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), outputScript.getProgram());
}
HD_Address address = BIP84Util.getInstance(context).getWallet().getAccountAt(RICOCHET_ACCOUNT).getChain(AddressFactory.RECEIVE_CHAIN).getAddressAt(prevIndex);
ECKey ecKey = address.getECKey();
SegwitAddress p2wpkh = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
Script redeemScript = p2wpkh.segWitRedeemScript();
Transaction tx = new Transaction(SamouraiWallet.getInstance().getCurrentNetworkParams());
if (nTimeLock > 0L) {
tx.setLockTime(nTimeLock);
}
tx.addOutput(output);
if (samouraiFeePair != null) {
byte[] bScriptPubKey = null;
try {
Pair<Byte, byte[]> pair = Bech32Segwit.decode(SamouraiWallet.getInstance().isTestNet() ? "tb" : "bc", samouraiFeePair.getLeft());
bScriptPubKey = Bech32Segwit.getScriptPubkey(pair.getLeft(), pair.getRight());
} catch (Exception e) {
return null;
}
TransactionOutput _output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(samouraiFeePair.getRight()), bScriptPubKey);
tx.addOutput(_output);
}
// Log.d("RicochetMeta", "spending from:" + p2wpkh.getBech32AsString());
// Log.d("RicochetMeta", "pubkey:" + Hex.toHexString(ecKey.getPubKey()));
Sha256Hash txHash = Sha256Hash.wrap(prevTxHash);
TransactionOutPoint outPoint = new TransactionOutPoint(SamouraiWallet.getInstance().getCurrentNetworkParams(), prevTxN, txHash, Coin.valueOf(prevSpendAmount));
TransactionInput txInput = new TransactionInput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, new byte[] {}, outPoint, Coin.valueOf(prevSpendAmount));
if (PrefsUtil.getInstance(context).getValue(PrefsUtil.RBF_OPT_IN, false) == true) {
txInput.setSequenceNumber(SamouraiWallet.RBF_SEQUENCE_VAL.longValue());
}
tx.addInput(txInput);
TransactionSignature sig = tx.calculateWitnessSignature(0, ecKey, redeemScript.scriptCode(), Coin.valueOf(prevSpendAmount), Transaction.SigHash.ALL, false);
final TransactionWitness witness = new TransactionWitness(2);
witness.setPush(0, sig.encodeToBitcoin());
witness.setPush(1, ecKey.getPubKey());
tx.setWitness(0, witness);
assert (0 == tx.getInput(0).getScriptBytes().length);
// Log.d("RicochetMeta", "script sig length:" + tx.getInput(0).getScriptBytes().length);
tx.verify();
return tx;
}
use of org.bitcoinj.core.TransactionWitness in project samourai-wallet-android by Samourai-Wallet.
the class AndroidTx0Service method signTx0.
@Override
protected void signTx0(Transaction tx, Collection<UnspentOutputWithKey> inputs, NetworkParameters params) {
int idx = 0;
for (UnspentOutputWithKey input : inputs) {
String address = input.addr;
ECKey spendFromKey = ECKey.fromPrivate(input.getKey());
// sign input
if (FormatsUtil.getInstance().isValidBech32(address) || Address.fromBase58(params, address).isP2SHAddress()) {
SegwitAddress segwitAddress = new SegwitAddress(spendFromKey.getPubKey(), params);
final Script redeemScript = segwitAddress.segWitRedeemScript();
final Script scriptCode = redeemScript.scriptCode();
TransactionSignature sig = tx.calculateWitnessSignature(idx, spendFromKey, scriptCode, Coin.valueOf(input.value), Transaction.SigHash.ALL, false);
final TransactionWitness witness = new TransactionWitness(2);
witness.setPush(0, sig.encodeToBitcoin());
witness.setPush(1, spendFromKey.getPubKey());
tx.setWitness(idx, witness);
if (!FormatsUtil.getInstance().isValidBech32(address) && Address.fromBase58(params, address).isP2SHAddress()) {
final ScriptBuilder sigScript = new ScriptBuilder();
sigScript.data(redeemScript.getProgram());
tx.getInput(idx).setScriptSig(sigScript.build());
// tx.getInput(idx).getScriptSig().correctlySpends(tx, idx, new Script(Hex.decode(input.script)), Coin.valueOf(input.value), Script.ALL_VERIFY_FLAGS);
}
} else {
TransactionSignature sig = tx.calculateSignature(idx, spendFromKey, new Script(Hex.decode(input.script)), Transaction.SigHash.ALL, false);
tx.getInput(idx).setScriptSig(ScriptBuilder.createInputScript(sig, spendFromKey));
}
idx++;
}
super.signTx0(tx, inputs, params);
}
Aggregations