-
-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2ccc490
commit 67d8a2c
Showing
5 changed files
with
178 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { SignTypedDataVersion } from '@metamask/keyring-controller'; | ||
|
||
import type { MessageParamsPersonal, MessageParamsTyped } from '../types'; | ||
import { | ||
normalizePersonalMessageParams, | ||
normalizeTypedMessageParams, | ||
} from './normalize'; | ||
|
||
describe('Normalize Utils', () => { | ||
describe('normalizePersonalMessageParams', () => { | ||
it('normalizes data', async () => { | ||
const firstNormalized = normalizePersonalMessageParams({ | ||
data: '879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0', | ||
} as MessageParamsPersonal); | ||
|
||
const secondNormalized = normalizePersonalMessageParams({ | ||
data: 'somedata', | ||
} as MessageParamsPersonal); | ||
|
||
expect(firstNormalized.data).toBe( | ||
'0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0', | ||
); | ||
|
||
expect(secondNormalized.data).toBe('0x736f6d6564617461'); | ||
}); | ||
}); | ||
|
||
describe('normalizeTypedMessageParams', () => { | ||
it.each([SignTypedDataVersion.V3, SignTypedDataVersion.V4])( | ||
'serializes data to JSON if not a string and version is %s', | ||
async (version) => { | ||
const normalized = normalizeTypedMessageParams( | ||
{ | ||
data: { test: 'data' }, | ||
} as unknown as MessageParamsTyped, | ||
version, | ||
); | ||
|
||
expect(normalized.data).toBe('{"test":"data"}'); | ||
}, | ||
); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import { SignTypedDataVersion } from '@metamask/keyring-controller'; | ||
import { add0x, bytesToHex, remove0x } from '@metamask/utils'; | ||
|
||
import type { MessageParamsPersonal, MessageParamsTyped } from '../types'; | ||
|
||
/** | ||
* Normalize personal message params. | ||
* @param messageParams - The message params to normalize. | ||
* @returns The normalized message params. | ||
*/ | ||
export function normalizePersonalMessageParams( | ||
messageParams: MessageParamsPersonal, | ||
): MessageParamsPersonal { | ||
return { | ||
...messageParams, | ||
data: normalizePersonalMessageData(messageParams.data), | ||
}; | ||
} | ||
|
||
/** | ||
* Normalize typed message params. | ||
* @param messageParams - The message params to normalize. | ||
* @param version - The version of the typed signature request. | ||
* @returns The normalized message params. | ||
*/ | ||
export function normalizeTypedMessageParams( | ||
messageParams: MessageParamsTyped, | ||
version: SignTypedDataVersion, | ||
): MessageParamsTyped { | ||
const normalizedMessageParams = { ...messageParams }; | ||
|
||
if ( | ||
typeof messageParams.data !== 'string' && | ||
(version === SignTypedDataVersion.V3 || version === SignTypedDataVersion.V4) | ||
) { | ||
normalizedMessageParams.data = JSON.stringify(messageParams.data); | ||
} | ||
|
||
return normalizedMessageParams; | ||
} | ||
|
||
/** | ||
* Converts raw message data buffer to a hex, or just returns the data if | ||
* it is already formatted as a hex. | ||
* | ||
* @param data - The buffer data to convert to a hex. | ||
* @returns A hex string conversion of the buffer data. | ||
*/ | ||
function normalizePersonalMessageData(data: string) { | ||
try { | ||
const stripped = remove0x(data); | ||
|
||
if (stripped.match(/^[0-9A-Fa-f]+$/gu)) { | ||
return add0x(stripped); | ||
} | ||
} catch (e) { | ||
/* istanbul ignore next */ | ||
} | ||
|
||
return bytesToHex(Buffer.from(data, 'utf8')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters