|
|
|
@ -3,7 +3,9 @@ const { BN, privateToAddress, isValidPrivate } = require('ethereumjs-util') |
|
|
|
|
const Web3 = require('web3') |
|
|
|
|
const crypto = require('crypto') |
|
|
|
|
|
|
|
|
|
const Accounts = function (executionContext) { |
|
|
|
|
class Accounts{ |
|
|
|
|
|
|
|
|
|
constructor(executionContext) { |
|
|
|
|
this.web3 = new Web3() |
|
|
|
|
this.executionContext = executionContext |
|
|
|
|
// TODO: make it random and/or use remix-libs
|
|
|
|
@ -25,7 +27,7 @@ const Accounts = function (executionContext) { |
|
|
|
|
this.executionContext.init({get: () => { return true }}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.init = async function () { |
|
|
|
|
async init () { |
|
|
|
|
let setBalance = (account) => { |
|
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey |
|
|
|
@ -47,7 +49,7 @@ Accounts.prototype.init = async function () { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.resetAccounts = function () { |
|
|
|
|
resetAccounts () { |
|
|
|
|
// TODO: setting this to {} breaks the app currently, unclear why still
|
|
|
|
|
// this.accounts = {}
|
|
|
|
|
// this.accountsKeys = {}
|
|
|
|
@ -58,7 +60,7 @@ Accounts.prototype.resetAccounts = function () { |
|
|
|
|
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype._addAccount = function (privateKey, balance) { |
|
|
|
|
_addAccount (privateKey, balance) { |
|
|
|
|
privateKey = Buffer.from(privateKey, 'hex') |
|
|
|
|
const address = ethJSUtil.privateToAddress(privateKey) |
|
|
|
|
|
|
|
|
@ -76,7 +78,7 @@ Accounts.prototype._addAccount = function (privateKey, balance) { |
|
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex') |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.newAccount = function (cb) { |
|
|
|
|
newAccount (cb) { |
|
|
|
|
let privateKey |
|
|
|
|
do { |
|
|
|
|
privateKey = crypto.randomBytes(32) |
|
|
|
@ -85,7 +87,7 @@ Accounts.prototype.newAccount = function (cb) { |
|
|
|
|
return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.methods = function () { |
|
|
|
|
methods () { |
|
|
|
|
return { |
|
|
|
|
eth_accounts: this.eth_accounts.bind(this), |
|
|
|
|
eth_getBalance: this.eth_getBalance.bind(this), |
|
|
|
@ -93,11 +95,11 @@ Accounts.prototype.methods = function () { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_accounts = function (_payload, cb) { |
|
|
|
|
eth_accounts (_payload, cb) { |
|
|
|
|
return cb(null, Object.keys(this.accounts)) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_getBalance = function (payload, cb) { |
|
|
|
|
eth_getBalance (payload, cb) { |
|
|
|
|
let address = payload.params[0] |
|
|
|
|
address = ethJSUtil.stripHexPrefix(address) |
|
|
|
|
|
|
|
|
@ -109,7 +111,7 @@ Accounts.prototype.eth_getBalance = function (payload, cb) { |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_sign = function (payload, cb) { |
|
|
|
|
eth_sign (payload, cb) { |
|
|
|
|
const address = payload.params[0] |
|
|
|
|
const message = payload.params[1] |
|
|
|
|
|
|
|
|
@ -123,5 +125,6 @@ Accounts.prototype.eth_sign = function (payload, cb) { |
|
|
|
|
|
|
|
|
|
cb(null, data.signature) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
module.exports = Accounts |
|
|
|
|