11import { signTypedData , SignTypedDataVersion , TypedMessage , MessageTypes } from '@metamask/eth-sig-util'
22import { privateToAddress , toChecksumAddress , isValidPrivate , Address , toBytes , bytesToHex , Account } from '@ethereumjs/util'
33import { privateKeyToAccount } from 'web3-eth-accounts'
4- import { toBigInt } from 'web3-utils'
4+ import { toBigInt , toHex } from 'web3-utils'
55import * as crypto from 'crypto'
66
77type AccountType = {
@@ -13,9 +13,11 @@ export class Web3Accounts {
1313 accounts : Record < string , AccountType >
1414 accountsKeys : Record < string , string >
1515 vmContext
16+ options
1617
17- constructor ( vmContext ) {
18+ constructor ( vmContext , options ) {
1819 this . vmContext = vmContext
20+ this . options = options
1921 // TODO: make it random and/or use remix-libs
2022
2123 this . accounts = { }
@@ -97,6 +99,8 @@ export class Web3Accounts {
9799 eth_getBalance ( payload , cb ) {
98100 const address = payload . params [ 0 ]
99101 this . vmContext . vm ( ) . stateManager . getAccount ( Address . fromString ( address ) ) . then ( ( account ) => {
102+ if ( ! account ) return cb ( null , toBigInt ( 0 ) . toString ( 10 ) )
103+ if ( ! account . balance ) return cb ( null , toBigInt ( 0 ) . toString ( 10 ) )
100104 cb ( null , toBigInt ( account . balance ) . toString ( 10 ) )
101105 } ) . catch ( ( error ) => {
102106 cb ( error )
@@ -119,7 +123,9 @@ export class Web3Accounts {
119123 }
120124
121125 eth_chainId ( _payload , cb ) {
122- return cb ( null , '0x539' ) // 0x539 is hex of 1337
126+ if ( ! this . options . chainId ) return cb ( null , '0x539' ) // 0x539 is hex of 1337
127+ const id = ( typeof this . options . chainId === 'number' ) ? toHex ( this . options . chainId ) : this . options . chainId
128+ return cb ( null , id )
123129 }
124130
125131 eth_signTypedData_v4 ( payload , cb ) {
0 commit comments