use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class STONEWALLx2 method doStep2.
//
// sender
//
protected boolean doStep2(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
Transaction transaction = psbt.getTransaction();
Log.d("STONEWALLx2", "step2 tx:" + transaction.toString());
Log.d("STONEWALLx2", "step2 tx:" + Hex.toHexString(transaction.bitcoinSerialize()));
for (_TransactionOutPoint outpoint : inputs.keySet()) {
Log.d("STONEWALLx2", "outpoint value:" + outpoint.getValue().longValue());
TransactionInput input = new TransactionInput(params, null, new byte[0], outpoint, outpoint.getValue());
transaction.addInput(input);
outpoints.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getValue().longValue());
}
for (_TransactionOutput output : outputs.keySet()) {
transaction.addOutput(output);
}
TransactionOutput _output = null;
if (FormatsUtil.getInstance().isValidBitcoinAddress(strDestination)) {
if (FormatsUtil.getInstance().isValidBech32(strDestination)) {
Pair<Byte, byte[]> pair = Bech32Segwit.decode(params instanceof TestNet3Params ? "tb" : "bc", strDestination);
byte[] scriptPubKey = Bech32Segwit.getScriptPubkey(pair.getLeft(), pair.getRight());
_output = new TransactionOutput(params, null, Coin.valueOf(spendAmount), scriptPubKey);
} else {
Script toOutputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), strDestination));
_output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), toOutputScript.getProgram());
}
transaction.addOutput(_output);
} else {
return false;
}
for (_TransactionOutPoint outpoint : inputs.keySet()) {
Triple triple = inputs.get(outpoint);
// input type 1
SegwitAddress segwitAddress = new SegwitAddress((byte[]) triple.getLeft(), params);
psbt.addInput(PSBT.PSBT_IN_WITNESS_UTXO, null, PSBT.writeSegwitInputUTXO(outpoint.getValue().longValue(), segwitAddress.segWitRedeemScript().getProgram()));
// input type 6
String[] s = ((String) triple.getRight()).split("/");
psbt.addInput(PSBT.PSBT_IN_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
for (_TransactionOutput output : outputs.keySet()) {
Triple triple = outputs.get(output);
// output type 2
String[] s = ((String) triple.getRight()).split("/");
psbt.addOutput(PSBT.PSBT_OUT_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
psbt.setTransaction(transaction);
this.setStep(2);
return true;
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class STONEWALLx2 method doStep1.
//
// counterparty
//
protected boolean doStep1(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
if (this.getStep() != 0 || this.getSpendAmount() == 0L) {
return false;
}
if (this.getType() == Cahoots.CAHOOTS_STONEWALLx2 && outputs == null) {
return false;
}
Transaction transaction = new Transaction(params);
for (_TransactionOutPoint outpoint : inputs.keySet()) {
TransactionInput input = new TransactionInput(params, null, new byte[0], outpoint, outpoint.getValue());
transaction.addInput(input);
outpoints.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getValue().longValue());
}
for (_TransactionOutput output : outputs.keySet()) {
transaction.addOutput(output);
}
PSBT psbt = new PSBT(transaction);
for (_TransactionOutPoint outpoint : inputs.keySet()) {
Triple triple = inputs.get(outpoint);
// input type 1
SegwitAddress segwitAddress = new SegwitAddress((byte[]) triple.getLeft(), params);
psbt.addInput(PSBT.PSBT_IN_WITNESS_UTXO, null, PSBT.writeSegwitInputUTXO(outpoint.getValue().longValue(), segwitAddress.segWitRedeemScript().getProgram()));
// input type 6
String[] s = ((String) triple.getRight()).split("/");
psbt.addInput(PSBT.PSBT_IN_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, cptyAccount, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
for (_TransactionOutput output : outputs.keySet()) {
Triple triple = outputs.get(output);
// output type 2
String[] s = ((String) triple.getRight()).split("/");
psbt.addOutput(PSBT.PSBT_OUT_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, cptyAccount, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
this.psbt = psbt;
// Log.d("STONEWALLx2", "input value:" + psbt.getTransaction().getInputs().get(0).getValue().longValue());
this.setStep(1);
return true;
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class Stowaway method doStep2.
//
// sender
//
public boolean doStep2(HashMap<_TransactionOutPoint, Triple<byte[], byte[], String>> inputs, HashMap<_TransactionOutput, Triple<byte[], byte[], String>> outputs) throws Exception {
Transaction transaction = psbt.getTransaction();
Log.d("Stowaway", "step2 tx:" + transaction.toString());
Log.d("Stowaway", "step2 tx:" + Hex.toHexString(transaction.bitcoinSerialize()));
// tx: modify spend output
long contributedAmount = 0L;
/*
for(TransactionInput input : transaction.getInputs()) {
// Log.d("Stowaway", input.getOutpoint().toString());
contributedAmount += input.getOutpoint().getValue().longValue();
}
*/
for (String outpoint : outpoints.keySet()) {
contributedAmount += outpoints.get(outpoint);
}
long outputAmount = transaction.getOutput(0).getValue().longValue();
TransactionOutput spendOutput = transaction.getOutput(0);
spendOutput.setValue(Coin.valueOf(outputAmount + contributedAmount));
transaction.clearOutputs();
transaction.addOutput(spendOutput);
for (_TransactionOutPoint outpoint : inputs.keySet()) {
Log.d("Stowaway", "outpoint value:" + outpoint.getValue().longValue());
TransactionInput input = new TransactionInput(params, null, new byte[0], outpoint, outpoint.getValue());
transaction.addInput(input);
outpoints.put(outpoint.getTxHash().toString() + "-" + outpoint.getTxOutputN(), outpoint.getValue().longValue());
}
for (_TransactionOutput output : outputs.keySet()) {
transaction.addOutput(output);
}
// psbt: modify spend output
List<PSBTEntry> updatedEntries = new ArrayList<PSBTEntry>();
for (PSBTEntry entry : psbt.getPsbtInputs()) {
if (entry.getKeyType()[0] == PSBT.PSBT_IN_WITNESS_UTXO) {
byte[] data = entry.getData();
byte[] scriptPubKey = new byte[data.length - Long.BYTES];
System.arraycopy(data, Long.BYTES, scriptPubKey, 0, scriptPubKey.length);
entry.setData(PSBT.writeSegwitInputUTXO(outputAmount + contributedAmount, scriptPubKey));
}
updatedEntries.add(entry);
}
psbt.setPsbtInputs(updatedEntries);
for (_TransactionOutPoint outpoint : inputs.keySet()) {
Triple triple = inputs.get(outpoint);
// input type 1
SegwitAddress segwitAddress = new SegwitAddress((byte[]) triple.getLeft(), params);
psbt.addInput(PSBT.PSBT_IN_WITNESS_UTXO, null, PSBT.writeSegwitInputUTXO(outpoint.getValue().longValue(), segwitAddress.segWitRedeemScript().getProgram()));
// input type 6
String[] s = ((String) triple.getRight()).split("/");
psbt.addInput(PSBT.PSBT_IN_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
for (_TransactionOutput output : outputs.keySet()) {
Triple triple = outputs.get(output);
// output type 2
String[] s = ((String) triple.getRight()).split("/");
psbt.addOutput(PSBT.PSBT_OUT_BIP32_DERIVATION, (byte[]) triple.getLeft(), PSBT.writeBIP32Derivation((byte[]) triple.getMiddle(), 84, params instanceof TestNet3Params ? 1 : 0, account, Integer.valueOf(s[1]), Integer.valueOf(s[2])));
}
psbt.setTransaction(transaction);
this.setStep(2);
return true;
}
use of com.samourai.wallet.segwit.SegwitAddress 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);
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class WhirlpoolWalletTest method testTx0.
@Test
public void testTx0() throws Exception {
Collection<UnspentOutputWithKey> spendFroms = new LinkedList<>();
ECKey ecKey = bip84w.getAccountAt(0).getChain(0).getAddressAt(61).getECKey();
UnspentResponse.UnspentOutput unspentOutput = newUnspentOutput("cc588cdcb368f894a41c372d1f905770b61ecb3fb8e5e01a97e7cedbf5e324ae", 1, 500000000);
unspentOutput.addr = new SegwitAddress(ecKey, networkParameters).getBech32AsString();
spendFroms.add(new UnspentOutputWithKey(unspentOutput, ecKey.getPrivKeyBytes()));
Pool pool = whirlpoolWallet.findPoolById("0.01btc");
Tx0Config tx0Config = whirlpoolWallet.getTx0Config().setMaxOutputs(1);
Tx0Preview tx0Preview = whirlpoolWallet.tx0Preview(pool, spendFroms, tx0Config, Tx0FeeTarget.BLOCKS_2);
Tx0 tx0 = whirlpoolWallet.tx0(pool, spendFroms, tx0Config, Tx0FeeTarget.BLOCKS_2);
Assert.assertEquals("dc398c99cf9ce18123ea916d69bb99da44a3979a625eeaac5e17837f879a8874", tx0.getTx().getHashAsString());
Assert.assertEquals("01000000000101ae24e3f5dbcee7971ae0e5b83fcb1eb67057901f2d371ca494f868b3dc8c58cc0100000000ffffffff040000000000000000426a408a9eb379a44ff4d4579118c64b64bbd327cd95ba826ac68f334155fd9ca4e3acd64acdfd75dd7c3cc5bc34d31af6c6e68b4db37eac62b574890f6cfc7b904d9950c300000000000016001441021632871b0f1cf61a7ac7b6a0187e88628291b44b0f00000000001600147e4a4628dd8fbd638681a728e39f7d92ada04070e954bd1d00000000160014df3a4bc83635917ad18621f3ba78cef6469c5f5902483045022100c48f02762ab9877533b5c7b0bc729479ce7809596b89cb9f62b740ea3350068f02205ef46ca67df39d35f940e33223c5ddd56669d953b6ef4948e355c1f3430f32e10121032e46baef8bcde0c3a19cadb378197fa31d69adb21535de3f84de699a1cf88b4500000000", new String(Hex.encode(tx0.getTx().bitcoinSerialize())));
}
Aggregations