@aeternity/aepp-sdk 13.0.0 → 13.1.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 (276) hide show
  1. package/README.md +4 -2
  2. package/bin/aesophia_cli +0 -0
  3. package/dist/aepp-sdk.browser-script.js +1 -1
  4. package/dist/aepp-sdk.browser-script.js.map +1 -1
  5. package/dist/aepp-sdk.browser.js +1 -1
  6. package/dist/aepp-sdk.browser.js.map +1 -1
  7. package/dist/aepp-sdk.js +27068 -16862
  8. package/dist/aepp-sdk.js.map +1 -1
  9. package/es/AeSdkBase.d.ts +11 -3
  10. package/es/AeSdkBase.mjs +24 -3
  11. package/es/AeSdkBase.mjs.map +1 -1
  12. package/es/AeSdkMethods.d.ts +9 -14
  13. package/es/AeSdkMethods.mjs +20 -49
  14. package/es/AeSdkMethods.mjs.map +1 -1
  15. package/es/AeSdkWallet.mjs +28 -5
  16. package/es/AeSdkWallet.mjs.map +1 -1
  17. package/es/Middleware.d.ts +38 -0
  18. package/es/Middleware.mjs +90 -0
  19. package/es/Middleware.mjs.map +1 -0
  20. package/es/MiddlewareSubscriber.d.ts +30 -0
  21. package/es/MiddlewareSubscriber.mjs +198 -0
  22. package/es/MiddlewareSubscriber.mjs.map +1 -0
  23. package/es/Node.d.ts +1 -1
  24. package/es/Node.mjs.map +1 -1
  25. package/es/account/Base.d.ts +14 -2
  26. package/es/account/Base.mjs.map +1 -1
  27. package/es/account/Generalized.d.ts +1 -0
  28. package/es/account/Generalized.mjs +5 -0
  29. package/es/account/Generalized.mjs.map +1 -1
  30. package/es/account/Ledger.d.ts +1 -0
  31. package/es/account/Ledger.mjs +5 -0
  32. package/es/account/Ledger.mjs.map +1 -1
  33. package/es/account/Memory.d.ts +2 -0
  34. package/es/account/Memory.mjs +18 -0
  35. package/es/account/Memory.mjs.map +1 -1
  36. package/es/account/Rpc.d.ts +1 -6
  37. package/es/account/Rpc.mjs +24 -10
  38. package/es/account/Rpc.mjs.map +1 -1
  39. package/es/aens.d.ts +1 -1
  40. package/es/aens.mjs +1 -1
  41. package/es/aens.mjs.map +1 -1
  42. package/es/aepp-wallet-communication/rpc/RpcClient.mjs +5 -2
  43. package/es/aepp-wallet-communication/rpc/RpcClient.mjs.map +1 -1
  44. package/es/aepp-wallet-communication/rpc/types.d.ts +10 -0
  45. package/es/aepp-wallet-communication/rpc/types.mjs.map +1 -1
  46. package/es/aepp-wallet-communication/schema.d.ts +1 -8
  47. package/es/aepp-wallet-communication/schema.mjs +1 -14
  48. package/es/aepp-wallet-communication/schema.mjs.map +1 -1
  49. package/es/aepp-wallet-communication/wallet-detector.d.ts +1 -1
  50. package/es/aepp-wallet-communication/wallet-detector.mjs +0 -3
  51. package/es/aepp-wallet-communication/wallet-detector.mjs.map +1 -1
  52. package/es/apis/compiler/compiler.mjs +15 -1
  53. package/es/apis/compiler/compiler.mjs.map +1 -1
  54. package/es/apis/compiler/models/index.d.ts +1 -7
  55. package/es/apis/compiler/models/index.mjs.map +1 -1
  56. package/es/apis/compiler/models/mappers.d.ts +0 -1
  57. package/es/apis/compiler/models/mappers.mjs +0 -43
  58. package/es/apis/compiler/models/mappers.mjs.map +1 -1
  59. package/es/apis/middleware/index.d.ts +2 -0
  60. package/es/apis/middleware/index.mjs +3 -0
  61. package/es/apis/middleware/index.mjs.map +1 -0
  62. package/es/apis/middleware/middleware.d.ts +309 -0
  63. package/es/apis/middleware/middleware.mjs +1527 -0
  64. package/es/apis/middleware/middleware.mjs.map +1 -0
  65. package/es/apis/middleware/models/index.d.ts +1496 -0
  66. package/es/apis/middleware/models/index.mjs +364 -0
  67. package/es/apis/middleware/models/index.mjs.map +1 -0
  68. package/es/apis/middleware/models/mappers.d.ts +102 -0
  69. package/es/apis/middleware/models/mappers.mjs +3334 -0
  70. package/es/apis/middleware/models/mappers.mjs.map +1 -0
  71. package/es/apis/middleware/models/parameters.d.ts +43 -0
  72. package/es/apis/middleware/models/parameters.mjs +417 -0
  73. package/es/apis/middleware/models/parameters.mjs.map +1 -0
  74. package/es/chain.d.ts +2 -1
  75. package/es/chain.mjs +4 -2
  76. package/es/chain.mjs.map +1 -1
  77. package/es/channel/Base.d.ts +1 -1
  78. package/es/channel/Base.mjs +1 -4
  79. package/es/channel/Base.mjs.map +1 -1
  80. package/es/channel/internal.mjs +2 -0
  81. package/es/channel/internal.mjs.map +1 -1
  82. package/es/contract/Contract.d.ts +1 -2
  83. package/es/contract/Contract.mjs +1 -1
  84. package/es/contract/Contract.mjs.map +1 -1
  85. package/es/contract/compiler/Base.d.ts +21 -0
  86. package/es/contract/compiler/Base.mjs.map +1 -1
  87. package/es/contract/compiler/Cli.d.ts +2 -0
  88. package/es/contract/compiler/Cli.mjs +24 -3
  89. package/es/contract/compiler/Cli.mjs.map +1 -1
  90. package/es/contract/compiler/Http.d.ts +2 -0
  91. package/es/contract/compiler/Http.mjs +22 -2
  92. package/es/contract/compiler/Http.mjs.map +1 -1
  93. package/es/contract/compiler/HttpNode.d.ts +1 -0
  94. package/es/contract/compiler/HttpNode.mjs +5 -0
  95. package/es/contract/compiler/HttpNode.mjs.map +1 -1
  96. package/es/deprecated.d.ts +14 -0
  97. package/es/deprecated.mjs +22 -0
  98. package/es/deprecated.mjs.map +1 -0
  99. package/es/index-browser.d.ts +20 -14
  100. package/es/index-browser.mjs +19 -15
  101. package/es/index-browser.mjs.map +1 -1
  102. package/es/tx/builder/constants.d.ts +0 -1
  103. package/es/tx/builder/constants.mjs +0 -1
  104. package/es/tx/builder/constants.mjs.map +1 -1
  105. package/es/tx/builder/field-types/coin-amount.d.ts +1 -1
  106. package/es/tx/builder/field-types/coin-amount.mjs +2 -1
  107. package/es/tx/builder/field-types/coin-amount.mjs.map +1 -1
  108. package/es/tx/builder/field-types/deposit.d.ts +1 -1
  109. package/es/tx/builder/field-types/entry.mjs +2 -1
  110. package/es/tx/builder/field-types/entry.mjs.map +1 -1
  111. package/es/tx/builder/field-types/index.d.ts +2 -2
  112. package/es/tx/builder/helpers.d.ts +8 -1
  113. package/es/tx/builder/helpers.mjs +53 -9
  114. package/es/tx/builder/helpers.mjs.map +1 -1
  115. package/es/tx/builder/index.d.ts +4 -2
  116. package/es/tx/builder/index.mjs +0 -1
  117. package/es/tx/builder/index.mjs.map +1 -1
  118. package/es/tx/builder/schema.d.ts +9 -9
  119. package/es/tx/execution-cost.d.ts +17 -0
  120. package/es/tx/execution-cost.mjs +17 -0
  121. package/es/tx/execution-cost.mjs.map +1 -1
  122. package/es/tx/validator.mjs +6 -3
  123. package/es/tx/validator.mjs.map +1 -1
  124. package/es/utils/amount-formatter.d.ts +1 -2
  125. package/es/utils/amount-formatter.mjs +2 -2
  126. package/es/utils/amount-formatter.mjs.map +1 -1
  127. package/es/utils/autorest.mjs +6 -8
  128. package/es/utils/autorest.mjs.map +1 -1
  129. package/es/utils/crypto.d.ts +1 -0
  130. package/es/utils/crypto.mjs +12 -2
  131. package/es/utils/crypto.mjs.map +1 -1
  132. package/es/utils/errors.d.ts +2 -2
  133. package/es/utils/errors.mjs.map +1 -1
  134. package/es/utils/other.d.ts +1 -0
  135. package/es/utils/other.mjs +5 -0
  136. package/es/utils/other.mjs.map +1 -1
  137. package/es/utils/typed-data.d.ts +45 -0
  138. package/es/utils/typed-data.mjs +71 -0
  139. package/es/utils/typed-data.mjs.map +1 -0
  140. package/package.json +23 -4
  141. package/src/AeSdkBase.ts +30 -5
  142. package/src/AeSdkMethods.ts +31 -50
  143. package/src/AeSdkWallet.ts +27 -5
  144. package/src/Middleware.ts +141 -0
  145. package/src/MiddlewareSubscriber.ts +207 -0
  146. package/src/Node.ts +1 -1
  147. package/src/account/Base.ts +20 -3
  148. package/src/account/Generalized.ts +5 -0
  149. package/src/account/Ledger.ts +5 -0
  150. package/src/account/Memory.ts +15 -0
  151. package/src/account/Rpc.ts +19 -7
  152. package/src/aens.ts +1 -1
  153. package/src/aepp-wallet-communication/rpc/RpcClient.ts +11 -3
  154. package/src/aepp-wallet-communication/rpc/types.ts +11 -0
  155. package/src/aepp-wallet-communication/schema.ts +2 -17
  156. package/src/aepp-wallet-communication/wallet-detector.ts +1 -2
  157. package/src/apis/compiler/compiler.ts +8 -1
  158. package/src/apis/compiler/models/index.ts +1 -8
  159. package/src/apis/compiler/models/mappers.ts +0 -36
  160. package/src/apis/middleware/index.ts +2 -0
  161. package/src/apis/middleware/middleware.ts +1807 -0
  162. package/src/apis/middleware/models/index.ts +1747 -0
  163. package/src/apis/middleware/models/mappers.ts +3370 -0
  164. package/src/apis/middleware/models/parameters.ts +463 -0
  165. package/src/chain.ts +3 -2
  166. package/src/channel/Base.ts +5 -5
  167. package/src/channel/internal.ts +2 -0
  168. package/src/contract/Contract.ts +2 -1
  169. package/src/contract/compiler/Base.ts +26 -0
  170. package/src/contract/compiler/Cli.ts +27 -3
  171. package/src/contract/compiler/Http.ts +21 -2
  172. package/src/contract/compiler/HttpNode.ts +6 -0
  173. package/src/deprecated.ts +23 -0
  174. package/src/index-browser.ts +77 -15
  175. package/src/tx/builder/constants.ts +0 -1
  176. package/src/tx/builder/field-types/coin-amount.ts +2 -1
  177. package/src/tx/builder/field-types/entry.ts +1 -1
  178. package/src/tx/builder/helpers.ts +54 -9
  179. package/src/tx/builder/index.ts +6 -9
  180. package/src/tx/execution-cost.ts +17 -0
  181. package/src/tx/validator.ts +4 -2
  182. package/src/utils/amount-formatter.ts +1 -1
  183. package/src/utils/autorest.ts +5 -10
  184. package/src/utils/crypto.ts +12 -2
  185. package/src/utils/errors.ts +2 -2
  186. package/src/utils/other.ts +5 -0
  187. package/src/utils/typed-data.ts +95 -0
  188. package/types-legacy/ts4.2/AeSdkBase.d.ts +11 -3
  189. package/types-legacy/ts4.2/AeSdkMethods.d.ts +9 -14
  190. package/types-legacy/ts4.2/Middleware.d.ts +46 -0
  191. package/types-legacy/ts4.2/MiddlewareSubscriber.d.ts +30 -0
  192. package/types-legacy/ts4.2/Node.d.ts +1 -1
  193. package/types-legacy/ts4.2/account/Base.d.ts +14 -2
  194. package/types-legacy/ts4.2/account/Generalized.d.ts +1 -0
  195. package/types-legacy/ts4.2/account/Ledger.d.ts +1 -0
  196. package/types-legacy/ts4.2/account/Memory.d.ts +2 -0
  197. package/types-legacy/ts4.2/account/Rpc.d.ts +1 -6
  198. package/types-legacy/ts4.2/aens.d.ts +1 -1
  199. package/types-legacy/ts4.2/aepp-wallet-communication/rpc/types.d.ts +10 -0
  200. package/types-legacy/ts4.2/aepp-wallet-communication/schema.d.ts +1 -8
  201. package/types-legacy/ts4.2/aepp-wallet-communication/wallet-detector.d.ts +1 -1
  202. package/types-legacy/ts4.2/apis/compiler/models/index.d.ts +1 -7
  203. package/types-legacy/ts4.2/apis/compiler/models/mappers.d.ts +0 -1
  204. package/types-legacy/ts4.2/apis/middleware/index.d.ts +2 -0
  205. package/types-legacy/ts4.2/apis/middleware/middleware.d.ts +309 -0
  206. package/types-legacy/ts4.2/apis/middleware/models/index.d.ts +1496 -0
  207. package/types-legacy/ts4.2/apis/middleware/models/mappers.d.ts +102 -0
  208. package/types-legacy/ts4.2/apis/middleware/models/parameters.d.ts +43 -0
  209. package/types-legacy/ts4.2/chain.d.ts +2 -1
  210. package/types-legacy/ts4.2/channel/Base.d.ts +1 -1
  211. package/types-legacy/ts4.2/contract/Contract.d.ts +1 -2
  212. package/types-legacy/ts4.2/contract/compiler/Base.d.ts +21 -0
  213. package/types-legacy/ts4.2/contract/compiler/Cli.d.ts +2 -0
  214. package/types-legacy/ts4.2/contract/compiler/Http.d.ts +2 -0
  215. package/types-legacy/ts4.2/contract/compiler/HttpNode.d.ts +1 -0
  216. package/types-legacy/ts4.2/deprecated.d.ts +14 -0
  217. package/types-legacy/ts4.2/index-browser.d.ts +20 -14
  218. package/types-legacy/ts4.2/tx/builder/constants.d.ts +0 -1
  219. package/types-legacy/ts4.2/tx/builder/field-types/coin-amount.d.ts +1 -1
  220. package/types-legacy/ts4.2/tx/builder/field-types/deposit.d.ts +1 -1
  221. package/types-legacy/ts4.2/tx/builder/field-types/index.d.ts +2 -2
  222. package/types-legacy/ts4.2/tx/builder/helpers.d.ts +8 -1
  223. package/types-legacy/ts4.2/tx/builder/index.d.ts +4 -2
  224. package/types-legacy/ts4.2/tx/builder/schema.d.ts +9 -9
  225. package/types-legacy/ts4.2/tx/execution-cost.d.ts +17 -0
  226. package/types-legacy/ts4.2/utils/amount-formatter.d.ts +1 -2
  227. package/types-legacy/ts4.2/utils/crypto.d.ts +1 -0
  228. package/types-legacy/ts4.2/utils/errors.d.ts +2 -2
  229. package/types-legacy/ts4.2/utils/other.d.ts +1 -0
  230. package/types-legacy/ts4.2/utils/typed-data.d.ts +60 -0
  231. package/types-legacy/ts4.5/AeSdkBase.d.ts +11 -3
  232. package/types-legacy/ts4.5/AeSdkMethods.d.ts +9 -14
  233. package/types-legacy/ts4.5/Middleware.d.ts +46 -0
  234. package/types-legacy/ts4.5/MiddlewareSubscriber.d.ts +30 -0
  235. package/types-legacy/ts4.5/Node.d.ts +1 -1
  236. package/types-legacy/ts4.5/account/Base.d.ts +14 -2
  237. package/types-legacy/ts4.5/account/Generalized.d.ts +1 -0
  238. package/types-legacy/ts4.5/account/Ledger.d.ts +1 -0
  239. package/types-legacy/ts4.5/account/Memory.d.ts +2 -0
  240. package/types-legacy/ts4.5/account/Rpc.d.ts +1 -6
  241. package/types-legacy/ts4.5/aens.d.ts +1 -1
  242. package/types-legacy/ts4.5/aepp-wallet-communication/rpc/types.d.ts +10 -0
  243. package/types-legacy/ts4.5/aepp-wallet-communication/schema.d.ts +1 -8
  244. package/types-legacy/ts4.5/aepp-wallet-communication/wallet-detector.d.ts +1 -1
  245. package/types-legacy/ts4.5/apis/compiler/models/index.d.ts +1 -7
  246. package/types-legacy/ts4.5/apis/compiler/models/mappers.d.ts +0 -1
  247. package/types-legacy/ts4.5/apis/middleware/index.d.ts +2 -0
  248. package/types-legacy/ts4.5/apis/middleware/middleware.d.ts +309 -0
  249. package/types-legacy/ts4.5/apis/middleware/models/index.d.ts +1496 -0
  250. package/types-legacy/ts4.5/apis/middleware/models/mappers.d.ts +102 -0
  251. package/types-legacy/ts4.5/apis/middleware/models/parameters.d.ts +43 -0
  252. package/types-legacy/ts4.5/chain.d.ts +2 -1
  253. package/types-legacy/ts4.5/channel/Base.d.ts +1 -1
  254. package/types-legacy/ts4.5/contract/Contract.d.ts +1 -2
  255. package/types-legacy/ts4.5/contract/compiler/Base.d.ts +21 -0
  256. package/types-legacy/ts4.5/contract/compiler/Cli.d.ts +2 -0
  257. package/types-legacy/ts4.5/contract/compiler/Http.d.ts +2 -0
  258. package/types-legacy/ts4.5/contract/compiler/HttpNode.d.ts +1 -0
  259. package/types-legacy/ts4.5/deprecated.d.ts +14 -0
  260. package/types-legacy/ts4.5/index-browser.d.ts +20 -14
  261. package/types-legacy/ts4.5/tx/builder/constants.d.ts +0 -1
  262. package/types-legacy/ts4.5/tx/builder/field-types/coin-amount.d.ts +1 -1
  263. package/types-legacy/ts4.5/tx/builder/field-types/deposit.d.ts +1 -1
  264. package/types-legacy/ts4.5/tx/builder/field-types/index.d.ts +2 -2
  265. package/types-legacy/ts4.5/tx/builder/helpers.d.ts +8 -1
  266. package/types-legacy/ts4.5/tx/builder/index.d.ts +4 -2
  267. package/types-legacy/ts4.5/tx/builder/schema.d.ts +9 -9
  268. package/types-legacy/ts4.5/tx/execution-cost.d.ts +17 -0
  269. package/types-legacy/ts4.5/utils/amount-formatter.d.ts +1 -2
  270. package/types-legacy/ts4.5/utils/crypto.d.ts +1 -0
  271. package/types-legacy/ts4.5/utils/errors.d.ts +2 -2
  272. package/types-legacy/ts4.5/utils/other.d.ts +1 -0
  273. package/types-legacy/ts4.5/utils/typed-data.d.ts +60 -0
  274. package/es/typings/@aeternity__aepp-calldata/index.d.mjs +0 -2
  275. package/es/typings/@aeternity__aepp-calldata/index.d.mjs.map +0 -1
  276. package/src/typings/@aeternity__aepp-calldata/index.d.ts +0 -20
@@ -0,0 +1,207 @@
1
+ /* eslint-disable max-classes-per-file */
2
+ import WebSocket from 'isomorphic-ws';
3
+ import { BaseError, UnexpectedTsError, InternalError } from './utils/errors';
4
+ import { Encoded } from './utils/encoder';
5
+
6
+ interface Message {
7
+ payload: Object;
8
+ source: Source.Middleware | Source.Node;
9
+ subscription: 'KeyBlocks' | 'MicroBlocks' | 'Transactions' | 'Object';
10
+ target?: string;
11
+ }
12
+
13
+ enum Source {
14
+ Middleware = 'mdw',
15
+ Node = 'node',
16
+ All = 'all',
17
+ }
18
+
19
+ export class MiddlewareSubscriberError extends BaseError {
20
+ constructor(message: string) {
21
+ super(message);
22
+ this.name = 'MiddlewareSubscriberError';
23
+ }
24
+ }
25
+
26
+ export class MiddlewareSubscriberDisconnected extends MiddlewareSubscriberError {
27
+ constructor(readonly closeEvent: WebSocket.CloseEvent) {
28
+ super('Connection closed');
29
+ this.name = 'MiddlewareSubscriberDisconnected';
30
+ }
31
+ }
32
+
33
+ export default class MiddlewareSubscriber {
34
+ #subscriptions: Array<
35
+ readonly [target: string, s: Source, cb: (p?: Object, e?: Error) => void]
36
+ > = [];
37
+
38
+ #requestQueue: Array<[isSubscribe: boolean, target: string]> = [];
39
+
40
+ #webSocket?: WebSocket;
41
+
42
+ get webSocket(): WebSocket | undefined {
43
+ return this.#webSocket;
44
+ }
45
+
46
+ get #targets(): Set<string> {
47
+ return new Set(this.#subscriptions.map(([target]) => target));
48
+ }
49
+
50
+ #sendMessage(message: any): void {
51
+ if (this.#webSocket == null) throw new UnexpectedTsError();
52
+ this.#webSocket.send(JSON.stringify(message));
53
+ }
54
+
55
+ #sendSubscribe(isSubscribe: boolean, target: string): void {
56
+ if (this.#webSocket == null) return;
57
+ const payload = ['KeyBlocks', 'MicroBlocks', 'Transactions'].includes(target)
58
+ ? target : 'Object';
59
+ this.#sendMessage({
60
+ op: isSubscribe ? 'Subscribe' : 'Unsubscribe',
61
+ payload,
62
+ ...payload === 'Object' && { target },
63
+ });
64
+ this.#requestQueue.push([isSubscribe, target]);
65
+ }
66
+
67
+ #emit(condition: (target: string, source: Source) => boolean, p?: Object, e?: Error): void {
68
+ this.#subscriptions
69
+ .filter(([target, source]) => condition(target, source))
70
+ .forEach(([, , cb]) => cb(p, e));
71
+ }
72
+
73
+ constructor(readonly url: string) {
74
+ }
75
+
76
+ #disconnect(onlyReset = false): void {
77
+ if (this.#webSocket == null) return;
78
+ if (!onlyReset) this.#webSocket.close();
79
+ Object.assign(this.#webSocket, {
80
+ onopen: undefined,
81
+ onerror: undefined,
82
+ onmessage: undefined,
83
+ });
84
+ this.#webSocket = undefined;
85
+ }
86
+
87
+ async reconnect(): Promise<void> {
88
+ this.#disconnect();
89
+ this.#webSocket = await new Promise((resolve) => {
90
+ const webSocket = new WebSocket(this.url);
91
+ Object.assign(webSocket, {
92
+ onopen: () => resolve(webSocket),
93
+ onerror: (errorEvent: WebSocket.ErrorEvent) => {
94
+ this.#emit(() => true, undefined, errorEvent.error);
95
+ },
96
+ onmessage: (event: WebSocket.MessageEvent) => {
97
+ if (typeof event.data !== 'string') {
98
+ throw new InternalError(`Unknown incoming message type: ${typeof event.data}`);
99
+ }
100
+ this.#messageHandler(JSON.parse(event.data));
101
+ },
102
+ onclose: (event: WebSocket.CloseEvent) => {
103
+ this.#emit(() => true, undefined, new MiddlewareSubscriberDisconnected(event));
104
+ this.#disconnect(true);
105
+ },
106
+ });
107
+ });
108
+ await Promise.all([...this.#targets].map((target) => this.#sendSubscribe(true, target)));
109
+ }
110
+
111
+ #messageHandler(message: string | string[] | Message): void {
112
+ if (typeof message === 'string' || Array.isArray(message)) {
113
+ const request = this.#requestQueue.shift();
114
+ if (request == null) throw new InternalError('Request queue is empty');
115
+ const [isSubscribe, target] = request;
116
+ let error;
117
+ if (typeof message === 'string') error = new MiddlewareSubscriberError(message);
118
+ if (message.includes(target) !== isSubscribe) {
119
+ error = new InternalError(`Expected ${target} to be${isSubscribe ? '' : ' not'} included into ${message}`);
120
+ }
121
+ if (error != null) this.#emit((t) => target === t, undefined, error);
122
+ return;
123
+ }
124
+ this.#emit(
125
+ (target, source) => (target === message.subscription || target === message.target)
126
+ && (source === message.source || source === Source.All),
127
+ message.payload,
128
+ );
129
+ }
130
+
131
+ #subscribe(target: string, source: Source, cb: (p?: Object, e?: Error) => void): () => void {
132
+ const subscription = [target, source, cb] as const;
133
+ if (this.#targets.size === 0) this.reconnect();
134
+ if (!this.#targets.has(target)) this.#sendSubscribe(true, target);
135
+ this.#subscriptions.push(subscription);
136
+ return () => {
137
+ this.#subscriptions = this.#subscriptions.filter((item) => item !== subscription);
138
+ if (!this.#targets.has(target)) this.#sendSubscribe(false, target);
139
+ if (this.#targets.size === 0) this.#disconnect();
140
+ };
141
+ }
142
+
143
+ // TODO: replace p?: any with a proper type definition
144
+
145
+ subscribeKeyBlocks(cb: (p?: any, e?: Error) => void): () => void {
146
+ return this.#subscribe('KeyBlocks', Source.Middleware, cb);
147
+ }
148
+
149
+ subscribeKeyBlocksNode(cb: (p?: any, e?: Error) => void): () => void {
150
+ return this.#subscribe('KeyBlocks', Source.Node, cb);
151
+ }
152
+
153
+ subscribeKeyBlocksAll(cb: (p?: any, e?: Error) => void): () => void {
154
+ return this.#subscribe('KeyBlocks', Source.All, cb);
155
+ }
156
+
157
+ subscribeMicroBlocks(cb: (p?: any, e?: Error) => void): () => void {
158
+ return this.#subscribe('MicroBlocks', Source.Middleware, cb);
159
+ }
160
+
161
+ subscribeMicroBlocksNode(cb: (p?: any, e?: Error) => void): () => void {
162
+ return this.#subscribe('MicroBlocks', Source.Node, cb);
163
+ }
164
+
165
+ subscribeMicroBlocksAll(cb: (p?: any, e?: Error) => void): () => void {
166
+ return this.#subscribe('MicroBlocks', Source.All, cb);
167
+ }
168
+
169
+ subscribeTransactions(cb: (p?: any, e?: Error) => void): () => void {
170
+ return this.#subscribe('Transactions', Source.Middleware, cb);
171
+ }
172
+
173
+ subscribeTransactionsNode(cb: (p?: any, e?: Error) => void): () => void {
174
+ return this.#subscribe('Transactions', Source.Node, cb);
175
+ }
176
+
177
+ subscribeTransactionsAll(cb: (p?: any, e?: Error) => void): () => void {
178
+ return this.#subscribe('Transactions', Source.All, cb);
179
+ }
180
+
181
+ subscribeObject(
182
+ target: Encoded.KeyBlockHash | Encoded.Channel | Encoded.ContractAddress
183
+ | Encoded.OracleAddress | Encoded.OracleQueryId | Encoded.AccountAddress
184
+ | Encoded.Name | `${string}.chain`,
185
+ cb: (p?: any, e?: Error) => void,
186
+ ): () => void {
187
+ return this.#subscribe(target, Source.Middleware, cb);
188
+ }
189
+
190
+ subscribeObjectNode(
191
+ target: Encoded.KeyBlockHash | Encoded.Channel | Encoded.ContractAddress
192
+ | Encoded.OracleAddress | Encoded.OracleQueryId | Encoded.AccountAddress
193
+ | Encoded.Name | `${string}.chain`,
194
+ cb: (p?: any, e?: Error) => void,
195
+ ): () => void {
196
+ return this.#subscribe(target, Source.Node, cb);
197
+ }
198
+
199
+ subscribeObjectAll(
200
+ target: Encoded.KeyBlockHash | Encoded.Channel | Encoded.ContractAddress
201
+ | Encoded.OracleAddress | Encoded.OracleQueryId | Encoded.AccountAddress
202
+ | Encoded.Name | `${string}.chain`,
203
+ cb: (p?: any, e?: Error) => void,
204
+ ): () => void {
205
+ return this.#subscribe(target, Source.All, cb);
206
+ }
207
+ }
package/src/Node.ts CHANGED
@@ -103,7 +103,7 @@ type NodeTransformedApi = new (...args: ConstructorParameters<typeof NodeApi>) =
103
103
  ? NodeApi[Name] : TransformNodeType<NodeApi[Name]>
104
104
  };
105
105
 
106
- export interface NodeInfo {
106
+ interface NodeInfo {
107
107
  url: string;
108
108
  nodeNetworkId: string;
109
109
  version: string;
@@ -2,6 +2,7 @@ import { Encoded } from '../utils/encoder';
2
2
  import Node from '../Node';
3
3
  import CompilerBase from '../contract/compiler/Base';
4
4
  import { Int } from '../tx/builder/constants';
5
+ import { AciValue, Domain } from '../utils/typed-data';
5
6
 
6
7
  interface AuthData {
7
8
  fee?: Int;
@@ -42,7 +43,7 @@ export default abstract class AccountBase {
42
43
  * Sign message
43
44
  * @param message - Message to sign
44
45
  * @param options - Options
45
- * @returns Signature as hex string of Uint8Array
46
+ * @returns Signature
46
47
  */
47
48
  abstract signMessage(
48
49
  message: string,
@@ -52,16 +53,32 @@ export default abstract class AccountBase {
52
53
  },
53
54
  ): Promise<Uint8Array>;
54
55
 
56
+ /**
57
+ * Sign typed data
58
+ * @param type - Type of data to sign
59
+ * @param data - Encoded data to sign
60
+ * @param options - Options
61
+ * @returns Signature
62
+ */
63
+ abstract signTypedData(
64
+ data: Encoded.ContractBytearray,
65
+ aci: AciValue,
66
+ options?: Domain & {
67
+ aeppOrigin?: string;
68
+ aeppRpcClientId?: string;
69
+ },
70
+ ): Promise<Encoded.Signature>;
71
+
55
72
  /**
56
73
  * Sign data blob
57
74
  * @param data - Data blob to sign
58
75
  * @param options - Options
59
- * @returns Signed data blob
76
+ * @returns Signature
60
77
  */
61
78
  abstract sign(data: string | Uint8Array, options?: any): Promise<Uint8Array>;
62
79
 
63
80
  /**
64
81
  * Account address
65
82
  */
66
- readonly address: Encoded.AccountAddress;
83
+ readonly address!: Encoded.AccountAddress;
67
84
  }
@@ -38,6 +38,11 @@ export default class AccountGeneralized extends AccountBase {
38
38
  throw new NotImplementedError('Can\'t sign using generalized account');
39
39
  }
40
40
 
41
+ // eslint-disable-next-line class-methods-use-this
42
+ override async signTypedData(): Promise<Encoded.Signature> {
43
+ throw new NotImplementedError('Can\'t sign using generalized account');
44
+ }
45
+
41
46
  override async signTransaction(
42
47
  tx: Encoded.Transaction,
43
48
  { authData, onCompiler, onNode }: Parameters<AccountBase['signTransaction']>[1],
@@ -40,6 +40,11 @@ export default class AccountLedger extends AccountBase {
40
40
  throw new NotImplementedError('RAW signing using Ledger HW');
41
41
  }
42
42
 
43
+ // eslint-disable-next-line class-methods-use-this
44
+ override async signTypedData(): Promise<Encoded.Signature> {
45
+ throw new NotImplementedError('Typed data signing using Ledger HW');
46
+ }
47
+
43
48
  override async signTransaction(
44
49
  tx: Encoded.Transaction,
45
50
  { innerTx, networkId }: { innerTx?: boolean; networkId?: string } = {},
@@ -7,6 +7,7 @@ import {
7
7
  decode, encode, Encoded, Encoding,
8
8
  } from '../utils/encoder';
9
9
  import { concatBuffers } from '../utils/other';
10
+ import { hashTypedData, AciValue } from '../utils/typed-data';
10
11
  import { buildTx } from '../tx/builder';
11
12
  import { Tag } from '../tx/builder/constants';
12
13
 
@@ -74,4 +75,18 @@ export default class AccountMemory extends AccountBase {
74
75
  override async signMessage(message: string, options?: any): Promise<Uint8Array> {
75
76
  return this.sign(messageToHash(message), options);
76
77
  }
78
+
79
+ override async signTypedData(
80
+ data: Encoded.ContractBytearray,
81
+ aci: AciValue,
82
+ {
83
+ name, version, networkId, contractAddress, ...options
84
+ }: Parameters<AccountBase['signTypedData']>[2] = {},
85
+ ): Promise<Encoded.Signature> {
86
+ const dHash = hashTypedData(data, aci, {
87
+ name, version, networkId, contractAddress,
88
+ });
89
+ const signature = await this.sign(dHash, options);
90
+ return encode(signature, Encoding.Signature);
91
+ }
77
92
  }
@@ -28,20 +28,17 @@ export default class AccountRpc extends AccountBase {
28
28
  throw new NotImplementedError('RAW signing using wallet');
29
29
  }
30
30
 
31
- /**
32
- * @returns Signed transaction
33
- */
34
31
  override async signTransaction(
35
32
  tx: Encoded.Transaction,
36
33
  { innerTx, networkId }: Parameters<AccountBase['signTransaction']>[1] = {},
37
34
  ): Promise<Encoded.Transaction> {
38
- if (innerTx != null) throw new NotImplementedError('innerTx option in AccountRpc');
39
35
  if (networkId == null) throw new ArgumentError('networkId', 'provided', networkId);
40
36
  const res = await this._rpcClient.request(METHODS.sign, {
41
37
  onAccount: this.address,
42
38
  tx,
43
39
  returnSigned: true,
44
40
  networkId,
41
+ innerTx,
45
42
  });
46
43
  if (res.signedTransaction == null) {
47
44
  throw new UnsupportedProtocolError('signedTransaction is missed in wallet response');
@@ -49,12 +46,27 @@ export default class AccountRpc extends AccountBase {
49
46
  return res.signedTransaction;
50
47
  }
51
48
 
52
- /**
53
- * @returns Signed message
54
- */
55
49
  override async signMessage(message: string): Promise<Uint8Array> {
56
50
  const { signature } = await this._rpcClient
57
51
  .request(METHODS.signMessage, { onAccount: this.address, message });
58
52
  return Buffer.from(signature, 'hex');
59
53
  }
54
+
55
+ override async signTypedData(
56
+ data: Encoded.ContractBytearray,
57
+ aci: Parameters<AccountBase['signTypedData']>[1],
58
+ {
59
+ name, version, contractAddress, networkId,
60
+ }: Parameters<AccountBase['signTypedData']>[2] = {},
61
+ ): Promise<Encoded.Signature> {
62
+ const { signature } = await this._rpcClient.request(METHODS.signTypedData, {
63
+ onAccount: this.address,
64
+ domain: {
65
+ name, version, networkId, contractAddress,
66
+ },
67
+ aci,
68
+ data,
69
+ });
70
+ return signature;
71
+ }
60
72
  }
package/src/aens.ts CHANGED
@@ -379,7 +379,7 @@ interface AensPreclaimOptions extends
379
379
  * @example
380
380
  * ```js
381
381
  * const name = 'test.chain'
382
- * const bidFee = computeBidFee(name, startFee, incrementPercentage)
382
+ * const bidFee = computeBidFee(name, { startFee, increment: 0.42 })
383
383
  *
384
384
  * await sdkInstance.aensBid(name, 213109412839123, { ttl, fee, nonce })
385
385
  * ```
@@ -1,6 +1,7 @@
1
1
  import { RpcError, RpcInternalError, RpcMethodNotFoundError } from '../schema';
2
2
  import BrowserConnection from '../connection/Browser';
3
3
  import { InvalidRpcMessageError, MissingCallbackError } from '../../utils/errors';
4
+ import { ensureError } from '../../utils/other';
4
5
 
5
6
  interface JsonRpcRequest {
6
7
  jsonrpc: '2.0';
@@ -64,17 +65,24 @@ export default class RpcClient <
64
65
 
65
66
  const request = msg as JsonRpcRequest;
66
67
  let result;
67
- let error;
68
+ let error: Error | undefined;
68
69
  try {
69
70
  if (!(request.method in this.#methods)) throw new RpcMethodNotFoundError();
70
71
  const methodName = request.method as keyof LocalApi;
71
72
  result = await this.#methods[methodName](request.params, origin);
72
73
  } catch (e) {
73
- error = e instanceof RpcError ? e : new RpcInternalError();
74
+ ensureError(e);
75
+ error = e;
74
76
  }
75
77
  if (request.id != null) {
76
- this.#sendResponse(request.id, request.method as keyof LocalApi, result, error);
78
+ this.#sendResponse(
79
+ request.id,
80
+ request.method as keyof LocalApi,
81
+ result,
82
+ error == null || error instanceof RpcError ? error : new RpcInternalError(),
83
+ );
77
84
  }
85
+ if (error != null && !(error instanceof RpcError)) throw error;
78
86
  }
79
87
 
80
88
  #sendRequest(
@@ -1,4 +1,5 @@
1
1
  import { Encoded } from '../../utils/encoder';
2
+ import { Domain, AciValue } from '../../utils/typed-data';
2
3
  import { METHODS, SUBSCRIPTION_TYPES, WALLET_TYPE } from '../schema';
3
4
  import { TransformNodeType } from '../../Node';
4
5
  import { SignedTx } from '../../apis/node';
@@ -55,6 +56,7 @@ export interface WalletApi {
55
56
  * @see {@link https://github.com/aeternity/aepp-sdk-js/commit/153fd89a52c4eab39fcd659b356b36d32129c1ba}
56
57
  */
57
58
  networkId: string;
59
+ innerTx?: boolean;
58
60
  }
59
61
  ) => Promise<{
60
62
  /**
@@ -71,6 +73,15 @@ export interface WalletApi {
71
73
  [METHODS.signMessage]: (
72
74
  p: { message: string; onAccount: Encoded.AccountAddress }
73
75
  ) => Promise<{ signature: string }>;
76
+
77
+ [METHODS.signTypedData]: (
78
+ p: {
79
+ domain: Domain;
80
+ aci: AciValue;
81
+ data: Encoded.ContractBytearray;
82
+ onAccount: Encoded.AccountAddress;
83
+ },
84
+ ) => Promise<{ signature: Encoded.Signature }>;
74
85
  }
75
86
 
76
87
  export interface AeppApi {
@@ -36,6 +36,7 @@ export const enum METHODS {
36
36
  connect = 'connection.open',
37
37
  sign = 'transaction.sign',
38
38
  signMessage = 'message.sign',
39
+ signTypedData = 'typedData.sign',
39
40
  subscribeAddress = 'address.subscribe',
40
41
  updateNetwork = 'networkId.update',
41
42
  closeConnection = 'connection.close',
@@ -64,7 +65,7 @@ const rpcErrors: Array<(new (data?: any) => RpcError) & { code: number }> = [];
64
65
  export abstract class RpcError extends BaseError {
65
66
  static code: number;
66
67
 
67
- code: number;
68
+ code!: number;
68
69
 
69
70
  data?: any;
70
71
 
@@ -99,22 +100,6 @@ export class RpcInvalidTransactionError extends RpcError {
99
100
  }
100
101
  rpcErrors.push(RpcInvalidTransactionError);
101
102
 
102
- /**
103
- * @category exception
104
- */
105
- export class RpcBroadcastError extends RpcError {
106
- static override code = 3;
107
-
108
- override code = 3;
109
-
110
- constructor(data?: any) {
111
- super('Broadcast failed');
112
- this.data = data;
113
- this.name = 'RpcBroadcastError';
114
- }
115
- }
116
- rpcErrors.push(RpcBroadcastError);
117
-
118
103
  /**
119
104
  * @category exception
120
105
  */
@@ -22,7 +22,7 @@ interface Wallets { [key: string]: Wallet }
22
22
  */
23
23
  export default (
24
24
  connection: BrowserConnection,
25
- onDetected: ({ wallets, newWallet }: { wallets: Wallets; newWallet?: Wallet }) => void,
25
+ onDetected: ({ wallets, newWallet }: { wallets: Wallets; newWallet: Wallet }) => void,
26
26
  ): () => void => {
27
27
  if (window == null) throw new UnsupportedPlatformError('Window object not found, you can run wallet detector only in browser');
28
28
  const wallets: Wallets = {};
@@ -49,7 +49,6 @@ export default (
49
49
  wallets[wallet.info.id] = wallet;
50
50
  onDetected({ wallets, newWallet: wallet });
51
51
  }, () => {});
52
- if (Object.keys(wallets).length > 0) onDetected({ wallets });
53
52
 
54
53
  return () => connection.disconnect();
55
54
  };
@@ -260,7 +260,14 @@ const generateACIOperationSpec: coreClient.OperationSpec = {
260
260
  httpMethod: "POST",
261
261
  responses: {
262
262
  200: {
263
- bodyMapper: Mappers.Aci
263
+ bodyMapper: {
264
+ type: {
265
+ name: "Sequence",
266
+ element: {
267
+ type: { name: "Dictionary", value: { type: { name: "any" } } }
268
+ }
269
+ }
270
+ }
264
271
  },
265
272
  400: {
266
273
  bodyMapper: {
@@ -12,13 +12,6 @@ export interface CompileOpts {
12
12
  srcFile?: string;
13
13
  }
14
14
 
15
- export interface Aci {
16
- /** Any object */
17
- encodedAci: Record<string, unknown>;
18
- externalEncodedAci?: Record<string, unknown>[];
19
- interface: string;
20
- }
21
-
22
15
  export interface CompilerError {
23
16
  type: string;
24
17
  pos: ErrorPos;
@@ -151,7 +144,7 @@ export interface GenerateACIOptionalParams
151
144
  extends coreClient.OperationOptions {}
152
145
 
153
146
  /** Contains response data for the generateACI operation. */
154
- export type GenerateACIResponse = Aci;
147
+ export type GenerateACIResponse = Record<string, unknown>[];
155
148
 
156
149
  /** Optional parameters. */
157
150
  export interface CompileContractOptionalParams
@@ -45,42 +45,6 @@ export const CompileOpts: coreClient.CompositeMapper = {
45
45
  }
46
46
  };
47
47
 
48
- export const Aci: coreClient.CompositeMapper = {
49
- type: {
50
- name: "Composite",
51
- className: "Aci",
52
- modelProperties: {
53
- encodedAci: {
54
- serializedName: "encoded_aci",
55
- required: true,
56
- type: {
57
- name: "Dictionary",
58
- value: { type: { name: "any" } }
59
- }
60
- },
61
- externalEncodedAci: {
62
- serializedName: "external_encoded_aci",
63
- type: {
64
- name: "Sequence",
65
- element: {
66
- type: {
67
- name: "Dictionary",
68
- value: { type: { name: "any" } }
69
- }
70
- }
71
- }
72
- },
73
- interface: {
74
- serializedName: "interface",
75
- required: true,
76
- type: {
77
- name: "String"
78
- }
79
- }
80
- }
81
- }
82
- };
83
-
84
48
  export const CompilerError: coreClient.CompositeMapper = {
85
49
  type: {
86
50
  name: "Composite",
@@ -0,0 +1,2 @@
1
+ export * from "./models";
2
+ export { Middleware } from "./middleware";