Skip to content

Commit

Permalink
generalize users query
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Jun 29, 2023
1 parent 56abfa9 commit b0b0e07
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 71 deletions.
25 changes: 25 additions & 0 deletions backend/native/backpack-api/src/blockchains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type BlockchainsNative = Record<
Blockchain,
{
validateSignature: (msg: Buffer, sig: string, pubkey: string) => boolean;
validatePublicKey: (address: string) => boolean;
}
>;

Expand All @@ -27,6 +28,14 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
validateSignature: (msg: Buffer, signature: string, publicKey: string) => {
return ethers.utils.verifyMessage(msg, signature) === publicKey;
},
validatePublicKey: (address: string) => {
try {
ethers.utils.getAddress(address);
} catch (e) {
return false;
}
return true;
},
},
[Blockchain.SOLANA]: {
/**
Expand Down Expand Up @@ -69,6 +78,14 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
return false;
}
},
validatePublicKey: (address: string) => {
try {
new PublicKey(address);
} catch (err) {
return false;
}
return true;
},
},
[Blockchain.ECLIPSE]: {
validateSignature: (
Expand All @@ -82,5 +99,13 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
encodedPublicKey
);
},
validatePublicKey: (address: string) => {
try {
new PublicKey(address);
} catch (err) {
return false;
}
return true;
},
},
};
52 changes: 25 additions & 27 deletions backend/native/backpack-api/src/routes/v1/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import {
} from "../../db/users";
import { getOrcreateXnftSecret } from "../../db/xnftSecrets";
import { logger } from "../../logger";
import { validatePublicKey } from "../../validation/publicKey";
import {
BlockchainPublicKey,
CreatePublicKeys,
Expand All @@ -50,35 +49,31 @@ router.get("/", extractUserId, async (req, res) => {
// @ts-ignore
const limit: number = req.query.limit ? parseInt(req.query.limit) : 20;

const isSolPublicKey = validatePublicKey(usernamePrefix, "solana");
const isEclipsePublicKey = validatePublicKey(usernamePrefix, "eclipse");
const isEthPublicKey = validatePublicKey(usernamePrefix, "ethereum");

let users: any = [];

//
// SVM.
// Maps blockchain -> boolean, where true if the usernamePrefix is a valid
// pubkey for that chian.
//
if (isSolPublicKey) {
users = users.concat(
await getUserByPublicKeyAndChain(usernamePrefix, Blockchain.SOLANA)
);
}
if (isEclipsePublicKey) {
users = users.concat(
await getUserByPublicKeyAndChain(usernamePrefix, Blockchain.ECLIPSE)
const blockchainsToSearch: { [blockchain: string]: boolean } =
Object.fromEntries(
new Map(
Object.entries(BLOCKCHAINS_NATIVE).map(([blockchain, native]) => {
return [blockchain, native.validatePublicKey(usernamePrefix)];
})
)
);
}

//
// EVM.
// The users found for that key (should only be one).
//
if (isEthPublicKey) {
users = await getUserByPublicKeyAndChain(
usernamePrefix,
Blockchain.ETHEREUM
);
}
let users = (
await Promise.all(
Object.entries(blockchainsToSearch)
.filter(([, isValid]) => isValid)
.map(([blockchain]) =>
getUserByPublicKeyAndChain(usernamePrefix, blockchain as Blockchain)
)
)
).reduce((accumulator, users) => accumulator.concat(users), []);

//
// Not a pubkey so assume it's a username.
Expand Down Expand Up @@ -109,9 +104,12 @@ router.get("/", extractUserId, async (req, res) => {
requested: friendship?.requested || false,
remoteRequested: friendship?.remoteRequested || false,
areFriends: friendship?.areFriends || false,
searchedSolPubKey: isSolPublicKey ? usernamePrefix : undefined,
searchedEclipsePubKey: isEclipsePublicKey ? usernamePrefix : undefined,
searchedEthPubKey: isEthPublicKey ? usernamePrefix : undefined,
searched: {
usernamePrefix,
blockchains: {
...blockchainsToSearch,
},
},
// TODO: fix the disambiguation with snake_case and camelCase in API responses
public_keys: public_keys.map((pk) => ({
...pk,
Expand Down
30 changes: 0 additions & 30 deletions backend/native/backpack-api/src/validation/publicKey.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/common/src/feature-gates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const DEFAULT_FEATURE_GATES = {
STICKER_ENABLED: false,
STRIPE_ENABLED: false,
SWAP_FEES_ENABLED: false,
ECLIPSE: false,
ECLIPSE: true,
} as const;

export type FEATURE_GATES_MAP = typeof DEFAULT_FEATURE_GATES;
Expand Down
7 changes: 4 additions & 3 deletions packages/common/src/messages/toServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,10 @@ export interface RemoteUserData {
requested: boolean;
remoteRequested: boolean;
username: string;
searchedSolPubKey?: string; // Returns a public key if it is searched for
searchedEclipsePubKey?: string;
searchedEthPubKey?: string;
searched: {
usernamePrefix?: string;
blockchains: { [blockchain: string]: boolean };
};
public_keys: {
blockchain: Blockchain;
publicKey: string;
Expand Down
14 changes: 4 additions & 10 deletions packages/message-sdk/src/components/UserList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -303,16 +303,10 @@ function UserListItem({
}}
>
{formatUsername(user.username, 15)}{" "}
{user.searchedSolPubKey ? (
<> ({formatWalletAddress(user.searchedSolPubKey, 2)})</>
) : (
""
)}{" "}
{user.searchedEthPubKey ? (
<>({formatWalletAddress(user.searchedEthPubKey, 2)})</>
) : (
""
)}
{Object.values(user.searched.blockchains).filter((bool) => bool)
.length > 0
? formatWalletAddress(user.searched.usernamePrefix!, 2)
: ""}
</div>
{BACKPACK_TEAM.includes(user.id) ? <BackpackStaffIcon /> : null}
</div>
Expand Down

1 comment on commit b0b0e07

@vercel
Copy link

@vercel vercel bot commented on b0b0e07 Jun 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.