viz-js-lib 0.11.0 → 0.12.4
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/.qoder/docs/spec/viz-dns-nameserver-spec.md +982 -0
- package/.qoder/docs/viz-cpp-node-docs/data-types.md +322 -0
- package/.qoder/docs/viz-cpp-node-docs/index.md +160 -0
- package/.qoder/docs/viz-cpp-node-docs/op-account-market.md +236 -0
- package/.qoder/docs/viz-cpp-node-docs/op-account.md +199 -0
- package/.qoder/docs/viz-cpp-node-docs/op-award.md +162 -0
- package/.qoder/docs/viz-cpp-node-docs/op-committee.md +193 -0
- package/.qoder/docs/viz-cpp-node-docs/op-content.md +157 -0
- package/.qoder/docs/viz-cpp-node-docs/op-escrow.md +224 -0
- package/.qoder/docs/viz-cpp-node-docs/op-invite.md +219 -0
- package/.qoder/docs/viz-cpp-node-docs/op-proposal.md +229 -0
- package/.qoder/docs/viz-cpp-node-docs/op-recovery.md +188 -0
- package/.qoder/docs/viz-cpp-node-docs/op-subscription.md +146 -0
- package/.qoder/docs/viz-cpp-node-docs/op-transfer-vesting.md +224 -0
- package/.qoder/docs/viz-cpp-node-docs/op-witness.md +252 -0
- package/.qoder/docs/viz-cpp-node-docs/plugins.md +887 -0
- package/.qoder/docs/viz-cpp-node-docs/virtual-operations.md +513 -0
- package/.qoder/repowiki/en/content/API Reference/API Reference.md +724 -0
- package/.qoder/repowiki/en/content/API Reference/Configuration Options.md +410 -0
- package/.qoder/repowiki/en/content/API Reference/Core API Methods.md +547 -0
- package/.qoder/repowiki/en/content/API Reference/Streaming APIs.md +380 -0
- package/.qoder/repowiki/en/content/API Reference/Transport Layer.md +341 -0
- package/.qoder/repowiki/en/content/API Reference/VIZ Blockchain Operations Coverage Status.md +427 -0
- package/.qoder/repowiki/en/content/Authentication & Cryptography/Authentication & Cryptography.md +430 -0
- package/.qoder/repowiki/en/content/Authentication & Cryptography/Digital Signatures.md +462 -0
- package/.qoder/repowiki/en/content/Authentication & Cryptography/Key Management.md +456 -0
- package/.qoder/repowiki/en/content/Authentication & Cryptography/Memo Encryption.md +331 -0
- package/.qoder/repowiki/en/content/Authentication & Cryptography/Security Practices.md +488 -0
- package/.qoder/repowiki/en/content/Broadcast Transactions/Broadcast Transactions.md +432 -0
- package/.qoder/repowiki/en/content/Broadcast Transactions/Network Broadcasting.md +418 -0
- package/.qoder/repowiki/en/content/Broadcast Transactions/Operation Construction.md +352 -0
- package/.qoder/repowiki/en/content/Broadcast Transactions/Transaction Preparation.md +353 -0
- package/.qoder/repowiki/en/content/Broadcast Transactions/Transaction Signing.md +404 -0
- package/.qoder/repowiki/en/content/Data Serialization/Data Serialization.md +540 -0
- package/.qoder/repowiki/en/content/Data Serialization/Encoding & Decoding.md +463 -0
- package/.qoder/repowiki/en/content/Data Serialization/Object Templates.md +413 -0
- package/.qoder/repowiki/en/content/Data Serialization/Type System.md +514 -0
- package/.qoder/repowiki/en/content/Data Serialization/Validation Rules.md +439 -0
- package/.qoder/repowiki/en/content/Examples & Tutorials.md +485 -0
- package/.qoder/repowiki/en/content/Getting Started.md +345 -0
- package/.qoder/repowiki/en/content/Testing & Development.md +637 -0
- package/.qoder/repowiki/en/content/Utilities & Helpers.md +557 -0
- package/.qoder/repowiki/en/meta/repowiki-metadata.json +1 -0
- package/VIZ-JS-LIB-COVERAGE-STATUS.md +356 -0
- package/config.json +3 -1
- package/dist/statistics.html +1 -1
- package/dist/viz-tests.min.js +32 -42
- package/dist/viz-tests.min.js.gz +0 -0
- package/dist/viz.min.js +8 -18
- package/dist/viz.min.js.gz +0 -0
- package/lib/api/methods.js +24 -0
- package/lib/auth/serializer/src/ChainTypes.js +2 -1
- package/lib/auth/serializer/src/operations.js +35 -2
- package/lib/broadcast/index.js +29 -15
- package/lib/broadcast/operations.js +4 -0
- package/lib/dns.js +658 -0
- package/lib/index.js +3 -1
- package/package.json +3 -2
- package/test/dns.test.js +395 -0
- package/webpack/makeConfig.js +3 -0
|
@@ -0,0 +1,514 @@
|
|
|
1
|
+
# Type System
|
|
2
|
+
|
|
3
|
+
<cite>
|
|
4
|
+
**Referenced Files in This Document**
|
|
5
|
+
- [ChainTypes.js](file://src/auth/serializer/src/ChainTypes.js)
|
|
6
|
+
- [types.js](file://src/auth/serializer/src/types.js)
|
|
7
|
+
- [precision.js](file://src/auth/serializer/src/precision.js)
|
|
8
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js)
|
|
9
|
+
- [validation.js](file://src/auth/serializer/src/validation.js)
|
|
10
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js)
|
|
11
|
+
- [convert.js](file://src/auth/serializer/src/convert.js)
|
|
12
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js)
|
|
13
|
+
- [fast_parser.js](file://src/auth/serializer/src/fast_parser.js)
|
|
14
|
+
- [operations.js](file://src/auth/serializer/src/operations.js)
|
|
15
|
+
- [template.js](file://src/auth/serializer/src/template.js)
|
|
16
|
+
- [types_test.js](file://test/types_test.js)
|
|
17
|
+
- [all_types.js](file://test/all_types.js)
|
|
18
|
+
</cite>
|
|
19
|
+
|
|
20
|
+
## Table of Contents
|
|
21
|
+
1. [Introduction](#introduction)
|
|
22
|
+
2. [Project Structure](#project-structure)
|
|
23
|
+
3. [Core Components](#core-components)
|
|
24
|
+
4. [Architecture Overview](#architecture-overview)
|
|
25
|
+
5. [Detailed Component Analysis](#detailed-component-analysis)
|
|
26
|
+
6. [Dependency Analysis](#dependency-analysis)
|
|
27
|
+
7. [Performance Considerations](#performance-considerations)
|
|
28
|
+
8. [Troubleshooting Guide](#troubleshooting-guide)
|
|
29
|
+
9. [Conclusion](#conclusion)
|
|
30
|
+
10. [Appendices](#appendices)
|
|
31
|
+
|
|
32
|
+
## Introduction
|
|
33
|
+
This document explains the VIZ blockchain type system used for serialization and deserialization of on-chain data. It covers fundamental data types, precision handling for financial amounts, numeric conversions, validation rules, sorting semantics, and extensibility patterns. It also documents how serializers transform JavaScript objects into compact binary representations suitable for blockchain operations and how type safety and overflow protections are enforced.
|
|
34
|
+
|
|
35
|
+
## Project Structure
|
|
36
|
+
The type system is implemented under the serializer module. Key files include:
|
|
37
|
+
- Type definitions and builders for primitives, arrays, sets, maps, and complex types
|
|
38
|
+
- Precision utilities for asset-like numeric values
|
|
39
|
+
- Validation helpers for safe numeric conversions and bounds checking
|
|
40
|
+
- Serializers for converting between JS objects and binary buffers
|
|
41
|
+
- Specialized types such as object IDs, public keys, addresses, and vote IDs
|
|
42
|
+
- Operations that compose types into blockchain structures
|
|
43
|
+
|
|
44
|
+
```mermaid
|
|
45
|
+
graph TB
|
|
46
|
+
subgraph "Serializer Core"
|
|
47
|
+
T["types.js"]
|
|
48
|
+
S["serializer.js"]
|
|
49
|
+
C["convert.js"]
|
|
50
|
+
P["precision.js"]
|
|
51
|
+
NU["number_utils.js"]
|
|
52
|
+
V["validation.js"]
|
|
53
|
+
CT["ChainTypes.js"]
|
|
54
|
+
OID["object_id.js"]
|
|
55
|
+
FP["fast_parser.js"]
|
|
56
|
+
OPS["operations.js"]
|
|
57
|
+
TMPL["template.js"]
|
|
58
|
+
end
|
|
59
|
+
T --> S
|
|
60
|
+
T --> OPS
|
|
61
|
+
OPS --> S
|
|
62
|
+
C --> S
|
|
63
|
+
P --> T
|
|
64
|
+
NU --> T
|
|
65
|
+
V --> T
|
|
66
|
+
CT --> T
|
|
67
|
+
OID --> T
|
|
68
|
+
FP --> T
|
|
69
|
+
TMPL --> S
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
**Diagram sources**
|
|
73
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
74
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
75
|
+
- [convert.js](file://src/auth/serializer/src/convert.js#L1-L40)
|
|
76
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
77
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
78
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L1-L288)
|
|
79
|
+
- [ChainTypes.js](file://src/auth/serializer/src/ChainTypes.js#L1-L84)
|
|
80
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L1-L66)
|
|
81
|
+
- [fast_parser.js](file://src/auth/serializer/src/fast_parser.js#L1-L58)
|
|
82
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
83
|
+
- [template.js](file://src/auth/serializer/src/template.js#L1-L17)
|
|
84
|
+
|
|
85
|
+
**Section sources**
|
|
86
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
87
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
88
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
89
|
+
|
|
90
|
+
## Core Components
|
|
91
|
+
- Primitive types: integers, booleans, strings, bytes, variable-length integers, and time points
|
|
92
|
+
- Collection types: arrays, sets, fixed arrays, and maps
|
|
93
|
+
- Complex types: object IDs, protocol IDs, vote IDs, public keys, addresses, and assets
|
|
94
|
+
- Numeric utilities: implied decimal conversions and precision handling for financial amounts
|
|
95
|
+
- Validation helpers: safe numeric conversions, overflow checks, and range enforcement
|
|
96
|
+
- Serialization engine: a generic serializer that applies type-specific encoders/decoders
|
|
97
|
+
- Operation templates: predefined serializers for blockchain operations
|
|
98
|
+
|
|
99
|
+
Examples of usage are demonstrated in tests and operation definitions.
|
|
100
|
+
|
|
101
|
+
**Section sources**
|
|
102
|
+
- [types.js](file://src/auth/serializer/src/types.js#L30-L69)
|
|
103
|
+
- [types.js](file://src/auth/serializer/src/types.js#L71-L222)
|
|
104
|
+
- [types.js](file://src/auth/serializer/src/types.js#L224-L341)
|
|
105
|
+
- [types.js](file://src/auth/serializer/src/types.js#L343-L389)
|
|
106
|
+
- [types.js](file://src/auth/serializer/src/types.js#L391-L433)
|
|
107
|
+
- [types.js](file://src/auth/serializer/src/types.js#L435-L555)
|
|
108
|
+
- [types.js](file://src/auth/serializer/src/types.js#L559-L631)
|
|
109
|
+
- [types.js](file://src/auth/serializer/src/types.js#L633-L680)
|
|
110
|
+
- [types.js](file://src/auth/serializer/src/types.js#L682-L723)
|
|
111
|
+
- [types.js](file://src/auth/serializer/src/types.js#L725-L797)
|
|
112
|
+
- [types.js](file://src/auth/serializer/src/types.js#L799-L877)
|
|
113
|
+
- [types.js](file://src/auth/serializer/src/types.js#L879-L953)
|
|
114
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
115
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
116
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L1-L288)
|
|
117
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
118
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
119
|
+
- [types_test.js](file://test/types_test.js#L1-L141)
|
|
120
|
+
- [all_types.js](file://test/all_types.js#L1-L115)
|
|
121
|
+
|
|
122
|
+
## Architecture Overview
|
|
123
|
+
The type system is composed of:
|
|
124
|
+
- Type builders that define how to encode/decode values
|
|
125
|
+
- A generic Serializer that iterates over a type specification and applies the appropriate encoder/decoder
|
|
126
|
+
- Validation and numeric utilities ensuring correctness and preventing overflow
|
|
127
|
+
- Specialized parsers for cryptographic primitives and object IDs
|
|
128
|
+
|
|
129
|
+
```mermaid
|
|
130
|
+
classDiagram
|
|
131
|
+
class Serializer {
|
|
132
|
+
+constructor(operation_name, types)
|
|
133
|
+
+fromByteBuffer(b)
|
|
134
|
+
+appendByteBuffer(b, object)
|
|
135
|
+
+fromObject(obj)
|
|
136
|
+
+toObject(obj, debug)
|
|
137
|
+
+compare(a, b)
|
|
138
|
+
+fromHex(hex)
|
|
139
|
+
+toHex(object)
|
|
140
|
+
+toByteBuffer(object)
|
|
141
|
+
+toBuffer(object)
|
|
142
|
+
}
|
|
143
|
+
class Types {
|
|
144
|
+
+uint8
|
|
145
|
+
+uint16
|
|
146
|
+
+uint32
|
|
147
|
+
+int16
|
|
148
|
+
+int64
|
|
149
|
+
+uint64
|
|
150
|
+
+string
|
|
151
|
+
+string_binary
|
|
152
|
+
+bytes(size?)
|
|
153
|
+
+bool
|
|
154
|
+
+void
|
|
155
|
+
+array(inner)
|
|
156
|
+
+set(inner)
|
|
157
|
+
+fixed_array(n, inner)
|
|
158
|
+
+map(key, value)
|
|
159
|
+
+protocol_id_type(name)
|
|
160
|
+
+object_id_type
|
|
161
|
+
+vote_id
|
|
162
|
+
+optional(inner)
|
|
163
|
+
+static_variant(cases)
|
|
164
|
+
+public_key
|
|
165
|
+
+address
|
|
166
|
+
+time_point_sec
|
|
167
|
+
+asset
|
|
168
|
+
}
|
|
169
|
+
class Validation {
|
|
170
|
+
+required(value)
|
|
171
|
+
+require_range(min, max, value)
|
|
172
|
+
+no_overflow53(value)
|
|
173
|
+
+no_overflow64(value)
|
|
174
|
+
+to_long(value)
|
|
175
|
+
+to_number(value)
|
|
176
|
+
+to_string(value)
|
|
177
|
+
+require_object_type(...)
|
|
178
|
+
+get_instance(...)
|
|
179
|
+
}
|
|
180
|
+
class Precision {
|
|
181
|
+
+to_bigint64(number, precision)
|
|
182
|
+
+to_string64(number, precision)
|
|
183
|
+
+_internal.decimal_precision_string(number, precision)
|
|
184
|
+
}
|
|
185
|
+
Serializer --> Types : "uses"
|
|
186
|
+
Types --> Validation : "validates"
|
|
187
|
+
Types --> Precision : "financial conversions"
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
**Diagram sources**
|
|
191
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
192
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
193
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L1-L288)
|
|
194
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
195
|
+
|
|
196
|
+
## Detailed Component Analysis
|
|
197
|
+
|
|
198
|
+
### Primitive Types and Collections
|
|
199
|
+
- Integers: uint8, uint16, uint32, int16, int64, uint64 with range validation and conversion helpers
|
|
200
|
+
- Strings and bytes: UTF-8 strings, binary strings, and typed byte buffers
|
|
201
|
+
- Booleans and void placeholders
|
|
202
|
+
- Collections: arrays, sets (with uniqueness and sorting), fixed arrays, and maps (with key uniqueness and sorting)
|
|
203
|
+
- Optional wrapper for nullable fields
|
|
204
|
+
- Static variant for tagged unions of operations
|
|
205
|
+
|
|
206
|
+
These types define three methods per field:
|
|
207
|
+
- fromByteBuffer(b): decode from a ByteBuffer
|
|
208
|
+
- appendByteBuffer(b, object): encode to a ByteBuffer
|
|
209
|
+
- fromObject(object)/toObject(object, debug): convert between JS values and serialized form
|
|
210
|
+
|
|
211
|
+
Sorting behavior is applied consistently for sets and maps to ensure deterministic serialization.
|
|
212
|
+
|
|
213
|
+
**Section sources**
|
|
214
|
+
- [types.js](file://src/auth/serializer/src/types.js#L71-L222)
|
|
215
|
+
- [types.js](file://src/auth/serializer/src/types.js#L224-L341)
|
|
216
|
+
- [types.js](file://src/auth/serializer/src/types.js#L343-L389)
|
|
217
|
+
- [types.js](file://src/auth/serializer/src/types.js#L435-L555)
|
|
218
|
+
- [types.js](file://src/auth/serializer/src/types.js#L725-L797)
|
|
219
|
+
- [types.js](file://src/auth/serializer/src/types.js#L799-L877)
|
|
220
|
+
- [types.js](file://src/auth/serializer/src/types.js#L941-L952)
|
|
221
|
+
|
|
222
|
+
### Complex Types
|
|
223
|
+
- Object IDs: composite identifiers packed into a 64-bit integer; parsed from human-readable strings and serialized efficiently
|
|
224
|
+
- Protocol ID types: typed identifiers bound to reserved spaces and object types
|
|
225
|
+
- Vote IDs: composite type combining a type and an ID with bit packing/unpacking
|
|
226
|
+
- Public keys and addresses: cryptographic primitives with fast parser support and string conversions
|
|
227
|
+
- Time points: seconds-since-epoch timestamps normalized to uint32 internally
|
|
228
|
+
- Asset: amount plus symbol with implied decimal precision; conversion utilities ensure correct scaling
|
|
229
|
+
|
|
230
|
+
Precision handling for assets is implemented via implied decimal arithmetic and validated against 64-bit boundaries.
|
|
231
|
+
|
|
232
|
+
**Section sources**
|
|
233
|
+
- [types.js](file://src/auth/serializer/src/types.js#L559-L631)
|
|
234
|
+
- [types.js](file://src/auth/serializer/src/types.js#L633-L680)
|
|
235
|
+
- [types.js](file://src/auth/serializer/src/types.js#L879-L937)
|
|
236
|
+
- [types.js](file://src/auth/serializer/src/types.js#L391-L433)
|
|
237
|
+
- [types.js](file://src/auth/serializer/src/types.js#L30-L69)
|
|
238
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L1-L66)
|
|
239
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
240
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
241
|
+
|
|
242
|
+
### Validation and Numeric Utilities
|
|
243
|
+
Validation enforces:
|
|
244
|
+
- Required presence of values
|
|
245
|
+
- Range checks for integer types
|
|
246
|
+
- Safe numeric conversions with overflow detection for 53-bit and 64-bit domains
|
|
247
|
+
- String/number/Long conversions with explicit error messages
|
|
248
|
+
- Object type and instance extraction for protocol/implementation IDs
|
|
249
|
+
|
|
250
|
+
Numeric utilities provide:
|
|
251
|
+
- Implied decimal conversion for financial amounts
|
|
252
|
+
- Decimal precision string computation with trimming and padding
|
|
253
|
+
- Safe conversion to/from 64-bit integers with overflow checks
|
|
254
|
+
|
|
255
|
+
**Section sources**
|
|
256
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L1-L288)
|
|
257
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
258
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
259
|
+
|
|
260
|
+
### Serialization Engine
|
|
261
|
+
The Serializer class orchestrates:
|
|
262
|
+
- Iterating over a type specification to encode/decode fields
|
|
263
|
+
- Capturing and reporting detailed errors with field names and offsets
|
|
264
|
+
- Converting between hex, binary buffers, and JS objects
|
|
265
|
+
- Deterministic ordering via comparison functions for sortable types
|
|
266
|
+
|
|
267
|
+
It supports:
|
|
268
|
+
- fromByteBuffer and appendByteBuffer for binary I/O
|
|
269
|
+
- fromObject and toObject for developer-friendly conversions
|
|
270
|
+
- Helper methods toHex/toBuffer/toByteBuffer for interoperability
|
|
271
|
+
|
|
272
|
+
**Section sources**
|
|
273
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
274
|
+
|
|
275
|
+
### Type Registration and Extensibility
|
|
276
|
+
- Types are registered as named fields in a types object passed to Serializer
|
|
277
|
+
- New primitive or complex types can be added by implementing the three methods (fromByteBuffer, appendByteBuffer, fromObject/toObject)
|
|
278
|
+
- Collection types (array/set/map/static_variant) are factory functions that accept inner types
|
|
279
|
+
- Object IDs and protocol IDs integrate with ChainTypes to maintain semantic correctness
|
|
280
|
+
- Operations are defined as Serializer instances that compose existing types
|
|
281
|
+
|
|
282
|
+
Extensibility patterns:
|
|
283
|
+
- Add a new type definition in types.js
|
|
284
|
+
- Reference it in a Serializer for a specific operation
|
|
285
|
+
- Use Convert helpers to test round-trip conversions
|
|
286
|
+
|
|
287
|
+
**Section sources**
|
|
288
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
289
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
290
|
+
- [ChainTypes.js](file://src/auth/serializer/src/ChainTypes.js#L1-L84)
|
|
291
|
+
- [convert.js](file://src/auth/serializer/src/convert.js#L1-L40)
|
|
292
|
+
|
|
293
|
+
### Examples and Usage Patterns
|
|
294
|
+
- Primitive types: uint8, uint16, uint32, int16, int64, uint64, string, bytes, bool
|
|
295
|
+
- Complex types: asset, public_key, address, object_id_type, protocol_id_type, vote_id, time_point_sec
|
|
296
|
+
- Collections: array, set, fixed_array, map, optional, static_variant
|
|
297
|
+
- Operations: transaction, signed_transaction, block_header, and many domain operations
|
|
298
|
+
|
|
299
|
+
Round-trip testing ensures correctness across conversions and buffers.
|
|
300
|
+
|
|
301
|
+
**Section sources**
|
|
302
|
+
- [all_types.js](file://test/all_types.js#L1-L115)
|
|
303
|
+
- [types_test.js](file://test/types_test.js#L1-L141)
|
|
304
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
305
|
+
|
|
306
|
+
## Architecture Overview
|
|
307
|
+
|
|
308
|
+
```mermaid
|
|
309
|
+
sequenceDiagram
|
|
310
|
+
participant Dev as "Developer"
|
|
311
|
+
participant Conv as "Convert(type)"
|
|
312
|
+
participant Ser as "Serializer"
|
|
313
|
+
participant Type as "Type Definition"
|
|
314
|
+
participant BB as "ByteBuffer"
|
|
315
|
+
Dev->>Conv : toHex(object)
|
|
316
|
+
Conv->>BB : toByteBuffer(type, object)
|
|
317
|
+
BB-->>Conv : ByteBuffer
|
|
318
|
+
Conv-->>Dev : hex string
|
|
319
|
+
Dev->>Ser : toByteBuffer(object)
|
|
320
|
+
loop for each field
|
|
321
|
+
Ser->>Type : appendByteBuffer(b, object[field])
|
|
322
|
+
Type->>BB : write encoded bytes
|
|
323
|
+
end
|
|
324
|
+
Ser-->>Dev : ByteBuffer
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
**Diagram sources**
|
|
328
|
+
- [convert.js](file://src/auth/serializer/src/convert.js#L1-L40)
|
|
329
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L184-L188)
|
|
330
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
331
|
+
|
|
332
|
+
## Detailed Component Analysis
|
|
333
|
+
|
|
334
|
+
### Asset Type and Precision Handling
|
|
335
|
+
Asset values combine an amount and a symbol with implied decimal precision. The amount is stored as a 64-bit integer scaled by the precision, and the symbol is stored as a compact representation.
|
|
336
|
+
|
|
337
|
+
```mermaid
|
|
338
|
+
flowchart TD
|
|
339
|
+
Start(["Input: amount string, precision, symbol"]) --> Parse["Parse amount and symbol"]
|
|
340
|
+
Parse --> ValidateSymbol{"Symbol length ≤ 6?"}
|
|
341
|
+
ValidateSymbol --> |No| Error["Throw invalid symbol length"]
|
|
342
|
+
ValidateSymbol --> |Yes| Scale["Compute implied decimal scale"]
|
|
343
|
+
Scale --> EncodeAmount["Encode amount as int64"]
|
|
344
|
+
EncodeAmount --> WriteHeader["Write amount, precision, symbol bytes"]
|
|
345
|
+
WriteHeader --> End(["Serialized Asset"])
|
|
346
|
+
```
|
|
347
|
+
|
|
348
|
+
**Diagram sources**
|
|
349
|
+
- [types.js](file://src/auth/serializer/src/types.js#L30-L69)
|
|
350
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L14-L31)
|
|
351
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L10-L53)
|
|
352
|
+
|
|
353
|
+
**Section sources**
|
|
354
|
+
- [types.js](file://src/auth/serializer/src/types.js#L30-L69)
|
|
355
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
356
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
357
|
+
|
|
358
|
+
### Object ID Encoding
|
|
359
|
+
Object IDs pack space, type, and instance into a single 64-bit value. They support conversion from string to packed long and back.
|
|
360
|
+
|
|
361
|
+
```mermaid
|
|
362
|
+
flowchart TD
|
|
363
|
+
A["String 'space.type.instance'"] --> B["Extract components"]
|
|
364
|
+
B --> C["Shift and OR to form 64-bit"]
|
|
365
|
+
C --> D["Write/read Uint64"]
|
|
366
|
+
D --> E["Back to string 'space.type.instance'"]
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
**Diagram sources**
|
|
370
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L19-L62)
|
|
371
|
+
- [types.js](file://src/auth/serializer/src/types.js#L604-L631)
|
|
372
|
+
|
|
373
|
+
**Section sources**
|
|
374
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L1-L66)
|
|
375
|
+
- [types.js](file://src/auth/serializer/src/types.js#L604-L631)
|
|
376
|
+
|
|
377
|
+
### Vote ID Bit Packing
|
|
378
|
+
Vote IDs combine a type and an ID into a packed 32-bit value with dedicated masks.
|
|
379
|
+
|
|
380
|
+
```mermaid
|
|
381
|
+
flowchart TD
|
|
382
|
+
Start(["Input: type, id"]) --> CheckRange{"type ∈ [0,255]?<br/>id ∈ [0, 0xFFFFFF]?"}
|
|
383
|
+
CheckRange --> |No| Err["Throw out of range"]
|
|
384
|
+
CheckRange --> |Yes| Pack["value = (id << 8) | type"]
|
|
385
|
+
Pack --> Write["Write Uint32"]
|
|
386
|
+
Write --> End(["Serialized Vote ID"])
|
|
387
|
+
```
|
|
388
|
+
|
|
389
|
+
**Diagram sources**
|
|
390
|
+
- [types.js](file://src/auth/serializer/src/types.js#L633-L680)
|
|
391
|
+
|
|
392
|
+
**Section sources**
|
|
393
|
+
- [types.js](file://src/auth/serializer/src/types.js#L633-L680)
|
|
394
|
+
|
|
395
|
+
### Serialization Pipeline
|
|
396
|
+
The Serializer applies type-specific encoders/decoders to produce deterministic binary output.
|
|
397
|
+
|
|
398
|
+
```mermaid
|
|
399
|
+
sequenceDiagram
|
|
400
|
+
participant Caller as "Caller"
|
|
401
|
+
participant Ser as "Serializer"
|
|
402
|
+
participant Type as "Type"
|
|
403
|
+
participant BB as "ByteBuffer"
|
|
404
|
+
Caller->>Ser : appendByteBuffer(BB, object)
|
|
405
|
+
loop iterate fields
|
|
406
|
+
Ser->>Type : appendByteBuffer(BB, object[field])
|
|
407
|
+
Type->>BB : write bytes
|
|
408
|
+
end
|
|
409
|
+
Ser-->>Caller : ByteBuffer
|
|
410
|
+
```
|
|
411
|
+
|
|
412
|
+
**Diagram sources**
|
|
413
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L59-L77)
|
|
414
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
415
|
+
|
|
416
|
+
**Section sources**
|
|
417
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
418
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
419
|
+
|
|
420
|
+
## Dependency Analysis
|
|
421
|
+
|
|
422
|
+
```mermaid
|
|
423
|
+
graph LR
|
|
424
|
+
Types["types.js"] --> Validation["validation.js"]
|
|
425
|
+
Types --> Precision["precision.js"]
|
|
426
|
+
Types --> NumberUtils["number_utils.js"]
|
|
427
|
+
Types --> ObjectId["object_id.js"]
|
|
428
|
+
Types --> FastParser["fast_parser.js"]
|
|
429
|
+
Serializer["serializer.js"] --> Types
|
|
430
|
+
Serializer --> Convert["convert.js"]
|
|
431
|
+
Operations["operations.js"] --> Serializer
|
|
432
|
+
Operations --> Types
|
|
433
|
+
Template["template.js"] --> Serializer
|
|
434
|
+
```
|
|
435
|
+
|
|
436
|
+
**Diagram sources**
|
|
437
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
438
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L1-L288)
|
|
439
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L1-L86)
|
|
440
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L1-L54)
|
|
441
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L1-L66)
|
|
442
|
+
- [fast_parser.js](file://src/auth/serializer/src/fast_parser.js#L1-L58)
|
|
443
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
444
|
+
- [convert.js](file://src/auth/serializer/src/convert.js#L1-L40)
|
|
445
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
446
|
+
- [template.js](file://src/auth/serializer/src/template.js#L1-L17)
|
|
447
|
+
|
|
448
|
+
**Section sources**
|
|
449
|
+
- [types.js](file://src/auth/serializer/src/types.js#L1-L953)
|
|
450
|
+
- [serializer.js](file://src/auth/serializer/src/serializer.js#L1-L195)
|
|
451
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L1-L922)
|
|
452
|
+
|
|
453
|
+
## Performance Considerations
|
|
454
|
+
- Variable-length integers and strings are encoded with varints to minimize size
|
|
455
|
+
- Fixed-size types (uint8/uint16/uint32/int64/uint64) avoid overhead but require strict bounds
|
|
456
|
+
- Sorting for sets/maps ensures canonical order, which is essential for deterministic hashing/signing
|
|
457
|
+
- Fast parser shortcuts reduce overhead for cryptographic primitives
|
|
458
|
+
- Use of 64-bit integers prevents precision loss for large financial amounts
|
|
459
|
+
|
|
460
|
+
[No sources needed since this section provides general guidance]
|
|
461
|
+
|
|
462
|
+
## Troubleshooting Guide
|
|
463
|
+
Common issues and remedies:
|
|
464
|
+
- Out-of-range errors: Ensure integer values fit within declared ranges; use validation helpers
|
|
465
|
+
- Overflow errors: Large numbers must be handled as strings to prevent precision loss; use 64-bit conversion helpers
|
|
466
|
+
- Duplicate entries in sets/maps: These are rejected during validation; deduplicate before serialization
|
|
467
|
+
- Invalid object IDs: Must match the reserved space/type pattern; use provided helpers to parse/format
|
|
468
|
+
- Precision mismatches: Verify implied decimal scaling and precision alignment for assets
|
|
469
|
+
|
|
470
|
+
**Section sources**
|
|
471
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L149-L178)
|
|
472
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L249-L286)
|
|
473
|
+
- [types.js](file://src/auth/serializer/src/types.js#L435-L499)
|
|
474
|
+
- [types.js](file://src/auth/serializer/src/types.js#L799-L877)
|
|
475
|
+
- [object_id.js](file://src/auth/serializer/src/object_id.js#L27-L36)
|
|
476
|
+
|
|
477
|
+
## Conclusion
|
|
478
|
+
The VIZ type system provides a robust, extensible foundation for blockchain serialization. It enforces type safety, handles financial precision carefully, and offers deterministic encoding for cryptographic signatures. By composing primitive and complex types, developers can build secure and efficient blockchain operations while maintaining compatibility and readability.
|
|
479
|
+
|
|
480
|
+
[No sources needed since this section summarizes without analyzing specific files]
|
|
481
|
+
|
|
482
|
+
## Appendices
|
|
483
|
+
|
|
484
|
+
### Type Safety Measures
|
|
485
|
+
- Range checks for integer types
|
|
486
|
+
- Overflow detection for 53-bit and 64-bit domains
|
|
487
|
+
- Strict validation for object IDs and protocol types
|
|
488
|
+
- Implied decimal arithmetic for assets with precision-aware conversions
|
|
489
|
+
|
|
490
|
+
**Section sources**
|
|
491
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L149-L178)
|
|
492
|
+
- [validation.js](file://src/auth/serializer/src/validation.js#L249-L286)
|
|
493
|
+
- [precision.js](file://src/auth/serializer/src/precision.js#L14-L31)
|
|
494
|
+
- [number_utils.js](file://src/auth/serializer/src/number_utils.js#L10-L53)
|
|
495
|
+
|
|
496
|
+
### Dynamic Type Creation and Extensibility
|
|
497
|
+
- Add new types by implementing the standard methods and registering them in a Serializer
|
|
498
|
+
- Use factory types (array, set, map, static_variant) to compose complex structures
|
|
499
|
+
- Extend operations by referencing new or existing types in Serializer definitions
|
|
500
|
+
|
|
501
|
+
**Section sources**
|
|
502
|
+
- [types.js](file://src/auth/serializer/src/types.js#L343-L389)
|
|
503
|
+
- [types.js](file://src/auth/serializer/src/types.js#L725-L797)
|
|
504
|
+
- [operations.js](file://src/auth/serializer/src/operations.js#L55-L58)
|
|
505
|
+
|
|
506
|
+
### Example Workflows
|
|
507
|
+
- Converting an object to hex and back using Convert helpers
|
|
508
|
+
- Building a transaction with operations and serializing to buffer
|
|
509
|
+
- Using templates to generate default-filled operation structures
|
|
510
|
+
|
|
511
|
+
**Section sources**
|
|
512
|
+
- [convert.js](file://src/auth/serializer/src/convert.js#L1-L40)
|
|
513
|
+
- [all_types.js](file://test/all_types.js#L65-L114)
|
|
514
|
+
- [template.js](file://src/auth/serializer/src/template.js#L1-L17)
|