use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.
the class APIFactory method bip47Lookahead.
private synchronized void bip47Lookahead(String pcode, String addr) {
debug("APIFactory", "bip47Lookahead():" + addr);
debug("APIFactory", "bip47Lookahead():" + pcode);
debug("APIFactory", "bip47Lookahead():" + BIP47Meta.getInstance().getPCode4Addr(addr));
int idx = BIP47Meta.getInstance().getIdx4Addr(addr);
debug("APIFactory", "bip47Lookahead():" + idx);
try {
idx++;
for (int i = idx; i < (idx + BIP47Meta.INCOMING_LOOKAHEAD); i++) {
info("APIFactory", "receive from " + i + ":" + BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i));
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i), pcode.toString());
// _addrs.add(BIP47Util.getInstance(RefreshService.this).getReceivePubKey(new PaymentCode(pcode), i));
}
idx--;
if (idx >= 2) {
for (int i = idx; i >= (idx - (BIP47Meta.INCOMING_LOOKAHEAD - 1)); i--) {
info("APIFactory", "receive from " + i + ":" + BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i));
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i), pcode.toString());
// _addrs.add(BIP47Util.getInstance(context).getReceivePubKey(new PaymentCode(pcode), i));
}
}
// addrs = _addrs.toArray(new String[_addrs.size()]);
} catch (NullPointerException | NotSecp256k1Exception | InvalidKeyException | NoSuchAlgorithmException | NoSuchProviderException | InvalidKeySpecException e) {
;
}
}
use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.
the class PayNymDetailsActivity method doSync.
private void doSync() {
progressBar.setVisibility(View.VISIBLE);
Disposable disposable = Observable.fromCallable(() -> {
PaymentCode payment_code = new PaymentCode(pcode);
int idx = 0;
boolean loop = true;
ArrayList<String> addrs = new ArrayList<String>();
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
// Log.i("PayNymDetailsActivity", "sync receive from " + i + ":" + BIP47Util.getInstance(PayNymDetailsActivity.this).getReceivePubKey(payment_code, i));
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i));
// Log.i("PayNymDetailsActivity", "p2pkh " + i + ":" + BIP47Util.getInstance(PayNymDetailsActivity.this).getReceiveAddress(payment_code, i).getReceiveECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(this).syncBIP47Incoming(s);
// Log.i("PayNymDetailsActivity", "sync receive idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
idx = 0;
loop = true;
BIP47Meta.getInstance().setOutgoingIdx(pcode, 0);
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
PaymentAddress sendAddress = BIP47Util.getInstance(this).getSendAddress(payment_code, i);
// Log.i("PayNymDetailsActivity", "sync send to " + i + ":" + sendAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
// BIP47Meta.getInstance().setOutgoingIdx(payment_code.toString(), i);
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getSendPubKey(payment_code, i));
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(this).syncBIP47Outgoing(s);
// Log.i("PayNymDetailsActivity", "sync send idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
BIP47Meta.getInstance().pruneIncoming();
PayloadUtil.getInstance(this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(this).getGUID() + AccessFactory.getInstance(this).getPIN()));
return true;
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(aBoolean -> {
progressBar.setVisibility(View.INVISIBLE);
setPayNym();
}, error -> {
error.printStackTrace();
progressBar.setVisibility(View.INVISIBLE);
});
disposables.add(disposable);
}
use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.
the class PayNymHome method getPcodeSyncObservable.
private Observable<String> getPcodeSyncObservable(String pcode) {
return Observable.fromCallable(() -> {
try {
PaymentCode payment_code = new PaymentCode(pcode);
int idx = 0;
boolean loop = true;
ArrayList<String> addrs = new ArrayList<String>();
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
// Log.i("BIP47Activity", "sync receive from " + i + ":" + BIP47Util.getInstance(BIP47Activity.this).getReceivePubKey(payment_code, i));
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getReceivePubKey(payment_code, i));
// Log.i("BIP47Activity", "p2pkh " + i + ":" + BIP47Util.getInstance(BIP47Activity.this).getReceiveAddress(payment_code, i).getReceiveECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(getApplicationContext()).syncBIP47Incoming(s);
// Log.i("BIP47Activity", "sync receive idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
idx = 0;
loop = true;
BIP47Meta.getInstance().setOutgoingIdx(pcode, 0);
while (loop) {
addrs.clear();
for (int i = idx; i < (idx + 20); i++) {
PaymentAddress sendAddress = BIP47Util.getInstance(this).getSendAddress(payment_code, i);
// Log.i("BIP47Activity", "sync send to " + i + ":" + sendAddress.getSendECKey().toAddress(SamouraiWallet.getInstance().getCurrentNetworkParams()).toString());
// BIP47Meta.getInstance().setOutgoingIdx(payment_code.toString(), i);
BIP47Meta.getInstance().getIdx4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), i);
BIP47Meta.getInstance().getPCode4AddrLookup().put(BIP47Util.getInstance(this).getSendPubKey(payment_code, i), payment_code.toString());
addrs.add(BIP47Util.getInstance(this).getSendPubKey(payment_code, i));
}
String[] s = addrs.toArray(new String[addrs.size()]);
int nb = APIFactory.getInstance(getApplicationContext()).syncBIP47Outgoing(s);
// Log.i("BIP47Activity", "sync send idx:" + idx + ", nb == " + nb);
if (nb == 0) {
loop = false;
}
idx += 20;
}
BIP47Meta.getInstance().pruneIncoming();
PayloadUtil.getInstance(this).saveWalletToJSON(new CharSequenceX(AccessFactory.getInstance(this).getGUID() + AccessFactory.getInstance(this).getPIN()));
} catch (IOException ioe) {
;
} catch (JSONException je) {
;
} catch (DecryptionException de) {
;
} catch (NotSecp256k1Exception nse) {
;
} catch (InvalidKeySpecException ikse) {
;
} catch (InvalidKeyException ike) {
;
} catch (NoSuchAlgorithmException nsae) {
;
} catch (NoSuchProviderException nspe) {
;
} catch (MnemonicException.MnemonicLengthException mle) {
;
} catch (Exception ex) {
}
return pcode;
});
}
use of com.samourai.wallet.bip47.rpc.PaymentCode in project samourai-wallet-android by Samourai-Wallet.
the class SendFactory method getPrivKey.
public static ECKey getPrivKey(String address, int account) {
// debug("SendFactory", "get privkey for:" + address);
ECKey ecKey = null;
try {
String path = APIFactory.getInstance(context).getUnspentPaths().get(address);
debug("SendFactory", "address path:" + path);
if (path != null) {
String[] s = path.split("/");
if (FormatsUtil.getInstance().isValidBech32(address)) {
debug("SendFactory", "address type:" + "bip84");
HD_Address addr = null;
if (account == 0) {
addr = BIP84Util.getInstance(context).getWallet().getAccount(account).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
} else {
addr = BIP84Util.getInstance(context).getWallet().getAccountAt(account).getChain(Integer.parseInt(s[1])).getAddressAt(Integer.parseInt(s[2]));
}
ecKey = addr.getECKey();
} else if (Address.fromBase58(SamouraiWallet.getInstance().getCurrentNetworkParams(), address).isP2SHAddress()) {
debug("SendFactory", "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 {
debug("SendFactory", "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 {
debug("SendFactory", "address type:" + "bip47");
debug("SendFactory", "address:" + address);
String pcode = BIP47Meta.getInstance().getPCode4Addr(address);
debug("SendFactory", "pcode:" + pcode);
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.bip47.rpc.PaymentCode 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;
}
Aggregations