@reef-knot/ledger-connector 4.5.0 → 5.0.0-alpha.1
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/hid/connector.d.ts.map +1 -1
- package/dist/hid/connector.js +85 -136
- package/dist/hid/constants.js +4 -2
- package/dist/hid/helpers.js +39 -60
- package/dist/hid/index.js +2 -0
- package/dist/hid/provider.js +95 -120
- package/dist/hid/signer.js +143 -184
- package/dist/iframe/connector.d.ts.map +1 -1
- package/dist/iframe/connector.js +110 -173
- package/dist/iframe/helpers.js +10 -9
- package/dist/iframe/index.js +2 -0
- package/dist/iframe/provider.js +9 -10
- package/dist/index.js +8 -4
- package/package.json +10 -8
- package/dist/_virtual/_tslib.js +0 -33
package/dist/hid/signer.js
CHANGED
|
@@ -1,187 +1,146 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
8
|
-
import {
|
|
9
|
-
import { LS_KEY_DERIVATION_PATH } from './constants.js';
|
|
1
|
+
import { LS_KEY_DERIVATION_PATH } from "./constants.js";
|
|
2
|
+
import { checkError, convertToUnsigned, toNumber } from "./helpers.js";
|
|
3
|
+
import { hexlify, joinSignature } from "@ethersproject/bytes";
|
|
4
|
+
import { BigNumber } from "@ethersproject/bignumber";
|
|
5
|
+
import { Signer } from "@ethersproject/abstract-signer";
|
|
6
|
+
import { serialize } from "@ethersproject/transactions";
|
|
7
|
+
import { toUtf8Bytes } from "@ethersproject/strings";
|
|
8
|
+
import { _TypedDataEncoder } from "@ethersproject/hash";
|
|
10
9
|
|
|
10
|
+
//#region src/hid/signer.ts
|
|
11
11
|
const defaultPath = "m/44'/60'/0'/0/0";
|
|
12
|
-
class LedgerHQSigner extends Signer {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
verifyingContract: domain.verifyingContract,
|
|
145
|
-
version: domain.version,
|
|
146
|
-
chainId: domain.chainId // eslint-disable-next-line radix
|
|
147
|
-
? parseInt(domain.chainId.toString()) : undefined
|
|
148
|
-
},
|
|
149
|
-
types: Object.assign(Object.assign({}, types), {
|
|
150
|
-
EIP712Domain: [{
|
|
151
|
-
name: 'name',
|
|
152
|
-
type: 'string'
|
|
153
|
-
}, {
|
|
154
|
-
name: 'version',
|
|
155
|
-
type: 'string'
|
|
156
|
-
}, {
|
|
157
|
-
name: 'chainId',
|
|
158
|
-
type: 'uint256'
|
|
159
|
-
}, {
|
|
160
|
-
name: 'verifyingContract',
|
|
161
|
-
type: 'address'
|
|
162
|
-
}]
|
|
163
|
-
}),
|
|
164
|
-
primaryType: encoder.primaryType,
|
|
165
|
-
message: value
|
|
166
|
-
};
|
|
167
|
-
return this.__signEIP712Message(data);
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
// custom method, also called directly on eth_signTypedData_v4 RPC request
|
|
171
|
-
__signEIP712Message(data) {
|
|
172
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
173
|
-
const {
|
|
174
|
-
r,
|
|
175
|
-
s,
|
|
176
|
-
v
|
|
177
|
-
} = yield this.withEthApp(eth => eth.signEIP712Message(this.path, data));
|
|
178
|
-
return joinSignature({
|
|
179
|
-
r: `0x${r}`,
|
|
180
|
-
s: `0x${s}`,
|
|
181
|
-
v
|
|
182
|
-
});
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
12
|
+
var LedgerHQSigner = class LedgerHQSigner extends Signer {
|
|
13
|
+
get path() {
|
|
14
|
+
if (typeof window !== "undefined") return window.localStorage.getItem(LS_KEY_DERIVATION_PATH) || defaultPath;
|
|
15
|
+
return defaultPath;
|
|
16
|
+
}
|
|
17
|
+
constructor(provider) {
|
|
18
|
+
super();
|
|
19
|
+
this._index = 0;
|
|
20
|
+
this._address = "";
|
|
21
|
+
this.provider = provider;
|
|
22
|
+
}
|
|
23
|
+
async withEthApp(callback) {
|
|
24
|
+
const transport = await this.provider.getTransport();
|
|
25
|
+
try {
|
|
26
|
+
const { default: Eth } = await import("@ledgerhq/hw-app-eth");
|
|
27
|
+
const eth = new Eth(transport);
|
|
28
|
+
await eth.getAppConfiguration();
|
|
29
|
+
return await callback(eth);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
return checkError(error);
|
|
32
|
+
} finally {
|
|
33
|
+
await transport.close();
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
async getAddress() {
|
|
37
|
+
const account = await this.withEthApp((eth) => eth.getAddress(this.path));
|
|
38
|
+
this._address = this.provider.formatter.address(account.address);
|
|
39
|
+
return this._address;
|
|
40
|
+
}
|
|
41
|
+
async signMessage(message) {
|
|
42
|
+
if (typeof message === "string") message = toUtf8Bytes(message);
|
|
43
|
+
const messageHex = hexlify(message).substring(2);
|
|
44
|
+
const sig = await this.withEthApp((eth) => eth.signPersonalMessage(this.path, messageHex));
|
|
45
|
+
sig.r = `0x${sig.r}`;
|
|
46
|
+
sig.s = `0x${sig.s}`;
|
|
47
|
+
return joinSignature(sig);
|
|
48
|
+
}
|
|
49
|
+
async populateUnsigned(transaction) {
|
|
50
|
+
const populated = await this.populateTransaction(transaction);
|
|
51
|
+
const nonce = toNumber(populated.nonce);
|
|
52
|
+
if (populated.type === 0) {
|
|
53
|
+
const { gasLimit, type, chainId, gasPrice, value, data, to } = populated;
|
|
54
|
+
return {
|
|
55
|
+
gasLimit,
|
|
56
|
+
type,
|
|
57
|
+
chainId,
|
|
58
|
+
gasPrice,
|
|
59
|
+
nonce,
|
|
60
|
+
value,
|
|
61
|
+
data,
|
|
62
|
+
to
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
...populated,
|
|
67
|
+
nonce
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
async signTransaction(transaction, loadConfig = {}, resolutionConfig = {}) {
|
|
71
|
+
const unsignedTx = await convertToUnsigned(transaction);
|
|
72
|
+
const populatedTx = await this.populateUnsigned(unsignedTx);
|
|
73
|
+
const serializedTx = serialize(populatedTx).substring(2);
|
|
74
|
+
const { ledgerService } = await import("@ledgerhq/hw-app-eth");
|
|
75
|
+
const resolution = await ledgerService.resolveTransaction(serializedTx, loadConfig, resolutionConfig);
|
|
76
|
+
const sig = await this.withEthApp((eth) => eth.signTransaction(this.path, serializedTx, resolution));
|
|
77
|
+
return serialize(populatedTx, {
|
|
78
|
+
v: BigNumber.from(`0x${sig.v}`).toNumber(),
|
|
79
|
+
r: `0x${sig.r}`,
|
|
80
|
+
s: `0x${sig.s}`
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
connect(provider) {
|
|
84
|
+
return new LedgerHQSigner(provider);
|
|
85
|
+
}
|
|
86
|
+
connectUnchecked() {
|
|
87
|
+
throw new Error("method is not implemented");
|
|
88
|
+
}
|
|
89
|
+
sendUncheckedTransaction() {
|
|
90
|
+
throw new Error("method is not implemented");
|
|
91
|
+
}
|
|
92
|
+
async unlock() {
|
|
93
|
+
throw new Error("method is not implemented");
|
|
94
|
+
}
|
|
95
|
+
async _legacySignMessage(_message) {
|
|
96
|
+
throw new Error("method is not implemented");
|
|
97
|
+
}
|
|
98
|
+
async _signTypedData(domain, _types, value) {
|
|
99
|
+
const types = { ..._types };
|
|
100
|
+
delete types.EIP712Domain;
|
|
101
|
+
const encoder = new _TypedDataEncoder(types);
|
|
102
|
+
const data = {
|
|
103
|
+
domain: {
|
|
104
|
+
name: domain.name,
|
|
105
|
+
verifyingContract: domain.verifyingContract,
|
|
106
|
+
version: domain.version,
|
|
107
|
+
chainId: domain.chainId ? parseInt(domain.chainId.toString()) : void 0
|
|
108
|
+
},
|
|
109
|
+
types: {
|
|
110
|
+
...types,
|
|
111
|
+
EIP712Domain: [
|
|
112
|
+
{
|
|
113
|
+
name: "name",
|
|
114
|
+
type: "string"
|
|
115
|
+
},
|
|
116
|
+
{
|
|
117
|
+
name: "version",
|
|
118
|
+
type: "string"
|
|
119
|
+
},
|
|
120
|
+
{
|
|
121
|
+
name: "chainId",
|
|
122
|
+
type: "uint256"
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
name: "verifyingContract",
|
|
126
|
+
type: "address"
|
|
127
|
+
}
|
|
128
|
+
]
|
|
129
|
+
},
|
|
130
|
+
primaryType: encoder.primaryType,
|
|
131
|
+
message: value
|
|
132
|
+
};
|
|
133
|
+
return this.__signEIP712Message(data);
|
|
134
|
+
}
|
|
135
|
+
async __signEIP712Message(data) {
|
|
136
|
+
const { r, s, v } = await this.withEthApp((eth) => eth.signEIP712Message(this.path, data));
|
|
137
|
+
return joinSignature({
|
|
138
|
+
r: `0x${r}`,
|
|
139
|
+
s: `0x${s}`,
|
|
140
|
+
v
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
};
|
|
186
144
|
|
|
187
|
-
|
|
145
|
+
//#endregion
|
|
146
|
+
export { LedgerHQSigner };
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../../src/iframe/connector.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAIrC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD,eAAO,MAAM,YAAY,eAAe,CAAC;AACzC,eAAO,MAAM,IAAI,gBAAgB,CAAC;AAElC,KAAK,uBAAuB,GAAG;IAC7B,OAAO,CAAC,EAAE,6BAA6B,CAAC;IACxC,YAAY,EAAE,KAAK,CAAC;CACrB,CAAC;AAGF,wBAAgB,mBAAmB,CAAC,EAClC,OAAO,EACP,YAAY,GACb,EAAE,uBAAuB,
|
|
1
|
+
{"version":3,"file":"connector.d.ts","sourceRoot":"","sources":["../../src/iframe/connector.ts"],"names":[],"mappings":"AAWA,OAAO,EAAE,KAAK,EAAE,MAAM,cAAc,CAAC;AAIrC,OAAO,KAAK,EAAE,6BAA6B,EAAE,MAAM,2BAA2B,CAAC;AAC/E,OAAO,KAAK,EAAE,oBAAoB,EAAE,MAAM,YAAY,CAAC;AAEvD,eAAO,MAAM,YAAY,eAAe,CAAC;AACzC,eAAO,MAAM,IAAI,gBAAgB,CAAC;AAElC,KAAK,uBAAuB,GAAG;IAC7B,OAAO,CAAC,EAAE,6BAA6B,CAAC;IACxC,YAAY,EAAE,KAAK,CAAC;CACrB,CAAC;AAGF,wBAAgB,mBAAmB,CAAC,EAClC,OAAO,EACP,YAAY,GACb,EAAE,uBAAuB,6GAmJzB;yBAtJe,mBAAmB"}
|
package/dist/iframe/connector.js
CHANGED
|
@@ -1,176 +1,113 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
6
|
-
import { Web3Provider } from '@ethersproject/providers';
|
|
1
|
+
import { SwitchChainNotSupportedError, createConnector } from "wagmi";
|
|
2
|
+
import { ResourceUnavailableRpcError, UserRejectedRequestError } from "viem";
|
|
3
|
+
import { getAddress } from "@ethersproject/address";
|
|
4
|
+
import { hexValue } from "@ethersproject/bytes";
|
|
5
|
+
import { Web3Provider } from "@ethersproject/providers";
|
|
7
6
|
|
|
8
|
-
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
}) {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
});
|
|
114
|
-
},
|
|
115
|
-
getAccounts() {
|
|
116
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
117
|
-
const provider = yield this.getProvider();
|
|
118
|
-
const accounts = yield provider.send('eth_requestAccounts');
|
|
119
|
-
// return checksum address
|
|
120
|
-
return accounts.map(getAddress);
|
|
121
|
-
});
|
|
122
|
-
},
|
|
123
|
-
getChainId() {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
-
const provider = yield this.getProvider();
|
|
126
|
-
const chainId = yield provider.send('eth_chainId');
|
|
127
|
-
return Number(chainId);
|
|
128
|
-
});
|
|
129
|
-
},
|
|
130
|
-
getSigner() {
|
|
131
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
-
const [provider, accounts] = yield Promise.all([this.getProvider(), this.getAccounts()]);
|
|
133
|
-
return new Web3Provider(provider).getSigner(accounts[0]);
|
|
134
|
-
});
|
|
135
|
-
},
|
|
136
|
-
isAuthorized() {
|
|
137
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
-
try {
|
|
139
|
-
const provider = yield this.getProvider();
|
|
140
|
-
const accounts = yield provider.send('eth_accounts');
|
|
141
|
-
const account = accounts[0];
|
|
142
|
-
return !!account;
|
|
143
|
-
} catch (_a) {
|
|
144
|
-
return false;
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
},
|
|
148
|
-
onAccountsChanged(accounts) {
|
|
149
|
-
if (accounts.length === 0 || !accounts[0]) {
|
|
150
|
-
emitter.emit('disconnect');
|
|
151
|
-
void this.disconnect();
|
|
152
|
-
} else {
|
|
153
|
-
emitter.emit('change', {
|
|
154
|
-
accounts
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
},
|
|
158
|
-
onChainChanged(chainId) {
|
|
159
|
-
emitter.emit('change', {
|
|
160
|
-
chainId: Number(chainId)
|
|
161
|
-
});
|
|
162
|
-
},
|
|
163
|
-
disconnect() {
|
|
164
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
165
|
-
const provider = yield this.getProvider();
|
|
166
|
-
provider.removeListener('accountsChanged', this.onAccountsChanged);
|
|
167
|
-
provider.removeListener('chainChanged', this.onChainChanged);
|
|
168
|
-
});
|
|
169
|
-
},
|
|
170
|
-
onDisconnect() {
|
|
171
|
-
emitter.emit('disconnect');
|
|
172
|
-
}
|
|
173
|
-
}));
|
|
7
|
+
//#region src/iframe/connector.ts
|
|
8
|
+
const idLedgerLive = "ledgerLive";
|
|
9
|
+
const name = "Ledger Live";
|
|
10
|
+
ledgerLiveConnector.type = "ledgerLive";
|
|
11
|
+
function ledgerLiveConnector({ options, defaultChain }) {
|
|
12
|
+
const providers = {};
|
|
13
|
+
return createConnector(({ chains, emitter }) => ({
|
|
14
|
+
id: idLedgerLive,
|
|
15
|
+
name,
|
|
16
|
+
type: ledgerLiveConnector.type,
|
|
17
|
+
async getProvider({ chainId } = {}) {
|
|
18
|
+
const chain = chains.find((x) => x.id === chainId) ?? defaultChain;
|
|
19
|
+
if (!providers[chain.id]) {
|
|
20
|
+
const { LedgerIFrameProvider } = await import("./provider.js");
|
|
21
|
+
providers[chain.id] = new LedgerIFrameProvider(options);
|
|
22
|
+
}
|
|
23
|
+
return providers[chain.id];
|
|
24
|
+
},
|
|
25
|
+
async connect({ chainId, withCapabilities } = {}) {
|
|
26
|
+
try {
|
|
27
|
+
const provider = await this.getProvider();
|
|
28
|
+
provider.on("accountsChanged", this.onAccountsChanged);
|
|
29
|
+
provider.on("chainChanged", this.onChainChanged);
|
|
30
|
+
emitter.emit("message", { type: "connecting" });
|
|
31
|
+
const accounts = await this.getAccounts();
|
|
32
|
+
let currentChainId = await this.getChainId();
|
|
33
|
+
if (chainId && currentChainId !== chainId && this.switchChain) currentChainId = (await this.switchChain({ chainId })).id;
|
|
34
|
+
return {
|
|
35
|
+
accounts: withCapabilities ? accounts.map((address) => ({
|
|
36
|
+
address,
|
|
37
|
+
capabilities: {}
|
|
38
|
+
})) : accounts,
|
|
39
|
+
chainId: currentChainId
|
|
40
|
+
};
|
|
41
|
+
} catch (error) {
|
|
42
|
+
if (error instanceof Error) {
|
|
43
|
+
if (error.code === 4001) throw new UserRejectedRequestError(error);
|
|
44
|
+
throw new ResourceUnavailableRpcError(error);
|
|
45
|
+
}
|
|
46
|
+
throw error;
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
async switchChain({ chainId }) {
|
|
50
|
+
const provider = await this.getProvider();
|
|
51
|
+
const id = hexValue(chainId);
|
|
52
|
+
try {
|
|
53
|
+
await provider.send("wallet_switchEthereumChain", [{ chainId: id }]);
|
|
54
|
+
return chains.find((x) => x.id === chainId) ?? {
|
|
55
|
+
id: chainId,
|
|
56
|
+
name: `Chain ${id}`,
|
|
57
|
+
network: `${id}`,
|
|
58
|
+
nativeCurrency: {
|
|
59
|
+
decimals: 18,
|
|
60
|
+
name: "Ether",
|
|
61
|
+
symbol: "ETH"
|
|
62
|
+
},
|
|
63
|
+
rpcUrls: {
|
|
64
|
+
default: { http: [""] },
|
|
65
|
+
public: { http: [""] }
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
} catch (error) {
|
|
69
|
+
const message = typeof error === "string" ? error : error?.message;
|
|
70
|
+
if (/user rejected request/i.test(message)) throw new UserRejectedRequestError(error);
|
|
71
|
+
throw new SwitchChainNotSupportedError({ connector: { name } });
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
async getAccounts() {
|
|
75
|
+
return (await (await this.getProvider()).send("eth_requestAccounts")).map(getAddress);
|
|
76
|
+
},
|
|
77
|
+
async getChainId() {
|
|
78
|
+
const chainId = await (await this.getProvider()).send("eth_chainId");
|
|
79
|
+
return Number(chainId);
|
|
80
|
+
},
|
|
81
|
+
async getSigner() {
|
|
82
|
+
const [provider, accounts] = await Promise.all([this.getProvider(), this.getAccounts()]);
|
|
83
|
+
return new Web3Provider(provider).getSigner(accounts[0]);
|
|
84
|
+
},
|
|
85
|
+
async isAuthorized() {
|
|
86
|
+
try {
|
|
87
|
+
return !!(await (await this.getProvider()).send("eth_accounts"))[0];
|
|
88
|
+
} catch {
|
|
89
|
+
return false;
|
|
90
|
+
}
|
|
91
|
+
},
|
|
92
|
+
onAccountsChanged(accounts) {
|
|
93
|
+
if (accounts.length === 0 || !accounts[0]) {
|
|
94
|
+
emitter.emit("disconnect");
|
|
95
|
+
this.disconnect();
|
|
96
|
+
} else emitter.emit("change", { accounts });
|
|
97
|
+
},
|
|
98
|
+
onChainChanged(chainId) {
|
|
99
|
+
emitter.emit("change", { chainId: Number(chainId) });
|
|
100
|
+
},
|
|
101
|
+
async disconnect() {
|
|
102
|
+
const provider = await this.getProvider();
|
|
103
|
+
provider.removeListener("accountsChanged", this.onAccountsChanged);
|
|
104
|
+
provider.removeListener("chainChanged", this.onChainChanged);
|
|
105
|
+
},
|
|
106
|
+
onDisconnect() {
|
|
107
|
+
emitter.emit("disconnect");
|
|
108
|
+
}
|
|
109
|
+
}));
|
|
174
110
|
}
|
|
175
111
|
|
|
176
|
-
|
|
112
|
+
//#endregion
|
|
113
|
+
export { idLedgerLive, ledgerLiveConnector, name };
|
package/dist/iframe/helpers.js
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
+
//#region src/iframe/helpers.ts
|
|
1
2
|
const isIframe = () => globalThis.window && globalThis.window.parent !== globalThis.window;
|
|
2
3
|
const isLedgerLive = () => {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
}
|
|
4
|
+
if (typeof window === "undefined") return false;
|
|
5
|
+
try {
|
|
6
|
+
const isEmbed = !!new URLSearchParams(window.self.location.search).get("embed");
|
|
7
|
+
return isIframe() && isEmbed;
|
|
8
|
+
} catch (error) {
|
|
9
|
+
return false;
|
|
10
|
+
}
|
|
11
11
|
};
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
//#endregion
|
|
14
|
+
export { isIframe, isLedgerLive };
|