-
-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes in typed signature validation and normalization #318
Changes from 5 commits
4ef3974
bd6b77e
b1b150b
294bab0
1646325
2c8430d
f0536b1
c5603c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,7 @@ | ||
import { add0x, isValidHexAddress, isStrictHexString } from '@metamask/utils'; | ||
import type { Hex } from '@metamask/utils'; | ||
import BN from 'bn.js'; | ||
|
||
type EIP712Domain = { | ||
verifyingContract: string; | ||
verifyingContract: Hex; | ||
}; | ||
|
||
type SignTypedMessageDataV3V4 = { | ||
|
@@ -14,7 +12,7 @@ | |
}; | ||
|
||
/** | ||
* Normalizes the messageData for the eth_signTypedData | ||
Check warning on line 15 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (18.x)
Check warning on line 15 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (20.x)
|
||
* | ||
* @param messageData - The messageData to normalize. | ||
* @returns The normalized messageData. | ||
|
@@ -40,12 +38,12 @@ | |
} | ||
|
||
/** | ||
* Parses the messageData to obtain the data object for EIP712 normalization | ||
Check warning on line 41 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (18.x)
Check warning on line 41 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (20.x)
|
||
* | ||
* @param data - The messageData to parse. | ||
* @returns The data object for EIP712 normalization. | ||
*/ | ||
function parseTypedMessage(data: string) { | ||
export function parseTypedMessage(data: string) { | ||
if (typeof data !== 'string') { | ||
return data; | ||
} | ||
|
@@ -54,36 +52,14 @@ | |
} | ||
|
||
/** | ||
* Normalizes the address to a hexadecimal format | ||
* Normalizes the address to standard hexadecimal format | ||
Check warning on line 55 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (18.x)
Check warning on line 55 in src/utils/normalize.ts GitHub Actions / Build, lint, and test / Lint (20.x)
|
||
* | ||
* @param address - The address to normalize. | ||
* @returns The normalized address. | ||
*/ | ||
function normalizeContractAddress(address: string): Hex | string { | ||
if (isStrictHexString(address) && isValidHexAddress(address)) { | ||
return address; | ||
function normalizeContractAddress(address: Hex): Hex { | ||
if (address.startsWith('0X')) { | ||
return `0x${address.slice(2)}`; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Normilizing decimal and octal value is removed now, only normalization now is replacing |
||
|
||
// Check if the address is in octal format, convert to hexadecimal | ||
if (address.startsWith('0o')) { | ||
// If octal, convert to hexadecimal | ||
return octalToHex(address); | ||
} | ||
|
||
// Check if the address is in decimal format, convert to hexadecimal | ||
try { | ||
const decimalBN = new BN(address, 10); | ||
const hexString = decimalBN.toString(16); | ||
return add0x(hexString); | ||
} catch (e) { | ||
// Ignore errors and return the original address | ||
} | ||
|
||
// Returning the original address without normalization | ||
return address; | ||
} | ||
|
||
function octalToHex(octalAddress: string): Hex { | ||
const decimalAddress = parseInt(octalAddress.slice(2), 8).toString(16); | ||
return add0x(decimalAddress); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,15 @@ | |
createScaffoldMiddleware, | ||
} from '@metamask/json-rpc-engine'; | ||
import { providerErrors, rpcErrors } from '@metamask/rpc-errors'; | ||
import type { | ||
Json, | ||
JsonRpcRequest, | ||
PendingJsonRpcResponse, | ||
import { | ||
isValidHexAddress, | ||
type Json, | ||
type JsonRpcRequest, | ||
type PendingJsonRpcResponse, | ||
} from '@metamask/utils'; | ||
|
||
import type { Block } from './types'; | ||
import { normalizeTypedMessage } from './utils/normalize'; | ||
import { normalizeTypedMessage, parseTypedMessage } from './utils/normalize'; | ||
|
||
/* | ||
export type TransactionParams = { | ||
|
@@ -278,6 +279,7 @@ | |
|
||
const address = await validateAndNormalizeKeyholder(params[0], req); | ||
const message = normalizeTypedMessage(params[1]); | ||
validateVerifyingContract(message); | ||
const version = 'V3'; | ||
const msgParams: TypedMessageParams = { | ||
data: message, | ||
|
@@ -308,6 +310,7 @@ | |
|
||
const address = await validateAndNormalizeKeyholder(params[0], req); | ||
const message = normalizeTypedMessage(params[1]); | ||
validateVerifyingContract(message); | ||
const version = 'V4'; | ||
const msgParams: TypedMessageParams = { | ||
data: message, | ||
|
@@ -459,7 +462,7 @@ | |
* | ||
* @param address - The address to validate and normalize. | ||
* @param req - The request object. | ||
* @returns {string} - The normalized address, if valid. Otherwise, throws | ||
Check warning on line 465 in src/wallet.ts GitHub Actions / Build, lint, and test / Lint (18.x)
Check warning on line 465 in src/wallet.ts GitHub Actions / Build, lint, and test / Lint (20.x)
Check warning on line 465 in src/wallet.ts GitHub Actions / Build, lint, and test / Lint (22.5.1)
|
||
* an error | ||
*/ | ||
async function validateAndNormalizeKeyholder( | ||
|
@@ -490,6 +493,20 @@ | |
} | ||
} | ||
|
||
/** | ||
* Validates verifyingContract of typedSignMessage. | ||
* | ||
* @param data - The data passed in typedSign request. | ||
*/ | ||
function validateVerifyingContract(data: string) { | ||
const { | ||
domain: { verifyingContract }, | ||
} = parseTypedMessage(data); | ||
if (!isValidHexAddress(verifyingContract)) { | ||
throw rpcErrors.invalidInput(); | ||
} | ||
} | ||
|
||
function resemblesAddress(str: string): boolean { | ||
// hex prefix 2 + 20 bytes | ||
return str.length === 2 + 20 * 2; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can maybe rollback this change ? It's not required for the ci if you update your branch the ci will work again
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍 I updated the PR