Skip to content

Commit

Permalink
Merge pull request #1711 from djing-chan/Fix-missing-PoW-check-for-us…
Browse files Browse the repository at this point in the history
…er-profile-creation

Fix missing pow check for user profile creation
  • Loading branch information
alvasw authored Feb 29, 2024
2 parents 793b50e + 811457e commit 3788da4
Show file tree
Hide file tree
Showing 21 changed files with 183 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ public DesktopApplicationService(String[] args, ShutDownHandler shutDownHandler)

userService = new UserService(UserService.Config.from(getConfig("user")),
persistenceService,
securityService,
identityService,
networkService,
bondedRolesService,
securityService.getHashCashProofOfWorkService());
bondedRolesService);

settingsService = new SettingsService(persistenceService);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@

package bisq.desktop.components.cathash;

import bisq.common.data.ByteArray;
import bisq.common.util.ByteArrayUtils;
import bisq.desktop.common.utils.ImageUtil;
import bisq.user.profile.UserProfile;
import javafx.scene.image.Image;
import lombok.extern.slf4j.Slf4j;

Expand All @@ -30,26 +31,28 @@
public class CatHash {
private static final int SIZE = 300;
private static final int MAX_CACHE_SIZE = 10000;
private static final ConcurrentHashMap<ByteArray, Image> CACHE = new ConcurrentHashMap<>();
private static final ConcurrentHashMap<BigInteger, Image> CACHE = new ConcurrentHashMap<>();

public static Image getImage(byte[] pubKeyHash) {
return getImage(new ByteArray(pubKeyHash), true);
public static Image getImage(UserProfile userProfile) {
return getImage(userProfile.getPubKeyHash(), userProfile.getProofOfWork().getSolution(), true);
}

public static Image getImage(byte[] pubKeyHash, boolean useCache) {
return getImage(new ByteArray(pubKeyHash), useCache);
public static Image getImage(byte[] pubKeyHash, byte[] powSolution) {
return getImage(pubKeyHash, powSolution, true);
}

private static Image getImage(ByteArray pubKeyHash, boolean useCache) {
if (useCache && CACHE.containsKey(pubKeyHash)) {
return CACHE.get(pubKeyHash);
public static Image getImage(byte[] pubKeyHash, byte[] powSolution, boolean useCache) {
byte[] combined = ByteArrayUtils.concat(powSolution, pubKeyHash);
BigInteger input = new BigInteger(combined);
if (useCache && CACHE.containsKey(input)) {
return CACHE.get(input);
}
BigInteger input = new BigInteger(pubKeyHash.getBytes());

int[] buckets = BucketEncoder.encode(input, BucketConfig.getBucketSizes());
String[] paths = BucketEncoder.toPaths(buckets, BucketConfig.getPathTemplates());
Image image = ImageUtil.composeImage(paths, SIZE, SIZE);
if (useCache && CACHE.size() < MAX_CACHE_SIZE) {
CACHE.put(pubKeyHash, image);
CACHE.put(input, image);
}
return image;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import bisq.common.monetary.PriceQuote;
import bisq.desktop.common.utils.ImageUtil;
import bisq.desktop.common.view.View;
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.table.BisqTableColumn;
import bisq.desktop.components.table.BisqTableView;
import bisq.desktop.main.content.components.ReputationScoreDisplay;
Expand Down Expand Up @@ -249,7 +249,7 @@ public void updateItem(final ListItem item, boolean empty) {
if (item != null && !empty) {
userName.setText(item.getMakerUserName());
item.getAuthorUserProfile().ifPresent(userProfile ->
roboIcon.setImage(CatHash.getImage(userProfile.getPubKeyHash())));
roboIcon.setImage(CatHash.getImage(userProfile)));
setGraphic(hBox);
} else {
setGraphic(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.common.view.NavigationController;
import bisq.desktop.components.controls.BisqIconButton;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.controls.BisqIconButton;
import bisq.desktop.main.content.chat.sidebar.ChannelSidebar;
import bisq.desktop.main.content.chat.sidebar.UserProfileSidebar;
import bisq.desktop.main.content.components.chatMessages.ChatMessagesComponent;
Expand Down Expand Up @@ -147,7 +147,7 @@ protected void selectedChannelChanged(@Nullable ChatChannel<? extends ChatMessag
protected void applyPeersIcon(PrivateChatChannel<?> privateChatChannel) {
if (privateChatChannel instanceof TwoPartyPrivateChatChannel) {
TwoPartyPrivateChatChannel twoPartyPrivateChatChannel = (TwoPartyPrivateChatChannel) privateChatChannel;
Image image = CatHash.getImage(twoPartyPrivateChatChannel.getPeer().getPubKeyHash());
Image image = CatHash.getImage(twoPartyPrivateChatChannel.getPeer());
ImageView imageView = new ImageView(image);
imageView.setFitWidth(35);
imageView.setFitHeight(35);
Expand All @@ -167,13 +167,13 @@ protected void applyPeersIcon(PrivateChatChannel<?> privateChatChannel) {
left = bisqEasyOpenTradeChannel.getPeer();
right = bisqEasyOpenTradeChannel.getMediator().get();
}
ImageView leftImageView = new ImageView(CatHash.getImage(left.getPubKeyHash()));
ImageView leftImageView = new ImageView(CatHash.getImage(left));
leftImageView.setFitWidth(35);
leftImageView.setFitHeight(35);
Button leftIconButton = BisqIconButton.createIconButton(leftImageView);
leftIconButton.setMouseTransparent(true);

ImageView rightImageView = new ImageView(CatHash.getImage(right.getPubKeyHash()));
ImageView rightImageView = new ImageView(CatHash.getImage(right));
rightImageView.setFitWidth(35);
rightImageView.setFitHeight(35);
Button rightIconButton = BisqIconButton.createIconButton(rightImageView);
Expand All @@ -184,7 +184,7 @@ protected void applyPeersIcon(PrivateChatChannel<?> privateChatChannel) {
hBox.setAlignment(Pos.CENTER_LEFT);
model.getChannelIconNode().set(hBox);
} else {
Image image = CatHash.getImage(bisqEasyOpenTradeChannel.getPeer().getPubKeyHash());
Image image = CatHash.getImage(bisqEasyOpenTradeChannel.getPeer());
ImageView imageView = new ImageView(image);
imageView.setFitWidth(35);
imageView.setFitHeight(35);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package bisq.desktop.main.content.chat.sidebar;

import bisq.desktop.components.controls.BisqTooltip;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.controls.BisqTooltip;
import bisq.i18n.Res;
import bisq.user.banned.BannedUserService;
import bisq.user.profile.UserProfile;
Expand Down Expand Up @@ -93,7 +93,7 @@ public void onActivate() {

String userName = userProfile.getUserName();
model.userName.set(isUserProfileBanned() ? Res.get("user.userProfile.userName.banned", userName) : userName);
model.roboHashImage.set(CatHash.getImage(userProfile.getPubKeyHash()));
model.roboHashImage.set(CatHash.getImage(userProfile));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
import bisq.desktop.ServiceProvider;
import bisq.desktop.common.Layout;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.controls.BisqIconButton;
import bisq.desktop.components.controls.BisqTooltip;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.main.content.components.ReportToModeratorWindow;
import bisq.desktop.main.content.components.ReputationScoreDisplay;
import bisq.i18n.Res;
Expand Down Expand Up @@ -129,7 +129,7 @@ public void onActivate() {
model.nickName.set(isUserProfileBanned() ? Res.get("user.userProfile.userName.banned", nickName) : nickName);
model.nym.set(Res.get("chat.sideBar.userProfile.nym", userProfile.getNym()));
model.userProfileIdString.set(Res.get("chat.sideBar.userProfile.id", userProfile.getId()));
model.roboHashNode.set(CatHash.getImage(userProfile.getPubKeyHash()));
model.roboHashNode.set(CatHash.getImage(userProfile));

model.addressByTransport.set(userProfile.getAddressByTransportDisplayString(26));
model.addressByTransportTooltip.set(userProfile.getAddressByTransportDisplayString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@
import bisq.chat.ChatService;
import bisq.chat.Citation;
import bisq.desktop.ServiceProvider;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.containers.Spacer;
import bisq.desktop.components.controls.BisqIconButton;
import bisq.desktop.components.cathash.CatHash;
import bisq.i18n.Res;
import bisq.user.profile.UserProfile;
import bisq.user.profile.UserProfileService;
Expand Down Expand Up @@ -92,7 +92,7 @@ private void reply(ChatMessage chatMessage) {
userProfileService.findUserProfile(chatMessage.getAuthorUserProfileId()).ifPresent(author -> {
model.author = author;
model.userName.set(author.getUserName());
model.roboHashNode.set(CatHash.getImage(author.getPubKeyHash()));
model.roboHashNode.set(CatHash.getImage(author));
model.citation.set(chatMessage.getText());
model.visible.set(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@

package bisq.desktop.main.content.components;

import bisq.desktop.components.controls.BisqTooltip;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.controls.BisqTooltip;
import bisq.user.profile.UserProfile;
import javafx.scene.control.Tooltip;
import javafx.scene.image.ImageView;
Expand All @@ -45,7 +45,7 @@ public void setUserProfile(@Nullable UserProfile userProfile) {
tooltip = new BisqTooltip(userProfile.getTooltipString());
tooltip.getStyleClass().add("medium-dark-tooltip");
Tooltip.install(this, tooltip);
setImage(CatHash.getImage(userProfile.getPubKeyHash()));
setImage(CatHash.getImage(userProfile));
} else {
setImage(null);
if (tooltip != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@
import bisq.desktop.common.observable.FxBindings;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.controls.AutoCompleteComboBox;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.components.cathash.CatHash;
import bisq.i18n.Res;
import bisq.user.identity.UserIdentity;
import bisq.user.identity.UserIdentityService;
Expand Down Expand Up @@ -267,7 +267,7 @@ protected void onViewAttached() {
if (userIdentity != null) {

userName.setText(comboBox.getConverter().toString(selected));
icon.setImage(CatHash.getImage(userIdentity.getPubKeyHash()));
icon.setImage(CatHash.getImage(userIdentity.getUserProfile()));
}
}
});
Expand Down Expand Up @@ -373,7 +373,7 @@ protected void updateItem(ListItem item, boolean empty) {
super.updateItem(item, empty);

if (item != null && !empty) {
imageView.setImage(CatHash.getImage(item.userIdentity.getPubKeyHash()));
imageView.setImage(CatHash.getImage(item.userIdentity.getUserProfile()));
label.setText(item.userIdentity.getUserName());

labelWidthListener = (observable, oldValue, newValue) -> {
Expand Down Expand Up @@ -463,7 +463,7 @@ public UserProfileSkin(ComboBox<ListItem> control, String description, String pr
if (newValue != null) {
UserIdentity userIdentity = newValue.userIdentity;
if (userIdentity != null) {
imageView.setImage(CatHash.getImage(userIdentity.getPubKeyHash()));
imageView.setImage(CatHash.getImage(userIdentity.getUserProfile()));
label.setText(control.getConverter().toString(newValue));
buttonPane.layout();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.Controller;
import bisq.desktop.common.view.Navigation;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.overlay.Popup;
import bisq.i18n.Res;
import bisq.network.p2p.services.data.BroadcastResult;
import bisq.presentation.formatters.TimeFormatter;
Expand Down Expand Up @@ -82,7 +82,7 @@ public void onActivate() {
model.getNickName().set(userProfile.getNickName());
model.getNymId().set(userProfile.getNym());
model.getProfileId().set(userProfile.getId());
model.getRoboHash().set(CatHash.getImage(userProfile.getPubKeyHash()));
model.getRoboHash().set(CatHash.getImage(userProfile));
model.getStatement().set(userProfile.getStatement());
model.getTerms().set(userProfile.getTerms());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import bisq.desktop.ServiceProvider;
import bisq.desktop.common.threading.UIThread;
import bisq.desktop.common.view.InitWithDataController;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.components.cathash.CatHash;
import bisq.desktop.components.overlay.Popup;
import bisq.desktop.overlay.OverlayController;
import bisq.i18n.Res;
import bisq.security.pow.ProofOfWork;
Expand Down Expand Up @@ -90,7 +90,7 @@ public void initWithData(InitData data) {
model.setProofOfWork(data.getProofOfWork());
model.getNickName().set(data.getNickName());
model.getNym().set(data.getNym());
model.getRoboHashImage().set(CatHash.getImage(data.getPubKeyHash()));
model.getRoboHashImage().set(CatHash.getImage(data.getPubKeyHash(), data.getProofOfWork().getSolution()));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import bisq.security.DigestUtil;
import bisq.security.keys.KeyBundleService;
import bisq.security.pow.ProofOfWork;
import bisq.security.pow.hashcash.HashCashProofOfWorkService;
import bisq.user.NymIdGenerator;
import bisq.user.identity.NymIdGenerator;
import bisq.user.identity.UserIdentityService;
import bisq.user.profile.UserProfile;
import javafx.scene.image.Image;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.fxmisc.easybind.EasyBind;
Expand All @@ -51,16 +51,13 @@ public class CreateProfileController implements Controller {
protected final CreateProfileView view;
protected final UserIdentityService userIdentityService;
protected final KeyBundleService keyBundleService;
// We do not support multiple proof of work types
protected final HashCashProofOfWorkService hashCashProofOfWorkService;
protected final IdentityService identityService;
private final OverlayController overlayController;
protected Optional<CompletableFuture<ProofOfWork>> mintNymProofOfWorkFuture = Optional.empty();
protected Subscription nickNameSubscription;

public CreateProfileController(ServiceProvider serviceProvider) {
keyBundleService = serviceProvider.getSecurityService().getKeyBundleService();
hashCashProofOfWorkService = serviceProvider.getSecurityService().getHashCashProofOfWorkService();
userIdentityService = serviceProvider.getUserService().getUserIdentityService();
identityService = serviceProvider.getIdentityService();
overlayController = OverlayController.getInstance();
Expand Down Expand Up @@ -144,21 +141,26 @@ void generateNewKeyPair() {
model.setKeyPair(Optional.of(keyPair));
byte[] pubKeyHash = DigestUtil.hash(keyPair.getPublic().getEncoded());
model.setPubKeyHash(Optional.of(pubKeyHash));
// mintNymProofOfWork is executed on a ForkJoinPool thread
mintNymProofOfWorkFuture = Optional.of(createProofOfWork(pubKeyHash));
}

private CompletableFuture<ProofOfWork> createProofOfWork(byte[] pubKeyHash) {
long ts = System.currentTimeMillis();
return hashCashProofOfWorkService.mintNymProofOfWork(pubKeyHash)
return CompletableFuture.supplyAsync(() -> userIdentityService.mintNymProofOfWork(pubKeyHash))
.thenApply(proofOfWork -> {
long powDuration = System.currentTimeMillis() - ts;
log.info("Proof of work creation completed after {} ms", powDuration);
createSimulatedDelay(powDuration);
UIThread.run(() -> {
model.setProofOfWork(Optional.of(proofOfWork));
String nym = NymIdGenerator.fromHash(pubKeyHash);
applyIdentityData(pubKeyHash, nym);
byte[] powSolution = proofOfWork.getSolution();
String nym = NymIdGenerator.generate(pubKeyHash, powSolution);
Image image = CatHash.getImage(pubKeyHash, powSolution);
model.getNym().set(nym);
model.getRoboHashImage().set(image);
model.getPowProgress().set(0);
model.getRoboHashIconVisible().set(true);
model.getReGenerateButtonDisabled().set(false);
});
return proofOfWork;
});
Expand Down Expand Up @@ -195,12 +197,4 @@ private void setPreGenerateState() {
model.getPowProgress().set(-1);
model.getNym().set(Res.get("onboarding.createProfile.nym.generating"));
}

private void applyIdentityData(byte[] pubKeyHash, String nym) {
model.getNym().set(nym);
model.getRoboHashImage().set(CatHash.getImage(pubKeyHash));
model.getPowProgress().set(0);
model.getRoboHashIconVisible().set(true);
model.getReGenerateButtonDisabled().set(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,10 +128,10 @@ public RestApiApplicationService(String[] args) {

userService = new UserService(UserService.Config.from(getConfig("user")),
persistenceService,
securityService,
identityService,
networkService,
bondedRolesService,
securityService.getHashCashProofOfWorkService());
bondedRolesService);

settingsService = new SettingsService(persistenceService);

Expand Down
10 changes: 0 additions & 10 deletions security/src/main/java/bisq/security/pow/ProofOfWorkService.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@
// Borrowed from: https://github.com/bisq-network/bisq
@Slf4j
public abstract class ProofOfWorkService {
public final static int MINT_NYM_DIFFICULTY = 65536; // Math.pow(2, 16) = 65536;

public ProofOfWorkService() {
}

Expand All @@ -54,14 +52,6 @@ public CompletableFuture<ProofOfWork> mintAsync(String itemId, String ownerId, d
return mintAsync(asUtf8Bytes(itemId), getChallenge(itemId, ownerId), difficulty);
}

public CompletableFuture<ProofOfWork> mintNymProofOfWork(byte[] pubKeyHash) {
return mintNymProofOfWork(pubKeyHash, MINT_NYM_DIFFICULTY);
}

public CompletableFuture<ProofOfWork> mintNymProofOfWork(byte[] pubKeyHash, double nymDifficulty) {
return mintAsync(pubKeyHash, null, nymDifficulty);
}

public boolean verify(ProofOfWork proofOfWork,
String itemId,
String ownerId,
Expand Down
Loading

0 comments on commit 3788da4

Please sign in to comment.