heimdall-tide 0.13.1 → 0.13.5

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 (59) hide show
  1. package/.github/PULL_REQUEST_TEMPLATE/release.md +34 -0
  2. package/.github/workflows/enforce_release.yml +87 -0
  3. package/dist/cjs/enclaves/ApprovalEnclaveNEW.d.ts +23 -0
  4. package/dist/cjs/enclaves/ApprovalEnclaveNEW.js +52 -0
  5. package/dist/cjs/enclaves/ApprovalEnclaveNEW.js.map +1 -0
  6. package/dist/cjs/enclaves/RequestEnclave.d.ts +9 -18
  7. package/dist/cjs/enclaves/RequestEnclave.js +20 -2
  8. package/dist/cjs/enclaves/RequestEnclave.js.map +1 -1
  9. package/dist/cjs/heimdall.d.ts +13 -0
  10. package/dist/cjs/heimdall.js +4 -1
  11. package/dist/cjs/heimdall.js.map +1 -1
  12. package/dist/cjs/index.d.ts +6 -0
  13. package/dist/cjs/index.js +13 -1
  14. package/dist/cjs/index.js.map +1 -1
  15. package/dist/cjs/models/PolicySignRequest.d.ts +7 -0
  16. package/dist/cjs/models/PolicySignRequest.js +18 -0
  17. package/dist/cjs/models/PolicySignRequest.js.map +1 -0
  18. package/dist/cjs/utils.d.ts +5 -0
  19. package/dist/cjs/utils.js +75 -0
  20. package/dist/cjs/utils.js.map +1 -0
  21. package/dist/cjs/wrapper.d.ts +1 -6
  22. package/dist/cjs/wrapper.js +4 -97
  23. package/dist/cjs/wrapper.js.map +1 -1
  24. package/dist/esm/enclaves/ApprovalEnclaveNEW.d.ts +23 -0
  25. package/dist/esm/enclaves/ApprovalEnclaveNEW.js +48 -0
  26. package/dist/esm/enclaves/ApprovalEnclaveNEW.js.map +1 -0
  27. package/dist/esm/enclaves/RequestEnclave.d.ts +9 -18
  28. package/dist/esm/enclaves/RequestEnclave.js +20 -2
  29. package/dist/esm/enclaves/RequestEnclave.js.map +1 -1
  30. package/dist/esm/heimdall.d.ts +13 -0
  31. package/dist/esm/heimdall.js +4 -1
  32. package/dist/esm/heimdall.js.map +1 -1
  33. package/dist/esm/index.d.ts +6 -0
  34. package/dist/esm/index.js +6 -0
  35. package/dist/esm/index.js.map +1 -1
  36. package/dist/esm/models/PolicySignRequest.d.ts +7 -0
  37. package/dist/esm/models/PolicySignRequest.js +15 -0
  38. package/dist/esm/models/PolicySignRequest.js.map +1 -0
  39. package/dist/esm/utils.d.ts +5 -0
  40. package/dist/esm/utils.js +68 -0
  41. package/dist/esm/utils.js.map +1 -0
  42. package/dist/esm/wrapper.d.ts +1 -6
  43. package/dist/esm/wrapper.js +1 -93
  44. package/dist/esm/wrapper.js.map +1 -1
  45. package/heimdall-tide-0.1.0.tgz +0 -0
  46. package/heimdall-tide-0.13.5.tgz +0 -0
  47. package/package.json +4 -1
  48. package/package.json.bak +4 -1
  49. package/src/enclaves/ApprovalEnclaveNEW.ts +51 -0
  50. package/src/enclaves/RequestEnclave.ts +34 -20
  51. package/src/heimdall.ts +17 -0
  52. package/src/index.ts +8 -1
  53. package/src/models/PolicySignRequest.ts +22 -0
  54. package/src/utils.ts +78 -0
  55. package/src/wrapper.ts +2 -118
  56. package/tsconfig.cjs.json +1 -0
  57. package/tsconfig.esm.json +1 -1
  58. package/tsconfig.json +4 -2
  59. package/heimdall-tide-0.13.1.tgz +0 -0
@@ -1,3 +1,4 @@
1
+ import { TideMemory } from "asgard-tide";
1
2
  export const version = "1";
2
3
  export function wrapper(arr) {
3
4
  // If array is only Uint8Arrays - create a TideMemory out of it
@@ -129,97 +130,4 @@ export function encode(data) {
129
130
  return undefined;
130
131
  }
131
132
  }
132
- // Tide Memory Object helper functions from tide-js
133
- export class TideMemory extends Uint8Array {
134
- static CreateFromArray(datas) {
135
- const length = datas.reduce((sum, next) => sum + next.length, 0);
136
- const mem = this.Create(datas[0], length);
137
- for (let i = 1; i < datas.length; i++) {
138
- mem.WriteValue(i, datas[i]);
139
- }
140
- return mem;
141
- }
142
- static Create(initialValue, totalLength, version = 1) {
143
- if (totalLength < initialValue.length + 4) {
144
- throw new Error("Not enough space to allocate requested data. Make sure to request more space in totalLength than length of InitialValue plus 4 bytes for length.");
145
- }
146
- // Total buffer length is 4 (version) + totalLength
147
- const bufferLength = 4 + totalLength;
148
- const buffer = new TideMemory(bufferLength);
149
- const dataView = new DataView(buffer.buffer);
150
- // Write version at position 0 (4 bytes)
151
- dataView.setInt32(0, version, true); // true for little-endian
152
- let dataLocationIndex = 4;
153
- // Write data length of initialValue at position 4 (4 bytes)
154
- dataView.setInt32(dataLocationIndex, initialValue.length, true);
155
- dataLocationIndex += 4;
156
- // Write initialValue starting from position 8
157
- buffer.set(initialValue, dataLocationIndex);
158
- return buffer;
159
- }
160
- WriteValue(index, value) {
161
- if (index < 0)
162
- throw new Error("Index cannot be less than 0");
163
- if (index === 0)
164
- throw new Error("Use CreateTideMemory to set value at index 0");
165
- if (this.length < 4 + value.length)
166
- throw new Error("Could not write to memory. Memory too small for this value");
167
- const dataView = new DataView(this.buffer);
168
- let dataLocationIndex = 4; // Start after the version number
169
- // Navigate through existing data segments
170
- for (let i = 0; i < index; i++) {
171
- if (dataLocationIndex + 4 > this.length) {
172
- throw new RangeError("Index out of range.");
173
- }
174
- // Read data length at current position
175
- const nextDataLength = dataView.getInt32(dataLocationIndex, true);
176
- dataLocationIndex += 4;
177
- dataLocationIndex += nextDataLength;
178
- }
179
- // Check if there's enough space to write the value
180
- if (dataLocationIndex + 4 + value.length > this.length) {
181
- throw new RangeError("Not enough space to write value");
182
- }
183
- // Check if data has already been written to this index
184
- const existingLength = dataView.getInt32(dataLocationIndex, true);
185
- if (existingLength !== 0) {
186
- throw new Error("Data has already been written to this index");
187
- }
188
- // Write data length of value at current position
189
- dataView.setInt32(dataLocationIndex, value.length, true);
190
- dataLocationIndex += 4;
191
- // Write value starting from current position
192
- this.set(value, dataLocationIndex);
193
- }
194
- GetValue(index) {
195
- // 'a' should be an ArrayBuffer or Uint8Array
196
- if (this.length < 4) {
197
- throw new Error("Insufficient data to read.");
198
- }
199
- // Create a DataView for reading integers in little-endian format
200
- const dataView = new DataView(this.buffer, this.byteOffset, this.byteLength);
201
- // Optional: Read the version if needed
202
- // const version = dataView.getInt32(0, true);
203
- let dataLocationIndex = 4;
204
- for (let i = 0; i < index; i++) {
205
- // Check if there's enough data to read the length of the next segment
206
- if (dataLocationIndex + 4 > this.length) {
207
- throw new RangeError("Index out of range.");
208
- }
209
- const nextDataLength = dataView.getInt32(dataLocationIndex, true);
210
- dataLocationIndex += 4 + nextDataLength;
211
- }
212
- // Check if there's enough data to read the length of the final segment
213
- if (dataLocationIndex + 4 > this.length) {
214
- throw new RangeError("Index out of range.");
215
- }
216
- const finalDataLength = dataView.getInt32(dataLocationIndex, true);
217
- dataLocationIndex += 4;
218
- // Check if the final data segment is within bounds
219
- if (dataLocationIndex + finalDataLength > this.length) {
220
- throw new RangeError("Index out of range.");
221
- }
222
- return this.subarray(dataLocationIndex, dataLocationIndex + finalDataLength);
223
- }
224
- }
225
133
  //# sourceMappingURL=wrapper.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"wrapper.js","sourceRoot":"","sources":["../../src/wrapper.ts"],"names":[],"mappings":"AAAA,MAAM,CAAC,MAAM,OAAO,GAAG,GAAG,CAAC;AAE3B,MAAM,UAAU,OAAO,CAAC,GAAgB;IACpC,gEAAgE;IAChE,0DAA0D;IAC1D,qDAAqD;IACrD,IAAG,GAAG,CAAC,KAAK,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,YAAY,UAAU,CAAC;QAAE,OAAO,UAAU,CAAC,eAAe,CAAC,GAAG,CAAC,CAAC;SAC9E,CAAC;QACF,wBAAwB;QACxB,GAAG,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE;YACd,kDAAkD;YAClD,IAAG,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,EAAC,CAAC;gBACjB,+DAA+D;gBAC/D,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,CAAC;YACnB,CAAC;iBAAK,IAAG,CAAC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AAE,CAAC,CAAC;IAC5D,CAAC;AACL,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,IAAmC;IACtD,QAAQ,OAAO,IAAI,EAAE,CAAC;QACtB,KAAK,QAAQ;YACT,MAAM,MAAM,GAAG,IAAI,WAAW,CAAC,CAAC,CAAC,CAAC;YAClC,MAAM,IAAI,GAAG,IAAI,QAAQ,CAAC,MAAM,CAAC,CAAC;YAClC,IAAI,CAAC,SAAS,CAAC,CAAC,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;YAC9B,OAAO,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;QAElC,KAAK,SAAS;YACZ,OAAO,IAAI,UAAU,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QAExC,KAAK,QAAQ;YACX,uEAAuE;YACvE,IAAI,IAAI,YAAY,UAAU,EAAE,CAAC;gBAC/B,OAAO,IAAI,UAAU,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,CAAC;YACvC,CAAC;YACD,yDAAyD;YACzD,MAAM,IAAI,SAAS,CAAC,4BAA4B,IAAI,EAAE,CAAC,CAAC;QAE1D;YACE,8CAA8C;YAC9C,OAAO,SAAS,CAAC;IACrB,CAAC;AACH,CAAC"}
Binary file
Binary file
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heimdall-tide",
3
- "version": "0.13.1",
3
+ "version": "0.13.5",
4
4
  "description": "SDK for communicating with a Tide Enclave",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -38,6 +38,9 @@
38
38
  "url": "https://github.com/tide-foundation/heimdall/issues"
39
39
  },
40
40
  "homepage": "https://github.com/tide-foundation/heimdall#readme",
41
+ "dependencies": {
42
+ "asgard-tide": "^0.13.5"
43
+ },
41
44
  "devDependencies": {
42
45
  "typescript": "^5.8.3"
43
46
  }
package/package.json.bak CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "heimdall-tide",
3
- "version": "0.1.0",
3
+ "version": "0.13.5",
4
4
  "description": "SDK for communicating with a Tide Enclave",
5
5
  "main": "dist/cjs/index.js",
6
6
  "module": "dist/esm/index.js",
@@ -38,6 +38,9 @@
38
38
  "url": "https://github.com/tide-foundation/heimdall/issues"
39
39
  },
40
40
  "homepage": "https://github.com/tide-foundation/heimdall#readme",
41
+ "dependencies": {
42
+ "asgard-tide": "file:~/asgard"
43
+ },
41
44
  "devDependencies": {
42
45
  "typescript": "^5.8.3"
43
46
  }
@@ -0,0 +1,51 @@
1
+ import {Heimdall, HiddenInit, windowType} from "../heimdall";
2
+ import { TideMemory } from "asgard-tide";
3
+ import { RequestEnclave } from "./RequestEnclave";
4
+
5
+ export class ApprovalEnclaveNew extends RequestEnclave{
6
+ name: string = "approvalNew";
7
+ _windowType: windowType = windowType.Popup;
8
+
9
+ init(data: HiddenInit): ApprovalEnclaveNew {
10
+ return super.init(data) as ApprovalEnclaveNew;
11
+ }
12
+
13
+ async approve(requestsToApprove: RequestToApprove[]) : Promise<OperatorApprovalResponse[]>{
14
+ // return fully serialized approved requests
15
+ this.checkEnclaveOpen();
16
+ await this.initDone;
17
+ const pre_resp = this.recieve("approvals");
18
+ this.send({
19
+ type: "approvalRequests",
20
+ message:{
21
+ requests: requestsToApprove,
22
+ }
23
+ })
24
+ const resp = await pre_resp;
25
+ if(!Array.isArray(resp)) throw 'Expecting request completed data to be an array, not' + resp;
26
+ if(!resp.every((d: any) => OperatorApprovalResponse.isOperatorApprovalResponse(d))) throw 'Expecting all entries in response to be OperatorApprovalResponse';
27
+ this.close();
28
+ return resp;
29
+ }
30
+
31
+ }
32
+ class RequestToApprove{
33
+ id: string;
34
+ request: TideMemory;
35
+ }
36
+ class OperatorApprovalResponse extends RequestToApprove{
37
+ status: Status;
38
+ static isOperatorApprovalResponse(object: any): object is OperatorApprovalResponse {
39
+ return (
40
+ object != null &&
41
+ typeof object.id === 'string' &&
42
+ (object.request instanceof Uint8Array || object.request == null) &&
43
+ Object.values(Status).includes(object.status)
44
+ );
45
+ }
46
+ }
47
+ enum Status{
48
+ Approved = "approved",
49
+ Denied = "denied",
50
+ Pending = "pending"
51
+ }
@@ -1,26 +1,16 @@
1
- import { Heimdall, windowType } from "../heimdall";
2
- import { TideMemory } from "../wrapper";
3
-
4
- interface HiddenInit{
5
- doken: string;
6
- /**
7
- * @returns A refresh doken for Heimdall
8
- */
9
- dokenRefreshCallback: () => Promise<string> | undefined;
10
- /**
11
- * @returns A function that re authenticates the current user from the client. (Used to update their session key on Identity System). Returns a new doken too.
12
- */
13
- requireReloginCallback: () => Promise<string>;
14
- }
1
+ import { BaseTideRequest } from "asgard-tide";
2
+ import { Heimdall, HiddenInit, windowType } from "../heimdall";
3
+ import { TideMemory } from "asgard-tide";
15
4
 
16
5
  export class RequestEnclave extends Heimdall<RequestEnclave>{
17
- private doken: string;
18
- private dokenRefreshCallback: () => Promise<string> | undefined;
19
- private requireReloginCallback: () => Promise<string>;
6
+ name: string = "request";
7
+ protected doken: string;
8
+ protected dokenRefreshCallback: () => Promise<string> | undefined;
9
+ protected requireReloginCallback: () => Promise<string>;
20
10
 
21
11
  _windowType: windowType = windowType.Hidden;
22
12
 
23
- private initDone: Promise<any> = this.recieve("init done");
13
+ protected initDone: Promise<any> = this.recieve("init done");
24
14
 
25
15
  init(data: HiddenInit): RequestEnclave {
26
16
  if(!data.doken) throw 'Doken not provided';
@@ -156,7 +146,7 @@ export class RequestEnclave extends Heimdall<RequestEnclave>{
156
146
  url.searchParams.set("voucherURL", encodeURIComponent(this.voucherURL));
157
147
 
158
148
  // Set requestsed enclave
159
- url.searchParams.set("type", "request");
149
+ url.searchParams.set("type", this.name);
160
150
 
161
151
  return url;
162
152
  }
@@ -178,7 +168,30 @@ export class RequestEnclave extends Heimdall<RequestEnclave>{
178
168
  }
179
169
  }
180
170
 
181
- async execute(data: TideMemory): Promise<Uint8Array[]>{
171
+ async initializeRequest(request: TideMemory): Promise<Uint8Array>{
172
+ // construct request to sign this request's creation
173
+ const requestToInitialize = BaseTideRequest.decode(request);
174
+ const requestToInitializeDetails = await requestToInitialize.getRequestInitDetails();
175
+ const initRequest = new BaseTideRequest(
176
+ "TideRequestInitialization",
177
+ "1",
178
+ "Doken:1",
179
+ TideMemory.CreateFromArray([
180
+ requestToInitializeDetails.creationTime,
181
+ requestToInitializeDetails.expireTime,
182
+ requestToInitializeDetails.modelId,
183
+ requestToInitializeDetails.draftHash
184
+ ]),
185
+ new TideMemory()
186
+ );
187
+
188
+ const creationSig = (await this.execute(initRequest.encode()))[0];
189
+
190
+ // returns the same request provided except with the policy authorized creation datas included
191
+ return requestToInitialize.addCreationSignature(requestToInitializeDetails.creationTime, creationSig).encode();
192
+ }
193
+
194
+ async execute(data: TideMemory, waitForAll: boolean = false): Promise<Uint8Array[]>{
182
195
  this.checkEnclaveOpen();
183
196
  await this.initDone;
184
197
  const pre_resp = this.recieve("sign request completed");
@@ -187,6 +200,7 @@ export class RequestEnclave extends Heimdall<RequestEnclave>{
187
200
  message:{
188
201
  flow: "sign",
189
202
  request: data,
203
+ waitForAll,
190
204
  }
191
205
  })
192
206
  const resp = await pre_resp;
package/src/heimdall.ts CHANGED
@@ -21,6 +21,7 @@ export interface HeimdallConstructor{
21
21
  homeOrkOrigin: string,
22
22
  voucherURL: string,
23
23
  signed_client_origin: string;
24
+ isRunningLocal?: boolean
24
25
  }
25
26
  export abstract class Heimdall<T> implements EnclaveFlow<T> {
26
27
  name: string;
@@ -29,6 +30,7 @@ export abstract class Heimdall<T> implements EnclaveFlow<T> {
29
30
  voucherURL: string;
30
31
  signed_client_origin: string;
31
32
  vendorId: string;
33
+ isRunningLocal: boolean;
32
34
 
33
35
  private enclaveWindow: WindowProxy;
34
36
 
@@ -37,6 +39,8 @@ export abstract class Heimdall<T> implements EnclaveFlow<T> {
37
39
  this.voucherURL = init.voucherURL;
38
40
  this.signed_client_origin = init.signed_client_origin;
39
41
  this.vendorId = init.vendorId;
42
+
43
+ this.isRunningLocal = init.isRunningLocal != null ? init.isRunningLocal : false;
40
44
  }
41
45
 
42
46
  enclaveClosed(){
@@ -134,6 +138,8 @@ export abstract class Heimdall<T> implements EnclaveFlow<T> {
134
138
  iframe.id = "heimdall"; // in case multiple frames get popped up - we only want one
135
139
  iframe.setAttribute('aria-hidden', 'true'); // accessibility hint
136
140
 
141
+ if(this.isRunningLocal) iframe.allow = "local-network-access";
142
+
137
143
  // 2. Add it to the document
138
144
  document.body.appendChild(iframe);
139
145
 
@@ -211,6 +217,17 @@ export enum windowType{
211
217
  Redirect,
212
218
  Hidden
213
219
  };
220
+ export interface HiddenInit{
221
+ doken: string;
222
+ /**
223
+ * @returns A refresh doken for Heimdall
224
+ */
225
+ dokenRefreshCallback: () => Promise<string> | undefined;
226
+ /**
227
+ * @returns A function that re authenticates the current user from the client. (Used to update their session key on Identity System). Returns a new doken too.
228
+ */
229
+ requireReloginCallback: () => Promise<string>;
230
+ }
214
231
  interface EnclaveFlow<T>{
215
232
  name: string;
216
233
  _windowType: windowType;
package/src/index.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import { ApprovalEnclave } from "./enclaves/ApprovalEnclave"
2
2
  import { RequestEnclave } from "./enclaves/RequestEnclave"
3
+ import { ApprovalEnclaveNew } from "./enclaves/ApprovalEnclaveNEW"
4
+ import PolicySignRequest from "./models/PolicySignRequest"
5
+ import { TideMemory, Policy, BaseTideRequest, PolicyParameters } from "asgard-tide"
6
+
3
7
  export { ApprovalEnclave }
4
- export { RequestEnclave }
8
+ export { RequestEnclave }
9
+ export { ApprovalEnclaveNew }
10
+ export { PolicySignRequest }
11
+ export { TideMemory, Policy, BaseTideRequest, PolicyParameters }
@@ -0,0 +1,22 @@
1
+ import { TideMemory } from "asgard-tide";
2
+ import { BaseTideRequest } from "asgard-tide";
3
+ import { Policy } from "asgard-tide";
4
+
5
+ export default class PolicySignRequest extends BaseTideRequest {
6
+ constructor(name: string, version: string, authFlow: string, draft: Uint8Array, dyanmicData: Uint8Array) {
7
+ super(name, version, authFlow, draft, dyanmicData);
8
+ }
9
+ static New(policy: Policy) : PolicySignRequest{
10
+ return new PolicySignRequest(
11
+ "Policy",
12
+ "1",
13
+ "Policy:1",
14
+ TideMemory.CreateFromArray([policy.toBytes()]),
15
+ new TideMemory()
16
+ );
17
+ }
18
+
19
+ getRequestedPolicy(){
20
+ return Policy.from(this.draft.GetValue(0));
21
+ }
22
+ }
package/src/utils.ts ADDED
@@ -0,0 +1,78 @@
1
+ const base64abc = [
2
+ "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M",
3
+ "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z",
4
+ "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m",
5
+ "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z",
6
+ "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "+", "/"
7
+ ];
8
+ export function bytesToBase64(bytes: Uint8Array) : string {
9
+ let result = '', i, l = bytes.length;
10
+ for (i = 2; i < l; i += 3) {
11
+ result += base64abc[bytes[i - 2] >> 2];
12
+ result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
13
+ result += base64abc[((bytes[i - 1] & 0x0F) << 2) | (bytes[i] >> 6)];
14
+ result += base64abc[bytes[i] & 0x3F];
15
+ }
16
+ if (i === l + 1) { // 1 octet yet to write
17
+ result += base64abc[bytes[i - 2] >> 2];
18
+ result += base64abc[(bytes[i - 2] & 0x03) << 4];
19
+ result += "==";
20
+ }
21
+ if (i === l) { // 2 octets yet to write
22
+ result += base64abc[bytes[i - 2] >> 2];
23
+ result += base64abc[((bytes[i - 2] & 0x03) << 4) | (bytes[i - 1] >> 4)];
24
+ result += base64abc[(bytes[i - 1] & 0x0F) << 2];
25
+ result += "=";
26
+ }
27
+ return result;
28
+ }
29
+
30
+ export function StringToUint8Array(string: string) : Uint8Array {
31
+ const enc = new TextEncoder();
32
+ return enc.encode(string);
33
+ }
34
+ export function StringFromUint8Array(bytes: Uint8Array) : string {
35
+ const enc = new TextDecoder();
36
+ return enc.decode(bytes);
37
+ }
38
+
39
+ export function BigIntToByteArray(value: bigint): Uint8Array {
40
+ if (value < 0n) {
41
+ throw new Error("Negative BigInt values are not supported");
42
+ }
43
+
44
+ const bytes: number[] = [];
45
+ let temp = value;
46
+
47
+ // Extract bytes in little-endian order
48
+ while (temp > 0n) {
49
+ bytes.push(Number(temp & 0xFFn));
50
+ temp = temp >> 8n;
51
+ }
52
+
53
+ // Handle zero case
54
+ if (bytes.length === 0) {
55
+ bytes.push(0);
56
+ }
57
+
58
+ // Pad or trim to specified length
59
+ const targetLength = bytes.length;
60
+ const result = new Uint8Array(targetLength);
61
+
62
+ for (let i = 0; i < Math.min(bytes.length, targetLength); i++) {
63
+ result[i] = bytes[i];
64
+ }
65
+
66
+ return result;
67
+ }
68
+
69
+ export function BigIntFromByteArray(bytes: Uint8Array): bigint {
70
+ let result = 0n;
71
+
72
+ // Read bytes in little-endian order
73
+ for (let i = bytes.length - 1; i >= 0; i--) {
74
+ result = (result << 8n) | BigInt(bytes[i]);
75
+ }
76
+
77
+ return result;
78
+ }
package/src/wrapper.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { TideMemory } from "asgard-tide";
1
2
  export const version = "1";
2
3
 
3
4
  export function wrapper(arr: NestedEntry): TideMemory {
@@ -138,121 +139,4 @@ interface entry{
138
139
  value: any;
139
140
  encoding?: string;
140
141
  }
141
- type NestedEntry = (entry | Uint8Array | NestedEntry)[]; // added Uint8Array as an optional type so we can serialize it without deep copy
142
-
143
-
144
- // Tide Memory Object helper functions from tide-js
145
- export class TideMemory extends Uint8Array{
146
- static CreateFromArray(datas: Uint8Array[]): TideMemory {
147
- const length = datas.reduce((sum, next) => sum + next.length, 0);
148
- const mem = this.Create(datas[0], length);
149
- for(let i = 1; i < datas.length; i++){
150
- mem.WriteValue(i, datas[i]);
151
- }
152
- return mem;
153
- }
154
- static Create(initialValue: Uint8Array, totalLength: number, version: number = 1): TideMemory {
155
- if (totalLength < initialValue.length + 4) {
156
- throw new Error("Not enough space to allocate requested data. Make sure to request more space in totalLength than length of InitialValue plus 4 bytes for length.");
157
- }
158
-
159
- // Total buffer length is 4 (version) + totalLength
160
- const bufferLength = 4 + totalLength;
161
- const buffer = new TideMemory(bufferLength);
162
- const dataView = new DataView(buffer.buffer);
163
-
164
- // Write version at position 0 (4 bytes)
165
- dataView.setInt32(0, version, true); // true for little-endian
166
-
167
- let dataLocationIndex = 4;
168
-
169
- // Write data length of initialValue at position 4 (4 bytes)
170
- dataView.setInt32(dataLocationIndex, initialValue.length, true);
171
- dataLocationIndex += 4;
172
-
173
- // Write initialValue starting from position 8
174
- buffer.set(initialValue, dataLocationIndex);
175
-
176
- return buffer;
177
- }
178
-
179
- WriteValue(index: number, value: Uint8Array): void {
180
- if (index < 0) throw new Error("Index cannot be less than 0");
181
- if (index === 0) throw new Error("Use CreateTideMemory to set value at index 0");
182
- if (this.length < 4 + value.length) throw new Error("Could not write to memory. Memory too small for this value");
183
-
184
- const dataView = new DataView(this.buffer);
185
- let dataLocationIndex = 4; // Start after the version number
186
-
187
- // Navigate through existing data segments
188
- for (let i = 0; i < index; i++) {
189
- if (dataLocationIndex + 4 > this.length) {
190
- throw new RangeError("Index out of range.");
191
- }
192
-
193
- // Read data length at current position
194
- const nextDataLength = dataView.getInt32(dataLocationIndex, true);
195
- dataLocationIndex += 4;
196
-
197
- dataLocationIndex += nextDataLength;
198
- }
199
-
200
- // Check if there's enough space to write the value
201
- if (dataLocationIndex + 4 + value.length > this.length) {
202
- throw new RangeError("Not enough space to write value");
203
- }
204
-
205
- // Check if data has already been written to this index
206
- const existingLength = dataView.getInt32(dataLocationIndex, true);
207
- if (existingLength !== 0) {
208
- throw new Error("Data has already been written to this index");
209
- }
210
-
211
- // Write data length of value at current position
212
- dataView.setInt32(dataLocationIndex, value.length, true);
213
- dataLocationIndex += 4;
214
-
215
- // Write value starting from current position
216
- this.set(value, dataLocationIndex);
217
- }
218
-
219
- GetValue<T extends Uint8Array>(index: number): T{
220
- // 'a' should be an ArrayBuffer or Uint8Array
221
- if (this.length < 4) {
222
- throw new Error("Insufficient data to read.");
223
- }
224
-
225
- // Create a DataView for reading integers in little-endian format
226
- const dataView = new DataView(this.buffer, this.byteOffset, this.byteLength);
227
-
228
- // Optional: Read the version if needed
229
- // const version = dataView.getInt32(0, true);
230
-
231
- let dataLocationIndex = 4;
232
-
233
- for (let i = 0; i < index; i++) {
234
- // Check if there's enough data to read the length of the next segment
235
- if (dataLocationIndex + 4 > this.length) {
236
- throw new RangeError("Index out of range.");
237
- }
238
-
239
- const nextDataLength = dataView.getInt32(dataLocationIndex, true);
240
- dataLocationIndex += 4 + nextDataLength;
241
- }
242
-
243
- // Check if there's enough data to read the length of the final segment
244
- if (dataLocationIndex + 4 > this.length) {
245
- throw new RangeError("Index out of range.");
246
- }
247
-
248
- const finalDataLength = dataView.getInt32(dataLocationIndex, true);
249
- dataLocationIndex += 4;
250
-
251
- // Check if the final data segment is within bounds
252
- if (dataLocationIndex + finalDataLength > this.length) {
253
- throw new RangeError("Index out of range.");
254
- }
255
-
256
- return this.subarray(dataLocationIndex, dataLocationIndex + finalDataLength) as T;
257
- }
258
- }
142
+ type NestedEntry = (entry | Uint8Array | NestedEntry)[]; // added Uint8Array as an optional type so we can serialize it without deep copy
package/tsconfig.cjs.json CHANGED
@@ -3,6 +3,7 @@
3
3
  "extends": "./tsconfig.json",
4
4
  "compilerOptions": {
5
5
  "module": "CommonJS",
6
+ "moduleResolution": "node",
6
7
  "outDir": "dist/cjs"
7
8
  },
8
9
  "include": ["src"]
package/tsconfig.esm.json CHANGED
@@ -3,7 +3,7 @@
3
3
  "extends": "./tsconfig.json",
4
4
  "compilerOptions": {
5
5
  "module": "ESNext",
6
- "moduleResolution": "node10",
6
+ "moduleResolution": "node",
7
7
  "outDir": "dist/esm"
8
8
  },
9
9
  "include": ["src"]
package/tsconfig.json CHANGED
@@ -1,9 +1,11 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "esModuleInterop": true,
4
- "declaration": true,
5
- "target": "es2017",
4
+ "declaration": true,
5
+ "target": "es2021",
6
6
  "sourceMap": true,
7
+ "moduleResolution": "node",
8
+ "skipLibCheck": true
7
9
  },
8
10
  "include": ["src"]
9
11
  }
Binary file