@exodus/web3-ethereum-utils 3.26.1 → 3.27.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/lib/createSimulateMessage.d.ts +8 -0
- package/lib/createSimulateMessage.js +26 -0
- package/lib/simulation/common.d.ts +8 -0
- package/lib/simulation/common.js +26 -0
- package/lib/simulation/getMessageType.d.ts +6 -0
- package/lib/simulation/getMessageType.js +21 -0
- package/lib/simulation/simulateMessage.d.ts +7 -0
- package/lib/simulation/simulateMessage.js +46 -0
- package/lib/simulation/simulateTransactions.d.ts +0 -7
- package/lib/simulation/simulateTransactions.js +1 -26
- package/lib/types.d.ts +19 -1
- package/package.json +3 -3
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import type { EthSimulateMessageParams } from './types.js';
|
|
2
|
+
import type { MessageSimulationResult } from '@exodus/web3-types';
|
|
3
|
+
export declare const createSimulateMessage: ({ apiEndpoint, headers, }?: {
|
|
4
|
+
apiEndpoint?: string | undefined;
|
|
5
|
+
headers?: {
|
|
6
|
+
'X-Api-Version': string;
|
|
7
|
+
} | undefined;
|
|
8
|
+
}) => ({ message, origin, asset, address, }: EthSimulateMessageParams) => Promise<MessageSimulationResult>;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { getMessageType } from './simulation/getMessageType.js';
|
|
2
|
+
import { simulateMessage } from './simulation/simulateMessage.js';
|
|
3
|
+
export const createSimulateMessage = ({ apiEndpoint = 'https://simulation.a.exodus.io/simulateMessage', headers = {
|
|
4
|
+
'X-Api-Version': '2023-06-05',
|
|
5
|
+
}, } = {}) => async ({ message, origin, asset, address, }) => {
|
|
6
|
+
const simulationResult = {
|
|
7
|
+
baseAssetName: asset.baseAssetName,
|
|
8
|
+
action: 'NONE',
|
|
9
|
+
};
|
|
10
|
+
const messageType = getMessageType(message);
|
|
11
|
+
if (messageType === 2 /* MessageTypeEnum.Unknown */) {
|
|
12
|
+
return simulationResult;
|
|
13
|
+
}
|
|
14
|
+
await simulateMessage({
|
|
15
|
+
address,
|
|
16
|
+
message: {
|
|
17
|
+
message,
|
|
18
|
+
messageType,
|
|
19
|
+
},
|
|
20
|
+
origin,
|
|
21
|
+
apiEndpoint,
|
|
22
|
+
headers,
|
|
23
|
+
simulationResult,
|
|
24
|
+
});
|
|
25
|
+
return simulationResult;
|
|
26
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export const BLOWFISH_EVM_CHAINS = {
|
|
2
|
+
ethereum: {
|
|
3
|
+
network: 'ethereum',
|
|
4
|
+
chain: 'mainnet',
|
|
5
|
+
},
|
|
6
|
+
matic: {
|
|
7
|
+
network: 'polygon',
|
|
8
|
+
chain: 'mainnet',
|
|
9
|
+
},
|
|
10
|
+
bsc: {
|
|
11
|
+
network: 'bnb',
|
|
12
|
+
chain: 'mainnet',
|
|
13
|
+
},
|
|
14
|
+
ethereumarbone: {
|
|
15
|
+
network: 'arbitrum',
|
|
16
|
+
chain: 'one',
|
|
17
|
+
},
|
|
18
|
+
optimism: {
|
|
19
|
+
network: 'optimism',
|
|
20
|
+
chain: 'goerli',
|
|
21
|
+
},
|
|
22
|
+
basemainnet: {
|
|
23
|
+
network: 'base',
|
|
24
|
+
chain: 'mainnet',
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
This utility figures out if the supplied message is plaint text to be signed or a structured typed data.
|
|
3
|
+
See for differences:
|
|
4
|
+
- https://docs.metamask.io/wallet/reference/personal_sign/
|
|
5
|
+
- https://docs.metamask.io/wallet/reference/eth_signtypeddata_v4/
|
|
6
|
+
|
|
7
|
+
The utility does not make any security checks (e.g. schema validation), it's supposed to be used only for choosing
|
|
8
|
+
a proper payload type for the simulation API.
|
|
9
|
+
*/
|
|
10
|
+
export const getMessageType = (message) => {
|
|
11
|
+
if (typeof message !== 'string') {
|
|
12
|
+
return 2 /* MessageTypeEnum.Unknown */;
|
|
13
|
+
}
|
|
14
|
+
try {
|
|
15
|
+
JSON.parse(message);
|
|
16
|
+
return 1 /* MessageTypeEnum.TypedData */;
|
|
17
|
+
}
|
|
18
|
+
catch (err) {
|
|
19
|
+
return 0 /* MessageTypeEnum.RawMessage */;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { SimulateMessageParams } from '../types.js';
|
|
2
|
+
import type { MessageSimulationResult, ScanMessageEvm200Response } from '@exodus/web3-types';
|
|
3
|
+
export declare const handleActionResponse: ({ simulationResult, response, }: {
|
|
4
|
+
simulationResult: MessageSimulationResult;
|
|
5
|
+
response: ScanMessageEvm200Response;
|
|
6
|
+
}) => void;
|
|
7
|
+
export declare const simulateMessage: ({ address, apiEndpoint, message, origin, headers, simulationResult, }: SimulateMessageParams) => Promise<void>;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { makeSimulationAPICall } from '@exodus/web3-utils';
|
|
2
|
+
import { BLOWFISH_EVM_CHAINS } from './common.js';
|
|
3
|
+
const convertToAPIPayload = ({ origin, address, message, }) => {
|
|
4
|
+
const restParameters = {
|
|
5
|
+
metadata: { origin },
|
|
6
|
+
userAccount: address,
|
|
7
|
+
};
|
|
8
|
+
if (message.messageType === 1 /* MessageTypeEnum.TypedData */) {
|
|
9
|
+
const kind = 'SIGN_TYPED_DATA';
|
|
10
|
+
return {
|
|
11
|
+
message: {
|
|
12
|
+
kind,
|
|
13
|
+
data: JSON.parse(message.message),
|
|
14
|
+
},
|
|
15
|
+
...restParameters,
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
const kind = 'SIGN_MESSAGE';
|
|
19
|
+
return {
|
|
20
|
+
message: {
|
|
21
|
+
kind,
|
|
22
|
+
rawMessage: message.message,
|
|
23
|
+
},
|
|
24
|
+
...restParameters,
|
|
25
|
+
};
|
|
26
|
+
};
|
|
27
|
+
export const handleActionResponse = ({ simulationResult, response, }) => {
|
|
28
|
+
if (['BLOCK', 'WARN'].includes(response.action)) {
|
|
29
|
+
simulationResult.action = response.action;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
export const simulateMessage = async ({ address, apiEndpoint, message, origin, headers, simulationResult, }) => {
|
|
33
|
+
const { network, chain } = BLOWFISH_EVM_CHAINS[simulationResult.baseAssetName];
|
|
34
|
+
const payload = convertToAPIPayload({ address, origin, message });
|
|
35
|
+
const response = await makeSimulationAPICall({
|
|
36
|
+
url: apiEndpoint,
|
|
37
|
+
chain,
|
|
38
|
+
network,
|
|
39
|
+
payload,
|
|
40
|
+
headers,
|
|
41
|
+
});
|
|
42
|
+
if (!response) {
|
|
43
|
+
return;
|
|
44
|
+
}
|
|
45
|
+
handleActionResponse({ simulationResult, response });
|
|
46
|
+
};
|
|
@@ -1,12 +1,5 @@
|
|
|
1
1
|
import type { EthAggregatedTransactionSimulationResult, EthSimulateTransactionParams } from '../types.js';
|
|
2
2
|
import type { CreateSimulateTransactionsParams } from '@exodus/web3-types';
|
|
3
|
-
interface BlowfishEvmChain {
|
|
4
|
-
[key: string]: {
|
|
5
|
-
network: string;
|
|
6
|
-
chain: string;
|
|
7
|
-
};
|
|
8
|
-
}
|
|
9
|
-
export declare const BLOWFISH_EVM_CHAINS: BlowfishEvmChain;
|
|
10
3
|
declare type SimulateTransactionParams = Required<Pick<CreateSimulateTransactionsParams, 'apiEndpoint'>> & Required<Pick<EthSimulateTransactionParams, 'transactions' | 'origin'>> & {
|
|
11
4
|
simulationResult: EthAggregatedTransactionSimulationResult;
|
|
12
5
|
headers?: Record<string, string>;
|
|
@@ -4,32 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
import { pick } from '@exodus/basic-utils';
|
|
6
6
|
import { createCurrency, makeSimulationAPICall } from '@exodus/web3-utils';
|
|
7
|
-
|
|
8
|
-
ethereum: {
|
|
9
|
-
network: 'ethereum',
|
|
10
|
-
chain: 'mainnet',
|
|
11
|
-
},
|
|
12
|
-
matic: {
|
|
13
|
-
network: 'polygon',
|
|
14
|
-
chain: 'mainnet',
|
|
15
|
-
},
|
|
16
|
-
bsc: {
|
|
17
|
-
network: 'bnb',
|
|
18
|
-
chain: 'mainnet',
|
|
19
|
-
},
|
|
20
|
-
ethereumarbone: {
|
|
21
|
-
network: 'arbitrum',
|
|
22
|
-
chain: 'one',
|
|
23
|
-
},
|
|
24
|
-
optimism: {
|
|
25
|
-
network: 'optimism',
|
|
26
|
-
chain: 'goerli',
|
|
27
|
-
},
|
|
28
|
-
basemainnet: {
|
|
29
|
-
network: 'base',
|
|
30
|
-
chain: 'mainnet',
|
|
31
|
-
},
|
|
32
|
-
};
|
|
7
|
+
import { BLOWFISH_EVM_CHAINS } from './common.js';
|
|
33
8
|
const convertTransactionToApiPayload = (transactions, origin) => ({
|
|
34
9
|
txObjects: transactions.map((transaction) => ({
|
|
35
10
|
from: transaction.from,
|
package/lib/types.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { MessageTypeEnum } from './simulation/getMessageType.js';
|
|
2
|
+
import type { AggregatedTransactionSimulationResult, Asset, BN, MessageSimulationResult, SimulateTransactionParams } from '@exodus/web3-types';
|
|
2
3
|
export interface EthTransactionParams {
|
|
3
4
|
chainId: number;
|
|
4
5
|
from: string;
|
|
@@ -50,3 +51,20 @@ export interface EthFeeDetails {
|
|
|
50
51
|
gasPrice: BN;
|
|
51
52
|
}
|
|
52
53
|
export declare type EthAggregatedTransactionSimulationResult = AggregatedTransactionSimulationResult<EthFeeDetails>;
|
|
54
|
+
export interface EthSimulateMessageParams {
|
|
55
|
+
asset: Asset;
|
|
56
|
+
message: string;
|
|
57
|
+
origin: string;
|
|
58
|
+
address: string;
|
|
59
|
+
}
|
|
60
|
+
export interface SimulateMessageParams {
|
|
61
|
+
address: EthSimulateMessageParams['address'];
|
|
62
|
+
message: {
|
|
63
|
+
message: string;
|
|
64
|
+
messageType: MessageTypeEnum.RawMessage | MessageTypeEnum.TypedData;
|
|
65
|
+
};
|
|
66
|
+
origin: EthSimulateMessageParams['origin'];
|
|
67
|
+
apiEndpoint: string;
|
|
68
|
+
headers: Record<string, string>;
|
|
69
|
+
simulationResult: MessageSimulationResult;
|
|
70
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@exodus/web3-ethereum-utils",
|
|
3
3
|
"description": "Web3 Ethereum utils.",
|
|
4
|
-
"version": "3.
|
|
4
|
+
"version": "3.27.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"sideEffects": false,
|
|
7
7
|
"module": "lib/index.js",
|
|
@@ -32,8 +32,8 @@
|
|
|
32
32
|
"@exodus/basic-utils": "^2.5.1",
|
|
33
33
|
"@exodus/ethereumjs-common": "^2.4.0-exodus.6",
|
|
34
34
|
"@exodus/ethereumjs-tx": "^3.3.0-exodus.6",
|
|
35
|
-
"@exodus/web3-utils": "^1.
|
|
35
|
+
"@exodus/web3-utils": "^1.44.0",
|
|
36
36
|
"@metamask/eth-sig-util": "^4.0.1"
|
|
37
37
|
},
|
|
38
|
-
"gitHead": "
|
|
38
|
+
"gitHead": "c9a274287bfba81c264dd9d48dac15d9be56bd47"
|
|
39
39
|
}
|