Search in sources :

Example 1 with PSBT

use of com.samourai.wallet.cahoots.psbt.PSBT in project samourai-wallet-android by Samourai-Wallet.

the class Cahoots method fromJSON.

public void fromJSON(JSONObject cObj) {
    try {
        JSONObject obj = null;
        if (cObj.has("cahoots")) {
            obj = cObj.getJSONObject("cahoots");
        }
        if (obj != null && obj.has("type") && obj.has("step") && obj.has("psbt") && obj.has("ts") && obj.has("id") && obj.has("version") && obj.has("spend_amount")) {
            if (obj.has("params") && obj.getString("params").equals("testnet")) {
                this.params = TestNet3Params.get();
            } else {
                this.params = MainNetParams.get();
            }
            this.version = obj.getInt("version");
            this.ts = obj.getLong("ts");
            this.strID = obj.getString("id");
            this.type = obj.getInt("type");
            this.step = obj.getInt("step");
            this.spendAmount = obj.getLong("spend_amount");
            this.feeAmount = obj.getLong("fee_amount");
            JSONArray _outpoints = obj.getJSONArray("outpoints");
            for (int i = 0; i < _outpoints.length(); i++) {
                JSONObject entry = _outpoints.getJSONObject(i);
                outpoints.put(entry.getString("outpoint"), entry.getLong("value"));
            }
            this.strDestination = obj.getString("dest");
            this.strCollabChange = obj.getString("collabChange");
            // this.strPayNymInit = obj.getString("paynym_init");
            if (obj.has("account")) {
                this.account = obj.getInt("account");
            } else {
                this.account = 0;
            }
            if (obj.has("cpty_account")) {
                this.cptyAccount = obj.getInt("cpty_account");
            } else {
                this.cptyAccount = 0;
            }
            if (obj.has("fingerprint")) {
                fingerprint = Hex.decode(obj.getString("fingerprint"));
            }
            if (obj.has("fingerprint_collab")) {
                fingerprintCollab = Hex.decode(obj.getString("fingerprint_collab"));
            }
            this.psbt = obj.getString("psbt").equals("") ? null : new PSBT(Z85.getInstance().decode(obj.getString("psbt")), params);
            if (this.psbt != null) {
                this.psbt.read();
            }
        }
    } catch (JSONException je) {
        je.printStackTrace();
    } catch (Exception e) {
    // throw new RuntimeException(e);
    }
}
Also used : PSBT(com.samourai.wallet.cahoots.psbt.PSBT) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) JSONException(org.json.JSONException)

Example 2 with PSBT

use of com.samourai.wallet.cahoots.psbt.PSBT in project samourai-wallet-android by Samourai-Wallet.

the class CahootsUtil method doPSBT.

public void doPSBT(final String strPSBT) {
    String msg = null;
    PSBT psbt = new PSBT(strPSBT, SamouraiWallet.getInstance().getCurrentNetworkParams());
    try {
        psbt.read();
        msg = psbt.dump();
    } catch (Exception e) {
        msg = e.getMessage();
    }
    final EditText edPSBT = new EditText(context);
    edPSBT.setSingleLine(false);
    edPSBT.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
    edPSBT.setLines(10);
    edPSBT.setHint(R.string.PSBT);
    edPSBT.setGravity(Gravity.START);
    TextWatcher textWatcher = new TextWatcher() {

        public void afterTextChanged(Editable s) {
            edPSBT.setSelection(0);
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            ;
        }

        public void onTextChanged(CharSequence s, int start, int before, int count) {
            ;
        }
    };
    edPSBT.addTextChangedListener(textWatcher);
    edPSBT.setText(msg);
    AlertDialog.Builder dlg = new AlertDialog.Builder(context).setTitle(R.string.app_name).setMessage(R.string.PSBT).setView(edPSBT).setCancelable(true).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int whichButton) {
            dialog.dismiss();
        }
    });
    if (!((Activity) context).isFinishing()) {
        dlg.show();
    }
}
Also used : PSBT(com.samourai.wallet.cahoots.psbt.PSBT) EditText(android.widget.EditText) AlertDialog(android.app.AlertDialog) DialogInterface(android.content.DialogInterface) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) MnemonicException(org.bitcoinj.crypto.MnemonicException) IOException(java.io.IOException) MyTransactionOutPoint(com.samourai.wallet.send.MyTransactionOutPoint) TransactionOutPoint(org.bitcoinj.core.TransactionOutPoint)

Example 3 with PSBT

use of com.samourai.wallet.cahoots.psbt.PSBT in project samourai-wallet-android by Samourai-Wallet.

the class Stowaway method doStep1.

// 
// receiver
// 
public 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());
        Log.d("Stowaway", "input value:" + input.getValue().longValue());
        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, 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])));
    }
    this.psbt = psbt;
    Log.d("Stowaway", "input value:" + psbt.getTransaction().getInputs().get(0).getValue().longValue());
    this.setStep(1);
    return true;
}
Also used : PSBT(com.samourai.wallet.cahoots.psbt.PSBT) Triple(org.apache.commons.lang3.tuple.Triple) TestNet3Params(org.bitcoinj.params.TestNet3Params) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress)

Example 4 with PSBT

use of com.samourai.wallet.cahoots.psbt.PSBT 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;
}
Also used : PSBT(com.samourai.wallet.cahoots.psbt.PSBT) Triple(org.apache.commons.lang3.tuple.Triple) TestNet3Params(org.bitcoinj.params.TestNet3Params) SegwitAddress(com.samourai.wallet.segwit.SegwitAddress)

Aggregations

PSBT (com.samourai.wallet.cahoots.psbt.PSBT)4 SegwitAddress (com.samourai.wallet.segwit.SegwitAddress)2 Triple (org.apache.commons.lang3.tuple.Triple)2 TestNet3Params (org.bitcoinj.params.TestNet3Params)2 AlertDialog (android.app.AlertDialog)1 DialogInterface (android.content.DialogInterface)1 Editable (android.text.Editable)1 TextWatcher (android.text.TextWatcher)1 EditText (android.widget.EditText)1 MyTransactionOutPoint (com.samourai.wallet.send.MyTransactionOutPoint)1 IOException (java.io.IOException)1 TransactionOutPoint (org.bitcoinj.core.TransactionOutPoint)1 MnemonicException (org.bitcoinj.crypto.MnemonicException)1 JSONArray (org.json.JSONArray)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1