@avalabs/evm-module 0.0.16 → 0.0.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/.turbo/turbo-build.log +10 -10
- package/.turbo/turbo-lint.log +1 -1
- package/.turbo/turbo-test.log +25 -24
- package/CHANGELOG.md +8 -0
- package/dist/index.cjs +25 -22
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +9 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.js +22 -21
- package/dist/index.js.map +1 -1
- package/package.json +4 -3
- package/src/constants.ts +1 -0
- package/src/handlers/eth-send-transaction/eth-send-transaction.test.ts +232 -1
- package/src/handlers/eth-send-transaction/eth-send-transaction.ts +24 -5
- package/src/handlers/eth-sign/eth-sign.test.ts +138 -35
- package/src/handlers/eth-sign/eth-sign.ts +29 -8
- package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.test.ts +2 -2
- package/src/handlers/get-balances/evm-balance-service/get-erc20-balances.ts +4 -6
- package/src/handlers/get-balances/get-balances.test.ts +0 -5
- package/src/handlers/get-balances/get-balances.ts +14 -3
- package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.test.ts +0 -1
- package/src/handlers/get-balances/glacier-balance-service/get-erc20-balances.ts +10 -7
- package/src/handlers/get-tokens/get-tokens.test.ts +6 -6
- package/src/module.ts +2 -0
- package/src/types.ts +9 -0
- package/src/utils/parse-erc20-transaction-type.ts +35 -0
- package/src/utils/process-transaction-simulation.test.ts +105 -0
- package/src/utils/process-transaction-simulation.ts +294 -0
- package/src/utils/scan-transaction.ts +63 -0
- package/src/handlers/eth-sign/schemas/parse-request-params.ts +0 -90
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
import Blockaid from '@blockaid/client';
|
|
2
|
+
import type { TransactionParams } from '../types';
|
|
3
|
+
import {
|
|
4
|
+
type NetworkContractToken,
|
|
5
|
+
type NetworkToken,
|
|
6
|
+
TokenType,
|
|
7
|
+
AlertType,
|
|
8
|
+
type Alert,
|
|
9
|
+
type BalanceChange,
|
|
10
|
+
type TokenApproval,
|
|
11
|
+
type TokenDiff,
|
|
12
|
+
type TokenDiffItem,
|
|
13
|
+
type TokenApprovals,
|
|
14
|
+
type RpcRequest,
|
|
15
|
+
RpcMethod,
|
|
16
|
+
} from '@avalabs/vm-module-types';
|
|
17
|
+
import { balanceToDisplayValue, numberToBN } from '@avalabs/utils-sdk';
|
|
18
|
+
import { isHexString } from 'ethers';
|
|
19
|
+
import { scanJsonRpc, scanTransaction } from './scan-transaction';
|
|
20
|
+
|
|
21
|
+
export const processTransactionSimulation = async ({
|
|
22
|
+
request,
|
|
23
|
+
dAppUrl,
|
|
24
|
+
params,
|
|
25
|
+
chainId,
|
|
26
|
+
proxyApiUrl,
|
|
27
|
+
}: {
|
|
28
|
+
request: RpcRequest;
|
|
29
|
+
dAppUrl?: string;
|
|
30
|
+
params: TransactionParams;
|
|
31
|
+
chainId: number;
|
|
32
|
+
proxyApiUrl: string;
|
|
33
|
+
}) => {
|
|
34
|
+
const { validation, simulation } = await scanTransaction({
|
|
35
|
+
proxyApiUrl,
|
|
36
|
+
chainId,
|
|
37
|
+
params,
|
|
38
|
+
domain: dAppUrl,
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
let alert: Alert | undefined;
|
|
42
|
+
if (!validation || validation.result_type === 'Error' || validation.result_type === 'Warning') {
|
|
43
|
+
alert = {
|
|
44
|
+
type: AlertType.WARNING,
|
|
45
|
+
details: {
|
|
46
|
+
title: 'Suspicious Transaction',
|
|
47
|
+
description: 'Use caution, this transaction may be malicious.',
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
} else if (validation.result_type === 'Malicious') {
|
|
51
|
+
alert = {
|
|
52
|
+
type: AlertType.DANGER,
|
|
53
|
+
details: {
|
|
54
|
+
title: 'Scam Transaction',
|
|
55
|
+
description: 'This transaction is malicious, do not proceed.',
|
|
56
|
+
actionTitles: {
|
|
57
|
+
reject: 'Reject Transaction',
|
|
58
|
+
proceed: 'Proceed Anyway',
|
|
59
|
+
},
|
|
60
|
+
},
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
let balanceChange: BalanceChange | undefined;
|
|
65
|
+
let tokenApprovals: TokenApprovals | undefined;
|
|
66
|
+
|
|
67
|
+
if (simulation?.status === 'Success') {
|
|
68
|
+
tokenApprovals = processTokenApprovals(request, simulation.account_summary.exposures);
|
|
69
|
+
balanceChange = processBalanceChange(simulation.account_summary.assets_diffs);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
return { alert, balanceChange, tokenApprovals };
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const processTokenApprovals = (
|
|
76
|
+
request: RpcRequest,
|
|
77
|
+
exposures: Blockaid.AddressAssetExposure[],
|
|
78
|
+
): TokenApprovals | undefined => {
|
|
79
|
+
const approvals: TokenApproval[] = [];
|
|
80
|
+
|
|
81
|
+
for (const exposurePerAsset of exposures) {
|
|
82
|
+
const token = convertAssetToNetworkContractToken(exposurePerAsset.asset);
|
|
83
|
+
if (!token) {
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
for (const [spenderAddress, exposurePerSpender] of Object.entries(exposurePerAsset.spenders)) {
|
|
88
|
+
if (exposurePerSpender.exposure.length === 0) {
|
|
89
|
+
approvals.push({
|
|
90
|
+
token,
|
|
91
|
+
spenderAddress,
|
|
92
|
+
logoUri: token.logoUri,
|
|
93
|
+
});
|
|
94
|
+
} else {
|
|
95
|
+
for (const exposure of exposurePerSpender.exposure) {
|
|
96
|
+
if ('raw_value' in exposure) {
|
|
97
|
+
approvals.push({
|
|
98
|
+
token,
|
|
99
|
+
spenderAddress,
|
|
100
|
+
value: exposure.raw_value,
|
|
101
|
+
usdPrice: exposure.usd_price,
|
|
102
|
+
logoUri: token.logoUri,
|
|
103
|
+
});
|
|
104
|
+
} else {
|
|
105
|
+
approvals.push({
|
|
106
|
+
token,
|
|
107
|
+
spenderAddress,
|
|
108
|
+
logoUri: exposure.logo_url,
|
|
109
|
+
usdPrice: exposure.usd_price,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
if (approvals.length === 0) {
|
|
118
|
+
return undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const isEditable =
|
|
122
|
+
approvals.length === 1 &&
|
|
123
|
+
approvals[0]?.token.type === TokenType.ERC20 &&
|
|
124
|
+
request.method === RpcMethod.ETH_SEND_TRANSACTION;
|
|
125
|
+
|
|
126
|
+
return { isEditable, approvals };
|
|
127
|
+
};
|
|
128
|
+
|
|
129
|
+
export const processBalanceChange = (assetDiffs: Blockaid.AssetDiff[]): BalanceChange | undefined => {
|
|
130
|
+
const ins = processAssetDiffs(assetDiffs, 'in');
|
|
131
|
+
const outs = processAssetDiffs(assetDiffs, 'out');
|
|
132
|
+
|
|
133
|
+
if (ins.length === 0 && outs.length === 0) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
return { ins, outs };
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const processAssetDiffs = (assetDiffs: Blockaid.AssetDiff[], type: 'in' | 'out'): TokenDiff[] => {
|
|
141
|
+
return (
|
|
142
|
+
assetDiffs
|
|
143
|
+
.filter((assetDiff) => assetDiff[type].length > 0)
|
|
144
|
+
// sort asset diffs by length of in/out array
|
|
145
|
+
// this is done to ensure that the token with multiple in/out values are displayed last,
|
|
146
|
+
// to put them in groups with appropriate UI(i.e. accordion), after single in/out tokens
|
|
147
|
+
.sort((a, b) => a[type].length - b[type].length)
|
|
148
|
+
.map((assetDiff) => {
|
|
149
|
+
const asset = assetDiff.asset;
|
|
150
|
+
// convert blockaid asset to network token
|
|
151
|
+
const token: NetworkToken | NetworkContractToken | undefined =
|
|
152
|
+
'address' in asset ? convertAssetToNetworkContractToken(asset) : convertNativeAssetToToken(asset);
|
|
153
|
+
if (!token) {
|
|
154
|
+
return undefined;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
const items = assetDiff[type]
|
|
158
|
+
.map((diff) => {
|
|
159
|
+
let displayValue;
|
|
160
|
+
if ('value' in diff && diff.value) {
|
|
161
|
+
if ('decimals' in token) {
|
|
162
|
+
const valueBN = numberToBN(diff.value, token.decimals);
|
|
163
|
+
displayValue = balanceToDisplayValue(valueBN, token.decimals);
|
|
164
|
+
} else if (isHexString(diff.value)) {
|
|
165
|
+
// for some token (like ERC1155) blockaid returns value in hex format
|
|
166
|
+
displayValue = parseInt(diff.value, 16).toString();
|
|
167
|
+
}
|
|
168
|
+
} else if ('type' in token && token.type === TokenType.ERC721) {
|
|
169
|
+
// for ERC721 type token, we just display 1 to indicate that a single NFT will be transferred
|
|
170
|
+
displayValue = '1';
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
return displayValue ? { displayValue, usdPrice: diff.usd_price } : undefined;
|
|
174
|
+
})
|
|
175
|
+
.filter((x): x is TokenDiffItem => x !== undefined);
|
|
176
|
+
|
|
177
|
+
return { token, items };
|
|
178
|
+
})
|
|
179
|
+
.filter((x): x is TokenDiff => x !== undefined)
|
|
180
|
+
);
|
|
181
|
+
};
|
|
182
|
+
|
|
183
|
+
const convertAssetToNetworkContractToken = (
|
|
184
|
+
asset:
|
|
185
|
+
| Blockaid.Erc20TokenDetails
|
|
186
|
+
| Blockaid.Erc1155TokenDetails
|
|
187
|
+
| Blockaid.Erc721TokenDetails
|
|
188
|
+
| Blockaid.NonercTokenDetails,
|
|
189
|
+
): NetworkContractToken | undefined => {
|
|
190
|
+
let token: NetworkContractToken | undefined;
|
|
191
|
+
if (asset.type === 'ERC20') {
|
|
192
|
+
token = {
|
|
193
|
+
type: TokenType.ERC20,
|
|
194
|
+
address: asset.address,
|
|
195
|
+
decimals: asset.decimals,
|
|
196
|
+
name: asset.name ?? asset.symbol ?? '',
|
|
197
|
+
symbol: asset.symbol ?? '',
|
|
198
|
+
logoUri: asset.logo_url,
|
|
199
|
+
};
|
|
200
|
+
} else if (asset.type === 'ERC1155') {
|
|
201
|
+
token = {
|
|
202
|
+
type: TokenType.ERC1155,
|
|
203
|
+
address: asset.address,
|
|
204
|
+
logoUri: asset.logo_url,
|
|
205
|
+
name: asset.name,
|
|
206
|
+
symbol: asset.symbol,
|
|
207
|
+
};
|
|
208
|
+
} else if (asset.type === 'ERC721') {
|
|
209
|
+
token = {
|
|
210
|
+
type: TokenType.ERC721,
|
|
211
|
+
address: asset.address,
|
|
212
|
+
logoUri: asset.logo_url,
|
|
213
|
+
name: asset.name,
|
|
214
|
+
symbol: asset.symbol,
|
|
215
|
+
};
|
|
216
|
+
} else if (asset.type === 'NONERC') {
|
|
217
|
+
token = {
|
|
218
|
+
type: TokenType.NONERC,
|
|
219
|
+
address: asset.address,
|
|
220
|
+
logoUri: asset.logo_url,
|
|
221
|
+
name: asset.name,
|
|
222
|
+
symbol: asset.symbol,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
return token;
|
|
227
|
+
};
|
|
228
|
+
|
|
229
|
+
const convertNativeAssetToToken = (asset: Blockaid.NativeAssetDetails): NetworkToken => {
|
|
230
|
+
return {
|
|
231
|
+
name: asset.name ?? '',
|
|
232
|
+
symbol: asset.symbol ?? '',
|
|
233
|
+
decimals: asset.decimals,
|
|
234
|
+
description: '',
|
|
235
|
+
logoUri: asset.logo_url,
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
export const processJsonRpcSimulation = async ({
|
|
240
|
+
request,
|
|
241
|
+
dAppUrl,
|
|
242
|
+
accountAddress,
|
|
243
|
+
chainId,
|
|
244
|
+
data,
|
|
245
|
+
proxyApiUrl,
|
|
246
|
+
}: {
|
|
247
|
+
request: RpcRequest;
|
|
248
|
+
dAppUrl?: string;
|
|
249
|
+
accountAddress: string;
|
|
250
|
+
data: { method: string; params: unknown };
|
|
251
|
+
chainId: number;
|
|
252
|
+
proxyApiUrl: string;
|
|
253
|
+
}) => {
|
|
254
|
+
const { validation, simulation } = await scanJsonRpc({
|
|
255
|
+
proxyApiUrl,
|
|
256
|
+
chainId,
|
|
257
|
+
accountAddress,
|
|
258
|
+
data: data as Blockaid.Evm.JsonRpcScanParams.Data,
|
|
259
|
+
domain: dAppUrl,
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
let alert: Alert | undefined;
|
|
263
|
+
if (!validation || validation.result_type === 'Error' || validation.result_type === 'Warning') {
|
|
264
|
+
alert = {
|
|
265
|
+
type: AlertType.WARNING,
|
|
266
|
+
details: {
|
|
267
|
+
title: 'Suspicious Transaction',
|
|
268
|
+
description: 'Use caution, this transaction may be malicious.',
|
|
269
|
+
},
|
|
270
|
+
};
|
|
271
|
+
} else if (validation.result_type === 'Malicious') {
|
|
272
|
+
alert = {
|
|
273
|
+
type: AlertType.DANGER,
|
|
274
|
+
details: {
|
|
275
|
+
title: 'Scam Transaction',
|
|
276
|
+
description: 'This transaction is malicious, do not proceed.',
|
|
277
|
+
actionTitles: {
|
|
278
|
+
reject: 'Reject Transaction',
|
|
279
|
+
proceed: 'Proceed Anyway',
|
|
280
|
+
},
|
|
281
|
+
},
|
|
282
|
+
};
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
let balanceChange: BalanceChange | undefined;
|
|
286
|
+
let tokenApprovals: TokenApprovals | undefined;
|
|
287
|
+
|
|
288
|
+
if (simulation?.status === 'Success') {
|
|
289
|
+
tokenApprovals = processTokenApprovals(request, simulation.account_summary.exposures);
|
|
290
|
+
balanceChange = processBalanceChange(simulation.account_summary.assets_diffs);
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
return { alert, balanceChange, tokenApprovals };
|
|
294
|
+
};
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
import Blockaid from '@blockaid/client';
|
|
2
|
+
import type { TransactionParams } from '../types';
|
|
3
|
+
|
|
4
|
+
const DUMMY_API_KEY = 'DUMMY_API_KEY'; // since we're using our own proxy and api key is handled there, we can use a dummy key here
|
|
5
|
+
|
|
6
|
+
export const scanTransaction = async ({
|
|
7
|
+
proxyApiUrl,
|
|
8
|
+
chainId,
|
|
9
|
+
params,
|
|
10
|
+
domain,
|
|
11
|
+
}: {
|
|
12
|
+
proxyApiUrl: string;
|
|
13
|
+
chainId: number;
|
|
14
|
+
params: TransactionParams;
|
|
15
|
+
domain?: string;
|
|
16
|
+
}): Promise<Blockaid.TransactionScanResponse> => {
|
|
17
|
+
const blockaid = new Blockaid({
|
|
18
|
+
baseURL: proxyApiUrl + '/proxy/blockaid/',
|
|
19
|
+
apiKey: DUMMY_API_KEY,
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
return blockaid.evm.transaction.scan({
|
|
23
|
+
account_address: params.from,
|
|
24
|
+
chain: chainId.toString(),
|
|
25
|
+
options: ['validation', 'simulation'],
|
|
26
|
+
data: {
|
|
27
|
+
from: params.from,
|
|
28
|
+
to: params.to,
|
|
29
|
+
data: params.data,
|
|
30
|
+
value: params.value,
|
|
31
|
+
gas: params.gas,
|
|
32
|
+
gas_price: params.gasPrice,
|
|
33
|
+
},
|
|
34
|
+
metadata: (domain && domain.length > 0 ? { domain } : { non_dapp: true }) as Blockaid.Evm.Metadata,
|
|
35
|
+
});
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
export const scanJsonRpc = async ({
|
|
39
|
+
proxyApiUrl,
|
|
40
|
+
chainId,
|
|
41
|
+
accountAddress,
|
|
42
|
+
data,
|
|
43
|
+
domain,
|
|
44
|
+
}: {
|
|
45
|
+
proxyApiUrl: string;
|
|
46
|
+
chainId: number;
|
|
47
|
+
accountAddress: string;
|
|
48
|
+
data: Blockaid.Evm.JsonRpcScanParams.Data;
|
|
49
|
+
domain?: string;
|
|
50
|
+
}): Promise<Blockaid.TransactionScanResponse> => {
|
|
51
|
+
const blockaid = new Blockaid({
|
|
52
|
+
baseURL: proxyApiUrl + '/proxy/blockaid/',
|
|
53
|
+
apiKey: DUMMY_API_KEY,
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
return blockaid.evm.jsonRpc.scan({
|
|
57
|
+
chain: chainId.toString(),
|
|
58
|
+
options: ['validation', 'simulation'],
|
|
59
|
+
account_address: accountAddress,
|
|
60
|
+
data,
|
|
61
|
+
metadata: (domain && domain.length > 0 ? { domain } : { non_dapp: true }) as Blockaid.Evm.Metadata,
|
|
62
|
+
});
|
|
63
|
+
};
|
|
@@ -1,90 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { ethSignSchema } from './eth-sign';
|
|
3
|
-
import {
|
|
4
|
-
combinedTypedDataSchema,
|
|
5
|
-
ethSignTypedDataSchema,
|
|
6
|
-
ethSignTypedDataV1Schema,
|
|
7
|
-
ethSignTypedDataV3Schema,
|
|
8
|
-
ethSignTypedDataV4Schema,
|
|
9
|
-
typedDataSchema,
|
|
10
|
-
} from './eth-sign-typed-data';
|
|
11
|
-
import { personalSignSchema } from './personal-sign';
|
|
12
|
-
import { RpcMethod } from '@avalabs/vm-module-types';
|
|
13
|
-
|
|
14
|
-
const paramsSchema = z
|
|
15
|
-
.discriminatedUnion('method', [
|
|
16
|
-
personalSignSchema,
|
|
17
|
-
ethSignSchema,
|
|
18
|
-
ethSignTypedDataSchema,
|
|
19
|
-
ethSignTypedDataV1Schema,
|
|
20
|
-
ethSignTypedDataV3Schema,
|
|
21
|
-
ethSignTypedDataV4Schema,
|
|
22
|
-
])
|
|
23
|
-
.transform((value, ctx) => {
|
|
24
|
-
const { method, params } = value;
|
|
25
|
-
|
|
26
|
-
switch (method) {
|
|
27
|
-
case RpcMethod.PERSONAL_SIGN:
|
|
28
|
-
return {
|
|
29
|
-
data: params[0],
|
|
30
|
-
address: params[1],
|
|
31
|
-
};
|
|
32
|
-
case RpcMethod.ETH_SIGN:
|
|
33
|
-
return {
|
|
34
|
-
data: params[1],
|
|
35
|
-
address: params[0],
|
|
36
|
-
};
|
|
37
|
-
case RpcMethod.SIGN_TYPED_DATA:
|
|
38
|
-
case RpcMethod.SIGN_TYPED_DATA_V1: {
|
|
39
|
-
const address = params[0];
|
|
40
|
-
const data = params[1];
|
|
41
|
-
|
|
42
|
-
if (typeof data !== 'string') return { data, address };
|
|
43
|
-
|
|
44
|
-
try {
|
|
45
|
-
const parsed = JSON.parse(data);
|
|
46
|
-
const result = combinedTypedDataSchema.parse(parsed);
|
|
47
|
-
|
|
48
|
-
return {
|
|
49
|
-
data: result,
|
|
50
|
-
address,
|
|
51
|
-
};
|
|
52
|
-
} catch (e) {
|
|
53
|
-
ctx.addIssue({
|
|
54
|
-
code: z.ZodIssueCode.custom,
|
|
55
|
-
message: 'param is not a valid json',
|
|
56
|
-
});
|
|
57
|
-
|
|
58
|
-
return z.NEVER;
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
case RpcMethod.SIGN_TYPED_DATA_V3:
|
|
62
|
-
case RpcMethod.SIGN_TYPED_DATA_V4: {
|
|
63
|
-
const address = params[0];
|
|
64
|
-
const data = params[1];
|
|
65
|
-
|
|
66
|
-
if (typeof data !== 'string') return { data, address };
|
|
67
|
-
|
|
68
|
-
try {
|
|
69
|
-
const parsed = JSON.parse(data);
|
|
70
|
-
const result = typedDataSchema.parse(parsed);
|
|
71
|
-
|
|
72
|
-
return {
|
|
73
|
-
data: result,
|
|
74
|
-
address,
|
|
75
|
-
};
|
|
76
|
-
} catch (e) {
|
|
77
|
-
ctx.addIssue({
|
|
78
|
-
code: z.ZodIssueCode.custom,
|
|
79
|
-
message: 'param is not a valid json',
|
|
80
|
-
});
|
|
81
|
-
|
|
82
|
-
return z.NEVER;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
export function parseRequestParams(params: { method: RpcMethod; params: unknown }) {
|
|
89
|
-
return paramsSchema.safeParse(params);
|
|
90
|
-
}
|