Commit 533e0626 authored by aniket-engg's avatar aniket-engg Committed by Aniket

js to ts

parent c7107ce5
const Provider = require('./src/provider')
module.exports = {
Provider: Provider
}
import {Provider} from './provider'
export { Provider }
const ethJSUtil = require('ethereumjs-util') import { BN, privateToAddress, toChecksumAddress, isValidPrivate} from 'ethereumjs-util'
const { BN, privateToAddress, isValidPrivate } = require('ethereumjs-util') import { stripHexPrefix } from 'ethjs-util'
const Web3 = require('web3') import Web3 from 'web3'
const crypto = require('crypto') import * as crypto from 'crypto'
class Accounts{ export class Accounts {
web3
accounts
accountsKeys
executionContext
constructor(executionContext) { constructor(executionContext) {
this.web3 = new Web3() this.web3 = new Web3()
...@@ -39,10 +44,10 @@ class Accounts{ ...@@ -39,10 +44,10 @@ class Accounts{
_addAccount (privateKey, balance) { _addAccount (privateKey, balance) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
privateKey = Buffer.from(privateKey, 'hex') privateKey = Buffer.from(privateKey, 'hex')
const address = ethJSUtil.privateToAddress(privateKey) const address = privateToAddress(privateKey)
this.accounts[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
this.accountsKeys[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex') this.accountsKeys[toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex')
let stateManager = this.executionContext.vm().stateManager let stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => { stateManager.getAccount(address, (error, account) => {
...@@ -81,7 +86,7 @@ class Accounts{ ...@@ -81,7 +86,7 @@ class Accounts{
eth_getBalance (payload, cb) { eth_getBalance (payload, cb) {
let address = payload.params[0] let address = payload.params[0]
address = ethJSUtil.stripHexPrefix(address) address = stripHexPrefix(address)
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => { this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => {
if (err) { if (err) {
...@@ -95,7 +100,7 @@ class Accounts{ ...@@ -95,7 +100,7 @@ class Accounts{
const address = payload.params[0] const address = payload.params[0]
const message = payload.params[1] const message = payload.params[1]
const privateKey = this.accountsKeys[ethJSUtil.toChecksumAddress(address)] const privateKey = this.accountsKeys[toChecksumAddress(address)]
if (!privateKey) { if (!privateKey) {
return cb(new Error('unknown account')) return cb(new Error('unknown account'))
} }
...@@ -105,6 +110,4 @@ class Accounts{ ...@@ -105,6 +110,4 @@ class Accounts{
cb(null, data.signature) cb(null, data.signature)
} }
} }
\ No newline at end of file
module.exports = Accounts
\ No newline at end of file
class Blocks { export class Blocks {
executionContext
coinbase
blockNumber
constructor (executionContext, _options) { constructor (executionContext, _options) {
this.executionContext = executionContext this.executionContext = executionContext
const options = _options || {} const options = _options || {}
...@@ -130,10 +135,8 @@ class Blocks { ...@@ -130,10 +135,8 @@ class Blocks {
return cb(err, '') return cb(err, '')
} }
let value = Object.values(result.storage)[0].value let value = Object.values(result.storage)[0]['value']
cb(err, value) cb(err, value)
}) })
} }
} }
\ No newline at end of file
module.exports = Blocks
\ No newline at end of file
class Debug { export class Debug {
executionContext
constructor (executionContext) { constructor (executionContext) {
this.executionContext = executionContext this.executionContext = executionContext
} }
...@@ -29,5 +32,3 @@ class Debug { ...@@ -29,5 +32,3 @@ class Debug {
cb) cb)
} }
} }
module.exports = Debug
class Filters { export class Filters {
executionContext
constructor(executionContext) { constructor(executionContext) {
this.executionContext = executionContext this.executionContext = executionContext
} }
...@@ -58,5 +61,3 @@ class Filters { ...@@ -58,5 +61,3 @@ class Filters {
cb(null, results) cb(null, results)
} }
} }
module.exports = Filters
\ No newline at end of file
const Web3 = require('web3') import Web3 from 'web3'
const ethJSUtil = require('ethereumjs-util') import { toChecksumAddress, BN } from 'ethereumjs-util'
const processTx = require('./txProcess.js') import { processTx } from './txProcess'
const BN = ethJSUtil.BN
class Transactions{ class Transactions{
executionContext
accounts
constructor(executionContext) { constructor(executionContext) {
this.executionContext = executionContext this.executionContext = executionContext
...@@ -30,7 +32,7 @@ class Transactions{ ...@@ -30,7 +32,7 @@ class Transactions{
eth_sendTransaction (payload, cb) { eth_sendTransaction (payload, cb) {
// from might be lowercased address (web3) // from might be lowercased address (web3)
if (payload.params && payload.params.length > 0 && payload.params[0].from) { if (payload.params && payload.params.length > 0 && payload.params[0].from) {
payload.params[0].from = ethJSUtil.toChecksumAddress(payload.params[0].from) payload.params[0].from = toChecksumAddress(payload.params[0].from)
} }
processTx(this.executionContext, this.accounts, payload, false, cb) processTx(this.executionContext, this.accounts, payload, false, cb)
} }
...@@ -83,10 +85,10 @@ class Transactions{ ...@@ -83,10 +85,10 @@ class Transactions{
eth_call (payload, cb) { eth_call (payload, cb) {
// from might be lowercased address (web3) // from might be lowercased address (web3)
if (payload.params && payload.params.length > 0 && payload.params[0].from) { if (payload.params && payload.params.length > 0 && payload.params[0].from) {
payload.params[0].from = ethJSUtil.toChecksumAddress(payload.params[0].from) payload.params[0].from = toChecksumAddress(payload.params[0].from)
} }
if (payload.params && payload.params.length > 0 && payload.params[0].to) { if (payload.params && payload.params.length > 0 && payload.params[0].to) {
payload.params[0].to = ethJSUtil.toChecksumAddress(payload.params[0].to) payload.params[0].to = toChecksumAddress(payload.params[0].to)
} }
payload.params[0].value = undefined payload.params[0].value = undefined
...@@ -136,7 +138,7 @@ class Transactions{ ...@@ -136,7 +138,7 @@ class Transactions{
} }
if (receipt.to) { if (receipt.to) {
r.to = receipt.to r['to'] = receipt.to
} }
if (r.value === '0x') { if (r.value === '0x') {
...@@ -182,7 +184,7 @@ class Transactions{ ...@@ -182,7 +184,7 @@ class Transactions{
} }
if (receipt.to) { if (receipt.to) {
r.to = receipt.to r['to'] = receipt.to
} }
if (r.value === '0x') { if (r.value === '0x') {
...@@ -224,7 +226,7 @@ class Transactions{ ...@@ -224,7 +226,7 @@ class Transactions{
} }
if (receipt.to) { if (receipt.to) {
r.to = receipt.to r['to'] = receipt.to
} }
if (r.value === '0x') { if (r.value === '0x') {
...@@ -234,6 +236,4 @@ class Transactions{ ...@@ -234,6 +236,4 @@ class Transactions{
cb(null, r) cb(null, r)
}) })
} }
} }
\ No newline at end of file
module.exports = Transactions
\ No newline at end of file
...@@ -39,7 +39,7 @@ function createContract (payload, from, data, value, gasLimit, txRunner, callbac ...@@ -39,7 +39,7 @@ function createContract (payload, from, data, value, gasLimit, txRunner, callbac
let txRunnerInstance let txRunnerInstance
function processTx (executionContext, accounts, payload, isCall, callback) { export function processTx (executionContext, accounts, payload, isCall, callback) {
const api = { const api = {
logMessage: (msg) => { logMessage: (msg) => {
}, },
...@@ -94,5 +94,3 @@ function processTx (executionContext, accounts, payload, isCall, callback) { ...@@ -94,5 +94,3 @@ function processTx (executionContext, accounts, payload, isCall, callback) {
createContract(payload, from, data, value, gas, txRunnerInstance, callbacks, callback) createContract(payload, from, data, value, gas, txRunnerInstance, callbacks, callback)
} }
} }
module.exports = processTx
...@@ -5,7 +5,7 @@ const log = require('./utils/logs.js') ...@@ -5,7 +5,7 @@ const log = require('./utils/logs.js')
const merge = require('merge') const merge = require('merge')
const Accounts = require('./methods/accounts.js') const Accounts = require('./methods/accounts.js')
const Blocks = require('./methods/blocks.js') import { Blocks } from './methods/blocks'
const Filters = require('./methods/filters.js') const Filters = require('./methods/filters.js')
const Misc = require('./methods/misc.js') const Misc = require('./methods/misc.js')
const Net = require('./methods/net.js') const Net = require('./methods/net.js')
...@@ -14,7 +14,14 @@ const Debug = require('./methods/debug.js') ...@@ -14,7 +14,14 @@ const Debug = require('./methods/debug.js')
const generateBlock = require('./genesis.js') const generateBlock = require('./genesis.js')
class Provider { export class Provider {
options
executionContext
Accounts
Transactions
methods
constructor(options = {}) { constructor(options = {}) {
this.options = options this.options = options
// TODO: init executionContext here // TODO: init executionContext here
...@@ -74,6 +81,4 @@ class Provider { ...@@ -74,6 +81,4 @@ class Provider {
on (type, cb) { on (type, cb) {
this.executionContext.logsManager.addListener(type, cb) this.executionContext.logsManager.addListener(type, cb)
} }
} }
\ No newline at end of file
module.exports = Provider
\ No newline at end of file
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"types": ["node"]
},
"include": ["**/*.ts"]
}
\ No newline at end of file
{ {
"extends": "../../tsconfig.json", "extends": "./tsconfig.json",
"compilerOptions": { "compilerOptions": {
"module": "commonjs", "module": "commonjs",
"outDir": "../../dist/out-tsc", "outDir": "../../dist/out-tsc",
"allowJs": true, "declaration": true,
"declaration": true, "rootDir": "./src",
"rootDir": "./", "types": ["node"]
"types": ["node"] },
}, "exclude": [
"exclude": ["**/*.spec.js"], "**/*.spec.ts"
"include": [ ],
"src/**/*.js", "include": ["**/*.ts"]
"./index.js", }
"bin/"
]
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment