-
Notifications
You must be signed in to change notification settings - Fork 8
/
Block.js
188 lines (145 loc) · 4.22 KB
/
Block.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
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/**
* Created by Oleksandr <alex@moonion.com> on 2019-08-10
* moonion.com;
*/
import * as RLP from 'rlp'
import * as ethUtil from 'ethereumjs-util';
import BD from 'binary-data';
import PatriciaMerkle from "../lib/PatriciaMerkle";
import config from "../../config";
import * as BlockDb from './db/Block';
import * as BlockPoolDb from './db/BlockMemPool';
import * as Transaction from './Transaction';
import * as MODEL_PROTOCOLS from '../../schemas/model-protocols';
const Protocol = MODEL_PROTOCOLS.Block;
function getBuffer(block, force = false) {
if (!force && block._buffer)
return block._buffer;
const packet = BD.encode(block, Protocol);
block._buffer = packet.slice();
return block._buffer;
}
function fromBuffer(buffer) {
if (!buffer) return null;
let block = BD.decode(buffer, Protocol);
block._buffer = buffer;
return block
}
async function buildMerkle(block, force) {
if (block.transactions.length === 0)
return;
if (!force && block.merkleRootHash)
return;
let leaves = block.transactions.map(tx => ({key: tx.tokenId, hash: tx.hash}));
block._merkle = new PatriciaMerkle(leaves);
await block._merkle.buildTree();
}
async function getMerkleRootHash(block) {
if (block.merkleRootHash)
return block.merkleRootHash;
if (!block._merkle)
await buildMerkle(block);
block.merkleRootHash = block._merkle.getMerkleRoot();
return block.merkleRootHash
}
async function getProof(block, tokenId) {
if (!block._merkle)
await buildMerkle(block, true);
return block._merkle.getProof(tokenId, true)
}
async function checkProof(block, proof, hash) {
if (!block._merkle)
await buildMerkle(block, true);
return block._merkle.checkProof(proof, hash)
}
async function getSignHash(block) {
if (block._signHash)
return block._signHash;
if (!block._signBuffer) {
if (!block.merkleRootHash)
await getMerkleRootHash(block);
const dataToEncode = [
block.number,
block.merkleRootHash,
];
block._signBuffer = RLP.encode(dataToEncode);
}
block._signHash = ethUtil.keccak(block._signBuffer);
return block._signHash
}
async function sign(block) {
if (block.signature)
return block;
const _signHash = await getSignHash(block);
let msgHash = ethUtil.hashPersonalMessage(_signHash);
let key = Buffer.from(config.plasmaNodeKey, 'hex');
let sig = ethUtil.ecsign(msgHash, key);
block.signature = ethUtil.toBuffer(ethUtil.toRpcSig(sig.v, sig.r, sig.s));
return block.signature;
}
async function validate(block) {
for (let tx of block.transactions) {
let txValid = await Transaction.validate(tx);
if (!txValid)
return false;
}
return true;
}
async function pushToPool(block) {
return await BlockPoolDb.add(await getMerkleRootHash(block), getBuffer(block))
}
async function getPool(merkle) {
const hash = ethUtil.stripHexPrefix(merkle);
const buffer = await BlockPoolDb.get(hash);
return fromBuffer(buffer);
}
async function removeFromPool(block) {
return await BlockPoolDb.remove(await getMerkleRootHash(block));
}
async function getSigner(block) {
if (block._signer)
return block._signer;
const _signHash = await getSignHash(block);
let msgHash = ethUtil.hashPersonalMessage(_signHash);
let sig = ethUtil.fromRpcSig(ethUtil.addHexPrefix(block.signature));
let pubKey = ethUtil.ecrecover(msgHash, sig.v, sig.r, sig.s);
block._signer = ethUtil.bufferToHex(ethUtil.pubToAddress(pubKey));
return block._signer;
}
async function save(block) {
return await BlockDb.add(block.number, getBuffer(block, true))
}
async function get(number) {
const buffer = await BlockDb.get(number);
return fromBuffer(buffer);
}
function getJson(block) {
if (block._json)
return block._json;
block._json = {
number: block.number,
merkleRootHash: ethUtil.addHexPrefix(block.merkleRootHash.toString('hex')),
signature: ethUtil.addHexPrefix(block.signature.toString('hex')),
countTx: block.transactions.length,
transactions: block.transactions.map(tx => Transaction.getJson(tx))
};
return block._json;
}
export {
Protocol,
buildMerkle,
getProof,
checkProof,
getMerkleRootHash,
sign,
getSigner,
getBuffer,
fromBuffer,
validate,
pushToPool,
getPool,
removeFromPool,
save,
get,
getJson
}