UNPKG

2.6 kBJavaScriptView Raw
1var cryptoLib = require('../lib/crypto.js');
2var ByteBuffer = require('bytebuffer');
3var bignum = require('browserify-bignum');
4var crypto = require('crypto');
5var dappTransactionsLib = require('../lib/dapptransactions.js');
6
7function getBytes(block, skipSignature) {
8 var size = 8 + 4 + 4 + 4 + 32 + 32 + 8 + 4 + 4 + 64;
9
10 var bb = new ByteBuffer(size, true);
11
12 if (block.prevBlockId) {
13 var pb = bignum(block.prevBlockId).toBuffer({size: '8'});
14 for (var i = 0; i < 8; i++) {
15 bb.writeByte(pb[i]);
16 }
17 } else {
18 for (var i = 0; i < 8; i++) {
19 bb.writeByte(0);
20 }
21 }
22
23 bb.writeInt(block.height);
24 bb.writeInt(block.timestamp);
25 bb.writeInt(block.payloadLength);
26
27 var ph = new Buffer(block.payloadHash, 'hex');
28 for (var i = 0; i < ph.length; i++) {
29 bb.writeByte(ph[i]);
30 }
31
32 var pb = new Buffer(block.delegate, 'hex');
33 for (var i = 0; i < pb.length; i++) {
34 bb.writeByte(pb[i]);
35 }
36
37 if (block.pointId) {
38 var pb = bignum(block.pointId).toBuffer({ size: '8' });
39 for (var i = 0; i < 8; i++) {
40 bb.writeByte(pb[i]);
41 }
42 }
43
44 if (block.pointHeight) {
45 bb.writeInt(block.pointHeight);
46 }
47
48 bb.writeInt(block.count);
49
50 if (!skipSignature && block.signature) {
51 var pb = new Buffer(block.signature, 'hex');
52 for (var i = 0; i < pb.length; i++) {
53 bb.writeByte(pb[i]);
54 }
55 }
56
57 bb.flip();
58 var b = bb.toBuffer();
59
60 return b;
61}
62
63module.exports = {
64 new: function (genesisAccount, publicKeys) {
65 var delegates = publicKeys.map(function (key) {
66 return "+" + key;
67 })
68 var delegatesTransaction = {
69 type: 4,
70 amount: 0,
71 fee: 0,
72 timestamp: 0,
73 recipientId: null,
74 senderId: genesisAccount.address,
75 senderPublicKey: genesisAccount.keypair.publicKey,
76 asset: {
77 delegates: {
78 list: delegates
79 }
80 }
81 }
82 var block = {
83 delegate: genesisAccount.keypair.publicKey,
84 height: 1,
85 pointId: null,
86 pointHeight: null,
87 transactions: [],
88 timestamp: 0
89 }
90 block.transactions.push(delegatesTransaction);
91 block.count = block.transactions.length;
92
93 bytes = dappTransactionsLib.getTransactionBytes(delegatesTransaction);
94 delegatesTransaction.signature = cryptoLib.sign(genesisAccount.keypair, bytes);
95 bytes = dappTransactionsLib.getTransactionBytes(delegatesTransaction);
96 delegatesTransaction.id = cryptoLib.getId(bytes);
97
98 block.payloadLength = bytes.length;
99 block.payloadHash = crypto.createHash('sha256').update(bytes).digest().toString('hex');
100
101 var bytes = getBytes(block);
102 block.signature = cryptoLib.sign(genesisAccount.keypair, bytes);
103 bytes = getBytes(block);
104 block.id = cryptoLib.getId(bytes);
105
106 return block;
107 }
108}