Skip to content

Commit

Permalink
Android robohash generator
Browse files Browse the repository at this point in the history
  • Loading branch information
KoalaSat committed Apr 30, 2024
1 parent 66e9fc4 commit 4b572d0
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 51 deletions.
3 changes: 1 addition & 2 deletions frontend/src/components/RobotAvatar/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import SmoothImage from 'react-smooth-image';
import { Avatar, Badge, Tooltip } from '@mui/material';
import { SendReceiveIcon } from '../Icons';
import placeholder from './placeholder.json';
// import { robohash } from './RobohashGenerator';
import { AppContext, type UseAppStoreType } from '../../contexts/AppContext';
import { roboidentitiesClient } from '../../services/Roboidentities';
import { roboidentitiesClient } from '../../services/Roboidentities/Web';

interface Props {
shortAlias?: string | undefined;
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/models/Coordinator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import {
type Order,
type Garage,
} from '.';
import { roboidentitiesClient } from '../services/Roboidentities';
import { roboidentitiesClient } from '../services/Roboidentities/Web';
import { apiClient } from '../services/api';
import { validateTokenEntropy } from '../utils';
import { compareUpdateLimit } from './Limit.model';
import { defaultOrder } from './Order.model';
// import { robohash } from '../components/RobotAvatar/RobohashGenerator';

export interface Contact {
nostr?: string | undefined;
Expand Down
3 changes: 1 addition & 2 deletions frontend/src/models/Slot.model.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { sha256 } from 'js-sha256';
import { Robot, type Order } from '.';
// import { robohash } from '../components/RobotAvatar/RobohashGenerator';
import { roboidentitiesClient } from '../services/Roboidentities';
import { roboidentitiesClient } from '../services/Roboidentities/Web';

class Slot {
constructor(token: string, shortAliases: string[], robotAttributes: Record<any, any>) {
Expand Down
4 changes: 4 additions & 0 deletions frontend/src/services/Roboidentities/Native.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import RoboidentitiesClientNativeClient from './RoboidentitiesNativeClient';
import { RoboidentitiesClient } from './type';

export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientNativeClient();
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { type RoboidentitiesClient } from '..';
import { type RoboidentitiesClient } from '../type';

class RoboidentitiesNativeClient implements RoboidentitiesClient {
private robonames: Record<string, string> = {};
Expand All @@ -19,26 +19,24 @@ class RoboidentitiesNativeClient implements RoboidentitiesClient {
}
};

public generateRobohash: (initialString: string, size: string) => Promise<string> = async (
initialString,
size,
) => {
const key = `${initialString};${size === 'small' ? 80 : 256}`;
public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string> =
async (initialString, size) => {
const key = `${initialString};${size === 'small' ? 80 : 256}`;

if (this.robohashes[key]) {
return this.robohashes[key];
} else {
const response = await window.NativeRobosats?.postMessage({
category: 'roboidentities',
type: 'robohash',
detail: key,
});
const result = response ? Object.values(response)[0] : '';
const image = `data:image/png;base64,${result}`;
this.robohashes[key] = image;
return image;
}
};
if (this.robohashes[key]) {
return this.robohashes[key];
} else {
const response = await window.NativeRobosats?.postMessage({
category: 'roboidentities',
type: 'robohash',
detail: key,
});
const result = response ? Object.values(response)[0] : '';
const image = `data:image/png;base64,${result}`;
this.robohashes[key] = image;
return image;
}
};
}

export default RoboidentitiesNativeClient;
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { type RoboidentitiesClient } from '..';
// import { generate_roboname } from 'robo-identities-wasm';
import { type RoboidentitiesClient } from '../type';
import { generate_roboname } from 'robo-identities-wasm';
import { robohash } from './RobohashGenerator';

class RoboidentitiesClientWebClient implements RoboidentitiesClient {
public generateRoboname: (initialString: string) => Promise<string> = async (initialString) => {
return new Promise<string>(async (resolve, _reject) => {
// resolve(generate_roboname(initialString))
resolve(generate_roboname(initialString));
});
};

public generateRobohash: (initialString: string, size: string) => Promise<string> = async (
initialString,
size,
) => {
return new Promise<string>(async (resolve, _reject) => {
// resolve(generate_roboname(initialString))
});
};
public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string> =
async (initialString, size) => {
return robohash.generate(initialString, size);
};
}

export default RoboidentitiesClientWebClient;
4 changes: 4 additions & 0 deletions frontend/src/services/Roboidentities/Web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import RoboidentitiesClientWebClient from './RoboidentitiesWebClient';
import { RoboidentitiesClient } from './type';

export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientWebClient();
14 changes: 0 additions & 14 deletions frontend/src/services/Roboidentities/index.ts

This file was deleted.

4 changes: 4 additions & 0 deletions frontend/src/services/Roboidentities/type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface RoboidentitiesClient {
generateRoboname: (initialString: string) => Promise<string>;
generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string>;
}
9 changes: 9 additions & 0 deletions frontend/webpack.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@ const configMobile: Configuration = {
async: true,
},
},
{
test: path.resolve(__dirname, 'src/services/Roboidentities/Web.ts'),
loader: 'file-replace-loader',
options: {
condition: 'if-replacement-exists',
replacement: path.resolve(__dirname, 'src/services/Roboidentities/Native.ts'),
async: true,
},
},
{
test: path.resolve(__dirname, 'src/components/RobotAvatar/placeholder.json'),
loader: 'file-replace-loader',
Expand Down

0 comments on commit 4b572d0

Please sign in to comment.