-
Notifications
You must be signed in to change notification settings - Fork 404
/
verification.ts
720 lines (658 loc) · 23.8 KB
/
verification.ts
1
2
3
4
5
6
7
8
9
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
import { CheckedContract } from './CheckedContract';
import {
/* ContextVariables, */
Create2Args,
ImmutableReferences,
ImmutablesTransformation,
Match,
Metadata,
AuxdataTransformation,
RecompilationResult,
StringMap,
Transformation,
LibraryTransformation,
ConstructorTransformation,
CallProtectionTransformation,
TransformationValues,
} from './types';
import {
decode as bytecodeDecode,
splitAuxdata,
} from '@ethereum-sourcify/bytecode-utils';
import { getAddress, getCreateAddress, keccak256 } from 'ethers';
/*
import { EVM } from '@ethereumjs/evm';
import { EEI } from '@ethereumjs/vm';
import { Address } from '@ethereumjs/util';
import { Common } from '@ethereumjs/common';
import { DefaultStateManager } from '@ethereumjs/statemanager';
import { Blockchain } from '@ethereumjs/blockchain';
*/
import { hexZeroPad, isHexString } from '@ethersproject/bytes';
import { BigNumber } from '@ethersproject/bignumber';
import { defaultAbiCoder as abiCoder, ParamType } from '@ethersproject/abi';
import { AbiConstructor } from 'abitype';
import { logDebug, logInfo, logWarn } from './logger';
import SourcifyChain from './SourcifyChain';
import { lt } from 'semver';
export async function verifyDeployed(
checkedContract: CheckedContract,
sourcifyChain: SourcifyChain,
address: string,
/* _contextVariables?: ContextVariables, */
creatorTxHash?: string,
forceEmscripten = false
): Promise<Match> {
let match: Match = {
address,
chainId: sourcifyChain.chainId.toString(),
runtimeMatch: null,
creationMatch: null,
runtimeTransformations: [],
creationTransformations: [],
runtimeTransformationValues: {},
creationTransformationValues: {},
};
logInfo(
`Verifying contract ${
checkedContract.name
} at address ${address} on chain ${sourcifyChain.chainId.toString()}`
);
let useEmscripten = forceEmscripten;
// See https://github.com/ethereum/sourcify/issues/1159
// The nightlies and pre-0.4.10 platform binaries are not available
if (
lt(checkedContract.metadata.compiler.version, '0.4.10') ||
checkedContract.metadata.compiler.version.includes('nightly')
) {
useEmscripten = true;
}
const recompiled = await checkedContract.recompile(useEmscripten);
if (
recompiled.runtimeBytecode === '0x' ||
recompiled.creationBytecode === '0x'
) {
throw new Error(
`The compiled contract bytecode is "0x". Are you trying to verify an abstract contract?`
);
}
const runtimeBytecode = await sourcifyChain.getBytecode(address);
// Can't match if there is no deployed bytecode
if (!runtimeBytecode) {
match.message = `Chain #${sourcifyChain.chainId} is temporarily unavailable.`;
return match;
} else if (runtimeBytecode === '0x') {
match.message = `Chain #${sourcifyChain.chainId} does not have a contract deployed at ${address}.`;
return match;
}
// Try to match with deployed bytecode directly
try {
logDebug(
`Matching with deployed bytecode chain=${sourcifyChain.chainId} address=${address}`
);
matchWithRuntimeBytecode(
match,
recompiled.runtimeBytecode,
runtimeBytecode,
recompiled.immutableReferences
);
if (match.runtimeMatch === 'partial') {
logDebug(
`Matched partial with deployed bytecode, tryToFindPerfectMetadataAndMatch chain=${sourcifyChain.chainId} address=${address}`
);
match = await tryToFindPerfectMetadataAndMatch(
checkedContract,
runtimeBytecode,
match,
async (match, recompiled) => {
matchWithRuntimeBytecode(
match,
recompiled.runtimeBytecode,
runtimeBytecode
);
},
'runtimeMatch'
);
}
} catch (e: any) {
logWarn(
`Error while matching with runtime bytecode for contract ${address} on chain ${sourcifyChain.chainId}: ` +
e.message
);
}
try {
// Try to match with creationTx, if available
if (creatorTxHash) {
logDebug(
`Matching with creation tx creatorTxHash=${creatorTxHash} chain=${sourcifyChain.chainId} address=${address}`
);
const recompiledMetadata: Metadata = JSON.parse(recompiled.metadata);
await matchWithCreationTx(
match,
recompiled.creationBytecode,
sourcifyChain,
address,
creatorTxHash,
recompiledMetadata
);
if (match.runtimeMatch === 'partial') {
logDebug(
`Matched partial with creation tx, tryToFindPerfectMetadataAndMatch creatorTxHash=${creatorTxHash} chain=${sourcifyChain.chainId} address=${address}`
);
match = await tryToFindPerfectMetadataAndMatch(
checkedContract,
runtimeBytecode,
match,
async (match, recompiled) => {
await matchWithCreationTx(
match,
recompiled.creationBytecode,
sourcifyChain,
address,
creatorTxHash,
recompiledMetadata
);
},
'creationMatch'
);
}
}
} catch (e: any) {
logWarn(
`Error while matching with creation tx for contract ${address} on chain ${sourcifyChain.chainId}: ` +
e.message
);
}
// Case when extra unused files in compiler input cause different bytecode (https://github.com/ethereum/sourcify/issues/618)
try {
if (
splitAuxdata(match.onchainRuntimeBytecode || '')[1] ===
splitAuxdata(checkedContract.runtimeBytecode || '')[1] &&
match.runtimeMatch === null &&
match.creationMatch === null &&
checkedContract.metadata.settings.optimizer?.enabled
) {
const [, deployedAuxdata] = splitAuxdata(runtimeBytecode);
const [, recompiledAuxdata] = splitAuxdata(recompiled.runtimeBytecode);
// Metadata hashes match but bytecodes don't match.
if (deployedAuxdata === recompiledAuxdata) {
(match as Match).runtimeMatch = 'extra-file-input-bug';
(match as Match).message =
"It seems your contract's metadata hashes match but not the bytecodes. You should add all the files input to the compiler during compilation and remove all others. See the issue for more information: https://github.com/ethereum/sourcify/issues/618";
return match;
}
}
} catch (e: any) {
logWarn(
`Error while checking for extra-file-input-bug for contract ${address} on chain ${sourcifyChain.chainId}: ` +
e.message
);
}
try {
// Handle when <0.8.21 and with viaIR and with optimizer disabled https://github.com/ethereum/sourcify/issues/1088
if (
match.runtimeMatch === null &&
match.creationMatch === null &&
lt(checkedContract.metadata.compiler.version, '0.8.21') &&
!checkedContract.metadata.settings.optimizer?.enabled &&
checkedContract.metadata.settings?.viaIR
) {
logInfo(
`Forcing compiling with the Emscripten compiler to match the deployed bytecode for ${
checkedContract.name
} to verify at ${address} on chain ${sourcifyChain.chainId.toString()}: `
);
return verifyDeployed(
checkedContract,
sourcifyChain,
address,
creatorTxHash,
true // Force compiling with Emscripten compiler
);
}
} catch (e: any) {
logWarn(
`Error while handling "<0.8.21 and viaIR" bug for contract ${address} on chain ${sourcifyChain.chainId}: ` +
e.message
);
}
if (match.creationMatch !== null || match.runtimeMatch !== null) {
logInfo(
`Verified contract successfully name=${checkedContract.name} address=${address} chainId=${match.chainId} runtimeMatch=${match.runtimeMatch} creationMatch=${match.creationMatch}`
);
return match;
}
logInfo(
`Failed to verify contract address=${address} chainId=${sourcifyChain.chainId} message=${match.message}`
);
throw Error("The deployed and recompiled bytecode don't match.");
}
async function tryToFindPerfectMetadataAndMatch(
checkedContract: CheckedContract,
runtimeBytecode: string,
match: Match,
matchFunction: (
match: Match,
recompilationResult: RecompilationResult
) => Promise<void>,
matchType: 'runtimeMatch' | 'creationMatch'
): Promise<Match> {
const checkedContractWithPerfectMetadata =
await checkedContract.tryToFindPerfectMetadata(runtimeBytecode);
if (checkedContractWithPerfectMetadata) {
// If found try to match again with the passed matchFunction
const matchWithPerfectMetadata = { ...match };
const recompiled = await checkedContractWithPerfectMetadata.recompile();
await matchFunction(matchWithPerfectMetadata, recompiled);
if (matchWithPerfectMetadata[matchType] === 'perfect') {
// Replace the metadata and solidity files that will be saved in the repo
checkedContract.initSolcJsonInput(
checkedContractWithPerfectMetadata.metadata,
checkedContractWithPerfectMetadata.solidity
);
return matchWithPerfectMetadata;
}
}
return match;
}
export async function verifyCreate2(
checkedContract: CheckedContract,
deployerAddress: string,
salt: string,
create2Address: string,
abiEncodedConstructorArguments?: string
): Promise<Match> {
const recompiled = await checkedContract.recompile();
const computedAddr = calculateCreate2Address(
deployerAddress,
salt,
recompiled.creationBytecode,
abiEncodedConstructorArguments
);
if (create2Address.toLowerCase() !== computedAddr.toLowerCase()) {
throw new Error(
`The provided create2 address doesn't match server's generated one. Expected: ${computedAddr} ; Received: ${create2Address} ;`
);
}
// TODO: Can create2 have library addresses?
const create2Args: Create2Args = {
deployerAddress,
salt,
};
const match: Match = {
address: computedAddr,
chainId: '0',
runtimeMatch: 'perfect',
creationMatch: null,
abiEncodedConstructorArguments,
create2Args,
runtimeTransformations: [],
creationTransformations: [],
runtimeTransformationValues: {},
creationTransformationValues: {},
// libraryMap: libraryMap,
};
return match;
}
export function matchWithRuntimeBytecode(
match: Match,
recompiledRuntimeBytecode: string,
onchainRuntimeBytecode: string,
immutableReferences?: ImmutableReferences
) {
// Updating the `match.onchainRuntimeBytecode` here so we are sure to always update it
match.onchainRuntimeBytecode = onchainRuntimeBytecode;
if (match.runtimeTransformations === undefined) {
match.runtimeTransformations = [];
}
if (match.runtimeTransformationValues === undefined) {
match.runtimeTransformationValues = {};
}
// Check if is a library with call protection
// See https://docs.soliditylang.org/en/v0.8.19/contracts.html#call-protection-for-libraries
// Replace the call protection with the real address
recompiledRuntimeBytecode = checkCallProtectionAndReplaceAddress(
recompiledRuntimeBytecode,
onchainRuntimeBytecode,
match.runtimeTransformations,
match.runtimeTransformationValues
);
// Replace the library placeholders in the recompiled bytecode with values from the deployed bytecode
const { replaced, libraryMap } = addLibraryAddresses(
recompiledRuntimeBytecode,
onchainRuntimeBytecode,
match.runtimeTransformations
);
recompiledRuntimeBytecode = replaced;
match.runtimeTransformationValues.libraries = libraryMap;
if (immutableReferences) {
onchainRuntimeBytecode = replaceImmutableReferences(
immutableReferences,
onchainRuntimeBytecode,
match.runtimeTransformations,
match.runtimeTransformationValues
);
}
if (recompiledRuntimeBytecode === onchainRuntimeBytecode) {
match.libraryMap = libraryMap;
match.immutableReferences = immutableReferences;
// if the bytecode doesn't contain metadata then "partial" match
if (endsWithMetadataHash(onchainRuntimeBytecode)) {
match.runtimeMatch = 'perfect';
} else {
match.runtimeMatch = 'partial';
}
} else {
// Try to match without the metadata hashes
const [trimmedOnchainRuntimeBytecode, auxdata] = splitAuxdata(
onchainRuntimeBytecode
);
const [trimmedRecompiledRuntimeBytecode] = splitAuxdata(
recompiledRuntimeBytecode
);
if (trimmedOnchainRuntimeBytecode === trimmedRecompiledRuntimeBytecode) {
match.libraryMap = libraryMap;
match.immutableReferences = immutableReferences;
match.runtimeMatch = 'partial';
match.runtimeTransformations?.push(
AuxdataTransformation(trimmedRecompiledRuntimeBytecode.length, '0')
);
match.runtimeTransformationValues.cborAuxdata = { '0': auxdata };
}
}
}
/**
* Matches the contract via the transaction that created the contract, if that tx is known.
* Checks if the tx.input matches the recompiled creation bytecode. Double checks that the contract address matches the address being verified.
*
*/
export async function matchWithCreationTx(
match: Match,
recompiledCreationBytecode: string,
sourcifyChain: SourcifyChain,
address: string,
creatorTxHash: string,
recompiledMetadata: Metadata
) {
if (recompiledCreationBytecode === '0x') {
match.creationMatch = null;
match.message = `Failed to match with creation bytecode: recompiled contract's creation bytecode is empty`;
return;
}
const creatorTx = await sourcifyChain.getTx(creatorTxHash);
let onchainCreationBytecode = '';
try {
onchainCreationBytecode =
(await sourcifyChain.getContractCreationBytecode(
address,
creatorTxHash,
creatorTx
)) || '';
} catch (e: any) {
logWarn(
`Failed to get contract creation bytecode for ${address} on chain ${sourcifyChain.chainId.toString()}. \n ${
e.message
}`
);
match.creationMatch = null;
match.message = `Failed to match with creation bytecode: couldn't get the creation bytecode.`;
return;
}
match.creatorTxHash = creatorTxHash;
match.onchainCreationBytecode = onchainCreationBytecode;
// Initialize the transformations array if undefined
if (match.creationTransformations === undefined) {
match.creationTransformations = [];
}
if (match.creationTransformationValues === undefined) {
match.creationTransformationValues = {};
}
// The reason why this uses `startsWith` instead of `===` is that creationTxData may contain constructor arguments at the end part.
// Replace the library placeholders in the recompiled bytecode with values from the deployed bytecode
const { replaced, libraryMap } = addLibraryAddresses(
recompiledCreationBytecode,
onchainCreationBytecode,
match.creationTransformations
);
recompiledCreationBytecode = replaced;
match.creationTransformationValues.libraries = libraryMap;
if (onchainCreationBytecode.startsWith(recompiledCreationBytecode)) {
// if the bytecode doesn't end with metadata then "partial" match
if (endsWithMetadataHash(recompiledCreationBytecode)) {
match.creationMatch = 'perfect';
logDebug(
`'perfect' creationMatch chain=${match.chainId} address=${address}`
);
} else {
match.creationMatch = 'partial';
logDebug(
`'partial' creationMatch chain=${match.chainId} address=${address}`
);
}
} else {
// Match without metadata hashes
// TODO: Handle multiple metadata hashes
logDebug(`Trying to match without metadata hashes`);
// Assuming the onchain and recompiled auxdata lengths are the same
const onchainCreationBytecodeWithoutConstructorArgs =
onchainCreationBytecode.slice(0, recompiledCreationBytecode.length);
logDebug('Split onchain creation bytecode');
const [trimmedOnchainCreationBytecode, auxdata] = splitAuxdata(
onchainCreationBytecodeWithoutConstructorArgs
); // In the case of creationTxData (not runtime bytecode) it is actually not CBOR encoded at the end because of the appended constr. args., but splitAuxdata returns the whole bytecode if it's not CBOR encoded, so will work with startsWith.
logDebug('Split recompiled creation bytecode');
const [trimmedRecompiledCreationBytecode] = splitAuxdata(
recompiledCreationBytecode
);
logDebug(`Split auxdata`);
if (
trimmedOnchainCreationBytecode.startsWith(
trimmedRecompiledCreationBytecode
)
) {
logDebug(
`'partial' creationMatch (trimmed metadata) chain=${match.chainId} address=${address}`
);
match.creationMatch = 'partial';
match.creationTransformations?.push(
AuxdataTransformation(trimmedRecompiledCreationBytecode.length, '0')
);
match.creationTransformationValues.cborAuxdata = { '0': auxdata };
}
// TODO: If we still don't have a match and we have multiple auxdata in legacyAssembly, try finding the metadata hashes and match with this info.
}
if (match.creationMatch) {
const abiEncodedConstructorArguments =
extractAbiEncodedConstructorArguments(
onchainCreationBytecode,
recompiledCreationBytecode
);
const constructorAbiParamInputs = (
recompiledMetadata?.output?.abi?.find(
(param) => param.type === 'constructor'
) as AbiConstructor
)?.inputs as ParamType[];
if (abiEncodedConstructorArguments) {
if (!constructorAbiParamInputs) {
match.creationMatch = null;
match.message = `Failed to match with creation bytecode: constructor ABI Inputs are missing`;
return;
}
// abiCoder doesn't break if called with a wrong `abiEncodedConstructorArguments`
// so in order to successfuly check if the constructor arguments actually match
// we need to re-encode it and compare them
const decodeResult = abiCoder.decode(
constructorAbiParamInputs,
abiEncodedConstructorArguments
);
const encodeResult = abiCoder.encode(
constructorAbiParamInputs,
decodeResult
);
if (encodeResult !== abiEncodedConstructorArguments) {
match.creationMatch = null;
match.message = `Failed to match with creation bytecode: constructor arguments ABI decoding failed ${encodeResult} vs ${abiEncodedConstructorArguments}`;
return;
}
match.creationTransformations?.push(
ConstructorTransformation(recompiledCreationBytecode.length)
);
match.creationTransformationValues.constructorArguments =
abiEncodedConstructorArguments;
}
// we need to check if this contract creation tx actually yields the same contract address https://github.com/ethereum/sourcify/issues/887
const createdContractAddress = getCreateAddress({
from: creatorTx.from,
nonce: creatorTx.nonce,
});
if (createdContractAddress.toLowerCase() !== address.toLowerCase()) {
match.creationMatch = null;
match.message = `The address being verified ${address} doesn't match the expected ddress of the contract ${createdContractAddress} that will be created by the transaction ${creatorTxHash}.`;
return;
}
match.libraryMap = libraryMap;
match.abiEncodedConstructorArguments = abiEncodedConstructorArguments;
}
}
export function addLibraryAddresses(
template: string,
real: string,
transformationsArray: Transformation[]
): {
replaced: string;
libraryMap: StringMap;
} {
const PLACEHOLDER_START = '__';
const PLACEHOLDER_LENGTH = 40;
const libraryMap: StringMap = {};
let index = template.indexOf(PLACEHOLDER_START);
while (index !== -1) {
const placeholder = template.slice(index, index + PLACEHOLDER_LENGTH);
const address = real.slice(index, index + PLACEHOLDER_LENGTH);
libraryMap[placeholder] = address;
// Replace regex with simple string replacement
template = template.split(placeholder).join(address);
index = template.indexOf(PLACEHOLDER_START);
transformationsArray.push(LibraryTransformation(index, template));
}
return {
replaced: template,
libraryMap,
};
}
// returns the full bytecode with the call protection replaced with the real address
export function checkCallProtectionAndReplaceAddress(
template: string,
real: string,
transformationsArray: Transformation[],
transformationValues: TransformationValues
): string {
const push20CodeOp = '73';
const callProtection = `0x${push20CodeOp}${'00'.repeat(20)}`;
if (template.startsWith(callProtection)) {
const replacedCallProtection = real.slice(0, 0 + callProtection.length);
transformationsArray.push(CallProtectionTransformation());
transformationValues.callProtection = replacedCallProtection;
return replacedCallProtection + template.substring(callProtection.length);
}
return template;
}
/**
* Replaces the values of the immutable variables in the (onchain) deployed bytecode with zeros, so that the bytecode can be compared with the (offchain) recompiled bytecode.
* Easier this way because we can simply replace with zeros
* Example immutableReferences: {"97":[{"length":32,"start":137}],"99":[{"length":32,"start":421}]} where 97 and 99 are the AST ids
*/
export function replaceImmutableReferences(
immutableReferences: ImmutableReferences,
onchainRuntimeBytecode: string,
transformationsArray: Transformation[],
transformationValues: TransformationValues
) {
onchainRuntimeBytecode = onchainRuntimeBytecode.slice(2); // remove "0x"
Object.keys(immutableReferences).forEach((astId) => {
immutableReferences[astId].forEach((reference) => {
const { start, length } = reference;
// Save the transformation
transformationsArray.push(ImmutablesTransformation(start * 2, astId));
const immutableValue = onchainRuntimeBytecode.slice(
start * 2,
start * 2 + length * 2
);
// Save the transformation value
if (transformationValues.immutables === undefined) {
transformationValues.immutables = {};
}
transformationValues.immutables[astId] = immutableValue;
// Write zeros in the place
const zeros = '0'.repeat(length * 2);
onchainRuntimeBytecode =
onchainRuntimeBytecode.slice(0, start * 2) +
zeros +
onchainRuntimeBytecode.slice(start * 2 + length * 2);
});
});
return '0x' + onchainRuntimeBytecode;
}
function extractAbiEncodedConstructorArguments(
onchainCreationBytecode: string,
compiledCreationBytecode: string
) {
if (onchainCreationBytecode.length === compiledCreationBytecode.length)
return undefined;
return '0x' + onchainCreationBytecode.slice(compiledCreationBytecode.length);
}
/**
* Calculates the address of the contract created with the EIP-1014 CREATE2 opcode.
*
* @param deployerAddress
* @param salt
* @param creationBytecode
* @param abiEncodedConstructorArguments
* @returns Match
*/
export function calculateCreate2Address(
deployerAddress: string,
salt: string,
creationBytecode: string,
abiEncodedConstructorArguments?: string
) {
let initcode = creationBytecode;
if (abiEncodedConstructorArguments) {
initcode += abiEncodedConstructorArguments.startsWith('0x')
? abiEncodedConstructorArguments.slice(2)
: abiEncodedConstructorArguments;
}
const address = `0x${keccak256(
`0x${['ff', deployerAddress, saltToHex(salt), keccak256(initcode)]
.map((x) => x.replace(/0x/, ''))
.join('')}`
).slice(-40)}`; // last 20 bytes
return getAddress(address); // checksum
}
const saltToHex = (salt: string) => {
if (isHexString(salt)) {
return hexZeroPad(salt, 32);
}
const bn = BigNumber.from(salt);
const hex = bn.toHexString();
const paddedHex = hexZeroPad(hex, 32);
return paddedHex;
};
/**
* Checks if there's a CBOR encoded metadata hash appended to the bytecode.
*
* @param bytecode
* @returns bool - true if there's a metadata hash
*/
function endsWithMetadataHash(bytecode: string) {
let endsWithMetadata: boolean;
try {
const decodedCBOR = bytecodeDecode(bytecode);
endsWithMetadata =
!!decodedCBOR.ipfs || !!decodedCBOR['bzzr0'] || !!decodedCBOR['bzzr1'];
} catch (e) {
logDebug("Can't decode CBOR");
endsWithMetadata = false;
}
return endsWithMetadata;
}