forked from ethereumjs/ethereumjs-monorepo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweb3.ts
51 lines (45 loc) · 1.56 KB
/
web3.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
import { bytesToHex, hexToBytes, toBytes } from '@ethereumjs/util'
import { keccak256 } from 'ethereum-cryptography/keccak'
import { getClientVersion } from '../../util/index.js'
import { callWithStackTrace } from '../helpers.js'
import { middleware, validators } from '../validation.js'
import type { Chain } from '../../blockchain/index.js'
import type { EthereumClient } from '../../index.js'
import type { FullEthereumService } from '../../service/index.js'
import type { PrefixedHexString } from '@ethereumjs/util'
/**
* web3_* RPC module
* @memberof module:rpc/modules
*/
export class Web3 {
private _chain?: Chain
private _rpcDebug: boolean
/**
* Create web3_* RPC module
* @param client Client to which the module binds
*/
constructor(client: EthereumClient, rpcDebug: boolean) {
const service = client.service as FullEthereumService
this._chain = service.chain
this._rpcDebug = rpcDebug
this.clientVersion = middleware(this.clientVersion.bind(this), 0, [])
this.sha3 = middleware(callWithStackTrace(this.sha3.bind(this), this._rpcDebug), 1, [
[validators.hex],
])
}
/**
* Returns the current client version
* @param params An empty array
*/
clientVersion(_params = []) {
return getClientVersion()
}
/**
* Returns Keccak-256 (not the standardized SHA3-256) of the given data
* @param params The data to convert into a SHA3 hash
*/
sha3(params: PrefixedHexString[]): PrefixedHexString {
const hexEncodedDigest = bytesToHex(keccak256(toBytes(hexToBytes(params[0]))))
return hexEncodedDigest
}
}