@tari-project/tarijs 0.11.0 → 0.12.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 (39) hide show
  1. package/docusaurus/tari-docs/package.json +4 -4
  2. package/eslint.config.mjs +9 -0
  3. package/examples/vite-typescript-react/src/App.tsx +1 -1
  4. package/package.json +15 -2
  5. package/packages/builders/package.json +1 -1
  6. package/packages/builders/src/helpers/submitTransaction.ts +10 -35
  7. package/packages/builders/src/transaction/TransactionBuilder.ts +223 -17
  8. package/packages/indexer_provider/package.json +1 -1
  9. package/packages/metamask_signer/package.json +1 -1
  10. package/packages/metamask_signer/src/index.ts +1 -1
  11. package/packages/permissions/package.json +1 -1
  12. package/packages/react-mui-connect-button/package.json +1 -1
  13. package/packages/tari_provider/package.json +1 -1
  14. package/packages/tari_provider/src/TariProvider.ts +6 -1
  15. package/packages/tari_signer/package.json +1 -1
  16. package/packages/tari_universe/package.json +1 -1
  17. package/packages/tarijs/package.json +1 -1
  18. package/packages/tarijs/src/index.ts +27 -49
  19. package/packages/tarijs/src/templates/Account.ts +7 -4
  20. package/packages/tarijs/test/integration-tests/.env +1 -1
  21. package/packages/tarijs/test/integration-tests/wallet_daemon/json_rpc_provider.spec.ts +112 -73
  22. package/packages/tarijs/tsconfig.json +2 -2
  23. package/packages/tarijs/vitest.config.ts +2 -1
  24. package/packages/tarijs_types/package.json +3 -2
  25. package/packages/tarijs_types/src/Account.ts +68 -0
  26. package/packages/tarijs_types/src/Amount.ts +5 -1
  27. package/packages/tarijs_types/src/TransactionResult.ts +1 -8
  28. package/packages/tarijs_types/src/consts.ts +3 -0
  29. package/packages/tarijs_types/src/helpers/index.ts +4 -0
  30. package/packages/tarijs_types/src/helpers/simpleResult.ts +345 -0
  31. package/packages/tarijs_types/src/helpers/txResult.ts +1 -2
  32. package/packages/tarijs_types/src/index.ts +8 -0
  33. package/packages/tarijs_types/src/signer.ts +1 -1
  34. package/packages/wallet_daemon/package.json +1 -1
  35. package/packages/wallet_daemon/src/signer.ts +7 -2
  36. package/packages/walletconnect/package.json +1 -1
  37. package/packages/walletconnect/src/index.ts +2 -2
  38. package/pnpm-workspace.yaml +1 -1
  39. package/typedoc.json +10 -0
@@ -0,0 +1,345 @@
1
+ import {
2
+ Vault,
3
+ SubstateId,
4
+ substateIdToString,
5
+ ComponentHeader,
6
+ NonFungible,
7
+ RejectReason,
8
+ Resource,
9
+ Substate,
10
+ SubstateDiff,
11
+ SubstateType,
12
+ TransactionReceipt,
13
+ UnclaimedConfidentialOutput,
14
+ ValidatorFeePool,
15
+ PublishedTemplate,
16
+ ValidatorFeeWithdrawal,
17
+ PublishedTemplateAddress,
18
+ TransactionId,
19
+ FinalizeResult,
20
+ LogEntry,
21
+ Event,
22
+ } from "@tari-project/typescript-bindings";
23
+ import { VaultSubstate, BuiltInAccount } from "../Account";
24
+ import { TransactionStatus } from "../TransactionStatus";
25
+ import { Option, Some, None } from "@thames/monads";
26
+ import { GetTransactionResultResponse } from "../GetTransactionResultResponse";
27
+ import { parseCbor } from "../";
28
+ import { ACCOUNT_TEMPLATE_ADDRESS } from "../consts";
29
+
30
+ export class SimpleTransactionResult {
31
+ private readonly transaction_id: TransactionId;
32
+ private readonly finalizeResult: FinalizeResult;
33
+ private readonly _status: TransactionStatus;
34
+
35
+ constructor(transaction_id: TransactionId, status: TransactionStatus, result: FinalizeResult) {
36
+ this.transaction_id = transaction_id;
37
+ this._status = status;
38
+ this.finalizeResult = result;
39
+ }
40
+
41
+ static new(transaction_id: TransactionId, status: TransactionStatus, result: FinalizeResult): SimpleTransactionResult {
42
+ return new SimpleTransactionResult(transaction_id, status, result);
43
+ }
44
+
45
+ static fromResponse(resp: GetTransactionResultResponse): SimpleTransactionResult {
46
+ if (!resp.result) {
47
+ throw new Error("Transaction result is missing in the response");
48
+ }
49
+ return SimpleTransactionResult.new(resp.transaction_id, resp.status, resp.result);
50
+ }
51
+
52
+ public get transactionId(): string {
53
+ return this.transaction_id;
54
+ }
55
+
56
+ public get result(): FinalizeResult {
57
+ return this.finalizeResult;
58
+ }
59
+
60
+ public get status(): TransactionStatus {
61
+ return this._status;
62
+ }
63
+
64
+ public get logs(): LogEntry[] {
65
+ return this.result.logs;
66
+ }
67
+
68
+ public get events(): Event[] {
69
+ return this.result.events;
70
+ }
71
+
72
+ public getResultingComponents(): ComponentHeader[] {
73
+ return this.getNewSubstatesOfType("Component") as ComponentHeader[];
74
+ }
75
+
76
+ public getResultingResources(): Resource[] {
77
+ return this.getNewSubstatesOfType("Resource") as Resource[];
78
+ }
79
+
80
+ public getResultingVaults(): VaultSubstate[] {
81
+ const vaults = this.getUpSubstatesOfType("Vault");
82
+ return vaults.map((upSubstate) => VaultSubstate.new(upSubstate.id, upSubstate.substate as Vault));
83
+ }
84
+
85
+ public getResultingNonFungibles(): NonFungible[] {
86
+ return this.getNewSubstatesOfType("NonFungible") as NonFungible[];
87
+ }
88
+
89
+ public getTransactionReceipt(): TransactionReceipt {
90
+ return this.getNewSubstatesOfType("TransactionReceipt")[0]! as TransactionReceipt;
91
+ }
92
+
93
+ public getNewPublishedTemplates(): PublishedTemplate[] {
94
+ return this.getNewSubstatesOfType("Template") as PublishedTemplate[];
95
+ }
96
+
97
+ public getNewSubstatesOfType(type: SubstateType): AnySubstate[] {
98
+ const upSubstates = this.getUpSubstatesOfType(type);
99
+ return upSubstates.map((upSubstate) => upSubstate.substate);
100
+ }
101
+
102
+ public getUpSubstatesOfType(type: SubstateType): UpSubstate[] {
103
+ const diff = this.diff;
104
+ if (diff.isNone()) {
105
+ return [];
106
+ }
107
+ const d = diff.unwrap();
108
+
109
+ const substates = [];
110
+ for (const upSubstate of d.upSubstates()) {
111
+ if (upSubstate.type === type) {
112
+ substates.push(upSubstate);
113
+ }
114
+ }
115
+
116
+ return substates;
117
+ }
118
+
119
+ public getComponentsByTemplateAddress(templateAddress: PublishedTemplateAddress): SimpleComponent[] {
120
+ const diff = this.diff;
121
+ if (diff.isNone()) {
122
+ return [];
123
+ }
124
+ const d = diff.unwrap();
125
+
126
+ const components = [];
127
+ for (const upSubstate of d.upSubstates()) {
128
+ if (upSubstate.type === "Component" && (upSubstate.substate as ComponentHeader).template_address === templateAddress) {
129
+ components.push(SimpleComponent.new(upSubstate.id, upSubstate.version, upSubstate.substate as ComponentHeader));
130
+ }
131
+ }
132
+
133
+ return components;
134
+ }
135
+
136
+ public getResultingAccounts(): GetAccountsResult[] {
137
+ const components = this.getComponentsByTemplateAddress(ACCOUNT_TEMPLATE_ADDRESS);
138
+
139
+ const accounts = [];
140
+ for (const component of components) {
141
+ const account = component.decodeBody<BuiltInAccount>();
142
+ accounts.push({ substate_id: component.id, version: component.version, account });
143
+ }
144
+
145
+ return accounts;
146
+ }
147
+
148
+ /**
149
+ * Returns the reject reason if the whole transaction was rejected or if the fee intent was executed but the main intent rejected
150
+ * Otherwise returns None.
151
+ */
152
+ public get anyRejectReason(): Option<RejectReason> {
153
+ return this.rejected.or(this.onlyFeeAccepted.map((x) => x[1]));
154
+ }
155
+
156
+ public get accept(): Option<SimpleSubstateDiff> {
157
+ const result = this.result as any;
158
+ const accept = result?.result.Accept;
159
+ if (!accept) {
160
+ return None;
161
+ }
162
+
163
+ return Some(SimpleSubstateDiff.from(accept));
164
+ }
165
+
166
+ public get diff(): Option<SimpleSubstateDiff> {
167
+ return this.accept.or(this.onlyFeeAccepted.map((x => x[0])));
168
+ }
169
+
170
+ public get onlyFeeAccepted(): Option<[SimpleSubstateDiff, RejectReason]> {
171
+ const result = this.result;
172
+ if (!result || !result.result || !("AcceptFeeRejectRest" in result.result)) {
173
+ return None;
174
+ }
175
+
176
+ const [diff, reason] = result?.result.AcceptFeeRejectRest;
177
+ return Some([SimpleSubstateDiff.from(diff), reason]);
178
+ }
179
+
180
+ public get rejected(): Option<RejectReason> {
181
+ const result = this.result as any;
182
+ if (!result || !result.result) {
183
+ return None;
184
+ }
185
+ if (!("Reject" in result.result)) {
186
+ return None;
187
+ }
188
+
189
+ return Some(result?.result.Reject);
190
+ }
191
+ }
192
+
193
+ export function splitOnce(str: string, separator: string): [string, string] | null {
194
+ const index = str.indexOf(separator);
195
+ if (index === -1) {
196
+ return null;
197
+ }
198
+ return [str.slice(0, index), str.slice(index + separator.length)];
199
+ }
200
+
201
+ function prefixToSubstateType(prefix: string): SubstateType | null {
202
+ switch (prefix) {
203
+ case "component":
204
+ return "Component";
205
+ case "resource":
206
+ return "Resource";
207
+ case "vault":
208
+ return "Vault";
209
+ case "nft":
210
+ return "NonFungible";
211
+ case "txreceipt":
212
+ return "TransactionReceipt";
213
+ case "vnfp":
214
+ return "ValidatorFeePool";
215
+ case "template":
216
+ return "Template";
217
+ default:
218
+ console.log("Unknown substate type prefix", prefix);
219
+ return null;
220
+ }
221
+ }
222
+
223
+ export class SimpleSubstateDiff {
224
+ private up_substates: UpSubstate[];
225
+ private down_substates: DownSubstate[];
226
+ private fee_withdrawals: ValidatorFeeWithdrawal[];
227
+
228
+ constructor(diff: SubstateDiff) {
229
+ this.up_substates = diff.up_substates
230
+ .map(([id, val]: [SubstateId | string, Substate]) => {
231
+
232
+ if (!val.substate) {
233
+ console.error("Substate is missing in the accept result", id, val);
234
+ return null;
235
+ }
236
+
237
+ const valType = Object.keys(val.substate)[0];
238
+ if (!valType) {
239
+ console.log("Substate is missing key", id, val);
240
+ return null;
241
+ }
242
+ const idVal = (typeof id === "string" ? id : Object.values(id)[0]) as string;
243
+ return {
244
+ type: valType,
245
+ id: idVal,
246
+ version: val.version,
247
+ // @ts-ignore
248
+ substate: val.substate[valType],
249
+ } as UpSubstate;
250
+ })
251
+ .filter((x) => x !== null);
252
+
253
+ this.down_substates = diff.down_substates
254
+ .map(([id, version]: [SubstateId | string, number]) => {
255
+ const type = (typeof id === "string" ? prefixToSubstateType(splitOnce(id, "_")![0]) : Object.keys(id)[0]);
256
+ const idVal = substateIdToString(id);
257
+ return {
258
+ type: type! as SubstateType,
259
+ id: idVal,
260
+ version,
261
+ } as DownSubstate;
262
+ });
263
+
264
+
265
+ this.fee_withdrawals = diff.fee_withdrawals;
266
+ }
267
+
268
+ public static from(diff: SubstateDiff): SimpleSubstateDiff {
269
+ return new SimpleSubstateDiff(diff);
270
+ }
271
+
272
+ public upSubstates(): UpSubstate[] {
273
+ return this.up_substates;
274
+ }
275
+
276
+ public downSubstates(): DownSubstate[] {
277
+ return this.down_substates;
278
+ }
279
+
280
+ public feeWithdrawals(): ValidatorFeeWithdrawal[] {
281
+ return this.fee_withdrawals;
282
+ }
283
+ }
284
+
285
+ export type AnySubstate =
286
+ ComponentHeader
287
+ | Resource
288
+ | Vault
289
+ | UnclaimedConfidentialOutput
290
+ | NonFungible
291
+ | TransactionReceipt
292
+ | ValidatorFeePool
293
+ | PublishedTemplate;
294
+
295
+ export type UpSubstate = {
296
+ type: SubstateType;
297
+ id: string;
298
+ version: number;
299
+ substate: AnySubstate
300
+ };
301
+
302
+ export type DownSubstate = {
303
+ type: SubstateType;
304
+ id: string;
305
+ version: number;
306
+ };
307
+
308
+ export class SimpleComponent {
309
+ private _id: string;
310
+ private _version: number;
311
+ private _substate: ComponentHeader;
312
+
313
+ constructor(id: string, version: number, substate: ComponentHeader) {
314
+ this._id = id;
315
+ this._version = version;
316
+ this._substate = substate;
317
+ }
318
+
319
+ public static new(id: string, version: number, substate: ComponentHeader): SimpleComponent {
320
+ return new SimpleComponent(id, version, substate);
321
+ }
322
+
323
+ public get id(): string {
324
+ return this._id;
325
+ }
326
+
327
+ public get version(): number {
328
+ return this._version;
329
+ }
330
+
331
+ public get substate(): ComponentHeader {
332
+ return this._substate;
333
+ }
334
+
335
+ public decodeBody<T>(): T {
336
+ return parseCbor(this.substate.body.state) as T;
337
+ }
338
+ }
339
+
340
+
341
+ export interface GetAccountsResult {
342
+ substate_id: string;
343
+ version: number;
344
+ account: BuiltInAccount;
345
+ }
@@ -59,11 +59,10 @@ export function getSubstateValueFromUpSubstates(
59
59
 
60
60
  export function getComponentsForTemplate(templateAddress: string, upSubstates: UpSubstates): ComponentAddress[] | null {
61
61
  const components: ComponentAddress[] = [];
62
- const templateAddressBytes = new TextEncoder().encode(templateAddress);
63
62
  for (const [substateId, substate] of upSubstates) {
64
63
  if ("Component" in substate.substate) {
65
64
  const componentHeader = substate.substate.Component;
66
- if (componentHeader.template_address === templateAddressBytes) {
65
+ if (componentHeader.template_address === templateAddress) {
67
66
  components.push(substateIdToString(substateId));
68
67
  }
69
68
  }
@@ -1,5 +1,6 @@
1
1
  export { ComponentAddress, ResourceAddress, PublishedTemplateAddress, Epoch } from "@tari-project/typescript-bindings";
2
2
  export { Amount } from "./Amount";
3
+ export { BuiltInAccount, VaultSubstate } from "./Account";
3
4
  export { TransactionArg } from "./TransactionArg";
4
5
  export { ConfidentialClaim } from "./ConfidentialClaim";
5
6
  export { ConfidentialOutput } from "./ConfidentialOutput";
@@ -53,4 +54,11 @@ export {
53
54
  txResultCheck,
54
55
  BinaryTag,
55
56
  CborValue,
57
+ SimpleTransactionResult,
58
+ SimpleSubstateDiff,
59
+ AnySubstate,
60
+ DownSubstate,
61
+ UpSubstate,
62
+ splitOnce,
56
63
  } from "./helpers";
64
+ export { ACCOUNT_TEMPLATE_ADDRESS, XTR, TARI_RESOURCE } from "./consts";
@@ -26,7 +26,7 @@ export interface AccountData {
26
26
  account_id: number;
27
27
  address: string;
28
28
  public_key: string;
29
- resources: VaultData[];
29
+ vaults: VaultData[];
30
30
  }
31
31
 
32
32
  export interface VaultData {
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tari-project/wallet-daemon-signer",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -93,6 +93,11 @@ export class WalletDaemonTariSigner implements TariSigner {
93
93
  return transport instanceof WebRtcRpcTransport ? transport : undefined;
94
94
  }
95
95
 
96
+ public async newTransactionKey(index?: number): Promise<string> {
97
+ const resp = await this.client.createKey({ branch: "transaction", specific_index: index || null });
98
+ return resp.public_key;
99
+ }
100
+
96
101
  public get token(): string | undefined {
97
102
  return this.getWebRtcTransport()?.token();
98
103
  }
@@ -125,7 +130,7 @@ export class WalletDaemonTariSigner implements TariSigner {
125
130
  account_id: res.account.key_index,
126
131
  address: (res.account.address as { Component: string }).Component,
127
132
  public_key: res.public_key,
128
- resources: [],
133
+ vaults: [],
129
134
  };
130
135
  }
131
136
 
@@ -142,7 +147,7 @@ export class WalletDaemonTariSigner implements TariSigner {
142
147
  address,
143
148
  public_key,
144
149
  // TODO: should be vaults not resources
145
- resources: balances.map((b: any) => ({
150
+ vaults: balances.map((b: any) => ({
146
151
  type: b.resource_type,
147
152
  resource_address: b.resource_address,
148
153
  balance: b.balance + b.confidential_balance,
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tari-project/wallet-connect-signer",
3
- "version": "0.11.0",
3
+ "version": "0.12.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -152,7 +152,7 @@ export class WalletConnectTariSigner implements TariSigner {
152
152
  address: account.address,
153
153
  public_key,
154
154
  // TODO: should be vaults not resources
155
- resources: balances.map((b: any) => ({
155
+ vaults: balances.map((b: any) => ({
156
156
  type: b.resource_type,
157
157
  resource_address: b.resource_address,
158
158
  balance: b.balance + b.confidential_balance,
@@ -213,7 +213,7 @@ export class WalletConnectTariSigner implements TariSigner {
213
213
  account_id: res.account.key_index,
214
214
  address: (res.account.address as { Component: string }).Component,
215
215
  public_key: res.public_key,
216
- resources: [],
216
+ vaults: [],
217
217
  };
218
218
  }
219
219
 
@@ -10,7 +10,7 @@ catalog:
10
10
  vitest: ^3.0.4
11
11
  vite: ^6.1.0
12
12
  '@types/node': ^22.13.1
13
- '@tari-project/typescript-bindings': '>=1.13.0'
13
+ '@tari-project/typescript-bindings': '>=1.14.0'
14
14
  '@tari-project/wallet_jrpc_client': ^1.7.2
15
15
  '@metamask/providers': ^18.2.0
16
16
  '@walletconnect/universal-provider': ^2.13.3
package/typedoc.json ADDED
@@ -0,0 +1,10 @@
1
+ {
2
+ "entryPoints": ["packages/builders/src/index.ts"],
3
+ "out": "tsdocs/api",
4
+ "tsconfig": "./tsconfig.json",
5
+ "excludePrivate": true,
6
+ "excludeInternal": true,
7
+ "excludeExternals": true,
8
+ "plugin": ["typedoc-plugin-markdown"],
9
+ "includeVersion": true
10
+ }