asgard-tide 0.0.1

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 (75) hide show
  1. package/LICENSE +334 -0
  2. package/README.md +103 -0
  3. package/dist/cjs/contracts/BaseContract.d.ts +47 -0
  4. package/dist/cjs/contracts/BaseContract.d.ts.map +1 -0
  5. package/dist/cjs/contracts/BaseContract.js +162 -0
  6. package/dist/cjs/contracts/BaseContract.js.map +1 -0
  7. package/dist/cjs/contracts/GenericRealmAccessThresholdRoleContract.d.ts +8 -0
  8. package/dist/cjs/contracts/GenericRealmAccessThresholdRoleContract.d.ts.map +1 -0
  9. package/dist/cjs/contracts/GenericRealmAccessThresholdRoleContract.js +26 -0
  10. package/dist/cjs/contracts/GenericRealmAccessThresholdRoleContract.js.map +1 -0
  11. package/dist/cjs/contracts/GenericResourceAccessThresholdRoleContract.d.ts +8 -0
  12. package/dist/cjs/contracts/GenericResourceAccessThresholdRoleContract.d.ts.map +1 -0
  13. package/dist/cjs/contracts/GenericResourceAccessThresholdRoleContract.js +26 -0
  14. package/dist/cjs/contracts/GenericResourceAccessThresholdRoleContract.js.map +1 -0
  15. package/dist/cjs/index.d.ts +15 -0
  16. package/dist/cjs/index.d.ts.map +1 -0
  17. package/dist/cjs/index.js +26 -0
  18. package/dist/cjs/index.js.map +1 -0
  19. package/dist/cjs/models/CustomTideRequest.d.ts +19 -0
  20. package/dist/cjs/models/CustomTideRequest.d.ts.map +1 -0
  21. package/dist/cjs/models/CustomTideRequest.js +40 -0
  22. package/dist/cjs/models/CustomTideRequest.js.map +1 -0
  23. package/dist/cjs/models/Policy.d.ts +41 -0
  24. package/dist/cjs/models/Policy.d.ts.map +1 -0
  25. package/dist/cjs/models/Policy.js +268 -0
  26. package/dist/cjs/models/Policy.js.map +1 -0
  27. package/dist/cjs/models/TideRequest.d.ts +48 -0
  28. package/dist/cjs/models/TideRequest.d.ts.map +1 -0
  29. package/dist/cjs/models/TideRequest.js +255 -0
  30. package/dist/cjs/models/TideRequest.js.map +1 -0
  31. package/dist/cjs/utils/Serialization.d.ts +7 -0
  32. package/dist/cjs/utils/Serialization.d.ts.map +1 -0
  33. package/dist/cjs/utils/Serialization.js +106 -0
  34. package/dist/cjs/utils/Serialization.js.map +1 -0
  35. package/dist/cjs/utils/TideMemory.d.ts +10 -0
  36. package/dist/cjs/utils/TideMemory.d.ts.map +1 -0
  37. package/dist/cjs/utils/TideMemory.js +110 -0
  38. package/dist/cjs/utils/TideMemory.js.map +1 -0
  39. package/dist/esm/contracts/BaseContract.d.ts +47 -0
  40. package/dist/esm/contracts/BaseContract.d.ts.map +1 -0
  41. package/dist/esm/contracts/BaseContract.js +154 -0
  42. package/dist/esm/contracts/BaseContract.js.map +1 -0
  43. package/dist/esm/contracts/GenericRealmAccessThresholdRoleContract.d.ts +8 -0
  44. package/dist/esm/contracts/GenericRealmAccessThresholdRoleContract.d.ts.map +1 -0
  45. package/dist/esm/contracts/GenericRealmAccessThresholdRoleContract.js +22 -0
  46. package/dist/esm/contracts/GenericRealmAccessThresholdRoleContract.js.map +1 -0
  47. package/dist/esm/contracts/GenericResourceAccessThresholdRoleContract.d.ts +8 -0
  48. package/dist/esm/contracts/GenericResourceAccessThresholdRoleContract.d.ts.map +1 -0
  49. package/dist/esm/contracts/GenericResourceAccessThresholdRoleContract.js +22 -0
  50. package/dist/esm/contracts/GenericResourceAccessThresholdRoleContract.js.map +1 -0
  51. package/dist/esm/index.d.ts +15 -0
  52. package/dist/esm/index.d.ts.map +1 -0
  53. package/dist/esm/index.js +15 -0
  54. package/dist/esm/index.js.map +1 -0
  55. package/dist/esm/models/CustomTideRequest.d.ts +19 -0
  56. package/dist/esm/models/CustomTideRequest.d.ts.map +1 -0
  57. package/dist/esm/models/CustomTideRequest.js +31 -0
  58. package/dist/esm/models/CustomTideRequest.js.map +1 -0
  59. package/dist/esm/models/Policy.d.ts +41 -0
  60. package/dist/esm/models/Policy.d.ts.map +1 -0
  61. package/dist/esm/models/Policy.js +263 -0
  62. package/dist/esm/models/Policy.js.map +1 -0
  63. package/dist/esm/models/TideRequest.d.ts +48 -0
  64. package/dist/esm/models/TideRequest.d.ts.map +1 -0
  65. package/dist/esm/models/TideRequest.js +252 -0
  66. package/dist/esm/models/TideRequest.js.map +1 -0
  67. package/dist/esm/utils/Serialization.d.ts +7 -0
  68. package/dist/esm/utils/Serialization.d.ts.map +1 -0
  69. package/dist/esm/utils/Serialization.js +98 -0
  70. package/dist/esm/utils/Serialization.js.map +1 -0
  71. package/dist/esm/utils/TideMemory.d.ts +10 -0
  72. package/dist/esm/utils/TideMemory.d.ts.map +1 -0
  73. package/dist/esm/utils/TideMemory.js +106 -0
  74. package/dist/esm/utils/TideMemory.js.map +1 -0
  75. package/package.json +41 -0
@@ -0,0 +1,154 @@
1
+ import { ApprovalType, ExecutionType, Policy } from "../models/Policy";
2
+ import BaseTideRequest from "../models/TideRequest";
3
+ import { StringFromUint8Array } from "../utils/Serialization";
4
+ export class BaseContract {
5
+ /**
6
+ * Inheritors must implement this if the policy has set it's approvalType to EXPLICIT
7
+ * @param policy Policy object
8
+ * @param approverDokens Approver Dokens
9
+ */
10
+ validateApprovers(policy, approverDokens) {
11
+ throw `validateApprovers not implemented`;
12
+ }
13
+ /**
14
+ * Inheritors must implement this if the policy has set it's executionType to PRIVATE
15
+ * @param policy Policy object
16
+ * @param executorDoken Executor Doken
17
+ */
18
+ validateExecutor(policy, executorDoken) {
19
+ throw `validateExecutor not implemented`;
20
+ }
21
+ /**
22
+ * To help with clients testing if their Tide Request will pass their contract's specified contract
23
+ * @param policy Serialized policy from Tide
24
+ * @returns
25
+ */
26
+ async testPolicy(policy, executorDoken = null) {
27
+ const p = policy instanceof Uint8Array ? Policy.from(policy) : policy;
28
+ if (p.contractId !== this.id)
29
+ throw `Mismatch between policy provided's contract (${p.contractId}) and this contract's id (${this.id})`;
30
+ if (p.modelId !== this.tideRequest.id() && p.modelId !== "any")
31
+ throw `Mismatch between policy provided model id (${p.modelId}) and tide request id (${this.tideRequest.id()})`;
32
+ try {
33
+ await this.validateData(p);
34
+ if (p.approvalType == ApprovalType.EXPLICIT)
35
+ await this.validateApprovers(p, this.dokens);
36
+ if (p.executionType == ExecutionType.PRIVATE) {
37
+ if (!executorDoken)
38
+ throw `Policy as set it's execution type to PRIVATE. You must test this with the doken of the executor`;
39
+ await this.validateExecutor(p, new Doken(executorDoken));
40
+ }
41
+ return {
42
+ success: true
43
+ };
44
+ }
45
+ catch (ex) {
46
+ return {
47
+ success: false,
48
+ failed: ex
49
+ };
50
+ }
51
+ }
52
+ constructor(tideRequest) {
53
+ this.dokens = []; // change to Doken type
54
+ this.tideRequest = tideRequest instanceof Uint8Array ? BaseTideRequest.decode(tideRequest) : tideRequest;
55
+ this.authorizedRequestPayload = this.tideRequest.draft;
56
+ this.informationalRequestPayload = this.tideRequest.dyanmicData;
57
+ // deserialize dokens
58
+ let res = { result: new Uint8Array() };
59
+ let i = 0;
60
+ while (this.tideRequest.authorizer.TryGetValue(i, res)) {
61
+ this.dokens.push(new Doken(res.result));
62
+ i++;
63
+ }
64
+ }
65
+ }
66
+ export class Doken {
67
+ constructor(d) {
68
+ if (!d || d.length === 0) {
69
+ throw new Error('Doken constructor: received empty or null Uint8Array');
70
+ }
71
+ const tokenString = typeof d === "string" ? d : StringFromUint8Array(d);
72
+ const s = tokenString.split(".");
73
+ if (s.length !== 3) {
74
+ throw new Error(`Doken constructor: invalid token format. Expected 3 parts (header.payload.signature) but got ${s.length} parts in: "${tokenString.substring(0, 50)}..."`);
75
+ }
76
+ try {
77
+ const decodedPayload = base64UrlDecode(s[1]);
78
+ this.payload = JSON.parse(decodedPayload);
79
+ }
80
+ catch (error) {
81
+ throw new Error(`Doken constructor: failed to parse token payload. ${error instanceof Error ? error.message : String(error)}. Raw payload part: "${s[1].substring(0, 50)}..."`);
82
+ }
83
+ if (!this.payload || typeof this.payload !== 'object') {
84
+ throw new Error(`Doken constructor: parsed payload is not a valid object. Got type: ${typeof this.payload}`);
85
+ }
86
+ }
87
+ hasResourceAccessRole(role, client) {
88
+ if (!role)
89
+ throw new Error('hasResourceAccessRole: role parameter is empty or undefined');
90
+ if (!client)
91
+ throw new Error('hasResourceAccessRole: client parameter is empty or undefined');
92
+ if (!this.payload.resource_access) {
93
+ return false;
94
+ }
95
+ if (!this.payload.resource_access[client]) {
96
+ return false;
97
+ }
98
+ if (!Array.isArray(this.payload.resource_access[client].roles)) {
99
+ return false;
100
+ }
101
+ return this.payload.resource_access[client].roles.includes(role);
102
+ }
103
+ hasRealmAccessRole(role) {
104
+ if (!role)
105
+ throw new Error('hasRealmAccessRole: role parameter is empty or undefined');
106
+ if (!this.payload.realm_access) {
107
+ return false;
108
+ }
109
+ if (!Array.isArray(this.payload.realm_access.roles)) {
110
+ return false;
111
+ }
112
+ return this.payload.realm_access.roles.includes(role);
113
+ }
114
+ hasVuid(vuid) {
115
+ if (!vuid)
116
+ throw new Error('hasVuid: vuid cannot be null');
117
+ if (!this.payload.vuid)
118
+ throw new Error("hasVuid: cannot find vuid in paylod");
119
+ return this.payload.vuid === vuid;
120
+ }
121
+ }
122
+ function base64UrlDecode(input) {
123
+ let output = input
124
+ .replaceAll("-", "+")
125
+ .replaceAll("_", "/");
126
+ switch (output.length % 4) {
127
+ case 0:
128
+ break;
129
+ case 2:
130
+ output += "==";
131
+ break;
132
+ case 3:
133
+ output += "=";
134
+ break;
135
+ default:
136
+ throw new Error("Input is not of the correct length.");
137
+ }
138
+ try {
139
+ return b64DecodeUnicode(output);
140
+ }
141
+ catch (error) {
142
+ return atob(output);
143
+ }
144
+ }
145
+ function b64DecodeUnicode(input) {
146
+ return decodeURIComponent(atob(input).replace(/(.)/g, (m, p) => {
147
+ let code = p.charCodeAt(0).toString(16).toUpperCase();
148
+ if (code.length < 2) {
149
+ code = "0" + code;
150
+ }
151
+ return "%" + code;
152
+ }));
153
+ }
154
+ //# sourceMappingURL=BaseContract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"B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IAAI,MAAM,GAAG,KAAK;SACb,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC;SACpB,UAAU,CAAC,GAAG,EAAE,GAAG,CAAC,CAAC;IAE1B,QAAQ,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACxB,KAAK,CAAC;YACF,MAAM;QACV,KAAK,CAAC;YACF,MAAM,IAAI,IAAI,CAAC;YACf,MAAM;QACV,KAAK,CAAC;YACF,MAAM,IAAI,GAAG,CAAC;YACd,MAAM;QACV;YACI,MAAM,IAAI,KAAK,CAAC,qCAAqC,CAAC,CAAC;IAC/D,CAAC;IAED,IAAI,CAAC;QACD,OAAO,gBAAgB,CAAC,MAAM,CAAC,CAAC;IACpC,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACb,OAAO,IAAI,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;AACL,CAAC;AACD,SAAS,gBAAgB,CAAC,KAAa;IACnC,OAAO,kBAAkB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,MAAM,EAAE,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE;QAC3D,IAAI,IAAI,GAAG,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,WAAW,EAAE,CAAC;QAEtD,IAAI,IAAI,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAClB,IAAI,GAAG,GAAG,GAAG,IAAI,CAAC;QACtB,CAAC;QAED,OAAO,GAAG,GAAG,IAAI,CAAC;IACtB,CAAC,CAAC,CAAC,CAAC;AACR,CAAC"}
@@ -0,0 +1,8 @@
1
+ import { Policy } from "../models/Policy";
2
+ import { BaseContract, Doken } from "./BaseContract";
3
+ export declare class GenericRealmAccessThresholdRoleContract extends BaseContract {
4
+ protected validateData(policy: Policy): Promise<void>;
5
+ id: string;
6
+ protected validateApprovers(policy: Policy, approverDokens: Doken[]): Promise<void>;
7
+ }
8
+ //# sourceMappingURL=GenericRealmAccessThresholdRoleContract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenericRealmAccessThresholdRoleContract.d.ts","sourceRoot":"","sources":["../../../src/contracts/GenericRealmAccessThresholdRoleContract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAErD,qBAAa,uCAAwC,SAAQ,YAAY;IACrE,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAI9C,EAAE,EAAE,MAAM,CAAuC;cACxC,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ5F"}
@@ -0,0 +1,22 @@
1
+ import { BaseContract } from "./BaseContract";
2
+ export class GenericRealmAccessThresholdRoleContract extends BaseContract {
3
+ constructor() {
4
+ super(...arguments);
5
+ this.id = "GenericRealmAccessThresholdRole:1";
6
+ }
7
+ validateData(policy) {
8
+ console.warn("Validate Data not implemented!");
9
+ return;
10
+ }
11
+ async validateApprovers(policy, approverDokens) {
12
+ let successfulDokens = 0;
13
+ approverDokens.forEach(d => {
14
+ if (d.hasRealmAccessRole(policy.params.getParameter("role")))
15
+ successfulDokens++;
16
+ });
17
+ const threshold = policy.params.getParameter("threshold");
18
+ if (successfulDokens < threshold)
19
+ throw 'Not enough successful dokens with requires roles/clients';
20
+ }
21
+ }
22
+ //# sourceMappingURL=GenericRealmAccessThresholdRoleContract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenericRealmAccessThresholdRoleContract.js","sourceRoot":"","sources":["../../../src/contracts/GenericRealmAccessThresholdRoleContract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAErD,MAAM,OAAO,uCAAwC,SAAQ,YAAY;IAAzE;;QAKW,OAAE,GAAW,mCAAmC,CAAC;IAS5D,CAAC;IAba,YAAY,CAAC,MAAc;QACjC,OAAO,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC;QAC/C,OAAO;IACX,CAAC;IAES,KAAK,CAAC,iBAAiB,CAAC,MAAc,EAAE,cAAuB;QACrE,IAAI,gBAAgB,GAAG,CAAC,CAAC;QACzB,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACvB,IAAG,CAAC,CAAC,kBAAkB,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAS,MAAM,CAAC,CAAC;gBAAE,gBAAgB,EAAE,CAAC;QAC5F,CAAC,CAAC,CAAA;QACF,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAS,WAAW,CAAC,CAAC;QAClE,IAAG,gBAAgB,GAAG,SAAS;YAAE,MAAM,0DAA0D,CAAC;IACtG,CAAC;CACJ"}
@@ -0,0 +1,8 @@
1
+ import { Policy } from "../models/Policy";
2
+ import { BaseContract, Doken } from "./BaseContract";
3
+ export declare class GenericResourceAccessThresholdRoleContract extends BaseContract {
4
+ id: string;
5
+ protected validateData(policy: Policy): Promise<void>;
6
+ protected validateApprovers(policy: Policy, approverDokens: Doken[]): Promise<void>;
7
+ }
8
+ //# sourceMappingURL=GenericResourceAccessThresholdRoleContract.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenericResourceAccessThresholdRoleContract.d.ts","sourceRoot":"","sources":["../../../src/contracts/GenericResourceAccessThresholdRoleContract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAC;AAC1C,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,gBAAgB,CAAC;AAErD,qBAAa,0CAA2C,SAAQ,YAAY;IACjE,EAAE,EAAE,MAAM,CAA0C;IAC3D,SAAS,CAAC,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;cAIrC,iBAAiB,CAAC,MAAM,EAAE,MAAM,EAAE,cAAc,EAAE,KAAK,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC;CAQ5F"}
@@ -0,0 +1,22 @@
1
+ import { BaseContract } from "./BaseContract";
2
+ export class GenericResourceAccessThresholdRoleContract extends BaseContract {
3
+ constructor() {
4
+ super(...arguments);
5
+ this.id = "GenericResourceAccessThresholdRole:1";
6
+ }
7
+ validateData(policy) {
8
+ console.warn("Validate Data not implemented!");
9
+ return;
10
+ }
11
+ async validateApprovers(policy, approverDokens) {
12
+ let successfulDokens = 0;
13
+ approverDokens.forEach(d => {
14
+ if (d.hasResourceAccessRole(policy.params.getParameter("role"), policy.params.getParameter("resource")))
15
+ successfulDokens++;
16
+ });
17
+ const threshold = policy.params.getParameter("threshold");
18
+ if (successfulDokens < threshold)
19
+ throw 'Not enough successful dokens with requires roles/clients';
20
+ }
21
+ }
22
+ //# sourceMappingURL=GenericResourceAccessThresholdRoleContract.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"GenericResourceAccessThresholdRoleContract.js","sourceRoot":"","sources":["../../../src/contracts/GenericResourceAccessThresholdRoleContract.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAS,MAAM,gBAAgB,CAAC;AAErD,MAAM,OAAO,0CAA2C,SAAQ,YAAY;IAA5E;;QACW,OAAE,GAAW,sCAAsC,CAAC;IAa/D,CAAC;IAZa,YAAY,CAAC,MAAc;QACjC,OAAO,CAAC,IAAI,CAAC,gCAAgC,CAAC,CAAC;QAC/C,OAAO;IACX,CAAC;IACS,KAAK,CAAC,iBAAiB,CAAC,MAAc,EAAE,cAAuB;QACrE,IAAI,gBAAgB,GAAG,CAAC,CAAC;QACzB,cAAc,CAAC,OAAO,CAAC,CAAC,CAAC,EAAE;YACvB,IAAG,CAAC,CAAC,qBAAqB,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAS,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,CAAC,YAAY,CAAS,UAAU,CAAC,CAAC;gBAAE,gBAAgB,EAAE,CAAC;QAC/I,CAAC,CAAC,CAAA;QACF,MAAM,SAAS,GAAG,MAAM,CAAC,MAAM,CAAC,YAAY,CAAS,WAAW,CAAC,CAAC;QAClE,IAAG,gBAAgB,GAAG,SAAS;YAAE,MAAM,0DAA0D,CAAC;IACtG,CAAC;CACJ"}
@@ -0,0 +1,15 @@
1
+ import { BaseContract } from "./contracts/BaseContract";
2
+ import { GenericResourceAccessThresholdRoleContract } from "./contracts/GenericResourceAccessThresholdRoleContract";
3
+ import { GenericRealmAccessThresholdRoleContract } from "./contracts/GenericRealmAccessThresholdRoleContract";
4
+ import { TideMemory } from "./utils/TideMemory";
5
+ import BaseTideRequest from "./models/TideRequest";
6
+ import { Policy, PolicyParameters, ApprovalType, ExecutionType } from "./models/Policy";
7
+ import { BasicCustomRequest, DynamicPayloadCustomRequest, DynamicPayloadApprovedCustomRequest } from "./models/CustomTideRequest";
8
+ export { GenericResourceAccessThresholdRoleContract };
9
+ export { BaseContract };
10
+ export { TideMemory };
11
+ export { BaseTideRequest };
12
+ export { Policy, PolicyParameters, ApprovalType, ExecutionType };
13
+ export { GenericRealmAccessThresholdRoleContract };
14
+ export { BasicCustomRequest, DynamicPayloadCustomRequest, DynamicPayloadApprovedCustomRequest };
15
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,0CAA0C,EAAE,MAAM,wDAAwD,CAAC;AACpH,OAAO,EAAE,uCAAuC,EAAE,MAAM,qDAAqD,CAAC;AAC9G,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,eAAe,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAExF,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AAElI,OAAO,EAAE,0CAA0C,EAAE,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,CAAA;AACrB,OAAO,EAAE,eAAe,EAAE,CAAA;AAC1B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,CAAA;AAChE,OAAO,EAAE,uCAAuC,EAAE,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,mCAAmC,EAAE,CAAA"}
@@ -0,0 +1,15 @@
1
+ import { BaseContract } from "./contracts/BaseContract";
2
+ import { GenericResourceAccessThresholdRoleContract } from "./contracts/GenericResourceAccessThresholdRoleContract";
3
+ import { GenericRealmAccessThresholdRoleContract } from "./contracts/GenericRealmAccessThresholdRoleContract";
4
+ import { TideMemory } from "./utils/TideMemory";
5
+ import BaseTideRequest from "./models/TideRequest";
6
+ import { Policy, PolicyParameters, ApprovalType, ExecutionType } from "./models/Policy";
7
+ import { BasicCustomRequest, DynamicPayloadCustomRequest, DynamicPayloadApprovedCustomRequest } from "./models/CustomTideRequest";
8
+ export { GenericResourceAccessThresholdRoleContract };
9
+ export { BaseContract };
10
+ export { TideMemory };
11
+ export { BaseTideRequest };
12
+ export { Policy, PolicyParameters, ApprovalType, ExecutionType };
13
+ export { GenericRealmAccessThresholdRoleContract };
14
+ export { BasicCustomRequest, DynamicPayloadCustomRequest, DynamicPayloadApprovedCustomRequest };
15
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAC;AACxD,OAAO,EAAE,0CAA0C,EAAE,MAAM,wDAAwD,CAAC;AACpH,OAAO,EAAE,uCAAuC,EAAE,MAAM,qDAAqD,CAAC;AAC9G,OAAO,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAChD,OAAO,eAAe,MAAM,sBAAsB,CAAC;AACnD,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAC;AAExF,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,mCAAmC,EAAE,MAAM,4BAA4B,CAAC;AAElI,OAAO,EAAE,0CAA0C,EAAE,CAAA;AACrD,OAAO,EAAE,YAAY,EAAE,CAAC;AACxB,OAAO,EAAE,UAAU,EAAE,CAAA;AACrB,OAAO,EAAE,eAAe,EAAE,CAAA;AAC1B,OAAO,EAAE,MAAM,EAAE,gBAAgB,EAAE,YAAY,EAAE,aAAa,EAAE,CAAA;AAChE,OAAO,EAAE,uCAAuC,EAAE,CAAA;AAClD,OAAO,EAAE,kBAAkB,EAAE,2BAA2B,EAAE,mCAAmC,EAAE,CAAA"}
@@ -0,0 +1,19 @@
1
+ import BaseTideRequest from "./TideRequest";
2
+ export declare class BasicCustomRequest extends BaseTideRequest {
3
+ id(): string;
4
+ }
5
+ export declare class DynamicPayloadCustomRequest extends BaseTideRequest {
6
+ id(): string;
7
+ }
8
+ export declare class DynamicPayloadApprovedCustomRequest extends BaseTideRequest {
9
+ customInfo: CustomInfo | undefined;
10
+ constructor(name: string, version: string, authFlow: string, humanReadableName: string, additionalInfo: any, dyanmicData: Uint8Array);
11
+ id(): string;
12
+ getAdditionalInfoSupplied(): any;
13
+ }
14
+ interface CustomInfo {
15
+ humanReadableName: string;
16
+ additionalInfo: any;
17
+ }
18
+ export {};
19
+ //# sourceMappingURL=CustomTideRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CustomTideRequest.d.ts","sourceRoot":"","sources":["../../../src/models/CustomTideRequest.ts"],"names":[],"mappings":"AAEA,OAAO,eAAe,MAAM,eAAe,CAAC;AAE5C,qBAAa,kBAAmB,SAAQ,eAAe;IACnD,EAAE,IAAI,MAAM;CAGf;AAED,qBAAa,2BAA4B,SAAQ,eAAe;IAC5D,EAAE,IAAI,MAAM;CAGf;AAED,qBAAa,mCAAoC,SAAQ,eAAe;IACpE,UAAU,EAAE,UAAU,GAAG,SAAS,CAAC;gBAEvB,IAAI,EAAE,MAAM,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,iBAAiB,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,WAAW,EAAE,UAAU;IAQpI,EAAE,IAAI,MAAM;IAIZ,yBAAyB,IAAI,GAAG;CAInC;AACD,UAAU,UAAU;IAChB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,cAAc,EAAE,GAAG,CAAC;CACvB"}
@@ -0,0 +1,31 @@
1
+ import { StringFromUint8Array, StringToUint8Array } from "../utils/Serialization";
2
+ import BaseTideRequest from "./TideRequest";
3
+ export class BasicCustomRequest extends BaseTideRequest {
4
+ id() {
5
+ return `BasicCustom<${this.name}>:BasicCustom<${this.version}>`;
6
+ }
7
+ }
8
+ export class DynamicPayloadCustomRequest extends BaseTideRequest {
9
+ id() {
10
+ return `DynamicCustom<${this.name}>:DynamicCustom<${this.version}>`;
11
+ }
12
+ }
13
+ export class DynamicPayloadApprovedCustomRequest extends BaseTideRequest {
14
+ constructor(name, version, authFlow, humanReadableName, additionalInfo, dyanmicData) {
15
+ const customInfo = {
16
+ humanReadableName: humanReadableName,
17
+ additionalInfo: additionalInfo
18
+ };
19
+ super(name, version, authFlow, StringToUint8Array(JSON.stringify(customInfo)), dyanmicData);
20
+ }
21
+ id() {
22
+ return `DynamicApprovedCustom<${this.name}>:DynamicApprovedCustom<${this.version}>`;
23
+ }
24
+ getAdditionalInfoSupplied() {
25
+ if (this.draft.length > 0)
26
+ return JSON.parse(StringFromUint8Array(this.draft))["additionalInfo"];
27
+ else
28
+ return null;
29
+ }
30
+ }
31
+ //# sourceMappingURL=CustomTideRequest.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CustomTideRequest.js","sourceRoot":"","sources":["../../../src/models/CustomTideRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,kBAAkB,EAAE,MAAM,wBAAwB,CAAC;AAElF,OAAO,eAAe,MAAM,eAAe,CAAC;AAE5C,MAAM,OAAO,kBAAmB,SAAQ,eAAe;IACnD,EAAE;QACE,OAAO,eAAe,IAAI,CAAC,IAAI,iBAAiB,IAAI,CAAC,OAAO,GAAG,CAAC;IACpE,CAAC;CACJ;AAED,MAAM,OAAO,2BAA4B,SAAQ,eAAe;IAC5D,EAAE;QACE,OAAO,iBAAiB,IAAI,CAAC,IAAI,mBAAmB,IAAI,CAAC,OAAO,GAAG,CAAC;IACxE,CAAC;CACJ;AAED,MAAM,OAAO,mCAAoC,SAAQ,eAAe;IAGpE,YAAY,IAAY,EAAE,OAAe,EAAE,QAAgB,EAAE,iBAAyB,EAAE,cAAmB,EAAE,WAAuB;QAChI,MAAM,UAAU,GAAG;YACf,iBAAiB,EAAE,iBAAiB;YACpC,cAAc,EAAE,cAAc;SACjC,CAAA;QACD,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,QAAQ,EAAE,kBAAkB,CAAC,IAAI,CAAC,SAAS,CAAC,UAAU,CAAC,CAAC,EAAE,WAAW,CAAC,CAAC;IAChG,CAAC;IAED,EAAE;QACE,OAAO,yBAAyB,IAAI,CAAC,IAAI,2BAA2B,IAAI,CAAC,OAAO,GAAG,CAAC;IACxF,CAAC;IAED,yBAAyB;QACrB,IAAI,IAAI,CAAC,KAAK,CAAC,MAAM,GAAG,CAAC;YAAE,OAAO,IAAI,CAAC,KAAK,CAAC,oBAAoB,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,gBAAgB,CAAC,CAAC;;YAC5F,OAAO,IAAI,CAAC;IACrB,CAAC;CACJ"}
@@ -0,0 +1,41 @@
1
+ import { TideMemory } from "../utils/TideMemory";
2
+ export declare enum ApprovalType {
3
+ EXPLICIT = 0,
4
+ IMPLICIT = 1
5
+ }
6
+ export declare enum ExecutionType {
7
+ PRIVATE = 0,
8
+ PUBLIC = 1
9
+ }
10
+ export declare class Policy {
11
+ static latestVersion: string;
12
+ version: string;
13
+ contractId: string;
14
+ modelId: string;
15
+ keyId: string;
16
+ approvalType: ApprovalType;
17
+ executionType: ExecutionType;
18
+ params: PolicyParameters;
19
+ dataToVerify: TideMemory | undefined;
20
+ signature: Uint8Array | undefined;
21
+ constructor(data: {
22
+ version: string;
23
+ contractId: string;
24
+ modelId: string;
25
+ keyId: string;
26
+ approvalType: ApprovalType;
27
+ executionType: ExecutionType;
28
+ params: Map<string, any> | PolicyParameters;
29
+ });
30
+ static from(data: Uint8Array): Policy;
31
+ toBytes(): TideMemory;
32
+ }
33
+ export declare class PolicyParameters {
34
+ entries: Map<string, any>;
35
+ constructor(data: Map<string, any> | Uint8Array);
36
+ private static fromBytes;
37
+ tryGetParameter<T extends string | number | bigint | boolean | Uint8Array>(key: string): [boolean, T];
38
+ getParameter<T extends string | number | bigint | boolean | Uint8Array>(key: string): T;
39
+ toBytes(): Uint8Array;
40
+ }
41
+ //# sourceMappingURL=Policy.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Policy.d.ts","sourceRoot":"","sources":["../../../src/models/Policy.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,qBAAqB,CAAC;AAGjD,oBAAY,YAAY;IACpB,QAAQ,IAAA;IACR,QAAQ,IAAA;CACX;AACD,oBAAY,aAAa;IACrB,OAAO,IAAA;IACP,MAAM,IAAA;CACT;AACD,qBAAa,MAAM;IACf,MAAM,CAAC,aAAa,EAAE,MAAM,CAAO;IACnC,OAAO,EAAE,MAAM,CAAC;IAChB,UAAU,EAAE,MAAM,CAAC;IACnB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,YAAY,EAAE,YAAY,CAAC;IAC3B,aAAa,EAAE,aAAa,CAAC;IAC7B,MAAM,EAAE,gBAAgB,CAAC;IAEzB,YAAY,EAAE,UAAU,GAAG,SAAS,CAAC;IACrC,SAAS,EAAE,UAAU,GAAG,SAAS,CAAC;gBAEtB,IAAI,EAAE;QAAE,OAAO,EAAE,MAAM,CAAC;QAAC,UAAU,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAC;QAAC,KAAK,EAAE,MAAM,CAAC;QAAC,YAAY,EAAE,YAAY,CAAC;QAAC,aAAa,EAAE,aAAa,CAAC;QAAC,MAAM,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,gBAAgB,CAAA;KAAE;IA0BhM,MAAM,CAAC,IAAI,CAAC,IAAI,EAAE,UAAU,GAAG,MAAM;IAyCrC,OAAO;CAgBV;AAED,qBAAa,gBAAgB;IACzB,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;gBACd,IAAI,EAAE,GAAG,CAAC,MAAM,EAAE,GAAG,CAAC,GAAG,UAAU;IAQ/C,OAAO,CAAC,MAAM,CAAC,SAAS;IAgDxB,eAAe,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC,OAAO,EAAE,CAAC,CAAC;IAQrG,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,UAAU,EAAE,GAAG,EAAE,MAAM,GAAG,CAAC;IAkCvF,OAAO,IAAI,UAAU;CAsCxB"}
@@ -0,0 +1,263 @@
1
+ import { TideMemory } from "../utils/TideMemory";
2
+ import { BigIntToByteArray, BigIntFromByteArray, StringFromUint8Array, StringToUint8Array } from "../utils/Serialization";
3
+ export var ApprovalType;
4
+ (function (ApprovalType) {
5
+ ApprovalType[ApprovalType["EXPLICIT"] = 0] = "EXPLICIT";
6
+ ApprovalType[ApprovalType["IMPLICIT"] = 1] = "IMPLICIT";
7
+ })(ApprovalType || (ApprovalType = {}));
8
+ export var ExecutionType;
9
+ (function (ExecutionType) {
10
+ ExecutionType[ExecutionType["PRIVATE"] = 0] = "PRIVATE";
11
+ ExecutionType[ExecutionType["PUBLIC"] = 1] = "PUBLIC";
12
+ })(ExecutionType || (ExecutionType = {}));
13
+ export class Policy {
14
+ constructor(data) {
15
+ if (typeof data["version"] !== "string")
16
+ throw 'Version is not a string';
17
+ this.version = data["version"];
18
+ if (typeof data["contractId"] !== "string")
19
+ throw 'ContractId is not a string';
20
+ this.contractId = data["contractId"];
21
+ if (typeof data["modelId"] !== "string")
22
+ throw 'ModelId is not a string';
23
+ this.modelId = data["modelId"];
24
+ if (typeof data["keyId"] !== "string")
25
+ throw 'KeyId is not a string';
26
+ this.keyId = data["keyId"];
27
+ this.approvalType = data.approvalType;
28
+ this.executionType = data.executionType;
29
+ if (!data["params"])
30
+ throw 'Params is null';
31
+ this.params = data["params"] instanceof PolicyParameters ? data["params"] : new PolicyParameters(data["params"]);
32
+ this.dataToVerify = TideMemory.CreateFromArray([
33
+ StringToUint8Array(this.version),
34
+ StringToUint8Array(this.contractId),
35
+ StringToUint8Array(this.modelId),
36
+ StringToUint8Array(this.keyId),
37
+ StringToUint8Array(ApprovalType[this.approvalType]),
38
+ StringToUint8Array(ExecutionType[this.executionType]),
39
+ this.params.toBytes()
40
+ ]);
41
+ }
42
+ static from(data) {
43
+ const d = new TideMemory(data.length);
44
+ d.set(data);
45
+ const dataToVerify = d.GetValue(0);
46
+ const version = StringFromUint8Array(dataToVerify.GetValue(0));
47
+ if (version != Policy.latestVersion) {
48
+ // old version
49
+ switch (version) {
50
+ case PolicyV1.thisVersion:
51
+ return PolicyV1.from(d);
52
+ default:
53
+ throw Error("Unknown policy version: " + version);
54
+ }
55
+ }
56
+ const contractId = StringFromUint8Array(dataToVerify.GetValue(1));
57
+ const modelId = StringFromUint8Array(dataToVerify.GetValue(2));
58
+ const keyId = StringFromUint8Array(dataToVerify.GetValue(3));
59
+ const approvalType = ApprovalType[StringFromUint8Array(dataToVerify.GetValue(4))];
60
+ const executionType = ExecutionType[StringFromUint8Array(dataToVerify.GetValue(5))];
61
+ const params = new PolicyParameters(dataToVerify.GetValue(6));
62
+ const p = new Policy({
63
+ version,
64
+ contractId,
65
+ modelId,
66
+ keyId,
67
+ approvalType,
68
+ executionType,
69
+ params
70
+ });
71
+ const sigRes = { result: undefined };
72
+ if (d.TryGetValue(1, sigRes)) {
73
+ p.signature = sigRes.result;
74
+ }
75
+ return p;
76
+ }
77
+ toBytes() {
78
+ let d = [
79
+ TideMemory.CreateFromArray([
80
+ StringToUint8Array(this.version),
81
+ StringToUint8Array(this.contractId),
82
+ StringToUint8Array(this.modelId),
83
+ StringToUint8Array(this.keyId),
84
+ StringToUint8Array(ApprovalType[this.approvalType]),
85
+ StringToUint8Array(ExecutionType[this.executionType]),
86
+ this.params.toBytes()
87
+ ])
88
+ ];
89
+ if (this.signature)
90
+ d.push(this.signature);
91
+ return TideMemory.CreateFromArray(d);
92
+ }
93
+ }
94
+ Policy.latestVersion = "2";
95
+ export class PolicyParameters {
96
+ constructor(data) {
97
+ if (data instanceof Uint8Array) {
98
+ this.entries = PolicyParameters.fromBytes(data);
99
+ }
100
+ else {
101
+ this.entries = new Map(data);
102
+ }
103
+ }
104
+ static fromBytes(data) {
105
+ let params = new Map();
106
+ let i = 0;
107
+ const value = { result: undefined };
108
+ // Create TideMemory instance to access TryGetValue
109
+ const tideData = new TideMemory(data.length);
110
+ tideData.set(data);
111
+ // Try to get values at sequential indices
112
+ while (tideData.TryGetValue(i, value)) {
113
+ const nameBytes = value.result.GetValue(0);
114
+ const name = StringFromUint8Array(nameBytes);
115
+ const typeBytes = value.result.GetValue(1);
116
+ const type = StringFromUint8Array(typeBytes);
117
+ const dataBytes = value.result.GetValue(2);
118
+ let datum;
119
+ switch (type) {
120
+ case "str":
121
+ datum = StringFromUint8Array(dataBytes);
122
+ break;
123
+ case "num":
124
+ const numView = new DataView(dataBytes.buffer, dataBytes.byteOffset, dataBytes.byteLength);
125
+ datum = numView.getInt32(0, true); // little-endian
126
+ break;
127
+ case "bnum":
128
+ // Convert bytes to BigInt (little-endian)
129
+ datum = BigIntFromByteArray(dataBytes);
130
+ break;
131
+ case "bln":
132
+ datum = dataBytes[0] === 1;
133
+ break;
134
+ case "byt":
135
+ datum = new Uint8Array(dataBytes);
136
+ break;
137
+ default:
138
+ throw new Error(`Could not find type of ${type}`);
139
+ }
140
+ params.set(name, datum);
141
+ i++;
142
+ }
143
+ return params;
144
+ }
145
+ tryGetParameter(key) {
146
+ try {
147
+ return [true, this.getParameter(key)];
148
+ }
149
+ catch {
150
+ return [false, null];
151
+ }
152
+ }
153
+ getParameter(key) {
154
+ if (!this.entries.has(key)) {
155
+ throw new Error(`Parameter '${key}' not found`);
156
+ }
157
+ const value = this.entries.get(key);
158
+ const actualType = value instanceof Uint8Array ? 'Uint8Array' : typeof value;
159
+ // Type checking logic
160
+ let expectedType;
161
+ if (value instanceof Uint8Array) {
162
+ expectedType = 'Uint8Array';
163
+ }
164
+ else {
165
+ expectedType = typeof value;
166
+ }
167
+ // Validate the type matches what was requested
168
+ // We can't directly check T at runtime, so we infer from the value type
169
+ const isCorrectType = (typeof value === 'string' && value.constructor === String) ||
170
+ (typeof value === 'number' && value.constructor === Number) ||
171
+ (typeof value === 'bigint' && value.constructor === BigInt) ||
172
+ (typeof value === 'boolean' && value.constructor === Boolean) ||
173
+ (value instanceof Uint8Array);
174
+ if (!isCorrectType) {
175
+ throw new Error(`Parameter '${key}' exists but has unexpected type '${actualType}'`);
176
+ }
177
+ return value;
178
+ }
179
+ toBytes() {
180
+ let params = [];
181
+ for (const [key, value] of this.entries) {
182
+ const nameBytes = StringToUint8Array(key);
183
+ let dataBytes, typeStr;
184
+ if (typeof value === 'string') {
185
+ dataBytes = StringToUint8Array(value);
186
+ typeStr = "str";
187
+ }
188
+ else if (typeof value === 'number' && Number.isInteger(value)) {
189
+ const buffer = new ArrayBuffer(4);
190
+ const view = new DataView(buffer);
191
+ view.setInt32(0, value, true); // little-endian
192
+ dataBytes = new Uint8Array(buffer);
193
+ typeStr = "num";
194
+ }
195
+ else if (typeof value === 'bigint') {
196
+ dataBytes = BigIntToByteArray(value);
197
+ typeStr = "bnum";
198
+ }
199
+ else if (typeof value === 'boolean') {
200
+ dataBytes = new Uint8Array([value ? 1 : 0]);
201
+ typeStr = "bln";
202
+ }
203
+ else if (value instanceof Uint8Array) {
204
+ dataBytes = value;
205
+ typeStr = "byt";
206
+ }
207
+ else {
208
+ throw new Error(`Could not serialize key '${key}' of type '${typeof value}'`);
209
+ }
210
+ const typeBytes = StringToUint8Array(typeStr);
211
+ const paramMemory = TideMemory.CreateFromArray([nameBytes, typeBytes, dataBytes]);
212
+ params.push(paramMemory);
213
+ }
214
+ return TideMemory.CreateFromArray(params);
215
+ }
216
+ }
217
+ class PolicyV1 extends Policy {
218
+ constructor() {
219
+ super(...arguments);
220
+ this.version = PolicyV1.thisVersion;
221
+ }
222
+ static from(data) {
223
+ const dataToVerify = data.GetValue(0);
224
+ const v = StringFromUint8Array(dataToVerify.GetValue(0));
225
+ if (v != PolicyV1.thisVersion) {
226
+ throw Error("Dev error");
227
+ }
228
+ const contractId = StringFromUint8Array(dataToVerify.GetValue(1));
229
+ const modelId = StringFromUint8Array(dataToVerify.GetValue(2));
230
+ const keyId = StringFromUint8Array(dataToVerify.GetValue(3));
231
+ const params = new PolicyParameters(dataToVerify.GetValue(4));
232
+ const p = new PolicyV1({
233
+ version: v,
234
+ contractId,
235
+ modelId,
236
+ keyId,
237
+ approvalType: ApprovalType.EXPLICIT, // didn't exist on v1 so this is default
238
+ executionType: ExecutionType.PUBLIC, // didn't exist on v1 so this is default
239
+ params
240
+ });
241
+ const sigRes = { result: undefined };
242
+ if (data.TryGetValue(1, sigRes)) {
243
+ p.signature = sigRes.result;
244
+ }
245
+ return p;
246
+ }
247
+ toBytes() {
248
+ let d = [
249
+ TideMemory.CreateFromArray([
250
+ StringToUint8Array(PolicyV1.thisVersion),
251
+ StringToUint8Array(this.contractId),
252
+ StringToUint8Array(this.modelId),
253
+ StringToUint8Array(this.keyId),
254
+ this.params.toBytes()
255
+ ])
256
+ ];
257
+ if (this.signature)
258
+ d.push(this.signature);
259
+ return TideMemory.CreateFromArray(d);
260
+ }
261
+ }
262
+ PolicyV1.thisVersion = "1";
263
+ //# sourceMappingURL=Policy.js.map