mirror of
https://github.com/bisq-network/bisq.git
synced 2024-11-19 09:52:23 +01:00
Validate that XMR subaddress view key matches the main address.
This commit is contained in:
parent
7319156c46
commit
e2b25c12e1
@ -7,6 +7,8 @@ import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519EncodedGroupE
|
|||||||
import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519Group;
|
import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519Group;
|
||||||
import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519GroupElement;
|
import bisq.core.xmr.org.nem.core.crypto.ed25519.arithmetic.Ed25519GroupElement;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
|
|
||||||
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.concat;
|
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.concat;
|
||||||
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.hexToBytes;
|
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.hexToBytes;
|
||||||
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.longToLittleEndianUint32ByteArray;
|
import static bisq.core.xmr.knaccc.monero.address.ByteUtil.longToLittleEndianUint32ByteArray;
|
||||||
@ -121,7 +123,21 @@ public class WalletAddress {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getSubaddressBase58(String privateViewKeyHex, long accountId, long subaddressId) {
|
public String getSubaddressBase58(String privateViewKeyHex, long accountId, long subaddressId) throws InvalidWalletAddressException {
|
||||||
|
if (!checkPrivateViewKey(privateViewKeyHex)) {
|
||||||
|
throw new InvalidWalletAddressException("Wrong private view key for main address");
|
||||||
|
}
|
||||||
return getSubaddressBase58(new Scalar(privateViewKeyHex), hexToBytes(getPublicSpendKeyHex()), accountId, subaddressId);
|
return getSubaddressBase58(new Scalar(privateViewKeyHex), hexToBytes(getPublicSpendKeyHex()), accountId, subaddressId);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean checkPrivateViewKey(String privateViewKey) {
|
||||||
|
return arePubPrivKeysRelated(this.publicViewKeyHex, privateViewKey);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static boolean arePubPrivKeysRelated(String publicKey, String privateKey) {
|
||||||
|
Scalar m = new Scalar(privateKey);
|
||||||
|
Ed25519GroupElement M = G.scalarMultiply(new Ed25519EncodedFieldElement(m.bytes));
|
||||||
|
byte[] generatedPubKey = M.encode().getRaw();
|
||||||
|
return Arrays.equals(generatedPubKey, hexToBytes(publicKey));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -30,6 +30,7 @@ public class WalletAddressTest {
|
|||||||
WalletAddress walletAddress = new WalletAddress(mainAddress);
|
WalletAddress walletAddress = new WalletAddress(mainAddress);
|
||||||
|
|
||||||
String privateViewKeyHex = "7b37d8922245a07244fd31855d1e705a590a9bd2881825f0542ad99cdaba090a";
|
String privateViewKeyHex = "7b37d8922245a07244fd31855d1e705a590a9bd2881825f0542ad99cdaba090a";
|
||||||
|
String publicViewKeyHex = "3cd5a3079e8b4cff3630ce16bfda6eebb2da86169accdb93206a92a58d586faa";
|
||||||
|
|
||||||
System.out.println("subaddress for account index 0, subaddress index 1: "
|
System.out.println("subaddress for account index 0, subaddress index 1: "
|
||||||
+ walletAddress.getSubaddressBase58(privateViewKeyHex, 0, 1));
|
+ walletAddress.getSubaddressBase58(privateViewKeyHex, 0, 1));
|
||||||
@ -46,5 +47,24 @@ public class WalletAddressTest {
|
|||||||
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 0, 1), addr01);
|
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 0, 1), addr01);
|
||||||
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 0), addr10);
|
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 0), addr10);
|
||||||
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 1), addr11);
|
assertEquals(walletAddress.getSubaddressBase58(privateViewKeyHex, 1, 1), addr11);
|
||||||
|
|
||||||
|
assertEquals(walletAddress.checkPrivateViewKey(privateViewKeyHex), true);
|
||||||
|
assertEquals(WalletAddress.arePubPrivKeysRelated(publicViewKeyHex, privateViewKeyHex), true);
|
||||||
|
assertEquals(WalletAddress.arePubPrivKeysRelated(privateViewKeyHex, privateViewKeyHex), false);
|
||||||
|
|
||||||
|
assertEquals(WalletAddress.arePubPrivKeysRelated(
|
||||||
|
"bdc158199c8933353627d54edb4bbae547dbbde3130860d7940313210edca0a6",
|
||||||
|
"a82a9017a1d259c71f5392ad9091b743b86dac7a21f5e402ea0a55e5c8a6750f"),
|
||||||
|
true);
|
||||||
|
|
||||||
|
assertEquals(WalletAddress.arePubPrivKeysRelated(
|
||||||
|
"d17698d07fe9edbc41552299b90a93de73bb1bd4b94b8083af0bbe3a1931e2ec",
|
||||||
|
"dae1bceeb2563b8c376f8e0456e5fe7aa3d6291b38ace18c6ad5647424a3b104"),
|
||||||
|
true);
|
||||||
|
|
||||||
|
assertEquals(WalletAddress.arePubPrivKeysRelated(
|
||||||
|
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF",
|
||||||
|
"0000111122223333444455556666777788889999AAAABBBBCCCCDDDDEEEEFFFF"),
|
||||||
|
false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,6 +251,7 @@ public class XmrForm extends AssetsForm {
|
|||||||
xmrAccountDelegate.setPrivateViewKey(privateViewKeyInputTextField.getText());
|
xmrAccountDelegate.setPrivateViewKey(privateViewKeyInputTextField.getText());
|
||||||
xmrAccountDelegate.setAccountIndex(accountIndex.getText());
|
xmrAccountDelegate.setAccountIndex(accountIndex.getText());
|
||||||
xmrAccountDelegate.setSubAddressIndex(subAddressIndex.getText());
|
xmrAccountDelegate.setSubAddressIndex(subAddressIndex.getText());
|
||||||
|
subAddressTextField.getStyleClass().remove("error-text");
|
||||||
if (accountIndex.validate() && subAddressIndex.validate()
|
if (accountIndex.validate() && subAddressIndex.validate()
|
||||||
&& mainAddressTextField.validate()
|
&& mainAddressTextField.validate()
|
||||||
&& privateViewKeyInputTextField.validate()
|
&& privateViewKeyInputTextField.validate()
|
||||||
@ -258,10 +259,13 @@ public class XmrForm extends AssetsForm {
|
|||||||
&& privateViewKeyInputTextField.getText().length() > 0) {
|
&& privateViewKeyInputTextField.getText().length() > 0) {
|
||||||
try {
|
try {
|
||||||
xmrAccountDelegate.createAndSetNewSubAddress();
|
xmrAccountDelegate.createAndSetNewSubAddress();
|
||||||
} catch (Exception ex) {
|
|
||||||
log.warn(ex.toString());
|
|
||||||
}
|
|
||||||
subAddressTextField.setText(xmrAccountDelegate.getSubAddress());
|
subAddressTextField.setText(xmrAccountDelegate.getSubAddress());
|
||||||
|
} catch (Exception ex) {
|
||||||
|
log.warn(ex.getMessage());
|
||||||
|
String[] parts = ex.getMessage().split(":");
|
||||||
|
subAddressTextField.setText(parts.length > 0 ? parts[parts.length-1] : ex.getMessage());
|
||||||
|
subAddressTextField.getStyleClass().add("error-text");
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
xmrAccountDelegate.setSubAddress("");
|
xmrAccountDelegate.setSubAddress("");
|
||||||
subAddressTextField.setText("");
|
subAddressTextField.setText("");
|
||||||
|
Loading…
Reference in New Issue
Block a user