@rocketh/verifier 0.17.15 → 0.17.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/README.md +6 -0
- package/dist/cli.js +5 -9
- package/dist/cli.js.map +1 -1
- package/package.json +8 -7
- package/src/blockscout.ts +157 -0
- package/src/cli.ts +98 -0
- package/src/etherscan.ts +420 -0
- package/src/index.ts +84 -0
- package/src/metadata.ts +31 -0
- package/src/sourcify.ts +109 -0
- package/src/utils/match-all.ts +72 -0
package/README.md
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
1
|
# @rocketh/verifier
|
|
2
|
+
|
|
3
|
+
Contract verification tool for rocketh, enabling you to submit verification proofs to verifier services like blockchain explorers (Etherscan) and Sourcify.
|
|
4
|
+
|
|
5
|
+
For full documentation, visit [rocketh.dev](https://rocketh.dev).
|
|
6
|
+
|
|
7
|
+
For hardhat-deploy documentation, see [rocketh.dev/hardhat-deploy/](https://rocketh.dev/hardhat-deploy/).
|
package/dist/cli.js
CHANGED
|
@@ -24,7 +24,6 @@ program
|
|
|
24
24
|
.option('--fix-mispell', 'if set, will use correct spelling of form field')
|
|
25
25
|
.action(async (options) => {
|
|
26
26
|
const { environment, ...programOptions } = program.opts();
|
|
27
|
-
;
|
|
28
27
|
const resolvedConfig = await readAndResolveConfig({ ...programOptions });
|
|
29
28
|
run(resolvedConfig, environment, {
|
|
30
29
|
verifier: {
|
|
@@ -33,7 +32,7 @@ program
|
|
|
33
32
|
endpoint: options.endpoint,
|
|
34
33
|
forceLicense: options.forceLicense,
|
|
35
34
|
license: options.license,
|
|
36
|
-
fixMispell: options.fixMispell
|
|
35
|
+
fixMispell: options.fixMispell,
|
|
37
36
|
},
|
|
38
37
|
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
39
38
|
});
|
|
@@ -45,14 +44,13 @@ program
|
|
|
45
44
|
.option('--min-interval <value>', 'min interval between request in ms')
|
|
46
45
|
.action(async (options) => {
|
|
47
46
|
const { environment, ...programOptions } = program.opts();
|
|
48
|
-
;
|
|
49
47
|
const resolvedConfig = await readAndResolveConfig({ ...programOptions });
|
|
50
48
|
run(resolvedConfig, environment, {
|
|
51
49
|
verifier: {
|
|
52
50
|
type: 'sourcify',
|
|
53
|
-
endpoint: options.endpoint
|
|
51
|
+
endpoint: options.endpoint,
|
|
54
52
|
},
|
|
55
|
-
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined
|
|
53
|
+
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
56
54
|
});
|
|
57
55
|
});
|
|
58
56
|
program
|
|
@@ -63,14 +61,13 @@ program
|
|
|
63
61
|
// .option('--api <value>', 'api version (default to v2)')
|
|
64
62
|
.action(async (options) => {
|
|
65
63
|
const { environment, ...programOptions } = program.opts();
|
|
66
|
-
;
|
|
67
64
|
const resolvedConfig = await readAndResolveConfig({ ...programOptions });
|
|
68
65
|
run(resolvedConfig, environment, {
|
|
69
66
|
verifier: {
|
|
70
67
|
type: 'blockscout',
|
|
71
|
-
endpoint: options.endpoint
|
|
68
|
+
endpoint: options.endpoint,
|
|
72
69
|
},
|
|
73
|
-
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined
|
|
70
|
+
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
74
71
|
});
|
|
75
72
|
});
|
|
76
73
|
program
|
|
@@ -79,7 +76,6 @@ program
|
|
|
79
76
|
.option('--out <value>', 'folder to write metadata into')
|
|
80
77
|
.action(async (options) => {
|
|
81
78
|
const { environment, ...programOptions } = program.opts();
|
|
82
|
-
;
|
|
83
79
|
const resolvedConfig = await readAndResolveConfig({ ...programOptions });
|
|
84
80
|
await exportMetadata(resolvedConfig, environment, { out: options.out || '_metadata' });
|
|
85
81
|
});
|
package/dist/cli.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cli.js","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA,OAAO,EAAC,OAA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,EAAC,GAAG,EAAE,OAAO,CAAC,GAAG,IAAI,WAAW,EAAC,CAAC,CAAC;AACtF,CAAC,CAAC,CAAC;AAEJ,OAAO,CAAC,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rocketh/verifier",
|
|
3
|
-
"version": "0.17.
|
|
3
|
+
"version": "0.17.17",
|
|
4
4
|
"description": "submit verification proof to verifier services (blockchain explorer, sourcify...",
|
|
5
5
|
"publishConfig": {
|
|
6
6
|
"access": "public"
|
|
@@ -21,7 +21,8 @@
|
|
|
21
21
|
"rocketh-verify": "dist/cli.js"
|
|
22
22
|
},
|
|
23
23
|
"files": [
|
|
24
|
-
"dist"
|
|
24
|
+
"dist",
|
|
25
|
+
"src"
|
|
25
26
|
],
|
|
26
27
|
"dependencies": {
|
|
27
28
|
"@types/fs-extra": "^11.0.4",
|
|
@@ -31,17 +32,17 @@
|
|
|
31
32
|
"fs-extra": "^11.3.3",
|
|
32
33
|
"ldenv": "^0.3.16",
|
|
33
34
|
"neoqs": "^6.13.0",
|
|
34
|
-
"@rocketh/core": "0.17.
|
|
35
|
+
"@rocketh/core": "0.17.9"
|
|
35
36
|
},
|
|
36
37
|
"devDependencies": {
|
|
37
|
-
"@types/node": "^25.0.
|
|
38
|
-
"as-soon": "^0.
|
|
38
|
+
"@types/node": "^25.0.10",
|
|
39
|
+
"as-soon": "^0.1.4",
|
|
39
40
|
"rimraf": "^6.1.2",
|
|
40
41
|
"typescript": "^5.9.3",
|
|
41
|
-
"@rocketh/node": "0.17.
|
|
42
|
+
"@rocketh/node": "0.17.17"
|
|
42
43
|
},
|
|
43
44
|
"peerDependencies": {
|
|
44
|
-
"@rocketh/node": "0.17.
|
|
45
|
+
"@rocketh/node": "0.17.17"
|
|
45
46
|
},
|
|
46
47
|
"scripts": {
|
|
47
48
|
"build": "tsc --project tsconfig.json",
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import type {Abi, Deployment, UnknownDeployments} from '@rocketh/core/types';
|
|
5
|
+
import {BlockscoutOptions} from './index.js';
|
|
6
|
+
|
|
7
|
+
//https://eth.blockscout.com/api/v2/search?q=WETH
|
|
8
|
+
const defaultEndpoints: {[chainId: string]: string} = {
|
|
9
|
+
'1': 'https://eth.blockscout.com/api/v2',
|
|
10
|
+
'11155111': 'https://eth-sepolia.blockscout.com/api/v2',
|
|
11
|
+
'5': 'https://eth-goerli.blockscout.com/api/v2',
|
|
12
|
+
'10': 'https://optimism.blockscout.com/api/v2',
|
|
13
|
+
'11155420': 'https://optimism-sepolia.blockscout.com/api/v2',
|
|
14
|
+
'61': 'https://etc.blockscout.com/api/v2',
|
|
15
|
+
'324': 'https://zksync.blockscout.com/api/v2',
|
|
16
|
+
'8453': 'https://base.blockscout.com/api/v2',
|
|
17
|
+
'84532': 'https://base-sepolia.blockscout.com/api/v2',
|
|
18
|
+
'100': 'https://gnosis.blockscout.com/api/v2',
|
|
19
|
+
'10200': 'https://gnosis-chiado.blockscout.com/api/v2',
|
|
20
|
+
'17001': 'https://17001-explorer-api.quarry.linfra.xyz/api/v2', // probably temporary
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
function sleep(ms: number) {
|
|
24
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function log(...args: any[]) {
|
|
28
|
+
console.log(...args);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function logError(...args: any[]) {
|
|
32
|
+
console.log(chalk.red(...args));
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function logInfo(...args: any[]) {
|
|
36
|
+
console.log(chalk.yellow(...args));
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function logSuccess(...args: any[]) {
|
|
40
|
+
console.log(chalk.green(...args));
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function ensureTrailingSlash(s: string): string {
|
|
44
|
+
if (!s.endsWith('/')) {
|
|
45
|
+
s = s + '/';
|
|
46
|
+
}
|
|
47
|
+
return s;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export async function submitSourcesToBlockscout(
|
|
51
|
+
env: {
|
|
52
|
+
deployments: UnknownDeployments;
|
|
53
|
+
networkName: string;
|
|
54
|
+
chainId: string;
|
|
55
|
+
deploymentNames?: string[];
|
|
56
|
+
minInterval?: number;
|
|
57
|
+
logErrorOnFailure?: boolean;
|
|
58
|
+
},
|
|
59
|
+
config?: BlockscoutOptions,
|
|
60
|
+
): Promise<void> {
|
|
61
|
+
config = config || {type: 'blockscout'};
|
|
62
|
+
const all = env.deployments;
|
|
63
|
+
const url = config.endpoint
|
|
64
|
+
? ensureTrailingSlash(config.endpoint)
|
|
65
|
+
: ensureTrailingSlash(defaultEndpoints[env.chainId]);
|
|
66
|
+
|
|
67
|
+
if (!url) {
|
|
68
|
+
logError(`no endpoint provided and no default known for chainId ${env.chainId}`);
|
|
69
|
+
return;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
async function submit(name: string, deployment: Deployment<Abi>) {
|
|
73
|
+
const {address, metadata} = deployment;
|
|
74
|
+
|
|
75
|
+
try {
|
|
76
|
+
const checkResponse = await fetch(`${url}smart-contracts/${address.toLowerCase()}`);
|
|
77
|
+
const json = await checkResponse.json();
|
|
78
|
+
if (json.is_verified) {
|
|
79
|
+
log(`already verified: ${name} (${address}), skipping.`);
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
} catch (e) {
|
|
83
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const metadataObj = JSON.parse(metadata);
|
|
87
|
+
const compilationTarget = metadataObj.settings?.compilationTarget;
|
|
88
|
+
|
|
89
|
+
let contractFilepath;
|
|
90
|
+
let contractName;
|
|
91
|
+
if (compilationTarget) {
|
|
92
|
+
contractFilepath = Object.keys(compilationTarget)[0];
|
|
93
|
+
contractName = compilationTarget[contractFilepath];
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
if (!contractFilepath || !contractName) {
|
|
97
|
+
return logError(
|
|
98
|
+
`Failed to extract contract fully qualified name from metadata.settings.compilationTarget for ${name}. Skipping.`,
|
|
99
|
+
);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const contractNamePath = `${contractFilepath}:${contractName}`;
|
|
103
|
+
|
|
104
|
+
const formData = new FormData();
|
|
105
|
+
formData.append('address_hash', address);
|
|
106
|
+
formData.append('compiler_version', JSON.parse(metadata).compiler.version);
|
|
107
|
+
formData.append('constructor_args', deployment.argsData);
|
|
108
|
+
formData.append('autodetect_constructor_args', 'false');
|
|
109
|
+
formData.append('contract_name', contractNamePath);
|
|
110
|
+
|
|
111
|
+
const metadataBlob = new Blob([metadata], {
|
|
112
|
+
type: 'application/json',
|
|
113
|
+
});
|
|
114
|
+
formData.append('files[0]', metadataBlob, 'metadata.json');
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
const submissionResponse = await fetch(
|
|
118
|
+
`${url}smart-contracts/${address.toLowerCase()}/verification/via/standard-input`,
|
|
119
|
+
{body: formData, method: 'POST'},
|
|
120
|
+
);
|
|
121
|
+
const json = await submissionResponse.json();
|
|
122
|
+
if (json.message === 'Smart-contract verification started') {
|
|
123
|
+
logSuccess(` => contract ${name} verification has started`);
|
|
124
|
+
} else {
|
|
125
|
+
logError(` => contract ${name} might not have gone throyugh`, json);
|
|
126
|
+
}
|
|
127
|
+
} catch (e) {
|
|
128
|
+
if (env?.logErrorOnFailure) {
|
|
129
|
+
const failingMetadataFolder = path.join('failing_metadata', env.chainId);
|
|
130
|
+
fs.ensureDirSync(failingMetadataFolder);
|
|
131
|
+
fs.writeFileSync(path.join(failingMetadataFolder, `${name}_at_${address}.json`), metadata);
|
|
132
|
+
}
|
|
133
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
for (const name of env.deploymentNames ? env.deploymentNames : Object.keys(all)) {
|
|
138
|
+
const deployment = all[name];
|
|
139
|
+
|
|
140
|
+
if (!deployment.metadata) {
|
|
141
|
+
logError(`Contract ${name} was deployed without saving metadata. Cannot submit to sourcify, skipping.`);
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
logInfo(`verifying ${name} (${deployment.address} on chain ${env.chainId}) ...`);
|
|
146
|
+
|
|
147
|
+
// if (config?.version === 'v1') {
|
|
148
|
+
// await submitV1(name, deployment);
|
|
149
|
+
// } else {
|
|
150
|
+
await submit(name, deployment);
|
|
151
|
+
// }
|
|
152
|
+
|
|
153
|
+
if (env.minInterval) {
|
|
154
|
+
await sleep(env.minInterval);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
package/src/cli.ts
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
#! /usr/bin/env node
|
|
2
|
+
import {loadEnv} from 'ldenv';
|
|
3
|
+
import {run} from './index.js';
|
|
4
|
+
import {Command, Option} from 'commander';
|
|
5
|
+
import pkg from '../package.json' with {type: 'json'};
|
|
6
|
+
import {exportMetadata} from './metadata.js';
|
|
7
|
+
import {readAndResolveConfig} from '@rocketh/node';
|
|
8
|
+
loadEnv();
|
|
9
|
+
|
|
10
|
+
const commandName = `rocketh-verify`;
|
|
11
|
+
|
|
12
|
+
const program = new Command();
|
|
13
|
+
program
|
|
14
|
+
.name(commandName)
|
|
15
|
+
.description('submit contract for verification')
|
|
16
|
+
.version(pkg.version)
|
|
17
|
+
|
|
18
|
+
.option('-d, --deployments <value>', 'folder where deployments are saved')
|
|
19
|
+
.requiredOption('-e, --environment <value>', 'environment context to use');
|
|
20
|
+
|
|
21
|
+
program
|
|
22
|
+
.command('etherscan')
|
|
23
|
+
.description('submit contract for verification to etherscan')
|
|
24
|
+
.option('--endpoint <value>', 'endpoint to connect to')
|
|
25
|
+
.option('--license <value>', 'source code license')
|
|
26
|
+
.option('--force-license', 'force the use of the license provided')
|
|
27
|
+
.option('--min-interval <value>', 'min interval between request in ms')
|
|
28
|
+
.option('--fix-mispell', 'if set, will use correct spelling of form field')
|
|
29
|
+
.action(
|
|
30
|
+
async (options: {
|
|
31
|
+
endpoint?: string;
|
|
32
|
+
forceLicense: boolean;
|
|
33
|
+
license: string;
|
|
34
|
+
minInterval?: string;
|
|
35
|
+
fixMispell?: boolean;
|
|
36
|
+
}) => {
|
|
37
|
+
const {environment, ...programOptions} = program.opts();
|
|
38
|
+
const resolvedConfig = await readAndResolveConfig({...programOptions});
|
|
39
|
+
run(resolvedConfig, environment, {
|
|
40
|
+
verifier: {
|
|
41
|
+
type: 'etherscan',
|
|
42
|
+
apiKey: process.env['ETHERSCAN_API_KEY'],
|
|
43
|
+
endpoint: options.endpoint,
|
|
44
|
+
forceLicense: options.forceLicense,
|
|
45
|
+
license: options.license,
|
|
46
|
+
fixMispell: options.fixMispell,
|
|
47
|
+
},
|
|
48
|
+
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
program
|
|
54
|
+
.command('sourcify')
|
|
55
|
+
.description('submit contract for verification to sourcify')
|
|
56
|
+
.option('--endpoint <value>', 'endpoint to connect to')
|
|
57
|
+
.option('--min-interval <value>', 'min interval between request in ms')
|
|
58
|
+
.action(async (options: {endpoint?: string; minInterval?: string}) => {
|
|
59
|
+
const {environment, ...programOptions} = program.opts();
|
|
60
|
+
const resolvedConfig = await readAndResolveConfig({...programOptions});
|
|
61
|
+
run(resolvedConfig, environment, {
|
|
62
|
+
verifier: {
|
|
63
|
+
type: 'sourcify',
|
|
64
|
+
endpoint: options.endpoint,
|
|
65
|
+
},
|
|
66
|
+
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
program
|
|
71
|
+
.command('blockscout')
|
|
72
|
+
.description('submit contract for verification to blockscout')
|
|
73
|
+
.option('--endpoint <value>', 'endpoint to connect to')
|
|
74
|
+
.option('--min-interval <value>', 'min interval between request in ms')
|
|
75
|
+
// .option('--api <value>', 'api version (default to v2)')
|
|
76
|
+
.action(async (options: {endpoint?: string; minInterval?: string}) => {
|
|
77
|
+
const {environment, ...programOptions} = program.opts();
|
|
78
|
+
const resolvedConfig = await readAndResolveConfig({...programOptions});
|
|
79
|
+
run(resolvedConfig, environment, {
|
|
80
|
+
verifier: {
|
|
81
|
+
type: 'blockscout',
|
|
82
|
+
endpoint: options.endpoint,
|
|
83
|
+
},
|
|
84
|
+
minInterval: options.minInterval ? parseInt(options.minInterval) : undefined,
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
|
88
|
+
program
|
|
89
|
+
.command('metadata')
|
|
90
|
+
.description('export metadata')
|
|
91
|
+
.option('--out <value>', 'folder to write metadata into')
|
|
92
|
+
.action(async (options: {out?: string}) => {
|
|
93
|
+
const {environment, ...programOptions} = program.opts();
|
|
94
|
+
const resolvedConfig = await readAndResolveConfig({...programOptions});
|
|
95
|
+
await exportMetadata(resolvedConfig, environment, {out: options.out || '_metadata'});
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
program.parse(process.argv);
|
package/src/etherscan.ts
ADDED
|
@@ -0,0 +1,420 @@
|
|
|
1
|
+
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
2
|
+
import fs from 'fs';
|
|
3
|
+
import * as qs from 'neoqs';
|
|
4
|
+
import chalk from 'chalk';
|
|
5
|
+
import {matchAll} from './utils/match-all.js';
|
|
6
|
+
import {UnknownDeployments} from '@rocketh/core/types';
|
|
7
|
+
import {EtherscanOptions} from './index.js';
|
|
8
|
+
|
|
9
|
+
const defaultEndpoint = `https://api.etherscan.io/v2/api`;
|
|
10
|
+
function log(...args: any[]) {
|
|
11
|
+
console.log(...args);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function logError(...args: any[]) {
|
|
15
|
+
console.log(chalk.red(...args));
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function logInfo(...args: any[]) {
|
|
19
|
+
console.log(chalk.yellow(...args));
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function logSuccess(...args: any[]) {
|
|
23
|
+
console.log(chalk.green(...args));
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function sleep(ms: number) {
|
|
27
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function writeRequestIfRequested(
|
|
31
|
+
write: boolean,
|
|
32
|
+
networkName: string,
|
|
33
|
+
name: string,
|
|
34
|
+
request: string,
|
|
35
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
36
|
+
postData: any,
|
|
37
|
+
) {
|
|
38
|
+
if (write) {
|
|
39
|
+
try {
|
|
40
|
+
fs.mkdirSync('etherscan_requests');
|
|
41
|
+
} catch (e) {}
|
|
42
|
+
const folder = `etherscan_requests/${networkName}`;
|
|
43
|
+
try {
|
|
44
|
+
fs.mkdirSync(folder);
|
|
45
|
+
} catch (e) {}
|
|
46
|
+
fs.writeFileSync(`${folder}/${name}.formdata`, request);
|
|
47
|
+
fs.writeFileSync(`${folder}/${name}.json`, JSON.stringify(postData));
|
|
48
|
+
fs.writeFileSync(`${folder}/${name}_multi-source.json`, postData.sourceCode);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function extractOneLicenseFromSourceFile(source: string): string | undefined {
|
|
53
|
+
const licenses = extractLicenseFromSources(source);
|
|
54
|
+
if (licenses.length === 0) {
|
|
55
|
+
return undefined;
|
|
56
|
+
}
|
|
57
|
+
return licenses[0]; // TODO error out on multiple SPDX ?
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function extractLicenseFromSources(metadata: string): string[] {
|
|
61
|
+
const regex = /\/\/\s*\t*SPDX-License-Identifier:\s*\t*(.*?)[\s\\]/g;
|
|
62
|
+
const matches = matchAll(metadata, regex).toArray();
|
|
63
|
+
const licensesFound: {[license: string]: boolean} = {};
|
|
64
|
+
const licenses = [];
|
|
65
|
+
if (matches) {
|
|
66
|
+
for (const match of matches) {
|
|
67
|
+
if (!licensesFound[match]) {
|
|
68
|
+
licensesFound[match] = true;
|
|
69
|
+
licenses.push(match);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return licenses;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function getLicenseType(license: string): undefined | number {
|
|
77
|
+
const licenseType = (() => {
|
|
78
|
+
if (license === 'None') {
|
|
79
|
+
return 1;
|
|
80
|
+
}
|
|
81
|
+
if (license === 'UNLICENSED') {
|
|
82
|
+
return 2;
|
|
83
|
+
}
|
|
84
|
+
if (license === 'MIT') {
|
|
85
|
+
return 3;
|
|
86
|
+
}
|
|
87
|
+
if (license === 'GPL-2.0') {
|
|
88
|
+
return 4;
|
|
89
|
+
}
|
|
90
|
+
if (license === 'GPL-3.0') {
|
|
91
|
+
return 5;
|
|
92
|
+
}
|
|
93
|
+
if (license === 'LGPL-2.1') {
|
|
94
|
+
return 6;
|
|
95
|
+
}
|
|
96
|
+
if (license === 'LGPL-3.0') {
|
|
97
|
+
return 7;
|
|
98
|
+
}
|
|
99
|
+
if (license === 'BSD-2-Clause') {
|
|
100
|
+
return 8;
|
|
101
|
+
}
|
|
102
|
+
if (license === 'BSD-3-Clause') {
|
|
103
|
+
return 9;
|
|
104
|
+
}
|
|
105
|
+
if (license === 'MPL-2.0') {
|
|
106
|
+
return 10;
|
|
107
|
+
}
|
|
108
|
+
if (license === 'OSL-3.0') {
|
|
109
|
+
return 11;
|
|
110
|
+
}
|
|
111
|
+
if (license === 'Apache-2.0') {
|
|
112
|
+
return 12;
|
|
113
|
+
}
|
|
114
|
+
if (license === 'AGPL-3.0') {
|
|
115
|
+
return 13;
|
|
116
|
+
}
|
|
117
|
+
if (license === 'BUSL-1.1') {
|
|
118
|
+
return 14;
|
|
119
|
+
}
|
|
120
|
+
})();
|
|
121
|
+
return licenseType;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export async function submitSourcesToEtherscan(
|
|
125
|
+
env: {
|
|
126
|
+
deployments: UnknownDeployments;
|
|
127
|
+
networkName: string;
|
|
128
|
+
chainId: string;
|
|
129
|
+
deploymentNames?: string[];
|
|
130
|
+
minInterval?: number;
|
|
131
|
+
logErrorOnFailure?: boolean;
|
|
132
|
+
},
|
|
133
|
+
config?: EtherscanOptions,
|
|
134
|
+
): Promise<void> {
|
|
135
|
+
config = config || {type: 'etherscan'};
|
|
136
|
+
const licenseOption = config.license;
|
|
137
|
+
const forceLicense = config.forceLicense;
|
|
138
|
+
const etherscanApiKey = config.apiKey;
|
|
139
|
+
const all = env.deployments;
|
|
140
|
+
const networkName = env.networkName;
|
|
141
|
+
let endpoint = config.endpoint;
|
|
142
|
+
if (!endpoint) {
|
|
143
|
+
endpoint = defaultEndpoint;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async function submit(name: string) {
|
|
147
|
+
const deployment = all[name];
|
|
148
|
+
const {address, metadata: metadataString} = deployment;
|
|
149
|
+
const abiResponse = await fetch(
|
|
150
|
+
`${endpoint}?chainid=${env.chainId}&module=contract&action=getabi&address=${address}&apikey=${etherscanApiKey}`,
|
|
151
|
+
);
|
|
152
|
+
const json = await abiResponse.json();
|
|
153
|
+
let contractABI;
|
|
154
|
+
if (json.status !== '0') {
|
|
155
|
+
try {
|
|
156
|
+
contractABI = JSON.parse(json.result);
|
|
157
|
+
} catch (e) {
|
|
158
|
+
logError(`faled to get abi`, JSON.stringify(json), e);
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
if (contractABI && contractABI !== '') {
|
|
163
|
+
log(`already verified: ${name} (${address}), skipping.`);
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
if (!metadataString) {
|
|
168
|
+
logError(`Contract ${name} was deployed without saving metadata. Cannot submit to etherscan, skipping.`);
|
|
169
|
+
return;
|
|
170
|
+
}
|
|
171
|
+
const metadata = JSON.parse(metadataString);
|
|
172
|
+
const compilationTarget = metadata.settings?.compilationTarget;
|
|
173
|
+
|
|
174
|
+
let contractFilepath;
|
|
175
|
+
let contractName;
|
|
176
|
+
if (compilationTarget) {
|
|
177
|
+
contractFilepath = Object.keys(compilationTarget)[0];
|
|
178
|
+
contractName = compilationTarget[contractFilepath];
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
if (!contractFilepath || !contractName) {
|
|
182
|
+
return logError(
|
|
183
|
+
`Failed to extract contract fully qualified name from metadata.settings.compilationTarget for ${name}. Skipping.`,
|
|
184
|
+
);
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const contractNamePath = `${contractFilepath}:${contractName}`;
|
|
188
|
+
|
|
189
|
+
const contractSourceFile = metadata.sources[contractFilepath].content;
|
|
190
|
+
const sourceLicenseType = extractOneLicenseFromSourceFile(contractSourceFile);
|
|
191
|
+
|
|
192
|
+
let license = licenseOption;
|
|
193
|
+
if (!sourceLicenseType) {
|
|
194
|
+
if (!license) {
|
|
195
|
+
return logError(
|
|
196
|
+
`no license speccified in the source code for ${name} (${contractNamePath}), Please use option --license <SPDX>`,
|
|
197
|
+
);
|
|
198
|
+
}
|
|
199
|
+
} else {
|
|
200
|
+
if (license && license !== sourceLicenseType) {
|
|
201
|
+
if (!forceLicense) {
|
|
202
|
+
return logError(
|
|
203
|
+
`mismatch for --license option (${licenseOption}) and the one specified in the source code for ${name}.\nLicenses found in source : ${sourceLicenseType}\nYou can use option --force-license to force option --license`,
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
} else {
|
|
207
|
+
license = sourceLicenseType;
|
|
208
|
+
if (!getLicenseType(license)) {
|
|
209
|
+
return logError(
|
|
210
|
+
`license :"${license}" found in source code for ${name} (${contractNamePath}) but this license is not supported by etherscan, list of supported license can be found here : https://etherscan.io/contract-license-types . This tool expect the SPDX id, except for "None" and "UNLICENSED"`,
|
|
211
|
+
);
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const licenseType = getLicenseType(license);
|
|
217
|
+
|
|
218
|
+
if (!licenseType) {
|
|
219
|
+
return logError(
|
|
220
|
+
`license :"${license}" not supported by etherscan, list of supported license can be found here : https://etherscan.io/contract-license-types . This tool expect the SPDX id, except for "None" and "UNLICENSED"`,
|
|
221
|
+
);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
let solcInput: {
|
|
225
|
+
language: string;
|
|
226
|
+
settings: any;
|
|
227
|
+
sources: Record<string, {content: string}>;
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const settings = {...metadata.settings};
|
|
231
|
+
delete settings.compilationTarget;
|
|
232
|
+
solcInput = {
|
|
233
|
+
language: metadata.language,
|
|
234
|
+
settings,
|
|
235
|
+
sources: {},
|
|
236
|
+
};
|
|
237
|
+
for (const sourcePath of Object.keys(metadata.sources)) {
|
|
238
|
+
const source = metadata.sources[sourcePath];
|
|
239
|
+
// only content as this fails otherwise
|
|
240
|
+
solcInput.sources[sourcePath] = {
|
|
241
|
+
content: source.content,
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
// Adding Libraries ....
|
|
246
|
+
if (deployment.libraries) {
|
|
247
|
+
const settings = solcInput.settings;
|
|
248
|
+
settings.libraries = settings.libraries || {};
|
|
249
|
+
for (const libraryName of Object.keys(deployment.libraries)) {
|
|
250
|
+
if (!settings.libraries[contractNamePath]) {
|
|
251
|
+
settings.libraries[contractNamePath] = {};
|
|
252
|
+
}
|
|
253
|
+
settings.libraries[contractNamePath][libraryName] = deployment.libraries[libraryName];
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const solcInputString = JSON.stringify(solcInput);
|
|
257
|
+
|
|
258
|
+
if (env.minInterval) {
|
|
259
|
+
await sleep(env.minInterval);
|
|
260
|
+
}
|
|
261
|
+
logInfo(`verifying ${name} (${address}) ...`);
|
|
262
|
+
|
|
263
|
+
let constructorArguments: string | undefined;
|
|
264
|
+
if (deployment.argsData) {
|
|
265
|
+
constructorArguments = deployment.argsData.slice(2);
|
|
266
|
+
} else {
|
|
267
|
+
// TODO ?
|
|
268
|
+
// logInfo(`no argsData found, falling back on args (hardhat-deploy v1)`);
|
|
269
|
+
// if ((deployment as any).args) {
|
|
270
|
+
// const constructorABI: {inputs: any[]} = deployment.abi.find(
|
|
271
|
+
// (v) => v.type === 'constructor'
|
|
272
|
+
// );
|
|
273
|
+
// if (constructorABI) {
|
|
274
|
+
// constructorArguements = encode
|
|
275
|
+
// .encode(constructor.inputs, deployment.args)
|
|
276
|
+
// .slice(2);
|
|
277
|
+
// }
|
|
278
|
+
// } else {
|
|
279
|
+
// logInfo(`no args found, assuming empty constructor...`);
|
|
280
|
+
// }
|
|
281
|
+
logInfo(`no args found, assuming empty constructor...`);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
const postData: {
|
|
285
|
+
[fieldName: string]: string | number | void | undefined; // TODO type
|
|
286
|
+
} = {
|
|
287
|
+
apikey: etherscanApiKey,
|
|
288
|
+
module: 'contract',
|
|
289
|
+
action: 'verifysourcecode',
|
|
290
|
+
contractaddress: address,
|
|
291
|
+
sourceCode: solcInputString,
|
|
292
|
+
codeformat: 'solidity-standard-json-input',
|
|
293
|
+
contractname: contractNamePath,
|
|
294
|
+
compilerversion: `v${metadata.compiler.version}`, // see http://etherscan.io/solcversions for list of support versions
|
|
295
|
+
constructorArguements: constructorArguments, // note the spelling mistake by etherscan
|
|
296
|
+
licenseType,
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
if (config?.fixMispell) {
|
|
300
|
+
postData.constructorArguments = postData.constructorArguements;
|
|
301
|
+
delete postData.constructorArguements;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const formDataAsString = qs.stringify(postData);
|
|
305
|
+
const data = new URLSearchParams();
|
|
306
|
+
for (const entry of Object.entries(postData)) {
|
|
307
|
+
if (entry[1]) {
|
|
308
|
+
if (typeof entry[1] === 'number') {
|
|
309
|
+
data.append(entry[0], entry[1].toString());
|
|
310
|
+
} else {
|
|
311
|
+
data.append(entry[0], entry[1]);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
const submissionResponse = await fetch(`${endpoint}?chainid=${env.chainId}`, {
|
|
316
|
+
method: 'POST',
|
|
317
|
+
headers: {'content-type': 'application/x-www-form-urlencoded'},
|
|
318
|
+
body: data,
|
|
319
|
+
});
|
|
320
|
+
|
|
321
|
+
let submissionJson;
|
|
322
|
+
let guid: string | undefined;
|
|
323
|
+
const text = await submissionResponse.clone().text();
|
|
324
|
+
try {
|
|
325
|
+
submissionJson = await submissionResponse.json();
|
|
326
|
+
|
|
327
|
+
if (submissionJson?.status === '1') {
|
|
328
|
+
guid = submissionJson.result;
|
|
329
|
+
} else {
|
|
330
|
+
logError(
|
|
331
|
+
`contract ${name} failed to submit : "${submissionJson.message}" : "${submissionJson.result}"`,
|
|
332
|
+
JSON.stringify(submissionJson),
|
|
333
|
+
);
|
|
334
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
335
|
+
return;
|
|
336
|
+
}
|
|
337
|
+
} catch (err) {
|
|
338
|
+
logError(`failed to parse response`, text, err);
|
|
339
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
340
|
+
return;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (!guid) {
|
|
344
|
+
logError(`contract submission for ${name} failed to return a guid`);
|
|
345
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
346
|
+
return;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
async function checkStatus(): Promise<string | undefined> {
|
|
350
|
+
if (env.minInterval) {
|
|
351
|
+
await sleep(env.minInterval);
|
|
352
|
+
}
|
|
353
|
+
// TODO while loop and delay :
|
|
354
|
+
const statusResponse = await fetch(
|
|
355
|
+
`${endpoint}?chainid=${env.chainId}&apikey=${etherscanApiKey}&guid=${guid}&module=contract&action=checkverifystatus`,
|
|
356
|
+
);
|
|
357
|
+
const json = await statusResponse.json();
|
|
358
|
+
|
|
359
|
+
// blockscout seems to return status == 1 in case of failure
|
|
360
|
+
// so we check string first
|
|
361
|
+
if (json.result === 'Pending in queue') {
|
|
362
|
+
return undefined;
|
|
363
|
+
}
|
|
364
|
+
if (json.result !== 'Fail - Unable to verify') {
|
|
365
|
+
if (json.status === '1') {
|
|
366
|
+
// console.log(statusData);
|
|
367
|
+
return 'success';
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
logError(`Failed to verify contract ${name}: ${json.message}, ${json.result}`);
|
|
371
|
+
|
|
372
|
+
logError(
|
|
373
|
+
JSON.stringify(
|
|
374
|
+
{
|
|
375
|
+
apikey: 'XXXXXX',
|
|
376
|
+
module: 'contract',
|
|
377
|
+
action: 'verifysourcecode',
|
|
378
|
+
contractaddress: address,
|
|
379
|
+
sourceCode: '...',
|
|
380
|
+
codeformat: 'solidity-standard-json-input',
|
|
381
|
+
contractname: contractNamePath,
|
|
382
|
+
compilerversion: `v${metadata.compiler.version}`, // see http://etherscan.io/solcversions for list of support versions
|
|
383
|
+
constructorArguements: constructorArguments, // note the spelling mistake by etherscan
|
|
384
|
+
licenseType,
|
|
385
|
+
},
|
|
386
|
+
null,
|
|
387
|
+
' ',
|
|
388
|
+
),
|
|
389
|
+
);
|
|
390
|
+
// logError(JSON.stringify(postData, null, " "));
|
|
391
|
+
// logInfo(postData.sourceCode);
|
|
392
|
+
return 'failure';
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
logInfo('waiting for result...');
|
|
396
|
+
let result;
|
|
397
|
+
while (!result) {
|
|
398
|
+
await new Promise((resolve) => setTimeout(resolve, 10 * 1000));
|
|
399
|
+
result = await checkStatus();
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (result === 'success') {
|
|
403
|
+
logSuccess(` => contract ${name} is now verified`);
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
if (result === 'failure') {
|
|
407
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
408
|
+
logInfo('Etherscan failed to verify the source provided');
|
|
409
|
+
} else {
|
|
410
|
+
writeRequestIfRequested(env?.logErrorOnFailure || false, networkName, name, formDataAsString, postData);
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
for (const name of env.deploymentNames ? env.deploymentNames : Object.keys(all)) {
|
|
415
|
+
await submit(name);
|
|
416
|
+
if (env.minInterval) {
|
|
417
|
+
await sleep(env.minInterval);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import {loadDeploymentsFromFiles} from '@rocketh/node';
|
|
2
|
+
import type {ResolvedUserConfig} from '@rocketh/core/types';
|
|
3
|
+
import {submitSourcesToEtherscan} from './etherscan.js';
|
|
4
|
+
import {submitSourcesToSourcify} from './sourcify.js';
|
|
5
|
+
import {submitSourcesToBlockscout} from './blockscout.js';
|
|
6
|
+
|
|
7
|
+
export type EtherscanOptions = {
|
|
8
|
+
type: 'etherscan';
|
|
9
|
+
endpoint?: string;
|
|
10
|
+
apiKey?: string;
|
|
11
|
+
license?: string;
|
|
12
|
+
forceLicense?: boolean;
|
|
13
|
+
fixMispell?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export type SourcifyOptions = {
|
|
17
|
+
type: 'sourcify';
|
|
18
|
+
endpoint?: string;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type BlockscoutOptions = {
|
|
22
|
+
type: 'blockscout';
|
|
23
|
+
endpoint?: string;
|
|
24
|
+
// version?: string;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
export type VerificationOptions = {
|
|
28
|
+
verifier: EtherscanOptions | SourcifyOptions | BlockscoutOptions;
|
|
29
|
+
deploymentNames?: string[];
|
|
30
|
+
minInterval?: number;
|
|
31
|
+
logErrorOnFailure?: boolean;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export async function run(config: ResolvedUserConfig, environmentName: string, options: VerificationOptions) {
|
|
35
|
+
const {deployments, chainId} = await loadDeploymentsFromFiles(config.deployments, environmentName, false);
|
|
36
|
+
|
|
37
|
+
if (Object.keys(deployments).length === 0) {
|
|
38
|
+
console.log(`the environment ${environmentName} has zero deployments`);
|
|
39
|
+
process.exit();
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
if (!chainId) {
|
|
43
|
+
console.error(`the environment ${environmentName} has no chainId recorded`);
|
|
44
|
+
process.exit(1);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
if (options.verifier.type === 'etherscan') {
|
|
48
|
+
await submitSourcesToEtherscan(
|
|
49
|
+
{
|
|
50
|
+
chainId,
|
|
51
|
+
deployments,
|
|
52
|
+
networkName: environmentName, // TODO ? should this not be the actual network name
|
|
53
|
+
deploymentNames: options.deploymentNames,
|
|
54
|
+
minInterval: options.minInterval,
|
|
55
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
56
|
+
},
|
|
57
|
+
options.verifier,
|
|
58
|
+
);
|
|
59
|
+
} else if (options.verifier.type === 'sourcify') {
|
|
60
|
+
await submitSourcesToSourcify(
|
|
61
|
+
{
|
|
62
|
+
chainId,
|
|
63
|
+
deployments,
|
|
64
|
+
networkName: environmentName, // TODO ? should this not be the actual network name
|
|
65
|
+
deploymentNames: options.deploymentNames,
|
|
66
|
+
minInterval: options.minInterval,
|
|
67
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
68
|
+
},
|
|
69
|
+
options.verifier,
|
|
70
|
+
);
|
|
71
|
+
} else if (options.verifier.type === 'blockscout') {
|
|
72
|
+
await submitSourcesToBlockscout(
|
|
73
|
+
{
|
|
74
|
+
chainId,
|
|
75
|
+
deployments,
|
|
76
|
+
networkName: environmentName, // TODO ? should this not be the actual network name
|
|
77
|
+
deploymentNames: options.deploymentNames,
|
|
78
|
+
minInterval: options.minInterval,
|
|
79
|
+
logErrorOnFailure: options.logErrorOnFailure,
|
|
80
|
+
},
|
|
81
|
+
options.verifier,
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
}
|
package/src/metadata.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import {loadDeploymentsFromFiles} from '@rocketh/node';
|
|
2
|
+
import type {ResolvedUserConfig} from '@rocketh/core/types';
|
|
3
|
+
import fs from 'fs-extra';
|
|
4
|
+
import path from 'path';
|
|
5
|
+
|
|
6
|
+
export async function exportMetadata(config: ResolvedUserConfig, environmentName: string, {out}: {out: string}) {
|
|
7
|
+
const {deployments, chainId} = await loadDeploymentsFromFiles(config.deployments, environmentName, false);
|
|
8
|
+
|
|
9
|
+
if (Object.keys(deployments).length === 0) {
|
|
10
|
+
console.log(`the environment ${environmentName} has zero deployments`);
|
|
11
|
+
process.exit();
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
if (!chainId) {
|
|
15
|
+
console.error(`the environment ${environmentName} has no chainId recorded`);
|
|
16
|
+
process.exit(1);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
const folder = path.join(out, environmentName);
|
|
20
|
+
fs.emptyDirSync(folder);
|
|
21
|
+
const deploymentNames = Object.keys(deployments);
|
|
22
|
+
for (const deploymentName of deploymentNames) {
|
|
23
|
+
const deployment = deployments[deploymentName];
|
|
24
|
+
if (deployment.metadata) {
|
|
25
|
+
fs.writeFileSync(
|
|
26
|
+
path.join(folder, `${deploymentName}_at_${deployment.address}.metadata.json`),
|
|
27
|
+
deployment.metadata,
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
}
|
package/src/sourcify.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
import chalk from 'chalk';
|
|
2
|
+
import fs from 'fs-extra';
|
|
3
|
+
import path from 'path';
|
|
4
|
+
import {UnknownDeployments} from '@rocketh/core/types';
|
|
5
|
+
import {SourcifyOptions} from './index.js';
|
|
6
|
+
|
|
7
|
+
function sleep(ms: number) {
|
|
8
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
function log(...args: any[]) {
|
|
12
|
+
console.log(...args);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function logError(...args: any[]) {
|
|
16
|
+
console.log(chalk.red(...args));
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
function logInfo(...args: any[]) {
|
|
20
|
+
console.log(chalk.yellow(...args));
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function logSuccess(...args: any[]) {
|
|
24
|
+
console.log(chalk.green(...args));
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function ensureTrailingSlash(s: string): string {
|
|
28
|
+
const lastChar = s.substr(-1);
|
|
29
|
+
if (lastChar != '/') {
|
|
30
|
+
s = s + '/';
|
|
31
|
+
}
|
|
32
|
+
return s;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// const defaultEndpoint = 'https://server.verificationstaging.shardlabs.io/';
|
|
36
|
+
const defaultEndpoint = 'https://sourcify.dev/server/';
|
|
37
|
+
|
|
38
|
+
export async function submitSourcesToSourcify(
|
|
39
|
+
env: {
|
|
40
|
+
deployments: UnknownDeployments;
|
|
41
|
+
networkName: string;
|
|
42
|
+
chainId: string;
|
|
43
|
+
deploymentNames?: string[];
|
|
44
|
+
minInterval?: number;
|
|
45
|
+
logErrorOnFailure?: boolean;
|
|
46
|
+
},
|
|
47
|
+
config?: SourcifyOptions,
|
|
48
|
+
): Promise<void> {
|
|
49
|
+
config = config || {type: 'sourcify'};
|
|
50
|
+
const all = env.deployments;
|
|
51
|
+
const url = config.endpoint ? ensureTrailingSlash(config.endpoint) : defaultEndpoint;
|
|
52
|
+
|
|
53
|
+
async function submit(name: string) {
|
|
54
|
+
const deployment = all[name];
|
|
55
|
+
const {address, metadata: metadataString} = deployment;
|
|
56
|
+
|
|
57
|
+
try {
|
|
58
|
+
const checkResponse = await fetch(
|
|
59
|
+
`${url}checkByAddresses?addresses=${address.toLowerCase()}&chainIds=${env.chainId}`,
|
|
60
|
+
);
|
|
61
|
+
const json = await checkResponse.json();
|
|
62
|
+
if (json[0].status === 'perfect') {
|
|
63
|
+
log(`already verified: ${name} (${address}), skipping.`);
|
|
64
|
+
return;
|
|
65
|
+
}
|
|
66
|
+
} catch (e) {
|
|
67
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if (!metadataString) {
|
|
71
|
+
logError(`Contract ${name} was deployed without saving metadata. Cannot submit to sourcify, skipping.`);
|
|
72
|
+
return;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
logInfo(`verifying ${name} (${address} on chain ${env.chainId}) ...`);
|
|
76
|
+
|
|
77
|
+
const formData = new FormData();
|
|
78
|
+
formData.append('address', address);
|
|
79
|
+
formData.append('chain', env.chainId);
|
|
80
|
+
const metadataBlob = new Blob([metadataString], {
|
|
81
|
+
type: 'application/json',
|
|
82
|
+
});
|
|
83
|
+
formData.append('files', metadataBlob, 'metadata.json');
|
|
84
|
+
|
|
85
|
+
try {
|
|
86
|
+
const submissionResponse = await fetch(url, {body: formData, method: 'POST'});
|
|
87
|
+
const json = await submissionResponse.json();
|
|
88
|
+
if (json.result[0].status === 'perfect') {
|
|
89
|
+
logSuccess(` => contract ${name} is now verified`);
|
|
90
|
+
} else {
|
|
91
|
+
logError(` => contract ${name} is not verified`);
|
|
92
|
+
}
|
|
93
|
+
} catch (e) {
|
|
94
|
+
if (env?.logErrorOnFailure) {
|
|
95
|
+
const failingMetadataFolder = path.join('failing_metadata', env.chainId);
|
|
96
|
+
fs.ensureDirSync(failingMetadataFolder);
|
|
97
|
+
fs.writeFileSync(path.join(failingMetadataFolder, `${name}_at_${address}.json`), metadataString);
|
|
98
|
+
}
|
|
99
|
+
logError(((e as any).response && JSON.stringify((e as any).response.data)) || e);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
for (const name of env.deploymentNames ? env.deploymentNames : Object.keys(all)) {
|
|
104
|
+
await submit(name);
|
|
105
|
+
if (env.minInterval) {
|
|
106
|
+
await sleep(env.minInterval);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
// taken from package match-all
|
|
2
|
+
export function matchAll(s: string, r: RegExp) {
|
|
3
|
+
return {
|
|
4
|
+
input: s,
|
|
5
|
+
regex: r,
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* next
|
|
9
|
+
* Get the next match in single group match.
|
|
10
|
+
*
|
|
11
|
+
* @name next
|
|
12
|
+
* @function
|
|
13
|
+
* @return {String|null} The matched snippet.
|
|
14
|
+
*/
|
|
15
|
+
next() {
|
|
16
|
+
let c = this.nextRaw();
|
|
17
|
+
if (c) {
|
|
18
|
+
for (let i = 1; i < c.length; i++) {
|
|
19
|
+
if (c[i]) {
|
|
20
|
+
return c[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return null;
|
|
25
|
+
},
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* nextRaw
|
|
29
|
+
* Get the next match in raw regex output. Usefull to get another group match.
|
|
30
|
+
*
|
|
31
|
+
* @name nextRaw
|
|
32
|
+
* @function
|
|
33
|
+
* @returns {Array|null} The matched snippet
|
|
34
|
+
*/
|
|
35
|
+
nextRaw() {
|
|
36
|
+
let c = this.regex.exec(this.input);
|
|
37
|
+
return c;
|
|
38
|
+
},
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* toArray
|
|
42
|
+
* Get all the matches.
|
|
43
|
+
*
|
|
44
|
+
* @name toArray
|
|
45
|
+
* @function
|
|
46
|
+
* @return {Array} The matched snippets.
|
|
47
|
+
*/
|
|
48
|
+
toArray() {
|
|
49
|
+
let res = [],
|
|
50
|
+
c = null;
|
|
51
|
+
|
|
52
|
+
while ((c = this.next())) {
|
|
53
|
+
res.push(c);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
return res;
|
|
57
|
+
},
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* reset
|
|
61
|
+
* Reset the index.
|
|
62
|
+
*
|
|
63
|
+
* @name reset
|
|
64
|
+
* @function
|
|
65
|
+
* @param {Number} i The new index (default: `0`).
|
|
66
|
+
* @return {Number} The new index.
|
|
67
|
+
*/
|
|
68
|
+
reset(i: number = 0) {
|
|
69
|
+
return (this.regex.lastIndex = i);
|
|
70
|
+
},
|
|
71
|
+
};
|
|
72
|
+
}
|