@toruslabs/ethereum-controllers 5.10.1 → 6.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/ethereumControllers.cjs.js +114 -432
- package/dist/ethereumControllers.esm.js +63 -351
- package/dist/ethereumControllers.umd.min.js +1 -1
- package/dist/ethereumControllers.umd.min.js.LICENSE.txt +7 -2
- package/dist/lib.cjs/Account/AccountTrackerController.js +160 -0
- package/dist/lib.cjs/Block/PollingBlockTracker.js +85 -0
- package/dist/lib.cjs/Currency/CurrencyController.js +111 -0
- package/dist/lib.cjs/Gas/GasFeeController.js +214 -0
- package/dist/lib.cjs/Gas/gasUtil.js +148 -0
- package/dist/lib.cjs/Keyring/KeyringController.js +93 -0
- package/dist/lib.cjs/Message/AbstractMessageController.js +107 -0
- package/dist/lib.cjs/Message/AddChainController.js +78 -0
- package/dist/lib.cjs/Message/MessageController.js +77 -0
- package/dist/lib.cjs/Message/PersonalMessageController.js +77 -0
- package/dist/lib.cjs/Message/SwitchChainController.js +78 -0
- package/dist/lib.cjs/Message/TypedMessageController.js +81 -0
- package/dist/lib.cjs/Message/utils.js +112 -0
- package/dist/lib.cjs/Network/NetworkController.js +201 -0
- package/dist/lib.cjs/Network/cacheIdentifier.js +112 -0
- package/dist/lib.cjs/Network/createEthereumMiddleware.js +302 -0
- package/dist/lib.cjs/Network/createJsonRpcClient.js +64 -0
- package/dist/lib.cjs/Nfts/NftHandler.js +180 -0
- package/dist/lib.cjs/Nfts/NftsController.js +213 -0
- package/dist/lib.cjs/Preferences/PreferencesController.js +476 -0
- package/dist/lib.cjs/Tokens/TokenHandler.js +51 -0
- package/dist/lib.cjs/Tokens/TokenRatesController.js +112 -0
- package/dist/lib.cjs/Tokens/TokensController.js +259 -0
- package/dist/lib.cjs/Transaction/NonceTracker.js +150 -0
- package/dist/lib.cjs/Transaction/PendingTransactionTracker.js +222 -0
- package/dist/lib.cjs/Transaction/TransactionController.js +515 -0
- package/dist/lib.cjs/Transaction/TransactionGasUtil.js +81 -0
- package/dist/lib.cjs/Transaction/TransactionStateHistoryHelper.js +42 -0
- package/dist/lib.cjs/Transaction/TransactionStateManager.js +296 -0
- package/dist/lib.cjs/Transaction/TransactionUtils.js +341 -0
- package/dist/lib.cjs/index.js +171 -0
- package/dist/lib.cjs/utils/abis.js +510 -0
- package/dist/lib.cjs/utils/constants.js +362 -0
- package/dist/lib.cjs/utils/contractAddresses.js +16 -0
- package/dist/lib.cjs/utils/conversionUtils.js +232 -0
- package/dist/lib.cjs/utils/helpers.js +244 -0
- package/dist/lib.cjs/utils/lodashUtils.js +25 -0
- package/dist/lib.esm/Account/AccountTrackerController.js +158 -0
- package/dist/lib.esm/Block/PollingBlockTracker.js +83 -0
- package/dist/lib.esm/Currency/CurrencyController.js +109 -0
- package/dist/lib.esm/Gas/GasFeeController.js +212 -0
- package/dist/lib.esm/Gas/gasUtil.js +141 -0
- package/dist/lib.esm/Keyring/KeyringController.js +91 -0
- package/dist/lib.esm/Message/AbstractMessageController.js +105 -0
- package/dist/lib.esm/Message/AddChainController.js +76 -0
- package/dist/lib.esm/Message/MessageController.js +75 -0
- package/dist/lib.esm/Message/PersonalMessageController.js +75 -0
- package/dist/lib.esm/Message/SwitchChainController.js +76 -0
- package/dist/lib.esm/Message/TypedMessageController.js +79 -0
- package/dist/lib.esm/Message/utils.js +105 -0
- package/dist/lib.esm/Network/NetworkController.js +199 -0
- package/dist/lib.esm/Network/cacheIdentifier.js +107 -0
- package/dist/lib.esm/Network/createEthereumMiddleware.js +289 -0
- package/dist/lib.esm/Network/createJsonRpcClient.js +60 -0
- package/dist/lib.esm/Nfts/NftHandler.js +178 -0
- package/dist/lib.esm/Nfts/NftsController.js +211 -0
- package/dist/lib.esm/Preferences/PreferencesController.js +474 -0
- package/dist/lib.esm/Tokens/TokenHandler.js +49 -0
- package/dist/lib.esm/Tokens/TokenRatesController.js +109 -0
- package/dist/lib.esm/Tokens/TokensController.js +257 -0
- package/dist/lib.esm/Transaction/NonceTracker.js +148 -0
- package/dist/lib.esm/Transaction/PendingTransactionTracker.js +220 -0
- package/dist/lib.esm/Transaction/TransactionController.js +513 -0
- package/dist/lib.esm/Transaction/TransactionGasUtil.js +79 -0
- package/dist/lib.esm/Transaction/TransactionStateHistoryHelper.js +38 -0
- package/dist/lib.esm/Transaction/TransactionStateManager.js +294 -0
- package/dist/lib.esm/Transaction/TransactionUtils.js +326 -0
- package/dist/lib.esm/index.js +33 -0
- package/dist/lib.esm/utils/abis.js +505 -0
- package/dist/lib.esm/utils/constants.js +323 -0
- package/dist/lib.esm/utils/contractAddresses.js +14 -0
- package/dist/lib.esm/utils/conversionUtils.js +218 -0
- package/dist/lib.esm/utils/helpers.js +227 -0
- package/dist/lib.esm/utils/lodashUtils.js +21 -0
- package/dist/types/Account/AccountTrackerController.d.ts +5 -5
- package/dist/types/Block/PollingBlockTracker.d.ts +1 -2
- package/dist/types/Currency/CurrencyController.d.ts +1 -1
- package/dist/types/Gas/GasFeeController.d.ts +3 -3
- package/dist/types/Gas/gasUtil.d.ts +1 -1
- package/dist/types/Keyring/KeyringController.d.ts +3 -5
- package/dist/types/Message/AbstractMessageController.d.ts +5 -6
- package/dist/types/Message/AddChainController.d.ts +4 -4
- package/dist/types/Message/MessageController.d.ts +4 -4
- package/dist/types/Message/PersonalMessageController.d.ts +4 -4
- package/dist/types/Message/SwitchChainController.d.ts +4 -4
- package/dist/types/Message/TypedMessageController.d.ts +6 -7
- package/dist/types/Message/utils.d.ts +2 -7
- package/dist/types/Network/NetworkController.d.ts +4 -4
- package/dist/types/Network/cacheIdentifier.d.ts +1 -1
- package/dist/types/Network/createEthereumMiddleware.d.ts +2 -18
- package/dist/types/Network/createJsonRpcClient.d.ts +2 -2
- package/dist/types/Nfts/NftsController.d.ts +2 -2
- package/dist/types/Preferences/PreferencesController.d.ts +4 -4
- package/dist/types/Tokens/TokensController.d.ts +3 -3
- package/dist/types/Transaction/NonceTracker.d.ts +5 -5
- package/dist/types/Transaction/PendingTransactionTracker.d.ts +5 -5
- package/dist/types/Transaction/TransactionController.d.ts +12 -12
- package/dist/types/Transaction/TransactionGasUtil.d.ts +4 -4
- package/dist/types/Transaction/TransactionStateManager.d.ts +3 -3
- package/dist/types/Transaction/TransactionUtils.d.ts +1 -1
- package/dist/types/index.d.ts +12 -14
- package/dist/types/utils/constants.d.ts +1 -5
- package/dist/types/utils/helpers.d.ts +7 -4
- package/dist/types/utils/interfaces.d.ts +43 -23
- package/package.json +22 -10
- package/dist/types/Message/DecryptMessageController.d.ts +0 -20
- package/dist/types/Message/EncryptionPublicKeyController.d.ts +0 -20
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { randomId } from '@toruslabs/base-controllers';
|
|
4
|
+
import log from 'loglevel';
|
|
5
|
+
import { MessageStatus, METHOD_TYPES, MESSAGE_EVENTS } from '../utils/constants.js';
|
|
6
|
+
import { AbstractMessageController } from './AbstractMessageController.js';
|
|
7
|
+
import { validateSignMessageData, normalizeMessageData } from './utils.js';
|
|
8
|
+
|
|
9
|
+
class MessageController extends AbstractMessageController {
|
|
10
|
+
constructor({
|
|
11
|
+
config,
|
|
12
|
+
state,
|
|
13
|
+
signMessage,
|
|
14
|
+
getNetworkIdentifier
|
|
15
|
+
}) {
|
|
16
|
+
super({
|
|
17
|
+
config,
|
|
18
|
+
state,
|
|
19
|
+
getNetworkIdentifier
|
|
20
|
+
});
|
|
21
|
+
_defineProperty(this, "name", "MessageController");
|
|
22
|
+
_defineProperty(this, "signMessage", void 0);
|
|
23
|
+
this.signMessage = signMessage;
|
|
24
|
+
this.initialize();
|
|
25
|
+
}
|
|
26
|
+
async processSignMessage(messageId) {
|
|
27
|
+
const msgObject = this.getMessage(messageId);
|
|
28
|
+
if (!msgObject) {
|
|
29
|
+
throw new Error(`Message not found`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const cleanMsgParams = await this.approveMessage(messageId, msgObject.messageParams);
|
|
33
|
+
const rawSig = await this.signMessage(cleanMsgParams.data, cleanMsgParams.from);
|
|
34
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
35
|
+
rawSig
|
|
36
|
+
}));
|
|
37
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
38
|
+
return rawSig;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
log.error(error);
|
|
41
|
+
msgObject.error = (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString());
|
|
42
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
46
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
47
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
48
|
+
}
|
|
49
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
50
|
+
validateSignMessageData(messageParams);
|
|
51
|
+
if (req) {
|
|
52
|
+
messageParams.origin = req.origin;
|
|
53
|
+
}
|
|
54
|
+
messageParams.data = normalizeMessageData(messageParams.data);
|
|
55
|
+
const messageId = messageParams.id || randomId();
|
|
56
|
+
const messageData = {
|
|
57
|
+
id: messageId,
|
|
58
|
+
messageParams,
|
|
59
|
+
status: MessageStatus.UNAPPROVED,
|
|
60
|
+
time: Date.now(),
|
|
61
|
+
type: METHOD_TYPES.ETH_SIGN
|
|
62
|
+
};
|
|
63
|
+
await this.addMessage(messageData);
|
|
64
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, {
|
|
65
|
+
messageData,
|
|
66
|
+
req
|
|
67
|
+
});
|
|
68
|
+
return messageId;
|
|
69
|
+
}
|
|
70
|
+
prepMessageForSigning(messageParams) {
|
|
71
|
+
return Promise.resolve(messageParams);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { MessageController };
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { randomId } from '@toruslabs/base-controllers';
|
|
4
|
+
import log from 'loglevel';
|
|
5
|
+
import { MessageStatus, METHOD_TYPES, MESSAGE_EVENTS } from '../utils/constants.js';
|
|
6
|
+
import { AbstractMessageController } from './AbstractMessageController.js';
|
|
7
|
+
import { validateSignMessageData, normalizeMessageData } from './utils.js';
|
|
8
|
+
|
|
9
|
+
class PersonalMessageController extends AbstractMessageController {
|
|
10
|
+
constructor({
|
|
11
|
+
config,
|
|
12
|
+
state,
|
|
13
|
+
signPersonalMessage,
|
|
14
|
+
getNetworkIdentifier
|
|
15
|
+
}) {
|
|
16
|
+
super({
|
|
17
|
+
config,
|
|
18
|
+
state,
|
|
19
|
+
getNetworkIdentifier
|
|
20
|
+
});
|
|
21
|
+
_defineProperty(this, "name", "PersonalMessageController");
|
|
22
|
+
_defineProperty(this, "signPersonalMessage", void 0);
|
|
23
|
+
this.signPersonalMessage = signPersonalMessage;
|
|
24
|
+
this.initialize();
|
|
25
|
+
}
|
|
26
|
+
async processSignPersonalMessage(messageId) {
|
|
27
|
+
const msgObject = this.getMessage(messageId);
|
|
28
|
+
if (!msgObject) {
|
|
29
|
+
throw new Error(`Message not found`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const cleanMsgParams = await this.approveMessage(messageId, msgObject.messageParams);
|
|
33
|
+
const rawSig = await this.signPersonalMessage(cleanMsgParams.data, cleanMsgParams.from);
|
|
34
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
35
|
+
rawSig
|
|
36
|
+
}));
|
|
37
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
38
|
+
return rawSig;
|
|
39
|
+
} catch (error) {
|
|
40
|
+
log.error(error);
|
|
41
|
+
msgObject.error = (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString());
|
|
42
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
46
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
47
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
48
|
+
}
|
|
49
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
50
|
+
validateSignMessageData(messageParams);
|
|
51
|
+
if (req) {
|
|
52
|
+
messageParams.origin = req.origin;
|
|
53
|
+
}
|
|
54
|
+
messageParams.data = normalizeMessageData(messageParams.data);
|
|
55
|
+
const messageId = messageParams.id || randomId();
|
|
56
|
+
const messageData = {
|
|
57
|
+
id: messageId,
|
|
58
|
+
messageParams,
|
|
59
|
+
status: MessageStatus.UNAPPROVED,
|
|
60
|
+
time: Date.now(),
|
|
61
|
+
type: METHOD_TYPES.PERSONAL_SIGN
|
|
62
|
+
};
|
|
63
|
+
await this.addMessage(messageData);
|
|
64
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, {
|
|
65
|
+
messageData,
|
|
66
|
+
req
|
|
67
|
+
});
|
|
68
|
+
return messageId;
|
|
69
|
+
}
|
|
70
|
+
prepMessageForSigning(messageParams) {
|
|
71
|
+
return Promise.resolve(messageParams);
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
export { PersonalMessageController };
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { randomId } from '@toruslabs/base-controllers';
|
|
4
|
+
import log from 'loglevel';
|
|
5
|
+
import { MessageStatus, METHOD_TYPES, MESSAGE_EVENTS } from '../utils/constants.js';
|
|
6
|
+
import { AbstractMessageController } from './AbstractMessageController.js';
|
|
7
|
+
import { validateSwitchChainData } from './utils.js';
|
|
8
|
+
|
|
9
|
+
class SwitchChainController extends AbstractMessageController {
|
|
10
|
+
constructor({
|
|
11
|
+
config,
|
|
12
|
+
state,
|
|
13
|
+
getNetworkIdentifier,
|
|
14
|
+
switchChain
|
|
15
|
+
}) {
|
|
16
|
+
super({
|
|
17
|
+
config,
|
|
18
|
+
state,
|
|
19
|
+
getNetworkIdentifier
|
|
20
|
+
});
|
|
21
|
+
_defineProperty(this, "name", "SwitchChainController");
|
|
22
|
+
_defineProperty(this, "switchChain", void 0);
|
|
23
|
+
this.switchChain = switchChain;
|
|
24
|
+
this.initialize();
|
|
25
|
+
}
|
|
26
|
+
async processSwitchChain(messageId) {
|
|
27
|
+
const msgObject = this.getMessage(messageId);
|
|
28
|
+
if (!msgObject) {
|
|
29
|
+
throw new Error(`Message not found`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
await this.approveMessage(messageId, msgObject.messageParams);
|
|
33
|
+
this.switchChain({
|
|
34
|
+
chainId: msgObject.messageParams.chainId
|
|
35
|
+
});
|
|
36
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
37
|
+
rawSig: JSON.stringify(msgObject.messageParams)
|
|
38
|
+
}));
|
|
39
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
40
|
+
return null;
|
|
41
|
+
} catch (error) {
|
|
42
|
+
log.error(error);
|
|
43
|
+
msgObject.error = (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString());
|
|
44
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
48
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
49
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
50
|
+
}
|
|
51
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
52
|
+
validateSwitchChainData(messageParams);
|
|
53
|
+
if (req) {
|
|
54
|
+
messageParams.origin = req.origin;
|
|
55
|
+
}
|
|
56
|
+
const messageId = messageParams.id || randomId();
|
|
57
|
+
const messageData = {
|
|
58
|
+
id: messageId,
|
|
59
|
+
messageParams,
|
|
60
|
+
status: MessageStatus.UNAPPROVED,
|
|
61
|
+
time: Date.now(),
|
|
62
|
+
type: METHOD_TYPES.SWITCH_CHAIN
|
|
63
|
+
};
|
|
64
|
+
await this.addMessage(messageData);
|
|
65
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, {
|
|
66
|
+
messageData,
|
|
67
|
+
req
|
|
68
|
+
});
|
|
69
|
+
return messageId;
|
|
70
|
+
}
|
|
71
|
+
prepMessageForSigning(messageParams) {
|
|
72
|
+
return Promise.resolve(messageParams);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
export { SwitchChainController };
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
2
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
3
|
+
import { randomId } from '@toruslabs/base-controllers';
|
|
4
|
+
import log from 'loglevel';
|
|
5
|
+
import { MessageStatus, METHOD_TYPES, MESSAGE_EVENTS } from '../utils/constants.js';
|
|
6
|
+
import { AbstractMessageController } from './AbstractMessageController.js';
|
|
7
|
+
import { validateTypedSignMessageDataV4 } from './utils.js';
|
|
8
|
+
|
|
9
|
+
class TypedMessageController extends AbstractMessageController {
|
|
10
|
+
constructor({
|
|
11
|
+
config,
|
|
12
|
+
state,
|
|
13
|
+
signTypedData,
|
|
14
|
+
getNetworkIdentifier
|
|
15
|
+
}) {
|
|
16
|
+
super({
|
|
17
|
+
config,
|
|
18
|
+
state,
|
|
19
|
+
getNetworkIdentifier
|
|
20
|
+
});
|
|
21
|
+
_defineProperty(this, "name", "TypedMessageController");
|
|
22
|
+
_defineProperty(this, "signTypedData", void 0);
|
|
23
|
+
this.signTypedData = signTypedData;
|
|
24
|
+
this.initialize();
|
|
25
|
+
}
|
|
26
|
+
async processSignTypedMessage(messageId) {
|
|
27
|
+
const msgObject = this.getMessage(messageId);
|
|
28
|
+
if (!msgObject) {
|
|
29
|
+
throw new Error(`Message not found`);
|
|
30
|
+
}
|
|
31
|
+
try {
|
|
32
|
+
const cleanMsgParams = await this.approveMessage(messageId, msgObject.messageParams);
|
|
33
|
+
const msgData = JSON.parse(cleanMsgParams.data);
|
|
34
|
+
const rawSig = await this.signTypedData(msgData, cleanMsgParams.from);
|
|
35
|
+
this.updateMessage(_objectSpread(_objectSpread({}, msgObject), {}, {
|
|
36
|
+
rawSig
|
|
37
|
+
}));
|
|
38
|
+
this.setMessageStatus(messageId, MessageStatus.SIGNED);
|
|
39
|
+
return rawSig;
|
|
40
|
+
} catch (error) {
|
|
41
|
+
log.error(error);
|
|
42
|
+
msgObject.error = (error === null || error === void 0 ? void 0 : error.message) || (error === null || error === void 0 ? void 0 : error.toString());
|
|
43
|
+
this.setMessageStatus(messageId, MessageStatus.FAILED);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
async addNewUnapprovedMessage(messageParams, req) {
|
|
47
|
+
await this.addUnapprovedMessage(messageParams, req);
|
|
48
|
+
return this.waitForFinishStatus(messageParams, this.name);
|
|
49
|
+
}
|
|
50
|
+
async addUnapprovedMessage(messageParams, req) {
|
|
51
|
+
const currentChainId = this.getNetworkIdentifier();
|
|
52
|
+
await validateTypedSignMessageDataV4(messageParams, currentChainId);
|
|
53
|
+
if (typeof messageParams.data !== "string") {
|
|
54
|
+
messageParams.data = JSON.stringify(messageParams.data);
|
|
55
|
+
}
|
|
56
|
+
if (req) {
|
|
57
|
+
messageParams.origin = req.origin;
|
|
58
|
+
}
|
|
59
|
+
const messageId = messageParams.id || randomId();
|
|
60
|
+
const messageData = {
|
|
61
|
+
id: messageId,
|
|
62
|
+
messageParams,
|
|
63
|
+
status: MessageStatus.UNAPPROVED,
|
|
64
|
+
time: Date.now(),
|
|
65
|
+
type: METHOD_TYPES.ETH_SIGN_TYPED_DATA_V4
|
|
66
|
+
};
|
|
67
|
+
await this.addMessage(messageData);
|
|
68
|
+
this.emit(MESSAGE_EVENTS.UNAPPROVED_MESSAGE, {
|
|
69
|
+
messageData,
|
|
70
|
+
req
|
|
71
|
+
});
|
|
72
|
+
return messageId;
|
|
73
|
+
}
|
|
74
|
+
prepMessageForSigning(messageParams) {
|
|
75
|
+
return Promise.resolve(messageParams);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export { TypedMessageController };
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { isValidAddress, stripHexPrefix, addHexPrefix, bytesToHex } from '@ethereumjs/util';
|
|
2
|
+
import { isHexString, JsonRpcProvider, toQuantity } from 'ethers';
|
|
3
|
+
|
|
4
|
+
const hexRe = /^[0-9A-Fa-f]+$/gu;
|
|
5
|
+
function validateAddress(address, propertyName) {
|
|
6
|
+
if (!address || typeof address !== "string" || !isValidAddress(address)) {
|
|
7
|
+
throw new Error(`Invalid "${propertyName}" address: ${address} must be a valid string.`);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
function validateSignMessageData(messageData) {
|
|
11
|
+
const {
|
|
12
|
+
from,
|
|
13
|
+
data
|
|
14
|
+
} = messageData;
|
|
15
|
+
validateAddress(from, "from");
|
|
16
|
+
if (!data || typeof data !== "string") {
|
|
17
|
+
throw new Error(`Invalid message "data": ${data} must be a valid string.`);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function normalizeMessageData(data) {
|
|
21
|
+
try {
|
|
22
|
+
const stripped = stripHexPrefix(data);
|
|
23
|
+
if (stripped.match(hexRe)) {
|
|
24
|
+
return addHexPrefix(stripped);
|
|
25
|
+
}
|
|
26
|
+
} catch (e) {}
|
|
27
|
+
return bytesToHex(Buffer.from(data, "utf8"));
|
|
28
|
+
}
|
|
29
|
+
async function validateTypedSignMessageDataV4(messageData, currentChainId) {
|
|
30
|
+
validateAddress(messageData.from, "from");
|
|
31
|
+
if (!messageData.data || Array.isArray(messageData.data) || typeof messageData.data !== "object" && typeof messageData.data !== "string") {
|
|
32
|
+
throw new Error(`Invalid message "data": Must be a valid string or object.`);
|
|
33
|
+
}
|
|
34
|
+
let data;
|
|
35
|
+
if (typeof messageData.data === "object") {
|
|
36
|
+
data = messageData.data;
|
|
37
|
+
} else {
|
|
38
|
+
try {
|
|
39
|
+
data = JSON.parse(messageData.data);
|
|
40
|
+
} catch (e) {
|
|
41
|
+
throw new Error("Data must be passed as a valid JSON string.");
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
if (!currentChainId) {
|
|
45
|
+
throw new Error("Current chainId cannot be null or undefined.");
|
|
46
|
+
}
|
|
47
|
+
let {
|
|
48
|
+
chainId
|
|
49
|
+
} = data.domain;
|
|
50
|
+
if (chainId) {
|
|
51
|
+
if (typeof chainId === "string") {
|
|
52
|
+
chainId = parseInt(chainId, chainId.startsWith("0x") ? 16 : 10);
|
|
53
|
+
}
|
|
54
|
+
const activeChainId = parseInt(currentChainId, 16);
|
|
55
|
+
if (Number.isNaN(activeChainId)) {
|
|
56
|
+
throw new Error(`Cannot sign messages for chainId "${chainId}", because MetaMask is switching networks.`);
|
|
57
|
+
}
|
|
58
|
+
if (chainId !== activeChainId) {
|
|
59
|
+
throw new Error(`Provided chainId "${chainId}" must match the active chainId "${activeChainId}"`);
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
async function validateAddChainData(data) {
|
|
64
|
+
const {
|
|
65
|
+
chainId,
|
|
66
|
+
rpcUrls,
|
|
67
|
+
nativeCurrency
|
|
68
|
+
} = data || {};
|
|
69
|
+
if (!chainId) {
|
|
70
|
+
throw new Error("Invalid add chain params: please pass chainId in params");
|
|
71
|
+
}
|
|
72
|
+
if (!isHexString(chainId)) {
|
|
73
|
+
throw new Error("Invalid add chain params: please pass a valid hex chainId in params, for: ex: 0x1");
|
|
74
|
+
}
|
|
75
|
+
if (!rpcUrls || rpcUrls.length === 0) throw new Error("params.rpcUrls not provided");
|
|
76
|
+
if (!nativeCurrency) throw new Error("params.nativeCurrency not provided");
|
|
77
|
+
const {
|
|
78
|
+
name,
|
|
79
|
+
symbol,
|
|
80
|
+
decimals
|
|
81
|
+
} = nativeCurrency;
|
|
82
|
+
if (!name) throw new Error("params.nativeCurrency.name not provided");
|
|
83
|
+
if (!symbol) throw new Error("params.nativeCurrency.symbol not provided");
|
|
84
|
+
if (decimals === undefined) throw new Error("params.nativeCurrency.decimals not provided");
|
|
85
|
+
const _web3 = new JsonRpcProvider(rpcUrls[0], "any");
|
|
86
|
+
const {
|
|
87
|
+
chainId: networkChainID
|
|
88
|
+
} = await _web3.getNetwork();
|
|
89
|
+
if (Number.parseInt(networkChainID.toString()) !== Number.parseInt(chainId, 16)) {
|
|
90
|
+
throw new Error(`Provided rpc url's chainId version is not matching with provided chainId, expected: ${toQuantity(networkChainID)}, received: ${chainId}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function validateSwitchChainData(data) {
|
|
94
|
+
const {
|
|
95
|
+
chainId
|
|
96
|
+
} = data || {};
|
|
97
|
+
if (!chainId) {
|
|
98
|
+
throw new Error("Invalid switch chain params: please pass chainId in params");
|
|
99
|
+
}
|
|
100
|
+
if (!isHexString(chainId)) {
|
|
101
|
+
throw new Error("Invalid switch chain params: please pass a valid hex chainId in params, for: ex: 0x1");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export { normalizeMessageData, validateAddChainData, validateAddress, validateSignMessageData, validateSwitchChainData, validateTypedSignMessageDataV4 };
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import _objectWithoutProperties from '@babel/runtime/helpers/objectWithoutProperties';
|
|
2
|
+
import _objectSpread from '@babel/runtime/helpers/objectSpread2';
|
|
3
|
+
import _defineProperty from '@babel/runtime/helpers/defineProperty';
|
|
4
|
+
import { BaseController, createSwappableProxy, createEventEmitterProxy } from '@toruslabs/base-controllers';
|
|
5
|
+
import { JRPCEngine, providerFromEngine } from '@web3auth/auth';
|
|
6
|
+
import { Mutex } from 'async-mutex';
|
|
7
|
+
import log from 'loglevel';
|
|
8
|
+
import { SUPPORTED_NETWORKS, MAINNET_CHAIN_ID } from '../utils/constants.js';
|
|
9
|
+
import { createEthereumMiddleware } from './createEthereumMiddleware.js';
|
|
10
|
+
import { createJsonRpcClient } from './createJsonRpcClient.js';
|
|
11
|
+
|
|
12
|
+
const _excluded = ["chainId", "rpcTarget"];
|
|
13
|
+
class NetworkController extends BaseController {
|
|
14
|
+
constructor({
|
|
15
|
+
config,
|
|
16
|
+
state
|
|
17
|
+
}) {
|
|
18
|
+
super({
|
|
19
|
+
config,
|
|
20
|
+
state
|
|
21
|
+
});
|
|
22
|
+
_defineProperty(this, "name", "NetworkController");
|
|
23
|
+
_defineProperty(this, "providerProxy", void 0);
|
|
24
|
+
_defineProperty(this, "blockTrackerProxy", void 0);
|
|
25
|
+
_defineProperty(this, "mutex", new Mutex());
|
|
26
|
+
_defineProperty(this, "provider", null);
|
|
27
|
+
_defineProperty(this, "blockTracker", null);
|
|
28
|
+
_defineProperty(this, "baseProviderHandlers", void 0);
|
|
29
|
+
this.defaultState = {
|
|
30
|
+
chainId: "loading",
|
|
31
|
+
properties: {
|
|
32
|
+
EIPS_1559: undefined
|
|
33
|
+
},
|
|
34
|
+
providerConfig: SUPPORTED_NETWORKS[MAINNET_CHAIN_ID]
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
// when a new network is set,
|
|
38
|
+
// we set to loading first and
|
|
39
|
+
// then when connection succeeds,
|
|
40
|
+
// we update the network
|
|
41
|
+
this.initialize();
|
|
42
|
+
}
|
|
43
|
+
getNetworkIdentifier() {
|
|
44
|
+
return this.state.chainId;
|
|
45
|
+
}
|
|
46
|
+
getNetworkRPCUrl() {
|
|
47
|
+
return this.state.providerConfig.rpcTarget;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Called by orchestrator once while initializing the class
|
|
52
|
+
* @param providerHandlers - JRPC handlers for provider
|
|
53
|
+
* @returns - provider - Returns the providerProxy
|
|
54
|
+
*/
|
|
55
|
+
initializeProvider(providerHandlers) {
|
|
56
|
+
this.baseProviderHandlers = providerHandlers;
|
|
57
|
+
this.configureProvider();
|
|
58
|
+
this.lookupNetwork(); // Not awaiting this, because we don't want to block the initialization
|
|
59
|
+
return this.providerProxy;
|
|
60
|
+
}
|
|
61
|
+
getProvider() {
|
|
62
|
+
return this.providerProxy;
|
|
63
|
+
}
|
|
64
|
+
getBlockTracker() {
|
|
65
|
+
return this.blockTrackerProxy;
|
|
66
|
+
}
|
|
67
|
+
getProviderConfig() {
|
|
68
|
+
return this.state.providerConfig;
|
|
69
|
+
}
|
|
70
|
+
setProviderConfig(config) {
|
|
71
|
+
this.update({
|
|
72
|
+
providerConfig: _objectSpread({}, config)
|
|
73
|
+
});
|
|
74
|
+
this.refreshNetwork();
|
|
75
|
+
}
|
|
76
|
+
async getEIP1559Compatibility() {
|
|
77
|
+
const {
|
|
78
|
+
EIPS_1559
|
|
79
|
+
} = this.state.properties;
|
|
80
|
+
// log.info('checking eip 1559 compatibility')
|
|
81
|
+
if (EIPS_1559 !== undefined) {
|
|
82
|
+
return EIPS_1559;
|
|
83
|
+
}
|
|
84
|
+
const latestBlock = await this.blockTracker.getLatestBlock();
|
|
85
|
+
const supportsEIP1559 = latestBlock && latestBlock.baseFeePerGas !== undefined;
|
|
86
|
+
this.update({
|
|
87
|
+
properties: {
|
|
88
|
+
EIPS_1559: supportsEIP1559
|
|
89
|
+
}
|
|
90
|
+
});
|
|
91
|
+
return supportsEIP1559;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Refreshes the current network code
|
|
96
|
+
*/
|
|
97
|
+
async lookupNetwork() {
|
|
98
|
+
const {
|
|
99
|
+
chainId,
|
|
100
|
+
rpcTarget
|
|
101
|
+
} = this.getProviderConfig();
|
|
102
|
+
if (!chainId || !rpcTarget || !this.provider) {
|
|
103
|
+
this.update({
|
|
104
|
+
chainId: "loading",
|
|
105
|
+
properties: {}
|
|
106
|
+
});
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const releaseLock = await this.mutex.acquire();
|
|
110
|
+
try {
|
|
111
|
+
// use eth_chainId
|
|
112
|
+
const [networkChainId] = await Promise.all([this.provider.request({
|
|
113
|
+
method: "eth_chainId"
|
|
114
|
+
}), this.getEIP1559Compatibility()]);
|
|
115
|
+
log.info("network fetched chain id", networkChainId);
|
|
116
|
+
// update chain ID
|
|
117
|
+
this.update({
|
|
118
|
+
chainId: networkChainId
|
|
119
|
+
});
|
|
120
|
+
this.emit("networkDidChange");
|
|
121
|
+
} catch {
|
|
122
|
+
this.update({
|
|
123
|
+
chainId: "loading"
|
|
124
|
+
});
|
|
125
|
+
} finally {
|
|
126
|
+
releaseLock();
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
configureProvider() {
|
|
130
|
+
const _this$getProviderConf = this.getProviderConfig(),
|
|
131
|
+
{
|
|
132
|
+
chainId,
|
|
133
|
+
rpcTarget
|
|
134
|
+
} = _this$getProviderConf,
|
|
135
|
+
rest = _objectWithoutProperties(_this$getProviderConf, _excluded);
|
|
136
|
+
if (!chainId || !rpcTarget) {
|
|
137
|
+
throw new Error("chainId and rpcTarget must be provider in providerConfig");
|
|
138
|
+
}
|
|
139
|
+
this.configureStandardProvider(_objectSpread({
|
|
140
|
+
chainId,
|
|
141
|
+
rpcTarget
|
|
142
|
+
}, rest));
|
|
143
|
+
}
|
|
144
|
+
setNetworkClient({
|
|
145
|
+
networkMiddleware,
|
|
146
|
+
blockTracker
|
|
147
|
+
}) {
|
|
148
|
+
const ethereumMiddleware = createEthereumMiddleware(this.baseProviderHandlers);
|
|
149
|
+
const engine = new JRPCEngine();
|
|
150
|
+
engine.push(ethereumMiddleware);
|
|
151
|
+
engine.push(networkMiddleware);
|
|
152
|
+
const provider = providerFromEngine(engine);
|
|
153
|
+
this.setProvider({
|
|
154
|
+
provider,
|
|
155
|
+
blockTracker
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
setProvider({
|
|
159
|
+
provider,
|
|
160
|
+
blockTracker
|
|
161
|
+
}) {
|
|
162
|
+
if (this.providerProxy) {
|
|
163
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
164
|
+
// @ts-ignore
|
|
165
|
+
this.providerProxy.setTarget(provider);
|
|
166
|
+
} else {
|
|
167
|
+
this.providerProxy = createSwappableProxy(provider);
|
|
168
|
+
}
|
|
169
|
+
if (this.blockTrackerProxy) {
|
|
170
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
171
|
+
// @ts-ignore
|
|
172
|
+
this.blockTrackerProxy.setTarget(blockTracker);
|
|
173
|
+
} else {
|
|
174
|
+
this.blockTrackerProxy = createEventEmitterProxy(blockTracker, {
|
|
175
|
+
eventFilter: "skipInternal"
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// set new provider and blockTracker
|
|
180
|
+
this.provider = provider;
|
|
181
|
+
provider.setMaxListeners(10);
|
|
182
|
+
this.blockTracker = blockTracker;
|
|
183
|
+
}
|
|
184
|
+
configureStandardProvider(providerConfig) {
|
|
185
|
+
const networkClient = createJsonRpcClient(providerConfig, this.config);
|
|
186
|
+
log.info("networkClient", networkClient);
|
|
187
|
+
this.setNetworkClient(networkClient);
|
|
188
|
+
}
|
|
189
|
+
refreshNetwork() {
|
|
190
|
+
this.update({
|
|
191
|
+
chainId: "loading",
|
|
192
|
+
properties: {}
|
|
193
|
+
});
|
|
194
|
+
this.configureProvider();
|
|
195
|
+
this.lookupNetwork();
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
export { NetworkController };
|