-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreceiver.js
120 lines (99 loc) · 3.24 KB
/
receiver.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
const Hyperswarm = require("hyperswarm");
const { Block, BlockHeader } = require("./block.js");
const crypto = require("crypto");
const util = require("util");
const scryptAsync = util.promisify(crypto.scrypt);
const swarm2 = new Hyperswarm();
const difficulty = 0x10; // Difficulty represented as hexadecimal
const maximumTarget = BigInt(
"0x000FFFFFFFFF0000000000000000000000000000000000000000000000000000"
);
const target = maximumTarget / BigInt(difficulty);
async function calculateHashForBlock(block) {
const data = [
block.blockHeader.version,
block.blockHeader.previousBlockHeader,
block.blockHeader.merkleRoot,
block.blockHeader.time,
block.nonce.toString(),
].join("");
const salt = crypto.randomBytes(16).toString("hex"); // Generate a random salt
const keylen = 32; // Length of the output key
// Adjust these for CPU-friendly mining
const N = 1024; // Lower CPU/memory cost factor
const r = 4; // Lower block size
const p = 1; // Lower parallelization factor
try {
const derivedKey = await scryptAsync(data, salt, keylen, { N, r, p });
return derivedKey.toString("hex");
} catch (err) {
throw err; // Handle the error as you see fit
}
}
async function mineBlock(block) {
console.log("target", target.toString());
let hash = await calculateHashForBlock(block);
console.log("Mining for hash", hash);
while (BigInt("0x" + hash) > target) {
block.nonce++;
hash = await calculateHashForBlock(block);
}
block.blockHeader.difficulty = "0x" + difficulty.toString(16); // Difficulty as hexadecimal
block.target = target;
block.blockHeader.hash = hash;
return block;
}
async function createNewBlock(transactions, minerAddress) {
const userTransactions = transactions.filter(
(transaction) =>
transaction.sender !== "coinbase" && transaction.sender !== "system"
);
const blockHeader = await createNewBlockHeader(transactions);
const index = 1;
console.log("INDEX", index);
const newBlock = new Block(
blockHeader,
index,
transactions,
userTransactions
);
return newBlock;
}
async function createNewBlockHeader(transactions) {
const timestamp = new Date().getTime();
const version = "1.0.0";
const previousBlockHeader = "12344567788"; // Placeholder value
const merkleRoot =
"f358f219b6b84556b6d1433ddfbcd36b18fff7fc193f4fa44b3b3a25ead68279"; // Placeholder value
const difficulty = null; // Placeholder value
return new BlockHeader(
version,
previousBlockHeader,
merkleRoot,
timestamp,
difficulty
);
}
const run = async () => {
const block1 = await createNewBlock([], "hello");
console.log("block Created");
const minedBlock = await mineBlock(block1);
console.log(minedBlock);
swarm2.on("connection", (conn, info) => {
console.log("inside");
conn.on("data", (data) =>
console.log("client got message:", data.toString())
);
});
const callFun = async () => {
const topic = Buffer.alloc(32, "UPOW"); // Fixed length buffer
console.log("Joining topic:", topic.toString());
const discovery = swarm2.join(topic, { server: false, client: true });
await discovery.flushed();
console.log("Topic announced on DHT");
await swarm2.flush();
console.log("done");
};
callFun();
};
run();