@tari-project/tarijs 0.4.1 → 0.4.2

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 (61) hide show
  1. package/.github/workflows/ci.yml +2 -1
  2. package/.github/workflows/documentation-deploy.yml +44 -0
  3. package/.github/workflows/documentation-test-deploy.yml +20 -0
  4. package/.moon/workspace.yml +1 -0
  5. package/README.md +19 -0
  6. package/docusaurus/tari-docs/README.md +41 -0
  7. package/docusaurus/tari-docs/docs/index.md +16 -0
  8. package/docusaurus/tari-docs/docs/installation.md +27 -0
  9. package/docusaurus/tari-docs/docs/providers/_category_.json +8 -0
  10. package/docusaurus/tari-docs/docs/providers/tari-universe.md +27 -0
  11. package/docusaurus/tari-docs/docs/providers/wallet-connect.md +52 -0
  12. package/docusaurus/tari-docs/docs/providers/wallet-daemon.md +37 -0
  13. package/docusaurus/tari-docs/docs/wallet/_category_.json +8 -0
  14. package/docusaurus/tari-docs/docs/wallet/default-account.md +51 -0
  15. package/docusaurus/tari-docs/docs/wallet/get-substate.md +110 -0
  16. package/docusaurus/tari-docs/docs/wallet/list-substates.md +69 -0
  17. package/docusaurus/tari-docs/docs/wallet/submit-transaction/_category_.json +8 -0
  18. package/docusaurus/tari-docs/docs/wallet/submit-transaction/build-execute-request.md +38 -0
  19. package/docusaurus/tari-docs/docs/wallet/submit-transaction/index.md +14 -0
  20. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/_category_.json +8 -0
  21. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/call-function.md +17 -0
  22. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/call-method.md +17 -0
  23. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/fee.md +28 -0
  24. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/index.md +426 -0
  25. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/inputs.md +24 -0
  26. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/instruction.md +108 -0
  27. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/min-max-epoch.md +19 -0
  28. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/raw-instructions.md +16 -0
  29. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/save-var.md +63 -0
  30. package/docusaurus/tari-docs/docs/wallet/submit-transaction/transaction-builder/transaction-internals.md +20 -0
  31. package/docusaurus/tari-docs/docs/wallet/template-definition.md +101 -0
  32. package/docusaurus/tari-docs/docusaurus.config.ts +108 -0
  33. package/docusaurus/tari-docs/moon.yml +41 -0
  34. package/docusaurus/tari-docs/package.json +49 -0
  35. package/docusaurus/tari-docs/sidebars.ts +7 -0
  36. package/docusaurus/tari-docs/src/components/HomepageFeatures/index.tsx +69 -0
  37. package/docusaurus/tari-docs/src/components/HomepageFeatures/styles.module.css +11 -0
  38. package/docusaurus/tari-docs/src/css/custom.css +30 -0
  39. package/docusaurus/tari-docs/src/pages/index.module.css +23 -0
  40. package/docusaurus/tari-docs/src/pages/index.tsx +44 -0
  41. package/docusaurus/tari-docs/static/.nojekyll +0 -0
  42. package/docusaurus/tari-docs/static/img/favicon.png +0 -0
  43. package/docusaurus/tari-docs/static/img/meta-image.png +0 -0
  44. package/docusaurus/tari-docs/static/img/tari/wallet-connect-1.png +0 -0
  45. package/docusaurus/tari-docs/static/img/tari/wallet-connect-2.png +0 -0
  46. package/docusaurus/tari-docs/static/img/tari/wallet-connect-3.png +0 -0
  47. package/docusaurus/tari-docs/static/img/tari-logo.svg +30 -0
  48. package/docusaurus/tari-docs/tsconfig.json +8 -0
  49. package/package.json +1 -1
  50. package/packages/tari_provider/package.json +1 -1
  51. package/packages/tari_provider/src/TariProvider.ts +1 -2
  52. package/packages/tari_provider/src/types.ts +2 -0
  53. package/packages/tari_universe/package.json +1 -1
  54. package/packages/tari_universe/src/provider.ts +2 -17
  55. package/packages/tarijs/moon.yml +8 -0
  56. package/packages/tarijs/package.json +1 -0
  57. package/packages/tarijs/src/cbor.spec.ts +259 -0
  58. package/packages/tarijs/src/cbor.ts +114 -0
  59. package/packages/tarijs/src/index.ts +3 -0
  60. package/pnpm-workspace.yaml +1 -0
  61. package/tsconfig.json +3 -0
@@ -8,6 +8,7 @@
8
8
  },
9
9
  "scripts": {
10
10
  "build": "tsc -b",
11
+ "test": "vitest run src",
11
12
  "integration-tests": "vitest run integration-tests"
12
13
  },
13
14
  "keywords": [],
@@ -0,0 +1,259 @@
1
+ import { assert, describe, expect, it, vi } from "vitest";
2
+ import { BinaryTag, CborValue, convertTaggedValue, getCborValueByPath, parseCbor } from "./cbor";
3
+
4
+ describe("convertTaggedValue", () => {
5
+ it.each([
6
+ {
7
+ tag: BinaryTag.VaultId,
8
+ value: { Bytes: [1, 2, 3, 4, 255] },
9
+ expected: "vault_01020304ff",
10
+ },
11
+ {
12
+ tag: BinaryTag.ComponentAddress,
13
+ value: { Bytes: [1, 2, 3, 4, 255] },
14
+ expected: "component_01020304ff",
15
+ },
16
+ {
17
+ tag: BinaryTag.ResourceAddress,
18
+ value: { Bytes: [1, 2, 3, 4, 255] },
19
+ expected: "resource_01020304ff",
20
+ },
21
+ ])("converts tags with bytes into a string representation", ({ tag, value, expected }) => {
22
+ expect(convertTaggedValue(tag, value)).toEqual(expected);
23
+ });
24
+
25
+ it.each([
26
+ {
27
+ tag: BinaryTag.Metadata,
28
+ value: { Array: [{ Text: "One" }, { Text: "Two" }] },
29
+ expected: ["metadata", ["One", "Two"]],
30
+ },
31
+ {
32
+ tag: BinaryTag.Metadata,
33
+ value: {
34
+ Map: [
35
+ [{ Text: "key1" }, { Integer: 5 }],
36
+ [{ Text: "key2" }, { Integer: 3 }],
37
+ ],
38
+ },
39
+ expected: ["metadata", { key1: 5, key2: 3 }],
40
+ },
41
+ ])(
42
+ "converts other values into a tuple",
43
+ ({ tag, value, expected }: { tag: BinaryTag; value: unknown; expected: unknown }) => {
44
+ expect(convertTaggedValue(tag, value as CborValue)).toEqual(expected);
45
+ },
46
+ );
47
+
48
+ it("falls back to 'unknown' for unknown tags", () => {
49
+ expect(convertTaggedValue(55, { Bool: true })).toEqual(["unknown", true]);
50
+ });
51
+ });
52
+
53
+ describe("parseCbor", () => {
54
+ describe("Null", () => {
55
+ it("returns null", () => {
56
+ expect(parseCbor("Null")).toBeNull();
57
+ });
58
+ });
59
+
60
+ describe("Integer", () => {
61
+ it.each([
62
+ {
63
+ value: { Integer: 5 },
64
+ expected: 5,
65
+ },
66
+ {
67
+ value: { Integer: 3 },
68
+ expected: 3,
69
+ },
70
+ ])("returns an integer", ({ value, expected }) => {
71
+ expect(parseCbor(value)).toEqual(expected);
72
+ });
73
+ });
74
+
75
+ describe("Float", () => {
76
+ it.each([
77
+ {
78
+ value: { Float: 3.9 },
79
+ expected: 3.9,
80
+ },
81
+ {
82
+ value: { Float: 3 },
83
+ expected: 3,
84
+ },
85
+ ])("returns a number", ({ value, expected }) => {
86
+ expect(parseCbor(value)).toEqual(expected);
87
+ });
88
+ });
89
+
90
+ describe("Text", () => {
91
+ it.each([
92
+ {
93
+ value: { Text: "Some text" },
94
+ expected: "Some text",
95
+ },
96
+ {
97
+ value: { Text: "DEF" },
98
+ expected: "DEF",
99
+ },
100
+ ])("returns a string", ({ value, expected }) => {
101
+ expect(parseCbor(value)).toEqual(expected);
102
+ });
103
+ });
104
+
105
+ describe("Bytes", () => {
106
+ it.each([
107
+ {
108
+ value: { Bytes: [1, 2, 0xff] },
109
+ expected: Uint8Array.from([1, 2, 0xff]),
110
+ },
111
+ {
112
+ value: { Bytes: [] },
113
+ expected: new Uint8Array(),
114
+ },
115
+ ])("returns Uint8Array", ({ value, expected }) => {
116
+ expect(parseCbor(value)).toEqual(expected);
117
+ });
118
+ });
119
+
120
+ describe("Bool", () => {
121
+ it.each([
122
+ {
123
+ value: { Bool: true },
124
+ expected: true,
125
+ },
126
+ {
127
+ value: { Bool: false },
128
+ expected: false,
129
+ },
130
+ ])("returns a boolean", ({ value, expected }) => {
131
+ expect(parseCbor(value)).toEqual(expected);
132
+ });
133
+ });
134
+
135
+ describe("Tag", () => {
136
+ it.each([
137
+ {
138
+ value: {
139
+ Tag: [BinaryTag.VaultId, { Bytes: [1, 2, 3, 4, 255] }],
140
+ },
141
+ expected: "vault_01020304ff",
142
+ },
143
+ {
144
+ value: { Tag: [BinaryTag.Metadata, { Array: [{ Text: "One" }, { Text: "Two" }] }] },
145
+ expected: ["metadata", ["One", "Two"]],
146
+ },
147
+ ])("returns a tag representation", ({ value, expected }: { value: unknown; expected: unknown }) => {
148
+ expect(parseCbor(value as CborValue)).toEqual(expected);
149
+ });
150
+ });
151
+
152
+ describe("Array", () => {
153
+ it.each([
154
+ {
155
+ value: {
156
+ Array: [{ Integer: 5 }, { Float: 3.9 }, { Text: "Some text" }, { Bytes: [1, 2, 0xff] }, { Bool: true }],
157
+ },
158
+ expected: [5, 3.9, "Some text", Uint8Array.from([1, 2, 0xff]), true],
159
+ },
160
+ ])("returns an array", ({ value, expected }) => {
161
+ expect(parseCbor(value)).toEqual(expected);
162
+ });
163
+ });
164
+
165
+ describe("Map", () => {
166
+ it.each([
167
+ {
168
+ value: {
169
+ Map: [
170
+ [{ Text: "key1" }, { Integer: 5 }],
171
+ [{ Text: "key2" }, { Float: 3.9 }],
172
+ [{ Text: "key3" }, { Text: "Some text" }],
173
+ [{ Text: "key4" }, { Bytes: [1, 2, 0xff] }],
174
+ [{ Text: "key5" }, { Bool: true }],
175
+ [{ Text: "key6" }, { Array: [{ Integer: 1 }, { Integer: 2 }] }],
176
+ [
177
+ { Text: "key_nested" },
178
+ {
179
+ Map: [
180
+ [{ Text: "nested1" }, { Text: "Nested text" }],
181
+ [{ Text: "nested2" }, { Integer: 2 }],
182
+ ],
183
+ },
184
+ ],
185
+ ],
186
+ },
187
+ expected: {
188
+ key1: 5,
189
+ key2: 3.9,
190
+ key3: "Some text",
191
+ key4: Uint8Array.from([1, 2, 0xff]),
192
+ key5: true,
193
+ key6: [1, 2],
194
+ key_nested: {
195
+ nested1: "Nested text",
196
+ nested2: 2,
197
+ },
198
+ },
199
+ },
200
+ ])("returns an object", ({ value, expected }: { value: unknown; expected: unknown }) => {
201
+ expect(parseCbor(value as CborValue)).toEqual(expected);
202
+ });
203
+ });
204
+
205
+ it.each([{}, { Unknown: 2 }])("throws, when data in unknown format is found", (value: unknown) => {
206
+ expect(() => parseCbor(value as CborValue)).toThrow();
207
+ });
208
+ });
209
+
210
+ describe("getCborValueByPath", () => {
211
+ it.each([
212
+ {
213
+ path: "$.key1",
214
+ expected: 5,
215
+ },
216
+ {
217
+ path: "$.key3",
218
+ expected: "Some text",
219
+ },
220
+ {
221
+ path: "$.key6.1",
222
+ expected: 2,
223
+ },
224
+ {
225
+ path: "$.key_nested.nested1",
226
+ expected: "Nested text",
227
+ },
228
+ {
229
+ path: "$.missing.path",
230
+ expected: null,
231
+ },
232
+ {
233
+ path: "$.key6.999",
234
+ expected: null,
235
+ },
236
+ ])("gets value by path", ({ path, expected }) => {
237
+ const cbor: CborValue = {
238
+ Map: [
239
+ [{ Text: "key1" }, { Integer: 5 }],
240
+ [{ Text: "key2" }, { Float: 3.9 }],
241
+ [{ Text: "key3" }, { Text: "Some text" }],
242
+ [{ Text: "key4" }, { Bytes: [1, 2, 0xff] }],
243
+ [{ Text: "key5" }, { Bool: true }],
244
+ [{ Text: "key6" }, { Array: [{ Integer: 1 }, { Integer: 2 }] }],
245
+ [
246
+ { Text: "key_nested" },
247
+ {
248
+ Map: [
249
+ [{ Text: "nested1" }, { Text: "Nested text" }],
250
+ [{ Text: "nested2" }, { Integer: 2 }],
251
+ ],
252
+ },
253
+ ],
254
+ ],
255
+ };
256
+
257
+ expect(getCborValueByPath(cbor, path)).toEqual(expected);
258
+ });
259
+ });
@@ -0,0 +1,114 @@
1
+ export type CborValue =
2
+ | { Map: Array<[CborValue, CborValue]> }
3
+ | { Array: CborValue[] }
4
+ | { Tag: [BinaryTag, CborValue] }
5
+ | { Bool: boolean }
6
+ | { Bytes: number[] }
7
+ | { Text: string }
8
+ | { Float: number }
9
+ | { Integer: number }
10
+ | "Null";
11
+
12
+ export function parseCbor(value: CborValue): unknown {
13
+ if (typeof value === "string") {
14
+ if (value === "Null") {
15
+ return null;
16
+ }
17
+ throw new Error("Unknown CBOR value type");
18
+ }
19
+ if ("Map" in value) {
20
+ return Object.fromEntries(value.Map.map(([k, v]) => [parseCbor(k), parseCbor(v)]));
21
+ } else if ("Array" in value) {
22
+ return value.Array.map(parseCbor);
23
+ } else if ("Tag" in value) {
24
+ const [type, data] = value.Tag;
25
+ return convertTaggedValue(type, data);
26
+ } else if ("Bool" in value) {
27
+ return value.Bool;
28
+ } else if ("Bytes" in value) {
29
+ return new Uint8Array(value.Bytes);
30
+ } else if ("Text" in value) {
31
+ return value.Text;
32
+ } else if ("Float" in value) {
33
+ return value.Float;
34
+ } else if ("Integer" in value) {
35
+ return value.Integer;
36
+ }
37
+ throw new Error("Unknown CBOR value type");
38
+ }
39
+
40
+ export function getCborValueByPath(cborRepr: CborValue | null, path: string): unknown {
41
+ if (!cborRepr) {
42
+ return null;
43
+ }
44
+ let value = cborRepr;
45
+ for (const part of path.split(".")) {
46
+ if (part == "$") {
47
+ continue;
48
+ }
49
+ if (typeof value !== "object") {
50
+ return null;
51
+ }
52
+ if ("Map" in value) {
53
+ const mapEntry = value.Map.find((v) => parseCbor(v[0]) === part)?.[1];
54
+ if (mapEntry) {
55
+ value = mapEntry;
56
+ continue;
57
+ } else {
58
+ return null;
59
+ }
60
+ }
61
+
62
+ if ("Array" in value) {
63
+ const arr = value.Array;
64
+ const index = parseInt(part);
65
+ if (!Number.isNaN(index) && Array.isArray(arr) && arr.length > index) {
66
+ value = arr[index];
67
+ continue;
68
+ }
69
+ }
70
+
71
+ return null;
72
+ }
73
+ return parseCbor(value);
74
+ }
75
+
76
+ function uint8ArrayToHex(bytes: Uint8Array): string {
77
+ return Array.from(bytes)
78
+ .map((byte) => byte.toString(16).padStart(2, "0"))
79
+ .join("");
80
+ }
81
+
82
+ export enum BinaryTag {
83
+ ComponentAddress = 128,
84
+ Metadata = 129,
85
+ NonFungibleAddress = 130,
86
+ ResourceAddress = 131,
87
+ VaultId = 132,
88
+ BucketId = 133,
89
+ TransactionReceipt = 134,
90
+ ProofId = 135,
91
+ UnclaimedConfidentialOutputAddress = 136,
92
+ TemplateAddress = 137,
93
+ ValidatorNodeFeePool = 138,
94
+ }
95
+
96
+ const BINARY_TAG_KEYS = new Map<BinaryTag, string>([
97
+ [BinaryTag.ComponentAddress, "component"],
98
+ [BinaryTag.Metadata, "metadata"],
99
+ [BinaryTag.NonFungibleAddress, "nft"],
100
+ [BinaryTag.ResourceAddress, "resource"],
101
+ [BinaryTag.VaultId, "vault"],
102
+ [BinaryTag.BucketId, "bucket"],
103
+ [BinaryTag.TransactionReceipt, "transaction-receipt"],
104
+ [BinaryTag.ProofId, "proof"],
105
+ [BinaryTag.UnclaimedConfidentialOutputAddress, "unclaimed-confidential-output-address"],
106
+ [BinaryTag.TemplateAddress, "template-address"],
107
+ [BinaryTag.ValidatorNodeFeePool, "validator-node-fee-pool"],
108
+ ]);
109
+
110
+ export function convertTaggedValue(type: number, value: CborValue): string | unknown {
111
+ const tag = BINARY_TAG_KEYS.get(type) ?? "unknown";
112
+ const decoded = parseCbor(value);
113
+ return decoded instanceof Uint8Array ? `${tag}_${uint8ArrayToHex(decoded)}` : [tag, decoded];
114
+ }
@@ -33,6 +33,7 @@ import {
33
33
  toWorkspace,
34
34
  } from "@tari-project/tarijs-builders";
35
35
  import * as templates from "./templates";
36
+ import { parseCbor, getCborValueByPath } from "./cbor";
36
37
 
37
38
  export * from "@tari-project/tarijs-types";
38
39
  export {
@@ -66,4 +67,6 @@ export {
66
67
  fromWorkspace,
67
68
  toWorkspace,
68
69
  templates,
70
+ parseCbor,
71
+ getCborValueByPath,
69
72
  };
@@ -1,6 +1,7 @@
1
1
  packages:
2
2
  - packages/*
3
3
  - apps/*
4
+ - docusaurus/*
4
5
  - "!**/test/**"
5
6
  catalog:
6
7
  typescript: ^5.0.4
package/tsconfig.json CHANGED
@@ -8,6 +8,9 @@
8
8
  "composite": true
9
9
  },
10
10
  "references": [
11
+ {
12
+ "path": "docusaurus/tari-docs"
13
+ },
11
14
  {
12
15
  "path": "packages/builders"
13
16
  },