@tari-project/tarijs 0.3.0 → 0.3.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.
@@ -4,7 +4,7 @@ import { SubmitTransactionRequest, SubstateRequirement } from "../../providers/t
4
4
  import { Transaction, TransactionResult } from "../types";
5
5
  import { DownSubstates, UpSubstates } from "../types/SubstateDiff";
6
6
  import { SubmitTxResult } from "../types/TransactionResult";
7
- export declare function buildTransactionRequest(transaction: Transaction, accountId: number, requiredSubstates: SubstateRequirement[], inputRefs?: never[], isDryRun?: boolean): SubmitTransactionRequest;
7
+ export declare function buildTransactionRequest(transaction: Transaction, accountId: number, requiredSubstates: SubstateRequirement[], inputRefs?: never[], isDryRun?: boolean, network?: number, isSealSignerAuthorized?: boolean, detectInputsUseUnversioned?: boolean): SubmitTransactionRequest;
8
8
  export declare function submitAndWaitForTransaction(provider: TariProvider, req: SubmitTransactionRequest): Promise<SubmitTxResult>;
9
9
  export declare function waitForTransactionResult(provider: TariProvider | TariUniverseProvider, transactionId: string): Promise<TransactionResult>;
10
10
  export declare function getAcceptResultSubstates(txResult: TransactionResult): {
@@ -1,8 +1,9 @@
1
1
  import { TransactionStatus } from "../types";
2
- export function buildTransactionRequest(transaction, accountId, requiredSubstates, inputRefs = [], isDryRun = false) {
2
+ export function buildTransactionRequest(transaction, accountId, requiredSubstates, inputRefs = [], isDryRun = false, network = 0, isSealSignerAuthorized = true, detectInputsUseUnversioned = true) {
3
3
  return {
4
4
  //TODO refactor SubTxReq type to not use 'object[]' and types match
5
5
  // https://github.com/tari-project/tari.js/issues/25
6
+ network,
6
7
  account_id: accountId,
7
8
  instructions: transaction.instructions,
8
9
  fee_instructions: transaction.feeInstructions,
@@ -12,6 +13,8 @@ export function buildTransactionRequest(transaction, accountId, requiredSubstate
12
13
  is_dry_run: isDryRun,
13
14
  min_epoch: transaction.minEpoch ?? null,
14
15
  max_epoch: transaction.maxEpoch ?? null,
16
+ is_seal_signer_authorized: isSealSignerAuthorized,
17
+ detect_inputs_use_unversioned: detectInputsUseUnversioned,
15
18
  };
16
19
  }
17
20
  export async function submitAndWaitForTransaction(provider, req) {
@@ -1,8 +1,8 @@
1
- import { TransactionSignature } from "@tari-project/typescript-bindings";
2
1
  import { Instruction } from "./Instruction";
3
2
  import { SubstateRequirement } from "./SubstateRequirement";
4
3
  import { Epoch } from "./Epoch";
5
4
  import { VersionedSubstateId } from "./VersionedSubstateId";
5
+ import { TransactionSignature } from "./TransactionSignature";
6
6
  export interface Transaction {
7
7
  id: string;
8
8
  feeInstructions: Array<Instruction>;
@@ -10,6 +10,7 @@ export type SubstateRequirement = {
10
10
  version?: number | null;
11
11
  };
12
12
  export type SubmitTransactionRequest = {
13
+ network: number;
13
14
  account_id: number;
14
15
  instructions: object[];
15
16
  fee_instructions: object[];
@@ -19,6 +20,8 @@ export type SubmitTransactionRequest = {
19
20
  is_dry_run: boolean;
20
21
  min_epoch: number | null;
21
22
  max_epoch: number | null;
23
+ is_seal_signer_authorized: boolean;
24
+ detect_inputs_use_unversioned: boolean;
22
25
  };
23
26
  export type SubmitTransactionResponse = {
24
27
  transaction_id: string;
@@ -5,27 +5,35 @@ import { Account } from "../types";
5
5
  import { WalletDaemonClient, SubstateType } from "@tari-project/wallet_jrpc_client";
6
6
  export declare const WalletDaemonNotConnected = "WALLET_DAEMON_NOT_CONNECTED";
7
7
  export declare const Unsupported = "UNSUPPORTED";
8
- export type WalletDaemonParameters = {
9
- signalingServerUrl?: string;
8
+ export interface WalletDaemonBaseParameters {
10
9
  permissions: TariPermissions;
11
- optionalPermissions: TariPermissions;
10
+ optionalPermissions?: TariPermissions;
11
+ onConnection?: () => void;
12
+ }
13
+ export interface WalletDaemonParameters extends WalletDaemonBaseParameters {
14
+ signalingServerUrl?: string;
12
15
  webRtcConfig?: RTCConfiguration;
13
16
  name?: string;
14
- onConnection?: () => void;
15
- };
17
+ }
18
+ export interface WalletDaemonFetchParameters extends WalletDaemonBaseParameters {
19
+ serverUrl: string;
20
+ }
16
21
  export declare class WalletDaemonTariProvider implements TariProvider {
17
22
  providerName: string;
18
23
  params: WalletDaemonParameters;
19
24
  client: WalletDaemonClient;
20
25
  private constructor();
21
26
  static build(params: WalletDaemonParameters): Promise<WalletDaemonTariProvider>;
27
+ static buildFetchProvider(params: WalletDaemonFetchParameters): Promise<WalletDaemonTariProvider>;
28
+ private static buildPermissions;
29
+ private getWebRtcTransport;
22
30
  get token(): string | undefined;
23
31
  get tokenUrl(): string | undefined;
24
32
  isConnected(): boolean;
25
33
  createFreeTestCoins(): Promise<Account>;
26
34
  getAccount(): Promise<Account>;
27
35
  getAccountBalances(componentAddress: string): Promise<unknown>;
28
- getSubstate(substate_id: string): Promise<Substate>;
36
+ getSubstate(substateId: string): Promise<Substate>;
29
37
  submitTransaction(req: SubmitTransactionRequest): Promise<SubmitTransactionResponse>;
30
38
  getTransactionResult(transactionId: string): Promise<TransactionResult>;
31
39
  getPublicKey(branch: string, index: number): Promise<string>;
@@ -1,10 +1,11 @@
1
1
  import { TariPermissions, } from "./tari_permissions";
2
2
  import { TariConnection } from "./webrtc";
3
3
  import { TransactionStatus, } from "../types";
4
- import { WalletDaemonClient, stringToSubstateId, substateIdToString, } from "@tari-project/wallet_jrpc_client";
4
+ import { WalletDaemonClient, substateIdToString, } from "@tari-project/wallet_jrpc_client";
5
5
  import { WebRtcRpcTransport } from "./webrtc_transport";
6
6
  export const WalletDaemonNotConnected = 'WALLET_DAEMON_NOT_CONNECTED';
7
7
  export const Unsupported = 'UNSUPPORTED';
8
+ ;
8
9
  export class WalletDaemonTariProvider {
9
10
  providerName = "WalletDaemon";
10
11
  params;
@@ -14,10 +15,7 @@ export class WalletDaemonTariProvider {
14
15
  this.client = connection;
15
16
  }
16
17
  static async build(params) {
17
- const allPermissions = new TariPermissions();
18
- allPermissions.addPermissions(params.permissions);
19
- allPermissions.addPermissions(params.optionalPermissions);
20
- console.log({ allPermissions });
18
+ const allPermissions = WalletDaemonTariProvider.buildPermissions(params);
21
19
  let connection = new TariConnection(params.signalingServerUrl, params.webRtcConfig);
22
20
  const client = WalletDaemonClient.new(WebRtcRpcTransport.new(connection));
23
21
  await connection.init(allPermissions, (conn) => {
@@ -28,8 +26,29 @@ export class WalletDaemonTariProvider {
28
26
  });
29
27
  return new WalletDaemonTariProvider(params, client);
30
28
  }
29
+ static async buildFetchProvider(params) {
30
+ const allPermissions = WalletDaemonTariProvider.buildPermissions(params);
31
+ const client = WalletDaemonClient.usingFetchTransport(params.serverUrl);
32
+ const plainPermissions = allPermissions.toJSON().flatMap((p) => typeof (p) === "string" ? [p] : []);
33
+ const authResponse = await client.authRequest(plainPermissions);
34
+ await client.authAccept(authResponse, "WalletDaemon");
35
+ params.onConnection?.();
36
+ return new WalletDaemonTariProvider(params, client);
37
+ }
38
+ static buildPermissions(params) {
39
+ const allPermissions = new TariPermissions();
40
+ allPermissions.addPermissions(params.permissions);
41
+ if (params.optionalPermissions) {
42
+ allPermissions.addPermissions(params.optionalPermissions);
43
+ }
44
+ return allPermissions;
45
+ }
46
+ getWebRtcTransport() {
47
+ const transport = this.client.getTransport();
48
+ return transport instanceof WebRtcRpcTransport ? transport : undefined;
49
+ }
31
50
  get token() {
32
- return this.client.getTransport().token();
51
+ return this.getWebRtcTransport()?.token();
33
52
  }
34
53
  get tokenUrl() {
35
54
  if (!this.token) {
@@ -42,7 +61,7 @@ export class WalletDaemonTariProvider {
42
61
  return `tari://${name}/${token}/${permissions}/${optionalPermissions}`;
43
62
  }
44
63
  isConnected() {
45
- return this.client.getTransport().isConnected();
64
+ return this.getWebRtcTransport()?.isConnected() || true;
46
65
  }
47
66
  async createFreeTestCoins() {
48
67
  const res = await this.client.createFreeTestCoins({
@@ -60,17 +79,18 @@ export class WalletDaemonTariProvider {
60
79
  }
61
80
  async getAccount() {
62
81
  const { account, public_key } = await this.client.accountsGetDefault({});
63
- const { balances } = await this.client.accountsGetBalances({ account: { ComponentAddress: account.address.Component }, refresh: false });
82
+ const address = typeof account.address === "object" ? account.address.Component : account.address;
83
+ const { balances } = await this.client.accountsGetBalances({ account: { ComponentAddress: address }, refresh: false });
64
84
  return {
65
85
  account_id: account.key_index,
66
- address: account.address.Component,
86
+ address,
67
87
  public_key,
68
88
  // TODO: should be vaults not resources
69
89
  resources: balances.map((b) => ({
70
90
  type: b.resource_type,
71
91
  resource_address: b.resource_address,
72
92
  balance: b.balance + b.confidential_balance,
73
- vault_id: ('Vault' in b.vault_address) ? b.vault_address.Vault : b.vault_address,
93
+ vault_id: (typeof (b.vault_address) === "object" && 'Vault' in b.vault_address) ? b.vault_address.Vault : b.vault_address,
74
94
  token_symbol: b.token_symbol,
75
95
  }))
76
96
  };
@@ -78,8 +98,8 @@ export class WalletDaemonTariProvider {
78
98
  async getAccountBalances(componentAddress) {
79
99
  return await this.client.accountsGetBalances({ account: { ComponentAddress: componentAddress }, refresh: true });
80
100
  }
81
- async getSubstate(substate_id) {
82
- const substateId = stringToSubstateId(substate_id);
101
+ async getSubstate(substateId) {
102
+ // Wallet daemon expects a SubstateId as a string
83
103
  const { value, record } = await this.client.substatesGet({ substate_id: substateId });
84
104
  return {
85
105
  value,
@@ -92,20 +112,25 @@ export class WalletDaemonTariProvider {
92
112
  async submitTransaction(req) {
93
113
  const params = {
94
114
  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,
115
+ V1: {
116
+ network: req.network,
117
+ instructions: req.instructions,
118
+ fee_instructions: req.fee_instructions,
119
+ inputs: req.required_substates.map((s) => ({
120
+ // TODO: Hmm The bindings want a SubstateId object, but the wallet only wants a string. Any is used to skip type checking here
121
+ substate_id: s.substate_id,
122
+ version: s.version ?? null,
123
+ })),
124
+ min_epoch: null,
125
+ max_epoch: null,
126
+ is_seal_signer_authorized: req.is_seal_signer_authorized,
127
+ },
104
128
  },
105
129
  signing_key_index: req.account_id,
106
130
  autofill_inputs: [],
107
131
  detect_inputs: true,
108
132
  proof_ids: [],
133
+ detect_inputs_use_unversioned: req.detect_inputs_use_unversioned,
109
134
  };
110
135
  const res = await this.client.submitTransaction(params);
111
136
  return { transaction_id: res.transaction_id };
@@ -143,10 +168,10 @@ export class WalletDaemonTariProvider {
143
168
  offset
144
169
  });
145
170
  const substates = resp.substates.map((s) => ({
146
- substate_id: substateIdToString(s.substate_id),
171
+ substate_id: typeof s.substate_id === "string" ? s.substate_id : substateIdToString(s.substate_id),
147
172
  module_name: s.module_name,
148
173
  version: s.version,
149
- template_address: s.template_address
174
+ template_address: s.template_address,
150
175
  }));
151
176
  return { substates };
152
177
  }
@@ -1,7 +1,7 @@
1
1
  import { TransactionStatus, } from "../types";
2
2
  import UniversalProvider from '@walletconnect/universal-provider';
3
3
  import { WalletConnectModal } from '@walletconnect/modal';
4
- import { stringToSubstateId, substateIdToString } from "@tari-project/wallet_jrpc_client";
4
+ import { substateIdToString } from "@tari-project/wallet_jrpc_client";
5
5
  const walletConnectParams = {
6
6
  requiredNamespaces: {
7
7
  tari: {
@@ -83,31 +83,30 @@ export class WalletConnectTariProvider {
83
83
  }
84
84
  async getAccount() {
85
85
  const { account, public_key } = await this.sendRequest('tari_getDefaultAccount', {});
86
- const { balances } = await this.sendRequest('tari_getAccountBalances', { account: { ComponentAddress: account.address.Component }, refresh: false
86
+ const { balances } = await this.sendRequest('tari_getAccountBalances', { account: { ComponentAddress: account.address }, refresh: false
87
87
  });
88
88
  return {
89
89
  account_id: account.key_index,
90
- address: account.address.Component,
90
+ address: account.address,
91
91
  public_key,
92
92
  // TODO: should be vaults not resources
93
93
  resources: balances.map((b) => ({
94
94
  type: b.resource_type,
95
95
  resource_address: b.resource_address,
96
96
  balance: b.balance + b.confidential_balance,
97
- vault_id: ('Vault' in b.vault_address) ? b.vault_address.Vault : b.vault_address,
97
+ vault_id: (typeof (b.vault_address) === "object" && 'Vault' in b.vault_address) ? b.vault_address.Vault : b.vault_address,
98
98
  token_symbol: b.token_symbol,
99
99
  }))
100
100
  };
101
101
  }
102
102
  async getSubstate(substate_address) {
103
- const substateId = stringToSubstateId(substate_address);
104
103
  const method = 'tari_getSubstate';
105
- const params = { substate_id: substateId };
104
+ const params = { substate_id: substate_address };
106
105
  const { value, record } = await this.sendRequest(method, params);
107
106
  return {
108
107
  value,
109
108
  address: {
110
- substate_id: substateIdToString(record.substate_id),
109
+ substate_id: record.substate_id,
111
110
  version: record.version
112
111
  }
113
112
  };
@@ -149,20 +148,25 @@ export class WalletConnectTariProvider {
149
148
  const method = 'tari_submitTransaction';
150
149
  const params = {
151
150
  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,
151
+ V1: {
152
+ network: req.network,
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 ?? null,
159
+ })),
160
+ min_epoch: null,
161
+ max_epoch: null,
162
+ is_seal_signer_authorized: req.is_seal_signer_authorized,
163
+ },
161
164
  },
162
165
  signing_key_index: req.account_id,
163
166
  autofill_inputs: [],
164
167
  detect_inputs: true,
165
168
  proof_ids: [],
169
+ detect_inputs_use_unversioned: req.detect_inputs_use_unversioned,
166
170
  };
167
171
  const res = await this.sendRequest(method, params);
168
172
  return { transaction_id: res.transaction_id };
package/package.json CHANGED
@@ -1,27 +1,29 @@
1
1
  {
2
2
  "name": "@tari-project/tarijs",
3
- "version": "0.3.0",
3
+ "version": "0.3.1",
4
4
  "description": "",
5
5
  "type": "module",
6
6
  "publishConfig": {
7
7
  "access": "public"
8
8
  },
9
9
  "scripts": {
10
- "build": "tsc -b"
10
+ "build": "tsc -b",
11
+ "integration-tests": "vitest run integration-tests"
11
12
  },
12
13
  "keywords": [],
13
14
  "author": "",
14
15
  "license": "ISC",
15
16
  "dependencies": {
16
17
  "@metamask/providers": "^18.2.0",
17
- "@tari-project/wallet_jrpc_client": "^1.1.0",
18
- "@tari-project/typescript-bindings": "^1.1.0",
18
+ "@tari-project/typescript-bindings": "^1.4.0",
19
+ "@tari-project/wallet_jrpc_client": "^1.4.0",
19
20
  "@walletconnect/modal": "^2.6.2",
20
21
  "@walletconnect/universal-provider": "^2.13.3"
21
22
  },
22
23
  "devDependencies": {
23
24
  "@types/node": "^20.12.6",
24
- "typescript": "^5.0.4"
25
+ "typescript": "^5.0.4",
26
+ "vitest": "^3.0.4"
25
27
  },
26
28
  "files": [
27
29
  "/dist"