Skip to content

Commit

Permalink
hashing and concantenating moved to utils so its in one place
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoshock authored and ldunnplaymint committed Oct 24, 2024
1 parent b350918 commit 9be8619
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 17 deletions.
14 changes: 9 additions & 5 deletions src/gui/components/ChannelBoot.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import md5 from 'md5';
import { memo, useMemo } from 'react';
import { config as socketConfig } from 'socket:application';
import { BOOTSTRAP_PEERS } from '../../runtime/bootstrap';
import { NETWORK_ID } from '../../runtime/config';
import { DB } from '../../runtime/db';
import { sleep } from '../../runtime/timers';
import { getVersionStringFromConfig } from '../../runtime/utils';
import {
getVersionNumberHash,
getVersionStringFromConfig,
splitChannelCode,
} from '../../runtime/utils';
import { ClientContextType, useClient } from '../hooks/use-client';
import { useCredentials } from '../hooks/use-credentials';
import { useDatabase } from '../hooks/use-database';
Expand Down Expand Up @@ -289,14 +292,15 @@ const terminalFlow = ({
reject('invalid key');
return;
}
const [channelId, hostVersionHash] = input.split(':');
const { channelId, hostVersionHash } =
splitChannelCode(input);
if (!hostVersionHash) {
reject('invalid key');
return;
}
const clientVersionHash = md5(
const clientVersionHash = getVersionNumberHash(
getVersionStringFromConfig(socketConfig),
).slice(0, 4);
);
if (hostVersionHash !== clientVersionHash) {
reject('client app version incompatible with host');
return;
Expand Down
15 changes: 3 additions & 12 deletions src/gui/components/ChannelView.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useLiveQuery } from 'dexie-react-hooks';
import md5 from 'md5';
import { memo, useCallback, useEffect, useMemo, useRef, useState } from 'react';
import { config as socketConfig } from 'socket:application';
import { SESSION_TIME_SECONDS } from '../../examples/spaceshooter';
import { ChannelInfo } from '../../runtime/channels';
import { PeerInfo } from '../../runtime/db';
import { DefaultMetrics } from '../../runtime/metrics';
import { sleep } from '../../runtime/timers';
import { getVersionStringFromConfig, hardReset } from '../../runtime/utils';
import { getChannelCode, hardReset } from '../../runtime/utils';
import { getPlayerColorUi } from '../fixtures/player-colors';
import { useClient } from '../hooks/use-client';
import { useCredentials } from '../hooks/use-credentials';
Expand All @@ -34,14 +33,6 @@ export const SIM_END = SESSION_TIME_SECONDS / (FIXED_UPDATE_RATE / 1000);

const src = '/examples/spaceshooter.js'; // not a real src yet see runtime/game.ts

const getChannelCode = (channelId: string) => {
return (
channelId +
':' +
md5(getVersionStringFromConfig(socketConfig)).slice(0, 4)
);
};

export default memo(function ChannelView({
channel,
details,
Expand All @@ -59,7 +50,7 @@ export default memo(function ChannelView({

const copyKeyToClipboard = () => {
navigator.clipboard
.writeText(getChannelCode(channel.id))
.writeText(getChannelCode(channel.id, socketConfig))
.catch((err) => {
console.error('clipboard write failed:', err);
});
Expand Down Expand Up @@ -219,7 +210,7 @@ export default memo(function ChannelView({
alignItems: 'center',
}}
>
{getChannelCode(channel.id)}{' '}
{getChannelCode(channel.id, socketConfig)}{' '}
<span
className={`${theme.materialSymbolsOutlined} ${termstyles.promptTextColor}`}
style={{ padding: '0 4px', cursor: 'pointer' }}
Expand Down
22 changes: 22 additions & 0 deletions src/runtime/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Dexie from 'dexie';
import md5 from 'md5';

// stop vite being a nob and just let me do a dynamic import
export async function importStatic(modulePath) {
Expand Down Expand Up @@ -50,3 +51,24 @@ export function getVersionStringFromConfig(socketConfig: any) {
? socketConfig['meta_title'].split('v:')[1]
: socketConfig['meta_version'];
}

export function getVersionNumberHash(version: string) {
return md5(version).slice(0, 4);
}

const CHANNEL_CODE_DELIMITER = ':';

export function getChannelCode(channelId: string, socketConfig: any) {
return (
channelId +
CHANNEL_CODE_DELIMITER +
getVersionNumberHash(getVersionStringFromConfig(socketConfig))
);
}

export function splitChannelCode(channelCode: string) {
const [channelId, hostVersionHash] = channelCode.split(
CHANNEL_CODE_DELIMITER,
);
return { channelId, hostVersionHash };
}

0 comments on commit 9be8619

Please sign in to comment.