heimdall-tide 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/LICENSE +334 -0
  2. package/README.md +3 -0
  3. package/dist/cjs/enclaves/ApprovalEnclave.d.ts +12 -0
  4. package/dist/cjs/enclaves/ApprovalEnclave.js +57 -0
  5. package/dist/cjs/enclaves/ApprovalEnclave.js.map +1 -0
  6. package/dist/cjs/enclaves/RequestEnclave.d.ts +38 -0
  7. package/dist/cjs/enclaves/RequestEnclave.js +204 -0
  8. package/dist/cjs/enclaves/RequestEnclave.js.map +1 -0
  9. package/dist/cjs/heimdall.d.ts +46 -0
  10. package/dist/cjs/heimdall.js +168 -0
  11. package/dist/cjs/heimdall.js.map +1 -0
  12. package/dist/cjs/index.d.ts +4 -0
  13. package/dist/cjs/index.js +8 -0
  14. package/dist/cjs/index.js.map +1 -0
  15. package/dist/cjs/wrapper.d.ts +16 -0
  16. package/dist/cjs/wrapper.js +232 -0
  17. package/dist/cjs/wrapper.js.map +1 -0
  18. package/dist/esm/enclaves/ApprovalEnclave.d.ts +12 -0
  19. package/dist/esm/enclaves/ApprovalEnclave.js +53 -0
  20. package/dist/esm/enclaves/ApprovalEnclave.js.map +1 -0
  21. package/dist/esm/enclaves/RequestEnclave.d.ts +38 -0
  22. package/dist/esm/enclaves/RequestEnclave.js +200 -0
  23. package/dist/esm/enclaves/RequestEnclave.js.map +1 -0
  24. package/dist/esm/heimdall.d.ts +46 -0
  25. package/dist/esm/heimdall.js +164 -0
  26. package/dist/esm/heimdall.js.map +1 -0
  27. package/dist/esm/index.d.ts +4 -0
  28. package/dist/esm/index.js +5 -0
  29. package/dist/esm/index.js.map +1 -0
  30. package/dist/esm/wrapper.d.ts +16 -0
  31. package/dist/esm/wrapper.js +225 -0
  32. package/dist/esm/wrapper.js.map +1 -0
  33. package/heimdall-tide-0.1.0.tgz +0 -0
  34. package/package.json +44 -0
  35. package/src/enclaves/ApprovalEnclave.ts +63 -0
  36. package/src/enclaves/RequestEnclave.ts +237 -0
  37. package/src/heimdall.ts +204 -0
  38. package/src/index.ts +4 -0
  39. package/src/wrapper.ts +258 -0
  40. package/tsconfig.cjs.json +9 -0
  41. package/tsconfig.esm.json +10 -0
  42. package/tsconfig.json +9 -0
@@ -0,0 +1,200 @@
1
+ import { Heimdall, windowType } from "../heimdall";
2
+ export class RequestEnclave extends Heimdall {
3
+ constructor() {
4
+ super(...arguments);
5
+ this._windowType = windowType.Hidden;
6
+ this.initDone = this.recieve("init done");
7
+ }
8
+ init(data) {
9
+ if (!data.doken)
10
+ throw 'Doken not provided';
11
+ this.doken = data.doken;
12
+ this.dokenRefreshCallback = data.dokenRefreshCallback;
13
+ this.requireReloginCallback = data.requireReloginCallback;
14
+ this.recieve("hidden enclave").then((data) => this.handleHiddenEnclaveResponse(data));
15
+ this.open().then((success) => {
16
+ if (success) {
17
+ this.send({
18
+ type: "init",
19
+ message: {
20
+ doken: this.doken
21
+ }
22
+ });
23
+ }
24
+ else
25
+ throw 'Error opening enclave';
26
+ });
27
+ return this;
28
+ }
29
+ async handleHiddenEnclaveResponse(msg) {
30
+ // Below is the session key mismatch flow that was implemented but then it was decided a basic relogin was more elegent
31
+ // Keeping it though because it is nearly identical to the flow where a tide user delegates a token to another tide user
32
+ // This would require the second tide user to sign a new delegated token with their current session key
33
+ // This would be gold in a cvk scenario
34
+ // if(msg == "session key mismatch" && this._windowType == windowType.Hidden){
35
+ // this.initDone = this.recieve("init done"); // await the REOPENED HIDDEN ENCLAVE INIT DONE SIGNAL
36
+ // // looks like the hidden iframe has not allowed data to be stored on the browser OR the session key is mismatched with whats on the enclave vs doken
37
+ // // either way we gotta get a doken with the appropriate session key
38
+ // // Close the hidden enclave
39
+ // this.close();
40
+ // // We're now going to open the request enclave as a popup with the mismatched doken
41
+ // // The page should recognise the doken is mismatched, generate a new one, then close
42
+ // this._windowType = windowType.Popup;
43
+ // // open popup
44
+ // await this.open();
45
+ // // send doken to refresh
46
+ // this.send({
47
+ // type: "init",
48
+ // message:{
49
+ // doken: this.doken
50
+ // }
51
+ // });
52
+ // // wait for new doken
53
+ // const resp = await this.recieve("refreshed doken");
54
+ // this.doken = resp.doken;
55
+ // if(this.requireReloginCallback) this.requireReloginCallback();
56
+ // // close pop up enclave
57
+ // this.close();
58
+ // // reset page to hidden iframe
59
+ // this._windowType = windowType.Hidden;
60
+ // // open hidden iframe
61
+ // this.open().then((success: boolean) => {
62
+ // if(success){
63
+ // this.send({
64
+ // type: "init",
65
+ // message: {
66
+ // doken: this.doken
67
+ // }
68
+ // });
69
+ // }else throw 'Error opening enclave';
70
+ // });
71
+ // }
72
+ if (msg == "session key mismatch") {
73
+ this.requireReloginCallback(); // should initiate a full client page reload, killing this
74
+ }
75
+ this.recieve("hidden enclave").then((data) => this.handleHiddenEnclaveResponse(data));
76
+ }
77
+ getOrkUrl() {
78
+ // construct ork url
79
+ const url = new URL(this.enclaveOrigin);
80
+ // Set hidden status
81
+ url.searchParams.set("hidden", this._windowType == windowType.Hidden ? "true" : "false");
82
+ // Set vendor public
83
+ url.searchParams.set("vendorId", this.vendorId);
84
+ // Set client origin
85
+ url.searchParams.set("origin", encodeURIComponent(window.location.origin));
86
+ // Set client origin signature (by vendor)
87
+ url.searchParams.set("originsig", encodeURIComponent(this.signed_client_origin));
88
+ // Set voucher url
89
+ url.searchParams.set("voucherURL", encodeURIComponent(this.voucherURL));
90
+ // Set requestsed enclave
91
+ url.searchParams.set("type", "request");
92
+ return url;
93
+ }
94
+ checkEnclaveOpen() {
95
+ if (this.enclaveClosed()) {
96
+ // Enclave was closed!
97
+ // We need to reopen the enclave and await the init again
98
+ this.initDone = this.recieve("init done");
99
+ this.open().then((success) => {
100
+ if (success) {
101
+ this.send({
102
+ type: "init",
103
+ message: {
104
+ doken: this.doken
105
+ }
106
+ });
107
+ }
108
+ else
109
+ throw 'Error opening enclave';
110
+ });
111
+ }
112
+ }
113
+ async execute(data) {
114
+ this.checkEnclaveOpen();
115
+ await this.initDone;
116
+ const pre_resp = this.recieve("sign request completed");
117
+ this.send({
118
+ type: "request",
119
+ message: {
120
+ flow: "sign",
121
+ request: data,
122
+ }
123
+ });
124
+ const resp = await pre_resp;
125
+ if (!Array.isArray(resp))
126
+ throw 'Expecting request completed data to be an array, not' + resp;
127
+ if (!resp.every((d) => d instanceof Uint8Array))
128
+ throw 'Expecting all entries in response to be Uint8Arrays';
129
+ return resp;
130
+ }
131
+ async decrypt(data) {
132
+ this.checkEnclaveOpen();
133
+ await this.initDone;
134
+ const pre_resp = this.recieve("decrypt request completed");
135
+ this.send({
136
+ type: "request",
137
+ message: {
138
+ flow: "decrypt",
139
+ request: data
140
+ }
141
+ });
142
+ const resp = await pre_resp;
143
+ if (!Array.isArray(resp))
144
+ throw 'Expecting request completed data to be an array, not' + resp;
145
+ if (!resp.every((d) => d instanceof Uint8Array))
146
+ throw 'Expecting all entries in response to be Uint8Arrays';
147
+ return resp;
148
+ }
149
+ async encrypt(data) {
150
+ this.checkEnclaveOpen();
151
+ await this.initDone;
152
+ const pre_resp = this.recieve("encrypt request completed");
153
+ this.send({
154
+ type: "request",
155
+ message: {
156
+ flow: "encrypt",
157
+ request: data
158
+ }
159
+ });
160
+ const resp = await pre_resp;
161
+ if (!Array.isArray(resp))
162
+ throw 'Expecting request completed data to be an array, not' + resp;
163
+ if (!resp.every((d) => d instanceof Uint8Array))
164
+ throw 'Expecting all entries in response to be Uint8Arrays';
165
+ return resp;
166
+ }
167
+ async updateDoken(doken) {
168
+ this.doken = doken;
169
+ this.send({
170
+ type: "doken refresh",
171
+ message: {
172
+ doken: this.doken
173
+ }
174
+ });
175
+ }
176
+ async onerror(data) {
177
+ if (typeof data.message === "string") {
178
+ switch (data.message) {
179
+ case "expired":
180
+ if (!this.dokenRefreshCallback) {
181
+ console.error("[HEIMDALL] Doken on enclave has expired but there is no Doken Refresh Callback registered");
182
+ return;
183
+ }
184
+ console.log("[HEIMDALL] Refreshing doken");
185
+ this.doken = await this.dokenRefreshCallback();
186
+ this.send({
187
+ type: "doken refresh",
188
+ message: {
189
+ doken: this.doken
190
+ }
191
+ });
192
+ break;
193
+ default:
194
+ this.close();
195
+ throw new Error("[HEIMDALL] Recieved enclave error: " + data.message);
196
+ }
197
+ }
198
+ }
199
+ }
200
+ //# sourceMappingURL=RequestEnclave.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"RequestEnclave.js","sourceRoot":"","sources":["../../../src/enclaves/RequestEnclave.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAenD,MAAM,OAAO,cAAe,SAAQ,QAAwB;IAA5D;;QAKI,gBAAW,GAAe,UAAU,CAAC,MAAM,CAAC;QAEpC,aAAQ,GAAiB,IAAI,CAAC,OAAO,CAAC,WAAW,CAAC,CAAC;IA6M/D,CAAC;IA3MG,IAAI,CAAC,IAAgB;QACjB,IAAG,CAAC,IAAI,CAAC,KAAK;YAAE,MAAM,oBAAoB,CAAC;QAE3C,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC;QACxB,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,CAAC;QACtD,IAAI,CAAC,sBAAsB,GAAG,IAAI,CAAC,sBAAsB,CAAC;QAE1D,IAAI,CAAC,OAAO,CAAC,gBAAgB,CAAC,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,IAAI,CAAC,2BAA2B,CAAC,IAAI,CAAC,CAAC,CAAC;QAEtF,IAAI,CAAC,IAAI,EAAE,CAAC,IAAI,CAAC,CAAC,OAAgB,EAAE,EAAE;YAClC,IAAG,OAAO,EAAC,CAAC;gBACR,IAAI,CAAC,IAAI,CAAC;oBACN,IAAI,EAAE,MAAM;oBACZ,OAAO,EAAE;wBACL,KAAK,EAAE,IAAI,CAAC,KAAK;qBACpB;iBACJ,CAAC,CAAC;YACP,CAAC;;gBAAK,MAAM,uBAAuB,C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BACJ,KAAK,EAAE,IAAI,CAAC,KAAK;yBACpB;qBACJ,CAAC,CAAC;oBACH,MAAM;gBACV;oBACI,IAAI,CAAC,KAAK,EAAE,CAAC;oBACb,MAAM,IAAI,KAAK,CAAC,qCAAqC,GAAG,IAAI,CAAC,OAAO,CAAC,CAAC;YAC9E,CAAC;QACL,CAAC;IACL,CAAC;CACJ"}
@@ -0,0 +1,46 @@
1
+ export interface HeimdallConstructor {
2
+ vendorId: string;
3
+ homeOrkOrigin: string;
4
+ voucherURL: string;
5
+ signed_client_origin: string;
6
+ }
7
+ export declare abstract class Heimdall<T> implements EnclaveFlow<T> {
8
+ name: string;
9
+ _windowType: windowType;
10
+ enclaveOrigin: string;
11
+ voucherURL: string;
12
+ signed_client_origin: string;
13
+ vendorId: string;
14
+ private enclaveWindow;
15
+ constructor(init: HeimdallConstructor);
16
+ enclaveClosed(): boolean;
17
+ getOrkUrl(): URL;
18
+ open(): Promise<boolean>;
19
+ send(data: any): void;
20
+ recieve(type: string): Promise<any>;
21
+ close(): void;
22
+ onerror(data: any): void;
23
+ private openPopUp;
24
+ private closeHiddenIframe;
25
+ private openHiddenIframe;
26
+ private closePopupEnclave;
27
+ private waitForWindowPostMessage;
28
+ private sendPostWindowMessage;
29
+ private processEvent;
30
+ }
31
+ export declare enum windowType {
32
+ Popup = 0,
33
+ Redirect = 1,
34
+ Hidden = 2
35
+ }
36
+ interface EnclaveFlow<T> {
37
+ name: string;
38
+ _windowType: windowType;
39
+ open(): Promise<boolean>;
40
+ send(data: any): void;
41
+ recieve(type: string): Promise<any>;
42
+ close(): void;
43
+ onerror(data: any): void;
44
+ getOrkUrl(): URL;
45
+ }
46
+ export {};
@@ -0,0 +1,164 @@
1
+ //
2
+ // Tide Protocol - Infrastructure for a TRUE Zero-Trust paradigm
3
+ // Copyright (C) 2022 Tide Foundation Ltd
4
+ //
5
+ // This program is free software and is subject to the terms of
6
+ // the Tide Community Open Code License as published by the
7
+ // Tide Foundation Limited. You may modify it and redistribute
8
+ // it in accordance with and subject to the terms of that License.
9
+ // This program is distributed WITHOUT WARRANTY of any kind,
10
+ // including without any implied warranty of MERCHANTABILITY or
11
+ // FITNESS FOR A PARTICULAR PURPOSE.
12
+ // See the Tide Community Open Code License for more details.
13
+ // You should have received a copy of the Tide Community Open
14
+ // Code License along with this program.
15
+ // If not, see https://tide.org/licenses_tcoc2-0-0-en
16
+ export class Heimdall {
17
+ constructor(init) {
18
+ this.enclaveOrigin = init.homeOrkOrigin;
19
+ this.voucherURL = init.voucherURL;
20
+ this.signed_client_origin = init.signed_client_origin;
21
+ this.vendorId = init.vendorId;
22
+ }
23
+ enclaveClosed() {
24
+ return this.enclaveWindow.closed;
25
+ }
26
+ getOrkUrl() {
27
+ throw new Error("Method not implemented.");
28
+ }
29
+ async open() {
30
+ switch (this._windowType) {
31
+ case windowType.Popup:
32
+ return this.openPopUp();
33
+ case windowType.Redirect:
34
+ throw new Error("Method not implemented.");
35
+ case windowType.Hidden:
36
+ return this.openHiddenIframe();
37
+ }
38
+ }
39
+ send(data) {
40
+ switch (this._windowType) {
41
+ case windowType.Popup:
42
+ this.sendPostWindowMessage(data);
43
+ break;
44
+ case windowType.Redirect:
45
+ throw new Error("Method not implemented.");
46
+ case windowType.Hidden:
47
+ this.sendPostWindowMessage(data);
48
+ break;
49
+ }
50
+ }
51
+ async recieve(type) {
52
+ switch (this._windowType) {
53
+ case windowType.Popup:
54
+ return this.waitForWindowPostMessage(type);
55
+ case windowType.Redirect:
56
+ throw new Error("Method not implemented.");
57
+ case windowType.Hidden:
58
+ return this.waitForWindowPostMessage(type);
59
+ }
60
+ }
61
+ close() {
62
+ switch (this._windowType) {
63
+ case windowType.Popup:
64
+ this.closePopupEnclave();
65
+ break;
66
+ case windowType.Redirect:
67
+ throw new Error("Method not implemented.");
68
+ case windowType.Hidden:
69
+ this.closeHiddenIframe();
70
+ break;
71
+ default:
72
+ throw "Unknown window type";
73
+ }
74
+ }
75
+ onerror(data) {
76
+ throw new Error("Method not implemented.");
77
+ }
78
+ async openPopUp() {
79
+ const left_pos = (window.length / 2) - 400;
80
+ const w = window.open(this.getOrkUrl(), "_blank", `width=800,height=800,left=${left_pos}`);
81
+ if (!w)
82
+ return false;
83
+ this.enclaveWindow = w;
84
+ await this.waitForWindowPostMessage("pageLoaded"); // we need to wait for the page to load before we send sensitive data
85
+ return true;
86
+ }
87
+ async closeHiddenIframe() {
88
+ window.document
89
+ .querySelectorAll('iframe#heimdall')
90
+ .forEach(iframe => iframe.remove());
91
+ }
92
+ async openHiddenIframe() {
93
+ // Remove any existing iframes with heimdall id
94
+ this.closeHiddenIframe();
95
+ // 1. Create the iframe
96
+ const iframe = document.createElement('iframe');
97
+ iframe.src = this.getOrkUrl().toString();
98
+ iframe.style.display = 'none'; // hide it visually
99
+ iframe.id = "heimdall"; // in case multiple frames get popped up - we only want one
100
+ iframe.setAttribute('aria-hidden', 'true'); // accessibility hint
101
+ // 2. Add it to the document
102
+ document.body.appendChild(iframe);
103
+ // 3. Keep a reference to its window for postMessage
104
+ this.enclaveWindow = iframe.contentWindow;
105
+ if (!this.enclaveWindow)
106
+ return false;
107
+ // 4. Wait for the iframe to signal it’s ready
108
+ await this.waitForWindowPostMessage("pageLoaded");
109
+ return true;
110
+ }
111
+ closePopupEnclave() {
112
+ this.enclaveWindow.close();
113
+ }
114
+ async waitForWindowPostMessage(responseTypeToAwait) {
115
+ return new Promise((resolve) => {
116
+ const handler = (event) => {
117
+ const response = this.processEvent(event.data, event.origin, responseTypeToAwait);
118
+ if (response.ok) {
119
+ resolve(response.message);
120
+ window.removeEventListener("message", handler);
121
+ }
122
+ else {
123
+ if (response.print)
124
+ console.error("[HEIMDALL] Recieved enclave error: " + response.error);
125
+ }
126
+ };
127
+ window.addEventListener("message", handler, false);
128
+ });
129
+ }
130
+ sendPostWindowMessage(message) {
131
+ this.enclaveWindow.postMessage(message, this.enclaveOrigin);
132
+ }
133
+ processEvent(data, origin, expectedType) {
134
+ if (origin !== this.enclaveOrigin) {
135
+ // Something's not right... The message has come from an unknown domain...
136
+ return { ok: false, print: false, error: "WRONG WINDOW SENT MESSAGE" };
137
+ }
138
+ switch (data.type) {
139
+ case "newORKUrl":
140
+ this.enclaveOrigin = new URL(data.url).origin;
141
+ break;
142
+ case "error":
143
+ this.onerror(data);
144
+ return { ok: false, print: false, error: "handled error" };
145
+ }
146
+ if (expectedType !== data.type) {
147
+ console.log("[HEIMDALL] Received type{" + data.type + "} but waiting for type{" + expectedType + "}");
148
+ return { ok: false, print: false, error: "handled error" };
149
+ }
150
+ else {
151
+ console.log("[HEIMDALL] Correctly received type{" + data.type + "}");
152
+ return { ok: true, message: data.message };
153
+ }
154
+ }
155
+ }
156
+ export var windowType;
157
+ (function (windowType) {
158
+ windowType[windowType["Popup"] = 0] = "Popup";
159
+ windowType[windowType["Redirect"] = 1] = "Redirect";
160
+ windowType[windowType["Hidden"] = 2] = "Hidden";
161
+ })(windowType || (windowType = {}));
162
+ ;
163
+ ;
164
+ //# sourceMappingURL=heimdall.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"heimdall.js","sourceRoot":"","sources":["../../src/heimdall.ts"],"names":[],"mappings":"AAAA,GAAG;AACH,gEAAgE;AAChE,yCAAyC;AACzC,GAAG;AACH,gEAAgE;AAChE,4DAA4D;AAC5D,+DAA+D;AAC/D,kEAAkE;AAClE,6DAA6D;AAC7D,gEAAgE;AAChE,oCAAoC;AACpC,6DAA6D;AAC7D,8DAA8D;AAC9D,wCAAwC;AACxC,qDAAqD;AAUrD,MAAM,OAAgB,QAAQ;IAU1B,YAAY,IAAyB;QACjC,IAAI,CAAC,aAAa,GAAG,IAAI,CAAC,aAAa,CAAC;QACxC,IAAI,CAAC,UAAU,GAAG,IAAI,CAAC,UAAU,CAAC;QAClC,IAAI,CAAC,oBAAoB,GAAG,IAAI,CAAC,oBAAoB,CAAC;QACtD,IAAI,CAAC,QAAQ,GAAG,IAAI,CAAC,QAAQ,CAAC;IAClC,CAAC;IAED,aAAa;QACT,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;IACL,CAAC;CACJ;AACD,MAAM,CAAN,IAAY,UAIX;AAJD,WAAY,UAAU;IAClB,6CAAK,CAAA;IACL,mDAAQ,CAAA;IACR,+CAAM,CAAA;AACV,CAAC,EAJW,UAAU,KAAV,UAAU,QAIrB;AAAA,CAAC;AAaD,CAAC"}
@@ -0,0 +1,4 @@
1
+ import { ApprovalEnclave } from "./enclaves/ApprovalEnclave";
2
+ import { RequestEnclave } from "./enclaves/RequestEnclave";
3
+ export { ApprovalEnclave };
4
+ export { RequestEnclave };
@@ -0,0 +1,5 @@
1
+ import { ApprovalEnclave } from "./enclaves/ApprovalEnclave";
2
+ import { RequestEnclave } from "./enclaves/RequestEnclave";
3
+ export { ApprovalEnclave };
4
+ export { RequestEnclave };
5
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAA;AAC5D,OAAO,EAAE,cAAc,EAAE,MAAM,2BAA2B,CAAA;AAC1D,OAAO,EAAE,eAAe,EAAE,CAAA;AAC1B,OAAO,EAAE,cAAc,EAAE,CAAA"}
@@ -0,0 +1,16 @@
1
+ export declare const version = "1";
2
+ export declare function wrapper(arr: NestedEntry): TideMemory;
3
+ export declare function encodeStr(str: string, enc: string): Uint8Array;
4
+ export declare function encode(data: number | boolean | Uint8Array): Uint8Array | undefined;
5
+ interface entry {
6
+ value: any;
7
+ encoding?: string;
8
+ }
9
+ type NestedEntry = (entry | Uint8Array | NestedEntry)[];
10
+ export declare class TideMemory extends Uint8Array {
11
+ static CreateFromArray(datas: Uint8Array[]): TideMemory;
12
+ static Create(initialValue: Uint8Array, totalLength: number, version?: number): TideMemory;
13
+ WriteValue(index: number, value: Uint8Array): void;
14
+ GetValue<T extends Uint8Array>(index: number): T;
15
+ }
16
+ export {};