1 | import { u8aConcatStrict } from './concat.js';
|
2 | import { u8aEq } from './eq.js';
|
3 | import { u8aToU8a } from './toU8a.js';
|
4 |
|
5 | export const U8A_WRAP_ETHEREUM = u8aToU8a('\x19Ethereum Signed Message:\n');
|
6 |
|
7 | export const U8A_WRAP_PREFIX = u8aToU8a('<Bytes>');
|
8 |
|
9 | export const U8A_WRAP_POSTFIX = u8aToU8a('</Bytes>');
|
10 | const WRAP_LEN = U8A_WRAP_PREFIX.length + U8A_WRAP_POSTFIX.length;
|
11 |
|
12 | export function u8aIsWrapped(u8a, withEthereum) {
|
13 | return ((u8a.length >= WRAP_LEN &&
|
14 | u8aEq(u8a.subarray(0, U8A_WRAP_PREFIX.length), U8A_WRAP_PREFIX) &&
|
15 | u8aEq(u8a.slice(-U8A_WRAP_POSTFIX.length), U8A_WRAP_POSTFIX)) ||
|
16 | (withEthereum &&
|
17 | u8a.length >= U8A_WRAP_ETHEREUM.length &&
|
18 | u8aEq(u8a.subarray(0, U8A_WRAP_ETHEREUM.length), U8A_WRAP_ETHEREUM)));
|
19 | }
|
20 |
|
21 |
|
22 |
|
23 |
|
24 | export function u8aUnwrapBytes(bytes) {
|
25 | const u8a = u8aToU8a(bytes);
|
26 |
|
27 | return u8aIsWrapped(u8a, false)
|
28 | ? u8a.subarray(U8A_WRAP_PREFIX.length, u8a.length - U8A_WRAP_POSTFIX.length)
|
29 | : u8a;
|
30 | }
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 | export function u8aWrapBytes(bytes) {
|
39 | const u8a = u8aToU8a(bytes);
|
40 | return u8aIsWrapped(u8a, true)
|
41 | ? u8a
|
42 | : u8aConcatStrict([U8A_WRAP_PREFIX, u8a, U8A_WRAP_POSTFIX]);
|
43 | }
|