@subwallet/extension-base 0.3.6-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 (166) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +10 -0
  3. package/build/LICENSE +201 -0
  4. package/build/README.md +10 -0
  5. package/build/background/KoniTypes.d.ts +462 -0
  6. package/build/background/KoniTypes.js +57 -0
  7. package/build/background/RequestBytesSign.d.ts +12 -0
  8. package/build/background/RequestBytesSign.js +16 -0
  9. package/build/background/RequestExtrinsicSign.d.ts +12 -0
  10. package/build/background/RequestExtrinsicSign.js +14 -0
  11. package/build/background/handlers/Extension.d.ts +49 -0
  12. package/build/background/handlers/Extension.js +676 -0
  13. package/build/background/handlers/State.d.ts +87 -0
  14. package/build/background/handlers/State.js +434 -0
  15. package/build/background/handlers/Tabs.d.ts +24 -0
  16. package/build/background/handlers/Tabs.js +230 -0
  17. package/build/background/handlers/helpers.d.ts +1 -0
  18. package/build/background/handlers/helpers.js +13 -0
  19. package/build/background/handlers/index.d.ts +3 -0
  20. package/build/background/handlers/index.js +43 -0
  21. package/build/background/handlers/subscriptions.d.ts +4 -0
  22. package/build/background/handlers/subscriptions.js +24 -0
  23. package/build/background/types.d.ts +345 -0
  24. package/build/background/types.js +1 -0
  25. package/build/bundle.d.ts +1 -0
  26. package/build/bundle.js +3 -0
  27. package/build/cjs/background/KoniTypes.js +69 -0
  28. package/build/cjs/background/RequestBytesSign.js +27 -0
  29. package/build/cjs/background/RequestExtrinsicSign.js +23 -0
  30. package/build/cjs/background/handlers/Extension.js +764 -0
  31. package/build/cjs/background/handlers/State.js +472 -0
  32. package/build/cjs/background/handlers/Tabs.js +273 -0
  33. package/build/cjs/background/handlers/helpers.js +20 -0
  34. package/build/cjs/background/handlers/index.js +60 -0
  35. package/build/cjs/background/handlers/subscriptions.js +32 -0
  36. package/build/cjs/background/types.js +1 -0
  37. package/build/cjs/bundle.js +13 -0
  38. package/build/cjs/defaults.js +26 -0
  39. package/build/cjs/detectOther.js +17 -0
  40. package/build/cjs/detectPackage.js +14 -0
  41. package/build/cjs/index.js +18 -0
  42. package/build/cjs/package.json +3 -0
  43. package/build/cjs/packageInfo.js +16 -0
  44. package/build/cjs/page/Accounts.js +31 -0
  45. package/build/cjs/page/Injected.js +30 -0
  46. package/build/cjs/page/Metadata.js +27 -0
  47. package/build/cjs/page/PostMessageProvider.js +184 -0
  48. package/build/cjs/page/Signer.js +44 -0
  49. package/build/cjs/page/index.js +76 -0
  50. package/build/cjs/page/types.js +1 -0
  51. package/build/cjs/stores/Accounts.js +33 -0
  52. package/build/cjs/stores/Base.js +84 -0
  53. package/build/cjs/stores/Metadata.js +23 -0
  54. package/build/cjs/stores/index.js +23 -0
  55. package/build/cjs/types.js +1 -0
  56. package/build/cjs/utils/canDerive.js +12 -0
  57. package/build/cjs/utils/getId.js +16 -0
  58. package/build/cjs/utils/index.js +13 -0
  59. package/build/defaults.d.ts +10 -0
  60. package/build/defaults.js +12 -0
  61. package/build/detectOther.d.ts +7 -0
  62. package/build/detectOther.js +6 -0
  63. package/build/detectPackage.d.ts +1 -0
  64. package/build/detectPackage.js +7 -0
  65. package/build/index.d.ts +1 -0
  66. package/build/index.js +5 -0
  67. package/build/package.json +210 -0
  68. package/build/packageInfo.d.ts +6 -0
  69. package/build/packageInfo.js +9 -0
  70. package/build/page/Accounts.d.ts +7 -0
  71. package/build/page/Accounts.js +22 -0
  72. package/build/page/Injected.d.ts +13 -0
  73. package/build/page/Injected.js +15 -0
  74. package/build/page/Metadata.d.ts +7 -0
  75. package/build/page/Metadata.js +18 -0
  76. package/build/page/PostMessageProvider.d.ts +62 -0
  77. package/build/page/PostMessageProvider.js +171 -0
  78. package/build/page/Signer.d.ts +8 -0
  79. package/build/page/Signer.js +35 -0
  80. package/build/page/index.d.ts +16 -0
  81. package/build/page/index.js +64 -0
  82. package/build/page/types.d.ts +6 -0
  83. package/build/page/types.js +1 -0
  84. package/build/stores/Accounts.d.ts +6 -0
  85. package/build/stores/Accounts.js +20 -0
  86. package/build/stores/Base.d.ts +10 -0
  87. package/build/stores/Base.js +74 -0
  88. package/build/stores/Metadata.d.ts +5 -0
  89. package/build/stores/Metadata.js +10 -0
  90. package/build/stores/index.d.ts +2 -0
  91. package/build/stores/index.js +4 -0
  92. package/build/types.d.ts +9 -0
  93. package/build/types.js +1 -0
  94. package/build/utils/canDerive.d.ts +2 -0
  95. package/build/utils/canDerive.js +5 -0
  96. package/build/utils/getId.d.ts +1 -0
  97. package/build/utils/getId.js +7 -0
  98. package/build/utils/index.d.ts +1 -0
  99. package/build/utils/index.js +3 -0
  100. package/build-cjs/background/KoniTypes.js +69 -0
  101. package/build-cjs/background/RequestBytesSign.js +27 -0
  102. package/build-cjs/background/RequestExtrinsicSign.js +23 -0
  103. package/build-cjs/background/handlers/Extension.js +764 -0
  104. package/build-cjs/background/handlers/State.js +472 -0
  105. package/build-cjs/background/handlers/Tabs.js +273 -0
  106. package/build-cjs/background/handlers/helpers.js +20 -0
  107. package/build-cjs/background/handlers/index.js +60 -0
  108. package/build-cjs/background/handlers/subscriptions.js +32 -0
  109. package/build-cjs/background/types.js +1 -0
  110. package/build-cjs/bundle.js +13 -0
  111. package/build-cjs/defaults.js +26 -0
  112. package/build-cjs/detectOther.js +17 -0
  113. package/build-cjs/detectPackage.js +14 -0
  114. package/build-cjs/index.js +18 -0
  115. package/build-cjs/packageInfo.js +16 -0
  116. package/build-cjs/page/Accounts.js +31 -0
  117. package/build-cjs/page/Injected.js +30 -0
  118. package/build-cjs/page/Metadata.js +27 -0
  119. package/build-cjs/page/PostMessageProvider.js +184 -0
  120. package/build-cjs/page/Signer.js +44 -0
  121. package/build-cjs/page/index.js +76 -0
  122. package/build-cjs/page/types.js +1 -0
  123. package/build-cjs/stores/Accounts.js +33 -0
  124. package/build-cjs/stores/Base.js +84 -0
  125. package/build-cjs/stores/Metadata.js +23 -0
  126. package/build-cjs/stores/index.js +23 -0
  127. package/build-cjs/types.js +1 -0
  128. package/build-cjs/utils/canDerive.js +12 -0
  129. package/build-cjs/utils/getId.js +16 -0
  130. package/build-cjs/utils/index.js +13 -0
  131. package/package.json +46 -0
  132. package/src/background/KoniTypes.ts +537 -0
  133. package/src/background/RequestBytesSign.ts +30 -0
  134. package/src/background/RequestExtrinsicSign.ts +23 -0
  135. package/src/background/handlers/Extension.spec.ts +456 -0
  136. package/src/background/handlers/Extension.ts +627 -0
  137. package/src/background/handlers/State.ts +513 -0
  138. package/src/background/handlers/Tabs.ts +223 -0
  139. package/src/background/handlers/helpers.ts +14 -0
  140. package/src/background/handlers/index.ts +49 -0
  141. package/src/background/handlers/subscriptions.ts +30 -0
  142. package/src/background/types.ts +437 -0
  143. package/src/bundle.ts +4 -0
  144. package/src/defaults.ts +24 -0
  145. package/src/detectOther.ts +8 -0
  146. package/src/detectPackage.ts +11 -0
  147. package/src/index.ts +7 -0
  148. package/src/packageInfo.ts +6 -0
  149. package/src/page/Accounts.ts +27 -0
  150. package/src/page/Injected.ts +27 -0
  151. package/src/page/Metadata.ts +22 -0
  152. package/src/page/PostMessageProvider.ts +178 -0
  153. package/src/page/Signer.ts +45 -0
  154. package/src/page/index.ts +87 -0
  155. package/src/page/types.ts +10 -0
  156. package/src/stores/Accounts.ts +24 -0
  157. package/src/stores/Base.ts +81 -0
  158. package/src/stores/Metadata.ts +13 -0
  159. package/src/stores/index.ts +5 -0
  160. package/src/types.ts +12 -0
  161. package/src/utils/canDerive.ts +8 -0
  162. package/src/utils/getId.ts +10 -0
  163. package/src/utils/index.ts +4 -0
  164. package/tsconfig.build.json +18 -0
  165. package/tsconfig.build.tsbuildinfo +1 -0
  166. package/tsconfig.json +14 -0
@@ -0,0 +1,210 @@
1
+ {
2
+ "author": "Jaco Greeff <jacogr@gmail.com>",
3
+ "bugs": "https://github.com/Koniverse/Subwallet-V2/issues",
4
+ "contributors": [],
5
+ "description": "Functions, classes and other utilities used in @subwallet/extension",
6
+ "homepage": "https://github.com/Koniverse/Subwallet-V2/tree/master/packages/extension-base#readme",
7
+ "license": "Apache-2.0",
8
+ "maintainers": [],
9
+ "name": "@subwallet/extension-base",
10
+ "repository": {
11
+ "directory": "packages/extension-base",
12
+ "type": "git",
13
+ "url": "https://github.com/Koniverse/Subwallet-V2.git"
14
+ },
15
+ "sideEffects": [
16
+ "./detectPackage.js",
17
+ "./cjs/detectPackage.js"
18
+ ],
19
+ "type": "module",
20
+ "version": "0.3.6-0",
21
+ "main": "./cjs/index.js",
22
+ "module": "./index.js",
23
+ "types": "./index.d.ts",
24
+ "exports": {
25
+ "./cjs/package.json": "./cjs/package.json",
26
+ "./cjs/*": "./cjs/*.js",
27
+ ".": {
28
+ "types": "./index.d.ts",
29
+ "require": "./cjs/index.js",
30
+ "default": "./index.js"
31
+ },
32
+ "./background/handlers": {
33
+ "types": "./background/handlers/index.d.ts",
34
+ "require": "./cjs/background/handlers/index.js",
35
+ "default": "./background/handlers/index.js"
36
+ },
37
+ "./background/handlers/Extension": {
38
+ "types": "./background/handlers/Extension.d.ts",
39
+ "require": "./cjs/background/handlers/Extension.js",
40
+ "default": "./background/handlers/Extension.js"
41
+ },
42
+ "./background/handlers/helpers": {
43
+ "types": "./background/handlers/helpers.d.ts",
44
+ "require": "./cjs/background/handlers/helpers.js",
45
+ "default": "./background/handlers/helpers.js"
46
+ },
47
+ "./background/handlers/State": {
48
+ "types": "./background/handlers/State.d.ts",
49
+ "require": "./cjs/background/handlers/State.js",
50
+ "default": "./background/handlers/State.js"
51
+ },
52
+ "./background/handlers/subscriptions": {
53
+ "types": "./background/handlers/subscriptions.d.ts",
54
+ "require": "./cjs/background/handlers/subscriptions.js",
55
+ "default": "./background/handlers/subscriptions.js"
56
+ },
57
+ "./background/handlers/Tabs": {
58
+ "types": "./background/handlers/Tabs.d.ts",
59
+ "require": "./cjs/background/handlers/Tabs.js",
60
+ "default": "./background/handlers/Tabs.js"
61
+ },
62
+ "./background/KoniTypes": {
63
+ "types": "./background/KoniTypes.d.ts",
64
+ "require": "./cjs/background/KoniTypes.js",
65
+ "default": "./background/KoniTypes.js"
66
+ },
67
+ "./background/RequestBytesSign": {
68
+ "types": "./background/RequestBytesSign.d.ts",
69
+ "require": "./cjs/background/RequestBytesSign.js",
70
+ "default": "./background/RequestBytesSign.js"
71
+ },
72
+ "./background/RequestExtrinsicSign": {
73
+ "types": "./background/RequestExtrinsicSign.d.ts",
74
+ "require": "./cjs/background/RequestExtrinsicSign.js",
75
+ "default": "./background/RequestExtrinsicSign.js"
76
+ },
77
+ "./background/types": {
78
+ "types": "./background/types.d.ts",
79
+ "require": "./cjs/background/types.js",
80
+ "default": "./background/types.js"
81
+ },
82
+ "./bundle": {
83
+ "types": "./bundle.d.ts",
84
+ "require": "./cjs/bundle.js",
85
+ "default": "./bundle.js"
86
+ },
87
+ "./defaults": {
88
+ "types": "./defaults.d.ts",
89
+ "require": "./cjs/defaults.js",
90
+ "default": "./defaults.js"
91
+ },
92
+ "./detectOther": {
93
+ "types": "./detectOther.d.ts",
94
+ "require": "./cjs/detectOther.js",
95
+ "default": "./detectOther.js"
96
+ },
97
+ "./detectPackage": {
98
+ "types": "./detectPackage.d.ts",
99
+ "require": "./cjs/detectPackage.js",
100
+ "default": "./detectPackage.js"
101
+ },
102
+ "./package.json": "./package.json",
103
+ "./packageInfo.js": {
104
+ "types": "./packageInfo.d.ts",
105
+ "require": "./cjs/packageInfo.js",
106
+ "default": "./packageInfo.js"
107
+ },
108
+ "./packageInfo": {
109
+ "types": "./packageInfo.d.ts",
110
+ "require": "./cjs/packageInfo.js",
111
+ "default": "./packageInfo.js"
112
+ },
113
+ "./page": {
114
+ "types": "./page/index.d.ts",
115
+ "require": "./cjs/page/index.js",
116
+ "default": "./page/index.js"
117
+ },
118
+ "./page/Accounts": {
119
+ "types": "./page/Accounts.d.ts",
120
+ "require": "./cjs/page/Accounts.js",
121
+ "default": "./page/Accounts.js"
122
+ },
123
+ "./page/Injected": {
124
+ "types": "./page/Injected.d.ts",
125
+ "require": "./cjs/page/Injected.js",
126
+ "default": "./page/Injected.js"
127
+ },
128
+ "./page/Metadata": {
129
+ "types": "./page/Metadata.d.ts",
130
+ "require": "./cjs/page/Metadata.js",
131
+ "default": "./page/Metadata.js"
132
+ },
133
+ "./page/PostMessageProvider": {
134
+ "types": "./page/PostMessageProvider.d.ts",
135
+ "require": "./cjs/page/PostMessageProvider.js",
136
+ "default": "./page/PostMessageProvider.js"
137
+ },
138
+ "./page/Signer": {
139
+ "types": "./page/Signer.d.ts",
140
+ "require": "./cjs/page/Signer.js",
141
+ "default": "./page/Signer.js"
142
+ },
143
+ "./page/types": {
144
+ "types": "./page/types.d.ts",
145
+ "require": "./cjs/page/types.js",
146
+ "default": "./page/types.js"
147
+ },
148
+ "./stores": {
149
+ "types": "./stores/index.d.ts",
150
+ "require": "./cjs/stores/index.js",
151
+ "default": "./stores/index.js"
152
+ },
153
+ "./stores/Accounts": {
154
+ "types": "./stores/Accounts.d.ts",
155
+ "require": "./cjs/stores/Accounts.js",
156
+ "default": "./stores/Accounts.js"
157
+ },
158
+ "./stores/Base": {
159
+ "types": "./stores/Base.d.ts",
160
+ "require": "./cjs/stores/Base.js",
161
+ "default": "./stores/Base.js"
162
+ },
163
+ "./stores/Metadata": {
164
+ "types": "./stores/Metadata.d.ts",
165
+ "require": "./cjs/stores/Metadata.js",
166
+ "default": "./stores/Metadata.js"
167
+ },
168
+ "./types": {
169
+ "types": "./types.d.ts",
170
+ "require": "./cjs/types.js",
171
+ "default": "./types.js"
172
+ },
173
+ "./utils": {
174
+ "types": "./utils/index.d.ts",
175
+ "require": "./cjs/utils/index.js",
176
+ "default": "./utils/index.js"
177
+ },
178
+ "./utils/canDerive": {
179
+ "types": "./utils/canDerive.d.ts",
180
+ "require": "./cjs/utils/canDerive.js",
181
+ "default": "./utils/canDerive.js"
182
+ },
183
+ "./utils/getId": {
184
+ "types": "./utils/getId.d.ts",
185
+ "require": "./cjs/utils/getId.js",
186
+ "default": "./utils/getId.js"
187
+ }
188
+ },
189
+ "dependencies": {
190
+ "@babel/runtime": "^7.16.7",
191
+ "@ethereumjs/common": "^2.6.3",
192
+ "@polkadot/api": "^7.3.1",
193
+ "@polkadot/keyring": "^8.3.1",
194
+ "@polkadot/networks": "^8.3.1",
195
+ "@polkadot/phishing": "^0.11.30",
196
+ "@polkadot/rpc-provider": "^7.3.1",
197
+ "@polkadot/types": "^7.3.1",
198
+ "@polkadot/ui-keyring": "^0.89.1",
199
+ "@polkadot/ui-settings": "^0.89.1",
200
+ "@polkadot/util": "^8.3.1",
201
+ "@polkadot/util-crypto": "^8.3.1",
202
+ "@subwallet/extension-chains": "^0.3.6-0",
203
+ "@subwallet/extension-dapp": "^0.3.6-0",
204
+ "@subwallet/extension-inject": "^0.3.6-0",
205
+ "ethereumjs-tx": "^2.1.2",
206
+ "eventemitter3": "^4.0.7",
207
+ "rxjs": "^7.5.1",
208
+ "web3": "^1.7.1"
209
+ }
210
+ }
@@ -0,0 +1,6 @@
1
+ export declare const packageInfo: {
2
+ name: string;
3
+ path: string;
4
+ type: string;
5
+ version: string;
6
+ };
@@ -0,0 +1,9 @@
1
+ // Copyright 2017-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // Do not edit, auto-generated by @polkadot/dev
4
+ export const packageInfo = {
5
+ name: '@subwallet/extension-base',
6
+ path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
7
+ type: 'esm',
8
+ version: '0.3.6-0'
9
+ };
@@ -0,0 +1,7 @@
1
+ import type { InjectedAccount, InjectedAccounts, Unsubcall } from '@subwallet/extension-inject/types';
2
+ import type { SendRequest } from './types';
3
+ export default class Accounts implements InjectedAccounts {
4
+ constructor(_sendRequest: SendRequest);
5
+ get(anyType?: boolean): Promise<InjectedAccount[]>;
6
+ subscribe(cb: (accounts: InjectedAccount[]) => unknown): Unsubcall;
7
+ }
@@ -0,0 +1,22 @@
1
+ // Copyright 2019-2022 @subwallet/extension authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // External to class, this.# is not private enough (yet)
4
+ let sendRequest;
5
+ export default class Accounts {
6
+ constructor(_sendRequest) {
7
+ sendRequest = _sendRequest;
8
+ }
9
+
10
+ get(anyType) {
11
+ return sendRequest('pub(accounts.listV2)', {
12
+ anyType
13
+ });
14
+ }
15
+
16
+ subscribe(cb) {
17
+ sendRequest('pub(accounts.subscribeV2)', null, cb).catch(error => console.error(error));
18
+ return () => {// FIXME we need the ability to unsubscribe
19
+ };
20
+ }
21
+
22
+ }
@@ -0,0 +1,13 @@
1
+ import type { Injected } from '@subwallet/extension-inject/types';
2
+ import type { SendRequest } from './types';
3
+ import Accounts from './Accounts';
4
+ import Metadata from './Metadata';
5
+ import PostMessageProvider from './PostMessageProvider';
6
+ import Signer from './Signer';
7
+ export default class implements Injected {
8
+ readonly accounts: Accounts;
9
+ readonly metadata: Metadata;
10
+ readonly provider: PostMessageProvider;
11
+ readonly signer: Signer;
12
+ constructor(sendRequest: SendRequest);
13
+ }
@@ -0,0 +1,15 @@
1
+ // Copyright 2019-2022 @subwallet/extension authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import Accounts from "./Accounts.js";
4
+ import Metadata from "./Metadata.js";
5
+ import PostMessageProvider from "./PostMessageProvider.js";
6
+ import Signer from "./Signer.js";
7
+ export default class {
8
+ constructor(sendRequest) {
9
+ this.accounts = new Accounts(sendRequest);
10
+ this.metadata = new Metadata(sendRequest);
11
+ this.provider = new PostMessageProvider(sendRequest);
12
+ this.signer = new Signer(sendRequest);
13
+ }
14
+
15
+ }
@@ -0,0 +1,7 @@
1
+ import type { InjectedMetadata, InjectedMetadataKnown, MetadataDef } from '@subwallet/extension-inject/types';
2
+ import type { SendRequest } from './types';
3
+ export default class Metadata implements InjectedMetadata {
4
+ constructor(_sendRequest: SendRequest);
5
+ get(): Promise<InjectedMetadataKnown[]>;
6
+ provide(definition: MetadataDef): Promise<boolean>;
7
+ }
@@ -0,0 +1,18 @@
1
+ // Copyright 2019-2022 @subwallet/extension authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // External to class, this.# is not private enough (yet)
4
+ let sendRequest;
5
+ export default class Metadata {
6
+ constructor(_sendRequest) {
7
+ sendRequest = _sendRequest;
8
+ }
9
+
10
+ get() {
11
+ return sendRequest('pub(metadata.list)');
12
+ }
13
+
14
+ provide(definition) {
15
+ return sendRequest('pub(metadata.provide)', definition);
16
+ }
17
+
18
+ }
@@ -0,0 +1,62 @@
1
+ import type { InjectedProvider, ProviderList, ProviderMeta } from '@subwallet/extension-inject/types';
2
+ import type { ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '@polkadot/rpc-provider/types';
3
+ import type { AnyFunction } from '@polkadot/types/types';
4
+ import type { SendRequest } from './types';
5
+ declare type CallbackHandler = (error?: null | Error, value?: unknown) => void;
6
+ interface SubscriptionHandler {
7
+ callback: CallbackHandler;
8
+ type: string;
9
+ }
10
+ /**
11
+ * @name PostMessageProvider
12
+ *
13
+ * @description Extension provider to be used by dapps
14
+ */
15
+ export default class PostMessageProvider implements InjectedProvider {
16
+ #private;
17
+ /**
18
+ * @param {function} sendRequest The function to be called to send requests to the node
19
+ * @param {function} subscriptionNotificationHandler Channel for receiving subscription messages
20
+ */
21
+ constructor(_sendRequest: SendRequest);
22
+ /**
23
+ * @description Returns a clone of the object
24
+ */
25
+ clone(): PostMessageProvider;
26
+ /**
27
+ * @description Manually disconnect from the connection, clearing autoconnect logic
28
+ */
29
+ connect(): Promise<void>;
30
+ /**
31
+ * @description Manually disconnect from the connection, clearing autoconnect logic
32
+ */
33
+ disconnect(): Promise<void>;
34
+ /**
35
+ * @summary `true` when this provider supports subscriptions
36
+ */
37
+ get hasSubscriptions(): boolean;
38
+ /**
39
+ * @summary Whether the node is connected or not.
40
+ * @return {boolean} true if connected
41
+ */
42
+ get isConnected(): boolean;
43
+ listProviders(): Promise<ProviderList>;
44
+ /**
45
+ * @summary Listens on events after having subscribed using the [[subscribe]] function.
46
+ * @param {ProviderInterfaceEmitted} type Event
47
+ * @param {ProviderInterfaceEmitCb} sub Callback
48
+ * @return unsubscribe function
49
+ */
50
+ on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
51
+ send(method: string, params: unknown[], _?: boolean, subscription?: SubscriptionHandler): Promise<any>;
52
+ /**
53
+ * @summary Spawn a provider on the extension background.
54
+ */
55
+ startProvider(key: string): Promise<ProviderMeta>;
56
+ subscribe(type: string, method: string, params: unknown[], callback: AnyFunction): Promise<number>;
57
+ /**
58
+ * @summary Allows unsubscribing to subscriptions made with [[subscribe]].
59
+ */
60
+ unsubscribe(type: string, method: string, id: number): Promise<boolean>;
61
+ }
62
+ export {};
@@ -0,0 +1,171 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import EventEmitter from 'eventemitter3';
4
+ import { isUndefined, logger } from '@polkadot/util';
5
+ const l = logger('PostMessageProvider');
6
+ // External to class, this.# is not private enough (yet)
7
+ let sendRequest;
8
+ /**
9
+ * @name PostMessageProvider
10
+ *
11
+ * @description Extension provider to be used by dapps
12
+ */
13
+
14
+ export default class PostMessageProvider {
15
+ #eventemitter; // Whether or not the actual extension background provider is connected
16
+
17
+ #isConnected = false; // Subscription IDs are (historically) not guaranteed to be globally unique;
18
+ // only unique for a given subscription method; which is why we identify
19
+ // the subscriptions based on subscription id + type
20
+
21
+ #subscriptions = {}; // {[(type,subscriptionId)]: callback}
22
+
23
+ /**
24
+ * @param {function} sendRequest The function to be called to send requests to the node
25
+ * @param {function} subscriptionNotificationHandler Channel for receiving subscription messages
26
+ */
27
+
28
+ constructor(_sendRequest) {
29
+ this.#eventemitter = new EventEmitter();
30
+ sendRequest = _sendRequest;
31
+ }
32
+ /**
33
+ * @description Returns a clone of the object
34
+ */
35
+
36
+
37
+ clone() {
38
+ return new PostMessageProvider(sendRequest);
39
+ }
40
+ /**
41
+ * @description Manually disconnect from the connection, clearing autoconnect logic
42
+ */
43
+ // eslint-disable-next-line @typescript-eslint/require-await
44
+
45
+
46
+ async connect() {
47
+ // FIXME This should see if the extension's state's provider can disconnect
48
+ console.error('PostMessageProvider.disconnect() is not implemented.');
49
+ }
50
+ /**
51
+ * @description Manually disconnect from the connection, clearing autoconnect logic
52
+ */
53
+ // eslint-disable-next-line @typescript-eslint/require-await
54
+
55
+
56
+ async disconnect() {
57
+ // FIXME This should see if the extension's state's provider can disconnect
58
+ console.error('PostMessageProvider.disconnect() is not implemented.');
59
+ }
60
+ /**
61
+ * @summary `true` when this provider supports subscriptions
62
+ */
63
+
64
+
65
+ get hasSubscriptions() {
66
+ // FIXME This should see if the extension's state's provider has subscriptions
67
+ return true;
68
+ }
69
+ /**
70
+ * @summary Whether the node is connected or not.
71
+ * @return {boolean} true if connected
72
+ */
73
+
74
+
75
+ get isConnected() {
76
+ return this.#isConnected;
77
+ }
78
+
79
+ listProviders() {
80
+ return sendRequest('pub(rpc.listProviders)', undefined);
81
+ }
82
+ /**
83
+ * @summary Listens on events after having subscribed using the [[subscribe]] function.
84
+ * @param {ProviderInterfaceEmitted} type Event
85
+ * @param {ProviderInterfaceEmitCb} sub Callback
86
+ * @return unsubscribe function
87
+ */
88
+
89
+
90
+ on(type, sub) {
91
+ this.#eventemitter.on(type, sub);
92
+ return () => {
93
+ this.#eventemitter.removeListener(type, sub);
94
+ };
95
+ } // eslint-disable-next-line @typescript-eslint/no-explicit-any
96
+
97
+
98
+ async send(method, params, _, subscription) {
99
+ if (subscription) {
100
+ const {
101
+ callback,
102
+ type
103
+ } = subscription;
104
+ const id = await sendRequest('pub(rpc.subscribe)', {
105
+ method,
106
+ params,
107
+ type
108
+ }, res => {
109
+ subscription.callback(null, res);
110
+ });
111
+ this.#subscriptions[`${type}::${id}`] = callback;
112
+ return id;
113
+ }
114
+
115
+ return sendRequest('pub(rpc.send)', {
116
+ method,
117
+ params
118
+ });
119
+ }
120
+ /**
121
+ * @summary Spawn a provider on the extension background.
122
+ */
123
+
124
+
125
+ async startProvider(key) {
126
+ // Disconnect from the previous provider
127
+ this.#isConnected = false;
128
+ this.#eventemitter.emit('disconnected');
129
+ const meta = await sendRequest('pub(rpc.startProvider)', key); // eslint-disable-next-line @typescript-eslint/no-floating-promises
130
+
131
+ sendRequest('pub(rpc.subscribeConnected)', null, connected => {
132
+ this.#isConnected = connected;
133
+
134
+ if (connected) {
135
+ this.#eventemitter.emit('connected');
136
+ } else {
137
+ this.#eventemitter.emit('disconnected');
138
+ }
139
+
140
+ return true;
141
+ });
142
+ return meta;
143
+ }
144
+
145
+ subscribe(type, method, params, callback) {
146
+ return this.send(method, params, false, {
147
+ callback,
148
+ type
149
+ });
150
+ }
151
+ /**
152
+ * @summary Allows unsubscribing to subscriptions made with [[subscribe]].
153
+ */
154
+
155
+
156
+ async unsubscribe(type, method, id) {
157
+ const subscription = `${type}::${id}`; // FIXME This now could happen with re-subscriptions. The issue is that with a re-sub
158
+ // the assigned id now does not match what the API user originally received. It has
159
+ // a slight complication in solving - since we cannot rely on the send id, but rather
160
+ // need to find the actual subscription id to map it
161
+
162
+ if (isUndefined(this.#subscriptions[subscription])) {
163
+ l.debug(() => `Unable to find active subscription=${subscription}`);
164
+ return false;
165
+ }
166
+
167
+ delete this.#subscriptions[subscription];
168
+ return this.send(method, [id]);
169
+ }
170
+
171
+ }
@@ -0,0 +1,8 @@
1
+ import type { Signer as SignerInterface, SignerResult } from '@polkadot/api/types';
2
+ import type { SignerPayloadJSON, SignerPayloadRaw } from '@polkadot/types/types';
3
+ import type { SendRequest } from './types';
4
+ export default class Signer implements SignerInterface {
5
+ constructor(_sendRequest: SendRequest);
6
+ signPayload(payload: SignerPayloadJSON): Promise<SignerResult>;
7
+ signRaw(payload: SignerPayloadRaw): Promise<SignerResult>;
8
+ }
@@ -0,0 +1,35 @@
1
+ // Copyright 2019-2022 @subwallet/extension-base authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ // External to class, this.# is not private enough (yet)
4
+ let sendRequest;
5
+ let nextId = 0;
6
+ export default class Signer {
7
+ constructor(_sendRequest) {
8
+ sendRequest = _sendRequest;
9
+ }
10
+
11
+ async signPayload(payload) {
12
+ const id = ++nextId;
13
+ const result = await sendRequest('pub(extrinsic.sign)', payload); // we add an internal id (number) - should have a mapping from the
14
+ // extension id (string) -> internal id (number) if we wish to provide
15
+ // updated via the update functionality (noop at this point)
16
+
17
+ return { ...result,
18
+ id
19
+ };
20
+ }
21
+
22
+ async signRaw(payload) {
23
+ const id = ++nextId;
24
+ const result = await sendRequest('pub(bytes.sign)', payload);
25
+ return { ...result,
26
+ id
27
+ };
28
+ } // NOTE We don't listen to updates at all, if we do we can interpret the
29
+ // resuklt as provided by the API here
30
+ // public update (id: number, status: Hash | SubmittableResult): void {
31
+ // // ignore
32
+ // }
33
+
34
+
35
+ }
@@ -0,0 +1,16 @@
1
+ import type { MessageTypes, MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes, TransportResponseMessage } from '../background/types';
2
+ import Injected from './Injected';
3
+ export interface Handler {
4
+ resolve: (data?: any) => void;
5
+ reject: (error: Error) => void;
6
+ subscriber?: (data: any) => void;
7
+ }
8
+ export declare type Handlers = Record<string, Handler>;
9
+ export declare function sendMessage<TMessageType extends MessageTypesWithNullRequest>(message: TMessageType): Promise<ResponseTypes[TMessageType]>;
10
+ export declare function sendMessage<TMessageType extends MessageTypesWithNoSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType]): Promise<ResponseTypes[TMessageType]>;
11
+ export declare function sendMessage<TMessageType extends MessageTypesWithSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType], subscriber: (data: SubscriptionMessageTypes[TMessageType]) => void): Promise<ResponseTypes[TMessageType]>;
12
+ export declare function enable(origin: string): Promise<Injected>;
13
+ export declare function redirectIfPhishing(): Promise<boolean>;
14
+ export declare function handleResponse<TMessageType extends MessageTypes>(data: TransportResponseMessage<TMessageType> & {
15
+ subscription?: string;
16
+ }): void;