Skip to content

Commit

Permalink
Chore/fix defaults and schema mapping (#191)
Browse files Browse the repository at this point in the history
# Problem

Not all the schemas were correctly mapping.

## Solution

Updated the mapping setup
  • Loading branch information
wilwade authored Oct 7, 2024
1 parent ea12535 commit 213b278
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 64 deletions.
20 changes: 5 additions & 15 deletions packages/example/src/routes/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,15 @@
type Config,
defaultConfig,
getLoginOrRegistrationPayload,
type RequestedSchema,
setConfig,
type WalletProxyResponse,
} from '@projectlibertylabs/siwf';
import SignInVerification from '$lib/components/SignInVerification.svelte';
import AccountCreator from '$lib/components/AccountCreator.svelte';
import { MultiSelect, type ObjectOption, type Option } from 'svelte-multiselect';
import { MultiSelect } from 'svelte-multiselect';
import Spinner from '../lib/components/Spinner.svelte';
import type { ApiPromise } from '@polkadot/api';
import { getApi, SCHEMA_NAME_TO_ID, setApiUrl } from '@projectlibertylabs/siwf-utils';
import { getApi, parseName, SCHEMA_NAME_TO_ID, setApiUrl } from '@projectlibertylabs/siwf-utils';
if (process.env.BASE_PATH_UI) {
setConfig({
Expand Down Expand Up @@ -58,11 +57,6 @@
http: 'https://0.rpc.testnet.amplica.io',
ws: 'wss://0.rpc.testnet.amplica.io',
},
{
name: 'Frequency Rococo',
http: 'https://rpc.rococo.frequency.xyz',
ws: 'wss://rpc.rococo.frequency.xyz',
},
];
type ProxyUrl = {
Expand All @@ -84,11 +78,7 @@
let chainUrl: ChainUrl;
let loginUrl: ProxyUrl;
let expirationSeconds: number = 300;
let requestedSchemas: Option[] = defaultConfig.schemas.map((s) => ({
...s,
label: s.name,
value: s.name,
}));
let requestedSchemas: string[] = defaultConfig.schemas.map((s) => s.name);
let options = [...SCHEMA_NAME_TO_ID.keys()];
let providerId: string = '1';
let isFetchingPayload = false;
Expand All @@ -102,8 +92,8 @@
frequencyRpcUrl: chainUrl.http,
proxyUrl: loginUrl.url,
schemas: requestedSchemas.map((option) => {
const { label: _label, value: _value, ...s } = option as unknown as ObjectOption;
return { ...s } as RequestedSchema;
const parsedOption = parseName(option);
return parsedOption;
}),
siwsOptions: {
expiresInMsecs: expirationSeconds * 1_000,
Expand Down
10 changes: 5 additions & 5 deletions packages/siwf/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ import { RequestedSchema, type SiwsOptions } from './messenger';

const defaultSchemas: RequestedSchema[] = [
{
name: 'profile-resources',
name: 'dsnp.profile-resources@v1',
},
{
name: 'public-key-key-agreement',
name: 'dsnp.public-key-key-agreement@v1',
},
{
name: 'public-follows',
name: 'dsnp.public-follows@v1',
},
{
name: 'private-follows',
name: 'dsnp.private-follows@v1',
},
{
name: 'private-connections',
name: 'dsnp.private-connections@v1',
},
];

Expand Down
3 changes: 3 additions & 0 deletions packages/siwf/src/messenger/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ export type RequestedSchema = {
/// Schema name registered on-chain
name: string;

/// Optional Schema namespace registered on-chain
namespace?: string;

/// Optional version of the named schema
/// (versions are monotonically increasing integers for each registered schema name)
version?: number;
Expand Down
118 changes: 103 additions & 15 deletions packages/ui/src/lib/utils/DSNPSchemas.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,139 @@
import type { SchemaName } from '@projectlibertylabs/siwf-utils';
export interface SchemaData {
name: SchemaName;
namespace?: string;
version?: number;
description: string;
id?: number;
}

export const DSNPSchemas: SchemaData[] = [
{
name: 'tombstone',
description:
'Publish a notice that previously announced content is invalid and the related Announcement should be considered reverted',
namespace: 'dsnp',
name: 'broadcast',
version: 1,
description: 'Post a public message',
},
{
namespace: 'dsnp',
name: 'broadcast',
version: 2,
description: 'Post a public message',
},
{
name: 'reply',
description: 'Post a public reply to a Broadcast',
namespace: 'dsnp',
name: 'dsnp-content-attribute-set',
version: 1,
description: 'Create DSNP content attributes',
},
{
namespace: 'dsnp',
name: 'ext-content-attribute-set',
version: 1,
description: 'Create external content attributes',
},
{
namespace: 'dsnp',
name: 'private-connections',
version: 1,
description: 'Establish private, mutual connections between two identities',
},
{
namespace: 'dsnp',
name: 'private-follows',
version: 1,
description: 'Privately follow another identity',
},
{
namespace: 'dsnp',
name: 'profile-resources',
version: 1,
description: 'Publish public profile',
},
{
namespace: 'dsnp',
name: 'profile',
version: 1,
description: '(Deprecated) Publish profile updates',
},
{
namespace: 'dsnp',
name: 'public-follows',
version: 1,
description: 'Publicly follow another identity',
},
{
namespace: 'dsnp',
name: 'public-key-assertion-method',
version: 1,
description: 'Announce that a new cryptographic key has been added for assertation validation',
},
{
namespace: 'dsnp',
name: 'public-key-key-agreement',
version: 1,
description: 'Announce that a new cryptographic key has been added for graph privacy',
},
{
namespace: 'dsnp',
name: 'reaction',
version: 1,
description: 'Publish emoji reactions to a Broadcast',
},
{
name: 'profile-resources',
description: 'Publish profile updates',
namespace: 'dsnp',
name: 'reply',
version: 1,
description: 'Post a public reply to a Broadcast',
},
{
namespace: 'dsnp',
name: 'reply',
version: 2,
description: 'Post a public reply to a Broadcast',
},
{
namespace: 'dsnp',
name: 'tombstone',
version: 1,
description:
'Publish a notice that previously announced content is invalid and the related Announcement should be considered reverted',
},
{
namespace: 'dsnp',
name: 'tombstone',
version: 2,
description:
'Publish a notice that previously announced content is invalid and the related Announcement should be considered reverted',
},
{
namespace: 'dsnp',
name: 'update',
version: 1,
description: 'Update previously announced content',
},
{
name: 'public-key-key-agreement',
description: 'Announce that a new cryptographic key has been added for logins and permissions',
namespace: 'dsnp',
name: 'update',
version: 2,
description: 'Update previously announced content',
},
{
name: 'public-follows',
description: 'Publicly follow another identity',
namespace: 'dsnp',
name: 'user-attribute-set',
version: 1,
description: '(Deprecated) Create attributes',
},
{
name: 'private-follows',
description: 'Privately follow another identity',
namespace: 'dsnp',
name: 'user-attribute-set',
version: 2,
description: 'Create attributes',
},
{
name: 'private-connections',
description: 'Establish private, mutual connections between two identities',
namespace: 'freq',
name: 'ency.default-token-address',
version: 1,
description: 'Set default token address',
},
];
4 changes: 3 additions & 1 deletion packages/ui/src/routes/signup/create-account/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
let payloadBytes: Uint8Array;
let schemas = $RequestResponseStore.request.requiredSchemas.map((schema) => {
const d = DSNPSchemas.find((ds) => ds.name === schema.name);
const d = DSNPSchemas.find(
(ds) => ds.name === schema.name && ds.namespace === schema.namespace && ds.version === schema.version
);
return {
...schema,
description: d?.description || `Update data associated with the ${schema.name} schema`,
Expand Down
32 changes: 4 additions & 28 deletions packages/utils/src/frequency/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import type { AnyNumber, Codec, ISubmittableResult } from '@polkadot/types/types
import { Bytes, Text, u16 } from '@polkadot/types';
import { SubmittableExtrinsic } from '@polkadot/api/types';
import { RequestedSchema } from '@projectlibertylabs/siwf';
import type { SchemaName } from '../schemas';

export { Codec };
export interface MsaInfo {
Expand Down Expand Up @@ -113,37 +112,14 @@ export async function getProviderRegistryInfo(providerId: AnyNumber) {
export async function resolveSchemas(schemas: RequestedSchema[]): Promise<void> {
const api = await getApi();

// hack to suppress errors on mainnet until schema names are deployed
if (!api.query.schemas.schemaNameToIds) {
const mainnetSchemas: SchemaName[] = [
'tombstone',
'broadcast',
'reply',
'reaction',
'profile-resources',
'update',
'public-key-key-agreement',
'public-follows',
'private-follows',
'private-connections',
];
schemas.forEach((schema) => {
schema.id = (mainnetSchemas.findIndex((name) => name === schema.name) || -1) + 1;
});
return;
}
const response = await api.query.schemas.schemaNameToIds.multi(schemas.map((s) => ['dsnp', s.name]));
const response = await api.query.schemas.schemaNameToIds.multi(schemas.map((s) => [s.namespace || 'dsnp', s.name]));

schemas.forEach((schema, index) => {
const ids = response[index]!.ids.toArray()
.map((id) => (id as u16).toNumber())
.sort();
const ids = response[index]!.ids.toArray().map((id) => (id as u16).toNumber());
if (schema?.version && schema.version > 0) {
if (schema.version > ids.length) {
throw new Error(`Unable to find version ${schema.version} of schema ${schema.name}`);
if (schema.version <= ids.length) {
schema.id = ids[schema.version - 1];
}

schema.id = ids[schema.version - 1];
} else {
ids.reverse();
schema.id = ids[0];
Expand Down
12 changes: 12 additions & 0 deletions packages/utils/src/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,15 @@ export const SCHEMA_NAME_TO_ID = new Map<string, number>([
const SCHEMA_NAME_TO_ID_OBJECT = Object.fromEntries(SCHEMA_NAME_TO_ID);

export type SchemaName = keyof typeof SCHEMA_NAME_TO_ID_OBJECT;

export const parseName = (full: string): { namespace: string; name: string; version?: number } | null => {
const match = full.match(/(\w+)\.([^@]+)@v(\d+)/);
if (match && match[1] && match[2]) {
return {
namespace: match[1],
name: match[2],
version: match[3] ? parseInt(match[3]) : undefined,
};
}
return null;
};

0 comments on commit 213b278

Please sign in to comment.