@tari-project/tarijs 0.3.1 → 0.4.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 (199) hide show
  1. package/.github/ISSUE_TEMPLATE/bug_report.md +36 -0
  2. package/.github/PULL_REQUEST_TEMPLATE.md +27 -0
  3. package/.github/dependabot.yml +8 -0
  4. package/.github/workflows/ci.yml +25 -0
  5. package/.github/workflows/npm_publish.yml +26 -0
  6. package/.github/workflows/pr_signed_commits_check.yml +19 -0
  7. package/.github/workflows/pr_title.yml +30 -0
  8. package/.moon/toolchain.yml +6 -0
  9. package/.moon/workspace.yml +32 -0
  10. package/.prettierrc +12 -0
  11. package/.prototools +9 -0
  12. package/CODEOWNERS +3 -0
  13. package/README.md +9 -8
  14. package/TODO.md +12 -0
  15. package/package.json +6 -24
  16. package/packages/builders/moon.yml +55 -0
  17. package/packages/builders/package.json +30 -0
  18. package/packages/builders/src/helpers/submitTransaction.ts +97 -0
  19. package/{dist/builders/helpers/workspace.js → packages/builders/src/helpers/workspace.ts} +10 -7
  20. package/packages/builders/src/index.ts +9 -0
  21. package/packages/builders/src/transaction/TransactionBuilder.ts +276 -0
  22. package/packages/builders/src/transaction/TransactionRequest.ts +93 -0
  23. package/packages/builders/src/transaction/index.ts +2 -0
  24. package/packages/builders/tsconfig.json +24 -0
  25. package/packages/metamask_provider/moon.yml +55 -0
  26. package/packages/metamask_provider/package.json +30 -0
  27. package/packages/metamask_provider/src/index.ts +232 -0
  28. package/packages/metamask_provider/src/utils.ts +86 -0
  29. package/packages/metamask_provider/tsconfig.json +21 -0
  30. package/packages/tari_permissions/moon.yml +55 -0
  31. package/packages/tari_permissions/package.json +26 -0
  32. package/packages/tari_permissions/src/index.ts +1 -0
  33. package/packages/tari_permissions/src/tari_permissions.ts +312 -0
  34. package/packages/tari_permissions/tsconfig.json +15 -0
  35. package/packages/tari_provider/moon.yml +55 -0
  36. package/packages/tari_provider/package.json +27 -0
  37. package/packages/tari_provider/src/TariProvider.ts +35 -0
  38. package/packages/tari_provider/src/index.ts +3 -0
  39. package/packages/tari_provider/src/types.ts +82 -0
  40. package/packages/tari_provider/tsconfig.json +11 -0
  41. package/packages/tari_universe/moon.yml +55 -0
  42. package/packages/tari_universe/package.json +31 -0
  43. package/{dist/providers/tari_universe/index.d.ts → packages/tari_universe/src/index.ts} +1 -1
  44. package/packages/tari_universe/src/provider.ts +135 -0
  45. package/{dist/providers/tari_universe/types.d.ts → packages/tari_universe/src/types.ts} +20 -17
  46. package/packages/tari_universe/src/utils.ts +23 -0
  47. package/packages/tari_universe/tsconfig.json +24 -0
  48. package/packages/tarijs/integration-tests/.env +1 -0
  49. package/packages/tarijs/integration-tests/wallet_daemon/json_rpc_provider.spec.ts +191 -0
  50. package/packages/tarijs/moon.yml +63 -0
  51. package/packages/tarijs/package.json +39 -0
  52. package/packages/tarijs/src/index.ts +69 -0
  53. package/packages/tarijs/src/network.ts +8 -0
  54. package/packages/tarijs/src/templates/Account.ts +98 -0
  55. package/packages/tarijs/src/templates/Pool.ts +42 -0
  56. package/packages/tarijs/src/templates/Tariswap.ts +101 -0
  57. package/packages/tarijs/src/templates/TemplateFactory.ts +22 -0
  58. package/packages/tarijs/src/templates/TestFaucet.ts +72 -0
  59. package/{dist/utils.js → packages/tarijs/src/utils.ts} +4 -3
  60. package/packages/tarijs/tsconfig.json +39 -0
  61. package/packages/tarijs/vitest.config.ts +9 -0
  62. package/packages/tarijs_types/moon.yml +55 -0
  63. package/packages/tarijs_types/package.json +27 -0
  64. package/packages/tarijs_types/src/Amount.ts +113 -0
  65. package/packages/tarijs_types/src/Arg.ts +3 -0
  66. package/packages/tarijs_types/src/ComponentAddress.ts +3 -0
  67. package/packages/tarijs_types/src/ConfidentialClaim.ts +9 -0
  68. package/packages/tarijs_types/src/ConfidentialOutput.ts +9 -0
  69. package/packages/tarijs_types/src/ConfidentialOutputStatement.ts +10 -0
  70. package/packages/tarijs_types/src/ConfidentialStatement.ts +9 -0
  71. package/packages/tarijs_types/src/ConfidentialWithdrawProof.ts +8 -0
  72. package/packages/tarijs_types/src/ElgamalVerifiableBalance.ts +4 -0
  73. package/packages/tarijs_types/src/Epoch.ts +3 -0
  74. package/packages/tarijs_types/src/FinalizeResult.ts +17 -0
  75. package/packages/tarijs_types/src/Instruction.ts +29 -0
  76. package/{dist/builders/types/ResourceAddress.js → packages/tarijs_types/src/ResourceAddress.ts} +2 -1
  77. package/{dist/builders/types/SubstateDiff.d.ts → packages/tarijs_types/src/SubstateDiff.ts} +4 -2
  78. package/{dist/builders/types/SubstateRequirement.d.ts → packages/tarijs_types/src/SubstateRequirement.ts} +3 -2
  79. package/{dist/builders/types/TemplateAddress.js → packages/tarijs_types/src/TemplateAddress.ts} +2 -1
  80. package/{dist/builders/types/Transaction.d.ts → packages/tarijs_types/src/Transaction.ts} +9 -8
  81. package/packages/tarijs_types/src/TransactionId.ts +4 -0
  82. package/packages/tarijs_types/src/TransactionResult.ts +26 -0
  83. package/packages/tarijs_types/src/TransactionSignature.ts +4 -0
  84. package/packages/tarijs_types/src/UnsignedTransaction.ts +15 -0
  85. package/{dist/builders/types/VersionedSubstateId.d.ts → packages/tarijs_types/src/VersionedSubstateId.ts} +3 -2
  86. package/packages/tarijs_types/src/ViewableBalanceProof.ts +12 -0
  87. package/{dist/builders/types/Workspace.d.ts → packages/tarijs_types/src/Workspace.ts} +1 -1
  88. package/{dist/builders/types/index.d.ts → packages/tarijs_types/src/index.ts} +4 -3
  89. package/packages/tarijs_types/tsconfig.json +15 -0
  90. package/packages/wallet_daemon/moon.yml +55 -0
  91. package/packages/wallet_daemon/package.json +29 -0
  92. package/{dist/providers/wallet_daemon/index.d.ts → packages/wallet_daemon/src/index.ts} +3 -1
  93. package/packages/wallet_daemon/src/provider.ts +263 -0
  94. package/packages/wallet_daemon/src/webrtc.ts +229 -0
  95. package/packages/wallet_daemon/src/webrtc_transport.ts +27 -0
  96. package/packages/wallet_daemon/tsconfig.json +21 -0
  97. package/packages/walletconnect/moon.yml +55 -0
  98. package/packages/walletconnect/package.json +32 -0
  99. package/packages/walletconnect/src/index.ts +274 -0
  100. package/packages/walletconnect/tsconfig.json +21 -0
  101. package/pnpm-workspace.yaml +14 -0
  102. package/tsconfig.json +42 -0
  103. package/dist/builders/helpers/index.js +0 -2
  104. package/dist/builders/helpers/submitTransaction.d.ts +0 -13
  105. package/dist/builders/helpers/submitTransaction.js +0 -62
  106. package/dist/builders/helpers/workspace.d.ts +0 -23
  107. package/dist/builders/index.d.ts +0 -3
  108. package/dist/builders/index.js +0 -3
  109. package/dist/builders/transaction/TransactionBuilder.d.ts +0 -44
  110. package/dist/builders/transaction/TransactionBuilder.js +0 -164
  111. package/dist/builders/transaction/TransactionRequest.d.ts +0 -27
  112. package/dist/builders/transaction/TransactionRequest.js +0 -67
  113. package/dist/builders/transaction/index.d.ts +0 -2
  114. package/dist/builders/transaction/index.js +0 -2
  115. package/dist/builders/types/Amount.d.ts +0 -23
  116. package/dist/builders/types/Amount.js +0 -93
  117. package/dist/builders/types/Arg.d.ts +0 -1
  118. package/dist/builders/types/Arg.js +0 -1
  119. package/dist/builders/types/Builder.d.ts +0 -47
  120. package/dist/builders/types/Builder.js +0 -1
  121. package/dist/builders/types/ComponentAddress.d.ts +0 -1
  122. package/dist/builders/types/ComponentAddress.js +0 -1
  123. package/dist/builders/types/ConfidentialClaim.d.ts +0 -8
  124. package/dist/builders/types/ConfidentialClaim.js +0 -1
  125. package/dist/builders/types/ConfidentialOutput.d.ts +0 -8
  126. package/dist/builders/types/ConfidentialOutput.js +0 -1
  127. package/dist/builders/types/ConfidentialOutputStatement.d.ts +0 -9
  128. package/dist/builders/types/ConfidentialOutputStatement.js +0 -1
  129. package/dist/builders/types/ConfidentialStatement.d.ts +0 -8
  130. package/dist/builders/types/ConfidentialStatement.js +0 -1
  131. package/dist/builders/types/ConfidentialWithdrawProof.d.ts +0 -7
  132. package/dist/builders/types/ConfidentialWithdrawProof.js +0 -1
  133. package/dist/builders/types/ElgamalVerifiableBalance.d.ts +0 -4
  134. package/dist/builders/types/ElgamalVerifiableBalance.js +0 -1
  135. package/dist/builders/types/Epoch.d.ts +0 -1
  136. package/dist/builders/types/Epoch.js +0 -1
  137. package/dist/builders/types/FinalizeResult.d.ts +0 -20
  138. package/dist/builders/types/FinalizeResult.js +0 -1
  139. package/dist/builders/types/Instruction.d.ts +0 -56
  140. package/dist/builders/types/Instruction.js +0 -1
  141. package/dist/builders/types/ResourceAddress.d.ts +0 -1
  142. package/dist/builders/types/SubstateDiff.js +0 -1
  143. package/dist/builders/types/SubstateRequirement.js +0 -1
  144. package/dist/builders/types/TemplateAddress.d.ts +0 -1
  145. package/dist/builders/types/Transaction.js +0 -1
  146. package/dist/builders/types/TransactionId.d.ts +0 -1
  147. package/dist/builders/types/TransactionId.js +0 -1
  148. package/dist/builders/types/TransactionResult.d.ts +0 -22
  149. package/dist/builders/types/TransactionResult.js +0 -10
  150. package/dist/builders/types/TransactionSignature.d.ts +0 -4
  151. package/dist/builders/types/TransactionSignature.js +0 -1
  152. package/dist/builders/types/UnsignedTransaction.d.ts +0 -12
  153. package/dist/builders/types/UnsignedTransaction.js +0 -1
  154. package/dist/builders/types/VersionedSubstateId.js +0 -1
  155. package/dist/builders/types/ViewableBalanceProof.d.ts +0 -10
  156. package/dist/builders/types/ViewableBalanceProof.js +0 -2
  157. package/dist/builders/types/Workspace.js +0 -1
  158. package/dist/builders/types/index.js +0 -2
  159. package/dist/index.d.ts +0 -13
  160. package/dist/index.js +0 -12
  161. package/dist/providers/index.d.ts +0 -16
  162. package/dist/providers/index.js +0 -1
  163. package/dist/providers/metamask/index.d.ts +0 -30
  164. package/dist/providers/metamask/index.js +0 -168
  165. package/dist/providers/metamask/utils.d.ts +0 -38
  166. package/dist/providers/metamask/utils.js +0 -55
  167. package/dist/providers/tari_universe/index.js +0 -3
  168. package/dist/providers/tari_universe/provider.d.ts +0 -23
  169. package/dist/providers/tari_universe/provider.js +0 -89
  170. package/dist/providers/tari_universe/types.js +0 -1
  171. package/dist/providers/tari_universe/utils.d.ts +0 -2
  172. package/dist/providers/tari_universe/utils.js +0 -16
  173. package/dist/providers/types.d.ts +0 -69
  174. package/dist/providers/types.js +0 -10
  175. package/dist/providers/wallet_daemon/index.js +0 -3
  176. package/dist/providers/wallet_daemon/provider.d.ts +0 -43
  177. package/dist/providers/wallet_daemon/provider.js +0 -198
  178. package/dist/providers/wallet_daemon/tari_permissions.d.ts +0 -216
  179. package/dist/providers/wallet_daemon/tari_permissions.js +0 -282
  180. package/dist/providers/wallet_daemon/webrtc.d.ts +0 -20
  181. package/dist/providers/wallet_daemon/webrtc.js +0 -201
  182. package/dist/providers/wallet_daemon/webrtc_transport.d.ts +0 -10
  183. package/dist/providers/wallet_daemon/webrtc_transport.js +0 -18
  184. package/dist/providers/walletconnect/index.d.ts +0 -23
  185. package/dist/providers/walletconnect/index.js +0 -221
  186. package/dist/templates/Account.d.ts +0 -65
  187. package/dist/templates/Account.js +0 -31
  188. package/dist/templates/Pool.d.ts +0 -30
  189. package/dist/templates/Pool.js +0 -20
  190. package/dist/templates/Tariswap.d.ts +0 -65
  191. package/dist/templates/Tariswap.js +0 -36
  192. package/dist/templates/TemplateFactory.d.ts +0 -9
  193. package/dist/templates/TemplateFactory.js +0 -18
  194. package/dist/templates/TestFaucet.d.ts +0 -49
  195. package/dist/templates/TestFaucet.js +0 -31
  196. package/dist/templates/index.js +0 -5
  197. package/dist/utils.d.ts +0 -2
  198. /package/{dist/builders/helpers/index.d.ts → packages/builders/src/helpers/index.ts} +0 -0
  199. /package/{dist/templates/index.d.ts → packages/tarijs/src/templates/index.ts} +0 -0
@@ -1,282 +0,0 @@
1
- export class Hash {
2
- value;
3
- constructor(value) {
4
- this.value = value;
5
- }
6
- toJSON() {
7
- return this.value;
8
- }
9
- }
10
- export var TAG;
11
- (function (TAG) {
12
- TAG[TAG["ComponentAddress"] = 128] = "ComponentAddress";
13
- TAG[TAG["Metadata"] = 129] = "Metadata";
14
- TAG[TAG["NonFungibleAddress"] = 130] = "NonFungibleAddress";
15
- TAG[TAG["ResourceAddress"] = 131] = "ResourceAddress";
16
- TAG[TAG["VaultId"] = 132] = "VaultId";
17
- TAG[TAG["TransactionReceipt"] = 134] = "TransactionReceipt";
18
- TAG[TAG["FeeClaim"] = 135] = "FeeClaim";
19
- })(TAG || (TAG = {}));
20
- export class Tagged {
21
- value;
22
- tag;
23
- constructor(tag, value) {
24
- this.tag = tag;
25
- this.value = value;
26
- }
27
- toJSON() {
28
- return { "@@TAGGED@@": [this.tag, this.value] };
29
- }
30
- }
31
- export class ResourceAddress {
32
- tagged;
33
- constructor(hash) {
34
- this.tagged = new Tagged(TAG.ResourceAddress, hash);
35
- }
36
- toJSON() {
37
- return this.tagged.toJSON();
38
- }
39
- }
40
- export class UnclaimedConfidentialOutputAddress {
41
- hash;
42
- constructor(hash) {
43
- this.hash = hash;
44
- }
45
- toJSON() {
46
- return this.hash.toJSON();
47
- }
48
- }
49
- export class U256 {
50
- value;
51
- constructor(value) {
52
- this.value = value;
53
- }
54
- toJSON() {
55
- return this.value;
56
- }
57
- }
58
- export class NonFungibleId {
59
- value;
60
- constructor(value) {
61
- this.value = value;
62
- }
63
- toJSON() {
64
- switch (typeof this.value) {
65
- case 'string':
66
- return { 'string': this.value };
67
- case 'number':
68
- return { 'Uint64': this.value };
69
- }
70
- return { 'U256': this.value };
71
- }
72
- }
73
- export class NonFungibleAddressContents {
74
- resource_address;
75
- id;
76
- constructor(resource_address, id) {
77
- this.resource_address = resource_address;
78
- this.id = id;
79
- }
80
- toJSON() {
81
- return { "resource_address": this.resource_address, "id": this.id };
82
- }
83
- }
84
- export class NonFungibleAddress {
85
- tagged;
86
- constructor(value) {
87
- this.tagged = new Tagged(TAG.NonFungibleAddress, value);
88
- }
89
- toJSON() {
90
- return this.tagged.toJSON();
91
- }
92
- }
93
- export class NonFungibleIndexAddress {
94
- resource_address;
95
- index;
96
- constructor(resource_address, index) {
97
- this.resource_address = resource_address;
98
- this.index = index;
99
- }
100
- toJSON() {
101
- return { "resource_address": this.resource_address, "index": this.index };
102
- }
103
- }
104
- export class ComponentAddress {
105
- tagged;
106
- constructor(hash) {
107
- this.tagged = new Tagged(TAG.ComponentAddress, hash);
108
- }
109
- toJSON() {
110
- return this.tagged.toJSON();
111
- }
112
- }
113
- export class VaultId {
114
- tagged;
115
- constructor(hash) {
116
- this.tagged = new Tagged(TAG.VaultId, hash);
117
- }
118
- toJSON() {
119
- return this.tagged.toJSON();
120
- }
121
- }
122
- export class SubstateAddress {
123
- value;
124
- constructor(value) {
125
- this.value = value;
126
- }
127
- toJSON() {
128
- if (this.value instanceof ComponentAddress) {
129
- return { "Component": this.value };
130
- }
131
- else if (this.value instanceof ResourceAddress) {
132
- return { "Resource": this.value };
133
- }
134
- else if (this.value instanceof VaultId) {
135
- return { "Vault": this.value };
136
- }
137
- else if (this.value instanceof UnclaimedConfidentialOutputAddress) {
138
- return { "UnclaimedConfidentialOutput": this.value };
139
- }
140
- else if (this.value instanceof NonFungibleAddress) {
141
- return { "NonFungible": this.value };
142
- }
143
- else if (this.value instanceof NonFungibleIndexAddress) {
144
- return { "NonFungibleIndex": this.value };
145
- }
146
- throw "Unknown type";
147
- }
148
- }
149
- export class TariPermissionAccountBalance {
150
- value;
151
- constructor(value) {
152
- this.value = value;
153
- }
154
- toJSON() {
155
- console.log("stringify", this.value);
156
- return { "AccountBalance": this.value };
157
- }
158
- }
159
- export class TariPermissionAccountInfo {
160
- constructor() { }
161
- toJSON() {
162
- return "AccountInfo";
163
- }
164
- }
165
- export class TariPermissionAccountList {
166
- value;
167
- constructor(value) {
168
- if (value === undefined) {
169
- this.value = null;
170
- }
171
- else {
172
- this.value = value;
173
- }
174
- }
175
- toJSON() {
176
- console.log('JSON TariPermissionAccountList', this.value);
177
- if (this.value === undefined) {
178
- return { "AccountList": null };
179
- }
180
- else {
181
- return { "AccountList": this.value };
182
- }
183
- }
184
- }
185
- export class TariPermissionKeyList {
186
- constructor() { }
187
- toJSON() {
188
- return "KeyList";
189
- }
190
- }
191
- export class TariPermissionTransactionGet {
192
- constructor() { }
193
- toJSON() {
194
- return "TransactionGet";
195
- }
196
- }
197
- export class TariPermissionTransactionSend {
198
- value;
199
- constructor(value) {
200
- this.value = value;
201
- }
202
- toJSON() {
203
- console.log('JSON TariPermissionTransactionSend', this.value);
204
- if (this.value === undefined) {
205
- return { "TransactionSend": null };
206
- }
207
- else {
208
- return { "TransactionSend": this.value };
209
- }
210
- }
211
- }
212
- export class TariPermissionGetNft {
213
- value0;
214
- value1;
215
- constructor(value0, value1) {
216
- this.value0 = value0;
217
- this.value1 = value1;
218
- }
219
- toJSON() {
220
- return { "GetNft": [this.value0, this.value1] };
221
- }
222
- }
223
- export class TariPermissionNftGetOwnershipProof {
224
- value;
225
- constructor(value) {
226
- this.value = value;
227
- }
228
- toJSON() {
229
- return { "NftGetOwnershipProof": this.value };
230
- }
231
- }
232
- export class TariPermissionTransactionsGet {
233
- constructor() { }
234
- toJSON() {
235
- return "TransactionGet";
236
- }
237
- }
238
- export class TariPermissionSubstatesRead {
239
- constructor() { }
240
- toJSON() {
241
- return "SubstatesRead";
242
- }
243
- }
244
- export class TariPermissionTemplatesRead {
245
- constructor() { }
246
- toJSON() {
247
- return "TemplatesRead";
248
- }
249
- }
250
- // export enum TariPermission {
251
- // AccountBalance = "AccountBalance",
252
- // AccountInfo = "AccountInfo",
253
- // AccountList = "AccountList",
254
- // KeyList = "KeyList",
255
- // TransactionGet = "TransactionGet",
256
- // TransactionSend = "TransactionSend",
257
- // GetNft = "GetNft",
258
- // NftGetOwnershipProof = "NftGetOwnershipProof",
259
- // TransactionsGet = "TransactionsGet",
260
- // SubstatesRead = "SubstatesRead",
261
- // TemplatesRead = "TemplatesRead",
262
- // }
263
- export class TariPermissions {
264
- permissions;
265
- constructor() {
266
- this.permissions = [];
267
- }
268
- addPermission(permission) {
269
- this.permissions.push(permission);
270
- return this;
271
- }
272
- addPermissions(other) {
273
- this.permissions.push(...other.permissions);
274
- return this;
275
- }
276
- toJSON() {
277
- return this.permissions;
278
- }
279
- }
280
- // TariPermissionType.prototype.toString = function () {
281
- // return "wtf"
282
- // }
@@ -1,20 +0,0 @@
1
- import { TariPermissions } from "./tari_permissions";
2
- import { transports } from "@tari-project/wallet_jrpc_client";
3
- export declare class TariConnection {
4
- private _peerConnection;
5
- private _dataChannel;
6
- private _signalingServer;
7
- private _callbacks;
8
- private _offer?;
9
- private _walletToken;
10
- onopen: (() => void) | undefined;
11
- onConnection: ((conn: TariConnection) => void) | undefined;
12
- constructor(signalig_server_url?: string, config?: RTCConfiguration);
13
- get token(): string | undefined;
14
- init(permissions: TariPermissions, onConnection: ((conn: TariConnection) => void) | undefined): Promise<void>;
15
- private setAnswer;
16
- private signalingServerPolling;
17
- isConnected(): boolean;
18
- sendMessage<T>(request: transports.RpcRequest, token: string | undefined, timeout_secs?: number | null): Promise<T>;
19
- private config;
20
- }
@@ -1,201 +0,0 @@
1
- class SignaligServer {
2
- _token;
3
- _server_url;
4
- constructor(server_url) {
5
- console.log(server_url);
6
- if (server_url !== undefined) {
7
- this._server_url = server_url;
8
- }
9
- else {
10
- this._server_url = "http://localhost:9100";
11
- }
12
- }
13
- async initToken(permissions) {
14
- this._token = await this.authLogin(permissions);
15
- }
16
- get token() {
17
- return this._token;
18
- }
19
- async jsonRpc(method, token, params) {
20
- console.log('jsonRpc', method, token, params);
21
- let id = 0;
22
- id += 1;
23
- let address = this._server_url;
24
- let headers = { 'Content-Type': 'application/json' };
25
- if (token) {
26
- headers["Authorization"] = `Bearer ${token}`;
27
- }
28
- let response = await fetch(address, {
29
- method: 'POST',
30
- body: JSON.stringify({
31
- method: method,
32
- jsonrpc: '2.0',
33
- id: id,
34
- params: params || {},
35
- }),
36
- headers: headers
37
- });
38
- let json = await response.json();
39
- if (json.error) {
40
- throw json.error;
41
- }
42
- return json.result;
43
- }
44
- async authLogin(permissions) {
45
- return await this.jsonRpc("auth.login", undefined, permissions);
46
- }
47
- async storeIceCandidate(ice_candidate) {
48
- return await this.jsonRpc("add.offer_ice_candidate", this._token, ice_candidate);
49
- }
50
- async storeOffer(offer) {
51
- return await this.jsonRpc("add.offer", this._token, offer.sdp);
52
- }
53
- async getAnswer() {
54
- return await this.jsonRpc("get.answer", this._token);
55
- }
56
- async getIceCandidates() {
57
- return await this.jsonRpc("get.answer_ice_candidates", this._token);
58
- }
59
- }
60
- export class TariConnection {
61
- _peerConnection;
62
- _dataChannel;
63
- _signalingServer;
64
- _callbacks;
65
- _offer;
66
- _walletToken;
67
- // This is public so that user can directly set the onopen callback that will be called once the data channel is open.
68
- onopen;
69
- onConnection;
70
- constructor(signalig_server_url, config) {
71
- this._peerConnection = new RTCPeerConnection(config || this.config());
72
- this._dataChannel = this._peerConnection.createDataChannel("tari-data");
73
- this._signalingServer = new SignaligServer(signalig_server_url);
74
- this._callbacks = {};
75
- }
76
- get token() {
77
- if (this._walletToken) {
78
- return this._walletToken;
79
- }
80
- return this._signalingServer.token;
81
- }
82
- async init(permissions, onConnection) {
83
- this.onConnection = onConnection;
84
- await this._signalingServer.initToken(permissions);
85
- // Setup our receiving end
86
- this._dataChannel.onmessage = (message) => {
87
- let response = JSON.parse(message.data);
88
- console.log('response', response);
89
- if (!this._callbacks[response.id]) {
90
- console.error("No callback found for id", response.id);
91
- return;
92
- }
93
- // The response should contain id, to identify the Promise.resolve, that is waiting for this result
94
- let [resolve, reject] = this._callbacks[response.id];
95
- delete this._callbacks[response.id];
96
- if (response.payload?.error) {
97
- reject(new Error(response.payload.error));
98
- }
99
- else {
100
- resolve(response.payload);
101
- }
102
- };
103
- this._dataChannel.onopen = () => {
104
- // This should be removed before the release, but it's good for debugging.
105
- console.log("Data channel is open!");
106
- this.sendMessage({ id: 0, jsonrpc: "2.0", method: "get.token", params: {} }, this._signalingServer.token)
107
- .then((walletToken) => {
108
- if (typeof walletToken !== 'string') {
109
- throw Error('Received invalid JWT from wallet daemon');
110
- }
111
- console.log("Wallet JWT received: ", walletToken);
112
- this._walletToken = walletToken;
113
- if (this.onConnection) {
114
- this.onConnection(this);
115
- }
116
- });
117
- };
118
- this._peerConnection.onicecandidate = (event) => {
119
- console.log('event', event);
120
- if (event?.candidate) {
121
- console.log("ICE ", event.candidate);
122
- console.log("ICE ", typeof event.candidate);
123
- // Store the ice candidates, so the other end can add them
124
- this._signalingServer.storeIceCandidate(event.candidate).then((resp) => {
125
- // This should be removed before the release, but it's good for debugging.
126
- console.log("Candidate stored", resp);
127
- });
128
- }
129
- };
130
- // Create offer
131
- this._offer = await this._peerConnection.createOffer();
132
- // Set the offer as our local sdp, at this point it will start getting the ice candidates
133
- this._peerConnection.setLocalDescription(this._offer);
134
- // Store the offer so the other end can set it as a remote sdp
135
- this._signalingServer.storeOffer(this._offer).then((resp) => {
136
- // This should be removed before the release, but it's good for debugging.
137
- console.log("Offer stored", resp);
138
- });
139
- await this.signalingServerPolling();
140
- }
141
- async setAnswer() {
142
- // This is called once the other end got the offer and ices and created and store an answer and its ice candidates
143
- // We get its answer sdp
144
- let sdp = await this._signalingServer.getAnswer();
145
- // And its ice candidates
146
- let iceCandidates = await this._signalingServer.getIceCandidates();
147
- // For us the answer is remote sdp
148
- let answer = new RTCSessionDescription({ sdp, type: "answer" });
149
- this._peerConnection.setRemoteDescription(answer);
150
- // We add all the ice candidates to connect, the other end is doing the same with our ice candidates
151
- for (const iceCandidate of iceCandidates) {
152
- this._peerConnection.addIceCandidate(iceCandidate);
153
- }
154
- }
155
- async signalingServerPolling() {
156
- // no need to keep retrying if we are already connected to the wallet
157
- if (this._peerConnection.connectionState === "connected") {
158
- return;
159
- }
160
- try {
161
- await this.setAnswer();
162
- }
163
- catch (error) {
164
- // we don't need to do anything on error, as the execution will be retried later
165
- console.error(error);
166
- }
167
- // try again later
168
- setTimeout(async () => {
169
- await this.signalingServerPolling();
170
- }, 2000);
171
- }
172
- isConnected() {
173
- return this._dataChannel.readyState === "open";
174
- }
175
- // If the last parameter has timeout property e.g. {timeout:1000}, it set the timeout for this call.
176
- async sendMessage(request, token, timeout_secs = null) {
177
- if (!this.isConnected) {
178
- throw new Error("WALLET_DAEMON_NOT_CONNECTED");
179
- }
180
- // This should be removed before the release, but it's good for debugging.
181
- console.log(request, 'timeout', timeout_secs);
182
- return new Promise((resolve, reject) => {
183
- // We store the resolve callback for this request,
184
- // so once the data channel receives a response we know where to return the data
185
- this._callbacks[request.id] = [resolve, reject];
186
- if (timeout_secs) {
187
- // If the user set a timeout which set it here so the promise will be rejected if not fulfilled in time.
188
- setTimeout(() => {
189
- delete this._callbacks[request.id];
190
- reject(new Error("Timeout"));
191
- }, timeout_secs * 1000);
192
- }
193
- // Make the actual call to the wallet daemon
194
- this._dataChannel.send(JSON.stringify({ token, ...request }));
195
- });
196
- }
197
- // This is our default config, use can set their own stun/turn server in the constructor.
198
- config() {
199
- return { iceServers: [{ urls: "stun:stun.l.google.com:19302" }] };
200
- }
201
- }
@@ -1,10 +0,0 @@
1
- import { transports } from "@tari-project/wallet_jrpc_client";
2
- import { TariConnection } from "./webrtc";
3
- export declare class WebRtcRpcTransport implements transports.RpcTransport {
4
- connection: TariConnection;
5
- constructor(connection: TariConnection);
6
- static new(connection: TariConnection): WebRtcRpcTransport;
7
- token(): string | undefined;
8
- isConnected(): boolean;
9
- sendRequest<T>(data: transports.RpcRequest, options: transports.RpcTransportOptions): Promise<T>;
10
- }
@@ -1,18 +0,0 @@
1
- export class WebRtcRpcTransport {
2
- connection;
3
- constructor(connection) {
4
- this.connection = connection;
5
- }
6
- static new(connection) {
7
- return new WebRtcRpcTransport(connection);
8
- }
9
- token() {
10
- return this.connection.token;
11
- }
12
- isConnected() {
13
- return this.connection.isConnected();
14
- }
15
- sendRequest(data, options) {
16
- return this.connection.sendMessage(data, options.token);
17
- }
18
- }
@@ -1,23 +0,0 @@
1
- import { TariProvider } from "../index";
2
- import { SubmitTransactionRequest, TransactionResult, SubmitTransactionResponse, VaultBalances, TemplateDefinition, Substate, Account, ListSubstatesResponse } from "../types";
3
- import UniversalProvider from '@walletconnect/universal-provider';
4
- import { SubstateType } from "@tari-project/wallet_jrpc_client";
5
- export declare class WalletConnectTariProvider implements TariProvider {
6
- providerName: string;
7
- projectId: string;
8
- wcProvider: UniversalProvider | null;
9
- wcSession: any | null;
10
- constructor(projectId: string);
11
- connect(): Promise<void>;
12
- private sendRequest;
13
- isConnected(): boolean;
14
- getAccount(): Promise<Account>;
15
- getSubstate(substate_address: string): Promise<Substate>;
16
- listSubstates(filter_by_template: string | null, filter_by_type: SubstateType | null, limit: number | null, offset: number | null): Promise<ListSubstatesResponse>;
17
- createFreeTestCoins(): Promise<Account>;
18
- submitTransaction(req: SubmitTransactionRequest): Promise<SubmitTransactionResponse>;
19
- getTransactionResult(transactionId: string): Promise<TransactionResult>;
20
- getTemplateDefinition(template_address: string): Promise<TemplateDefinition>;
21
- getPublicKey(branch: string, index: number): Promise<string>;
22
- getConfidentialVaultBalances(viewKeyId: number, vaultId: string, min: number | null, max: number | null): Promise<VaultBalances>;
23
- }