Skip to content

Commit

Permalink
Append hash_id to existing robots on first login after upgrade
Browse files Browse the repository at this point in the history
  • Loading branch information
Reckless-Satoshi committed Jul 23, 2023
1 parent c437a48 commit a814909
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
5 changes: 4 additions & 1 deletion frontend/src/components/RobotAvatar/RobohashGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ class RoboGenerator {
}
}

public generate: (hash: string, size: number) => Promise<string> = async (hash, size) => {
public generate: (hash: string, size: 'small' | 'large') => Promise<string> = async (
hash,
size,
) => {
const cacheKey = `${size}px;${hash}`;
if (this.assetsCache[cacheKey]) {
return this.assetsCache[cacheKey];
Expand Down
10 changes: 4 additions & 6 deletions frontend/src/contexts/AppContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import { createTheme, type Theme } from '@mui/material/styles';
import i18n from '../i18n/Web';
import { systemClient } from '../services/System';
import { robohash } from '../components/RobotAvatar/RobohashGenerator';
import { generate_roboname } from 'robo-identities-wasm';

const getWindowSize = function (fontSize: number) {
// returns window size in EM units
Expand Down Expand Up @@ -331,6 +332,7 @@ export const useAppStore = () => {
const token = newToken ?? robot.token ?? '';
const hash_id = sha256(sha256(token));

const nickname = generate_roboname(hash_id);
robohash.generate(hash_id, 'small');
robohash.generate(hash_id, 'large');

Expand Down Expand Up @@ -367,14 +369,10 @@ export const useAppStore = () => {
apiClient
.get(baseUrl, '/api/robot/', auth)
.then((data: any) => {
// TODO remove when using hash_id as robohash
robohash.generate(data.nickname, 'small');
robohash.generate(data.nickname, 'large');
// END TODO

const newRobot = {
avatarLoaded: isRefresh ? robot.avatarLoaded : false,
nickname: data.nickname,
nickname,
hash_id,
token,
tokenSHA256,
loading: false,
Expand Down
15 changes: 14 additions & 1 deletion robosats/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ def __call__(self, request):
token = Token.objects.get(key=token_sha256_b91)
update_last_login(None, token.user)

# DEPRECATE After v0.5.2
# Will add hash_id to robots created before 0.5.2.
# Can be completely deleted after the transitional period
if not token.user.robot.hash_id:
robot = token.user.robot
token_sha256 = base91_to_hex(token_sha256_b91)
hash = hashlib.sha256(token_sha256.encode("utf-8")).hexdigest()
robot.hash_id = hash
robot.save()
# END DEPRECATE

except Token.DoesNotExist:
# If we get here the user does not have a robot on this coordinator
# Let's create a new user & robot on-the-fly.
Expand Down Expand Up @@ -103,11 +114,13 @@ def __call__(self, request):
# `user = User.objects.create_user(username=nickname, password=None)`
try:
user = User.objects.create_user(username=nickname, password=None)
user.robot.hash_id = hash
except IntegrityError:
# UNIQUE constrain failed, user exist. Get it.
user = User.objects.get(username=nickname)

# Save hash_id
user.robot.hash_id = hash

# Django rest_framework authtokens are limited to 40 characters.
# We use base91 so we can store the full entropy in the field.
Token.objects.create(key=token_sha256_b91, user=user)
Expand Down

0 comments on commit a814909

Please sign in to comment.