Skip to content

Commit

Permalink
fix: 1271 sig (#130)
Browse files Browse the repository at this point in the history
* fix: 1271 sig

* chore: Update node

* chore: Update ts

* test: IUse vitest

* removel lint temp
  • Loading branch information
0xjojoex authored Apr 15, 2024
1 parent bc7f5fe commit 6b79d08
Show file tree
Hide file tree
Showing 7 changed files with 774 additions and 15 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/format.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x
node-version: 18.x

- name: Get yarn cache directory path
id: yarn-cache-dir-path
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x
node-version: 18.x

- name: Get yarn cache directory path
id: yarn-cache-dir-path
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Setup Node
uses: actions/setup-node@v2
with:
node-version: 14.x
node-version: 18.x

- name: Setup MongoDB
uses: supercharge/mongodb-github-action@1.7.0
Expand Down
13 changes: 10 additions & 3 deletions api/users/register.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { VercelRequest, VercelResponse } from "@vercel/node";
import { verifyMessage } from "ethers/lib/utils";
import { isValid } from "../../utils";
import { getModel } from "../../utils/mongo";
import { verifyMessage } from "../../utils/verifyMessage";
import { getAddress, isHex } from "viem";

export default async (req: VercelRequest, res: VercelResponse): Promise<VercelResponse | void> => {
if (req.method?.toUpperCase() === "OPTIONS") {
Expand All @@ -15,8 +16,14 @@ export default async (req: VercelRequest, res: VercelResponse): Promise<VercelRe
return res.status(400).json({ error: { message } });
}

const signedAddress = verifyMessage(username, signature);
if (address.toLowerCase() !== signedAddress?.toLowerCase()) {
const sig = signature.startWith("0x") ? signature : `0x${signature}`;

if (!isHex(sig)) {
return res.status(400).json({ error: { message: "Invalid signature." } });
}

const isValidMessage = await verifyMessage(username, sig, getAddress(address));
if (!isValidMessage) {
return res.status(400).json({ error: { message: "Invalid signature." } });
}

Expand Down
10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"scripts": {
"format:check": "prettier --check '*/**/*.{js,ts}'",
"format:write": "prettier --write '*/**/*.{js,ts}'",
"lint": "eslint '*/**/*.{js,ts}'",
"test": "jest",
"lint": "echo 'no'",
"test": "vitest --run --globals",
"test:coverage": "jest --coverage"
},
"husky": {
Expand All @@ -25,7 +25,8 @@
"graphql": "^15.5.0",
"graphql-request": "^3.5.0",
"json2csv": "^5.0.6",
"mongoose": "^5.13.0"
"mongoose": "^5.13.0",
"viem": "^2.9.17"
},
"devDependencies": {
"@commitlint/cli": "^12.1.4",
Expand All @@ -43,6 +44,7 @@
"prettier": "^2.3.2",
"ts-jest": "^27.0.3",
"ts-node": "^10.0.0",
"typescript": "^4.4.2"
"typescript": "^5.4.5",
"vitest": "^1.5.0"
}
}
19 changes: 19 additions & 0 deletions utils/verifyMessage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Address, Hex, createPublicClient, http } from "viem";
import { bsc } from "viem/chains";

const bscPublicClient = createPublicClient({
chain: bsc,
transport: http("https://nodes.pancakeswap.info"),
});

export async function verifyMessage(
message: string,
signature: Hex,
address: Address
): Promise<boolean> {
return bscPublicClient.verifyMessage({
address,
message,
signature,
});
}
Loading

0 comments on commit 6b79d08

Please sign in to comment.