Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HashString for LOC public data #511

Merged
merged 2 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/certificate/Certificate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import Authenticate from "./Authenticate";
import TokensRecords from "./TokensRecords";
import './Certificate.css'
import CertificateTitle from "./CertificateTitle";
import InlineHashString, { validValueOrHex } from "src/components/inlinehashstring/InlineHashString";

export default function Certificate() {

Expand Down Expand Up @@ -431,8 +432,8 @@ function MetadataItemCellRow(props: { items: MergedMetadataItem[], checkResult:
return (
<Row>
{ props.items.map(
item => <CertificateCell key={ item.name } md={ 6 } label={ <ItemCellTitle text={ item.name } timestamp={ item.addedOn } /> } matched={ props.checkResult?.metadataItem?.value === item.value } >
<pre>{ item.value }</pre>
item => <CertificateCell key={ item.name.hash.toHex() } md={ 6 } label={ <ItemCellTitle text={ validValueOrHex(item.name) } timestamp={ item.addedOn } /> } matched={ props.checkResult?.metadataItem?.value === item.value } >
<pre>{ validValueOrHex(item.value) }</pre>
</CertificateCell>)
}
</Row>
Expand All @@ -454,7 +455,7 @@ function FileCellRow(props: { loc: LocData, files: MergedFile[], checkResult: Ch
<Row>
{ props.files.map(
file => <CertificateCell key={ file.hash.toHex() } md={ 6 } label={ <ItemCellTitle
text={ <span>Document Hash <span className="file-nature">({ file.nature })</span></span> }
text={ <span>Document Hash <span className="file-nature">(<InlineHashString value={file.nature}/>)</span></span> }
timestamp={ file.addedOn } /> } matched={ props.checkResult?.file?.hash === file.hash }>
<p>{ file.hash.toHex() }</p>
{
Expand All @@ -465,7 +466,7 @@ function FileCellRow(props: { loc: LocData, files: MergedFile[], checkResult: Ch
item={ props.item }
file={{
hash: file.hash,
name: file.nature,
name: validValueOrHex(file.nature),
type: "Collection",
}}
owner={ props.loc.ownerAddress }
Expand All @@ -484,7 +485,7 @@ function LinkCellRow(props: { links: MergedLink[] }) {
<Row>
{ props.links.map(
link =>
<CertificateCell key={ link.target } md={ 6 } label={ <ItemCellTitle text={ <span>Linked LOC <span className="file-nature">({ link.nature })</span></span> } timestamp={ link.addedOn } /> }>
<CertificateCell key={ link.target } md={ 6 } label={ <ItemCellTitle text={ <span>Linked LOC <span className="file-nature">(<InlineHashString value={link.nature}/>)</span></span> } timestamp={ link.addedOn } /> }>
<NewTabLink href={ fullCertificateUrl(new UUID(link.target)) } iconId="loc-link">{ new UUID(link.target).toDecimalString() }</NewTabLink>
</CertificateCell>)
}
Expand Down
1 change: 1 addition & 0 deletions src/common/Dialog.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
border-style: solid;
text-align: center;
border-color: #e6007a;
overflow: hidden;
}

.Dialog .modal-body {
Expand Down
6 changes: 5 additions & 1 deletion src/components/inlinehashstring/InlineHashString.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,9 @@ export interface Props {
}

export default function InlineHashString(props: Props) {
return <>{ props.value.isValidValue() ? props.value.validValue() : props.value.hash.toHex() }</>;
return <>{ validValueOrHex(props.value) }</>;
}

export function validValueOrHex(hashString: HashString): string {
return hashString.isValidValue() ? hashString.validValue() : hashString.hash.toHex();
}
4 changes: 2 additions & 2 deletions src/loc/AcknowledgeButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export default function AcknowledgeButton(props: Props) {
if(locState instanceof OpenLoc) {
if(props.locItem.type === "Data") {
fees = await locState.legalOfficer.estimateFeesAcknowledgeMetadata({
nameHash: props.locItem.as<MetadataData>().nameHash,
nameHash: props.locItem.as<MetadataData>().name.hash,
})
} else if(props.locItem.type === "Document") {
fees = await locState.legalOfficer.estimateFeesAcknowledgeFile({
Expand Down Expand Up @@ -80,7 +80,7 @@ export default function AcknowledgeButton(props: Props) {
});
} else if(props.locItem.type === "Data") {
return current.legalOfficer.acknowledgeMetadata({
nameHash: props.locItem.as<MetadataData>().nameHash,
nameHash: props.locItem.as<MetadataData>().name.hash,
signer,
callback,
});
Expand Down
8 changes: 3 additions & 5 deletions src/loc/CloseLocButton.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { render } from '@testing-library/react';
import { LocData, ItemStatus } from "@logion/client";
import { LocData, ItemStatus, HashString } from "@logion/client";

import { clickByName, expectNoDialogVisible } from '../tests';
import { DEFAULT_LEGAL_OFFICER } from '../common/TestData';
Expand All @@ -9,7 +9,6 @@ import CloseLocButton from './CloseLocButton';
import { OpenLoc } from 'src/__mocks__/LogionClientMock';
import { mockSubmittableResult } from 'src/logion-chain/__mocks__/SignatureMock';
import { LocItem, MetadataItem } from './LocItem';
import { Hash } from '@logion/node-api';

jest.mock("../logion-chain");
jest.mock("./LocContext");
Expand Down Expand Up @@ -121,9 +120,8 @@ function metadataItem(status: ItemStatus): LocItem {
template: false,
},
{
name: "Test",
value: "Test",
nameHash: Hash.of("Test"),
name: HashString.fromValue("Test"),
value: HashString.fromValue("Test"),
}
);
}
10 changes: 5 additions & 5 deletions src/loc/LocContext.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
LocData,
PublicApi,
EditableRequest as RealEditableRequest,
HashString,
LocRequestState as RealLocRequestState
} from "@logion/client";
import { LogionClient } from '@logion/client/dist/LogionClient.js';
Expand Down Expand Up @@ -251,13 +252,12 @@ async function thenItemsAdded() {
}

function givenDraftItems() {
const metadataName = "New data";
const metadataName = HashString.fromValue("New data");
_locData.metadata.push({
addedOn: "",
name: metadataName,
nameHash: Hash.of(metadataName),
submitter: mockValidPolkadotAccountId(OPEN_IDENTITY_LOC.owner),
value: "Some value",
value: HashString.fromValue("Some value"),
published: false,
status: "DRAFT",
acknowledgedByOwner: false,
Expand All @@ -268,7 +268,7 @@ function givenDraftItems() {
addedOn: "",
name: "New file",
submitter: mockValidPolkadotAccountId(OPEN_IDENTITY_LOC.owner),
nature: "Some nature",
nature: HashString.fromValue("Some nature"),
published: false,
restrictedDelivery: false,
contentType: "text/plain",
Expand All @@ -279,7 +279,7 @@ function givenDraftItems() {
})
_locData.links.push({
addedOn: "",
nature: "New link",
nature: HashString.fromValue("New link"),
target: _linkedLocData.id.toString(),
submitter: mockValidPolkadotAccountId(OPEN_IDENTITY_LOC.owner),
published: false,
Expand Down
6 changes: 3 additions & 3 deletions src/loc/LocDetailsTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -153,23 +153,23 @@ export function LocDetailsTabContent(props: ContentProps) {
const templateLinks = new Set();
if(loc.status !== "CLOSED") {
for(const documentTemplate of theTemplate.documents) {
const file = loc.files.find(item => item.nature === documentTemplate.publicDescription);
const file = loc.files.find(item => item.nature.validValue() === documentTemplate.publicDescription);
if(file) {
templateDocuments.add(file.nature);
}
items.push(createDocumentTemplateItem(documentTemplate, file));
}

for(const dataTemplate of theTemplate.metadata) {
const data = loc.metadata.find(item => item.name === dataTemplate.name);
const data = loc.metadata.find(item => item.name.validValue() === dataTemplate.name);
if(data) {
templateItems.add(data.name);
}
items.push(createMetadataTemplateItem(dataTemplate, data));
}

for(const linkTemplate of theTemplate.links) {
const link = loc.links.find(item => item.nature === linkTemplate.publicDescription);
const link = loc.links.find(item => item.nature.validValue() === linkTemplate.publicDescription);
let linkData: LinkData | undefined;
if(link) {
templateLinks.add(link.nature);
Expand Down
16 changes: 8 additions & 8 deletions src/loc/LocItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
PublicLoc,
ItemStatus,
ReviewResult,
HashString,
} from "@logion/client";
import { UUID, LocType, Fees, ValidAccountId, Hash } from "@logion/node-api";
import { useCallback } from "react";
Expand Down Expand Up @@ -39,9 +40,8 @@ export interface LinkData {
}

export interface MetadataData {
readonly name?: string;
readonly nameHash: Hash;
readonly value: string;
readonly name: HashString;
readonly value: HashString;
}

export interface FileData {
Expand Down Expand Up @@ -169,7 +169,7 @@ export class MetadataItem extends AbstractLocItem<MetadataData> {

override title() {
if (this.hasData()) {
return this.data().name || "-";
return this.data().name.value || "-";
} else {
return this.defaultTitle;
}
Expand Down Expand Up @@ -382,7 +382,7 @@ export function useDeleteMetadataCallback(mutateLocState: (mutator: (current: Lo
await mutateLocState(async current => {
if(current instanceof EditableRequest) {
return current.deleteMetadata({
nameHash: item.as<MetadataData>().nameHash,
nameHash: item.as<MetadataData>().name.hash,
});
} else {
return current;
Expand Down Expand Up @@ -533,7 +533,7 @@ export async function getLinkData(
return {
linkedLoc,
linkDetailsPath,
nature: link.nature,
nature: link.nature.validValue(),
};
}

Expand All @@ -542,7 +542,7 @@ export function useRequestReviewCallback(mutateLocState: (mutator: (current: Loc
mutateLocState(async current => {
if (current instanceof EditableRequest) {
if (locItem.type === "Data") {
return current.requestMetadataReview({ nameHash: locItem.as<MetadataData>().nameHash });
return current.requestMetadataReview({ nameHash: locItem.as<MetadataData>().name.hash });
} else if (locItem.type === "Document") {
return current.requestFileReview({ hash: locItem.as<FileData>().hash });
} else if (locItem.type === "Linked LOC") {
Expand All @@ -563,7 +563,7 @@ export function useReviewCallback(mutateLocState: (mutator: (current: LocRequest
if(current instanceof EditableRequest) {
if(locItem.type === "Data") {
return current.legalOfficer.reviewMetadata({
nameHash: locItem.as<MetadataData>().nameHash,
nameHash: locItem.as<MetadataData>().name.hash,
decision,
rejectReason
});
Expand Down
8 changes: 3 additions & 5 deletions src/loc/LocItemFactory.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MergedMetadataItem, MergedFile, MergedLink, Fees as ClientFees, toFeesClass } from "@logion/client";
import { MergedMetadataItem, MergedFile, MergedLink, Fees as ClientFees, toFeesClass, HashString } from "@logion/client";
import { FileItem, LinkData, LinkItem, LocItem, MetadataItem } from "./LocItem";
import { LocTemplateDocumentOrLink, LocTemplateMetadataItem } from "./Template";

Expand Down Expand Up @@ -44,7 +44,7 @@ function createDraftFileLocItem(parameters: MergedFile): FileItem {
{
hash: parameters.hash,
fileName: parameters.name,
nature: parameters.nature,
nature: parameters.nature.validValue(),
size: parameters.size,
storageFeePaidBy: parameters.storageFeePaidBy || "",
}
Expand Down Expand Up @@ -80,7 +80,6 @@ function createDraftMetadataLocItem(locItem: MergedMetadataItem): LocItem {
},
{
name: locItem.name,
nameHash: locItem.nameHash,
value: locItem.value,
}
);
Expand Down Expand Up @@ -172,8 +171,7 @@ export function createMetadataTemplateItem(templateItem: LocTemplateMetadataItem
defaultTitle: templateItem.name,
},
locItem ? {
nameHash: locItem.nameHash,
name: templateItem.name,
name: HashString.fromValue(templateItem.name),
value: locItem.value,
} : undefined,
);
Expand Down
16 changes: 7 additions & 9 deletions src/loc/LocItems.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { UUID, Hash } from "@logion/node-api";
import { ItemStatus, LocData } from "@logion/client";
import { ItemStatus, LocData, HashString } from "@logion/client";
import { render as renderTesting, waitFor, screen } from "@testing-library/react";

import { clickByName, render } from "../tests";
Expand Down Expand Up @@ -202,11 +202,10 @@ let _locState: EditableRequest;

function givenMetadataItem(request: LocData, status: ItemStatus): LocItem[] {
request.metadata.push({
name: "Name",
nameHash: Hash.of("Name"),
name: HashString.fromValue("Name"),
addedOn: "2022-01-20T15:45:00.000",
submitter: accounts!.current!.accountId,
value: "Value",
value: HashString.fromValue("Value"),
published: status === "PUBLISHED",
status,
acknowledgedByOwner: status === "ACKNOWLEDGED",
Expand All @@ -222,9 +221,8 @@ function givenMetadataItem(request: LocData, status: ItemStatus): LocItem[] {
template: false,
},
{
name: "Name",
value: "Value",
nameHash: Hash.of("Name"),
name: HashString.fromValue("Name"),
value: HashString.fromValue("Value"),
}
)];
}
Expand All @@ -245,7 +243,7 @@ function givenFileItem(request: LocData, status: ItemStatus): LocItem[] {
name: "Name",
addedOn: "2022-01-20T15:45:00.000",
submitter: accounts!.current!.accountId,
nature: "Some nature",
nature: HashString.fromValue("Some nature"),
hash: Hash.fromHex("0xfb45e95061306e90fd154272ba3b4d67bb6d295feeccdc3a34572995f08e268a"),
published: status === "PUBLISHED",
restrictedDelivery: false,
Expand Down Expand Up @@ -293,7 +291,7 @@ function givenLinkItem(request: LocData, status: ItemStatus): LocItem[] {
request.links.push({
addedOn: "2022-01-20T15:45:00.000",
submitter: accounts!.current!.accountId,
nature: "Some nature",
nature: HashString.fromValue("Some nature"),
target: targetId.toString(),
published: status === "PUBLISHED",
status,
Expand Down
19 changes: 9 additions & 10 deletions src/loc/LocPublicDataButton.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { TEST_WALLET_USER } from "../wallet-user/TestData";
import { LocPublicDataButton } from "./LocPublicDataButton";
import { MetadataItem, MetadataData } from "./LocItem";
import { setLocItems, setLocState } from "./__mocks__/LocContextMock";
import { Hash } from "@logion/node-api";
import { HashString } from "@logion/client";

jest.mock("./LocContext");
jest.mock("./UserLocContext");

const value = "Value";
const value = HashString.fromValue("Value");
const existingItem = new MetadataItem(
{
status: "DRAFT",
Expand All @@ -22,8 +22,7 @@ const existingItem = new MetadataItem(
template: false,
},
{
name: "Name",
nameHash: Hash.of("Name"),
name: HashString.fromValue("Name"),
value,
}
);
Expand Down Expand Up @@ -64,8 +63,8 @@ async function testAddsMetadata(component: React.ReactElement, addMetadata: jest

await clickAdd();
const modal = screen.getByRole("dialog");
await typeByLabel("Public data name (No confidential or personal information)", existingItem.data().name || "");
await typeByLabel("Public data (No confidential or personal information)", existingItem.data().value);
await typeByLabel("Public data name (No confidential or personal information)", existingItem.data().name.validValue());
await typeByLabel("Public data (No confidential or personal information)", existingItem.data().value.validValue());
await clickByName("Submit");
await waitFor(() => expect(addMetadata).toBeCalled());
await waitFor(() => expect(modal).not.toBeInTheDocument());
Expand All @@ -80,8 +79,8 @@ async function testDoesNothingOnCancel(component: React.ReactElement, addMetadat

await clickAdd();
const modal = screen.getByRole("dialog");
await typeByLabel("Public data name (No confidential or personal information)", existingItem.data().name || "");
await typeByLabel("Public data (No confidential or personal information)", existingItem.data().value);
await typeByLabel("Public data name (No confidential or personal information)", existingItem.data().name.validValue());
await typeByLabel("Public data (No confidential or personal information)", existingItem.data().value.validValue());
await clickByName("Cancel");
expect(addMetadata).not.toBeCalled();
await waitFor(() => expect(modal).not.toBeInTheDocument());
Expand All @@ -92,8 +91,8 @@ async function testDoesNotNothingIfItemExists(component: React.ReactElement, add

await clickAdd();
const modal = screen.getByRole("dialog");
await typeByLabel("Public data name (No confidential or personal information)", existingItem.as<MetadataData>().name || "");
await typeByLabel("Public data (No confidential or personal information)", existingItem.as<MetadataData>().value);
await typeByLabel("Public data name (No confidential or personal information)", existingItem.as<MetadataData>().name.validValue());
await typeByLabel("Public data (No confidential or personal information)", existingItem.as<MetadataData>().value.validValue());
await clickByName("Submit");
await waitFor(() => expect(addMetadata).not.toBeCalled());
await waitFor(() => expect(modal).not.toBeInTheDocument());
Expand Down
Loading
Loading