UNPKG

2.58 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3var rlp = require("rlp");
4var ethUtil = require('ethereumjs-util');
5var Buffer = require('safe-buffer').Buffer;
6var Account = /** @class */ (function () {
7 function Account(data) {
8 var fields = [
9 {
10 name: 'nonce',
11 default: Buffer.alloc(0),
12 },
13 {
14 name: 'balance',
15 default: Buffer.alloc(0),
16 },
17 {
18 name: 'stateRoot',
19 length: 32,
20 default: ethUtil.KECCAK256_RLP,
21 },
22 {
23 name: 'codeHash',
24 length: 32,
25 default: ethUtil.KECCAK256_NULL,
26 },
27 ];
28 ethUtil.defineProperties(this, fields, data);
29 }
30 Account.prototype.serialize = function () {
31 return rlp.encode([this.nonce, this.balance, this.stateRoot, this.codeHash]);
32 };
33 Account.prototype.isContract = function () {
34 return this.codeHash.toString('hex') !== ethUtil.KECCAK256_NULL_S;
35 };
36 Account.prototype.getCode = function (trie, cb) {
37 if (!this.isContract()) {
38 cb(null, Buffer.alloc(0));
39 return;
40 }
41 trie.getRaw(this.codeHash, cb);
42 };
43 Account.prototype.setCode = function (trie, code, cb) {
44 var _this = this;
45 this.codeHash = ethUtil.keccak256(code);
46 if (this.codeHash.toString('hex') === ethUtil.KECCAK256_NULL_S) {
47 cb(null, Buffer.alloc(0));
48 return;
49 }
50 trie.putRaw(this.codeHash, code, function (err) {
51 cb(err, _this.codeHash);
52 });
53 };
54 Account.prototype.getStorage = function (trie, key, cb) {
55 var t = trie.copy();
56 t.root = this.stateRoot;
57 t.get(key, cb);
58 };
59 Account.prototype.setStorage = function (trie, key, val, cb) {
60 var _this = this;
61 var t = trie.copy();
62 t.root = this.stateRoot;
63 t.put(key, val, function (err) {
64 if (err)
65 return cb();
66 _this.stateRoot = t.root;
67 cb();
68 });
69 };
70 Account.prototype.isEmpty = function () {
71 return (this.balance.toString('hex') === '' &&
72 this.nonce.toString('hex') === '' &&
73 this.stateRoot.toString('hex') === ethUtil.KECCAK256_RLP_S &&
74 this.codeHash.toString('hex') === ethUtil.KECCAK256_NULL_S);
75 };
76 return Account;
77}());
78exports.default = Account;
79//# sourceMappingURL=index.js.map
\No newline at end of file