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

Fix: Encryption and params on methods and smart inboxes UX #72

Merged
merged 3 commits into from
Nov 8, 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
2 changes: 1 addition & 1 deletion apps/shinkai-app/src/pages/Chat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ const Chat: React.FC = () => {
profile_identity_sk: auth.profile_identity_sk,
});
} else {
sendMessageToInbox({
await sendMessageToInbox({
sender: auth.shinkai_identity,
sender_subidentity: auth.profile,
receiver,
Expand Down
6 changes: 4 additions & 2 deletions apps/shinkai-app/src/pages/CreateChat.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,17 @@ const CreateChat: React.FC = () => {
if (!auth) return;
const [receiver, ...rest] = data.receiver.split('/');

createChat({
await createChat({
sender: auth.shinkai_identity,
senderSubidentity: `${auth.profile}/device/${auth.registration_name}`,
senderSubidentity: auth.profile,
receiver,
receiverSubidentity: rest.join('/'),
message: data.message,
my_device_encryption_sk: auth.my_device_encryption_sk,
my_device_identity_sk: auth.my_device_identity_sk,
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
});
};

Expand Down
6 changes: 2 additions & 4 deletions apps/shinkai-app/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,16 @@ const MessageButton = ({ inbox }: { inbox: SmartInbox }) => {

const onSubmit = async (data: z.infer<typeof updateInboxNameSchema>) => {
if (!auth) return;
console.log('data', data);
await updateInboxName({
sender: auth?.shinkai_identity ?? '',
senderSubidentity: auth?.profile,
receiver: auth.shinkai_identity,
receiverSubidentity: '',
my_device_encryption_sk: auth.my_device_encryption_sk,
my_device_identity_sk: auth.my_device_identity_sk,
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
inboxId: decodeURIComponent(inbox.inbox_id),
inboxId: inbox.inbox_id,
inboxName: data.inboxName,
});
setIsEditable(false);
Expand Down Expand Up @@ -149,7 +147,7 @@ const Home: React.FC = () => {

const { inboxes } = useGetInboxes({
sender: auth?.shinkai_identity ?? '',
senderSubidentity: `${auth?.profile}/device/${auth?.registration_name}`,
senderSubidentity: auth?.profile ?? '',
// Assuming receiver and target_shinkai_name_profile are the same as sender
receiver: auth?.shinkai_identity ?? '',
targetShinkaiNameProfile: `${auth?.shinkai_identity}/${auth?.profile}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ export const CreateInbox = () => {
const [receiver, ...rest] = values.receiverIdentity.split('/');
createChat({
sender: auth.shinkai_identity,
senderSubidentity: `${auth.profile}/device/${auth.registration_name}`,
senderSubidentity: auth.profile,
receiver,
receiverSubidentity: rest.join('/'),
message: values.message,
my_device_encryption_sk: auth.my_device_encryption_sk,
my_device_identity_sk: auth.my_device_identity_sk,
node_encryption_pk: auth.node_encryption_pk,
profile_encryption_sk: auth.profile_encryption_sk,
profile_identity_sk: auth.profile_identity_sk,
});
};

Expand Down
2 changes: 1 addition & 1 deletion apps/shinkai-visor/src/components/inboxes/inboxes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export const Inboxes = () => {
const sender = auth?.shinkai_identity ?? '';
const { inboxes } = useGetInboxes({
sender: auth?.shinkai_identity ?? '',
senderSubidentity: `${auth?.profile}/device/${auth?.registration_name}`,
senderSubidentity: auth?.profile ?? '',
// Assuming receiver and target_shinkai_name_profile are the same as sender
receiver: sender,
targetShinkaiNameProfile: `${auth?.shinkai_identity}/${auth?.profile}`,
Expand Down
69 changes: 11 additions & 58 deletions libs/shinkai-message-ts/src/api/methods.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
}
};

export const fetchPublicKey = () => async (): Promise<any> => {

Check warning on line 36 in libs/shinkai-message-ts/src/api/methods.ts

View workflow job for this annotation

GitHub Actions / health-checks (lint)

Unexpected any. Specify a different type
const apiEndpoint = ApiConfig.getInstance().getEndpoint();
try {
const response = await fetch(`${apiEndpoint}/get_public_key`);
Expand All @@ -50,11 +50,7 @@
receiver: string,
receiver_subidentity: string,
text_message: string,
setupDetailsState: {
my_device_encryption_sk: string;
my_device_identity_sk: string;
node_encryption_pk: string;
}
setupDetailsState: CredentialsPayload
): Promise<{ inboxId: string; message: ShinkaiMessage }> => {
const senderShinkaiName = new ShinkaiNameWrapper(
sender + '/' + sender_subidentity
Expand All @@ -76,8 +72,8 @@

try {
const messageStr = ShinkaiMessageBuilderWrapper.create_chat_with_message(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand Down Expand Up @@ -121,8 +117,8 @@
try {
const messageStr =
ShinkaiMessageBuilderWrapper.send_text_message_with_inbox(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand Down Expand Up @@ -203,8 +199,8 @@
try {
const messageString =
ShinkaiMessageBuilderWrapper.get_all_inboxes_for_profile(
setupDetailsState.my_device_encryption_sk,
setupDetailsState.my_device_identity_sk,
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
sender,
sender_subidentity,
Expand Down Expand Up @@ -236,12 +232,10 @@
sender: string,
sender_subidentity: string,
receiver: string,
receiver_subidentity: string,
setupDetailsState: CredentialsPayload,
inboxName: string,
inboxId: string
// TODO: fix type
): Promise<any> => {
): Promise<{ success: string; data: null }> => {
try {
const messageString =
ShinkaiMessageBuilderWrapper.update_shinkai_inbox_name(
Expand All @@ -251,7 +245,7 @@
sender,
sender_subidentity,
receiver,
"",
'',
inboxId,
inboxName
);
Expand All @@ -266,7 +260,7 @@
});
const data = await response.json();
await handleHttpError(response);
return data.data;
return data;
} catch (error) {
console.error('Error updating inbox name:', error);
throw error;
Expand All @@ -278,7 +272,7 @@
count: number,
lastKey: string | undefined,
setupDetailsState: LastMessagesFromInboxCredentialsPayload
): Promise<any[]> => {
): Promise<ShinkaiMessage[]> => {
try {
const messageStr =
ShinkaiMessageBuilderWrapper.get_last_messages_from_inbox(
Expand Down Expand Up @@ -310,47 +304,6 @@
}
};

export const getLastUnreadMessagesFromInbox = async (
inbox: string,
count: number,
fromKey: string | undefined,
setupDetailsState: LastMessagesFromInboxCredentialsPayload
): Promise<any[]> => {
try {
const messageStr =
ShinkaiMessageBuilderWrapper.get_last_messages_from_inbox(
setupDetailsState.profile_encryption_sk,
setupDetailsState.profile_identity_sk,
setupDetailsState.node_encryption_pk,
inbox,
count,
fromKey,
setupDetailsState.shinkai_identity,
setupDetailsState.profile,
setupDetailsState.shinkai_identity
);

const message = JSON.parse(messageStr);

const apiEndpoint = ApiConfig.getInstance().getEndpoint();
const response = await fetch(
`${apiEndpoint}/v1/last_unread_messages_from_inbox`,
{
method: 'POST',
body: JSON.stringify(message),
headers: { 'Content-Type': 'application/json' },
}
);
await handleHttpError(response);
const data = await response.json();
return data;
// dispatch(receiveUnreadMessagesFromInbox(inbox, results));
} catch (error) {
console.error('Error getting last messages from inbox:', error);
throw error;
}
};

export const submitRequestRegistrationCode = async (
identity_permissions: string,
code_type = 'profile',
Expand Down Expand Up @@ -491,7 +444,7 @@
};

export const createJob = async (
scope: any,

Check warning on line 447 in libs/shinkai-message-ts/src/api/methods.ts

View workflow job for this annotation

GitHub Actions / health-checks (lint)

Unexpected any. Specify a different type
sender: string,
sender_subidentity: string,
receiver: string,
Expand Down
49 changes: 22 additions & 27 deletions libs/shinkai-message-ts/src/wasm/ShinkaiMessageBuilderWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,17 @@ export class ShinkaiMessageBuilderWrapper {
this.wasmBuilder.external_metadata(recipient, sender);
}

external_metadata_with_intra(recipient: string, sender: string, intra_sender: string): void {
this.wasmBuilder.external_metadata_with_intra(recipient, sender, intra_sender);
};
external_metadata_with_intra(
recipient: string,
sender: string,
intra_sender: string
): void {
this.wasmBuilder.external_metadata_with_intra(
recipient,
sender,
intra_sender
);
}

external_metadata_with_other(
recipient: string,
Expand Down Expand Up @@ -271,16 +279,9 @@ export class ShinkaiMessageBuilderWrapper {

builder.message_raw_content(target_shinkai_name_profile);
builder.message_schema_type(MessageSchemaType.TextContent.toString());
builder.internal_metadata(
sender_subidentity,
'',
'None'
);
builder.external_metadata(receiver, sender);
// TODO: fix encryption
builder.body_encryption(
'None'
);
builder.internal_metadata(sender_subidentity, '', 'None');
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand Down Expand Up @@ -435,7 +436,7 @@ export class ShinkaiMessageBuilderWrapper {
sender,
sender_subidentity,
receiver,
""
''
);
}

Expand Down Expand Up @@ -465,7 +466,7 @@ export class ShinkaiMessageBuilderWrapper {
'None'
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption('None');
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand Down Expand Up @@ -499,7 +500,7 @@ export class ShinkaiMessageBuilderWrapper {
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);

builder.body_encryption('None');
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();

Expand All @@ -514,7 +515,7 @@ export class ShinkaiMessageBuilderWrapper {
symmetric_key_sk: string,
sender: string,
sender_subidentity: string,
receiver: string,
receiver: string
): string {
const builder = new ShinkaiMessageBuilderWrapperWASM(
my_encryption_secret_key,
Expand All @@ -523,17 +524,12 @@ export class ShinkaiMessageBuilderWrapper {
);

builder.message_raw_content(symmetric_key_sk);
builder.message_schema_type(MessageSchemaType.SymmetricKeyExchange.toString());
builder.internal_metadata_with_inbox(
sender_subidentity,
'',
inbox,
'None'
builder.message_schema_type(
MessageSchemaType.SymmetricKeyExchange.toString()
);
builder.internal_metadata_with_inbox(sender_subidentity, '', inbox, 'None');
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
builder.body_encryption(
'DiffieHellmanChaChaPoly1305'
);
builder.body_encryption('DiffieHellmanChaChaPoly1305');
const message = builder.build_to_string();
return message;
}
Expand Down Expand Up @@ -563,7 +559,6 @@ export class ShinkaiMessageBuilderWrapper {
'None'
);
builder.external_metadata_with_intra(receiver, sender, sender_subidentity);
// TODO: fix encryption
builder.body_encryption('DiffieHellmanChaChaPoly1305');

const message = builder.build_to_string();
Expand Down
8 changes: 6 additions & 2 deletions libs/shinkai-node-state/src/lib/mutations/createChat/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createChatWithMessage } from "@shinkai_network/shinkai-message-ts/api";
import { createChatWithMessage } from '@shinkai_network/shinkai-message-ts/api';

import { CreateChatInput } from "./types";
import { CreateChatInput } from './types';

export const createChat = async ({
sender,
Expand All @@ -11,6 +11,8 @@ export const createChat = async ({
my_device_encryption_sk,
my_device_identity_sk,
node_encryption_pk,
profile_encryption_sk,
profile_identity_sk,
}: CreateChatInput) => {
const response = await createChatWithMessage(
sender,
Expand All @@ -22,6 +24,8 @@ export const createChat = async ({
my_device_encryption_sk,
my_device_identity_sk,
node_encryption_pk,
profile_encryption_sk,
profile_identity_sk,
}
);
return response;
Expand Down
7 changes: 2 additions & 5 deletions libs/shinkai-node-state/src/lib/mutations/createChat/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import type {
CredentialsPayload,
ShinkaiMessage,
} from "@shinkai_network/shinkai-message-ts/models";
} from '@shinkai_network/shinkai-message-ts/models';

export type CreateChatInput = Pick<
CredentialsPayload,
"my_device_encryption_sk" | "my_device_identity_sk" | "node_encryption_pk"
> & {
export type CreateChatInput = CredentialsPayload & {
sender: string;
senderSubidentity: string;
receiver: string;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import type { UseMutationOptions } from "@tanstack/react-query";
import { useMutation } from "@tanstack/react-query";
import type { UseMutationOptions } from '@tanstack/react-query';
import { useMutation } from '@tanstack/react-query';

import { FunctionKey, queryClient } from "../../constants";
import { createChat } from ".";
import { CreateChatInput, CreateChatOutput } from "./types";
import { FunctionKey, queryClient } from '../../constants';
import { createChat } from '.';
import { CreateChatInput, CreateChatOutput } from './types';

type Options = UseMutationOptions<CreateChatOutput, Error, CreateChatInput>;

Expand Down
Loading
Loading