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 { 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;

View File

@ -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;

View File

@ -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>) {

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 {
private robonames: Record<string, string> = {};
@ -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;

View File

@ -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;

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,
},
},
{
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',