forked from chainapsis/keplr-wallet
-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feature: Messaging release v0.4 (#149)
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
1 parent
db49c75
commit d220dd6
Showing
102 changed files
with
10,087 additions
and
4,467 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
.vscode | ||
node_modules | ||
dist | ||
dev-user1 | ||
dev-user2 | ||
releases | ||
prod | ||
.gitsecret/keys/random_seed | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export const ROUTE = "messaging"; | ||
export const MESSAGE_CHANNEL_ID = "MESSAGING"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./service"; | ||
export * from "./messages"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from "./service"; | ||
export * from "./init"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; | ||
} | ||
}; |
Oops, something went wrong.