forked from ethereumjs/ethereumjs-account
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
79 lines (67 loc) · 1.78 KB
/
index.js
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
const ethUtil = require('@shyftnetwork/shyft_ethereumjs-util')
const rlp = require('rlp')
const Buffer = require('safe-buffer').Buffer
var Account = module.exports = function (data) {
// Define Properties
var fields = [{
name: 'nonce',
default: Buffer.alloc(0)
}, {
name: 'balance',
default: Buffer.alloc(0)
}, {
name: 'stateRoot',
length: 32,
default: ethUtil.SHA3_RLP
}, {
name: 'codeHash',
length: 32,
default: ethUtil.SHA3_NULL
}]
ethUtil.defineProperties(this, fields, data)
}
Account.prototype.serialize = function () {
return rlp.encode(this.raw)
}
Account.prototype.isContract = function () {
return this.codeHash.toString('hex') !== ethUtil.SHA3_NULL_S
}
Account.prototype.getCode = function (state, cb) {
if (!this.isContract()) {
cb(null, Buffer.alloc(0))
return
}
state.getRaw(this.codeHash, cb)
}
Account.prototype.setCode = function (trie, code, cb) {
var self = this
this.codeHash = ethUtil.sha3(code)
if (this.codeHash.toString('hex') === ethUtil.SHA3_NULL_S) {
cb(null, Buffer.alloc(0))
return
}
trie.putRaw(this.codeHash, code, function (err) {
cb(err, self.codeHash)
})
}
Account.prototype.getStorage = function (trie, key, cb) {
var t = trie.copy()
t.root = this.stateRoot
t.get(key, cb)
}
Account.prototype.setStorage = function (trie, key, val, cb) {
var self = this
var t = trie.copy()
t.root = self.stateRoot
t.put(key, val, function (err) {
if (err) return cb()
self.stateRoot = t.root
cb()
})
}
Account.prototype.isEmpty = function () {
return this.balance.toString('hex') === '' &&
this.nonce.toString('hex') === '' &&
this.stateRoot.toString('hex') === ethUtil.SHA3_RLP_S &&
this.codeHash.toString('hex') === ethUtil.SHA3_NULL_S
}