Skip to content

Commit

Permalink
Merge pull request #742 from subquery/feat/sign-in
Browse files Browse the repository at this point in the history
feat: erc 4321 sign in replace erc721
  • Loading branch information
HuberTRoy committed Jul 15, 2024
2 parents 0843a98 + bce3e5d commit 4eddfcc
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 23 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@
"react-router": "^6.7.0",
"react-router-dom": "^6.7.0",
"reflect-metadata": "^0.1.13",
"siwe": "^2.3.2",
"subscriptions-transport-ws": "^0.11.0",
"typescript": "^5.4.5",
"viem": "^1.19.0",
Expand Down
42 changes: 21 additions & 21 deletions src/hooks/useConsumerHostServices.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import { useEffect, useRef, useState } from 'react';
import { useAccount } from '@containers/Web3';
import { Modal, openNotification, Typography } from '@subql/components';
import { getAuthReqHeader, parseError, POST } from '@utils';
import { ConsumerHostMessageType, domain, EIP712Domain, withChainIdRequestBody } from '@utils/eip712';
import { limitContract, makeCacheKey } from '@utils/limitation';
import { waitForSomething } from '@utils/waitForSomething';
import { Button } from 'antd';
import axios, { AxiosResponse } from 'axios';
import { BigNumberish } from 'ethers';
import { isObject } from 'lodash-es';
import { useSignTypedData } from 'wagmi';
import { generateNonce, SiweMessage } from 'siwe';
import { useChainId, useSignMessage } from 'wagmi';

const instance = axios.create({
baseURL: import.meta.env.VITE_CONSUMER_HOST_ENDPOINT,
Expand Down Expand Up @@ -40,7 +40,9 @@ export const useConsumerHostServices = (
{ alert = false, autoLogin = true }: ConsumerHostServicesProps = { alert: false, autoLogin: true },
) => {
const { address: account } = useAccount();
const { signTypedDataAsync } = useSignTypedData();
const chainId = useChainId();
const { signMessageAsync } = useSignMessage();

const authHeaders = useRef<{ Authorization: string }>(
getAuthReqHeader(localStorage.getItem(`consumer-host-services-token-${account}`) || ''),
);
Expand All @@ -50,31 +52,29 @@ export const useConsumerHostServices = (
const requestConsumerHostToken = async (account: string) => {
try {
const tokenRequestUrl = `${import.meta.env.VITE_CONSUMER_HOST_ENDPOINT}/login`;
const timestamp = new Date().getTime();

const signMsg = {
consumer: account,
timestamp,
};

const eip721Signature = await signTypedDataAsync({
types: {
EIP712Domain,
messageType: ConsumerHostMessageType,
},
primaryType: 'messageType',
// TODO: FIX, it seems is wagmi bug.
const newMsg = new SiweMessage({
domain: window.location.host,
address: account,
statement: `Login to SubQuery Network`,
uri: window.location.origin,
version: '1',
chainId,
nonce: generateNonce(),
});

// @ts-ignore
domain,
message: signMsg,
const signature = await signMessageAsync({
message: newMsg.prepareMessage(),
});

if (!eip721Signature) throw new Error();
if (!signature) throw new Error();

const { response, error } = await POST({
endpoint: tokenRequestUrl,
requestBody: withChainIdRequestBody(signMsg, eip721Signature),
requestBody: {
message: newMsg.prepareMessage(),
signature: signature,
},
});

const sortedResponse = response && (await response.json());
Expand Down
34 changes: 32 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3493,7 +3493,7 @@
resolved "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz#6f26dbc8fbc7205873ce3cee2f690eba0d421b39"
integrity sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==

"@noble/[email protected]", "@noble/hashes@^1.3.3":
"@noble/[email protected]", "@noble/hashes@^1.1.2", "@noble/hashes@^1.3.3":
version "1.4.0"
resolved "https://registry.npmjs.org/@noble/hashes/-/hashes-1.4.0.tgz#45814aa329f30e4fe0ba49426f49dfccdd066426"
integrity sha512-V1JJ1WTRUqHHrOSh597hURcMqVKVGL/ea3kv0gSnEdsEZ0/+VyPghM1lMNGc00z7CIQorSvbKpuJkxvuHbvdbg==
Expand Down Expand Up @@ -4348,6 +4348,16 @@
rpc-websockets "^7.5.1"
superstruct "^0.14.2"

"@spruceid/siwe-parser@^2.1.2":
version "2.1.2"
resolved "https://registry.yarnpkg.com/@spruceid/siwe-parser/-/siwe-parser-2.1.2.tgz#3e13e7d3ac0bfdaf109a07342590eb21daee2fc3"
integrity sha512-d/r3S1LwJyMaRAKQ0awmo9whfXeE88Qt00vRj91q5uv5ATtWIQEGJ67Yr5eSZw5zp1/fZCXZYuEckt8lSkereQ==
dependencies:
"@noble/hashes" "^1.1.2"
apg-js "^4.3.0"
uri-js "^4.4.1"
valid-url "^1.0.9"

"@stablelib/aead@^1.0.1":
version "1.0.1"
resolved "https://registry.npmjs.org/@stablelib/aead/-/aead-1.0.1.tgz#c4b1106df9c23d1b867eb9b276d8f42d5fc4c0c3"
Expand Down Expand Up @@ -6016,6 +6026,11 @@ anymatch@^3.1.3, anymatch@~3.1.2:
normalize-path "^3.0.0"
picomatch "^2.0.4"

apg-js@^4.3.0:
version "4.4.0"
resolved "https://registry.yarnpkg.com/apg-js/-/apg-js-4.4.0.tgz#09dcecab0731fbde233b9f2352fdd2d07e56b2cf"
integrity sha512-fefmXFknJmtgtNEXfPwZKYkMFX4Fyeyz+fNF6JWp87biGOPslJbCBVU158zvKRZfHBKnJDy8CMM40oLFGkXT8Q==

apollo-link-error@^1.1.13:
version "1.1.13"
resolved "https://registry.yarnpkg.com/apollo-link-error/-/apollo-link-error-1.1.13.tgz#c1a1bb876ffe380802c8df0506a32c33aad284cd"
Expand Down Expand Up @@ -13294,6 +13309,16 @@ signal-exit@^3.0.2, signal-exit@^3.0.3, signal-exit@^3.0.7:
resolved "https://registry.yarnpkg.com/signal-exit/-/signal-exit-3.0.7.tgz#a9a1767f8af84155114eaabd73f99273c8f59ad9"
integrity sha512-wnD2ZE+l+SPC/uoS0vXeE9L1+0wuaMqKlfz9AMUo38JsyLSBWSFcHR1Rri62LZc12vLr1gb3jl7iwQhgwpAbGQ==

siwe@^2.3.2:
version "2.3.2"
resolved "https://registry.yarnpkg.com/siwe/-/siwe-2.3.2.tgz#0794ae25f734f3068de0ab093ddd2f7867bc2d67"
integrity sha512-aSf+6+Latyttbj5nMu6GF3doMfv2UYj83hhwZgUF20ky6fTS83uVhkQABdIVnEuS8y1bBdk7p6ltb9SmlhTTlA==
dependencies:
"@spruceid/siwe-parser" "^2.1.2"
"@stablelib/random" "^1.0.1"
uri-js "^4.4.1"
valid-url "^1.0.9"

size-sensor@^1.0.1:
version "1.0.1"
resolved "https://registry.yarnpkg.com/size-sensor/-/size-sensor-1.0.1.tgz#f84e46206d3e259faff1d548e4b3beca93219dbb"
Expand Down Expand Up @@ -14358,7 +14383,7 @@ uqr@^0.1.2:
resolved "https://registry.npmjs.org/uqr/-/uqr-0.1.2.tgz#5c6cd5dcff9581f9bb35b982cb89e2c483a41d7d"
integrity sha512-MJu7ypHq6QasgF5YRTjqscSzQp/W11zoUk6kvmlH+fmWEs63Y0Eib13hYFwAzagRJcVY8WVnlV+eBDUGMJ5IbA==

uri-js@^4.2.2:
uri-js@^4.2.2, uri-js@^4.4.1:
version "4.4.1"
resolved "https://registry.yarnpkg.com/uri-js/-/uri-js-4.4.1.tgz#9b1a52595225859e55f669d928f88c6c57f2a77e"
integrity sha512-7rKUyy33Q1yc98pQ1DAmLtwX109F7TIfWlW1Ydo8Wl1ii1SeHieeh0HHfPeL2fMXK6z0s8ecKs9frCuLJvndBg==
Expand Down Expand Up @@ -14436,6 +14461,11 @@ uuid@^8.3.2:
resolved "https://registry.npmjs.org/uuid/-/uuid-8.3.2.tgz#80d5b5ced271bb9af6c445f21a1a04c606cefbe2"
integrity sha512-+NYs2QeMWy+GWFOEm9xnn6HCDp0l7QBD7ml8zLUmJ+93Q5NF0NocErnwkTkXVFNiX3/fpC6afS8Dhb/gz7R7eg==

valid-url@^1.0.9:
version "1.0.9"
resolved "https://registry.yarnpkg.com/valid-url/-/valid-url-1.0.9.tgz#1c14479b40f1397a75782f115e4086447433a200"
integrity sha512-QQDsV8OnSf5Uc30CKSwG9lnhMPe6exHtTXLRYX8uMwKENy640pU+2BgBL0LRbDh/eYRahNCS7aewCx0wf3NYVA==

validate-npm-package-license@^3.0.1:
version "3.0.4"
resolved "https://registry.npmjs.org/validate-npm-package-license/-/validate-npm-package-license-3.0.4.tgz#fc91f6b9c7ba15c857f4cb2c5defeec39d4f410a"
Expand Down

0 comments on commit 4eddfcc

Please sign in to comment.