Skip to content

Commit

Permalink
Feature: Messaging release v0.4 (#149)
Browse files Browse the repository at this point in the history
Co-authored-by: imvinay84 <84082477+imvinay84@users.noreply.github.com>
Co-authored-by: sumit <dev123@gmail.com>
Co-authored-by: Edward FitzGerald <edward.fitzgerald@fetch.ai>
Co-authored-by: Pratap Mahey <pratap.mahey@fetch.ai>
Co-authored-by: imvinay84 <vsvinaysingh82@gmail.com>
  • Loading branch information
6 people authored Oct 27, 2022
1 parent db49c75 commit d220dd6
Show file tree
Hide file tree
Showing 102 changed files with 10,087 additions and 4,467 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
branches:
- master
- release/*
- integration/*

pull_request:
branches:
- master
- release/*
- integration/*

jobs:
lint:
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@ on:
branches:
- master
- release/*
- integration/*

pull_request:
branches:
- master
- release/*
- integration/*

jobs:
test:
Expand Down
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
.vscode
node_modules
dist
dev-user1
dev-user2
releases
prod
.gitsecret/keys/random_seed
Expand Down
4 changes: 2 additions & 2 deletions docs/account_management.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ Using the wallet, you can create a new account and address on the Fetch ledger:
If you have an account on the Fetch network, for example having had one already on the Fetch wallet and want to access it again, have an account on another wallet (e.g. Cosmostation, Keplr, ...) and wish to bring it to the Fetch wallet, or having created an address using one of our tools (e.g. the [AEA framework](https://docs.fetch.ai/aea)), you can import it into the Fetch wallet:

1. On the [welcome page](#welcome-page), click **Import existing account**.
2. Enter your mnemonic seed (set of words) or private key (hexadecimal).
2. Enter your mnemonic seed (set of words) or private key (hexidecimal).

!!! warning
**KEEP IT SAFE!** Anyone with your mnemonic seed or private key can access your wallet and take your assets.
Expand Down Expand Up @@ -82,7 +82,7 @@ To remove an account from your Fetch wallet:
4. Enter your wallet password.

!!! warning
If you have not yet backed up your mnemonic seed, click on **Back-up account** and enter your password to view it.<br />
If you have not yet backed up your mnemonic seed, click on **Back-up account** and enter you password to view it.<br />
Then back it up safely. If you lose your mnemonic seed you will lose access to your account.

5. Click **Confirm** to remove the account from your wallet.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"license": "Apache-2.0",
"devDependencies": {
"@octokit/core": "^3.5.1",
"@types/domhandler": "^2.4.2",
"@types/jest": "^26.0.22",
"@types/node": "^16.6.2",
"@types/webpack": "^4.39.1",
Expand Down
7 changes: 5 additions & 2 deletions packages/background/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@
"@fetchai/blst-ts": "^0.3.1"
},
"dependencies": {
"@cosmjs/launchpad": "^0.24.0-alpha.25",
"@cosmjs/proto-signing": "^0.24.0-alpha.25",
"@apollo/client": "^3.6.9",
"eciesjs": "^0.3.15",
"@cosmjs/launchpad": "^0.27.1",
"@cosmjs/encoding": "^0.28.13",
"@cosmjs/proto-signing": "^0.28.13",
"@ethersproject/bytes": "^5.5.0",
"@ethersproject/keccak256": "^5.5.0",
"@ethersproject/wallet": "^5.5.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/background/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as Tokens from "./tokens/internal";
import * as Interaction from "./interaction/internal";
import * as Permission from "./permission/internal";
import * as Umbral from "./umbral/internal";
import * as Messaging from "./messaging/internal";

export * from "./persistent-memory";
export * from "./chains";
Expand Down Expand Up @@ -117,6 +118,9 @@ export function init(
const umbralService = container.resolve(Umbral.UmbralService);
Umbral.init(router, umbralService);

const messagingService = container.resolve(Messaging.MessagingService);
Messaging.init(router, messagingService);

const backgroundTxService = container.resolve(
BackgroundTx.BackgroundTxService
);
Expand Down
2 changes: 2 additions & 0 deletions packages/background/src/messaging/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const ROUTE = "messaging";
export const MESSAGE_CHANNEL_ID = "MESSAGING";
104 changes: 104 additions & 0 deletions packages/background/src/messaging/handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { Env, Handler, InternalHandler, Message } from "@keplr-wallet/router";
import {
DecryptMessagingMessage,
EncryptMessagingMessage,
GetMessagingPublicKey,
RegisterPublicKey,
SignMessagingPayload,
} from "./messages";
import { MessagingService } from "./service";

export const getHandler: (service: MessagingService) => Handler = (service) => {
return (env: Env, msg: Message<unknown>) => {
switch (msg.constructor) {
case GetMessagingPublicKey:
return handleGetMessagingPublicKey(service)(
env,
msg as GetMessagingPublicKey
);

case RegisterPublicKey:
return handleRegisterPublicKey(service)(env, msg as RegisterPublicKey);

case EncryptMessagingMessage:
return handleEncryptMessagingMessage(service)(
env,
msg as EncryptMessagingMessage
);

case DecryptMessagingMessage:
return handleDecryptMessagingMessage(service)(
env,
msg as DecryptMessagingMessage
);

case SignMessagingPayload:
return handleSignMessagingPayload(service)(
env,
msg as SignMessagingPayload
);
default:
throw new Error("Unknown msg type");
}
};
};

const handleGetMessagingPublicKey: (
service: MessagingService
) => InternalHandler<GetMessagingPublicKey> = (service) => {
return async (env, msg) => {
const pubKey = await service.getPublicKey(
env,
msg.chainId,
msg.targetAddress,
msg.accessToken
);
return pubKey;
};
};

const handleRegisterPublicKey: (
service: MessagingService
) => InternalHandler<RegisterPublicKey> = (service) => {
return async (env, msg) => {
return await service.registerPublicKey(
env,
msg.chainId,
msg.address,
msg.accessToken,
msg.privacySetting
);
};
};

const handleEncryptMessagingMessage: (
service: MessagingService
) => InternalHandler<EncryptMessagingMessage> = (service) => {
return async (env, msg) => {
console.log("msg encryptMessage", msg);

return await service.encryptMessage(
env,
msg.chainId,
msg.targetAddress,
msg.message,
msg.accessToken
);
};
};

const handleDecryptMessagingMessage: (
service: MessagingService
) => InternalHandler<DecryptMessagingMessage> = (service) => {
return async (env, msg) => {
return await service.decryptMessage(env, msg.chainId, msg.cipherText);
};
};

const handleSignMessagingPayload: (
service: MessagingService
) => InternalHandler<SignMessagingPayload> = (service) => {
return async (env, msg) => {
return await service.sign(env, msg.chainId, msg.payload);
};
};
2 changes: 2 additions & 0 deletions packages/background/src/messaging/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./service";
export * from "./messages";
21 changes: 21 additions & 0 deletions packages/background/src/messaging/init.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Router } from "@keplr-wallet/router";
import { ROUTE } from "./constants";
import { getHandler } from "./handler";
import { MessagingService } from "./service";
import {
DecryptMessagingMessage,
EncryptMessagingMessage,
GetMessagingPublicKey,
RegisterPublicKey,
SignMessagingPayload,
} from "./messages";

export function init(router: Router, service: MessagingService): void {
router.registerMessage(GetMessagingPublicKey);
router.registerMessage(RegisterPublicKey);
router.registerMessage(EncryptMessagingMessage);
router.registerMessage(DecryptMessagingMessage);
router.registerMessage(SignMessagingPayload);

router.addHandler(ROUTE, getHandler(service));
}
2 changes: 2 additions & 0 deletions packages/background/src/messaging/internal.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./service";
export * from "./init";
95 changes: 95 additions & 0 deletions packages/background/src/messaging/memorandum-client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import {
ApolloClient,
InMemoryCache,
gql,
DefaultOptions,
} from "@apollo/client";
import { PrivacySetting, PubKey } from "./types";

const defaultOptions: DefaultOptions = {
watchQuery: {
fetchPolicy: "no-cache",
errorPolicy: "ignore",
},
query: {
fetchPolicy: "no-cache",
errorPolicy: "all",
},
};

const client = new ApolloClient({
uri: "https://messaging-server.sandbox-london-b.fetch-ai.com/graphql",
cache: new InMemoryCache(),
defaultOptions,
});

export const registerPubKey = async (
accessToken: string,
messagingPubKey: string,
walletAddress: string,
privacySetting: PrivacySetting,
channelId: string
): Promise<void> => {
try {
await client.mutate({
mutation: gql(`mutation Mutation($publicKeyDetails: InputPublicKey!) {
updatePublicKey(publicKeyDetails: $publicKeyDetails) {
publicKey
privacySetting
}
}`),
variables: {
publicKeyDetails: {
publicKey: messagingPubKey,
address: walletAddress,
channelId,
privacySetting,
},
},
context: {
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
});
} catch (e) {
console.log(e);
}
};

export const getPubKey = async (
accessToken: string,
targetAddress: string,
channelId: string
): Promise<PubKey> => {
try {
const { data } = await client.query({
query: gql(`query Query($address: String!, $channelId: ChannelId!) {
publicKey(address: $address, channelId: $channelId) {
publicKey
privacySetting
}
}`),
variables: {
address: targetAddress,
channelId,
},
context: {
headers: {
Authorization: `Bearer ${accessToken}`,
},
},
});

return {
publicKey: data.publicKey && data.publicKey.publicKey,
privacySetting: data.publicKey && data.publicKey.privacySetting,
};
} catch (e) {
console.log(e);
return {
publicKey: undefined,
privacySetting: undefined,
};
}
};
Loading

0 comments on commit d220dd6

Please sign in to comment.