UNPKG

2.17 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3exports.u8aWrapBytes = exports.u8aUnwrapBytes = exports.u8aIsWrapped = exports.U8A_WRAP_POSTFIX = exports.U8A_WRAP_PREFIX = exports.U8A_WRAP_ETHEREUM = void 0;
4const concat_js_1 = require("./concat.js");
5const eq_js_1 = require("./eq.js");
6const toU8a_js_1 = require("./toU8a.js");
7/** @internal */
8exports.U8A_WRAP_ETHEREUM = (0, toU8a_js_1.u8aToU8a)('\x19Ethereum Signed Message:\n');
9/** @internal */
10exports.U8A_WRAP_PREFIX = (0, toU8a_js_1.u8aToU8a)('<Bytes>');
11/** @internal */
12exports.U8A_WRAP_POSTFIX = (0, toU8a_js_1.u8aToU8a)('</Bytes>');
13const WRAP_LEN = exports.U8A_WRAP_PREFIX.length + exports.U8A_WRAP_POSTFIX.length;
14/** @internal */
15function u8aIsWrapped(u8a, withEthereum) {
16 return ((u8a.length >= WRAP_LEN &&
17 (0, eq_js_1.u8aEq)(u8a.subarray(0, exports.U8A_WRAP_PREFIX.length), exports.U8A_WRAP_PREFIX) &&
18 (0, eq_js_1.u8aEq)(u8a.slice(-exports.U8A_WRAP_POSTFIX.length), exports.U8A_WRAP_POSTFIX)) ||
19 (withEthereum &&
20 u8a.length >= exports.U8A_WRAP_ETHEREUM.length &&
21 (0, eq_js_1.u8aEq)(u8a.subarray(0, exports.U8A_WRAP_ETHEREUM.length), exports.U8A_WRAP_ETHEREUM)));
22}
23exports.u8aIsWrapped = u8aIsWrapped;
24/**
25 * @name u8aUnwrapBytes
26 * @description Removes all <Bytes>...</Bytes> wrappers from the supplied value
27 */
28function u8aUnwrapBytes(bytes) {
29 const u8a = (0, toU8a_js_1.u8aToU8a)(bytes);
30 // we don't want to unwrap Ethereum-style wraps
31 return u8aIsWrapped(u8a, false)
32 ? u8a.subarray(exports.U8A_WRAP_PREFIX.length, u8a.length - exports.U8A_WRAP_POSTFIX.length)
33 : u8a;
34}
35exports.u8aUnwrapBytes = u8aUnwrapBytes;
36/**
37 * @name u8aWrapBytes
38 * @description
39 * Adds a <Bytes>...</Bytes> wrapper to the supplied value, if
40 * - We don't already have a Bytes wrapper
41 * - The message is not an Ethereum-style message
42 */
43function u8aWrapBytes(bytes) {
44 const u8a = (0, toU8a_js_1.u8aToU8a)(bytes);
45 return u8aIsWrapped(u8a, true)
46 ? u8a
47 : (0, concat_js_1.u8aConcatStrict)([exports.U8A_WRAP_PREFIX, u8a, exports.U8A_WRAP_POSTFIX]);
48}
49exports.u8aWrapBytes = u8aWrapBytes;