Skip to content

Commit 878ab8a

Browse files
committed
examples: Fix all occurrences indicated by ESLint
1 parent ce8f84b commit 878ab8a

File tree

4 files changed

+78
-87
lines changed

4 files changed

+78
-87
lines changed

.eslintrc.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,6 @@ module.exports = {
2626
"react/require-default-props": [0],
2727
"jsx-a11y/anchor-is-valid": [0], // NextJS Link need to contain a hyperlink
2828
"react/prefer-stateless-function": [0],
29+
"eqeqeq": 0
2930
}
30-
};
31+
};

examples/constructAndPublishLater.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ const iota = require('@iota/core').composeAPI({
66
provider: 'https://node.deviceproof.org',
77
});
88

9+
function generateSeed(length = 81) {
10+
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
11+
const retVal = [81];
12+
for (let i = 0, n = charset.length; i < length; ++i) {
13+
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
14+
}
15+
const result = retVal.join('');
16+
return result;
17+
}
18+
919
(async () => {
1020
try {
1121
const seed = generateSeed();
@@ -17,7 +27,7 @@ const iota = require('@iota/core').composeAPI({
1727

1828
console.log('Generated RAAM channel. Reading channel...');
1929
const { messages, errors } = await raam.syncChannel({
20-
callback: (err, m) => {
30+
callback: (err) => {
2131
if (err) {
2232
console.error(err);
2333
}
@@ -52,13 +62,3 @@ const iota = require('@iota/core').composeAPI({
5262
console.error(e);
5363
}
5464
})();
55-
56-
function generateSeed(length = 81) {
57-
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
58-
const retVal = [81];
59-
for (let i = 0, n = charset.length; i < length; ++i) {
60-
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
61-
}
62-
const result = retVal.join('');
63-
return result;
64-
}

examples/manualMessageConstruction.js

+44-61
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,29 @@ const merkle = require('../lib/merkle');
33
const sign = require('../lib/sign');
44
const sender = require('../lib/message');
55
const file = require('../lib/file');
6-
const { intToTrytes, padTritsMultipleOf, digest } = require('../lib/helpers');
6+
const { digest } = require('../lib/helpers');
77
const fs = require('fs');
88
const iota = require('@iota/core').composeAPI({
99
provider: 'https://node.deviceproof.org',
1010
});
1111

12+
function generateSeed(length = 81) {
13+
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
14+
const retVal = [81];
15+
for (let i = 0, n = charset.length; i < length; ++i) {
16+
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
17+
}
18+
const result = retVal.join('');
19+
return result;
20+
}
21+
22+
function verify(merkleRoot, sig, message, verifyingKey, index, authPathHashes, nextRoot) {
23+
const sigDigest = digest(message, index, authPathHashes, verifyingKey, nextRoot);
24+
const verified = sign.verifyMessage(sig, sigDigest, verifyingKey);
25+
const authenticated = merkle.verifyMerkleTree(merkleRoot, verifyingKey, index, authPathHashes);
26+
return verified && authenticated;
27+
}
28+
1229
(async () => {
1330
try {
1431
const channelPassword = '';
@@ -24,7 +41,7 @@ const iota = require('@iota/core').composeAPI({
2441
security,
2542
progressCallback: (leafs, hashes) => {
2643
generated += leafs.length;
27-
console.log(`${Math.floor((generated / Math.pow(2, h)) * 10000) / 100}% complete`);
44+
console.log(`${Math.floor((generated / (2 ** h)) * 10000) / 100}% complete`);
2845
callback(leafs, hashes);
2946
},
3047
});
@@ -45,7 +62,6 @@ const iota = require('@iota/core').composeAPI({
4562
const authPath = merkle.getAuthPath(index, height);
4663
console.log('Auth path:', authPath);
4764
const authPathHashes = authPath.map((i, level) => hashes[level][i].hash);
48-
// await forge(merkleRoot, index, channelPassword)
4965

5066
const { private: signingKey, public: verifyingKey } = leafs[index];
5167
console.log(`Using ${index}th message to sign message '${message.slice(0, 81)}'...`);
@@ -58,87 +74,54 @@ const iota = require('@iota/core').composeAPI({
5874
console.log('Verified:', verify(merkleRoot, sig, message, verifyingKey, index, authPathHashes, nextRoot));
5975

6076
console.log('Publishing message...');
61-
const { bundle } = await sender.sendMessage(iota, merkleRoot, message, sig, index, verifyingKey, authPathHashes, { channelPassword, nextRoot });
77+
const { bundle } = await sender.sendMessage(
78+
iota, merkleRoot, message,
79+
sig, index, verifyingKey,
80+
authPathHashes, { channelPassword, nextRoot },
81+
);
6282
console.log('Bundle:', bundle[0].bundle);
6383

6484
console.log('Getting message...');
65-
const { message: tangleMessage, skipped } = await sender.getMessage(iota, merkleRoot, index, { channelPassword, height, security });
85+
const { message: tangleMessage, skipped } = await sender.getMessage(
86+
iota, merkleRoot, index,
87+
{
88+
channelPassword,
89+
height,
90+
security,
91+
},
92+
);
6693
skipped.forEach(e => console.error(`Bundle ${e.bundle} -`, e.error));
6794
console.log({
6895
index: tangleMessage.index,
6996
signature: `${converter.trytes(tangleMessage.signature).slice(0, 81)}...`,
7097
message: `${converter.trytesToAscii(tangleMessage.message).slice(0, 243)}...`,
71-
authPathHashes: tangleMessage.authPathHashes.map(h => `${converter.trytes(h).slice(0, 81)}...`),
98+
authPathHashes: tangleMessage.authPathHashes.map(hh => `${converter.trytes(hh).slice(0, 81)}...`),
7299
verifyingKey: `${converter.trytes(tangleMessage.verifyingKey).slice(0, 81)}...`,
73100
nextRoot: `${converter.trytes(tangleMessage.nextRoot).slice(0, 81)}...`,
74101
});
75102

76103
// Verifying message
77-
const tangleDigest = digest(tangleMessage.message, tangleMessage.index, tangleMessage.authPathHashes, tangleMessage.verifyingKey, tangleMessage.nextRoot);
104+
const tangleDigest = digest(
105+
tangleMessage.message, tangleMessage.index,
106+
tangleMessage.authPathHashes,
107+
tangleMessage.verifyingKey,
108+
tangleMessage.nextRoot,
109+
);
78110
console.log('Signature valid:', converter.trytes(sig) == converter.trytes(tangleMessage.signature));
79111
console.log('Auth path valid:', authPathHashes
80-
.map((h, i) => converter.trytes(h) == converter.trytes(tangleMessage.authPathHashes[i]))
112+
.map((hh, i) => converter.trytes(hh) == converter.trytes(tangleMessage.authPathHashes[i]))
81113
.reduce((acc, v) => acc && v, true));
82114
console.log('Verifying key valid:', converter.trytes(verifyingKey) == converter.trytes(tangleMessage.verifyingKey));
83115

84116
console.log('Message verified:', sign.verifyMessage(tangleMessage.signature, tangleDigest, tangleMessage.verifyingKey));
85117
console.log(
86118
'Signature authenticated:',
87-
merkle.verifyMerkleTree(merkleRoot, tangleMessage.verifyingKey, tangleMessage.index, tangleMessage.authPathHashes),
119+
merkle.verifyMerkleTree(
120+
merkleRoot, tangleMessage.verifyingKey,
121+
tangleMessage.index, tangleMessage.authPathHashes,
122+
),
88123
);
89124
} catch (err) {
90125
console.error(err);
91126
}
92127
})();
93-
94-
async function forge(merkleRoot, index, password) {
95-
const indexTrits = converter.trits(intToTrytes(index));
96-
const add = require('@iota/signing/out/signing/src/add').default;
97-
const Kerl = require('@iota/kerl').default;
98-
99-
const subroot = add(merkleRoot, indexTrits);
100-
const kerl = new Kerl();
101-
kerl.initialize();
102-
kerl.absorb(subroot, 0, subroot.length);
103-
if (password) {
104-
const padded = padTritsMultipleOf(Kerl.HASH_LENGTH, Kerl.HASH_LENGTH, password);
105-
kerl.absorb(padded, 0, padded.length);
106-
}
107-
const buffer = new Int8Array(Kerl.HASH_LENGTH);
108-
kerl.squeeze(buffer, 0, Kerl.HASH_LENGTH);
109-
const address = converter.trytes(buffer);
110-
111-
const transfers = [{
112-
address,
113-
value: 0,
114-
message: 'SDFGHJBVCSRTZUJBVCSERTZHBVCD',
115-
}, {
116-
address,
117-
value: 0,
118-
message: 'ERTZHJNBVCSRTZHBVCRTHBVCDRTGHBV',
119-
}, {
120-
address,
121-
value: 0,
122-
message: 'WILLITBESKIPPED',
123-
}];
124-
const trytes = await iota.prepareTransfers('9'.repeat(81), transfers);
125-
const bundle = await iota.sendTrytes(trytes, 3, 9);
126-
console.log('Forged bundle:', bundle[0].bundle);
127-
}
128-
129-
function verify(merkleRoot, sig, message, verifyingKey, index, authPathHashes, nextRoot) {
130-
const sigDigest = digest(message, index, authPathHashes, verifyingKey, nextRoot);
131-
const verified = sign.verifyMessage(sig, sigDigest, verifyingKey);
132-
const authenticated = merkle.verifyMerkleTree(merkleRoot, verifyingKey, index, authPathHashes);
133-
return verified && authenticated;
134-
}
135-
136-
function generateSeed(length = 81) {
137-
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
138-
const retVal = [81];
139-
for (let i = 0, n = charset.length; i < length; ++i) {
140-
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
141-
}
142-
const result = retVal.join('');
143-
return result;
144-
}

examples/simpleReadWrite.js

+21-14
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,16 @@ const iota = require('@iota/core').composeAPI({
55
provider: 'https://node.deviceproof.org',
66
});
77

8+
function generateSeed(length = 81) {
9+
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
10+
const retVal = [81];
11+
for (let i = 0, n = charset.length; i < length; ++i) {
12+
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
13+
}
14+
const result = retVal.join('');
15+
return result;
16+
}
17+
818
(async () => {
919
try {
1020
const messagePasswords = ['KUCHEN', 'MEHRKUCHEN', 'NOCHMEHRKUCHEN'];
@@ -17,7 +27,7 @@ const iota = require('@iota/core').composeAPI({
1727

1828
console.log('Generated RAAM channel. Reading channel...');
1929
const { messages, errors } = await raam.syncChannel({
20-
callback: (err, m) => {
30+
callback: (err) => {
2131
if (err) {
2232
console.error(err);
2333
}
@@ -30,8 +40,14 @@ const iota = require('@iota/core').composeAPI({
3040

3141
console.log('Publishing 2 messages...');
3242
console.log('Bundle 1:', (await raam.publish('HELLOIOTA', { messagePassword: messagePasswords[0] }))[0].bundle);
33-
const { channelRoot: nextRoot } = await RAAM.fromSeed(generateSeed(), { security: 1, height: 2 });
34-
console.log('Bundle 2:', (await raam.publish('', { index: 3, messagePassword: messagePasswords[2], nextRoot }))[0].bundle);
43+
const { channelRoot: nextRoot } = await RAAM.fromSeed(
44+
generateSeed(),
45+
{ security: 1, height: 2 },
46+
);
47+
console.log(
48+
'Bundle 2:',
49+
(await raam.publish('', { index: 3, messagePassword: messagePasswords[2], nextRoot }))[0].bundle,
50+
);
3551

3652
let response = await raam.fetch({ end: 3, messagePasswords });
3753
console.log('Messages:', response.messages);
@@ -44,7 +60,8 @@ const iota = require('@iota/core').composeAPI({
4460
console.log('Branch messages:', response.messages);
4561

4662
const reader = new RAAMReader(raam.channelRoot, { iota, channelPassword: 'PASSWORD' });
47-
// fetching will stop after index 1 because it is empty and reader has no locally stored messages
63+
// fetching will stop after index 1 because it is empty and reader has
64+
// no locally stored messages
4865
// messages will be empty
4966
response = await reader.fetch({ start: 1, messagePasswords });
5067
console.log('Messages:', response.messages);
@@ -61,13 +78,3 @@ const iota = require('@iota/core').composeAPI({
6178
console.error(e);
6279
}
6380
})();
64-
65-
function generateSeed(length = 81) {
66-
const charset = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ9';
67-
const retVal = [81];
68-
for (let i = 0, n = charset.length; i < length; ++i) {
69-
retVal[i] = charset.charAt(Math.floor(Math.random() * n));
70-
}
71-
const result = retVal.join('');
72-
return result;
73-
}

0 commit comments

Comments
 (0)