Skip to content

Commit

Permalink
fix: messaging server url picked dynamically (#255)
Browse files Browse the repository at this point in the history
  • Loading branch information
agent-dominatrix authored Apr 25, 2023
1 parent fad3c68 commit 34f5276
Show file tree
Hide file tree
Showing 12 changed files with 93 additions and 15 deletions.
3 changes: 3 additions & 0 deletions packages/background/src/messaging/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ const handleGetMessagingPublicKey: (
return async (env, msg) => {
const pubKey = await service.getPublicKey(
env,
msg.memorendumUrl,
msg.chainId,
msg.targetAddress,
msg.accessToken
Expand All @@ -63,6 +64,7 @@ const handleRegisterPublicKey: (
return async (env, msg) => {
return await service.registerPublicKey(
env,
msg.memorendumUrl,
msg.chainId,
msg.address,
msg.accessToken,
Expand All @@ -80,6 +82,7 @@ const handleEncryptMessagingMessage: (

return await service.encryptMessage(
env,
msg.memorendumUrl,
msg.chainId,
msg.targetAddress,
msg.message,
Expand Down
21 changes: 15 additions & 6 deletions packages/background/src/messaging/memorandum-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@ const defaultOptions: DefaultOptions = {
};
// export const MESSAGING_SERVER =
// "https://messaging-server.sandbox-london-b.fetch-ai.com/graphql";
export const MESSAGING_SERVER = "https://messaging.fetch-ai.network/graphql";
// export const MESSAGING_SERVER = "https://messaging.fetch-ai.network/graphql";
// export const MESSAGING_SERVER = "http://localhost:4000/graphql";
const client = new ApolloClient({
uri: MESSAGING_SERVER,
cache: new InMemoryCache(),
defaultOptions,
});

const inMemCache = new InMemoryCache();

const getClient = (memorandumUrl: string) => {
return new ApolloClient({
uri: memorandumUrl,
cache: inMemCache,
defaultOptions,
});
};

export const registerPubKey = async (
memorandumURl: string,
accessToken: string,
messagingPubKey: string,
walletAddress: string,
Expand All @@ -39,6 +45,7 @@ export const registerPubKey = async (
signedObjBase64?: string
): Promise<void> => {
try {
const client = getClient(memorandumURl);
await client.mutate({
mutation: gql(`mutation Mutation($publicKeyDetails: InputPublicKey!) {
updatePublicKey(publicKeyDetails: $publicKeyDetails) {
Expand Down Expand Up @@ -73,12 +80,14 @@ export const registerPubKey = async (
};

export const getPubKey = async (
memorandumURl: string,
accessToken: string,
targetAddress: string,
channelId: string,
chainId: string
): Promise<PubKey> => {
try {
const client = getClient(memorandumURl);
const { data } = await client.query({
query: gql(`query Query($address: String!, $chainId: String! $channelId: ChannelId!) {
publicKey(address: $address, chainId: $chainId, channelId: $channelId) {
Expand Down
3 changes: 3 additions & 0 deletions packages/background/src/messaging/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class GetMessagingPublicKey extends Message<PubKey> {
}

constructor(
public readonly memorendumUrl: string,
public readonly chainId: string,
public readonly accessToken: string,
public readonly targetAddress: string | null
Expand Down Expand Up @@ -36,6 +37,7 @@ export class RegisterPublicKey extends Message<PubKey> {
}

constructor(
public readonly memorendumUrl: string,
public readonly chainId: string,
public readonly accessToken: string,
public readonly address: string,
Expand Down Expand Up @@ -66,6 +68,7 @@ export class EncryptMessagingMessage extends Message<string> {
}

constructor(
public readonly memorendumUrl: string,
public readonly chainId: string,
public readonly targetAddress: string,
public readonly message: string,
Expand Down
21 changes: 19 additions & 2 deletions packages/background/src/messaging/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export class MessagingService {
*/
public async getPublicKey(
env: Env,
memorandumUrl: string,
chainId: string,
targetAddress: string | null,
accessToken: string
Expand All @@ -44,7 +45,12 @@ export class MessagingService {
chatReadReceiptSetting: true,
};
} else {
return await this.lookupPublicKey(accessToken, targetAddress, chainId);
return await this.lookupPublicKey(
memorandumUrl,
accessToken,
targetAddress,
chainId
);
}
}

Expand All @@ -59,6 +65,7 @@ export class MessagingService {
*/
public async registerPublicKey(
env: Env,
memorandumUrl: string,
chainId: string,
address: string,
accessToken: string,
Expand All @@ -69,7 +76,12 @@ export class MessagingService {
const privateKey = new PrivateKey(Buffer.from(sk));
const pubKey = toHex(privateKey.publicKey.compressed);

const regPubKey = await this.lookupPublicKey(accessToken, address, chainId);
const regPubKey = await this.lookupPublicKey(
memorandumUrl,
accessToken,
address,
chainId
);
if (
!regPubKey.privacySetting ||
!regPubKey.publicKey ||
Expand Down Expand Up @@ -117,6 +129,7 @@ export class MessagingService {
}

await registerPubKey(
memorandumUrl,
accessToken,
pubKey,
address,
Expand Down Expand Up @@ -177,6 +190,7 @@ export class MessagingService {
*/
async encryptMessage(
_env: Env,
memorandumUrl: string,
_chainId: string,
targetAddress: string,
message: string,
Expand All @@ -185,6 +199,7 @@ export class MessagingService {
const rawMessage = Buffer.from(fromBase64(message));

const targetPublicKey = await this.lookupPublicKey(
memorandumUrl,
accessToken,
targetAddress,
_chainId
Expand Down Expand Up @@ -231,6 +246,7 @@ export class MessagingService {
* @protected
*/
protected async lookupPublicKey(
memorandumUrl: string,
accessToken: string,
targetAddress: string,
chainId: string
Expand All @@ -247,6 +263,7 @@ export class MessagingService {
// Step 2. Cache miss, fetch the public key from the memorandum service and
// update the cache
targetPublicKey = await getPubKey(
memorandumUrl,
accessToken,
targetAddress,
MESSAGE_CHANNEL_ID,
Expand Down
2 changes: 2 additions & 0 deletions packages/extension/src/components/chat/chat-init-popup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
import privacyIcon from "@assets/hello.png";
import { useStore } from "../../stores";
import style from "./style.module.scss";
import { GRAPHQL_URL } from "../../config.ui.var";

export const ChatInitPopup = ({
openDialog,
Expand Down Expand Up @@ -52,6 +53,7 @@ export const ChatInitPopup = ({
const messagingPubKey = await requester.sendMessage(
BACKGROUND_PORT,
new RegisterPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
current.chainId,
userState.accessToken,
walletAddress,
Expand Down
2 changes: 2 additions & 0 deletions packages/extension/src/pages/setting/chat/privacy/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { HeaderLayout } from "@layouts/index";
import { useStore } from "../../../../stores";
import { PageButton } from "../../page-button";
import style from "./style.module.scss";
import { GRAPHQL_URL } from "../../../../config.ui.var";

export const Privacy: FunctionComponent = observer(() => {
// const language = useLanguage();
Expand Down Expand Up @@ -46,6 +47,7 @@ export const Privacy: FunctionComponent = observer(() => {
const messagingPubKey = await requester.sendMessage(
BACKGROUND_PORT,
new RegisterPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainStore.current.chainId,
userState.accessToken,
walletAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { PageButton } from "../../page-button";
import { useStore } from "../../../../stores";
import style from "./style.module.scss";
import amplitude from "amplitude-js";
import { GRAPHQL_URL } from "../../../../config.ui.var";

export const ReadRecipt: FunctionComponent = observer(() => {
const history = useHistory();
Expand Down Expand Up @@ -45,6 +46,7 @@ export const ReadRecipt: FunctionComponent = observer(() => {
const messagingPubKey = await requester.sendMessage(
BACKGROUND_PORT,
new RegisterPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainStore.current.chainId,
userState.accessToken,
walletAddress,
Expand Down
3 changes: 2 additions & 1 deletion packages/extension/src/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { InExtensionMessageRequester } from "@keplr-wallet/router-extension";
import { rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
import { toBase64, Bech32, fromHex } from "@cosmjs/encoding";
import { serializeSignDoc } from "@cosmjs/launchpad";
import { GRAPHQL_URL } from "../config.ui.var";

declare let window: Window;

Expand Down Expand Up @@ -86,7 +87,7 @@ export const getJWT = async (chainId: string, url: string) => {

const pubKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, "", null)
new GetMessagingPublicKey(GRAPHQL_URL.MESSAGING_SERVER, chainId, "", null)
);

if (!pubKey.publicKey) throw new Error("public key not found");
Expand Down
24 changes: 21 additions & 3 deletions packages/extension/src/utils/encrypt-group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
decryptEncryptedSymmetricKey,
encryptGroupData,
} from "./symmetric-key";
import { GRAPHQL_URL } from "../config.ui.var";

export interface GroupTimestampUpdateEnvelope {
data: string; // base64 encoded
Expand Down Expand Up @@ -78,12 +79,22 @@ export async function encryptGroupTimestampToEnvelope(
// lookup both our (sender) and target public keys
const senderPublicKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, senderAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
senderAddress
)
);

const targetPublicKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, targetAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
targetAddress
)
);

if (!senderPublicKey.publicKey || !targetPublicKey.publicKey) {
Expand All @@ -100,6 +111,7 @@ export async function encryptGroupTimestampToEnvelope(
const senderCipher = await requester.sendMessage(
BACKGROUND_PORT,
new EncryptMessagingMessage(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
senderAddress,
toBase64(toUtf8(JSON.stringify(message))),
Expand All @@ -111,6 +123,7 @@ export async function encryptGroupTimestampToEnvelope(
const targetCipher = await requester.sendMessage(
BACKGROUND_PORT,
new EncryptMessagingMessage(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
targetAddress,
toBase64(toUtf8(JSON.stringify(message))),
Expand Down Expand Up @@ -175,7 +188,12 @@ export async function encryptGroupMessageToEnvelope(
// lookup both our (sender) and target public keys
const senderPublicKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, senderAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
senderAddress
)
);

if (!senderPublicKey.publicKey) {
Expand Down
17 changes: 15 additions & 2 deletions packages/extension/src/utils/encrypt-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
SignMessagingPayload,
} from "@keplr-wallet/background/build/messaging";
import { MESSAGE_CHANNEL_ID } from "@keplr-wallet/background/build/messaging/constants";
import { GRAPHQL_URL } from "../config.ui.var";

export interface MessageEnvelope {
data: string; // base64 encoded
Expand Down Expand Up @@ -70,12 +71,22 @@ export async function encryptToEnvelope(
// lookup both our (sender) and target public keys
const senderPublicKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, senderAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
senderAddress
)
);

const targetPublicKey = await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, targetAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
targetAddress
)
);

if (!senderPublicKey.publicKey || !targetPublicKey.publicKey) {
Expand All @@ -99,6 +110,7 @@ export async function encryptToEnvelope(
const senderCipher = await requester.sendMessage(
BACKGROUND_PORT,
new EncryptMessagingMessage(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
senderAddress,
toBase64(toUtf8(JSON.stringify(message))),
Expand All @@ -110,6 +122,7 @@ export async function encryptToEnvelope(
const targetCipher = await requester.sendMessage(
BACKGROUND_PORT,
new EncryptMessagingMessage(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
targetAddress,
toBase64(toUtf8(JSON.stringify(message))),
Expand Down
8 changes: 7 additions & 1 deletion packages/extension/src/utils/fetch-public-key.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BACKGROUND_PORT } from "@keplr-wallet/router";
import { InExtensionMessageRequester } from "@keplr-wallet/router-extension";
import { GetMessagingPublicKey } from "@keplr-wallet/background/build/messaging";
import { GRAPHQL_URL } from "../config.ui.var";

export const fetchPublicKey = async (
accessToken: string,
Expand All @@ -11,7 +12,12 @@ export const fetchPublicKey = async (
const requester = new InExtensionMessageRequester();
return await requester.sendMessage(
BACKGROUND_PORT,
new GetMessagingPublicKey(chainId, accessToken, targetAddress)
new GetMessagingPublicKey(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
accessToken,
targetAddress
)
);
} catch (error: any) {
console.log(error);
Expand Down
2 changes: 2 additions & 0 deletions packages/extension/src/utils/symmetric-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { InExtensionMessageRequester } from "@keplr-wallet/router-extension";
import crypto from "crypto";
import { GroupMembers } from "@chatTypes";
import { decryptMessageContent } from "./decrypt-message";
import { GRAPHQL_URL } from "../config.ui.var";

function generateSymmetricKey() {
const secret = "fetchwallet";
Expand Down Expand Up @@ -34,6 +35,7 @@ export async function encryptSymmetricKey(
) {
const requester = new InExtensionMessageRequester();
const encryptMsg = new EncryptMessagingMessage(
GRAPHQL_URL.MESSAGING_SERVER,
chainId,
address,
toBase64(toUtf8(JSON.stringify(symmetricKey))),
Expand Down

0 comments on commit 34f5276

Please sign in to comment.