use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class SweepUtil method sweep.
public void sweep(final PrivKeyReader privKeyReader, final int type) {
new Thread(new Runnable() {
@Override
public void run() {
Looper.prepare();
try {
if (privKeyReader == null || privKeyReader.getKey() == null || !privKeyReader.getKey().hasPrivKey()) {
Toast.makeText(context, R.string.cannot_recognize_privkey, Toast.LENGTH_SHORT).show();
return;
}
final String address;
UTXO utxo = null;
if (type == TYPE_P2SH_P2WPKH) {
utxo = utxoP2SH_P2WPKH;
address = addressP2SH_P2WPKH;
} else if (type == TYPE_P2WPKH) {
utxo = utxoP2WPKH;
address = addressP2WPKH;
} else {
addressP2PKH = privKeyReader.getKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
Log.d("SweepUtil", "address derived P2PKH:" + addressP2PKH);
addressP2SH_P2WPKH = new SegwitAddress(privKeyReader.getKey(), SamouraiWallet.getInstance().getCurrentNetworkParams()).getAddressAsString();
Log.d("SweepUtil", "address derived P2SH_P2WPKH:" + addressP2SH_P2WPKH);
addressP2WPKH = new SegwitAddress(privKeyReader.getKey(), SamouraiWallet.getInstance().getCurrentNetworkParams()).getBech32AsString();
Log.d("SweepUtil", "address derived P2WPKH:" + addressP2WPKH);
utxoP2PKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2PKH);
utxoP2SH_P2WPKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2SH_P2WPKH);
utxoP2WPKH = APIFactory.getInstance(context).getUnspentOutputsForSweep(addressP2WPKH);
utxo = utxoP2PKH;
address = addressP2PKH;
}
if (utxo != null) {
long total_value = 0L;
final List<MyTransactionOutPoint> outpoints = utxo.getOutpoints();
for (MyTransactionOutPoint outpoint : outpoints) {
total_value += outpoint.getValue().longValue();
}
if (FeeUtil.getInstance().getSuggestedFee().getDefaultPerKB().longValue() <= 1000L) {
SuggestedFee suggestedFee = new SuggestedFee();
suggestedFee.setDefaultPerKB(BigInteger.valueOf(1100L));
Log.d("SweepUtil", "adjusted fee:" + suggestedFee.getDefaultPerKB().longValue());
FeeUtil.getInstance().setSuggestedFee(suggestedFee);
}
Log.d("SweepUtil", "outpoints:" + outpoints.size());
Log.d("SweepUtil", "type:" + type);
final BigInteger fee;
if (type == TYPE_P2SH_P2WPKH) {
fee = FeeUtil.getInstance().estimatedFeeSegwit(0, outpoints.size(), 1);
} else if (type == TYPE_P2WPKH) {
fee = FeeUtil.getInstance().estimatedFeeSegwit(0, 0, outpoints.size(), 1);
} else {
fee = FeeUtil.getInstance().estimatedFee(outpoints.size(), 1);
}
final long amount = total_value - fee.longValue();
// Log.d("BalanceActivity", "Total value:" + total_value);
// Log.d("BalanceActivity", "Amount:" + amount);
Log.d("SweepUtil", "Fee:" + fee.toString());
String message = "Sweep " + Coin.valueOf(amount).toPlainString() + " from " + address + " (fee:" + Coin.valueOf(fee.longValue()).toPlainString() + ")?";
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(R.string.app_name);
builder.setMessage(message);
builder.setCancelable(false);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int whichButton) {
final ProgressDialog progress = new ProgressDialog(context);
progress.setCancelable(false);
progress.setTitle(R.string.app_name);
progress.setMessage(context.getString(R.string.please_wait_sending));
progress.show();
String receive_address = null;
if (PrefsUtil.getInstance(context).getValue(PrefsUtil.USE_SEGWIT, true) == true) {
receive_address = AddressFactory.getInstance(context).getBIP84(AddressFactory.RECEIVE_CHAIN).getBech32AsString();
} else {
receive_address = AddressFactory.getInstance(context).get(AddressFactory.RECEIVE_CHAIN).getAddressString();
}
final HashMap<String, BigInteger> receivers = new HashMap<String, BigInteger>();
receivers.put(receive_address, BigInteger.valueOf(amount));
org.bitcoinj.core.Transaction tx = SendFactory.getInstance(context).makeTransaction(0, outpoints, receivers);
tx = SendFactory.getInstance(context).signTransactionForSweep(tx, privKeyReader);
Log.d("SweepUtil", "tx size:" + tx.bitcoinSerialize().length);
final String hexTx = new String(Hex.encode(tx.bitcoinSerialize()));
// Log.d("BalanceActivity", hexTx);
String response = null;
try {
if (PrefsUtil.getInstance(context).getValue(PrefsUtil.USE_TRUSTED_NODE, false) == true) {
if (TrustedNodeUtil.getInstance().isSet()) {
response = PushTx.getInstance(context).trustedNode(hexTx);
JSONObject jsonObject = new org.json.JSONObject(response);
if (jsonObject.has("result")) {
if (jsonObject.getString("result").matches("^[A-Za-z0-9]{64}$")) {
Toast.makeText(context, R.string.tx_sent, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, R.string.trusted_node_tx_error, Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(context, R.string.trusted_node_not_valid, Toast.LENGTH_SHORT).show();
}
} else {
response = PushTx.getInstance(context).samourai(hexTx);
if (response != null) {
JSONObject jsonObject = new org.json.JSONObject(response);
if (jsonObject.has("status")) {
if (jsonObject.getString("status").equals("ok")) {
Toast.makeText(context, R.string.tx_sent, Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(context, R.string.pushtx_returns_null, Toast.LENGTH_SHORT).show();
}
}
} catch (JSONException je) {
Toast.makeText(context, "pushTx:" + je.getMessage(), Toast.LENGTH_SHORT).show();
}
if (progress != null && progress.isShowing()) {
progress.dismiss();
}
}
});
builder.setNegativeButton(R.string.no, new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int whichButton) {
;
}
});
AlertDialog alert = builder.create();
alert.show();
} else if (type == TYPE_P2SH_P2WPKH) {
sweep(privKeyReader, TYPE_P2WPKH);
} else if (type == TYPE_P2PKH) {
sweep(privKeyReader, TYPE_P2SH_P2WPKH);
} else if (type == TYPE_P2WPKH) {
;
} else {
;
}
} catch (Exception e) {
Toast.makeText(context, R.string.cannot_sweep_privkey, Toast.LENGTH_SHORT).show();
}
Looper.loop();
}
}).start();
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class BatchSendActivity method processPCode.
private void processPCode(String pcode, String meta) {
if (FormatsUtil.getInstance().isValidPaymentCode(pcode)) {
if (BIP47Meta.getInstance().getOutgoingStatus(pcode) == BIP47Meta.STATUS_SENT_CFM) {
try {
PaymentCode _pcode = new PaymentCode(pcode);
PaymentAddress paymentAddress = BIP47Util.getInstance(BatchSendActivity.this).getSendAddress(_pcode, BIP47Meta.getInstance().getOutgoingIdx(pcode));
if (BIP47Meta.getInstance().getSegwit(pcode)) {
SegwitAddress segwitAddress = new SegwitAddress(paymentAddress.getSendECKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
strDestinationBTCAddress = segwitAddress.getBech32AsString();
} else {
strDestinationBTCAddress = paymentAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
}
strPCode = _pcode.toString();
edAddress.setText(BIP47Meta.getInstance().getDisplayLabel(strPCode));
edAddress.setEnabled(false);
} catch (Exception e) {
Toast.makeText(BatchSendActivity.this, R.string.error_payment_code, Toast.LENGTH_SHORT).show();
}
} else {
if (meta != null && meta.startsWith("?") && meta.length() > 1) {
meta = meta.substring(1);
}
Intent intent = new Intent(BatchSendActivity.this, PayNymDetailsActivity.class);
intent.putExtra("pcode", pcode);
if (meta != null && meta.length() > 0) {
if (meta.startsWith("?") && meta.length() > 1) {
meta = meta.substring(1);
if (meta.length() > 0) {
String _meta = null;
Map<String, String> map = new HashMap<String, String>();
meta.length();
try {
_meta = URLDecoder.decode(meta, "UTF-8");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
map = Splitter.on('&').trimResults().withKeyValueSeparator("=").split(_meta);
intent.putExtra("label", map.containsKey("title") ? map.get("title").trim() : "");
}
}
}
startActivity(intent);
}
} else {
Toast.makeText(BatchSendActivity.this, R.string.invalid_payment_code, Toast.LENGTH_SHORT).show();
}
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class PayNymCalcActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paynym_calc);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
edPayNym = (EditText) findViewById(R.id.paynym);
edIndex = (EditText) findViewById(R.id.index);
edPayNym.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// final int DRAWABLE_LEFT = 0;
// final int DRAWABLE_TOP = 1;
final int DRAWABLE_RIGHT = 2;
if (event.getAction() == MotionEvent.ACTION_UP && event.getRawX() >= (edPayNym.getRight() - edPayNym.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
final List<String> entries = new ArrayList<String>();
entries.addAll(BIP47Meta.getInstance().getSortedByLabels(true));
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(PayNymCalcActivity.this, android.R.layout.select_dialog_singlechoice);
for (int i = 0; i < entries.size(); i++) {
arrayAdapter.add(BIP47Meta.getInstance().getDisplayLabel(entries.get(i)));
}
AlertDialog.Builder dlg = new AlertDialog.Builder(PayNymCalcActivity.this);
dlg.setIcon(R.drawable.ic_launcher);
dlg.setTitle(R.string.app_name);
dlg.setAdapter(arrayAdapter, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
edPayNym.setText(entries.get(which));
}
});
dlg.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
dlg.show();
return true;
}
return false;
}
});
btOK = (Button) findViewById(R.id.ok);
btOK.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String strPayNym = edPayNym.getText().toString();
String strIndex = edIndex.getText().toString();
PaymentCode pcode = new PaymentCode(strPayNym);
if (pcode == null) {
Toast.makeText(PayNymCalcActivity.this, R.string.invalid_payment_code, Toast.LENGTH_SHORT).show();
return;
}
if (strIndex == null || strIndex.length() < 1) {
Toast.makeText(PayNymCalcActivity.this, R.string.invalid_index, Toast.LENGTH_SHORT).show();
return;
}
try {
int index = Integer.parseInt(strIndex);
String message = strPayNym;
final ECKey receiveECKey;
final SegwitAddress receiveSegwit;
PaymentAddress receiveAddress = BIP47Util.getInstance(PayNymCalcActivity.this).getReceiveAddress(new PaymentCode(strPayNym), index);
PaymentAddress sendAddress = BIP47Util.getInstance(PayNymCalcActivity.this).getSendAddress(new PaymentCode(strPayNym), index);
receiveECKey = receiveAddress.getReceiveECKey();
ECKey sendECKey = sendAddress.getSendECKey();
receiveSegwit = new SegwitAddress(receiveECKey, SamouraiWallet.getInstance().getCurrentNetworkParams());
SegwitAddress sendSegwit = new SegwitAddress(sendECKey, SamouraiWallet.getInstance().getCurrentNetworkParams());
message += "\n";
message += index + ":";
message += "\n";
message += "\n";
message += PayNymCalcActivity.this.getText(R.string.receive_addresses).toString() + ":";
message += "\n";
message += receiveECKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
message += "\n";
message += receiveSegwit.getAddressAsString();
message += "\n";
message += receiveSegwit.getBech32AsString();
message += "\n";
message += PayNymCalcActivity.this.getText(R.string.send_addresses).toString() + ":";
message += "\n";
message += sendECKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
message += "\n";
message += sendSegwit.getAddressAsString();
message += "\n";
message += sendSegwit.getBech32AsString();
message += "\n";
final TextView tvText = new TextView(getApplicationContext());
tvText.setTextSize(12);
tvText.setText(message);
tvText.setTextIsSelectable(true);
tvText.setPadding(40, 10, 40, 10);
tvText.setTextColor(0xffffffff);
AlertDialog.Builder dlg = new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(tvText).setCancelable(true).setPositiveButton(R.string.display_receive_redeem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String redeemScript = org.spongycastle.util.encoders.Hex.toHexString(receiveSegwit.segWitRedeemScript().getProgram());
TextView showText = new TextView(PayNymCalcActivity.this);
showText.setText(redeemScript);
showText.setTextIsSelectable(true);
showText.setPadding(40, 10, 40, 10);
showText.setTextSize(18.0f);
new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(showText).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).show();
}
}).setNeutralButton(R.string.close, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.dismiss();
}
}).setNegativeButton(R.string.display_receive_privkey, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String strPrivKey = receiveECKey.getPrivateKeyAsWiF(SamouraiWallet.getInstance().getCurrentNetworkParams());
ImageView showQR = new ImageView(PayNymCalcActivity.this);
Bitmap bitmap = null;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(strPrivKey, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), 500);
try {
bitmap = qrCodeEncoder.encodeAsBitmap();
} catch (WriterException e) {
e.printStackTrace();
}
showQR.setImageBitmap(bitmap);
TextView showText = new TextView(PayNymCalcActivity.this);
showText.setText(strPrivKey);
showText.setTextIsSelectable(true);
showText.setPadding(40, 10, 40, 10);
showText.setTextSize(18.0f);
LinearLayout privkeyLayout = new LinearLayout(PayNymCalcActivity.this);
privkeyLayout.setOrientation(LinearLayout.VERTICAL);
privkeyLayout.addView(showQR);
privkeyLayout.addView(showText);
new AlertDialog.Builder(PayNymCalcActivity.this).setTitle(R.string.app_name).setView(privkeyLayout).setCancelable(false).setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
;
}
}).show();
}
});
if (!isFinishing()) {
dlg.show();
}
} catch (Exception e) {
Toast.makeText(PayNymCalcActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
btCancel = (Button) findViewById(R.id.cancel);
btCancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class UTXODetailsActivity method redeem.
private void redeem() {
ECKey ecKey = SendFactory.getPrivKey(addr, account);
SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
if (ecKey != null && segwitAddress != null) {
String redeemScript = Hex.toHexString(segwitAddress.segWitRedeemScript().getProgram());
TextView showText = new TextView(this);
showText.setText(redeemScript);
showText.setTextIsSelectable(true);
showText.setPadding(40, 10, 40, 10);
showText.setTextSize(18.0f);
new AlertDialog.Builder(this).setTitle(R.string.app_name).setView(showText).setCancelable(false).setPositiveButton(R.string.ok, (dialog, whichButton) -> {
}).show();
}
}
use of com.samourai.wallet.segwit.SegwitAddress in project samourai-wallet-android by Samourai-Wallet.
the class AddressFactory method getBIP49.
public SegwitAddress getBIP49(int chain) {
int idx = 0;
HD_Address addr = null;
SegwitAddress p2shp2wpkh = null;
// try {
HD_Wallet hdw = BIP49Util.getInstance(context).getWallet();
if (hdw != null) {
idx = BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddrIdx();
addr = BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).getAddressAt(idx);
p2shp2wpkh = new SegwitAddress(addr.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
if (chain == RECEIVE_CHAIN && canIncBIP49ReceiveAddress(idx)) {
BIP49Util.getInstance(context).getWallet().getAccount(SamouraiWallet.SAMOURAI_ACCOUNT).getChain(chain).incAddrIdx();
// PayloadUtil.getInstance(context).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(context).getGUID() + AccessFactory.getInstance(context).getPIN()));
}
}
return p2shp2wpkh;
}
Aggregations