use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.
the class APIFactory method lockXPUB.
public synchronized JSONObject lockXPUB(String xpub, boolean bip49) {
String _url = SamouraiWallet.getInstance().isTestNet() ? WebUtil.SAMOURAI_API2_TESTNET : WebUtil.SAMOURAI_API2;
JSONObject jsonObject = null;
try {
String response = null;
ECKey ecKey = null;
if (AddressFactory.getInstance(context).xpub2account().get(xpub) != null || xpub.equals(BIP49Util.getInstance(context).getWallet().getAccount(0).ypubstr())) {
HD_Address addr = null;
if (bip49) {
addr = BIP49Util.getInstance(context).getWallet().getAccountAt(0).getChange().getAddressAt(0);
} else {
addr = HD_WalletFactory.getInstance(context).get().getAccount(0).getChain(AddressFactory.CHANGE_CHAIN).getAddressAt(0);
}
ecKey = addr.getECKey();
if (ecKey != null && ecKey.hasPrivKey()) {
String sig = ecKey.signMessage("lock");
String address = null;
if (bip49) {
SegwitAddress segwitAddress = new SegwitAddress(ecKey.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
address = segwitAddress.getAddressAsString();
} else {
address = ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString();
}
if (!TorUtil.getInstance(context).statusFromBroadcast()) {
StringBuilder args = new StringBuilder();
args.append("address=");
args.append(address);
args.append("&signature=");
args.append(Uri.encode(sig));
args.append("&message=");
args.append("lock");
// Log.i("APIFactory", "lock XPUB:" + args.toString());
response = WebUtil.getInstance(context).postURL(_url + "xpub/" + xpub + "/lock/", args.toString());
// Log.i("APIFactory", "lock XPUB response:" + response);
} else {
HashMap<String, String> args = new HashMap<String, String>();
args.put("address", address);
args.put("signature", Uri.encode(sig));
args.put("message", "lock");
// Log.i("APIFactory", "lock XPUB:" + args.toString());
response = WebUtil.getInstance(context).tor_postURL(_url + "xpub" + xpub + "/lock/", args);
// Log.i("APIFactory", "lock XPUB response:" + response);
}
try {
jsonObject = new JSONObject(response);
if (jsonObject.has("status") && jsonObject.getString("status").equals("ok")) {
if (bip49) {
PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB49LOCK, true);
} else {
PrefsUtil.getInstance(context).setValue(PrefsUtil.XPUB44LOCK, true);
}
}
} catch (JSONException je) {
je.printStackTrace();
jsonObject = null;
}
}
}
} catch (Exception e) {
jsonObject = null;
e.printStackTrace();
}
return jsonObject;
}
use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.
the class RicochetMeta method getHopTx.
private Transaction getHopTx(String prevTxHash, int prevTxN, String scriptPubKey, int prevIndex, long spendAmount, String destination) {
Transaction tx = null;
HD_Address address = null;
try {
address = HD_WalletFactory.getInstance(context).get().getAccountAt(RICOCHET_ACCOUNT).getChain(0).getAddressAt(prevIndex);
ECKey ecKey = address.getECKey();
// Log.d("RicochetMeta", "getHopTx address:" + ecKey.toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
byte[] hashBytes = Hex.decode(prevTxHash);
Sha256Hash txHash = new Sha256Hash(hashBytes);
TransactionOutPoint outpoint = new TransactionOutPoint(SamouraiWallet.getInstance().getCurrentNetworkParams(), prevTxN, txHash);
TransactionInput input = new TransactionInput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Hex.decode(scriptPubKey), outpoint);
Script outputScript = null;
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 {
outputScript = ScriptBuilder.createOutputScript(org.bitcoinj.core.Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), destination));
output = new TransactionOutput(SamouraiWallet.getInstance().getCurrentNetworkParams(), null, Coin.valueOf(spendAmount), outputScript.getProgram());
}
tx = new Transaction(SamouraiWallet.getInstance().getCurrentNetworkParams());
tx.addInput(input);
tx.addOutput(output);
TransactionSignature sig = tx.calculateSignature(0, ecKey, Hex.decode(scriptPubKey), Transaction.SigHash.ALL, false);
tx.getInput(0).setScriptSig(ScriptBuilder.createInputScript(sig, ecKey));
} catch (IOException ioe) {
return null;
} catch (MnemonicException.MnemonicLengthException mle) {
return null;
}
return tx;
}
use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.
the class SendFactory method getPrivKey.
public static ECKey getPrivKey(String address) {
ECKey ecKey = null;
try {
String path = APIFactory.getInstance(context).getUnspentPaths().get(address);
// Log.d("APIFactory", "address:" + path);
if (path != null) {
String[] s = path.split("/");
if (Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
// Log.d("APIFactory", "address type:" + "bip49");
HD_Address addr = BIP49Util.getInstance(context).getWallet().getAccount(0).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
ecKey = addr.getECKey();
} else {
// Log.d("APIFactory", "address type:" + "bip44");
int account_no = APIFactory.getInstance(context).getUnspentAccounts().get(address);
HD_Address hd_address = AddressFactory.getInstance(context).get(account_no, 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 {
// Log.d("APIFactory", "address type:" + "bip47");
String pcode = BIP47Meta.getInstance().getPCode4Addr(address);
int idx = BIP47Meta.getInstance().getIdx4Addr(address);
PaymentAddress addr = BIP47Util.getInstance(context).getReceiveAddress(new PaymentCode(pcode), idx);
ecKey = addr.getReceiveECKey();
}
} catch (Exception e) {
e.printStackTrace();
return null;
}
return ecKey;
}
use of com.samourai.wallet.hd.HD_Address in project samourai-wallet-android by Samourai-Wallet.
the class BIP84Util method getAddressAt.
public SegwitAddress getAddressAt(int chain, int idx) {
HD_Address addr = getWallet().getAccount(0).getChain(chain).getAddressAt(idx);
SegwitAddress segwitAddress = new SegwitAddress(addr.getPubKey(), SamouraiWallet.getInstance().getCurrentNetworkParams());
return segwitAddress;
}
use of com.samourai.wallet.hd.HD_Address 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