Skip to content

Commit

Permalink
normalize addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
lauti7 committed Apr 23, 2024
1 parent cbbee00 commit db5b582
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
20 changes: 10 additions & 10 deletions src/adapters/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
let query: SQLStatement

if (onlyActive) {
query = SQL`SELECT * FROM friendships WHERE (LOWER(address_requester) = LOWER(${userAddress}) OR LOWER(address_requester) = LOWER(${userAddress})) AND is_active = true`
query = SQL`SELECT * FROM friendships WHERE (address_requester = ${userAddress} OR address_requested = ${userAddress}) AND is_active = true`
} else {
query = SQL`SELECT * FROM friendships WHERE (LOWER(address_requester) = LOWER(${userAddress}) OR LOWER(address_requester) = LOWER(${userAddress}))`
query = SQL`SELECT * FROM friendships WHERE (address_requester = ${userAddress} OR address_requested = ${userAddress})`
}

const generator = pg.streamQuery<Friendship>(query)
Expand All @@ -46,7 +46,7 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
SQL`WITH friendsA as (
SELECT
CASE
WHEN LOWER(address_requester) = LOWER(${userAddress1}) then address_requested
WHEN address_requester = ${userAddress1} then address_requested
else address_requester
end as address
FROM
Expand All @@ -57,8 +57,8 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
friendships f_a
where
(
LOWER(f_a.address_requester) = LOWER(${userAddress1})
or LOWER(f_a.address_requested) = LOWER(${userAddress1})
f_a.address_requester = ${userAddress1}
or f_a.address_requested = ${userAddress1}
) and f_a.is_active = true
) as friends_a
)
Expand All @@ -70,7 +70,7 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
address IN (
SELECT
CASE
WHEN LOWER(address_requester) = LOWER(${userAddress2}) then address_requested
WHEN address_requester = ${userAddress2} then address_requested
else address_requester
end as address_a
FROM
Expand All @@ -81,8 +81,8 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
friendships f_b
where
(
LOWER(f_b.address_requester) = LOWER(${userAddress2})
or LOWER(f_b.address_requested) = LOWER(${userAddress2})
f_b.address_requester = ${userAddress2}
or f_b.address_requested = ${userAddress2}
) and f_b.is_active = true
) as friends_b
);`
Expand All @@ -95,9 +95,9 @@ export function createDBComponent(components: Pick<AppComponents, 'pg' | 'logs'>
const query = SQL`
SELECT * FROM friendships
WHERE
(LOWER(address_requester) = LOWER(${userAddress1}) AND LOWER(address_requested) = LOWER(${userAddress2}))
(address_requester = ${userAddress1} AND address_requested = ${userAddress2})
OR
(LOWER(address_requester) = LOWER(${userAddress2}) AND LOWER(address_requested) = LOWER(${userAddress1}))
(address_requester = ${userAddress2} AND address_requested = ${userAddress1})
`

const results = await pg.query<Friendship>(query)
Expand Down
3 changes: 2 additions & 1 deletion src/adapters/rpcServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
validateNewFriendshipAction
} from '../logic/friendships'
import emitterToAsyncGenerator from '../utils/emitterToGenerator'
import { normalizeAddress } from '../utils/address'

export type IRPCServerComponent = IBaseComponent & {
attachUser(user: { transport: Transport; address: string }): void
Expand Down Expand Up @@ -89,7 +90,7 @@ export default async function createRpcServerComponent(
logger.debug(`getting mutual friends ${context.address}<>${request.user!.address}`)
let mutualFriends: AsyncGenerator<{ address: string }> | undefined
try {
mutualFriends = db.getMutualFriends(context.address, request.user!.address)
mutualFriends = db.getMutualFriends(context.address, normalizeAddress(request.user!.address))
} catch (error) {
logger.error(error as any)
// throw an error bc there is no sense to create a generator to send an error
Expand Down
3 changes: 2 additions & 1 deletion src/controllers/handlers/ws-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WebSocketTransport } from '@dcl/rpc/dist/transports/WebSocket'
import future from 'fp-future'
import { verify } from '@dcl/platform-crypto-middleware'
import { GlobalContext } from '../../types'
import { normalizeAddress } from '../../utils/address'

export async function wsHandler(context: IHttpServerComponent.DefaultContext<GlobalContext>) {
const { logs, rpcServer, fetcher } = context.components
Expand Down Expand Up @@ -57,7 +58,7 @@ export async function wsHandler(context: IHttpServerComponent.DefaultContext<Glo

logger.debug('addresss > ', { address: authchainVerifyResult.auth })

const address = authchainVerifyResult.auth.toLowerCase()
const address = normalizeAddress(authchainVerifyResult.auth)

rpcServer.attachUser({ transport: wsTransport, address })

Expand Down
11 changes: 6 additions & 5 deletions src/logic/friendships.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
FriendshipStatus,
SubscriptionEventsEmitter
} from '../types'
import { normalizeAddress } from '../utils/address'

export function isFriendshipActionValid(from: Action | null, to: Action) {
return FRIENDSHIP_ACTION_TRANSITIONS[to].includes(from)
Expand Down Expand Up @@ -79,27 +80,27 @@ export function parseUpsertFriendshipRequest(request: UpsertFriendshipPayload):
case 'accept':
return {
action: Action.ACCEPT,
user: request.action.accept.user!.address
user: normalizeAddress(request.action.accept.user!.address)
}
case 'cancel':
return {
action: Action.CANCEL,
user: request.action.cancel.user!.address
user: normalizeAddress(request.action.cancel.user!.address)
}
case 'delete':
return {
action: Action.DELETE,
user: request.action.delete.user!.address
user: normalizeAddress(request.action.delete.user!.address)
}
case 'reject':
return {
action: Action.REJECT,
user: request.action.reject.user!.address
user: normalizeAddress(request.action.reject.user!.address)
}
case 'request':
return {
action: Action.REQUEST,
user: request.action.request.user!.address,
user: normalizeAddress(request.action.request.user!.address),
metadata: request.action.request.message ? { message: request.action.request.message } : null
}
default:
Expand Down
3 changes: 3 additions & 0 deletions src/utils/address.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export function normalizeAddress(address: string) {
return address.toLowerCase()
}

0 comments on commit db5b582

Please sign in to comment.