@leofcoin/messages 1.3.0 → 1.3.2
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.
- package/exports/index.js +8 -0
- package/exports/messages/block.js +55 -0
- package/exports/messages/bw-request.js +16 -0
- package/exports/messages/bw.js +18 -0
- package/exports/messages/contract.js +18 -0
- package/exports/messages/last-block-request.js +16 -0
- package/exports/messages/last-block.js +18 -0
- package/exports/messages/transaction.js +23 -0
- package/exports/messages/validator.js +18 -0
- package/exports/typings/index.d.ts +8 -0
- package/exports/typings/messages/block.d.ts +24 -0
- package/exports/typings/messages/bw-request.d.ts +5 -0
- package/exports/typings/messages/bw.d.ts +5 -0
- package/exports/typings/messages/contract.d.ts +5 -0
- package/exports/typings/messages/last-block-request.d.ts +5 -0
- package/exports/typings/messages/last-block.d.ts +5 -0
- package/exports/typings/messages/publish.d.ts +5 -0
- package/exports/typings/messages/transaction.d.ts +5 -0
- package/exports/typings/messages/validator.d.ts +5 -0
- package/exports/typings/protos/block.proto.d.ts +10 -0
- package/exports/typings/protos/bw-request.proto.d.ts +2 -0
- package/exports/typings/protos/bw.proto.d.ts +5 -0
- package/exports/typings/protos/contract.proto.d.ts +6 -0
- package/exports/typings/protos/last-block-request.proto.d.ts +2 -0
- package/exports/typings/protos/last-block.proto.d.ts +5 -0
- package/exports/typings/protos/publish.proto.d.ts +8 -0
- package/exports/typings/protos/transaction.proto.d.ts +10 -0
- package/exports/typings/protos/validator.proto.d.ts +5 -0
- package/exports/typings/workers/transaction-worker.d.ts +1 -0
- package/exports/workers/transaction-worker.js +23 -0
- package/package.json +16 -14
- package/rollup.config.js +54 -5
- package/src/{messages.js → index.ts} +7 -2
- package/src/messages/block.ts +56 -0
- package/src/messages/{bw-request.js → bw-request.ts} +2 -6
- package/src/messages/{bw.js → bw.ts} +2 -6
- package/src/messages/{contract.js → contract.ts} +2 -6
- package/src/messages/{last-block-request.js → last-block-request.ts} +2 -6
- package/src/messages/{last-block.js → last-block.ts} +2 -6
- package/src/messages/publish.ts +13 -0
- package/src/messages/{transaction.js → transaction.ts} +2 -6
- package/src/messages/validator.ts +13 -0
- package/src/protos/block.proto.js +8 -25
- package/src/protos/bw-request.proto.js +1 -4
- package/src/protos/bw.proto.js +3 -6
- package/src/protos/contract.proto.js +4 -6
- package/src/protos/last-block-request.proto.js +1 -4
- package/src/protos/last-block.proto.js +3 -6
- package/src/protos/publish.proto.js +7 -0
- package/src/protos/transaction.proto.js +8 -11
- package/src/protos/validator.proto.js +4 -0
- package/src/types.d.ts +1 -0
- package/test.js +83 -0
- package/tsconfig.json +16 -0
- package/dist/messages.esm +0 -7887
- package/dist/messages.js +0 -147
- package/dist/protons.esm +0 -3786
- package/src/messages/block.js +0 -17
- package/webpack.config.js +0 -22
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import proto from '
|
|
1
|
+
import proto from '../protos/last-block.proto.js'
|
|
2
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
3
|
|
|
4
4
|
export default class LastBlockMessage extends FormatInterface {
|
|
5
|
-
get keys() {
|
|
6
|
-
return ['hash', 'index']
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
get messageName() {
|
|
10
6
|
return 'LastBlockMessage'
|
|
11
7
|
}
|
|
12
8
|
|
|
13
|
-
constructor(buffer) {
|
|
9
|
+
constructor(buffer: messageInput) {
|
|
14
10
|
const name = 'last-block-message'
|
|
15
11
|
super(buffer, proto, {name})
|
|
16
12
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import proto from '../protos/publish.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
export default class BWRequestMessage extends FormatInterface {
|
|
5
|
+
get messageName() {
|
|
6
|
+
return 'PublishMessage'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(buffer: messageInput) {
|
|
10
|
+
const name = 'publish-message'
|
|
11
|
+
super(buffer, proto, {name})
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
import proto from '
|
|
1
|
+
import proto from '../protos/transaction.proto.js'
|
|
2
2
|
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
3
|
|
|
4
4
|
export default class TransactionMessage extends FormatInterface {
|
|
5
|
-
get keys() {
|
|
6
|
-
return ['timestamp', 'from', 'to', 'nonce', 'method', 'params', 'signature']
|
|
7
|
-
}
|
|
8
|
-
|
|
9
5
|
get messageName() {
|
|
10
6
|
return 'TransactionMessage'
|
|
11
7
|
}
|
|
12
8
|
|
|
13
|
-
constructor(buffer) {
|
|
9
|
+
constructor(buffer: messageInput) {
|
|
14
10
|
const name = 'transaction-message'
|
|
15
11
|
super(buffer, proto, {name})
|
|
16
12
|
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import proto from '../protos/validator.proto.js'
|
|
2
|
+
import { FormatInterface } from '@leofcoin/codec-format-interface'
|
|
3
|
+
|
|
4
|
+
export default class ValidatorMessage extends FormatInterface {
|
|
5
|
+
get messageName() {
|
|
6
|
+
return 'ValidatorMessage'
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
constructor(buffer: messageInput) {
|
|
10
|
+
const name = 'validator-message'
|
|
11
|
+
super(buffer, proto, {name})
|
|
12
|
+
}
|
|
13
|
+
}
|
|
@@ -1,26 +1,9 @@
|
|
|
1
|
-
export default
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export default {
|
|
2
|
+
index: Number(),
|
|
3
|
+
previousHash: String(),
|
|
4
|
+
timestamp: Number(),
|
|
5
|
+
reward: Number(),
|
|
6
|
+
fees: Number(),
|
|
7
|
+
transactions: new Uint8Array(),
|
|
8
|
+
validators: new Uint8Array()
|
|
5
9
|
}
|
|
6
|
-
|
|
7
|
-
message Transaction {
|
|
8
|
-
required string hash = 1;
|
|
9
|
-
required uint64 timestamp = 2;
|
|
10
|
-
required string from = 3;
|
|
11
|
-
required string to = 4;
|
|
12
|
-
required uint64 nonce = 5;
|
|
13
|
-
required string method = 6;
|
|
14
|
-
repeated string params = 7;
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
message BlockMessage {
|
|
18
|
-
required uint64 index = 1;
|
|
19
|
-
required string previousHash = 3;
|
|
20
|
-
required uint64 timestamp = 4;
|
|
21
|
-
required uint64 reward = 5;
|
|
22
|
-
required string fees = 6;
|
|
23
|
-
repeated Transaction transactions = 7;
|
|
24
|
-
repeated ValidatorMessage validators = 8;
|
|
25
|
-
}
|
|
26
|
-
`
|
package/src/protos/bw.proto.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
export default
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
repeated string constructorParameters = 3;
|
|
1
|
+
export default {
|
|
2
|
+
creator: String(),
|
|
3
|
+
contract: new Uint8Array(),
|
|
4
|
+
constructorParameters: Array()
|
|
6
5
|
}
|
|
7
|
-
`
|
|
@@ -1,12 +1,9 @@
|
|
|
1
|
-
export default
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
repeated string params = 6;
|
|
10
|
-
required string signature = 7;
|
|
1
|
+
export default {
|
|
2
|
+
timestamp: Number(),
|
|
3
|
+
from: String(),
|
|
4
|
+
to: String(),
|
|
5
|
+
nonce: Number(),
|
|
6
|
+
method: String(),
|
|
7
|
+
params: Array(),
|
|
8
|
+
signature: new Uint8Array()
|
|
11
9
|
}
|
|
12
|
-
`
|
package/src/types.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
declare type messageInput = Uint8Array | String | Object | ArrayBuffer
|
package/test.js
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import BlockMessage from "./exports/messages/block.js";
|
|
2
|
+
import BWRequestMessage from "./exports/messages/bw-request.js";
|
|
3
|
+
import { ContractMessage } from "./exports/index.js";
|
|
4
|
+
globalThis.peernet = globalThis.peernet || {}
|
|
5
|
+
peernet.codecs = {
|
|
6
|
+
'contract-message': {
|
|
7
|
+
codec: parseInt('63636d', 16),
|
|
8
|
+
hashAlg: 'keccak-256'
|
|
9
|
+
},
|
|
10
|
+
'transaction-message': {
|
|
11
|
+
codec: parseInt('746d', 16),
|
|
12
|
+
hashAlg: 'keccak-256'
|
|
13
|
+
},
|
|
14
|
+
|
|
15
|
+
'block-message': {
|
|
16
|
+
codec: parseInt('626d', 16),
|
|
17
|
+
hashAlg: 'keccak-256'
|
|
18
|
+
},
|
|
19
|
+
|
|
20
|
+
'bw-message': {
|
|
21
|
+
codec: parseInt('62776d', 16),
|
|
22
|
+
hashAlg: 'keccak-256'
|
|
23
|
+
},
|
|
24
|
+
|
|
25
|
+
'bw-request-message': {
|
|
26
|
+
codec: parseInt('6277726d', 16),
|
|
27
|
+
hashAlg: 'keccak-256'
|
|
28
|
+
},
|
|
29
|
+
'validator-message': {
|
|
30
|
+
codec: parseInt('766d', 16),
|
|
31
|
+
hashAlg: 'keccak-256'
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// globalThis.peernet.codecs
|
|
36
|
+
const validators = [{
|
|
37
|
+
address: 'address',
|
|
38
|
+
reward: 0
|
|
39
|
+
}]
|
|
40
|
+
|
|
41
|
+
const transactions = []
|
|
42
|
+
for (let i = 0; i <= 11_235; i++) {
|
|
43
|
+
transactions.push(
|
|
44
|
+
{
|
|
45
|
+
timestamp: new Date().getTime(),
|
|
46
|
+
from: 'fromAddress',
|
|
47
|
+
to: 'toAddress',
|
|
48
|
+
nonce: 0,
|
|
49
|
+
method: 'getAddress',
|
|
50
|
+
params: [],
|
|
51
|
+
signature: new Uint8Array(32)
|
|
52
|
+
}
|
|
53
|
+
)
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const block = {
|
|
57
|
+
index: 0,
|
|
58
|
+
previousHash: 'hash',
|
|
59
|
+
timestamp: new Date().getTime(),
|
|
60
|
+
reward: 0,
|
|
61
|
+
fees: 0,
|
|
62
|
+
transactions,
|
|
63
|
+
validators
|
|
64
|
+
}
|
|
65
|
+
const contractMessage = new ContractMessage({
|
|
66
|
+
creator: '',
|
|
67
|
+
contract: new Uint8Array(),
|
|
68
|
+
constructorParameters: []
|
|
69
|
+
})
|
|
70
|
+
|
|
71
|
+
const bw = new BWRequestMessage()
|
|
72
|
+
|
|
73
|
+
const message = new BlockMessage(block)
|
|
74
|
+
console.time('message encoded')
|
|
75
|
+
await message.encode()
|
|
76
|
+
console.timeEnd('message encoded')
|
|
77
|
+
console.time('normal encoded')
|
|
78
|
+
const normalEncoded = new TextEncoder().encode(JSON.stringify(block))
|
|
79
|
+
console.timeEnd('normal encoded')
|
|
80
|
+
|
|
81
|
+
console.log('# is smaller then normal encoded');
|
|
82
|
+
console.info(`normal: ${normalEncoded.length}\nmessage: ${message.encoded.length}`);
|
|
83
|
+
console.log(normalEncoded.length > message.encoded.length);
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"module": "ES2022",
|
|
4
|
+
"target": "es2022",
|
|
5
|
+
"outDir": "./exports",
|
|
6
|
+
"moduleResolution":"NodeNext",
|
|
7
|
+
"declaration": true,
|
|
8
|
+
"declarationDir": "./exports/typings",
|
|
9
|
+
"allowJs": true,
|
|
10
|
+
"allowSyntheticDefaultImports": true
|
|
11
|
+
},
|
|
12
|
+
"include": [
|
|
13
|
+
"./src/**/*"
|
|
14
|
+
],
|
|
15
|
+
"exclude": ["./node_modules"]
|
|
16
|
+
}
|