-
Notifications
You must be signed in to change notification settings - Fork 900
/
accounts.ts
111 lines (95 loc) · 4.46 KB
/
accounts.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
import { BN, privateToAddress, toChecksumAddress, isValidPrivate } from 'ethereumjs-util'
import { stripHexPrefix } from 'ethjs-util'
import Web3 from 'web3'
import * as crypto from 'crypto'
export class Accounts {
web3
accounts: Record<string, unknown>
accountsKeys: Record<string, unknown>
executionContext
constructor (executionContext) {
this.web3 = new Web3()
this.executionContext = executionContext
// TODO: make it random and/or use remix-libs
this.accounts = {}
this.accountsKeys = {}
this.executionContext.init({ get: () => { return true } })
}
async resetAccounts (): Promise<void> {
// TODO: setting this to {} breaks the app currently, unclear why still
// this.accounts = {}
// this.accountsKeys = {}
await this._addAccount('503f38a9c967ed597e47fe25643985f032b072db8075426a92110f82df48dfcb', '0x56BC75E2D63100000')
await this._addAccount('7e5bfb82febc4c2c8529167104271ceec190eafdca277314912eaabdb67c6e5f', '0x56BC75E2D63100000')
await this._addAccount('cc6d63f85de8fef05446ebdd3c537c72152d0fc437fd7aa62b3019b79bd1fdd4', '0x56BC75E2D63100000')
await this._addAccount('638b5c6c8c5903b15f0d3bf5d3f175c64e6e98a10bdb9768a2003bf773dcb86a', '0x56BC75E2D63100000')
await this._addAccount('f49bf239b6e554fdd08694fde6c67dac4d01c04e0dda5ee11abee478983f3bc0', '0x56BC75E2D63100000')
await this._addAccount('adeee250542d3790253046eee928d8058fd544294a5219bea152d1badbada395', '0x56BC75E2D63100000')
await this._addAccount('097ffe12069dcb3c3d99e6771e2cbf491a9b8b2f93ff4d3468f550c5e8264755', '0x56BC75E2D63100000')
await this._addAccount('5f58e8b9f1867ef00578b6f03e159428ab168f776aa445bc3ecdb02c7db8e865', '0x56BC75E2D63100000')
await this._addAccount('290e721ac87c7b3f31bef7b70104b9280ed3fa1425a59451490c9c02bf50d08f', '0x56BC75E2D63100000')
await this._addAccount('27efe944ff128cf510ab447b529eec28772f13bf65ebf1cbd504192c4f26e9d8', '0x56BC75E2D63100000')
await this._addAccount('3cd7232cd6f3fc66a57a6bedc1a8ed6c228fff0a327e169c2bcc5e869ed49511', '0x56BC75E2D63100000')
await this._addAccount('2ac6c190b09897cd8987869cc7b918cfea07ee82038d492abce033c75c1b1d0c', '0x56BC75E2D63100000')
await this._addAccount('dae9801649ba2d95a21e688b56f77905e5667c44ce868ec83f82e838712a2c7a', '0x56BC75E2D63100000')
await this._addAccount('d74aa6d18aa79a05f3473dd030a97d3305737cbc8337d940344345c1f6b72eea', '0x56BC75E2D63100000')
await this._addAccount('71975fbf7fe448e004ac7ae54cad0a383c3906055a65468714156a07385e96ce', '0x56BC75E2D63100000')
}
_addAccount (privateKey, balance) {
return new Promise((resolve, reject) => {
privateKey = Buffer.from(privateKey, 'hex')
const address: Buffer = privateToAddress(privateKey)
this.accounts[toChecksumAddress('0x' + address.toString('hex'))] = { privateKey, nonce: 0 }
this.accountsKeys[toChecksumAddress('0x' + address.toString('hex'))] = '0x' + privateKey.toString('hex')
const stateManager = this.executionContext.vm().stateManager
stateManager.getAccount(address, (error, account) => {
if (error) {
console.log(error)
reject(error)
return
}
account.balance = balance || '0xf00000000000000001'
resolve()
})
})
}
newAccount (cb) {
let privateKey:Buffer
do {
privateKey = crypto.randomBytes(32)
} while (!isValidPrivate(privateKey))
this._addAccount(privateKey, '0x56BC75E2D63100000')
return cb(null, '0x' + privateToAddress(privateKey).toString('hex'))
}
methods (): Record<string, unknown> {
return {
eth_accounts: this.eth_accounts.bind(this),
eth_getBalance: this.eth_getBalance.bind(this),
eth_sign: this.eth_sign.bind(this)
}
}
eth_accounts (_payload, cb) {
return cb(null, Object.keys(this.accounts))
}
eth_getBalance (payload, cb) {
let address = payload.params[0]
address = stripHexPrefix(address)
this.executionContext.vm().stateManager.getAccount(Buffer.from(address, 'hex'), (err, account) => {
if (err) {
return cb(err)
}
cb(null, new BN(account.balance).toString(10))
})
}
eth_sign (payload, cb) {
const address = payload.params[0]
const message = payload.params[1]
const privateKey = this.accountsKeys[toChecksumAddress(address)]
if (!privateKey) {
return cb(new Error('unknown account'))
}
const account = this.web3.eth.accounts.privateKeyToAccount(privateKey)
const data = account.sign(message)
cb(null, data.signature)
}
}