Skip to content

Commit

Permalink
Add support for addresses with integrated payment ID
Browse files Browse the repository at this point in the history
  • Loading branch information
chimp1984 committed Sep 3, 2020
1 parent bf73ffc commit 8682127
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
30 changes: 23 additions & 7 deletions assets/src/main/java/bisq/asset/CryptoNoteUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,38 @@
import java.util.Map;

public class CryptoNoteUtils {
public static String convertToRawHex(String address) {
public static String getRawSpendKeyAndViewKey(String address) throws CryptoNoteUtils.CryptoNoteException {
try {
byte[] decoded = MoneroBase58.decode(address);
// omit the type (1st byte) and checksum (last 4 byte)
byte[] slice = Arrays.copyOfRange(decoded, 1, decoded.length - 4);
// See https://monerodocs.org/public-address/standard-address/
byte[] decoded = CryptoNoteUtils.MoneroBase58.decode(address);
// Standard addresses are of length 69 and addresses with integrated payment ID of length 77.

if (decoded.length <= 65) {
throw new CryptoNoteUtils.CryptoNoteException("The address we received is too short. address=" + address);
}

// If the length is not as expected but still can be truncated we log an error and continue.
if (decoded.length != 69 && decoded.length != 77) {
System.out.println("The address we received is not in the expected format. address=" + address);
}

// We remove the network type byte, the checksum (4 bytes) and optionally the payment ID (8 bytes if present)
// So extract the 64 bytes after the first byte, which are the 32 byte public spend key + the 32 byte public view key
byte[] slice = Arrays.copyOfRange(decoded, 1, 65);
return Utils.HEX.encode(slice);
} catch (CryptoNoteException e) {
e.printStackTrace();
} catch (CryptoNoteUtils.CryptoNoteException e) {
throw new CryptoNoteUtils.CryptoNoteException(e);
}
return null;
}

public static class CryptoNoteException extends Exception {
CryptoNoteException(String msg) {
super(msg);
}

public CryptoNoteException(CryptoNoteException exception) {
super(exception);
}
}

static class Keccak {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public XmrTxProofRequest.Result parse(XmrTxProofModel model, String jsonTxt) {
if (jsonAddress == null) {
return XmrTxProofRequest.Result.ERROR.with(XmrTxProofRequest.Detail.API_INVALID.error("Missing address field"));
} else {
String expectedAddressHex = CryptoNoteUtils.convertToRawHex(model.getRecipientAddress());
String expectedAddressHex = CryptoNoteUtils.getRawSpendKeyAndViewKey(model.getRecipientAddress());
if (!jsonAddress.getAsString().equalsIgnoreCase(expectedAddressHex)) {
log.warn("Address from json result (convertToRawHex):\n{}\nExpected (convertToRawHex):\n{}\nRecipient address:\n{}",
jsonAddress.getAsString(), expectedAddressHex, model.getRecipientAddress());
Expand Down Expand Up @@ -167,6 +167,8 @@ public XmrTxProofRequest.Result parse(XmrTxProofModel model, String jsonTxt) {

} catch (JsonParseException | NullPointerException e) {
return XmrTxProofRequest.Result.ERROR.with(XmrTxProofRequest.Detail.API_INVALID.error(e.toString()));
} catch (CryptoNoteUtils.CryptoNoteException e) {
return XmrTxProofRequest.Result.ERROR.with(XmrTxProofRequest.Detail.ADDRESS_INVALID.error(e.toString()));
}
}
}

0 comments on commit 8682127

Please sign in to comment.