@pezkuwi/extension-dapp 0.62.14 → 0.62.17
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.
- package/build/README.md +3 -0
- package/build/bundle.js +232 -0
- package/build/cjs/bundle.d.ts +61 -0
- package/build/cjs/bundle.js +246 -0
- package/build/cjs/index.js +4 -0
- package/build/cjs/packageInfo.js +4 -0
- package/build/cjs/util.js +13 -0
- package/build/cjs/wrapBytes.js +10 -0
- package/build/index.d.ts +1 -0
- package/build/index.js +1 -0
- package/build/package.json +142 -0
- package/build/packageDetect.d.ts +1 -0
- package/build/packageDetect.js +4 -0
- package/build/packageInfo.d.ts +6 -0
- package/build/packageInfo.js +1 -0
- package/build/util.d.ts +1 -0
- package/build/util.js +10 -0
- package/build/wrapBytes.d.ts +7 -0
- package/build/wrapBytes.js +7 -0
- package/build-tsc-cjs/packageDetect.js +6 -0
- package/{cjs → build-tsc-cjs}/packageInfo.js +1 -1
- package/{packageInfo.js → build-tsc-esm/packageInfo.js} +1 -1
- package/package.json +10 -115
- package/src/bundle.ts +319 -0
- package/src/index.ts +7 -0
- package/src/packageDetect.ts +12 -0
- package/src/packageInfo.ts +6 -0
- package/src/util.ts +12 -0
- package/src/wrapBytes.spec.ts +137 -0
- package/src/wrapBytes.ts +12 -0
- package/tsconfig.build.json +14 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.spec.json +16 -0
- package/tsconfig.spec.tsbuildinfo +1 -0
- package/bundle-pezkuwi-extension-dapp.js +0 -205
- /package/{LICENSE → build/LICENSE} +0 -0
- /package/{cjs → build}/bundle.d.ts +0 -0
- /package/{index.d.ts → build/cjs/index.d.ts} +0 -0
- /package/{cjs → build/cjs}/package.json +0 -0
- /package/{packageDetect.d.ts → build/cjs/packageDetect.d.ts} +0 -0
- /package/{cjs → build/cjs}/packageDetect.js +0 -0
- /package/{packageInfo.d.ts → build/cjs/packageInfo.d.ts} +0 -0
- /package/{util.d.ts → build/cjs/util.d.ts} +0 -0
- /package/{wrapBytes.d.ts → build/cjs/wrapBytes.d.ts} +0 -0
- /package/{bundle.d.ts → build-tsc/bundle.d.ts} +0 -0
- /package/{cjs → build-tsc}/index.d.ts +0 -0
- /package/{cjs → build-tsc}/packageDetect.d.ts +0 -0
- /package/{cjs → build-tsc}/packageInfo.d.ts +0 -0
- /package/{cjs → build-tsc}/util.d.ts +0 -0
- /package/{cjs → build-tsc}/wrapBytes.d.ts +0 -0
- /package/{cjs → build-tsc-cjs}/bundle.js +0 -0
- /package/{cjs → build-tsc-cjs}/index.js +0 -0
- /package/{cjs → build-tsc-cjs}/util.js +0 -0
- /package/{cjs → build-tsc-cjs}/wrapBytes.js +0 -0
- /package/{bundle.js → build-tsc-esm/bundle.js} +0 -0
- /package/{index.js → build-tsc-esm/index.js} +0 -0
- /package/{packageDetect.js → build-tsc-esm/packageDetect.js} +0 -0
- /package/{util.js → build-tsc-esm/util.js} +0 -0
- /package/{wrapBytes.js → build-tsc-esm/wrapBytes.js} +0 -0
package/build/README.md
ADDED
package/build/bundle.js
ADDED
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { isPromise, objectSpread, u8aEq } from '@pezkuwi/util';
|
|
2
|
+
import { decodeAddress, encodeAddress } from '@pezkuwi/util-crypto';
|
|
3
|
+
import { documentReadyPromise } from './util.js';
|
|
4
|
+
export { packageInfo } from './packageInfo.js';
|
|
5
|
+
export { unwrapBytes, wrapBytes } from './wrapBytes.js';
|
|
6
|
+
const win = window;
|
|
7
|
+
win.injectedWeb3 = win.injectedWeb3 || {};
|
|
8
|
+
let isWeb3Injected = web3IsInjected();
|
|
9
|
+
let web3EnablePromise = null;
|
|
10
|
+
export { isWeb3Injected, web3EnablePromise };
|
|
11
|
+
/** @internal true when anything has been injected and is available */
|
|
12
|
+
function web3IsInjected() {
|
|
13
|
+
return Object
|
|
14
|
+
.values(win.injectedWeb3)
|
|
15
|
+
.filter(({ connect, enable }) => !!(connect || enable))
|
|
16
|
+
.length !== 0;
|
|
17
|
+
}
|
|
18
|
+
/** @internal throw a consistent error when not extensions have not been enabled */
|
|
19
|
+
function throwError(method) {
|
|
20
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
21
|
+
}
|
|
22
|
+
/** @internal map from Array<InjectedAccount> to Array<InjectedAccountWithMeta> */
|
|
23
|
+
function mapAccounts(source, list, ss58Format) {
|
|
24
|
+
return list.map(({ address, genesisHash, name, type }) => ({
|
|
25
|
+
address: address.length === 42
|
|
26
|
+
? address
|
|
27
|
+
: encodeAddress(decodeAddress(address), ss58Format),
|
|
28
|
+
meta: { genesisHash, name, source },
|
|
29
|
+
type
|
|
30
|
+
}));
|
|
31
|
+
}
|
|
32
|
+
/** @internal filter accounts based on genesisHash and type of account */
|
|
33
|
+
function filterAccounts(list, genesisHash, type) {
|
|
34
|
+
return list.filter((a) => (!a.type || !type || type.includes(a.type)) &&
|
|
35
|
+
(!a.genesisHash || !genesisHash || a.genesisHash === genesisHash));
|
|
36
|
+
}
|
|
37
|
+
/** @internal retrieves all the extensions available on the window */
|
|
38
|
+
function getWindowExtensions(originName) {
|
|
39
|
+
return Promise
|
|
40
|
+
.all(Object
|
|
41
|
+
.entries(win.injectedWeb3)
|
|
42
|
+
.map(([nameOrHash, { connect, enable, version }]) => Promise
|
|
43
|
+
.resolve()
|
|
44
|
+
.then(() => connect
|
|
45
|
+
// new style, returning all info
|
|
46
|
+
? connect(originName)
|
|
47
|
+
: enable
|
|
48
|
+
// previous interface, leakages on name/version
|
|
49
|
+
? enable(originName).then((e) => objectSpread({ name: nameOrHash, version: version || 'unknown' }, e))
|
|
50
|
+
: Promise.reject(new Error('No connect(..) or enable(...) hook found')))
|
|
51
|
+
.catch(({ message }) => {
|
|
52
|
+
console.error(`Error initializing ${nameOrHash}: ${message}`);
|
|
53
|
+
})))
|
|
54
|
+
.then((exts) => exts.filter((e) => !!e));
|
|
55
|
+
}
|
|
56
|
+
/** @internal Ensure the enable promise is resolved and filter by extensions */
|
|
57
|
+
async function filterEnable(caller, extensions) {
|
|
58
|
+
if (!web3EnablePromise) {
|
|
59
|
+
return throwError(caller);
|
|
60
|
+
}
|
|
61
|
+
const sources = await web3EnablePromise;
|
|
62
|
+
return sources.filter(({ name }) => !extensions ||
|
|
63
|
+
extensions.includes(name));
|
|
64
|
+
}
|
|
65
|
+
/**
|
|
66
|
+
* @summary Enables all the providers found on the injected window interface
|
|
67
|
+
* @description
|
|
68
|
+
* Enables all injected extensions that has been found on the page. This
|
|
69
|
+
* should be called before making use of any other web3* functions.
|
|
70
|
+
*/
|
|
71
|
+
export function web3Enable(originName, compatInits = []) {
|
|
72
|
+
if (!originName) {
|
|
73
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
74
|
+
}
|
|
75
|
+
const initCompat = compatInits.length
|
|
76
|
+
? Promise.all(compatInits.map((c) => c().catch(() => false)))
|
|
77
|
+
: Promise.resolve([true]);
|
|
78
|
+
web3EnablePromise = documentReadyPromise(() => initCompat.then(() => getWindowExtensions(originName)
|
|
79
|
+
.then((values) => values.map((e) => {
|
|
80
|
+
// if we don't have an accounts subscriber, add a single-shot version
|
|
81
|
+
if (!e.accounts.subscribe) {
|
|
82
|
+
e.accounts.subscribe = (cb) => {
|
|
83
|
+
e.accounts
|
|
84
|
+
.get()
|
|
85
|
+
.then(cb)
|
|
86
|
+
.catch(console.error);
|
|
87
|
+
return () => {
|
|
88
|
+
// no ubsubscribe needed, this is a single-shot
|
|
89
|
+
};
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
return e;
|
|
93
|
+
}))
|
|
94
|
+
.catch(() => [])
|
|
95
|
+
.then((values) => {
|
|
96
|
+
const names = values.map(({ name, version }) => `${name}/${version}`);
|
|
97
|
+
isWeb3Injected = web3IsInjected();
|
|
98
|
+
console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
99
|
+
return values;
|
|
100
|
+
})));
|
|
101
|
+
return web3EnablePromise;
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* @summary Retrieves all the accounts across all providers
|
|
105
|
+
* @description
|
|
106
|
+
* This returns the full list of account available (across all extensions) to
|
|
107
|
+
* the page. Filtering options are available of a per-extension, per type and
|
|
108
|
+
* per-genesisHash basis. Optionally the accounts can be encoded with the provided
|
|
109
|
+
* ss58Format
|
|
110
|
+
*/
|
|
111
|
+
export async function web3Accounts({ accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
112
|
+
const accounts = [];
|
|
113
|
+
const sources = await filterEnable('web3Accounts', extensions);
|
|
114
|
+
const retrieved = await Promise.all(sources.map(async ({ accounts, name: source }) => {
|
|
115
|
+
try {
|
|
116
|
+
const list = await accounts.get();
|
|
117
|
+
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
118
|
+
}
|
|
119
|
+
catch {
|
|
120
|
+
// cannot handle this one
|
|
121
|
+
return [];
|
|
122
|
+
}
|
|
123
|
+
}));
|
|
124
|
+
retrieved.forEach((result) => {
|
|
125
|
+
accounts.push(...result);
|
|
126
|
+
});
|
|
127
|
+
console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
|
|
128
|
+
return accounts;
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* @summary Subscribes to all the accounts across all providers
|
|
132
|
+
* @description
|
|
133
|
+
* This is the subscription version of the web3Accounts interface with
|
|
134
|
+
* updates as to when new accounts do become available. The list of filtering
|
|
135
|
+
* options are the same as for the web3Accounts interface.
|
|
136
|
+
*/
|
|
137
|
+
export async function web3AccountsSubscribe(cb, { accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
138
|
+
const sources = await filterEnable('web3AccountsSubscribe', extensions);
|
|
139
|
+
const accounts = {};
|
|
140
|
+
const triggerUpdate = () => cb(Object
|
|
141
|
+
.entries(accounts)
|
|
142
|
+
.reduce((result, [source, list]) => {
|
|
143
|
+
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
|
144
|
+
return result;
|
|
145
|
+
}, []));
|
|
146
|
+
const unsubs = sources.map(({ accounts: { subscribe }, name: source }) => subscribe((result) => {
|
|
147
|
+
accounts[source] = result;
|
|
148
|
+
try {
|
|
149
|
+
const result = triggerUpdate();
|
|
150
|
+
if (result && isPromise(result)) {
|
|
151
|
+
result.catch(console.error);
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
catch (error) {
|
|
155
|
+
console.error(error);
|
|
156
|
+
}
|
|
157
|
+
}));
|
|
158
|
+
return () => {
|
|
159
|
+
unsubs.forEach((unsub) => {
|
|
160
|
+
unsub();
|
|
161
|
+
});
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @summary Finds a specific provider based on the name
|
|
166
|
+
* @description
|
|
167
|
+
* This retrieves a specific source (extension) based on the name. In most
|
|
168
|
+
* cases it should not be needed to call it directly (e.g. it is used internally
|
|
169
|
+
* by calls such as web3FromAddress) but would allow operation on a specific
|
|
170
|
+
* known extension.
|
|
171
|
+
*/
|
|
172
|
+
export async function web3FromSource(source) {
|
|
173
|
+
if (!web3EnablePromise) {
|
|
174
|
+
return throwError('web3FromSource');
|
|
175
|
+
}
|
|
176
|
+
const sources = await web3EnablePromise;
|
|
177
|
+
const found = source && sources.find(({ name }) => name === source);
|
|
178
|
+
if (!found) {
|
|
179
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
180
|
+
}
|
|
181
|
+
return found;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* @summary Find a specific provider that provides a specific address
|
|
185
|
+
* @description
|
|
186
|
+
* Based on an address, return the provider that has makes this address
|
|
187
|
+
* available to the page.
|
|
188
|
+
*/
|
|
189
|
+
export async function web3FromAddress(address) {
|
|
190
|
+
if (!web3EnablePromise) {
|
|
191
|
+
return throwError('web3FromAddress');
|
|
192
|
+
}
|
|
193
|
+
const accounts = await web3Accounts();
|
|
194
|
+
let found;
|
|
195
|
+
if (address) {
|
|
196
|
+
const accountU8a = decodeAddress(address);
|
|
197
|
+
found = accounts.find((account) => u8aEq(decodeAddress(account.address), accountU8a));
|
|
198
|
+
}
|
|
199
|
+
if (!found) {
|
|
200
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
201
|
+
}
|
|
202
|
+
return web3FromSource(found.meta.source);
|
|
203
|
+
}
|
|
204
|
+
/**
|
|
205
|
+
* @summary List all providers exposed by one source
|
|
206
|
+
* @description
|
|
207
|
+
* For extensions that supply RPC providers, this call would return the list
|
|
208
|
+
* of RPC providers that any extension may supply.
|
|
209
|
+
*/
|
|
210
|
+
export async function web3ListRpcProviders(source) {
|
|
211
|
+
const { provider } = await web3FromSource(source);
|
|
212
|
+
if (!provider) {
|
|
213
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
214
|
+
return null;
|
|
215
|
+
}
|
|
216
|
+
return provider.listProviders();
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @summary Start an RPC provider provider by a specific source
|
|
220
|
+
* @description
|
|
221
|
+
* For extensions that supply RPC providers, this call would return an
|
|
222
|
+
* enabled provider (initialized with the specific key) from the
|
|
223
|
+
* specified extension source.
|
|
224
|
+
*/
|
|
225
|
+
export async function web3UseRpcProvider(source, key) {
|
|
226
|
+
const { provider } = await web3FromSource(source);
|
|
227
|
+
if (!provider) {
|
|
228
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
229
|
+
}
|
|
230
|
+
const meta = await provider.startProvider(key);
|
|
231
|
+
return { meta, provider };
|
|
232
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import type { InjectedAccountWithMeta, InjectedExtension, InjectedProviderWithMeta, ProviderList, Unsubcall, Web3AccountsOptions } from '@pezkuwi/extension-inject/types';
|
|
2
|
+
export { packageInfo } from './packageInfo.js';
|
|
3
|
+
export { unwrapBytes, wrapBytes } from './wrapBytes.js';
|
|
4
|
+
declare let isWeb3Injected: boolean;
|
|
5
|
+
declare let web3EnablePromise: Promise<InjectedExtension[]> | null;
|
|
6
|
+
export { isWeb3Injected, web3EnablePromise };
|
|
7
|
+
/**
|
|
8
|
+
* @summary Enables all the providers found on the injected window interface
|
|
9
|
+
* @description
|
|
10
|
+
* Enables all injected extensions that has been found on the page. This
|
|
11
|
+
* should be called before making use of any other web3* functions.
|
|
12
|
+
*/
|
|
13
|
+
export declare function web3Enable(originName: string, compatInits?: (() => Promise<boolean>)[]): Promise<InjectedExtension[]>;
|
|
14
|
+
/**
|
|
15
|
+
* @summary Retrieves all the accounts across all providers
|
|
16
|
+
* @description
|
|
17
|
+
* This returns the full list of account available (across all extensions) to
|
|
18
|
+
* the page. Filtering options are available of a per-extension, per type and
|
|
19
|
+
* per-genesisHash basis. Optionally the accounts can be encoded with the provided
|
|
20
|
+
* ss58Format
|
|
21
|
+
*/
|
|
22
|
+
export declare function web3Accounts({ accountType, extensions, genesisHash, ss58Format }?: Web3AccountsOptions): Promise<InjectedAccountWithMeta[]>;
|
|
23
|
+
/**
|
|
24
|
+
* @summary Subscribes to all the accounts across all providers
|
|
25
|
+
* @description
|
|
26
|
+
* This is the subscription version of the web3Accounts interface with
|
|
27
|
+
* updates as to when new accounts do become available. The list of filtering
|
|
28
|
+
* options are the same as for the web3Accounts interface.
|
|
29
|
+
*/
|
|
30
|
+
export declare function web3AccountsSubscribe(cb: (accounts: InjectedAccountWithMeta[]) => void | Promise<void>, { accountType, extensions, genesisHash, ss58Format }?: Web3AccountsOptions): Promise<Unsubcall>;
|
|
31
|
+
/**
|
|
32
|
+
* @summary Finds a specific provider based on the name
|
|
33
|
+
* @description
|
|
34
|
+
* This retrieves a specific source (extension) based on the name. In most
|
|
35
|
+
* cases it should not be needed to call it directly (e.g. it is used internally
|
|
36
|
+
* by calls such as web3FromAddress) but would allow operation on a specific
|
|
37
|
+
* known extension.
|
|
38
|
+
*/
|
|
39
|
+
export declare function web3FromSource(source: string): Promise<InjectedExtension>;
|
|
40
|
+
/**
|
|
41
|
+
* @summary Find a specific provider that provides a specific address
|
|
42
|
+
* @description
|
|
43
|
+
* Based on an address, return the provider that has makes this address
|
|
44
|
+
* available to the page.
|
|
45
|
+
*/
|
|
46
|
+
export declare function web3FromAddress(address: string): Promise<InjectedExtension>;
|
|
47
|
+
/**
|
|
48
|
+
* @summary List all providers exposed by one source
|
|
49
|
+
* @description
|
|
50
|
+
* For extensions that supply RPC providers, this call would return the list
|
|
51
|
+
* of RPC providers that any extension may supply.
|
|
52
|
+
*/
|
|
53
|
+
export declare function web3ListRpcProviders(source: string): Promise<ProviderList | null>;
|
|
54
|
+
/**
|
|
55
|
+
* @summary Start an RPC provider provider by a specific source
|
|
56
|
+
* @description
|
|
57
|
+
* For extensions that supply RPC providers, this call would return an
|
|
58
|
+
* enabled provider (initialized with the specific key) from the
|
|
59
|
+
* specified extension source.
|
|
60
|
+
*/
|
|
61
|
+
export declare function web3UseRpcProvider(source: string, key: string): Promise<InjectedProviderWithMeta>;
|
|
@@ -0,0 +1,246 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.web3EnablePromise = exports.isWeb3Injected = exports.wrapBytes = exports.unwrapBytes = exports.packageInfo = void 0;
|
|
4
|
+
exports.web3Enable = web3Enable;
|
|
5
|
+
exports.web3Accounts = web3Accounts;
|
|
6
|
+
exports.web3AccountsSubscribe = web3AccountsSubscribe;
|
|
7
|
+
exports.web3FromSource = web3FromSource;
|
|
8
|
+
exports.web3FromAddress = web3FromAddress;
|
|
9
|
+
exports.web3ListRpcProviders = web3ListRpcProviders;
|
|
10
|
+
exports.web3UseRpcProvider = web3UseRpcProvider;
|
|
11
|
+
const util_1 = require("@pezkuwi/util");
|
|
12
|
+
const util_crypto_1 = require("@pezkuwi/util-crypto");
|
|
13
|
+
const util_js_1 = require("./util.js");
|
|
14
|
+
var packageInfo_js_1 = require("./packageInfo.js");
|
|
15
|
+
Object.defineProperty(exports, "packageInfo", { enumerable: true, get: function () { return packageInfo_js_1.packageInfo; } });
|
|
16
|
+
var wrapBytes_js_1 = require("./wrapBytes.js");
|
|
17
|
+
Object.defineProperty(exports, "unwrapBytes", { enumerable: true, get: function () { return wrapBytes_js_1.unwrapBytes; } });
|
|
18
|
+
Object.defineProperty(exports, "wrapBytes", { enumerable: true, get: function () { return wrapBytes_js_1.wrapBytes; } });
|
|
19
|
+
const win = window;
|
|
20
|
+
win.injectedWeb3 = win.injectedWeb3 || {};
|
|
21
|
+
let isWeb3Injected = web3IsInjected();
|
|
22
|
+
exports.isWeb3Injected = isWeb3Injected;
|
|
23
|
+
let web3EnablePromise = null;
|
|
24
|
+
exports.web3EnablePromise = web3EnablePromise;
|
|
25
|
+
/** @internal true when anything has been injected and is available */
|
|
26
|
+
function web3IsInjected() {
|
|
27
|
+
return Object
|
|
28
|
+
.values(win.injectedWeb3)
|
|
29
|
+
.filter(({ connect, enable }) => !!(connect || enable))
|
|
30
|
+
.length !== 0;
|
|
31
|
+
}
|
|
32
|
+
/** @internal throw a consistent error when not extensions have not been enabled */
|
|
33
|
+
function throwError(method) {
|
|
34
|
+
throw new Error(`${method}: web3Enable(originName) needs to be called before ${method}`);
|
|
35
|
+
}
|
|
36
|
+
/** @internal map from Array<InjectedAccount> to Array<InjectedAccountWithMeta> */
|
|
37
|
+
function mapAccounts(source, list, ss58Format) {
|
|
38
|
+
return list.map(({ address, genesisHash, name, type }) => ({
|
|
39
|
+
address: address.length === 42
|
|
40
|
+
? address
|
|
41
|
+
: (0, util_crypto_1.encodeAddress)((0, util_crypto_1.decodeAddress)(address), ss58Format),
|
|
42
|
+
meta: { genesisHash, name, source },
|
|
43
|
+
type
|
|
44
|
+
}));
|
|
45
|
+
}
|
|
46
|
+
/** @internal filter accounts based on genesisHash and type of account */
|
|
47
|
+
function filterAccounts(list, genesisHash, type) {
|
|
48
|
+
return list.filter((a) => (!a.type || !type || type.includes(a.type)) &&
|
|
49
|
+
(!a.genesisHash || !genesisHash || a.genesisHash === genesisHash));
|
|
50
|
+
}
|
|
51
|
+
/** @internal retrieves all the extensions available on the window */
|
|
52
|
+
function getWindowExtensions(originName) {
|
|
53
|
+
return Promise
|
|
54
|
+
.all(Object
|
|
55
|
+
.entries(win.injectedWeb3)
|
|
56
|
+
.map(([nameOrHash, { connect, enable, version }]) => Promise
|
|
57
|
+
.resolve()
|
|
58
|
+
.then(() => connect
|
|
59
|
+
// new style, returning all info
|
|
60
|
+
? connect(originName)
|
|
61
|
+
: enable
|
|
62
|
+
// previous interface, leakages on name/version
|
|
63
|
+
? enable(originName).then((e) => (0, util_1.objectSpread)({ name: nameOrHash, version: version || 'unknown' }, e))
|
|
64
|
+
: Promise.reject(new Error('No connect(..) or enable(...) hook found')))
|
|
65
|
+
.catch(({ message }) => {
|
|
66
|
+
console.error(`Error initializing ${nameOrHash}: ${message}`);
|
|
67
|
+
})))
|
|
68
|
+
.then((exts) => exts.filter((e) => !!e));
|
|
69
|
+
}
|
|
70
|
+
/** @internal Ensure the enable promise is resolved and filter by extensions */
|
|
71
|
+
async function filterEnable(caller, extensions) {
|
|
72
|
+
if (!web3EnablePromise) {
|
|
73
|
+
return throwError(caller);
|
|
74
|
+
}
|
|
75
|
+
const sources = await web3EnablePromise;
|
|
76
|
+
return sources.filter(({ name }) => !extensions ||
|
|
77
|
+
extensions.includes(name));
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* @summary Enables all the providers found on the injected window interface
|
|
81
|
+
* @description
|
|
82
|
+
* Enables all injected extensions that has been found on the page. This
|
|
83
|
+
* should be called before making use of any other web3* functions.
|
|
84
|
+
*/
|
|
85
|
+
function web3Enable(originName, compatInits = []) {
|
|
86
|
+
if (!originName) {
|
|
87
|
+
throw new Error('You must pass a name for your app to the web3Enable function');
|
|
88
|
+
}
|
|
89
|
+
const initCompat = compatInits.length
|
|
90
|
+
? Promise.all(compatInits.map((c) => c().catch(() => false)))
|
|
91
|
+
: Promise.resolve([true]);
|
|
92
|
+
exports.web3EnablePromise = web3EnablePromise = (0, util_js_1.documentReadyPromise)(() => initCompat.then(() => getWindowExtensions(originName)
|
|
93
|
+
.then((values) => values.map((e) => {
|
|
94
|
+
// if we don't have an accounts subscriber, add a single-shot version
|
|
95
|
+
if (!e.accounts.subscribe) {
|
|
96
|
+
e.accounts.subscribe = (cb) => {
|
|
97
|
+
e.accounts
|
|
98
|
+
.get()
|
|
99
|
+
.then(cb)
|
|
100
|
+
.catch(console.error);
|
|
101
|
+
return () => {
|
|
102
|
+
// no ubsubscribe needed, this is a single-shot
|
|
103
|
+
};
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
return e;
|
|
107
|
+
}))
|
|
108
|
+
.catch(() => [])
|
|
109
|
+
.then((values) => {
|
|
110
|
+
const names = values.map(({ name, version }) => `${name}/${version}`);
|
|
111
|
+
exports.isWeb3Injected = isWeb3Injected = web3IsInjected();
|
|
112
|
+
console.info(`web3Enable: Enabled ${values.length} extension${values.length !== 1 ? 's' : ''}: ${names.join(', ')}`);
|
|
113
|
+
return values;
|
|
114
|
+
})));
|
|
115
|
+
return web3EnablePromise;
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* @summary Retrieves all the accounts across all providers
|
|
119
|
+
* @description
|
|
120
|
+
* This returns the full list of account available (across all extensions) to
|
|
121
|
+
* the page. Filtering options are available of a per-extension, per type and
|
|
122
|
+
* per-genesisHash basis. Optionally the accounts can be encoded with the provided
|
|
123
|
+
* ss58Format
|
|
124
|
+
*/
|
|
125
|
+
async function web3Accounts({ accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
126
|
+
const accounts = [];
|
|
127
|
+
const sources = await filterEnable('web3Accounts', extensions);
|
|
128
|
+
const retrieved = await Promise.all(sources.map(async ({ accounts, name: source }) => {
|
|
129
|
+
try {
|
|
130
|
+
const list = await accounts.get();
|
|
131
|
+
return mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
// cannot handle this one
|
|
135
|
+
return [];
|
|
136
|
+
}
|
|
137
|
+
}));
|
|
138
|
+
retrieved.forEach((result) => {
|
|
139
|
+
accounts.push(...result);
|
|
140
|
+
});
|
|
141
|
+
console.info(`web3Accounts: Found ${accounts.length} address${accounts.length !== 1 ? 'es' : ''}`);
|
|
142
|
+
return accounts;
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* @summary Subscribes to all the accounts across all providers
|
|
146
|
+
* @description
|
|
147
|
+
* This is the subscription version of the web3Accounts interface with
|
|
148
|
+
* updates as to when new accounts do become available. The list of filtering
|
|
149
|
+
* options are the same as for the web3Accounts interface.
|
|
150
|
+
*/
|
|
151
|
+
async function web3AccountsSubscribe(cb, { accountType, extensions, genesisHash, ss58Format } = {}) {
|
|
152
|
+
const sources = await filterEnable('web3AccountsSubscribe', extensions);
|
|
153
|
+
const accounts = {};
|
|
154
|
+
const triggerUpdate = () => cb(Object
|
|
155
|
+
.entries(accounts)
|
|
156
|
+
.reduce((result, [source, list]) => {
|
|
157
|
+
result.push(...mapAccounts(source, filterAccounts(list, genesisHash, accountType), ss58Format));
|
|
158
|
+
return result;
|
|
159
|
+
}, []));
|
|
160
|
+
const unsubs = sources.map(({ accounts: { subscribe }, name: source }) => subscribe((result) => {
|
|
161
|
+
accounts[source] = result;
|
|
162
|
+
try {
|
|
163
|
+
const result = triggerUpdate();
|
|
164
|
+
if (result && (0, util_1.isPromise)(result)) {
|
|
165
|
+
result.catch(console.error);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
console.error(error);
|
|
170
|
+
}
|
|
171
|
+
}));
|
|
172
|
+
return () => {
|
|
173
|
+
unsubs.forEach((unsub) => {
|
|
174
|
+
unsub();
|
|
175
|
+
});
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* @summary Finds a specific provider based on the name
|
|
180
|
+
* @description
|
|
181
|
+
* This retrieves a specific source (extension) based on the name. In most
|
|
182
|
+
* cases it should not be needed to call it directly (e.g. it is used internally
|
|
183
|
+
* by calls such as web3FromAddress) but would allow operation on a specific
|
|
184
|
+
* known extension.
|
|
185
|
+
*/
|
|
186
|
+
async function web3FromSource(source) {
|
|
187
|
+
if (!web3EnablePromise) {
|
|
188
|
+
return throwError('web3FromSource');
|
|
189
|
+
}
|
|
190
|
+
const sources = await web3EnablePromise;
|
|
191
|
+
const found = source && sources.find(({ name }) => name === source);
|
|
192
|
+
if (!found) {
|
|
193
|
+
throw new Error(`web3FromSource: Unable to find an injected ${source}`);
|
|
194
|
+
}
|
|
195
|
+
return found;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* @summary Find a specific provider that provides a specific address
|
|
199
|
+
* @description
|
|
200
|
+
* Based on an address, return the provider that has makes this address
|
|
201
|
+
* available to the page.
|
|
202
|
+
*/
|
|
203
|
+
async function web3FromAddress(address) {
|
|
204
|
+
if (!web3EnablePromise) {
|
|
205
|
+
return throwError('web3FromAddress');
|
|
206
|
+
}
|
|
207
|
+
const accounts = await web3Accounts();
|
|
208
|
+
let found;
|
|
209
|
+
if (address) {
|
|
210
|
+
const accountU8a = (0, util_crypto_1.decodeAddress)(address);
|
|
211
|
+
found = accounts.find((account) => (0, util_1.u8aEq)((0, util_crypto_1.decodeAddress)(account.address), accountU8a));
|
|
212
|
+
}
|
|
213
|
+
if (!found) {
|
|
214
|
+
throw new Error(`web3FromAddress: Unable to find injected ${address}`);
|
|
215
|
+
}
|
|
216
|
+
return web3FromSource(found.meta.source);
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* @summary List all providers exposed by one source
|
|
220
|
+
* @description
|
|
221
|
+
* For extensions that supply RPC providers, this call would return the list
|
|
222
|
+
* of RPC providers that any extension may supply.
|
|
223
|
+
*/
|
|
224
|
+
async function web3ListRpcProviders(source) {
|
|
225
|
+
const { provider } = await web3FromSource(source);
|
|
226
|
+
if (!provider) {
|
|
227
|
+
console.warn(`Extension ${source} does not expose any provider`);
|
|
228
|
+
return null;
|
|
229
|
+
}
|
|
230
|
+
return provider.listProviders();
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* @summary Start an RPC provider provider by a specific source
|
|
234
|
+
* @description
|
|
235
|
+
* For extensions that supply RPC providers, this call would return an
|
|
236
|
+
* enabled provider (initialized with the specific key) from the
|
|
237
|
+
* specified extension source.
|
|
238
|
+
*/
|
|
239
|
+
async function web3UseRpcProvider(source, key) {
|
|
240
|
+
const { provider } = await web3FromSource(source);
|
|
241
|
+
if (!provider) {
|
|
242
|
+
throw new Error(`Extension ${source} does not expose any provider`);
|
|
243
|
+
}
|
|
244
|
+
const meta = await provider.startProvider(key);
|
|
245
|
+
return { meta, provider };
|
|
246
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.documentReadyPromise = documentReadyPromise;
|
|
4
|
+
function documentReadyPromise(creator) {
|
|
5
|
+
return new Promise((resolve) => {
|
|
6
|
+
if (document.readyState === 'complete') {
|
|
7
|
+
resolve(creator());
|
|
8
|
+
}
|
|
9
|
+
else {
|
|
10
|
+
window.addEventListener('load', () => resolve(creator()));
|
|
11
|
+
}
|
|
12
|
+
});
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.wrapBytes = exports.unwrapBytes = exports.isWrapped = exports.PREFIX = exports.POSTFIX = exports.ETHEREUM = void 0;
|
|
4
|
+
const util_1 = require("@pezkuwi/util");
|
|
5
|
+
exports.ETHEREUM = util_1.U8A_WRAP_ETHEREUM;
|
|
6
|
+
exports.POSTFIX = util_1.U8A_WRAP_POSTFIX;
|
|
7
|
+
exports.PREFIX = util_1.U8A_WRAP_PREFIX;
|
|
8
|
+
exports.isWrapped = util_1.u8aIsWrapped;
|
|
9
|
+
exports.unwrapBytes = util_1.u8aUnwrapBytes;
|
|
10
|
+
exports.wrapBytes = util_1.u8aWrapBytes;
|
package/build/index.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './bundle.js';
|
package/build/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './bundle.js';
|