@reef-knot/ledger-connector 2.0.1 → 3.0.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.
- package/dist/iframe/connector.d.ts +33 -22
- package/dist/iframe/connector.d.ts.map +1 -1
- package/dist/iframe/connector.js +140 -70
- package/dist/iframe/helpers.d.ts +1 -1
- package/dist/iframe/helpers.d.ts.map +1 -1
- package/dist/iframe/helpers.js +7 -18
- package/dist/index.js +2 -2
- package/package.json +10 -9
|
@@ -1,24 +1,35 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
1
|
+
import { Address, Chain, Connector } from 'wagmi';
|
|
2
|
+
import { IFrameEthereumProvider } from '@ledgerhq/iframe-provider';
|
|
3
|
+
import type { IFrameEthereumProviderOptions } from '@ledgerhq/iframe-provider';
|
|
4
|
+
export declare class LedgerLiveConnector extends Connector<IFrameEthereumProvider, IFrameEthereumProviderOptions> {
|
|
5
|
+
#private;
|
|
6
|
+
readonly id = "ledgerLive";
|
|
7
|
+
readonly name = "Ledger Live";
|
|
8
|
+
readonly ready: boolean;
|
|
9
|
+
constructor({ chains, options, }: {
|
|
10
|
+
chains?: Chain[];
|
|
11
|
+
options?: IFrameEthereumProviderOptions;
|
|
12
|
+
});
|
|
13
|
+
connect({ chainId }: {
|
|
14
|
+
chainId?: number;
|
|
15
|
+
}): Promise<{
|
|
16
|
+
account: `0x${string}`;
|
|
17
|
+
chain: {
|
|
18
|
+
id: number;
|
|
19
|
+
unsupported: boolean;
|
|
20
|
+
};
|
|
21
|
+
provider: IFrameEthereumProvider;
|
|
22
|
+
}>;
|
|
23
|
+
disconnect(): Promise<void>;
|
|
24
|
+
getAccount(): Promise<`0x${string}`>;
|
|
25
|
+
getChainId(): Promise<number>;
|
|
26
|
+
getProvider(): Promise<IFrameEthereumProvider>;
|
|
27
|
+
getSigner(): Promise<import("@ethersproject/providers").JsonRpcSigner>;
|
|
28
|
+
isAuthorized(): Promise<boolean>;
|
|
29
|
+
switchChain(chainId: number): Promise<Chain>;
|
|
30
|
+
protected onAccountsChanged(accounts: Address[]): void;
|
|
31
|
+
protected onChainChanged(chainId: number | string): void;
|
|
32
|
+
protected onDisconnect(): void;
|
|
33
|
+
protected isUserRejectedRequestError(error: unknown): boolean;
|
|
22
34
|
}
|
|
23
|
-
export {};
|
|
24
35
|
//# sourceMappingURL=connector.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../../src/iframe/connector.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../../src/iframe/connector.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,OAAO,EACP,KAAK,EACL,SAAS,EAMV,MAAM,OAAO,CAAC;AAEf,OAAO,EAAE,sBAAsB,EAAE,MAAM,2BAA2B,CAAC;AACnE,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAO/E,qBAAa,mBAAoB,SAAQ,SAAS,CAChD,sBAAsB,EACtB,6BAA6B,CAC9B;;IACC,QAAQ,CAAC,EAAE,gBAAgB;IAC3B,QAAQ,CAAC,IAAI,iBAAiB;IAC9B,QAAQ,CAAC,KAAK,EAAE,OAAO,CAAS;gBAGpB,EACV,MAAM,EACN,OAAY,GACb,EAAE;QACD,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC;QACjB,OAAO,CAAC,EAAE,6BAA6B,CAAC;KACzC;IAMK,OAAO,CAAC,EAAE,OAAO,EAAE,EAAE;QAAE,OAAO,CAAC,EAAE,MAAM,CAAA;KAAE;;;;;;;;IA4BzC,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAM3B,UAAU;IAOV,UAAU;IAKhB,WAAW;IAOL,SAAS;IAUT,YAAY;IAWZ,WAAW,CAAC,OAAO,EAAE,MAAM,GAAG,OAAO,CAAC,KAAK,CAAC;IA2BlD,SAAS,CAAC,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE;IAQ/C,SAAS,CAAC,cAAc,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM;IAMjD,SAAS,CAAC,YAAY;IAItB,SAAS,CAAC,0BAA0B,CAAC,KAAK,EAAE,OAAO;CAGpD"}
|
package/dist/iframe/connector.js
CHANGED
|
@@ -1,98 +1,168 @@
|
|
|
1
|
-
import { __awaiter } from '../_virtual/_tslib.js';
|
|
2
|
-
import
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { __awaiter, __classPrivateFieldGet, __classPrivateFieldSet } from '../_virtual/_tslib.js';
|
|
2
|
+
import { Connector, UserRejectedRequestError, ResourceUnavailableError, SwitchChainError } from 'wagmi';
|
|
3
|
+
import { normalizeChainId } from '@wagmi/core';
|
|
4
|
+
import { IFrameEthereumProvider } from '@ledgerhq/iframe-provider';
|
|
5
|
+
import { getAddress } from '@ethersproject/address';
|
|
6
|
+
import { hexValue } from '@ethersproject/bytes';
|
|
7
|
+
import { Web3Provider } from '@ethersproject/providers';
|
|
5
8
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
var _LedgerLiveConnector_provider;
|
|
10
|
+
/* eslint-disable @typescript-eslint/unbound-method */
|
|
11
|
+
class LedgerLiveConnector extends Connector {
|
|
12
|
+
constructor({
|
|
13
|
+
chains,
|
|
14
|
+
options = {}
|
|
15
|
+
}) {
|
|
10
16
|
super({
|
|
11
|
-
|
|
17
|
+
chains,
|
|
18
|
+
options
|
|
12
19
|
});
|
|
13
|
-
this.
|
|
14
|
-
this.
|
|
15
|
-
this.
|
|
16
|
-
this
|
|
17
|
-
this.
|
|
20
|
+
this.id = 'ledgerLive';
|
|
21
|
+
this.name = 'Ledger Live';
|
|
22
|
+
this.ready = false;
|
|
23
|
+
_LedgerLiveConnector_provider.set(this, void 0);
|
|
24
|
+
this.onAccountsChanged = this.onAccountsChanged.bind(this);
|
|
25
|
+
this.onChainChanged = this.onChainChanged.bind(this);
|
|
18
26
|
}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
27
|
+
connect({
|
|
28
|
+
chainId
|
|
29
|
+
}) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
try {
|
|
32
|
+
const provider = yield this.getProvider();
|
|
33
|
+
provider.on('accountsChanged', this.onAccountsChanged);
|
|
34
|
+
provider.on('chainChanged', this.onChainChanged);
|
|
35
|
+
this.emit('message', {
|
|
36
|
+
type: 'connecting'
|
|
37
|
+
});
|
|
38
|
+
const account = yield this.getAccount();
|
|
39
|
+
let currentChainId = yield this.getChainId();
|
|
40
|
+
let unsupported = this.isChainUnsupported(currentChainId);
|
|
41
|
+
if (chainId && currentChainId !== chainId) {
|
|
42
|
+
const chain = yield this.switchChain(chainId);
|
|
43
|
+
currentChainId = chain.id;
|
|
44
|
+
unsupported = this.isChainUnsupported(currentChainId);
|
|
45
|
+
}
|
|
46
|
+
return {
|
|
47
|
+
account,
|
|
48
|
+
chain: {
|
|
49
|
+
id: currentChainId,
|
|
50
|
+
unsupported
|
|
51
|
+
},
|
|
52
|
+
provider
|
|
53
|
+
};
|
|
54
|
+
} catch (error) {
|
|
55
|
+
if (this.isUserRejectedRequestError(error)) throw new UserRejectedRequestError(error);
|
|
56
|
+
if (error.code === -32002) throw new ResourceUnavailableError(error);
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
23
59
|
});
|
|
24
60
|
}
|
|
25
|
-
|
|
26
|
-
this
|
|
27
|
-
|
|
61
|
+
disconnect() {
|
|
62
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
63
|
+
const provider = yield this.getProvider();
|
|
64
|
+
provider.removeListener('accountsChanged', this.onAccountsChanged);
|
|
65
|
+
provider.removeListener('chainChanged', this.onChainChanged);
|
|
28
66
|
});
|
|
29
67
|
}
|
|
30
|
-
|
|
31
|
-
this
|
|
32
|
-
|
|
68
|
+
getAccount() {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
const provider = yield this.getProvider();
|
|
71
|
+
const accounts = yield provider.send('eth_requestAccounts');
|
|
72
|
+
// return checksum address
|
|
73
|
+
return getAddress(accounts[0]);
|
|
33
74
|
});
|
|
34
75
|
}
|
|
35
|
-
|
|
36
|
-
this
|
|
76
|
+
getChainId() {
|
|
77
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
78
|
+
const provider = yield this.getProvider();
|
|
79
|
+
return provider.send('eth_chainId').then(normalizeChainId);
|
|
80
|
+
});
|
|
37
81
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
82
|
+
getProvider() {
|
|
83
|
+
if (!__classPrivateFieldGet(this, _LedgerLiveConnector_provider, "f")) {
|
|
84
|
+
__classPrivateFieldSet(this, _LedgerLiveConnector_provider, new IFrameEthereumProvider(this.options), "f");
|
|
85
|
+
}
|
|
86
|
+
return Promise.resolve(__classPrivateFieldGet(this, _LedgerLiveConnector_provider, "f"));
|
|
41
87
|
}
|
|
42
|
-
|
|
88
|
+
getSigner() {
|
|
43
89
|
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
const {
|
|
47
|
-
IFrameEthereumProvider
|
|
48
|
-
} = yield import('@ledgerhq/iframe-provider');
|
|
49
|
-
return new IFrameEthereumProvider(this.config);
|
|
90
|
+
const [provider, account] = yield Promise.all([this.getProvider(), this.getAccount()]);
|
|
91
|
+
return new Web3Provider(provider).getSigner(account);
|
|
50
92
|
});
|
|
51
93
|
}
|
|
52
|
-
|
|
94
|
+
isAuthorized() {
|
|
53
95
|
return __awaiter(this, void 0, void 0, function* () {
|
|
54
|
-
|
|
55
|
-
|
|
96
|
+
try {
|
|
97
|
+
const provider = yield this.getProvider();
|
|
98
|
+
const accounts = yield provider.send('eth_accounts');
|
|
99
|
+
const account = accounts[0];
|
|
100
|
+
return !!account;
|
|
101
|
+
} catch (_a) {
|
|
102
|
+
return false;
|
|
56
103
|
}
|
|
57
|
-
this.provider.on('networkChanged', this.handleNetworkChanged);
|
|
58
|
-
this.provider.on('chainChanged', this.handleChainChanged);
|
|
59
|
-
this.provider.on('accountsChanged', this.handleAccountsChanged);
|
|
60
|
-
this.provider.on('close', this.handleClose);
|
|
61
|
-
const accounts = yield this.provider.enable();
|
|
62
|
-
const account = accounts[0];
|
|
63
|
-
return {
|
|
64
|
-
provider: this.provider,
|
|
65
|
-
account
|
|
66
|
-
};
|
|
67
104
|
});
|
|
68
105
|
}
|
|
69
|
-
|
|
70
|
-
|
|
106
|
+
switchChain(chainId) {
|
|
107
|
+
var _a;
|
|
71
108
|
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
-
|
|
109
|
+
const provider = yield this.getProvider();
|
|
110
|
+
const id = hexValue(chainId);
|
|
111
|
+
try {
|
|
112
|
+
yield provider.send('wallet_switchEthereumChain', [{
|
|
113
|
+
chainId: id
|
|
114
|
+
}]);
|
|
115
|
+
return (_a = this.chains.find(x => x.id === chainId)) !== null && _a !== void 0 ? _a : {
|
|
116
|
+
id: chainId,
|
|
117
|
+
name: `Chain ${id}`,
|
|
118
|
+
network: `${id}`,
|
|
119
|
+
nativeCurrency: {
|
|
120
|
+
decimals: 18,
|
|
121
|
+
name: 'Ether',
|
|
122
|
+
symbol: 'ETH'
|
|
123
|
+
},
|
|
124
|
+
rpcUrls: {
|
|
125
|
+
default: {
|
|
126
|
+
http: ['']
|
|
127
|
+
},
|
|
128
|
+
public: {
|
|
129
|
+
http: ['']
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
} catch (error) {
|
|
134
|
+
const message = typeof error === 'string' ? error : error === null || error === void 0 ? void 0 : error.message;
|
|
135
|
+
if (/user rejected request/i.test(message)) throw new UserRejectedRequestError(error);
|
|
136
|
+
throw new SwitchChainError(error);
|
|
137
|
+
}
|
|
73
138
|
});
|
|
74
139
|
}
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
140
|
+
onAccountsChanged(accounts) {
|
|
141
|
+
if (accounts.length === 0 || !accounts[0]) {
|
|
142
|
+
this.emit('disconnect');
|
|
143
|
+
} else {
|
|
144
|
+
this.emit('change', {
|
|
145
|
+
account: getAddress(accounts[0])
|
|
146
|
+
});
|
|
147
|
+
}
|
|
80
148
|
}
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
149
|
+
onChainChanged(chainId) {
|
|
150
|
+
const id = normalizeChainId(chainId);
|
|
151
|
+
const unsupported = this.isChainUnsupported(id);
|
|
152
|
+
this.emit('change', {
|
|
153
|
+
chain: {
|
|
154
|
+
id,
|
|
155
|
+
unsupported
|
|
156
|
+
}
|
|
87
157
|
});
|
|
88
158
|
}
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
this.provider.removeListener('close', this.handleClose);
|
|
159
|
+
onDisconnect() {
|
|
160
|
+
this.emit('disconnect');
|
|
161
|
+
}
|
|
162
|
+
isUserRejectedRequestError(error) {
|
|
163
|
+
return error.code === 4001;
|
|
95
164
|
}
|
|
96
165
|
}
|
|
166
|
+
_LedgerLiveConnector_provider = new WeakMap();
|
|
97
167
|
|
|
98
|
-
export {
|
|
168
|
+
export { LedgerLiveConnector };
|
package/dist/iframe/helpers.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/iframe/helpers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/iframe/helpers.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,QAAQ,eACgD,CAAC;AAEtE,eAAO,MAAM,YAAY,eAWxB,CAAC"}
|
package/dist/iframe/helpers.js
CHANGED
|
@@ -1,24 +1,13 @@
|
|
|
1
|
-
const isIframe = () =>
|
|
1
|
+
const isIframe = () => globalThis.window && globalThis.window.parent !== globalThis.window;
|
|
2
|
+
const isLedgerLive = () => {
|
|
3
|
+
if (typeof window === 'undefined') return false;
|
|
2
4
|
try {
|
|
3
|
-
|
|
5
|
+
const params = new URLSearchParams(window.self.location.search);
|
|
6
|
+
const isEmbed = !!params.get('embed');
|
|
7
|
+
return isIframe() && isEmbed;
|
|
4
8
|
} catch (error) {
|
|
5
9
|
return false;
|
|
6
10
|
}
|
|
7
11
|
};
|
|
8
|
-
const isLedgerDappBrowserProvider = (() => {
|
|
9
|
-
let state = null;
|
|
10
|
-
return () => {
|
|
11
|
-
if (typeof state === 'boolean') return state;
|
|
12
|
-
if (typeof window === 'undefined') return false;
|
|
13
|
-
try {
|
|
14
|
-
const params = new URLSearchParams(window.self.location.search);
|
|
15
|
-
const isEmbed = !!params.get('embed');
|
|
16
|
-
state = isIframe() && isEmbed;
|
|
17
|
-
} catch (error) {
|
|
18
|
-
state = false;
|
|
19
|
-
}
|
|
20
|
-
return state;
|
|
21
|
-
};
|
|
22
|
-
})();
|
|
23
12
|
|
|
24
|
-
export { isIframe,
|
|
13
|
+
export { isIframe, isLedgerLive };
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export {
|
|
2
|
-
export { isIframe,
|
|
1
|
+
export { LedgerLiveConnector } from './iframe/connector.js';
|
|
2
|
+
export { isIframe, isLedgerLive } from './iframe/helpers.js';
|
|
3
3
|
export { LedgerHIDConnector } from './hid/connector.js';
|
|
4
4
|
export { LS_KEY_DERIVATION_PATH } from './hid/signer.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reef-knot/ledger-connector",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"main": "dist/index.js",
|
|
5
5
|
"types": "dist/index.d.ts",
|
|
6
6
|
"exports": {
|
|
@@ -37,14 +37,15 @@
|
|
|
37
37
|
"eslint-config-custom": "*"
|
|
38
38
|
},
|
|
39
39
|
"dependencies": {
|
|
40
|
-
"@ethersproject/abstract-signer": "^5.
|
|
41
|
-
"@ethersproject/bignumber": "^5.
|
|
42
|
-
"@ethersproject/bytes": "^5.
|
|
43
|
-
"@ethersproject/properties": "^5.
|
|
44
|
-
"@ethersproject/providers": "^5.
|
|
45
|
-
"@ethersproject/strings": "^5.
|
|
46
|
-
"@ethersproject/transactions": "^5.
|
|
47
|
-
"@ethersproject/hash": "^5.
|
|
40
|
+
"@ethersproject/abstract-signer": "^5.7.0",
|
|
41
|
+
"@ethersproject/bignumber": "^5.7.0",
|
|
42
|
+
"@ethersproject/bytes": "^5.7.0",
|
|
43
|
+
"@ethersproject/properties": "^5.7.0",
|
|
44
|
+
"@ethersproject/providers": "^5.7.2",
|
|
45
|
+
"@ethersproject/strings": "^5.7.0",
|
|
46
|
+
"@ethersproject/transactions": "^5.7.0",
|
|
47
|
+
"@ethersproject/hash": "^5.7.0",
|
|
48
|
+
"@ethersproject/address": "^5.7.0",
|
|
48
49
|
"@ledgerhq/hw-app-eth": "^6.35.2",
|
|
49
50
|
"@ledgerhq/hw-transport": "^6.30.1",
|
|
50
51
|
"@ledgerhq/hw-transport-webhid": "^6.28.1",
|