Skip to content

Commit

Permalink
feat: add post hash debug for post author
Browse files Browse the repository at this point in the history
  • Loading branch information
Jack-Works committed Sep 23, 2019
1 parent 243f208 commit eb85c84
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 1 deletion.
90 changes: 90 additions & 0 deletions src/components/DebugModeUI/PostHashDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import React, { useState } from 'react'
import { makeStyles } from '@material-ui/core/styles'
import Button from '@material-ui/core/Button'
import List from '@material-ui/core/List'
import ListItem from '@material-ui/core/ListItem'
import ListItemAvatar from '@material-ui/core/ListItemAvatar'
import ListItemText from '@material-ui/core/ListItemText'
import DialogTitle from '@material-ui/core/DialogTitle'
import Dialog from '@material-ui/core/Dialog'
import { blue } from '@material-ui/core/colors'
import { PortalShadowRoot } from '../../utils/jss/ShadowRootPortal'
import { useFriendsList } from '../DataSource/useActivatedUI'
import { Avatar } from '../../utils/components/Avatar'
import { Person } from '../../database'
import { useAsync } from '../../utils/components/AsyncComponent'
import Services from '../../extension/service'
import { PostIVIdentifier } from '../../database/type'
import { deconstructPayload, Payload } from '../../utils/type-transform/Payload'
import { DialogContentText, DialogContent } from '@material-ui/core'

const useStyles = makeStyles({
avatar: {
backgroundColor: blue[100],
color: blue[600],
},
})

export interface SimpleDialogProps {
open: boolean
onClose: (value: string) => void
friends: Person[]
hashMap: [string, string][]
}

function SimpleDialog(props: SimpleDialogProps) {
const { open } = props

const map = new Map<string, string>()
for (const data of props.hashMap) {
map.set(data[0], data[1])
}

return (
<Dialog container={PortalShadowRoot} onClose={props.onClose} open={open}>
<DialogTitle>
Post hash for each of your friends (Appear in this list is not related to if you have shared this post
to someone or not.)
</DialogTitle>
<List>
{props.friends.map(one => {
return (
<ListItem>
<ListItemAvatar>
<Avatar person={one} />
</ListItemAvatar>
<ListItemText
primary={one.nickname || one.identifier.userId}
secondary={
<span style={{ fontFamily: 'monospace' }}>
{map.get(one.identifier.toText()) || 'Unknown'}
</span>
}
/>
</ListItem>
)
})}
</List>
</Dialog>
)
}

export function DebugModeUI_PostHashDialog(props: { post: string; network: string }) {
const [open, setOpen] = React.useState(false)
const payload = deconstructPayload(props.post)
const [hashMap, setHashMap] = useState<[string, string][]>([])
const friends = useFriendsList()
useAsync(() => {
if (!payload) return Promise.resolve([] as typeof hashMap)
const ivID = new PostIVIdentifier(props.network, payload.iv)
return Services.Crypto.debugShowAllPossibleHashForPost(ivID)
}, [props.post]).then(setHashMap)
return (
<>
<Button variant="outlined" color="primary" onClick={() => setOpen(true)}>
See what code my friends will see for this post
</Button>
<SimpleDialog hashMap={hashMap} friends={friends} open={open} onClose={() => setOpen(false)} />
</>
)
}
5 changes: 4 additions & 1 deletion src/components/InjectedComponents/DecryptedPost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import {
} from '../../extension/background-script/CryptoServices/decryptFrom'
import { useValueRef } from '../../utils/hooks/useValueRef'
import { debugModeSetting } from '../shared-settings/debugMode'
import { DebugModeUI_PostHashDialog } from '../DebugModeUI/PostHashDialog'

interface DecryptPostSuccessProps {
data: { signatureVerifyResult: boolean; content: string }
Expand Down Expand Up @@ -129,7 +130,9 @@ function DecryptPost(props: DecryptPostProps) {
)
const debugHashJSX = (
<ul>
{postBy.equals(whoAmI) ? null : (
{postBy.equals(whoAmI) ? (
<DebugModeUI_PostHashDialog network={postBy.network} post={props.encryptedText} />
) : (
<li>
Hash of this post: {debugHash}
<br />
Expand Down
1 change: 1 addition & 0 deletions src/extension/background-script/CryptoService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export { appendShareTarget } from './CryptoServices/appendShareTarget'
export { getSharedListOfPost } from './CryptoServices/getSharedListOfPost'
export { verifyOthersProve } from './CryptoServices/verifyOthersProve'
export { getMyProveBio } from './CryptoServices/getMyProveBio'
export { debugShowAllPossibleHashForPost } from './CryptoServices/debugShowAllPossibleHashForPost'
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { PostIVIdentifier } from '../../../database/type'
import { queryPeopleDB } from '../../../database/people'
import { hashPostSalt, hashCryptoKey } from '../../../network/gun/version.2/hash'

export async function debugShowAllPossibleHashForPost(post: PostIVIdentifier) {
const friends = await queryPeopleDB(x => x.network === post.network)
return Promise.all(
friends
.filter(x => x.publicKey)
.map(
async x =>
[
x.identifier.toText(),
(await hashPostSalt(post.postIV)) + '-' + (await hashCryptoKey(x.publicKey!)),
] as [string, string],
),
)
}

0 comments on commit eb85c84

Please sign in to comment.