Skip to content

Commit

Permalink
use fn for public key zod
Browse files Browse the repository at this point in the history
  • Loading branch information
armaniferrante committed Jun 29, 2023
1 parent 5f8f988 commit 130a1f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 41 deletions.
83 changes: 43 additions & 40 deletions backend/native/backpack-api/src/blockchains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type BlockchainsNative = Record<
Blockchain,
{
validateSignature: (msg: Buffer, sig: string, pubkey: string) => boolean;
ZodPublicKey: any; // TODO: type.
ZodPublicKey: () => any; // TODO: type.
ZodCreatePublicKey: () => any; // TODO: type.
}
>;
Expand All @@ -30,20 +30,21 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
validateSignature: (msg: Buffer, signature: string, publicKey: string) => {
return ethers.utils.verifyMessage(msg, signature) === publicKey;
},
ZodPublicKey: z.object({
publicKey: z.string().refine((str) => {
try {
ethers.utils.getAddress(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Ethereum public key"),
blockchain: z.literal("ethereum"),
}),
ZodPublicKey: () =>
z.object({
publicKey: z.string().refine((str) => {
try {
ethers.utils.getAddress(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Ethereum public key"),
blockchain: z.literal("ethereum"),
}),
ZodCreatePublicKey: () =>
BLOCKCHAINS_NATIVE[Blockchain.ETHEREUM].ZodPublicKey.extend({
BLOCKCHAINS_NATIVE[Blockchain.ETHEREUM].ZodPublicKey().extend({
signature: z.string(),
}),
},
Expand Down Expand Up @@ -88,20 +89,21 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
return false;
}
},
ZodPublicKey: z.object({
publicKey: z.string().refine((str) => {
try {
new PublicKey(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Solana public key"),
blockchain: z.literal("solana"),
}),
ZodPublicKey: () =>
z.object({
publicKey: z.string().refine((str) => {
try {
new PublicKey(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Solana public key"),
blockchain: z.literal("solana"),
}),
ZodCreatePublicKey: () =>
BLOCKCHAINS_NATIVE[Blockchain.SOLANA].ZodPublicKey.extend({
BLOCKCHAINS_NATIVE[Blockchain.SOLANA].ZodPublicKey().extend({
signature: z.string(),
}),
},
Expand All @@ -117,20 +119,21 @@ export const BLOCKCHAINS_NATIVE: BlockchainsNative = {
encodedPublicKey
);
},
ZodPublicKey: z.object({
publicKey: z.string().refine((str) => {
try {
new PublicKey(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Eclipse public key"),
blockchain: z.literal("eclipse"),
}),
ZodPublicKey: () =>
z.object({
publicKey: z.string().refine((str) => {
try {
new PublicKey(str);
return true;
} catch {
// Pass
}
return false;
}, "must be a valid Eclipse public key"),
blockchain: z.literal("eclipse"),
}),
ZodCreatePublicKey: () =>
BLOCKCHAINS_NATIVE[Blockchain.ECLIPSE].ZodPublicKey.extend({
BLOCKCHAINS_NATIVE[Blockchain.ECLIPSE].ZodPublicKey().extend({
signature: z.string(),
}),
},
Expand Down
2 changes: 1 addition & 1 deletion backend/native/backpack-api/src/validation/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const BaseCreateUser = z.object({
//
export const BlockchainPublicKey = z.discriminatedUnion(
"blockchain",
Object.values(BLOCKCHAINS_NATIVE).map((native) => native.ZodPublicKey)
Object.values(BLOCKCHAINS_NATIVE).map((native) => native.ZodPublicKey())
);

//
Expand Down

1 comment on commit 130a1f1

@vercel
Copy link

@vercel vercel bot commented on 130a1f1 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.