@tari-project/tarijs 0.1.24 → 0.3.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.
@@ -1,6 +1,6 @@
1
1
  import { TariProvider } from "../index";
2
2
  import { SubmitTransactionRequest, TransactionResult, SubmitTransactionResponse, VaultBalances, TemplateDefinition, Substate, ListSubstatesResponse } from "../types";
3
- import { MetaMaskInpageProvider } from '@metamask/providers';
3
+ import { MetaMaskInpageProvider } from "@metamask/providers";
4
4
  import { Snap } from "./utils";
5
5
  import { Account } from "../types";
6
6
  import { SubstateType } from "@tari-project/typescript-bindings";
@@ -1,8 +1,8 @@
1
1
  import { TransactionStatus, } from "../types";
2
2
  import { connectSnap, getSnap, isFlask } from "./utils";
3
- export const MetamaskNotInstalled = 'METAMASK_NOT_INSTALLED';
4
- export const MetamaskIsNotFlask = 'METAMASK_IS_NOT_FLASK';
5
- export const TariSnapNotInstalled = 'TARI_SNAP_NOT_INSTALLED';
3
+ export const MetamaskNotInstalled = "METAMASK_NOT_INSTALLED";
4
+ export const MetamaskIsNotFlask = "METAMASK_IS_NOT_FLASK";
5
+ export const TariSnapNotInstalled = "TARI_SNAP_NOT_INSTALLED";
6
6
  export class MetamaskTariProvider {
7
7
  providerName = "Metamask";
8
8
  snapId;
@@ -41,32 +41,32 @@ export class MetamaskTariProvider {
41
41
  return this.metamaskConnected;
42
42
  }
43
43
  async createFreeTestCoins(account_id) {
44
- const res = await this.metamaskRequest('getFreeTestCoins', {
44
+ const res = (await this.metamaskRequest("getFreeTestCoins", {
45
45
  amount: 1000000,
46
46
  account_id,
47
- fee: 2000
48
- });
47
+ fee: 2000,
48
+ }));
49
49
  return {
50
50
  account_id,
51
51
  address: res.address,
52
52
  public_key: res.public_key,
53
- resources: []
53
+ resources: [],
54
54
  };
55
55
  }
56
56
  async getAccount() {
57
- return await this.metamaskRequest('getAccountData', { account_id: 0 });
57
+ return (await this.metamaskRequest("getAccountData", { account_id: 0 }));
58
58
  }
59
59
  async getSubstate(substate_address) {
60
- const { substate, address: substate_id, version } = await this.metamaskRequest('getSubstate', { substate_address });
60
+ const { substate, address: substate_id, version, } = await this.metamaskRequest("getSubstate", { substate_address });
61
61
  return { value: substate, address: { substate_id, version } };
62
62
  }
63
63
  async listSubstates(filter_by_template, filter_by_type, limit, offset) {
64
- const res = await this.metamaskRequest('listSubstates', {
64
+ const res = (await this.metamaskRequest("listSubstates", {
65
65
  filter_by_template,
66
66
  filter_by_type,
67
67
  limit,
68
68
  offset,
69
- });
69
+ }));
70
70
  return res;
71
71
  }
72
72
  async submitTransaction(req) {
@@ -77,7 +77,7 @@ export class MetamaskTariProvider {
77
77
  required_substates: req.required_substates || [],
78
78
  is_dry_run: req.is_dry_run,
79
79
  };
80
- const resp = await this.metamaskRequest('sendTransaction', params);
80
+ const resp = await this.metamaskRequest("sendTransaction", params);
81
81
  if (!resp) {
82
82
  throw new Error("Failed to submit transaction to metamask snap: empty response");
83
83
  }
@@ -88,7 +88,7 @@ export class MetamaskTariProvider {
88
88
  }
89
89
  async getTransactionResult(transactionId) {
90
90
  // This request returns the response from the indexer get_transaction_result request
91
- const resp = await this.metamaskRequest('getTransactionResult', { transaction_id: transactionId });
91
+ const resp = await this.metamaskRequest("getTransactionResult", { transaction_id: transactionId });
92
92
  if (!resp) {
93
93
  throw new Error("Failed to get transaction result from metamask snap: empty response");
94
94
  }
@@ -96,7 +96,7 @@ export class MetamaskTariProvider {
96
96
  return {
97
97
  transaction_id: transactionId,
98
98
  status: TransactionStatus.Pending,
99
- result: null
99
+ result: null,
100
100
  };
101
101
  }
102
102
  if (!resp?.result?.Finalized) {
@@ -106,28 +106,27 @@ export class MetamaskTariProvider {
106
106
  return {
107
107
  transaction_id: transactionId,
108
108
  status: newStatus,
109
- result: resp.result.Finalized.execution_result.finalize
109
+ result: resp.result.Finalized.execution_result.finalize,
110
110
  };
111
111
  }
112
112
  async getPublicKey(_branch, index) {
113
- const resp = await this.metamaskRequest('getPublicKey', { index });
113
+ const resp = await this.metamaskRequest("getPublicKey", { index });
114
114
  if (!resp) {
115
115
  throw new Error("Failed to get public key from metamask snap: empty response");
116
116
  }
117
117
  return resp.public_key;
118
118
  }
119
119
  async getConfidentialVaultBalances(viewKeyId, vaultId, min = null, max = null) {
120
- const res = await this.metamaskRequest('getConfidentialVaultBalances', {
120
+ const res = (await this.metamaskRequest("getConfidentialVaultBalances", {
121
121
  view_key_id: viewKeyId,
122
122
  vault_id: vaultId,
123
123
  minimum_expected_value: min,
124
124
  maximum_expected_value: max,
125
- });
125
+ }));
126
126
  return { balances: res };
127
127
  }
128
128
  getTemplateDefinition(template_address) {
129
- return this.metamaskRequest('getTemplateDefinition', { template_address })
130
- .then(resp => {
129
+ return this.metamaskRequest("getTemplateDefinition", { template_address }).then((resp) => {
131
130
  if (!resp) {
132
131
  throw new Error("Template not found");
133
132
  }
@@ -137,13 +136,13 @@ export class MetamaskTariProvider {
137
136
  async metamaskRequest(method, params) {
138
137
  console.log("Metamask request:", method, params);
139
138
  const resp = await this.metamask.request({
140
- method: 'wallet_invokeSnap',
139
+ method: "wallet_invokeSnap",
141
140
  params: {
142
141
  snapId: this.snapId,
143
142
  request: {
144
143
  method,
145
- params
146
- }
144
+ params,
145
+ },
147
146
  },
148
147
  });
149
148
  console.log("Metamask response:", resp);
@@ -1,3 +1,4 @@
1
+ import { sendProviderCall } from "./utils";
1
2
  export class TariUniverseProvider {
2
3
  params;
3
4
  providerName = "TariUniverse";
@@ -16,20 +17,7 @@ export class TariUniverseProvider {
16
17
  }
17
18
  async sendRequest(req) {
18
19
  const id = ++this.__id;
19
- return new Promise(function (resolve, reject) {
20
- const event_ref = function (resp) {
21
- if (resp.data.resultError) {
22
- window.removeEventListener("message", event_ref);
23
- reject(resp.data.resultError);
24
- }
25
- if (resp && resp.data && resp.data.id && resp.data.id == id && resp.data.type === "provider-call") {
26
- window.removeEventListener("message", event_ref);
27
- resolve(resp.data.result);
28
- }
29
- };
30
- window.addEventListener("message", event_ref, false);
31
- window.parent.postMessage({ ...req, id, type: "provider-call" }, "*");
32
- });
20
+ return sendProviderCall(req, id);
33
21
  }
34
22
  isConnected() {
35
23
  return true;
@@ -56,7 +44,20 @@ export class TariUniverseProvider {
56
44
  return this.sendRequest({ methodName: "requestParentSize", args: [] });
57
45
  }
58
46
  async getAccount() {
59
- return this.sendRequest({ methodName: "getAccount", args: [] });
47
+ const { account_id, address, public_key } = await this.sendRequest({ methodName: "getAccount", args: [] });
48
+ const { balances } = await this.getAccountBalances(address);
49
+ return {
50
+ account_id,
51
+ address,
52
+ public_key,
53
+ resources: balances.map((b) => ({
54
+ type: b.resource_type,
55
+ resource_address: b.resource_address,
56
+ balance: b.balance + b.confidential_balance,
57
+ vault_id: "Vault" in b.vault_address ? b.vault_address.Vault : b.vault_address,
58
+ token_symbol: b.token_symbol,
59
+ })),
60
+ };
60
61
  }
61
62
  async getAccountBalances(componentAddress) {
62
63
  return this.sendRequest({
@@ -0,0 +1,2 @@
1
+ import { ProviderMethodNames, ProviderRequest, ProviderReturnType } from "./types";
2
+ export declare function sendProviderCall<MethodName extends ProviderMethodNames>(req: Omit<ProviderRequest<MethodName>, "id">, id: number): Promise<ProviderReturnType<MethodName>>;
@@ -0,0 +1,16 @@
1
+ export function sendProviderCall(req, id) {
2
+ return new Promise((resolve, reject) => {
3
+ const event_ref = (resp) => {
4
+ if (resp.data.resultError) {
5
+ window.removeEventListener("message", event_ref);
6
+ reject(resp.data.resultError);
7
+ }
8
+ if (resp && resp.data && resp.data.id && resp.data.id === id && resp.data.type === "provider-call") {
9
+ window.removeEventListener("message", event_ref);
10
+ resolve(resp.data.result);
11
+ }
12
+ };
13
+ window.addEventListener("message", event_ref, false);
14
+ window.parent.postMessage({ ...req, id, type: "provider-call" }, "*");
15
+ });
16
+ }
@@ -91,21 +91,21 @@ export class WalletDaemonTariProvider {
91
91
  }
92
92
  async submitTransaction(req) {
93
93
  const params = {
94
- transaction: null,
94
+ transaction: {
95
+ instructions: req.instructions,
96
+ fee_instructions: req.fee_instructions,
97
+ inputs: req.required_substates.map((s) => ({
98
+ // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
99
+ substate_id: s.substate_id,
100
+ version: s.version
101
+ })),
102
+ min_epoch: null,
103
+ max_epoch: null,
104
+ },
95
105
  signing_key_index: req.account_id,
96
- fee_instructions: req.fee_instructions,
97
- instructions: req.instructions,
98
- inputs: req.required_substates.map((s) => ({
99
- // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
100
- substate_id: s.substate_id,
101
- version: s.version
102
- })),
103
- input_refs: [],
104
- override_inputs: false,
105
- is_dry_run: req.is_dry_run,
106
+ autofill_inputs: [],
107
+ detect_inputs: true,
106
108
  proof_ids: [],
107
- min_epoch: null,
108
- max_epoch: null,
109
109
  };
110
110
  const res = await this.client.submitTransaction(params);
111
111
  return { transaction_id: res.transaction_id };
@@ -148,21 +148,21 @@ export class WalletConnectTariProvider {
148
148
  async submitTransaction(req) {
149
149
  const method = 'tari_submitTransaction';
150
150
  const params = {
151
- transaction: null,
151
+ transaction: {
152
+ fee_instructions: req.fee_instructions,
153
+ instructions: req.instructions,
154
+ inputs: req.required_substates.map((s) => ({
155
+ // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
156
+ substate_id: s.substate_id,
157
+ version: s.version
158
+ })),
159
+ min_epoch: null,
160
+ max_epoch: null,
161
+ },
152
162
  signing_key_index: req.account_id,
153
- fee_instructions: req.fee_instructions,
154
- instructions: req.instructions,
155
- inputs: req.required_substates.map((s) => ({
156
- // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
157
- substate_id: s.substate_id,
158
- version: s.version
159
- })),
160
- input_refs: [],
161
- override_inputs: false,
162
- is_dry_run: req.is_dry_run,
163
+ autofill_inputs: [],
164
+ detect_inputs: true,
163
165
  proof_ids: [],
164
- min_epoch: null,
165
- max_epoch: null,
166
166
  };
167
167
  const res = await this.sendRequest(method, params);
168
168
  return { transaction_id: res.transaction_id };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tari-project/tarijs",
3
- "version": "0.1.24",
3
+ "version": "0.3.0",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
@@ -13,9 +13,9 @@
13
13
  "author": "",
14
14
  "license": "ISC",
15
15
  "dependencies": {
16
- "@metamask/providers": "^9.0.0",
17
- "@tari-project/wallet_jrpc_client": "^1.0.8",
18
- "@tari-project/typescript-bindings": "^1.0.6",
16
+ "@metamask/providers": "^18.2.0",
17
+ "@tari-project/wallet_jrpc_client": "^1.1.0",
18
+ "@tari-project/typescript-bindings": "^1.1.0",
19
19
  "@walletconnect/modal": "^2.6.2",
20
20
  "@walletconnect/universal-provider": "^2.13.3"
21
21
  },