-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcheckEtherScan.js
executable file
·71 lines (66 loc) · 2.6 KB
/
checkEtherScan.js
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
const re = /<span id=('|")rawinput('|").*<\/span>/
const witnessNetworkMap = {
'mainnet': 'https://etherscan.io/tx',
'sepolia': 'https://sepolia.etherscan.io/tx',
'holesky': 'https://holesky.etherscan.io/tx',
}
function sleep(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
/**
* Checks if the witness verification hash matches the hash timestamped on an
* Ethereum blockchain, via etherscan.io.
* Steps:
* - Determines the etherscan.io witness url from the witness network passed
* in.
* - Does a lookup of a hash from the witness url combined with the transaction
* hash.
* - Returns true if witnessVerificationHash equals the hash value returned
* from the previous step, false otherwise.
* @param {string} witnessNetwork see the keys of witnessNetworkMap for
* possible values.
* @param {string} txHash the Ethereum transaction hash.
* @param {string} witnessVerificationHash SHA3 verification hash
* @returns {Promise<boolean>} whether the hash in the lookup matches witnessVerificationHash
*/
async function checkEtherScan(witnessNetwork, txHash, witnessVerificationHash) {
try {
const witnessURL = witnessNetworkMap[witnessNetwork]
const options = {
timeout: 10000 // 10 seconds
}
const response = await fetch(`${witnessURL}/${txHash}`, options)
if (!response.ok) {
return `ERROR HTTP ${response.status} ${response.statusText}`
}
const body = await response.text()
const outArray = re.exec(body)
let status = ''
if (!!outArray) {
let result = outArray[0].split('0x9cef4ea1')[1]
result = result.slice(0, 128)
//console.log(result == witnessVerificationHash)
status = `${result == witnessVerificationHash}`
} else {
status = 'Transaction hash not found'
}
// To avoid IP banning by etherscan.io
await sleep(300)
return status
}
catch (e) {
return e.toString()
}
}
async function testCheckEtherScan() {
let out = await checkEtherScan('sepolia', 'stuff', '9e518db9cfdcf9854bb7e5097ef15a77e3409c6ed3e26171ec62a075c4ef179a1651560e11b8bdd3e2ed70a1097afd4744b1dbf07c3c68884b1ebaca3026764d')
console.log(out)
out = await checkEtherScan('sepolia', '0x1b35843949a90869a7f79a132afcda0271799afd766140da1b13ae984beb6a80', 'stuff')
console.log(out)
out = await checkEtherScan('sepolia', '0x1b35843949a90869a7f79a132afcda0271799afd766140da1b13ae984beb6a80', '9e518db9cfdcf9854bb7e5097ef15a77e3409c6ed3e26171ec62a075c4ef179a1651560e11b8bdd3e2ed70a1097afd4744b1dbf07c3c68884b1ebaca3026764d')
console.log(out)
}
// testCheckEtherScan()
export { checkEtherScan, witnessNetworkMap}