Skip to content

Commit

Permalink
fix: mf-4185 accounts from Firefly are all trusted (#9628)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleBill authored May 18, 2023
1 parent c2c6d51 commit 6e13011
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 59 deletions.
69 changes: 15 additions & 54 deletions packages/plugins/EVM/src/state/IdentityService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,7 @@ import {
type SocialAddress,
type SocialIdentity,
} from '@masknet/shared-base'
import {
ENS,
Firefly,
Lens,
MaskX,
NextIDProof,
NextIDStorageProvider,
RSS3,
SpaceID,
Twitter,
} from '@masknet/web3-providers'
import { ENS, Lens, MaskX, NextIDProof, NextIDStorageProvider, RSS3, SpaceID, Twitter } from '@masknet/web3-providers'
import { captureAsyncTransaction } from '@masknet/web3-providers/helpers'
import { MaskX_BaseAPI } from '@masknet/web3-providers/types'
import { ChainId, isValidAddress, isZeroAddress } from '@masknet/web3-shared-evm'
Expand Down Expand Up @@ -148,7 +138,7 @@ export class IdentityService extends IdentityServiceState<ChainId> {
return this.createSocialAddress(SocialAddressType.Crossbell, info.address, info.crossbell)
}),
)
return compact(allSettled.map((x) => (x.status === 'fulfilled' ? x.value : undefined)).filter(Boolean))
return compact(allSettled.map((x) => (x.status === 'fulfilled' ? x.value : undefined)))
}

/** Read a social address from avatar NextID storage. */
Expand Down Expand Up @@ -228,22 +218,7 @@ export class IdentityService extends IdentityServiceState<ChainId> {
return this.createSocialAddress(SocialAddressType.Lens, profile.ownedBy, name)
}),
)
return compact(allSettled.map((x) => (x.status === 'fulfilled' ? x.value : undefined)).filter(Boolean))
}

private async getSocialAddressFromFirefly({ identifier }: SocialIdentity) {
const lensAccounts = await Firefly.getLensByTwitterId(identifier?.userId)
const names = lensAccounts.map((x) => x.handle)
if (!names.length) return

const allSettled = await Promise.allSettled(
names.map(async (name) => {
const profile = await Lens.getProfileByHandle(name)
if (!profile) return
return this.createSocialAddress(SocialAddressType.Firefly, profile.ownedBy, name)
}),
)
return compact(allSettled.map((x) => (x.status === 'fulfilled' ? x.value : undefined)).filter(Boolean))
return compact(allSettled.map((x) => (x.status === 'fulfilled' ? x.value : undefined)))
}

/** Read a social address from Twitter Blue. */
Expand Down Expand Up @@ -273,6 +248,7 @@ export class IdentityService extends IdentityServiceState<ChainId> {
MaskX_BaseAPI.SourceType.Leaderboard,
MaskX_BaseAPI.SourceType.OpenSea,
MaskX_BaseAPI.SourceType.Sybil,
MaskX_BaseAPI.SourceType.RSS3,
]
const results = response.records.filter((x) => {
if (!isValidAddress(x.web3_addr) || !sourceTypes.includes(x.source)) return false
Expand Down Expand Up @@ -305,7 +281,7 @@ export class IdentityService extends IdentityServiceState<ChainId> {
}

override async getFromRemote(identity: SocialIdentity, includes?: SocialAddressType[]) {
const socialAddressFromFirefly = this.getSocialAddressFromFirefly(identity)
const socialAddressFromMaskX = this.getSocialAddressesFromMaskX(identity)
const allSettled = await Promise.allSettled([
captureAsyncTransaction('getSocialAddressFromBio', this.getSocialAddressFromBio(identity)),
captureAsyncTransaction('getSocialAddressFromENS', this.getSocialAddressFromENS(identity)),
Expand All @@ -317,40 +293,25 @@ export class IdentityService extends IdentityServiceState<ChainId> {
captureAsyncTransaction('getSocialAddressFromCrossbell', this.getSocialAddressFromCrossbell(identity)),
captureAsyncTransaction('getSocialAddressFromTwitterBlue', this.getSocialAddressFromTwitterBlue(identity)),
captureAsyncTransaction('getSocialAddressesFromNextID', this.getSocialAddressesFromNextID(identity)),
captureAsyncTransaction('getSocialAddressesFromMaskX', this.getSocialAddressesFromMaskX(identity)),
captureAsyncTransaction('getSocialAddressesFromMaskX', socialAddressFromMaskX),
captureAsyncTransaction('getSocialAddressFromLens', this.getSocialAddressFromLens(identity)),
captureAsyncTransaction('getSocialAddressFromFirefly', socialAddressFromFirefly),
])
const identities_ = allSettled
.flatMap((x) => (x.status === 'fulfilled' ? x.value : []))
.filter(Boolean) as Array<SocialAddress<ChainId>>
const identities_ = compact(allSettled.flatMap((x) => (x.status === 'fulfilled' ? x.value : [])))

const identities = uniqBy(identities_, (x) => [x.type, x.label, x.address.toLowerCase()].join('_'))
if (!getNextIDPlatform() || !(process.env.channel === 'stable' && process.env.NODE_ENV === 'production')) {
return identities
}
const [identitiesFromNextID, lensAccounts] = await Promise.all([
const [identitiesFromNextID, trustedAccounts] = await Promise.all([
this.getSocialAddressesFromNextID(identity),
socialAddressFromFirefly,
socialAddressFromMaskX,
])
const lensAddresses = lensAccounts?.map((x) => x.address.toLowerCase()) ?? []
const trustedAddresses = trustedAccounts?.map((x) => x.address.toLowerCase()) ?? []
const identitiesAddressesFromNextID = identitiesFromNextID.map((y) => y.address.toLowerCase())
const allSettledTrustedIdentities = await Promise.allSettled(
uniqBy(identities, (x) => x.address.toLowerCase()).map(async (x) => {
const trusted =
lensAddresses.includes(x.address.toLowerCase()) ||
(await NextIDProof.verifyTwitterHandlerByAddress(x.address, identity.identifier?.userId ?? ''))
return { ...x, trusted }
}),
)

return uniqBy(
(
allSettledTrustedIdentities
.flatMap((x) => (x.status === 'fulfilled' && x.value.trusted ? x.value : undefined))
.filter(Boolean) as Array<SocialAddress<ChainId>>
)
.filter((x) => !identitiesAddressesFromNextID.includes(x.address.toLowerCase()))
identities
.filter((x) => {
const address = x.address.toLowerCase()
return !identitiesAddressesFromNextID.includes(address) && trustedAddresses.includes(address)
})
.concat(identitiesFromNextID),
(x) => x.address.toLowerCase(),
)
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/UI/components/AccountIcon/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,16 @@ export function AccountIcon({ socialAccount, classes: externalClasses }: Account
/>
</AccountTooltips>
) : null}

{supportedAddressTypes.includes(SocialAddressType.RSS3) ? (
<AccountTooltips type={SocialAddressType.RSS3}>
<Icons.RSS3
className={cx(classes.actionIcon, classes.icon, classes.roundedIcon)}
size={18}
style={iconStyle}
/>
</AccountTooltips>
) : null}
</>
)
}
12 changes: 8 additions & 4 deletions packages/shared/src/UI/components/AddressItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { first } from 'lodash-es'
import { Icons } from '@masknet/icons'
import { makeStyles } from '@masknet/theme'
import { Box, Link, Typography, type TypographyProps } from '@mui/material'
import type { SocialAccount } from '@masknet/shared-base'
import { SocialAddressType, type SocialAccount } from '@masknet/shared-base'
import { useWeb3State } from '@masknet/web3-hooks-base'
import { isSameAddress } from '@masknet/web3-shared-base'
import type { Web3Helper } from '@masknet/web3-helpers'
Expand Down Expand Up @@ -45,12 +45,16 @@ export function AddressItem({

if (!socialAccount) return null

const preferAddress =
!socialAccount.label ||
isEnsSubdomain(socialAccount.label) ||
socialAccount.supportedAddressTypes?.includes(SocialAddressType.Firefly) || // Label from Firefly is not reliable
isSameAddress(socialAccount.label, socialAccount.address)

return (
<>
<Box onClick={(ev: React.MouseEvent) => onClick?.(ev)}>
{!socialAccount.label ||
isEnsSubdomain(socialAccount.label) ||
isSameAddress(socialAccount.label, socialAccount.address) ? (
{preferAddress ? (
<ReversedAddress
{...TypographyProps}
address={socialAccount.address}
Expand Down
2 changes: 1 addition & 1 deletion packages/web3-providers/src/types/MaskX.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export namespace MaskX_BaseAPI {
}

export enum SourceType {
CyberConnect = 'cyberconnect',
CyberConnect = 'cyber',
Firefly = 'firefly',
OpenSea = 'opensea',
Sybil = 'sybil',
Expand Down

0 comments on commit 6e13011

Please sign in to comment.