Android robohash generator

This commit is contained in:
KoalaSat 2024-04-30 16:01:54 +02:00
parent 66e9fc46c5
commit 4b572d0171
No known key found for this signature in database
GPG Key ID: 2F7F61C6146AB157
12 changed files with 50 additions and 51 deletions

View File

@ -3,9 +3,8 @@ import SmoothImage from 'react-smooth-image';
import { Avatar, Badge, Tooltip } from '@mui/material'; import { Avatar, Badge, Tooltip } from '@mui/material';
import { SendReceiveIcon } from '../Icons'; import { SendReceiveIcon } from '../Icons';
import placeholder from './placeholder.json'; import placeholder from './placeholder.json';
// import { robohash } from './RobohashGenerator';
import { AppContext, type UseAppStoreType } from '../../contexts/AppContext'; import { AppContext, type UseAppStoreType } from '../../contexts/AppContext';
import { roboidentitiesClient } from '../../services/Roboidentities'; import { roboidentitiesClient } from '../../services/Roboidentities/Web';
interface Props { interface Props {
shortAlias?: string | undefined; shortAlias?: string | undefined;

View File

@ -6,12 +6,11 @@ import {
type Order, type Order,
type Garage, type Garage,
} from '.'; } from '.';
import { roboidentitiesClient } from '../services/Roboidentities'; import { roboidentitiesClient } from '../services/Roboidentities/Web';
import { apiClient } from '../services/api'; import { apiClient } from '../services/api';
import { validateTokenEntropy } from '../utils'; import { validateTokenEntropy } from '../utils';
import { compareUpdateLimit } from './Limit.model'; import { compareUpdateLimit } from './Limit.model';
import { defaultOrder } from './Order.model'; import { defaultOrder } from './Order.model';
// import { robohash } from '../components/RobotAvatar/RobohashGenerator';
export interface Contact { export interface Contact {
nostr?: string | undefined; nostr?: string | undefined;

View File

@ -1,7 +1,6 @@
import { sha256 } from 'js-sha256'; import { sha256 } from 'js-sha256';
import { Robot, type Order } from '.'; import { Robot, type Order } from '.';
// import { robohash } from '../components/RobotAvatar/RobohashGenerator'; import { roboidentitiesClient } from '../services/Roboidentities/Web';
import { roboidentitiesClient } from '../services/Roboidentities';
class Slot { class Slot {
constructor(token: string, shortAliases: string[], robotAttributes: Record<any, any>) { constructor(token: string, shortAliases: string[], robotAttributes: Record<any, any>) {

View File

@ -0,0 +1,4 @@
import RoboidentitiesClientNativeClient from './RoboidentitiesNativeClient';
import { RoboidentitiesClient } from './type';
export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientNativeClient();

View File

@ -1,4 +1,4 @@
import { type RoboidentitiesClient } from '..'; import { type RoboidentitiesClient } from '../type';
class RoboidentitiesNativeClient implements RoboidentitiesClient { class RoboidentitiesNativeClient implements RoboidentitiesClient {
private robonames: Record<string, string> = {}; private robonames: Record<string, string> = {};
@ -19,26 +19,24 @@ class RoboidentitiesNativeClient implements RoboidentitiesClient {
} }
}; };
public generateRobohash: (initialString: string, size: string) => Promise<string> = async ( public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string> =
initialString, async (initialString, size) => {
size, const key = `${initialString};${size === 'small' ? 80 : 256}`;
) => {
const key = `${initialString};${size === 'small' ? 80 : 256}`;
if (this.robohashes[key]) { if (this.robohashes[key]) {
return this.robohashes[key]; return this.robohashes[key];
} else { } else {
const response = await window.NativeRobosats?.postMessage({ const response = await window.NativeRobosats?.postMessage({
category: 'roboidentities', category: 'roboidentities',
type: 'robohash', type: 'robohash',
detail: key, detail: key,
}); });
const result = response ? Object.values(response)[0] : ''; const result = response ? Object.values(response)[0] : '';
const image = `data:image/png;base64,${result}`; const image = `data:image/png;base64,${result}`;
this.robohashes[key] = image; this.robohashes[key] = image;
return image; return image;
} }
}; };
} }
export default RoboidentitiesNativeClient; export default RoboidentitiesNativeClient;

View File

@ -1,21 +1,18 @@
import { type RoboidentitiesClient } from '..'; import { type RoboidentitiesClient } from '../type';
// import { generate_roboname } from 'robo-identities-wasm'; import { generate_roboname } from 'robo-identities-wasm';
import { robohash } from './RobohashGenerator';
class RoboidentitiesClientWebClient implements RoboidentitiesClient { class RoboidentitiesClientWebClient implements RoboidentitiesClient {
public generateRoboname: (initialString: string) => Promise<string> = async (initialString) => { public generateRoboname: (initialString: string) => Promise<string> = async (initialString) => {
return new Promise<string>(async (resolve, _reject) => { return new Promise<string>(async (resolve, _reject) => {
// resolve(generate_roboname(initialString)) resolve(generate_roboname(initialString));
}); });
}; };
public generateRobohash: (initialString: string, size: string) => Promise<string> = async ( public generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string> =
initialString, async (initialString, size) => {
size, return robohash.generate(initialString, size);
) => { };
return new Promise<string>(async (resolve, _reject) => {
// resolve(generate_roboname(initialString))
});
};
} }
export default RoboidentitiesClientWebClient; export default RoboidentitiesClientWebClient;

View File

@ -0,0 +1,4 @@
import RoboidentitiesClientWebClient from './RoboidentitiesWebClient';
import { RoboidentitiesClient } from './type';
export const roboidentitiesClient: RoboidentitiesClient = new RoboidentitiesClientWebClient();

View File

@ -1,14 +0,0 @@
import RoboidentitiesClientNativeClient from './RoboidentitiesNativeClient';
import RoboidentitiesClientWebClient from './RoboidentitiesWebClient';
export interface RoboidentitiesClient {
generateRoboname: (initialString: string) => Promise<string>;
generateRobohash: (initialString: string, size: string) => Promise<string>;
}
export const roboidentitiesClient: RoboidentitiesClient =
// If userAgent has "RoboSats", we assume the app is running inside of the
// react-native-web view of the RoboSats Android app.
window.navigator.userAgent.includes('robosats')
? new RoboidentitiesClientNativeClient()
: new RoboidentitiesClientWebClient();

View File

@ -0,0 +1,4 @@
export interface RoboidentitiesClient {
generateRoboname: (initialString: string) => Promise<string>;
generateRobohash: (initialString: string, size: 'small' | 'large') => Promise<string>;
}

View File

@ -56,6 +56,15 @@ const configMobile: Configuration = {
async: true, 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'), test: path.resolve(__dirname, 'src/components/RobotAvatar/placeholder.json'),
loader: 'file-replace-loader', loader: 'file-replace-loader',