Detect and verify wrapped messages (#1110)

* Detect and verify wrapped messages

* Expose isWrapped detection

* Complexity cleanups

* Update wrap.spec.ts

* Update wrap.ts

* Extract fn getter

* Test for wrapping to detect path
This commit is contained in:
Jaco
2021-09-17 14:48:33 +02:00
committed by GitHub
parent 5dcf99eaa7
commit 7047bf5aa5
7 changed files with 249 additions and 8 deletions
+1
View File
@@ -16,3 +16,4 @@ export { u8aToBuffer } from './toBuffer';
export { u8aToHex } from './toHex';
export { u8aToString } from './toString';
export { u8aToU8a } from './toU8a';
export { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aIsWrapped, u8aWrapBytes, u8aUnwrapBytes } from './wrap';
+137
View File
@@ -0,0 +1,137 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Originally from https://github.com/polkadot-js/extension/pull/743
import { u8aConcat, u8aEq, u8aToString } from '@polkadot/util';
import { U8A_WRAP_ETHEREUM, U8A_WRAP_POSTFIX, U8A_WRAP_PREFIX, u8aUnwrapBytes, u8aWrapBytes } from './wrap';
const TEST_DATA = 'this is just some random message that we expect to be wrapped along the way';
const TEST_ETH = u8aConcat(U8A_WRAP_ETHEREUM, TEST_DATA);
const TEST_WRAP_EMPTY = `${u8aToString(U8A_WRAP_PREFIX)}${u8aToString(U8A_WRAP_POSTFIX)}`;
const TEST_WRAP_FULL = `${u8aToString(U8A_WRAP_PREFIX)}${TEST_DATA}${u8aToString(U8A_WRAP_POSTFIX)}`;
const TEST_WARP_HALF_PRE = `${u8aToString(U8A_WRAP_PREFIX)}${TEST_DATA}`;
const TEST_WRAP_HALF_POST = `${TEST_DATA}${u8aToString(U8A_WRAP_POSTFIX)}`;
describe('u8aWrapBytes', (): void => {
it('wraps empty bytes', (): void => {
expect(
u8aEq(
u8aWrapBytes(new Uint8Array()),
u8aConcat(U8A_WRAP_PREFIX, U8A_WRAP_POSTFIX)
)
).toBe(true);
});
it('wraps when no wrapping is detected', (): void => {
expect(
u8aToString(
u8aWrapBytes(TEST_DATA)
)
).toEqual(TEST_WRAP_FULL);
});
it('wraps when only start wrap is detected', (): void => {
expect(
u8aToString(
u8aWrapBytes(TEST_WARP_HALF_PRE)
)
).toEqual(`${u8aToString(U8A_WRAP_PREFIX)}${TEST_WARP_HALF_PRE}${u8aToString(U8A_WRAP_POSTFIX)}`);
});
it('wraps when only end wrap is detected', (): void => {
expect(
u8aToString(
u8aWrapBytes(TEST_WRAP_HALF_POST)
)
).toEqual(`${u8aToString(U8A_WRAP_PREFIX)}${TEST_WRAP_HALF_POST}${u8aToString(U8A_WRAP_POSTFIX)}`);
});
it('does not re-wrap when a wrap is already present', (): void => {
expect(
u8aToString(
u8aWrapBytes(TEST_WRAP_FULL)
)
).toEqual(TEST_WRAP_FULL);
});
it('does not re-wrap when a wrap (empty data) is already present', (): void => {
expect(
u8aToString(
u8aWrapBytes(TEST_WRAP_EMPTY)
)
).toEqual(TEST_WRAP_EMPTY);
});
});
describe('unwrapBytes', (): void => {
it('unwraps empty bytes', (): void => {
expect(
u8aEq(
u8aUnwrapBytes(new Uint8Array()),
new Uint8Array()
)
).toBe(true);
});
it('unwraps when no wrapping is detected', (): void => {
expect(
u8aToString(
u8aUnwrapBytes(TEST_DATA)
)
).toEqual(TEST_DATA);
});
it('unwraps when no wrapping is detected (only start)', (): void => {
expect(
u8aToString(
u8aUnwrapBytes(TEST_WARP_HALF_PRE)
)
).toEqual(TEST_WARP_HALF_PRE);
});
it('unwraps when no wrapping is detected (only end)', (): void => {
expect(
u8aToString(
u8aUnwrapBytes(TEST_WRAP_HALF_POST)
)
).toEqual(TEST_WRAP_HALF_POST);
});
it('unwraps when a wrap is present', (): void => {
expect(
u8aToString(
u8aUnwrapBytes(TEST_WRAP_FULL)
)
).toEqual(TEST_DATA);
});
it('unwraps when a an empty wrap is present', (): void => {
expect(
u8aToString(
u8aUnwrapBytes(TEST_WRAP_EMPTY)
)
).toEqual('');
});
describe('Ethereum-style', (): void => {
it('does not wrap an Ethereum wrap', (): void => {
expect(
u8aEq(
u8aWrapBytes(TEST_ETH),
TEST_ETH
)
).toBe(true);
});
it('does not unwrap an Ethereum wrap', (): void => {
expect(
u8aEq(
u8aUnwrapBytes(TEST_ETH),
TEST_ETH
)
).toBe(true);
});
});
});
+49
View File
@@ -0,0 +1,49 @@
// Copyright 2017-2021 @polkadot/util authors & contributors
// SPDX-License-Identifier: Apache-2.0
// Originally from https://github.com/polkadot-js/extension/pull/743
import type { U8aLike } from '../types';
import { u8aConcat } from './concat';
import { u8aEq } from './eq';
import { u8aToU8a } from './toU8a';
export const U8A_WRAP_ETHEREUM = u8aToU8a('\x19Ethereum Signed Message:\n');
export const U8A_WRAP_PREFIX = u8aToU8a('<Bytes>');
export const U8A_WRAP_POSTFIX = u8aToU8a('</Bytes>');
const WRAP_LEN = U8A_WRAP_PREFIX.length + U8A_WRAP_POSTFIX.length;
export function u8aIsWrapped (u8a: Uint8Array, withEthereum: boolean): boolean {
return (
(
u8a.length >= WRAP_LEN &&
u8aEq(u8a.subarray(0, U8A_WRAP_PREFIX.length), U8A_WRAP_PREFIX) &&
u8aEq(u8a.slice(-U8A_WRAP_POSTFIX.length), U8A_WRAP_POSTFIX)
) ||
(
withEthereum &&
u8a.length >= U8A_WRAP_ETHEREUM.length &&
u8aEq(u8a.subarray(0, U8A_WRAP_ETHEREUM.length), U8A_WRAP_ETHEREUM)
)
);
}
export function u8aUnwrapBytes (bytes: U8aLike): Uint8Array {
const u8a = u8aToU8a(bytes);
// we don't want to unwrap Ethereum-style wraps
return u8aIsWrapped(u8a, false)
? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - U8A_WRAP_POSTFIX.length)
: u8a;
}
export function u8aWrapBytes (bytes: U8aLike): Uint8Array {
const u8a = u8aToU8a(bytes);
// if Ethereum-wrapping, we don't add our wrapping bytes
return u8aIsWrapped(u8a, true)
? u8a
: u8aConcat(U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX);
}