Skip to content

Commit f4095eb

Browse files
committed
chore: move detectSIWEMessage to utils package
1 parent 8eef8a5 commit f4095eb

File tree

6 files changed

+28
-28
lines changed

6 files changed

+28
-28
lines changed

packages/popup/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
"@fluent-wallet/tx-history-checker": "workspace:*",
3636
"@fluent-wallet/use-rpc": "workspace:packages/ui/useRPC",
3737
"@fluent-wallet/utils": "workspace:packages/utils",
38-
"@spruceid/siwe-parser": "patch:@spruceid/siwe-parser@npm%3A3.0.0#~/.yarn/patches/@spruceid-siwe-parser-npm-3.0.0-4572197cda.patch",
3938
"bn.js": "5.2.1",
4039
"dayjs": "1.10.7",
4140
"i18next": "21.5.2",

packages/popup/src/pages/RequestSignature/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@ import {
1616
} from '../../hooks/useApi'
1717

1818
import {RPC_METHODS} from '../../constants'
19-
import {detectSIWEMessage} from '../../utils'
2019
import {useMemo} from 'react'
2120
import {TypedDataSign} from './components/TypedDataSign'
2221
import {PersonalSign} from './components/PersonalSign'
2322
import {SignInSign} from './components/SignInSign'
2423
import Alert from '@fluent-wallet/component-alert'
24+
import {detectSIWEMessage} from '@fluent-wallet/utils'
2525
const {PERSONAL_SIGN, ACCOUNT_GROUP_TYPE} = RPC_METHODS
2626

2727
function RequestSignature() {

packages/popup/src/utils/index.js

-25
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {validateBase32Address} from '@fluent-wallet/base32-address'
66
import {isHexAddress, isChecksummed, toChecksum} from '@fluent-wallet/account'
77
import {CFX_MAINNET_CHAINID, ETH_MAINNET_CHAINID} from '@fluent-wallet/consts'
88
import {isArray, isString} from '@fluent-wallet/checks'
9-
import {ParsedMessage} from '@spruceid/siwe-parser'
109
import {
1110
PASSWORD_REG_EXP,
1211
RPC_METHODS,
@@ -403,27 +402,3 @@ export const formatNsName = nsName => {
403402

404403
return nsName
405404
}
406-
407-
export const detectSIWEMessage = message => {
408-
try {
409-
const sanitizedMessage = stripHexPrefix(message)
410-
const bytes = new Uint8Array(sanitizedMessage.length / 2)
411-
for (let i = 0; i < sanitizedMessage.length; i += 2) {
412-
bytes[i / 2] = Number.parseInt(sanitizedMessage.substr(i, 2), 16)
413-
}
414-
const decoder = new TextDecoder('utf-8')
415-
416-
const decodedMessage = decoder.decode(bytes)
417-
418-
const parsedMessage = new ParsedMessage(decodedMessage)
419-
420-
return {
421-
parsedMessage,
422-
isSIWEMessage: true,
423-
}
424-
} catch (e) {
425-
return {
426-
isSIWEMessage: false,
427-
}
428-
}
429-
}

packages/utils/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import randombytes from 'randombytes'
22
import {Buffer} from 'buffer'
33
import BN from 'bn.js'
4+
import {ParsedMessage} from '@spruceid/siwe-parser'
45

56
export const randomHex = function (size) {
67
return '0x' + randombytes(size).toString('hex')
@@ -200,3 +201,27 @@ export const setLength = setLengthLeft
200201
export const setLengthRight = function (msg, length) {
201202
return setLength(msg, length, true)
202203
}
204+
205+
export const detectSIWEMessage = message => {
206+
try {
207+
const sanitizedMessage = stripHexPrefix(message)
208+
const bytes = new Uint8Array(sanitizedMessage.length / 2)
209+
for (let i = 0; i < sanitizedMessage.length; i += 2) {
210+
bytes[i / 2] = Number.parseInt(sanitizedMessage.substr(i, 2), 16)
211+
}
212+
const decoder = new TextDecoder('utf-8')
213+
214+
const decodedMessage = decoder.decode(bytes)
215+
216+
const parsedMessage = new ParsedMessage(decodedMessage)
217+
218+
return {
219+
parsedMessage,
220+
isSIWEMessage: true,
221+
}
222+
} catch (e) {
223+
return {
224+
isSIWEMessage: false,
225+
}
226+
}
227+
}

packages/utils/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
"type": "module",
55
"main": "index.js",
66
"dependencies": {
7+
"@spruceid/siwe-parser": "patch:@spruceid/siwe-parser@npm%3A3.0.0#~/.yarn/patches/@spruceid-siwe-parser-npm-3.0.0-4572197cda.patch",
78
"bn.js": "5.2.1",
89
"is-hex-prefixed": "^1.0.0",
910
"randombytes": "^2.1.0",

yarn.lock

+1-1
Original file line numberDiff line numberDiff line change
@@ -5052,6 +5052,7 @@ __metadata:
50525052
version: 0.0.0-use.local
50535053
resolution: "@fluent-wallet/utils@workspace:packages/utils"
50545054
dependencies:
5055+
"@spruceid/siwe-parser": "patch:@spruceid/siwe-parser@npm%3A3.0.0#~/.yarn/patches/@spruceid-siwe-parser-npm-3.0.0-4572197cda.patch"
50555056
bn.js: "npm:5.2.1"
50565057
is-hex-prefixed: "npm:^1.0.0"
50575058
randombytes: "npm:^2.1.0"
@@ -18373,7 +18374,6 @@ __metadata:
1837318374
"@fluent-wallet/tx-history-checker": "workspace:*"
1837418375
"@fluent-wallet/use-rpc": "workspace:packages/ui/useRPC"
1837518376
"@fluent-wallet/utils": "workspace:packages/utils"
18376-
"@spruceid/siwe-parser": "patch:@spruceid/siwe-parser@npm%3A3.0.0#~/.yarn/patches/@spruceid-siwe-parser-npm-3.0.0-4572197cda.patch"
1837718377
bn.js: "npm:5.2.1"
1837818378
dayjs: "npm:1.10.7"
1837918379
i18next: "npm:21.5.2"

0 commit comments

Comments
 (0)