@pezkuwi/extension-base 0.62.14 → 0.62.18

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 (160) hide show
  1. package/package.json +16 -470
  2. package/src/background/RequestBytesSign.ts +28 -0
  3. package/src/background/RequestExtrinsicSign.ts +22 -0
  4. package/src/background/handlers/Extension.spec.ts +478 -0
  5. package/src/background/handlers/Extension.ts +690 -0
  6. package/src/background/handlers/State.ts +664 -0
  7. package/src/background/handlers/Tabs.ts +289 -0
  8. package/src/background/handlers/helpers.ts +14 -0
  9. package/src/background/handlers/index.ts +60 -0
  10. package/src/background/handlers/subscriptions.ts +32 -0
  11. package/src/background/index.ts +4 -0
  12. package/src/background/types.ts +432 -0
  13. package/src/bundle.ts +4 -0
  14. package/{defaults.js → src/defaults.ts} +15 -2
  15. package/src/index.ts +7 -0
  16. package/{packageDetect.js → src/packageDetect.ts} +8 -0
  17. package/src/packageInfo.ts +6 -0
  18. package/src/page/Accounts.ts +33 -0
  19. package/src/page/Injected.ts +33 -0
  20. package/src/page/Metadata.ts +22 -0
  21. package/src/page/PostMessageProvider.ts +182 -0
  22. package/src/page/Signer.ts +45 -0
  23. package/src/page/index.ts +89 -0
  24. package/src/page/types.ts +10 -0
  25. package/src/stores/Accounts.ts +28 -0
  26. package/src/stores/Base.ts +93 -0
  27. package/src/stores/Metadata.ts +17 -0
  28. package/{stores/index.js → src/stores/index.ts} +3 -0
  29. package/src/types.ts +12 -0
  30. package/src/utils/canDerive.ts +8 -0
  31. package/src/utils/getId.ts +10 -0
  32. package/src/utils/index.ts +4 -0
  33. package/src/utils/portUtils.ts +65 -0
  34. package/tsconfig.build.json +16 -0
  35. package/tsconfig.build.tsbuildinfo +1 -0
  36. package/tsconfig.spec.json +18 -0
  37. package/tsconfig.spec.tsbuildinfo +1 -0
  38. package/LICENSE +0 -201
  39. package/background/RequestBytesSign.d.ts +0 -12
  40. package/background/RequestBytesSign.js +0 -12
  41. package/background/RequestExtrinsicSign.d.ts +0 -12
  42. package/background/RequestExtrinsicSign.js +0 -11
  43. package/background/handlers/Extension.d.ts +0 -49
  44. package/background/handlers/Extension.js +0 -489
  45. package/background/handlers/State.js +0 -478
  46. package/background/handlers/Tabs.d.ts +0 -25
  47. package/background/handlers/Tabs.js +0 -195
  48. package/background/handlers/helpers.js +0 -11
  49. package/background/handlers/index.d.ts +0 -3
  50. package/background/handlers/index.js +0 -40
  51. package/background/handlers/subscriptions.d.ts +0 -3
  52. package/background/handlers/subscriptions.js +0 -18
  53. package/background/index.d.ts +0 -1
  54. package/background/index.js +0 -1
  55. package/background/types.js +0 -1
  56. package/bundle.d.ts +0 -1
  57. package/bundle.js +0 -1
  58. package/cjs/background/RequestBytesSign.d.ts +0 -12
  59. package/cjs/background/RequestBytesSign.js +0 -15
  60. package/cjs/background/RequestExtrinsicSign.d.ts +0 -12
  61. package/cjs/background/RequestExtrinsicSign.js +0 -14
  62. package/cjs/background/handlers/Extension.d.ts +0 -49
  63. package/cjs/background/handlers/Extension.js +0 -492
  64. package/cjs/background/handlers/State.d.ts +0 -96
  65. package/cjs/background/handlers/State.js +0 -482
  66. package/cjs/background/handlers/Tabs.d.ts +0 -25
  67. package/cjs/background/handlers/Tabs.js +0 -199
  68. package/cjs/background/handlers/helpers.d.ts +0 -1
  69. package/cjs/background/handlers/helpers.js +0 -14
  70. package/cjs/background/handlers/index.d.ts +0 -3
  71. package/cjs/background/handlers/index.js +0 -46
  72. package/cjs/background/handlers/subscriptions.d.ts +0 -3
  73. package/cjs/background/handlers/subscriptions.js +0 -22
  74. package/cjs/background/index.d.ts +0 -1
  75. package/cjs/background/index.js +0 -7
  76. package/cjs/background/types.d.ts +0 -343
  77. package/cjs/background/types.js +0 -2
  78. package/cjs/bundle.d.ts +0 -1
  79. package/cjs/bundle.js +0 -5
  80. package/cjs/defaults.js +0 -16
  81. package/cjs/index.d.ts +0 -1
  82. package/cjs/index.js +0 -4
  83. package/cjs/package.json +0 -3
  84. package/cjs/packageDetect.d.ts +0 -1
  85. package/cjs/packageDetect.js +0 -8
  86. package/cjs/packageInfo.d.ts +0 -6
  87. package/cjs/packageInfo.js +0 -4
  88. package/cjs/page/Accounts.d.ts +0 -7
  89. package/cjs/page/Accounts.js +0 -24
  90. package/cjs/page/Injected.d.ts +0 -13
  91. package/cjs/page/Injected.js +0 -25
  92. package/cjs/page/Metadata.d.ts +0 -7
  93. package/cjs/page/Metadata.js +0 -15
  94. package/cjs/page/PostMessageProvider.d.ts +0 -63
  95. package/cjs/page/PostMessageProvider.js +0 -135
  96. package/cjs/page/Signer.d.ts +0 -8
  97. package/cjs/page/Signer.js +0 -29
  98. package/cjs/page/index.d.ts +0 -16
  99. package/cjs/page/index.js +0 -52
  100. package/cjs/page/types.d.ts +0 -6
  101. package/cjs/page/types.js +0 -2
  102. package/cjs/stores/Accounts.js +0 -21
  103. package/cjs/stores/Base.js +0 -70
  104. package/cjs/stores/Metadata.js +0 -13
  105. package/cjs/stores/index.js +0 -8
  106. package/cjs/types.js +0 -2
  107. package/cjs/utils/canDerive.d.ts +0 -2
  108. package/cjs/utils/canDerive.js +0 -6
  109. package/cjs/utils/getId.js +0 -8
  110. package/cjs/utils/index.d.ts +0 -1
  111. package/cjs/utils/index.js +0 -5
  112. package/cjs/utils/portUtils.d.ts +0 -13
  113. package/cjs/utils/portUtils.js +0 -49
  114. package/defaults.d.ts +0 -9
  115. package/index.d.ts +0 -1
  116. package/index.js +0 -1
  117. package/packageDetect.d.ts +0 -1
  118. package/packageInfo.d.ts +0 -6
  119. package/packageInfo.js +0 -1
  120. package/page/Accounts.d.ts +0 -7
  121. package/page/Accounts.js +0 -21
  122. package/page/Injected.d.ts +0 -13
  123. package/page/Injected.js +0 -21
  124. package/page/Metadata.d.ts +0 -7
  125. package/page/Metadata.js +0 -12
  126. package/page/PostMessageProvider.d.ts +0 -63
  127. package/page/PostMessageProvider.js +0 -132
  128. package/page/Signer.d.ts +0 -8
  129. package/page/Signer.js +0 -26
  130. package/page/index.d.ts +0 -16
  131. package/page/index.js +0 -45
  132. package/page/types.d.ts +0 -6
  133. package/page/types.js +0 -1
  134. package/stores/Accounts.d.ts +0 -6
  135. package/stores/Accounts.js +0 -17
  136. package/stores/Base.d.ts +0 -9
  137. package/stores/Base.js +0 -67
  138. package/stores/Metadata.d.ts +0 -5
  139. package/stores/Metadata.js +0 -9
  140. package/stores/index.d.ts +0 -2
  141. package/types.d.ts +0 -9
  142. package/types.js +0 -1
  143. package/utils/canDerive.d.ts +0 -2
  144. package/utils/canDerive.js +0 -3
  145. package/utils/getId.d.ts +0 -1
  146. package/utils/getId.js +0 -5
  147. package/utils/index.d.ts +0 -1
  148. package/utils/index.js +0 -1
  149. package/utils/portUtils.d.ts +0 -13
  150. package/utils/portUtils.js +0 -43
  151. /package/{background → src/background}/handlers/State.d.ts +0 -0
  152. /package/{background → src/background}/handlers/helpers.d.ts +0 -0
  153. /package/{background → src/background}/types.d.ts +0 -0
  154. /package/{cjs → src}/defaults.d.ts +0 -0
  155. /package/{cjs → src}/stores/Accounts.d.ts +0 -0
  156. /package/{cjs → src}/stores/Base.d.ts +0 -0
  157. /package/{cjs → src}/stores/Metadata.d.ts +0 -0
  158. /package/{cjs → src}/stores/index.d.ts +0 -0
  159. /package/{cjs → src}/types.d.ts +0 -0
  160. /package/{cjs → src}/utils/getId.d.ts +0 -0
@@ -1,49 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.wakeUpServiceWorkerWrapper = void 0;
4
- exports.setupPort = setupPort;
5
- exports.wakeUpServiceWorker = wakeUpServiceWorker;
6
- exports.ensurePortConnection = ensurePortConnection;
7
- const chrome_1 = require("@pezkuwi/extension-inject/chrome");
8
- function setupPort(portName, onMessageHandler, onDisconnectHandler) {
9
- const port = chrome_1.chrome.runtime.connect({ name: portName });
10
- port.onMessage.addListener(onMessageHandler);
11
- port.onDisconnect.addListener(() => {
12
- console.log(`Disconnected from ${portName}`);
13
- onDisconnectHandler();
14
- });
15
- return port;
16
- }
17
- async function wakeUpServiceWorker() {
18
- return new Promise((resolve, reject) => {
19
- chrome_1.chrome.runtime.sendMessage({ type: 'wakeup' }, (response) => {
20
- if (chrome_1.chrome.runtime.lastError) {
21
- reject(new Error(chrome_1.chrome.runtime.lastError.message));
22
- }
23
- else {
24
- resolve(response);
25
- }
26
- });
27
- });
28
- }
29
- exports.wakeUpServiceWorkerWrapper = { wakeUpServiceWorker };
30
- async function ensurePortConnection(portRef, portConfig) {
31
- const maxAttempts = 5;
32
- const delayMs = 1000;
33
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
34
- try {
35
- const response = await exports.wakeUpServiceWorkerWrapper.wakeUpServiceWorker();
36
- if (response?.status === 'awake') {
37
- if (!portRef) {
38
- return setupPort(portConfig.portName, portConfig.onPortMessageHandler, portConfig.onPortDisconnectHandler);
39
- }
40
- return portRef;
41
- }
42
- }
43
- catch (error) {
44
- console.error(`Attempt ${attempt + 1} failed: ${error.message}`);
45
- await new Promise((resolve) => setTimeout(resolve, delayMs));
46
- }
47
- }
48
- throw new Error('Failed to wake up the service worker and setup the port after multiple attempts');
49
- }
package/defaults.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export declare const EXTENSION_PREFIX: string;
2
- export declare const PORT_CONTENT: string;
3
- export declare const PORT_EXTENSION: string;
4
- export declare const MESSAGE_ORIGIN_PAGE: string;
5
- export declare const MESSAGE_ORIGIN_CONTENT: string;
6
- export declare const ALLOWED_PATH: readonly ["/", "/account/import-ledger", "/account/restore-json"];
7
- export declare const PASSWORD_EXPIRY_MIN = 15;
8
- export declare const PASSWORD_EXPIRY_MS: number;
9
- export declare const PHISHING_PAGE_REDIRECT = "/phishing-page-detected";
package/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export * from './bundle.js';
package/index.js DELETED
@@ -1 +0,0 @@
1
- export * from './bundle.js';
@@ -1 +0,0 @@
1
- export {};
package/packageInfo.d.ts DELETED
@@ -1,6 +0,0 @@
1
- export declare const packageInfo: {
2
- name: string;
3
- path: string;
4
- type: string;
5
- version: string;
6
- };
package/packageInfo.js DELETED
@@ -1 +0,0 @@
1
- export const packageInfo = { name: '@pezkuwi/extension-base', path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto', type: 'esm', version: '0.62.14' };
@@ -1,7 +0,0 @@
1
- import type { InjectedAccount, InjectedAccounts, Unsubcall } from '@pezkuwi/extension-inject/types';
2
- import type { SendRequest } from './types.js';
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
- }
package/page/Accounts.js DELETED
@@ -1,21 +0,0 @@
1
- let sendRequest;
2
- export default class Accounts {
3
- constructor(_sendRequest) {
4
- sendRequest = _sendRequest;
5
- }
6
- get(anyType) {
7
- return sendRequest('pub(accounts.list)', { anyType });
8
- }
9
- subscribe(cb) {
10
- let id = null;
11
- sendRequest('pub(accounts.subscribe)', null, cb)
12
- .then((subId) => {
13
- id = subId;
14
- })
15
- .catch(console.error);
16
- return () => {
17
- id && sendRequest('pub(accounts.unsubscribe)', { id })
18
- .catch(console.error);
19
- };
20
- }
21
- }
@@ -1,13 +0,0 @@
1
- import type { Injected } from '@pezkuwi/extension-inject/types';
2
- import type { SendRequest } from './types.js';
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 implements Injected {
8
- readonly accounts: Accounts;
9
- readonly metadata: Metadata;
10
- readonly provider: PostMessageProvider;
11
- readonly signer: Signer;
12
- constructor(sendRequest: SendRequest);
13
- }
package/page/Injected.js DELETED
@@ -1,21 +0,0 @@
1
- import Accounts from './Accounts.js';
2
- import Metadata from './Metadata.js';
3
- import PostMessageProvider from './PostMessageProvider.js';
4
- import Signer from './Signer.js';
5
- export default class {
6
- accounts;
7
- metadata;
8
- provider;
9
- signer;
10
- constructor(sendRequest) {
11
- this.accounts = new Accounts(sendRequest);
12
- this.metadata = new Metadata(sendRequest);
13
- this.provider = new PostMessageProvider(sendRequest);
14
- this.signer = new Signer(sendRequest);
15
- setInterval(() => {
16
- sendRequest('pub(ping)', null).catch(() => {
17
- console.error('Extension unavailable, ping failed');
18
- });
19
- }, 5_000 + Math.floor(Math.random() * 5_000));
20
- }
21
- }
@@ -1,7 +0,0 @@
1
- import type { InjectedMetadata, InjectedMetadataKnown, MetadataDef } from '@pezkuwi/extension-inject/types';
2
- import type { SendRequest } from './types.js';
3
- export default class Metadata implements InjectedMetadata {
4
- constructor(_sendRequest: SendRequest);
5
- get(): Promise<InjectedMetadataKnown[]>;
6
- provide(definition: MetadataDef): Promise<boolean>;
7
- }
package/page/Metadata.js DELETED
@@ -1,12 +0,0 @@
1
- let sendRequest;
2
- export default class Metadata {
3
- constructor(_sendRequest) {
4
- sendRequest = _sendRequest;
5
- }
6
- get() {
7
- return sendRequest('pub(metadata.list)');
8
- }
9
- provide(definition) {
10
- return sendRequest('pub(metadata.provide)', definition);
11
- }
12
- }
@@ -1,63 +0,0 @@
1
- import type { InjectedProvider, ProviderList, ProviderMeta } from '@pezkuwi/extension-inject/types';
2
- import type { ProviderInterfaceEmitCb, ProviderInterfaceEmitted } from '@pezkuwi/rpc-provider/types';
3
- import type { AnyFunction } from '@pezkuwi/types/types';
4
- import type { SendRequest } from './types.js';
5
- 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
- get isClonable(): boolean;
23
- /**
24
- * @description Returns a clone of the object
25
- */
26
- clone(): PostMessageProvider;
27
- /**
28
- * @description Manually disconnect from the connection, clearing autoconnect logic
29
- */
30
- connect(): Promise<void>;
31
- /**
32
- * @description Manually disconnect from the connection, clearing autoconnect logic
33
- */
34
- disconnect(): Promise<void>;
35
- /**
36
- * @summary `true` when this provider supports subscriptions
37
- */
38
- get hasSubscriptions(): boolean;
39
- /**
40
- * @summary Whether the node is connected or not.
41
- * @return {boolean} true if connected
42
- */
43
- get isConnected(): boolean;
44
- listProviders(): Promise<ProviderList>;
45
- /**
46
- * @summary Listens on events after having subscribed using the [[subscribe]] function.
47
- * @param {ProviderInterfaceEmitted} type Event
48
- * @param {ProviderInterfaceEmitCb} sub Callback
49
- * @return unsubscribe function
50
- */
51
- on(type: ProviderInterfaceEmitted, sub: ProviderInterfaceEmitCb): () => void;
52
- send(method: string, params: unknown[], _?: boolean, subscription?: SubscriptionHandler): Promise<any>;
53
- /**
54
- * @summary Spawn a provider on the extension background.
55
- */
56
- startProvider(key: string): Promise<ProviderMeta>;
57
- subscribe(type: string, method: string, params: unknown[], callback: AnyFunction): Promise<number>;
58
- /**
59
- * @summary Allows unsubscribing to subscriptions made with [[subscribe]].
60
- */
61
- unsubscribe(type: string, method: string, id: number): Promise<boolean>;
62
- }
63
- export {};
@@ -1,132 +0,0 @@
1
- import { EventEmitter } from 'eventemitter3';
2
- import { isUndefined, logger } from '@pezkuwi/util';
3
- const l = logger('PostMessageProvider');
4
- let sendRequest;
5
- /**
6
- * @name PostMessageProvider
7
- *
8
- * @description Extension provider to be used by dapps
9
- */
10
- export default class PostMessageProvider {
11
- #eventemitter;
12
- // Whether or not the actual extension background provider is connected
13
- #isConnected = false;
14
- // Subscription IDs are (historically) not guaranteed to be globally unique;
15
- // only unique for a given subscription method; which is why we identify
16
- // the subscriptions based on subscription id + type
17
- #subscriptions = {}; // {[(type,subscriptionId)]: callback}
18
- /**
19
- * @param {function} sendRequest The function to be called to send requests to the node
20
- * @param {function} subscriptionNotificationHandler Channel for receiving subscription messages
21
- */
22
- constructor(_sendRequest) {
23
- this.#eventemitter = new EventEmitter();
24
- sendRequest = _sendRequest;
25
- }
26
- get isClonable() {
27
- return !!true;
28
- }
29
- /**
30
- * @description Returns a clone of the object
31
- */
32
- clone() {
33
- return new PostMessageProvider(sendRequest);
34
- }
35
- /**
36
- * @description Manually disconnect from the connection, clearing autoconnect logic
37
- */
38
- // eslint-disable-next-line @typescript-eslint/require-await
39
- async connect() {
40
- // FIXME This should see if the extension's state's provider can disconnect
41
- console.error('PostMessageProvider.disconnect() is not implemented.');
42
- }
43
- /**
44
- * @description Manually disconnect from the connection, clearing autoconnect logic
45
- */
46
- // eslint-disable-next-line @typescript-eslint/require-await
47
- async disconnect() {
48
- // FIXME This should see if the extension's state's provider can disconnect
49
- console.error('PostMessageProvider.disconnect() is not implemented.');
50
- }
51
- /**
52
- * @summary `true` when this provider supports subscriptions
53
- */
54
- get hasSubscriptions() {
55
- // FIXME This should see if the extension's state's provider has subscriptions
56
- return !!true;
57
- }
58
- /**
59
- * @summary Whether the node is connected or not.
60
- * @return {boolean} true if connected
61
- */
62
- get isConnected() {
63
- return this.#isConnected;
64
- }
65
- listProviders() {
66
- return sendRequest('pub(rpc.listProviders)', undefined);
67
- }
68
- /**
69
- * @summary Listens on events after having subscribed using the [[subscribe]] function.
70
- * @param {ProviderInterfaceEmitted} type Event
71
- * @param {ProviderInterfaceEmitCb} sub Callback
72
- * @return unsubscribe function
73
- */
74
- on(type, sub) {
75
- this.#eventemitter.on(type, sub);
76
- return () => {
77
- this.#eventemitter.removeListener(type, sub);
78
- };
79
- }
80
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
81
- async send(method, params, _, subscription) {
82
- if (subscription) {
83
- const { callback, type } = subscription;
84
- const id = await sendRequest('pub(rpc.subscribe)', { method, params, type }, (res) => {
85
- subscription.callback(null, res);
86
- });
87
- this.#subscriptions[`${type}::${id}`] = callback;
88
- return id;
89
- }
90
- return sendRequest('pub(rpc.send)', { method, params });
91
- }
92
- /**
93
- * @summary Spawn a provider on the extension background.
94
- */
95
- async startProvider(key) {
96
- // Disconnect from the previous provider
97
- this.#isConnected = false;
98
- this.#eventemitter.emit('disconnected');
99
- const meta = await sendRequest('pub(rpc.startProvider)', key);
100
- // eslint-disable-next-line @typescript-eslint/no-floating-promises
101
- sendRequest('pub(rpc.subscribeConnected)', null, (connected) => {
102
- this.#isConnected = connected;
103
- if (connected) {
104
- this.#eventemitter.emit('connected');
105
- }
106
- else {
107
- this.#eventemitter.emit('disconnected');
108
- }
109
- return true;
110
- });
111
- return meta;
112
- }
113
- subscribe(type, method, params, callback) {
114
- return this.send(method, params, false, { callback, type });
115
- }
116
- /**
117
- * @summary Allows unsubscribing to subscriptions made with [[subscribe]].
118
- */
119
- async unsubscribe(type, method, id) {
120
- const subscription = `${type}::${id}`;
121
- // FIXME This now could happen with re-subscriptions. The issue is that with a re-sub
122
- // the assigned id now does not match what the API user originally received. It has
123
- // a slight complication in solving - since we cannot rely on the send id, but rather
124
- // need to find the actual subscription id to map it
125
- if (isUndefined(this.#subscriptions[subscription])) {
126
- l.debug(() => `Unable to find active subscription=${subscription}`);
127
- return false;
128
- }
129
- delete this.#subscriptions[subscription];
130
- return this.send(method, [id]);
131
- }
132
- }
package/page/Signer.d.ts DELETED
@@ -1,8 +0,0 @@
1
- import type { Signer as SignerInterface, SignerResult } from '@pezkuwi/api/types';
2
- import type { SignerPayloadJSON, SignerPayloadRaw } from '@pezkuwi/types/types';
3
- import type { SendRequest } from './types.js';
4
- export default class Signer implements SignerInterface {
5
- constructor(_sendRequest: SendRequest);
6
- signPayload(payload: SignerPayloadJSON): Promise<SignerResult>;
7
- signRaw(payload: SignerPayloadRaw): Promise<SignerResult>;
8
- }
package/page/Signer.js DELETED
@@ -1,26 +0,0 @@
1
- let sendRequest;
2
- let nextId = 0;
3
- export default class Signer {
4
- constructor(_sendRequest) {
5
- sendRequest = _sendRequest;
6
- }
7
- async signPayload(payload) {
8
- const id = ++nextId;
9
- const result = await sendRequest('pub(extrinsic.sign)', payload);
10
- // we add an internal id (number) - should have a mapping from the
11
- // extension id (string) -> internal id (number) if we wish to provide
12
- // updated via the update functionality (noop at this point)
13
- return {
14
- ...result,
15
- id
16
- };
17
- }
18
- async signRaw(payload) {
19
- const id = ++nextId;
20
- const result = await sendRequest('pub(bytes.sign)', payload);
21
- return {
22
- ...result,
23
- id
24
- };
25
- }
26
- }
package/page/index.d.ts DELETED
@@ -1,16 +0,0 @@
1
- import type { MessageTypes, MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes, TransportResponseMessage } from '../background/types.js';
2
- import Injected from './Injected.js';
3
- export interface Handler {
4
- resolve: (data?: any) => void;
5
- reject: (error: Error) => void;
6
- subscriber?: (data: any) => void;
7
- }
8
- export 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;
package/page/index.js DELETED
@@ -1,45 +0,0 @@
1
- import { MESSAGE_ORIGIN_PAGE } from '../defaults.js';
2
- import { getId } from '../utils/getId.js';
3
- import Injected from './Injected.js';
4
- const handlers = {};
5
- export function sendMessage(message, request, subscriber) {
6
- return new Promise((resolve, reject) => {
7
- const id = getId();
8
- handlers[id] = { reject, resolve, subscriber };
9
- const transportRequestMessage = {
10
- id,
11
- message,
12
- origin: MESSAGE_ORIGIN_PAGE,
13
- request: request || null
14
- };
15
- window.postMessage(transportRequestMessage, '*');
16
- });
17
- }
18
- export async function enable(origin) {
19
- await sendMessage('pub(authorize.tab)', { origin });
20
- return new Injected(sendMessage);
21
- }
22
- export async function redirectIfPhishing() {
23
- const res = await sendMessage('pub(phishing.redirectIfDenied)');
24
- return res;
25
- }
26
- export function handleResponse(data) {
27
- const handler = handlers[data.id];
28
- if (!handler) {
29
- console.error(`Unknown response: ${JSON.stringify(data)}`);
30
- return;
31
- }
32
- if (!handler.subscriber) {
33
- delete handlers[data.id];
34
- }
35
- if (data.subscription) {
36
- // eslint-disable-next-line @typescript-eslint/ban-types
37
- handler.subscriber(data.subscription);
38
- }
39
- else if (data.error) {
40
- handler.reject(new Error(data.error));
41
- }
42
- else {
43
- handler.resolve(data.response);
44
- }
45
- }
package/page/types.d.ts DELETED
@@ -1,6 +0,0 @@
1
- import type { MessageTypesWithNoSubscriptions, MessageTypesWithNullRequest, MessageTypesWithSubscriptions, RequestTypes, ResponseTypes, SubscriptionMessageTypes } from '../background/types.js';
2
- export interface SendRequest {
3
- <TMessageType extends MessageTypesWithNullRequest>(message: TMessageType): Promise<ResponseTypes[TMessageType]>;
4
- <TMessageType extends MessageTypesWithNoSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType]): Promise<ResponseTypes[TMessageType]>;
5
- <TMessageType extends MessageTypesWithSubscriptions>(message: TMessageType, request: RequestTypes[TMessageType], subscriber: (data: SubscriptionMessageTypes[TMessageType]) => void): Promise<ResponseTypes[TMessageType]>;
6
- }
package/page/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,6 +0,0 @@
1
- import type { KeyringJson, KeyringStore } from '@pezkuwi/ui-keyring/types';
2
- import BaseStore from './Base.js';
3
- export default class AccountsStore extends BaseStore<KeyringJson> implements KeyringStore {
4
- constructor();
5
- set(key: string, value: KeyringJson, update?: () => void): Promise<void>;
6
- }
@@ -1,17 +0,0 @@
1
- import { EXTENSION_PREFIX } from '../defaults.js';
2
- import BaseStore from './Base.js';
3
- export default class AccountsStore extends BaseStore {
4
- constructor() {
5
- super(EXTENSION_PREFIX && EXTENSION_PREFIX !== 'polkadot{.js}'
6
- ? `${EXTENSION_PREFIX}accounts`
7
- : null);
8
- }
9
- async set(key, value, update) {
10
- // shortcut, don't save testing accounts in extension storage
11
- if (key.startsWith('account:') && value.meta && value.meta.isTesting) {
12
- update && update();
13
- return;
14
- }
15
- await super.set(key, value, update);
16
- }
17
- }
package/stores/Base.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export default abstract class BaseStore<T> {
2
- #private;
3
- constructor(prefix: string | null);
4
- all(update: (key: string, value: T) => void): Promise<void>;
5
- allMap(update: (value: Record<string, T>) => Promise<void>): Promise<void>;
6
- get(key: string, update: (value: T) => void): Promise<void>;
7
- remove(key: string, update?: () => void): Promise<void>;
8
- set(key: string, value: T, update?: () => void): Promise<void>;
9
- }
package/stores/Base.js DELETED
@@ -1,67 +0,0 @@
1
- const lastError = (type) => {
2
- const error = chrome.runtime.lastError;
3
- if (error) {
4
- console.error(`BaseStore.${type}:: runtime.lastError:`, error);
5
- }
6
- };
7
- export default class BaseStore {
8
- #prefix;
9
- constructor(prefix) {
10
- this.#prefix = prefix ? `${prefix}:` : '';
11
- }
12
- async all(update) {
13
- await this.allMap(async (map) => {
14
- const entries = Object.entries(map);
15
- for (const [key, value] of entries) {
16
- // eslint-disable-next-line @typescript-eslint/await-thenable
17
- await update(key, value);
18
- }
19
- });
20
- }
21
- async allMap(update) {
22
- await chrome.storage.local.get(null).then(async (result) => {
23
- lastError('all');
24
- const entries = Object.entries(result);
25
- const map = {};
26
- for (let i = 0, count = entries.length; i < count; i++) {
27
- const [key, value] = entries[i];
28
- if (key.startsWith(this.#prefix)) {
29
- map[key.replace(this.#prefix, '')] = value;
30
- }
31
- }
32
- await update(map);
33
- }).catch(({ message }) => {
34
- console.error(`BaseStore error within allMap: ${message}`);
35
- });
36
- }
37
- async get(key, update) {
38
- const prefixedKey = `${this.#prefix}${key}`;
39
- await chrome.storage.local.get([prefixedKey]).then(async (result) => {
40
- lastError('get');
41
- // eslint-disable-next-line @typescript-eslint/await-thenable
42
- await update(result[prefixedKey]);
43
- }).catch(({ message }) => {
44
- console.error(`BaseStore error within get: ${message}`);
45
- });
46
- }
47
- async remove(key, update) {
48
- const prefixedKey = `${this.#prefix}${key}`;
49
- await chrome.storage.local.remove(prefixedKey).then(async () => {
50
- lastError('remove');
51
- // eslint-disable-next-line @typescript-eslint/await-thenable
52
- update && await update();
53
- }).catch(({ message }) => {
54
- console.error(`BaseStore error within remove: ${message}`);
55
- });
56
- }
57
- async set(key, value, update) {
58
- const prefixedKey = `${this.#prefix}${key}`;
59
- await chrome.storage.local.set({ [prefixedKey]: value }).then(async () => {
60
- lastError('set');
61
- // eslint-disable-next-line @typescript-eslint/await-thenable
62
- update && await update();
63
- }).catch(({ message }) => {
64
- console.error(`BaseStore error within set: ${message}`);
65
- });
66
- }
67
- }
@@ -1,5 +0,0 @@
1
- import type { MetadataDef } from '@pezkuwi/extension-inject/types';
2
- import BaseStore from './Base.js';
3
- export default class MetadataStore extends BaseStore<MetadataDef> {
4
- constructor();
5
- }
@@ -1,9 +0,0 @@
1
- import { EXTENSION_PREFIX } from '../defaults.js';
2
- import BaseStore from './Base.js';
3
- export default class MetadataStore extends BaseStore {
4
- constructor() {
5
- super(EXTENSION_PREFIX && EXTENSION_PREFIX !== 'polkadot{.js}'
6
- ? `${EXTENSION_PREFIX}metadata`
7
- : 'metadata');
8
- }
9
- }
package/stores/index.d.ts DELETED
@@ -1,2 +0,0 @@
1
- export { default as AccountsStore } from './Accounts.js';
2
- export { default as MetadataStore } from './Metadata.js';
package/types.d.ts DELETED
@@ -1,9 +0,0 @@
1
- export interface Message extends MessageEvent {
2
- data: {
3
- error?: string;
4
- id: string;
5
- origin: string;
6
- response?: string;
7
- subscription?: string;
8
- };
9
- }
package/types.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,2 +0,0 @@
1
- import type { KeypairType } from '@pezkuwi/util-crypto/types';
2
- export declare function canDerive(type?: KeypairType): boolean;
@@ -1,3 +0,0 @@
1
- export function canDerive(type) {
2
- return !!type && ['ed25519', 'sr25519', 'ecdsa', 'ethereum'].includes(type);
3
- }
package/utils/getId.d.ts DELETED
@@ -1 +0,0 @@
1
- export declare function getId(): string;
package/utils/getId.js DELETED
@@ -1,5 +0,0 @@
1
- import { EXTENSION_PREFIX } from '../defaults.js';
2
- let counter = 0;
3
- export function getId() {
4
- return `${EXTENSION_PREFIX}.${Date.now()}.${++counter}`;
5
- }
package/utils/index.d.ts DELETED
@@ -1 +0,0 @@
1
- export { canDerive } from './canDerive.js';
package/utils/index.js DELETED
@@ -1 +0,0 @@
1
- export { canDerive } from './canDerive.js';