|
|
@ -3,7 +3,9 @@ const { BN, privateToAddress, isValidPrivate } = require('ethereumjs-util') |
|
|
|
const Web3 = require('web3') |
|
|
|
const Web3 = require('web3') |
|
|
|
const crypto = require('crypto') |
|
|
|
const crypto = require('crypto') |
|
|
|
|
|
|
|
|
|
|
|
const Accounts = function (executionContext) { |
|
|
|
class Accounts{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(executionContext) { |
|
|
|
this.web3 = new Web3() |
|
|
|
this.web3 = new Web3() |
|
|
|
this.executionContext = executionContext |
|
|
|
this.executionContext = executionContext |
|
|
|
// TODO: make it random and/or use remix-libs
|
|
|
|
// TODO: make it random and/or use remix-libs
|
|
|
@ -23,9 +25,9 @@ const Accounts = function (executionContext) { |
|
|
|
this.accountsKeys = {} |
|
|
|
this.accountsKeys = {} |
|
|
|
|
|
|
|
|
|
|
|
this.executionContext.init({get: () => { return true }}) |
|
|
|
this.executionContext.init({get: () => { return true }}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.init = async function () { |
|
|
|
async init () { |
|
|
|
let setBalance = (account) => { |
|
|
|
let setBalance = (account) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey |
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress(account.address)] = account.privateKey |
|
|
@ -45,9 +47,9 @@ Accounts.prototype.init = async function () { |
|
|
|
for (let _account of this.accountsList) { |
|
|
|
for (let _account of this.accountsList) { |
|
|
|
await setBalance(_account) |
|
|
|
await setBalance(_account) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.resetAccounts = function () { |
|
|
|
resetAccounts () { |
|
|
|
// TODO: setting this to {} breaks the app currently, unclear why still
|
|
|
|
// TODO: setting this to {} breaks the app currently, unclear why still
|
|
|
|
// this.accounts = {}
|
|
|
|
// this.accounts = {}
|
|
|
|
// this.accountsKeys = {}
|
|
|
|
// this.accountsKeys = {}
|
|
|
@ -56,9 +58,9 @@ Accounts.prototype.resetAccounts = function () { |
|
|
|
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') |
|
|
|
this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000') |
|
|
|
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') |
|
|
|
this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000') |
|
|
|
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') |
|
|
|
this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype._addAccount = function (privateKey, balance) { |
|
|
|
_addAccount (privateKey, balance) { |
|
|
|
privateKey = Buffer.from(privateKey, 'hex') |
|
|
|
privateKey = Buffer.from(privateKey, 'hex') |
|
|
|
const address = ethJSUtil.privateToAddress(privateKey) |
|
|
|
const address = ethJSUtil.privateToAddress(privateKey) |
|
|
|
|
|
|
|
|
|
|
@ -74,30 +76,30 @@ Accounts.prototype._addAccount = function (privateKey, balance) { |
|
|
|
|
|
|
|
|
|
|
|
this.accounts[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } |
|
|
|
this.accounts[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 } |
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex') |
|
|
|
this.accountsKeys[ethJSUtil.toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex') |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.newAccount = function (cb) { |
|
|
|
newAccount (cb) { |
|
|
|
let privateKey |
|
|
|
let privateKey |
|
|
|
do { |
|
|
|
do { |
|
|
|
privateKey = crypto.randomBytes(32) |
|
|
|
privateKey = crypto.randomBytes(32) |
|
|
|
} while (!isValidPrivate(privateKey)) |
|
|
|
} while (!isValidPrivate(privateKey)) |
|
|
|
this._addAccount(privateKey, '0x56BC75E2D63100000') |
|
|
|
this._addAccount(privateKey, '0x56BC75E2D63100000') |
|
|
|
return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
|
|
return cb(null, '0x' + privateToAddress(privateKey).toString('hex')) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.methods = function () { |
|
|
|
methods () { |
|
|
|
return { |
|
|
|
return { |
|
|
|
eth_accounts: this.eth_accounts.bind(this), |
|
|
|
eth_accounts: this.eth_accounts.bind(this), |
|
|
|
eth_getBalance: this.eth_getBalance.bind(this), |
|
|
|
eth_getBalance: this.eth_getBalance.bind(this), |
|
|
|
eth_sign: this.eth_sign.bind(this) |
|
|
|
eth_sign: this.eth_sign.bind(this) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_accounts = function (_payload, cb) { |
|
|
|
eth_accounts (_payload, cb) { |
|
|
|
return cb(null, Object.keys(this.accounts)) |
|
|
|
return cb(null, Object.keys(this.accounts)) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_getBalance = function (payload, cb) { |
|
|
|
eth_getBalance (payload, cb) { |
|
|
|
let address = payload.params[0] |
|
|
|
let address = payload.params[0] |
|
|
|
address = ethJSUtil.stripHexPrefix(address) |
|
|
|
address = ethJSUtil.stripHexPrefix(address) |
|
|
|
|
|
|
|
|
|
|
@ -107,9 +109,9 @@ Accounts.prototype.eth_getBalance = function (payload, cb) { |
|
|
|
} |
|
|
|
} |
|
|
|
cb(null, new BN(account.balance).toString(10)) |
|
|
|
cb(null, new BN(account.balance).toString(10)) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
Accounts.prototype.eth_sign = function (payload, cb) { |
|
|
|
eth_sign (payload, cb) { |
|
|
|
const address = payload.params[0] |
|
|
|
const address = payload.params[0] |
|
|
|
const message = payload.params[1] |
|
|
|
const message = payload.params[1] |
|
|
|
|
|
|
|
|
|
|
@ -122,6 +124,7 @@ Accounts.prototype.eth_sign = function (payload, cb) { |
|
|
|
const data = account.sign(message) |
|
|
|
const data = account.sign(message) |
|
|
|
|
|
|
|
|
|
|
|
cb(null, data.signature) |
|
|
|
cb(null, data.signature) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = Accounts |
|
|
|
module.exports = Accounts |
|
|
|