trac-msb 0.1.76 → 0.1.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,137 +0,0 @@
1
- import { isHexString, createHash } from './functions.js';
2
- import { OperationType } from './constants.js';
3
- import fileUtils from './fileUtils.js';
4
- import b4a from 'b4a';
5
- import Wallet from 'trac-wallet';
6
-
7
- class MsgUtils {
8
-
9
- static createMessage(...args) {
10
- let buf = null;
11
- if (args.length >= 1) {
12
- buf = b4a.concat(
13
- args.map(arg => b4a.from(arg, isHexString(arg) ? 'hex' : undefined))
14
- );
15
- }
16
- return buf;
17
- }
18
-
19
- static async #assembleMessageBase(wallet, keyParam, operationType) {
20
- let nonce = null;
21
- let msg = null;
22
- let hash = null;
23
- let baseKey = wallet.publicKey;
24
- let value = null;
25
-
26
- switch (operationType) {
27
- case OperationType.ADD_ADMIN:
28
- case OperationType.ADD_WRITER:
29
- case OperationType.REMOVE_WRITER:
30
- nonce = Wallet.generateNonce().toString('hex');
31
- msg = this.createMessage(wallet.publicKey, keyParam, nonce, operationType);
32
- hash = await createHash('sha256', msg);
33
- value = {
34
- pub : wallet.publicKey,
35
- wk: keyParam,
36
- nonce: nonce,
37
- sig: wallet.sign(hash)
38
- };
39
- break;
40
- case OperationType.APPEND_WHITELIST:
41
- case OperationType.WHITELISTED:
42
- case OperationType.BAN_VALIDATOR:
43
- case OperationType.ADD_INDEXER:
44
- case OperationType.REMOVE_INDEXER:
45
- nonce = Wallet.generateNonce().toString('hex');
46
- msg = this.createMessage(keyParam, nonce, operationType);
47
- hash = await createHash('sha256', msg);
48
- baseKey = keyParam;
49
- value = {
50
- nonce: nonce,
51
- sig: wallet.sign(hash)
52
- };
53
- break;
54
-
55
- default:
56
- return undefined;
57
- }
58
-
59
- return {
60
- type: operationType,
61
- key: baseKey,
62
- value
63
- };
64
- }
65
-
66
- static async assembleAdminMessage(adminEntry, writingKey, wallet, bootstrap) {
67
- if ((!adminEntry && wallet && writingKey && writingKey === bootstrap) || // Admin entry doesn't exist yet, thus admin public key can only be associated with bootstrap writing key
68
- (adminEntry && adminEntry.tracPublicKey === wallet.publicKey && writingKey && writingKey !== adminEntry.wk)) { // Admin entry exists and we have to update its writing key in base, so it can recover admin access
69
-
70
- return await this.#assembleMessageBase(wallet, writingKey, OperationType.ADD_ADMIN);
71
- }
72
- }
73
-
74
- static async assembleWhitelistMessages(adminEntry, wallet) {
75
- try {
76
- if (!adminEntry || !wallet || wallet.publicKey !== adminEntry.tracPublicKey) {
77
- return null;
78
- }
79
-
80
- const messages = [];
81
- const pubKeys = await fileUtils.readPublicKeysFromFile();
82
-
83
- for (const pubKey of pubKeys) {
84
- const assembledMessage = await this.#assembleMessageBase(wallet, pubKey, OperationType.APPEND_WHITELIST);
85
- messages.push(assembledMessage);
86
- }
87
-
88
- return messages;
89
- } catch (err) {
90
- console.log(`Failed to create whitelist messages: ${err.message}`);
91
- }
92
- }
93
-
94
- static async assembleAddWriterMessage(wallet, writingKey) {
95
- return await this.#assembleMessageBase(wallet, writingKey, OperationType.ADD_WRITER);
96
- }
97
-
98
- static async assembleRemoveWriterMessage(wallet, writingKey) {
99
- return await this.#assembleMessageBase(wallet, writingKey, OperationType.REMOVE_WRITER);
100
- }
101
-
102
- static async assembleAddIndexerMessage(wallet, writerTracPublicKey) {
103
- return await this.#assembleMessageBase(wallet, writerTracPublicKey, OperationType.ADD_INDEXER);
104
- }
105
-
106
- static async assembleRemoveIndexerMessage(wallet, writerTracPublicKey) {
107
- return await this.#assembleMessageBase(wallet, writerTracPublicKey, OperationType.REMOVE_INDEXER);
108
- }
109
-
110
- static async assembleBanValidatorMessage(wallet, writerTracPublicKey) {
111
- return await this.#assembleMessageBase(wallet, writerTracPublicKey, OperationType.BAN_VALIDATOR);
112
- }
113
-
114
- static async assembleWhitelistedMessage(wallet, writerTracPublicKey) {
115
- return await this.#assembleMessageBase(wallet, writerTracPublicKey, OperationType.WHITELISTED);
116
- }
117
-
118
- static async verifyEventMessage(parsedRequest, wallet, check) {
119
- const { type, key, value } = parsedRequest;
120
- if (
121
- type !== OperationType.ADD_ADMIN &&
122
- type !== OperationType.ADD_WRITER &&
123
- type !== OperationType.REMOVE_WRITER
124
- ) {
125
- return false;
126
- }
127
- const sanitizationResult = check.sanitizeAdminAndWritersOperations(parsedRequest);
128
- if (!sanitizationResult) return false;
129
-
130
- const msg = this.createMessage(key, value.wk, value.nonce, type);
131
- const hash = await createHash('sha256', msg);
132
- return wallet.verify(value.sig, hash, key);
133
- }
134
-
135
- }
136
-
137
- export default MsgUtils;
@@ -1,18 +0,0 @@
1
- // This runner is auto-generated by Brittle
2
-
3
- import { default as test } from 'brittle';
4
-
5
- async function runTests() {
6
- test.pause();
7
-
8
- await import('./check.test.js');
9
- await import('./fileUtils.test.js');
10
- await import('./functions.test.js');
11
-
12
- //TODO add test MsgUtils
13
- //TODO add test Apply function
14
-
15
- test.resume();
16
- }
17
-
18
- runTests();
@@ -1,21 +0,0 @@
1
- import test from 'brittle'
2
- import Check from '../src/utils/check.js';
3
-
4
- test('preTx', function (t) {
5
- const check = new Check();
6
- const validData = {
7
- op: 'pre-tx',
8
- tx: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
9
- is: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
10
- wp: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
11
- i: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
12
- ipk: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
13
- ch: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
14
- in: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
15
- bs: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234',
16
- mbs: 'abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234abcd1234'
17
- }
18
- const result = check.sanitizePreTx(validData)
19
- t.ok(result, 'Valid data should pass the sanitization')
20
-
21
- })
@@ -1,16 +0,0 @@
1
- import test from 'brittle';
2
- import fileUtils from '../src/utils/fileUtils.js';
3
-
4
- test('readPublicKeysFromFile', async (t) => {
5
- // TODO: This is reading the real whitelist file (which is not a good practice)
6
- // -- In the future, this function should be generalized so we can mock the file reading
7
- // -- and test the function without relying on the actual file.
8
- // -- For now, we will just check if the file reading works and returns an array of public keys.
9
- const pubKeys = await fileUtils.readPublicKeysFromFile();
10
- t.ok(Array.isArray(pubKeys), 'Should return an array');
11
- t.ok(pubKeys.length > 0, 'Should return a non-empty array'); // Assuming the file has at least one public key. Without being able to mock the file, we can't guarantee this.
12
- pubKeys.forEach((key) => {
13
- t.is(typeof key, 'string', 'Each public key should be a string');
14
- });
15
- }
16
- );
@@ -1,22 +0,0 @@
1
- import test from 'brittle';
2
- import * as functions from '../src/utils/functions.js';
3
-
4
- test('isHexString', (t) => {
5
- // t.ok(functions.isHexString('0x1234567890abcdef'), 'Valid hex string should return true'); // Deactivated. See TODO in functions.js
6
- t.ok(functions.isHexString('1234567890abcdef'), 'Valid hex string should return true');
7
- t.ok(functions.isHexString('1234567890xyz') === false, 'Invalid hex string should return false');
8
- t.ok(functions.isHexString('123456789') === false, 'Invalid size hex string should return false');
9
- // t.ok(functions.isHexString('') === false, 'Empty string should return false'); // Deactivated. See TODO in functions.js
10
- });
11
-
12
- test('createHash', async (t) => {
13
- // TODO: Add tests for other supported hash types
14
- t.test('sha256', async (k) => {
15
- const hash = await functions.createHash('sha256', 'test');
16
- k.is(typeof hash, 'string', 'Hash should be a string');
17
- k.ok(hash.length === 64, 'Hash should be 64 characters long');
18
- k.ok(hash.match(/^[a-f0-9]+$/), 'Hash should be a hex string');
19
- k.ok(hash !== await functions.createHash('sha256', 'Test'), 'Hash should be different for different inputs');
20
- k.ok(hash === await functions.createHash('sha256', 'test'), 'Hash should be the same for the same input');
21
- });
22
- });