Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| b2e9af0acb | |||
| 4036133476 | |||
| 9817af8a46 | |||
| 6d7ab1e4ad | |||
| c80505c810 | |||
| 0e157f824a | |||
| faa20bd00b | |||
| 79b43b0665 | |||
| a84dcc880d | |||
| a368c64f7e | |||
| dbfc013c8a | |||
| 7a8e840793 |
@@ -4,6 +4,10 @@ Serialization helpers for the Gajumaru.
|
||||
|
||||
For an overview of the static serializer, see [this document](doc/static.md).
|
||||
|
||||
To export static templates as portable ASN.1 type definitions (for codegen
|
||||
in other languages; wire format remains RLP), see
|
||||
[doc/schema_export.md](doc/schema_export.md) and module `gmser_schema_export`.
|
||||
|
||||
## Build
|
||||
|
||||
$ rebar3 compile
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
# ASN.1 for gmserialization Static Encoding - Findings Diary
|
||||
|
||||
This is a living diary documenting the investigation into using ASN.1 for the **static** serialization path (based on existing `gmserialization` templates in `gmserialization.erl` and `gmser_chain_objects.erl`). Dynamic encoding is out of scope.
|
||||
|
||||
Focus areas:
|
||||
- Modeling static templates with ASN.1 (portability goal).
|
||||
- Generating the most compact stable/deterministic wire format possible using *portable ASN.1 techniques* (UPER etc.).
|
||||
- Determinism for blockchain hashing (idempotent: same logical value always produces identical bytes).
|
||||
- (Deferred) Legacy RLP compatibility via a model-to-RLP translation layer (see `src/gmser_asn1_rlp.erl`).
|
||||
|
||||
The single source of truth is the ASN.1 schema in `GajumaruSerialization.asn`.
|
||||
|
||||
---
|
||||
|
||||
## 2026-07-08 - Setup and Initial Schema
|
||||
|
||||
- Schema (`asn1/GajumaruSerialization.asn`) models:
|
||||
- `GajumaruData` as top-level (tag + vsn + content).
|
||||
- `Content` CHOICE with `templateFields` (generic) and concrete types (e.g. `SignedTx`, `ContractV*`).
|
||||
- `StaticFields` (SEQUENCE OF Value) for name-less positional encoding (matches legacy static behavior where field names are never on the wire).
|
||||
- `Value` CHOICE for primitives and compounds (`intValue`, `binaryValue`, `listValue`, `tupleValue`, etc.).
|
||||
- Supports all static template types: `int`, `bool`, `binary`, `id`, `[T]`, tuples, `#{items => [...]}`.
|
||||
|
||||
- Initial schema comments were DER-oriented (migration path). Updated to emphasize compact UPER + portability.
|
||||
|
||||
- Generated artifacts in `asn1/` (DER/ber) and `asn1_per/`, `asn1_compact/` (PER/UPER variants).
|
||||
|
||||
- Key files:
|
||||
- `asn1/GajumaruSerialization.asn` (source)
|
||||
- `src/gmser_asn1_rlp.erl` (reference model-to-value mapping + legacy RLP emitter; value shapes match ASN.1)
|
||||
- Tests in `test/gmser_chain_objects_tests.erl` and inside `gmser_asn1_rlp` for equivalence.
|
||||
|
||||
## 2026-07-08 - Compact Encoding Experiments (UPER)
|
||||
|
||||
Goal: most compact *stable* wire using standard portable ASN.1 (not custom non-portable rules, not RLP).
|
||||
|
||||
- Tried standard DER → too verbose (tiny case: ~36 bytes vs legacy RLP 5 bytes).
|
||||
- Switched to **UPER (Unaligned PER)** — the most compact *standard* ASN.1 encoding rule with good canonical/deterministic properties.
|
||||
- Compiled via `asn1ct:compile(..., [uper])`.
|
||||
- Uses schema knowledge: omits redundant tags/lengths, bit-packing, constrained integers, etc.
|
||||
- Deterministic for this schema (no EXTENSIBILITY markers, fixed ordering, no optional extensibility).
|
||||
|
||||
- Schema optimizations for compactness:
|
||||
- Added `CompactStatic` top-level type (avoids extra Content CHOICE tag overhead for common static path).
|
||||
- `staticFields` (pure `SEQUENCE OF Value`) — no IA5String names on wire.
|
||||
- Constrained `tag`/`vsn` (INTEGER (0..65535), (0..255)) for better packing.
|
||||
- Prefer concrete SEQUENCES (e.g. `SignedTx`) or `staticFields` over generic `templateFields` (names add cost).
|
||||
- Kept `TemplateFields` for debug/transition only.
|
||||
|
||||
- Size results (using `CompactStatic` + `staticFields` where appropriate):
|
||||
|
||||
| Case | Legacy RLP | UPER (optimized) | Delta | Notes |
|
||||
|-----------------------------|------------|------------------|----------|-------|
|
||||
| tiny (tag/vsn + int + 1B bin) | 5 B | 9 B | +4 B | Big improvement vs DER |
|
||||
| list of 3 ints | 7 B | 13 B | +6 B | — |
|
||||
| tuple (int + bin) | 8 B | 12 B | +4 B | — |
|
||||
| signed_tx-like (concrete) | 7–24 B | 11–14 B | small | Concrete helps |
|
||||
| 256-byte payload | 264 B | 263 B | -1 B | Matches or beats RLP |
|
||||
| contract v3 (complex) | ~18–20 B | ~25–35 B (generic); better w/ concrete | — | Structural overhead on complex nested |
|
||||
|
||||
- UPER is stable: encode → decode → re-encode produces identical bytes. Roundtrips work.
|
||||
|
||||
- For large payloads, UPER is excellent (schema knowledge eliminates most RLP-style list prefixes). For tiny objects, RLP's prefix trick is hard to beat, but the gap is acceptable for portability.
|
||||
|
||||
- OER (Octet Encoding Rules) also compiled but was larger (18 B on tiny case).
|
||||
|
||||
## 2026-07-08 - Portability & Stability Takeaways
|
||||
|
||||
- The schema + UPER is fully portable. Other languages can:
|
||||
1. Compile the `.asn` with their ASN.1 tool.
|
||||
2. Build a value matching `CompactStatic` / `staticFields` / concrete types.
|
||||
3. Call their UPER encoder → identical compact bytes.
|
||||
|
||||
- No Erlang-specific runtime required for the new wire format.
|
||||
- Determinism comes from:
|
||||
- UPER canonical packing rules.
|
||||
- Constrained types in schema.
|
||||
- Explicit staticFields (no map iteration, names omitted).
|
||||
- Same rules as legacy for ints (minimal), ordering, etc.
|
||||
|
||||
- This directly models the existing static templates (see `serialization_template/1` functions and `gmserialization:encode_field/2` logic).
|
||||
- Concrete types in schema give best compactness for known objects.
|
||||
- Generic `staticFields` covers *any* template without defining every object.
|
||||
|
||||
## Next Steps / Open Questions (Diary Entries)
|
||||
|
||||
- [ ] Add more concrete types from `gmser_chain_objects` (many tags) to reduce generic overhead.
|
||||
- [ ] Add more ASN.1 constraints (SIZE, value ranges) to help PER pack tighter.
|
||||
- [ ] Measure on real on-chain objects (key_block, etc.).
|
||||
- [ ] Decide on top-level header for the new format (keep tag/vsn?).
|
||||
- [ ] (Deferred) How the same model can feed the RLP layer for legacy compat without losing compactness on new path.
|
||||
- [ ] Consider if a custom "ASN.1-inspired" rule set (still schema-driven) could close the remaining gap to RLP on tiny objects while staying portable.
|
||||
|
||||
## 2026-07-08 - Schema Updated for Bignums
|
||||
|
||||
- Updated `GajumaruSerialization.asn`:
|
||||
- Introduced `BigInt ::= INTEGER (0..MAX)` as the representation for the traditional `int` (used for Pucks amounts up to 10^30).
|
||||
- `Value` CHOICE now uses `bigIntValue` for the bignum case.
|
||||
- Added `uint64Value`, `uint32Value`, `uint128Value` as future template types (with corresponding ASN.1 subtypes).
|
||||
- Updated header comments to document the bignum nature of `int`.
|
||||
|
||||
- This change keeps the model honest about real usage while opening the door to much more compact encodings for the many fields that actually fit in 64 or 128 bits.
|
||||
|
||||
- Next: We should extend the Erlang-side `type()` in `gmserialization.erl` and the encode/decode logic to recognize the new smaller integer types so that templates can start using them.
|
||||
|
||||
---
|
||||
|
||||
*This file should be kept as a living diary. Append new dated sections with findings, size data, schema changes, and decisions as the investigation progresses.*
|
||||
|
||||
## 2026-07-08 - Handling of `int` as Bignums (Pucks, amounts, etc.)
|
||||
|
||||
Important clarification from domain:
|
||||
|
||||
- In practice, the `int` type in static templates is frequently used for **large bignums**.
|
||||
- Example: Amount fields (balances, transaction amounts, etc.) are denominated in "Pucks".
|
||||
- Maximum value mentioned: 1 × 10^30.
|
||||
- This is ~ 2^99.66, i.e., requires up to ~13 bytes in minimal unsigned encoding.
|
||||
|
||||
Current legacy handling (in `gmserialization.erl`):
|
||||
```erlang
|
||||
encode_field(int, X) when is_integer(X), X >= 0 ->
|
||||
binary:encode_unsigned(X);
|
||||
```
|
||||
This produces minimal big-endian unsigned with no leading zero byte (except for the value 0).
|
||||
|
||||
Implications for ASN.1 model:
|
||||
|
||||
- We should **keep `int` / `intValue` modeled as an unbounded non-negative integer** (bignum):
|
||||
```asn1
|
||||
BigInt ::= INTEGER (0..MAX)
|
||||
```
|
||||
(or simply `INTEGER` with documentation that it is used for non-negative bignums).
|
||||
|
||||
- Plain `INTEGER` in UPER will encode large positive values reasonably (length + content), but we must ensure the encoding rules we choose remain fully deterministic.
|
||||
|
||||
- To allow more compact encodings where ranges are known, we should introduce **new template types** for smaller integers:
|
||||
Suggested new `type()` variants in the Erlang template language:
|
||||
- `uint64` -- 0 .. 2^64-1
|
||||
- `uint32` -- 0 .. 2^32-1
|
||||
- `uint16`, `uint8`, `uint128` etc. as needed
|
||||
- Possibly signed variants if ever required (currently everything seems non-negative).
|
||||
|
||||
Corresponding in ASN.1 (inside Value CHOICE or as reusable types):
|
||||
```asn1
|
||||
Uint64 ::= INTEGER (0..18446744073709551615)
|
||||
Uint32 ::= INTEGER (0..4294967295)
|
||||
Uint128 ::= INTEGER (0..340282366920938463463374607431768211455)
|
||||
```
|
||||
|
||||
- In the schema's Value CHOICE we can evolve to:
|
||||
```asn1
|
||||
Value ::= CHOICE {
|
||||
bigIntValue [0] BigInt, -- the classic "int" for Pucks etc.
|
||||
uint64Value [7] Uint64,
|
||||
uint32Value [8] Uint32,
|
||||
...
|
||||
-- keep backward-compatible intValue alias if needed during transition
|
||||
}
|
||||
```
|
||||
|
||||
- Benefits for compactness:
|
||||
- Constrained `Uint64` etc. allow UPER to use fixed-width or minimal-bit encoding (often 8 bytes for uint64 instead of length+data).
|
||||
- Still fully portable and deterministic.
|
||||
|
||||
- Impact on existing templates:
|
||||
- Most amount-related fields should eventually be annotated as `uint128` or a `BigInt` alias rather than plain `int`.
|
||||
- Small counters, versions, indices can use `uint32` / `uint64`.
|
||||
- This may require extending the `type()` in `gmserialization.erl` and the encoders.
|
||||
|
||||
## 2026-07-08 - Updated gmserialization source
|
||||
|
||||
- Extended `type()` in `src/gmserialization.erl` with `uint128`, `uint64`, `uint32`, `uint16`, `uint8` (keeping `int` for bignums).
|
||||
- Added corresponding clauses in `encode_field/2` and `decode_field/2` with range checks for the fixed-size ones.
|
||||
- Updated `src/gmser_asn1_rlp.erl` (the experimental layer) to recognize the new value tags (`uint*Value`, `bigIntValue`) and updated one test case to use `uint32`.
|
||||
- Updated `doc/static.md` example types.
|
||||
- `int` remains fully backward compatible for bignum use.
|
||||
- All existing tests + new type usage pass.
|
||||
|
||||
- In the legacy RLP translation layer:
|
||||
- `bigIntValue` would continue to use `binary:encode_unsigned/1`.
|
||||
- Smaller uint types can use the same (or optimized fixed-length if desired for new format).
|
||||
|
||||
Next action items:
|
||||
- Update `GajumaruSerialization.asn` to introduce `BigInt`, `Uint*` types and adjust Value.
|
||||
- Decide on naming in the Erlang template DSL (`int` remains bignum alias? or rename?).
|
||||
- Add example in the schema for a balance field.
|
||||
- Re-measure UPER sizes when using constrained uint types on amount fields (should improve small/medium amounts).
|
||||
|
||||
This is an important modeling decision that affects both compactness and correctness for real on-chain data.
|
||||
|
||||
## 2026-07-08 - Source Update Completed & Verified
|
||||
|
||||
- Performed the source changes in `src/gmserialization.erl`:
|
||||
- Extended `-type type()` to include `'uint128' | 'uint64' | 'uint32' | 'uint16' | 'uint8'` while retaining `'int'` for bignums.
|
||||
- Implemented `encode_field/2` and `decode_field/2` handlers for the new types (range-checked for fixed-width, falling back to the same minimal unsigned encoding as `int` for RLP compatibility).
|
||||
- Synced `src/gmser_asn1_rlp.erl`:
|
||||
- Added support for new ASN.1 value variants (`{uint*Value, ...}`, `{bigIntValue, ...}`) in `encode_asn1_value/1`.
|
||||
- Updated test data and comments to use the new types.
|
||||
- Updated `doc/static.md` to reflect the extended type language.
|
||||
- Verified:
|
||||
- Legacy templates using `int` continue to work unchanged.
|
||||
- New types (e.g. `{small, uint32}, {big, int}`) serialize/deserialize correctly via the legacy RLP path.
|
||||
- All 7 equivalence tests in `gmser_asn1_rlp` still pass.
|
||||
- `gmser_chain_objects_tests` (12 tests) still pass.
|
||||
- The ASN.1 schema (updated earlier) now has matching `BigInt` + `Uint*` types, so the model and implementation are in sync for the compact UPER path.
|
||||
|
||||
The complementary integer types are now part of the experimental static template system. This enables the ASN.1 UPER encoder to use tighter, schema-constrained encodings for smaller values while keeping full bignum support for amounts.
|
||||
|
||||
Next diary items (still open):
|
||||
- Extend more concrete types in the schema.
|
||||
- Add actual UPER-based encode path in the library (beyond the RLP layer).
|
||||
- Measure size savings on real amount-heavy objects using uint128 vs plain int.
|
||||
@@ -0,0 +1,267 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data for portability
|
||||
-- (other languages use ASN.1 compilers to get types/parsers).
|
||||
-- * Define compact canonical wire format using standard ASN.1 techniques
|
||||
-- (primarily Unaligned PER / UPER with constraints for packing).
|
||||
-- * Static encoding only (templates from gmserialization).
|
||||
-- * Legacy RLP compatibility is achieved via separate translation layer
|
||||
-- (deferred in current focus).
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct for compact wire):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [uper]).
|
||||
-- {ok, Bytes} = 'GajumaruSerialization':encode('GajumaruData', Asn1Value).
|
||||
-- Bytes is the compact UPER wire format (deterministic with this schema).
|
||||
-- For legacy RLP, use the model-to-RLP layer instead (see gmser_asn1_rlp).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl (static templates):
|
||||
-- - int (bignum) -> BigInt ::= INTEGER (0..MAX)
|
||||
-- In practice used for amounts/balances in Pucks (up to 1*10^30).
|
||||
-- Encoded as non-negative bignum.
|
||||
-- - New smaller integer types will be added to templates (uint64, uint32, ...)
|
||||
-- for cases where range is known → enables tighter UPER packing.
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
-- Constrained for better PER packing
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Preferred top-level for the compact static wire format.
|
||||
-- Avoids the extra Content CHOICE tag when the structure is known to be static.
|
||||
CompactStatic ::= SEQUENCE {
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
fields StaticFields
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Static-optimized: no field names on wire (matches legacy static behavior exactly)
|
||||
-- Preferred for compact wire format of known templates.
|
||||
staticFields [10] StaticFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
-- Optimized for static wire format: just the values in order, no names.
|
||||
-- This matches the legacy static encoding where maps/records are positional only.
|
||||
StaticFields ::= SEQUENCE OF Value
|
||||
|
||||
|
||||
Value ::= CHOICE {
|
||||
-- "int" in static templates is used for bignums in practice
|
||||
-- (e.g. balances and amounts in Pucks, up to 1*10^30).
|
||||
-- We keep it as unbounded non-negative integer.
|
||||
bigIntValue [0] BigInt,
|
||||
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue, -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
|
||||
-- Additional integer types for smaller ranges (future use in templates
|
||||
-- for better UPER packing when the range is known).
|
||||
uint64Value [7] Uint64,
|
||||
uint32Value [8] Uint32,
|
||||
uint128Value [9] Uint128
|
||||
}
|
||||
|
||||
-- Bignum integer (non-negative). Used for the traditional "int" in static
|
||||
-- templates. Max practical value mentioned: 1*10^30 (≈ 2^100 bits).
|
||||
BigInt ::= INTEGER (0..MAX)
|
||||
|
||||
-- Convenience sized unsigned integer types.
|
||||
Uint64 ::= INTEGER (0..18446744073709551615)
|
||||
Uint32 ::= INTEGER (0..4294967295)
|
||||
Uint128 ::= INTEGER (0..340282366920938463463374607431768211455)
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. For the compact wire format we use UPER (unaligned PER).
|
||||
-- It is stable/deterministic for a given schema (no extensibility
|
||||
-- markers on these types, fixed order).
|
||||
--
|
||||
-- 4. INTEGER uses ASN.1 PER encoding (canonical for the constraints).
|
||||
-- This may differ from legacy RLP minimal unsigned; new format
|
||||
-- will have different hashes (expected when introducing new encoding).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is not in scope here.
|
||||
--
|
||||
-- 6. To generate the compact wire:
|
||||
-- asn1ct:compile(GajumaruSerialization, [uper]).
|
||||
-- {ok, CompactBytes} = 'GajumaruSerialization':encode('GajumaruData', Value).
|
||||
--
|
||||
-- Use staticFields (not templateFields) for best compactness on static data.
|
||||
|
||||
END
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,27 @@
|
||||
-module(detect_demo).
|
||||
-export([run/0]).
|
||||
|
||||
run() ->
|
||||
application:ensure_all_started(gmserialization),
|
||||
code:add_path("asn1"),
|
||||
|
||||
Sample = {'GajumaruData', 10, 1,
|
||||
{templateFields, [
|
||||
{'TemplateField', <<"foo">>, {intValue, 42}},
|
||||
{'TemplateField', <<"bar">>, {binaryValue, <<"hello">>}}
|
||||
]}},
|
||||
|
||||
{ok, Der} = 'GajumaruSerialization':encode('GajumaruData', Sample),
|
||||
io:format("DER first byte: ~p (0x~2.16.0B)~n", [binary:at(Der,0), binary:at(Der,0)]),
|
||||
|
||||
{ok, _Dec} = 'GajumaruSerialization':decode('GajumaruData', Der),
|
||||
io:format("DER roundtrip OK~n"),
|
||||
|
||||
Legacy = gmser_chain_objects:serialize(account, 1,
|
||||
[{foo,int},{bar,binary}],
|
||||
[{foo,42},{bar,<<"hello">>}]),
|
||||
<<L0>> = binary:part(Legacy,0,1),
|
||||
<<D0>> = binary:part(Der,0,1),
|
||||
io:format("Legacy 0x~2.16.0B (>= 0xC0 -> legacy: ~p)~n", [L0, L0 >= 16#C0]),
|
||||
io:format("DER 0x~2.16.0B (< 0xC0 -> new DER: ~p)~n", [D0, D0 < 16#C0]),
|
||||
ok.
|
||||
@@ -0,0 +1,114 @@
|
||||
-module(size_comparison).
|
||||
-export([run/0]).
|
||||
|
||||
run() ->
|
||||
application:ensure_all_started(gmserialization),
|
||||
code:add_path("asn1"),
|
||||
|
||||
io:format("=== Size Comparison: Legacy RLP vs DER ===~n~n"),
|
||||
|
||||
Compare = fun(Desc, LegacyBin, DerBin) ->
|
||||
L = byte_size(LegacyBin),
|
||||
D = byte_size(DerBin),
|
||||
Overhead = D - L,
|
||||
Pct = case L of 0 -> 0; _ -> round(Overhead * 100 / L) end,
|
||||
io:format("~s~n", [Desc]),
|
||||
io:format(" Legacy: ~p bytes ~w~n", [L, LegacyBin]),
|
||||
Show = binary:part(DerBin, 0, min(18, D)),
|
||||
io:format(" DER: ~p bytes ~w~n", [D, Show]),
|
||||
io:format(" Overhead: +~p bytes (~p%)~n~n", [Overhead, Pct])
|
||||
end,
|
||||
|
||||
%% Case 1: tag+vsn + small int + small binary
|
||||
T1 = [{foo,int},{bar,binary}],
|
||||
V1 = [{foo,1},{bar,<<2>>}],
|
||||
Leg1 = gmser_chain_objects:serialize(account, 1, T1, V1),
|
||||
DerVal1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"foo">>, {intValue, 1}},
|
||||
{'TemplateField', <<"bar">>, {binaryValue, <<2>>}}
|
||||
]}},
|
||||
{ok, Der1} = 'GajumaruSerialization':encode('GajumaruData', DerVal1),
|
||||
Compare("Case 1: tag+vsn + small int + tiny binary (2 fields)", Leg1, Der1),
|
||||
|
||||
%% Case 2: zero + empty binary
|
||||
V2 = [{foo,0},{bar,<<>>}],
|
||||
Leg2 = gmser_chain_objects:serialize(account, 1, T1, V2),
|
||||
DerVal2 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"foo">>, {intValue, 0}},
|
||||
{'TemplateField', <<"bar">>, {binaryValue, <<>>}}
|
||||
]}},
|
||||
{ok, Der2} = 'GajumaruSerialization':encode('GajumaruData', DerVal2),
|
||||
Compare("Case 2: zero int + empty binary", Leg2, Der2),
|
||||
|
||||
%% Case 3: list of ints
|
||||
T3 = [{xs,[int]}],
|
||||
V3 = [{xs,[1,2,3]}],
|
||||
Leg3 = gmser_chain_objects:serialize(account, 1, T3, V3),
|
||||
DerVal3 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"xs">>, {listValue, [
|
||||
{intValue,1},{intValue,2},{intValue,3}
|
||||
]}}
|
||||
]}},
|
||||
{ok, Der3} = 'GajumaruSerialization':encode('GajumaruData', DerVal3),
|
||||
Compare("Case 3: list of 3 small ints", Leg3, Der3),
|
||||
|
||||
%% Case 4: tuple (int, binary)
|
||||
T4 = [{p,{int,binary}}],
|
||||
V4 = [{p,{42,<<"hi">>}}],
|
||||
Leg4 = gmser_chain_objects:serialize(account, 1, T4, V4),
|
||||
DerVal4 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"p">>, {tupleValue, [
|
||||
{intValue,42}, {binaryValue,<<"hi">>}
|
||||
]}}
|
||||
]}},
|
||||
{ok, Der4} = 'GajumaruSerialization':encode('GajumaruData', DerVal4),
|
||||
Compare("Case 4: fixed tuple (int + 2-byte binary)", Leg4, Der4),
|
||||
|
||||
%% Case 5: 256-byte payload
|
||||
Bin5 = crypto:strong_rand_bytes(256),
|
||||
T5 = [{data,binary}],
|
||||
V5 = [{data,Bin5}],
|
||||
Leg5 = gmser_chain_objects:serialize(account, 1, T5, V5),
|
||||
DerVal5 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"data">>, {binaryValue, Bin5}}
|
||||
]}},
|
||||
{ok, Der5} = 'GajumaruSerialization':encode('GajumaruData', DerVal5),
|
||||
Compare("Case 5: 256-byte binary payload only", Leg5, Der5),
|
||||
|
||||
%% Case 6: Concrete SignedTx style (no field names in DER)
|
||||
T6 = [{signatures,[binary]},{tx,binary}],
|
||||
V6 = [{signatures,[<<"sig1">>,<<"sig2">>]},{tx,<<"txbody123">>}],
|
||||
Leg6 = gmser_chain_objects:serialize(signed_tx, 1, T6, V6),
|
||||
DerVal6 = {'GajumaruData', 11, 1, {signedTx, {'SignedTx',
|
||||
[<<"sig1">>, <<"sig2">>], <<"txbody123">>}}},
|
||||
{ok, Der6} = 'GajumaruSerialization':encode('GajumaruData', DerVal6),
|
||||
Compare("Case 6: SignedTx-like (concrete DER, no names)", Leg6, Der6),
|
||||
|
||||
%% Case 7: 33-byte id-like value
|
||||
IdBin = <<1, 0:256>>,
|
||||
T7 = [{owner,binary}],
|
||||
V7 = [{owner,IdBin}],
|
||||
Leg7 = gmser_chain_objects:serialize(account, 1, T7, V7),
|
||||
DerVal7 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"owner">>, {binaryValue, IdBin}}
|
||||
]}},
|
||||
{ok, Der7} = 'GajumaruSerialization':encode('GajumaruData', DerVal7),
|
||||
Compare("Case 7: 33-byte value (id-like)", Leg7, Der7),
|
||||
|
||||
%% Case 8: Deeper nesting / more fields
|
||||
T8 = [{a,int},{b,binary},{c,[int]},{d,{int,int}}],
|
||||
V8 = [{a,123456},{b,<<"abcdef">>},{c,[10,20,30]},{d,{7,8}}],
|
||||
Leg8 = gmser_chain_objects:serialize(account, 1, T8, V8),
|
||||
DerVal8 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"a">>, {intValue, 123456}},
|
||||
{'TemplateField', <<"b">>, {binaryValue, <<"abcdef">>}},
|
||||
{'TemplateField', <<"c">>, {listValue, [{intValue,10},{intValue,20},{intValue,30}]}},
|
||||
{'TemplateField', <<"d">>, {tupleValue, [{intValue,7},{intValue,8}]}}
|
||||
]}},
|
||||
{ok, Der8} = 'GajumaruSerialization':encode('GajumaruData', DerVal8),
|
||||
Compare("Case 8: 4 fields mixed (int, bin, list, tuple)", Leg8, Der8),
|
||||
|
||||
io:format("=== Analysis ===~n"),
|
||||
io:format("Note: Generic templateFields path includes IA5String field names.~n"),
|
||||
io:format("Concrete types (e.g. SignedTx) avoid name overhead.~n"),
|
||||
ok.
|
||||
@@ -0,0 +1,267 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data for portability
|
||||
-- (other languages use ASN.1 compilers to get types/parsers).
|
||||
-- * Define compact canonical wire format using standard ASN.1 techniques
|
||||
-- (primarily Unaligned PER / UPER with constraints for packing).
|
||||
-- * Static encoding only (templates from gmserialization).
|
||||
-- * Legacy RLP compatibility is achieved via separate translation layer
|
||||
-- (deferred in current focus).
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct for compact wire):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [uper]).
|
||||
-- {ok, Bytes} = 'GajumaruSerialization':encode('GajumaruData', Asn1Value).
|
||||
-- Bytes is the compact UPER wire format (deterministic with this schema).
|
||||
-- For legacy RLP, use the model-to-RLP layer instead (see gmser_asn1_rlp).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl (static templates):
|
||||
-- - int (bignum) -> BigInt ::= INTEGER (0..MAX)
|
||||
-- In practice used for amounts/balances in Pucks (up to 1*10^30).
|
||||
-- Encoded as non-negative bignum.
|
||||
-- - New smaller integer types will be added to templates (uint64, uint32, ...)
|
||||
-- for cases where range is known → enables tighter UPER packing.
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
-- Constrained for better PER packing
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Preferred top-level for the compact static wire format.
|
||||
-- Avoids the extra Content CHOICE tag when the structure is known to be static.
|
||||
CompactStatic ::= SEQUENCE {
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
fields StaticFields
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Static-optimized: no field names on wire (matches legacy static behavior exactly)
|
||||
-- Preferred for compact wire format of known templates.
|
||||
staticFields [10] StaticFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
-- Optimized for static wire format: just the values in order, no names.
|
||||
-- This matches the legacy static encoding where maps/records are positional only.
|
||||
StaticFields ::= SEQUENCE OF Value
|
||||
|
||||
|
||||
Value ::= CHOICE {
|
||||
-- "int" in static templates is used for bignums in practice
|
||||
-- (e.g. balances and amounts in Pucks, up to 1*10^30).
|
||||
-- We keep it as unbounded non-negative integer.
|
||||
bigIntValue [0] BigInt,
|
||||
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue, -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
|
||||
-- Additional integer types for smaller ranges (future use in templates
|
||||
-- for better UPER packing when the range is known).
|
||||
uint64Value [7] Uint64,
|
||||
uint32Value [8] Uint32,
|
||||
uint128Value [9] Uint128
|
||||
}
|
||||
|
||||
-- Bignum integer (non-negative). Used for the traditional "int" in static
|
||||
-- templates. Max practical value mentioned: 1*10^30 (≈ 2^100 bits).
|
||||
BigInt ::= INTEGER (0..MAX)
|
||||
|
||||
-- Convenience sized unsigned integer types.
|
||||
Uint64 ::= INTEGER (0..18446744073709551615)
|
||||
Uint32 ::= INTEGER (0..4294967295)
|
||||
Uint128 ::= INTEGER (0..340282366920938463463374607431768211455)
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. For the compact wire format we use UPER (unaligned PER).
|
||||
-- It is stable/deterministic for a given schema (no extensibility
|
||||
-- markers on these types, fixed order).
|
||||
--
|
||||
-- 4. INTEGER uses ASN.1 PER encoding (canonical for the constraints).
|
||||
-- This may differ from legacy RLP minimal unsigned; new format
|
||||
-- will have different hashes (expected when introducing new encoding).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is not in scope here.
|
||||
--
|
||||
-- 6. To generate the compact wire:
|
||||
-- asn1ct:compile(GajumaruSerialization, [uper]).
|
||||
-- {ok, CompactBytes} = 'GajumaruSerialization':encode('GajumaruData', Value).
|
||||
--
|
||||
-- Use staticFields (not templateFields) for best compactness on static data.
|
||||
|
||||
END
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('CompactStatic', {
|
||||
tag,
|
||||
vsn,
|
||||
fields
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,247 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data for portability
|
||||
-- (other languages use ASN.1 compilers to get types/parsers).
|
||||
-- * Define compact canonical wire format using standard ASN.1 techniques
|
||||
-- (primarily Unaligned PER / UPER with constraints for packing).
|
||||
-- * Static encoding only (templates from gmserialization).
|
||||
-- * Legacy RLP compatibility is achieved via separate translation layer
|
||||
-- (deferred in current focus).
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct for compact wire):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [uper]).
|
||||
-- {ok, Bytes} = 'GajumaruSerialization':encode('GajumaruData', Asn1Value).
|
||||
-- Bytes is the compact UPER wire format (deterministic with this schema).
|
||||
-- For legacy RLP, use the model-to-RLP layer instead (see gmser_asn1_rlp).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl:
|
||||
-- - int -> INTEGER (new data uses canonical DER INTEGER)
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE) (cleaner than the legacy packed 33-byte form)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- Legacy integers had strict unsigned-minimal no-leading-zero (except for 0)
|
||||
-- and RLP-level rules. New DER data does not need to follow those rules.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
-- Constrained for better PER packing
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Preferred top-level for the compact static wire format.
|
||||
-- Avoids the extra Content CHOICE tag when the structure is known to be static.
|
||||
CompactStatic ::= SEQUENCE {
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
fields StaticFields
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Static-optimized: no field names on wire (matches legacy static behavior exactly)
|
||||
-- Preferred for compact wire format of known templates.
|
||||
staticFields [10] StaticFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
-- Optimized for static wire format: just the values in order, no names.
|
||||
-- This matches the legacy static encoding where maps/records are positional only.
|
||||
StaticFields ::= SEQUENCE OF Value
|
||||
|
||||
|
||||
Value ::= CHOICE {
|
||||
intValue [0] INTEGER,
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
}
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. For the compact wire format we use UPER (unaligned PER).
|
||||
-- It is stable/deterministic for a given schema (no extensibility
|
||||
-- markers on these types, fixed order).
|
||||
--
|
||||
-- 4. INTEGER uses ASN.1 PER encoding (canonical for the constraints).
|
||||
-- This may differ from legacy RLP minimal unsigned; new format
|
||||
-- will have different hashes (expected when introducing new encoding).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is not in scope here.
|
||||
--
|
||||
-- 6. To generate the compact wire:
|
||||
-- asn1ct:compile(GajumaruSerialization, [uper]).
|
||||
-- {ok, CompactBytes} = 'GajumaruSerialization':encode('GajumaruData', Value).
|
||||
--
|
||||
-- Use staticFields (not templateFields) for best compactness on static data.
|
||||
|
||||
END
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,82 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('CompactStatic', {
|
||||
tag,
|
||||
vsn,
|
||||
fields
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,235 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data.
|
||||
-- * Enable migration from the legacy RLP-based format to DER.
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [ber, der]).
|
||||
-- {ok, Term} = 'GajumaruSerialization':decode('GajumaruData', DerBinary).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl:
|
||||
-- - int -> INTEGER (new data uses canonical DER INTEGER)
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE) (cleaner than the legacy packed 33-byte form)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- Legacy integers had strict unsigned-minimal no-leading-zero (except for 0)
|
||||
-- and RLP-level rules. New DER data does not need to follow those rules.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
-- Constrained for better PER packing
|
||||
tag INTEGER (0..65535),
|
||||
vsn INTEGER (0..255),
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Static-optimized: no field names on wire (matches legacy static behavior exactly)
|
||||
-- Preferred for compact wire format of known templates.
|
||||
staticFields [10] StaticFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
-- Optimized for static wire format: just the values in order, no names.
|
||||
-- This matches the legacy static encoding where maps/records are positional only.
|
||||
StaticFields ::= SEQUENCE OF Value
|
||||
|
||||
|
||||
Value ::= CHOICE {
|
||||
intValue [0] INTEGER,
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
}
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. INTEGER in DER is signed and uses a different minimal encoding
|
||||
-- than the legacy unsigned big-endian no leading zero form. This is
|
||||
-- fine for new data.
|
||||
--
|
||||
-- 4. If you need to preserve exact legacy integer wire bytes inside
|
||||
-- the new format (e.g. for some hash preimage reason), you can
|
||||
-- carry selected integers as OCTET STRING (legacy unsigned minimal bytes).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is intentionally not modeled here
|
||||
-- in full, because it is runtime-schema driven (type codes 246-255,
|
||||
-- labels, alt/switch, etc.). You can still use the generic
|
||||
-- TemplateFields + Value for some dynamic cases, or model specific
|
||||
-- message schemas as additional CHOICE arms.
|
||||
--
|
||||
-- 6. Compile with DER for canonical output:
|
||||
-- asn1ct:compile(GajumaruSerialization, [der]).
|
||||
--
|
||||
-- The ber option is also accepted; der implies the stricter rules.
|
||||
|
||||
END
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,225 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data.
|
||||
-- * Enable migration from the legacy RLP-based format to DER.
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [ber, der]).
|
||||
-- {ok, Term} = 'GajumaruSerialization':decode('GajumaruData', DerBinary).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl:
|
||||
-- - int -> INTEGER (new data uses canonical DER INTEGER)
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE) (cleaner than the legacy packed 33-byte form)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- Legacy integers had strict unsigned-minimal no-leading-zero (except for 0)
|
||||
-- and RLP-level rules. New DER data does not need to follow those rules.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
tag INTEGER,
|
||||
vsn INTEGER,
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
Value ::= CHOICE {
|
||||
intValue [0] INTEGER,
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
}
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. INTEGER in DER is signed and uses a different minimal encoding
|
||||
-- than the legacy unsigned big-endian no leading zero form. This is
|
||||
-- fine for new data.
|
||||
--
|
||||
-- 4. If you need to preserve exact legacy integer wire bytes inside
|
||||
-- the new format (e.g. for some hash preimage reason), you can
|
||||
-- carry selected integers as OCTET STRING (legacy unsigned minimal bytes).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is intentionally not modeled here
|
||||
-- in full, because it is runtime-schema driven (type codes 246-255,
|
||||
-- labels, alt/switch, etc.). You can still use the generic
|
||||
-- TemplateFields + Value for some dynamic cases, or model specific
|
||||
-- message schemas as additional CHOICE arms.
|
||||
--
|
||||
-- 6. Compile with DER for canonical output:
|
||||
-- asn1ct:compile(GajumaruSerialization, [der]).
|
||||
--
|
||||
-- The ber option is also accepted; der implies the stricter rules.
|
||||
|
||||
END
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,225 @@
|
||||
-- GajumaruSerialization.asn
|
||||
--
|
||||
-- ASN.1 model for the structures serialized by gmserialization.erl
|
||||
-- (the layer on top of RLP).
|
||||
--
|
||||
-- Purpose:
|
||||
-- * Provide a formal, toolable description of the data.
|
||||
-- * Enable migration from the legacy RLP-based format to DER.
|
||||
--
|
||||
-- Detection of legacy vs new:
|
||||
-- Legacy data (produced by gmserialization:serialize / gmser_chain_objects:serialize
|
||||
-- or gmser_dyn:serialize) is always an RLP *list* at the top level.
|
||||
-- This means the first byte is in the range 0xC0 .. 0xFF.
|
||||
--
|
||||
-- DER-encoded data using the types below will start with 0x30 (SEQUENCE,
|
||||
-- constructed, universal tag) for the outermost GajumaruData (or any
|
||||
-- top-level SEQUENCE we define). 0x30 < 0xC0, so a single-byte prefix
|
||||
-- check reliably distinguishes the two formats.
|
||||
--
|
||||
-- Usage in Erlang (after compiling with asn1ct):
|
||||
-- {ok, Mod} = asn1ct:compile(GajumaruSerialization.asn, [ber, der]).
|
||||
-- {ok, Term} = 'GajumaruSerialization':decode('GajumaruData', DerBinary).
|
||||
--
|
||||
-- Notes on type mapping from gmserialization.erl:
|
||||
-- - int -> INTEGER (new data uses canonical DER INTEGER)
|
||||
-- - binary -> OCTET STRING
|
||||
-- - bool -> BOOLEAN
|
||||
-- - id -> Id (SEQUENCE) (cleaner than the legacy packed 33-byte form)
|
||||
-- - [T] -> SEQUENCE OF T
|
||||
-- - {T1,...} -> SEQUENCE (fixed-size, order matters)
|
||||
-- - #{items := [{K,T},...]} -> SEQUENCE with the fields in template order
|
||||
-- (static maps carry *values only*, no keys on the wire)
|
||||
-- - tag + vsn are preserved as the first two fields for compatibility
|
||||
-- with existing dispatch logic.
|
||||
--
|
||||
-- Legacy integers had strict unsigned-minimal no-leading-zero (except for 0)
|
||||
-- and RLP-level rules. New DER data does not need to follow those rules.
|
||||
--
|
||||
-- WARNING:
|
||||
-- Any cryptographic hash or signature computed over a serialized object
|
||||
-- will change when switching from RLP to DER for that object. Plan a
|
||||
-- coordinated upgrade.
|
||||
|
||||
GajumaruSerialization DEFINITIONS
|
||||
AUTOMATIC TAGS ::=
|
||||
BEGIN
|
||||
|
||||
EXPORTS ALL;
|
||||
|
||||
-- ============================================================
|
||||
-- Top-level wrapper used for new DER data.
|
||||
-- This is what a decoder will see first.
|
||||
-- ============================================================
|
||||
|
||||
GajumaruData ::= SEQUENCE {
|
||||
tag INTEGER,
|
||||
vsn INTEGER,
|
||||
content Content
|
||||
}
|
||||
|
||||
-- Content can be a specific structured type (preferred) or a generic
|
||||
-- representation of a template-driven object.
|
||||
Content ::= CHOICE {
|
||||
-- Generic fallback that can represent any [{Field, Type}] template
|
||||
-- without having a pre-defined SEQUENCE for every object.
|
||||
templateFields [0] TemplateFields,
|
||||
|
||||
-- Examples of concrete versioned types (extend as needed)
|
||||
account [1] Account,
|
||||
signedTx [2] SignedTx,
|
||||
contract [3] ContractCode
|
||||
-- Add more alternatives for other tags from gmser_chain_objects
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Generic template-driven representation
|
||||
-- (useful during transition or for unregistered types)
|
||||
-- ============================================================
|
||||
|
||||
TemplateFields ::= SEQUENCE OF TemplateField
|
||||
|
||||
TemplateField ::= SEQUENCE {
|
||||
-- Field name is included for debuggability / generic processing.
|
||||
-- In the original static encoding the name is NOT on the wire;
|
||||
-- only position and type matter. We include it here for convenience.
|
||||
name IA5String OPTIONAL,
|
||||
value Value
|
||||
}
|
||||
|
||||
Value ::= CHOICE {
|
||||
intValue [0] INTEGER,
|
||||
boolValue [1] BOOLEAN,
|
||||
binaryValue [2] OCTET STRING,
|
||||
idValue [3] Id,
|
||||
listValue [4] SEQUENCE OF Value,
|
||||
tupleValue [5] SEQUENCE OF Value,
|
||||
mapValue [6] SEQUENCE OF KeyValue -- only needed if you want to
|
||||
-- represent dynamic-style maps
|
||||
}
|
||||
|
||||
KeyValue ::= SEQUENCE {
|
||||
key Value,
|
||||
val Value
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Common types
|
||||
-- ============================================================
|
||||
|
||||
Id ::= SEQUENCE {
|
||||
-- Corresponds to the simple tags in gmser_id (account=1, name=2, etc.)
|
||||
-- and the extended account subtype (high bit in legacy).
|
||||
type INTEGER (0..255),
|
||||
value OCTET STRING (SIZE (32))
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Concrete object examples (derived from usage in the codebase)
|
||||
-- Add / evolve per version as you introduce new vsns.
|
||||
-- ============================================================
|
||||
|
||||
-- Example: a very simple account-like object used in tests
|
||||
Account ::= SEQUENCE {
|
||||
foo INTEGER,
|
||||
bar OCTET STRING
|
||||
}
|
||||
|
||||
-- Simplified signed transaction
|
||||
SignedTx ::= SEQUENCE {
|
||||
signatures SEQUENCE OF OCTET STRING,
|
||||
transaction OCTET STRING
|
||||
}
|
||||
|
||||
-- Contract code objects (see gmser_contract_code.erl)
|
||||
-- We model the three versions that exist today.
|
||||
ContractCode ::= CHOICE {
|
||||
v1 [0] ContractV1,
|
||||
v2 [1] ContractV2,
|
||||
v3 [2] ContractV3
|
||||
}
|
||||
|
||||
ContractV1 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
-- typeInfo is a list of 4-tuples in legacy:
|
||||
-- {typeHash, name, argType, outType}
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING
|
||||
}
|
||||
|
||||
ContractV2 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV1,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING
|
||||
}
|
||||
|
||||
ContractV3 ::= SEQUENCE {
|
||||
sourceHash OCTET STRING,
|
||||
typeInfo SEQUENCE OF TypeInfoV3,
|
||||
byteCode OCTET STRING,
|
||||
compilerVersion OCTET STRING,
|
||||
payable BOOLEAN
|
||||
}
|
||||
|
||||
TypeInfoV1 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
TypeInfoV3 ::= SEQUENCE {
|
||||
typeHash OCTET STRING,
|
||||
name OCTET STRING,
|
||||
payable BOOLEAN,
|
||||
argType OCTET STRING,
|
||||
outType OCTET STRING
|
||||
}
|
||||
|
||||
-- ============================================================
|
||||
-- Notes for implementers
|
||||
-- ============================================================
|
||||
-- 1. Detection (recommended decoder entry point):
|
||||
--
|
||||
-- decode(Binary) ->
|
||||
-- case Binary of
|
||||
-- <<B, _/binary>> when B >= 16#C0 ->
|
||||
-- decode_legacy_rlp(Binary); % existing gmser_* path
|
||||
-- _ ->
|
||||
-- {ok, Term} =
|
||||
-- 'GajumaruSerialization':decode('GajumaruData', Binary),
|
||||
-- Term
|
||||
-- end.
|
||||
--
|
||||
-- This works because:
|
||||
-- - All current top-level output from serialize() is an RLP list
|
||||
-- (first byte 0xC0-0xFF).
|
||||
-- - GajumaruData and the concrete choices above are SEQUENCEs
|
||||
-- (first byte 0x30 for short form, or 0x30 0x81/0x82... for long).
|
||||
--
|
||||
-- 2. When you add a new object type or version, prefer adding a
|
||||
-- concrete SEQUENCE alternative in the Content CHOICE rather than
|
||||
-- always falling back to TemplateFields. This gives you better
|
||||
-- validation and generated types.
|
||||
--
|
||||
-- 3. INTEGER in DER is signed and uses a different minimal encoding
|
||||
-- than the legacy unsigned big-endian no leading zero form. This is
|
||||
-- fine for new data.
|
||||
--
|
||||
-- 4. If you need to preserve exact legacy integer wire bytes inside
|
||||
-- the new format (e.g. for some hash preimage reason), you can
|
||||
-- carry selected integers as OCTET STRING (legacy unsigned minimal bytes).
|
||||
--
|
||||
-- 5. Dynamic encoding (gmser_dyn) is intentionally not modeled here
|
||||
-- in full, because it is runtime-schema driven (type codes 246-255,
|
||||
-- labels, alt/switch, etc.). You can still use the generic
|
||||
-- TemplateFields + Value for some dynamic cases, or model specific
|
||||
-- message schemas as additional CHOICE arms.
|
||||
--
|
||||
-- 6. Compile with DER for canonical output:
|
||||
-- asn1ct:compile(GajumaruSerialization, [der]).
|
||||
--
|
||||
-- The ber option is also accepted; der implies the stricter rules.
|
||||
|
||||
END
|
||||
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,76 @@
|
||||
%% Generated by the Erlang ASN.1 compiler. Version: 5.4.3
|
||||
%% Purpose: Erlang record definitions for each named and unnamed
|
||||
%% SEQUENCE and SET, and macro definitions for each value
|
||||
%% definition in module GajumaruSerialization.
|
||||
|
||||
-ifndef(_GAJUMARUSERIALIZATION_HRL_).
|
||||
-define(_GAJUMARUSERIALIZATION_HRL_, true).
|
||||
|
||||
-record('GajumaruData', {
|
||||
tag,
|
||||
vsn,
|
||||
content
|
||||
}).
|
||||
|
||||
-record('TemplateField', {
|
||||
name = asn1_NOVALUE,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('KeyValue', {
|
||||
key,
|
||||
val
|
||||
}).
|
||||
|
||||
-record('Id', {
|
||||
type,
|
||||
value
|
||||
}).
|
||||
|
||||
-record('Account', {
|
||||
foo,
|
||||
bar
|
||||
}).
|
||||
|
||||
-record('SignedTx', {
|
||||
signatures,
|
||||
transaction
|
||||
}).
|
||||
|
||||
-record('ContractV1', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode
|
||||
}).
|
||||
|
||||
-record('ContractV2', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion
|
||||
}).
|
||||
|
||||
-record('ContractV3', {
|
||||
sourceHash,
|
||||
typeInfo,
|
||||
byteCode,
|
||||
compilerVersion,
|
||||
payable
|
||||
}).
|
||||
|
||||
-record('TypeInfoV1', {
|
||||
typeHash,
|
||||
name,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-record('TypeInfoV3', {
|
||||
typeHash,
|
||||
name,
|
||||
payable,
|
||||
argType,
|
||||
outType
|
||||
}).
|
||||
|
||||
-endif. %% _GAJUMARUSERIALIZATION_HRL_
|
||||
@@ -0,0 +1,70 @@
|
||||
# ASN.1 for Static Serialization - Compact Wire Format
|
||||
|
||||
## Goal
|
||||
Use portable ASN.1 techniques to produce the most compact *deterministic* (stable/idempotent for hashing) wire format for gmserialization **static** encoding, based on existing templates.
|
||||
|
||||
Focus is on the wire format itself (not RLP translation for legacy, which is deferred).
|
||||
|
||||
## Approach
|
||||
- The `asn1/GajumaruSerialization.asn` is the single source of truth (abstract syntax).
|
||||
- Use **Unaligned PER (UPER)** as the standard compact canonical encoding rule provided by the ASN.1 framework.
|
||||
- Portable across languages/tools that support ASN.1 UPER.
|
||||
- Deterministic for a fixed schema (no extensibility, consistent packing).
|
||||
- Optimize the schema for packing:
|
||||
- Constrain INTEGER ranges.
|
||||
- Provide `staticFields` (SEQUENCE OF Value) -- no field names (names are never on the wire for static case).
|
||||
- Provide `CompactStatic` top-level type to avoid unnecessary CHOICE overhead for the common static path.
|
||||
- Concrete SEQUENCEs for well-known objects (SignedTx, ContractV* etc.) when possible.
|
||||
- Encode with the generated ASN.1 module: ` 'GajumaruSerialization':encode('CompactStatic', Value) `.
|
||||
|
||||
## Results (example sizes)
|
||||
|
||||
Using current optimized UPER:
|
||||
|
||||
- Tiny object (tag/vsn + int + 1-byte bin): 9 bytes (legacy RLP = 5)
|
||||
- List of 3 ints: 13 bytes (legacy = 7)
|
||||
- Signed tx example: 11 bytes (legacy = 7)
|
||||
- 256-byte payload: ~263 bytes (legacy ~264) -- matches or slightly better
|
||||
|
||||
PER/UPER overhead is mainly the structural tags for the generic case. Concrete types and `staticFields` + `CompactStatic` minimize it.
|
||||
|
||||
Compared to DER (previous orientation): dramatically better (e.g. tiny case was ~36B in DER).
|
||||
|
||||
## Usage in Erlang (for the compact format)
|
||||
|
||||
```erlang
|
||||
% Build value according to schema (using staticFields for best compactness)
|
||||
Value = {'CompactStatic', Tag, Vsn, [
|
||||
{'intValue', 42},
|
||||
{'binaryValue', <<"data">>}
|
||||
% ...
|
||||
]},
|
||||
|
||||
{ok, CompactBytes} = 'GajumaruSerialization':encode('CompactStatic', Value).
|
||||
```
|
||||
|
||||
Compile the schema with:
|
||||
```
|
||||
asn1ct:compile("GajumaruSerialization.asn", [uper]).
|
||||
```
|
||||
|
||||
## Schema Notes
|
||||
- `staticFields` should be used for generic static templates (mirrors legacy positional encoding).
|
||||
- Concrete types (e.g. `signedTx`) are preferred when the structure is fixed.
|
||||
- `TemplateFields` (with names) is kept for debug/transition but not optimal for wire size.
|
||||
- The model directly reflects the static `template()` types from `gmserialization.erl`.
|
||||
|
||||
## Portability
|
||||
Any language with an ASN.1 UPER codec can produce and consume the exact same bytes by using the schema and the same value construction rules.
|
||||
|
||||
## Stability
|
||||
- UPER encoding of this schema is stable (tested roundtrip + re-encode identical).
|
||||
- No random/padding choices.
|
||||
- Same input value always produces identical bytes.
|
||||
|
||||
## Limitations / Future
|
||||
- For very small objects, hand-crafted RLP is still smaller because it has almost no structural overhead.
|
||||
- If an even more compact custom encoding is desired while keeping the model, a custom "encoding rule" can be implemented driven by the schema (similar to how the RLP layer works, but targeting a new bit-packed format).
|
||||
- Dynamic encoder (gmser_dyn) is out of scope.
|
||||
|
||||
See also: `asn1/GajumaruSerialization.asn`, `asn1_compact/`, tests in `src/gmser_asn1_rlp.erl` (for value shapes), `doc/static.md`.
|
||||
@@ -0,0 +1,88 @@
|
||||
# Schema export (ASN.1)
|
||||
|
||||
`gmser_schema_export` turns static serialization templates into ASN.1 type
|
||||
definitions. ASN.1 is the **portable abstract model** (headers / codegen for
|
||||
other languages). The on-chain wire format remains RLP via `gmserialization`
|
||||
and `gmser_rlp`.
|
||||
|
||||
## Core API
|
||||
|
||||
Callers supply the three facts that fully define a static object layout:
|
||||
|
||||
```erlang
|
||||
Tag = gmser_chain_objects:tag(spend_tx), %% 12
|
||||
Vsn = 1,
|
||||
Template = [ {sender_id, id}
|
||||
, {recipient_id, id}
|
||||
, {amount, int}
|
||||
, {gas_price, int}
|
||||
, {gas, int}
|
||||
, {ttl, int}
|
||||
, {nonce, int}
|
||||
, {payload, binary}
|
||||
], %% e.g. aec_spend_tx:serialization_template(1)
|
||||
|
||||
{ok, TypeName, Defs} =
|
||||
gmser_schema_export:object_to_asn1(Tag, Vsn, Template).
|
||||
%% TypeName = "SpendTxV1"
|
||||
%% Defs = ASN.1 SEQUENCE body for that type
|
||||
```
|
||||
|
||||
Assemble a full module:
|
||||
|
||||
```erlang
|
||||
{ok, Asn1} =
|
||||
gmser_schema_export:module_to_asn1(
|
||||
'GajumaruChainObjects',
|
||||
[ {spend_tx, Tag, Vsn, Template}
|
||||
, {signed_tx, 11, 1, SignedTemplate}
|
||||
]).
|
||||
|
||||
ok = gmser_schema_export:write_module(
|
||||
"asn1_generated/GajumaruChainObjects.asn",
|
||||
'GajumaruChainObjects',
|
||||
Objects).
|
||||
```
|
||||
|
||||
Object specs are either `{Tag, Vsn, Template}` or
|
||||
`{TypeName, Tag, Vsn, Template}`. When the name is omitted, it is derived
|
||||
from `gmser_chain_objects:rev_tag(Tag)` (e.g. `spend_tx` → `SpendTxV1`).
|
||||
|
||||
## Options
|
||||
|
||||
```erlang
|
||||
#{ type_name => atom() | string() | binary() %% override derived name
|
||||
, include_tag_vsn => boolean() %% default true:
|
||||
%% first fields are
|
||||
%% tag INTEGER (Tag),
|
||||
%% vsn INTEGER (Vsn)
|
||||
}
|
||||
```
|
||||
|
||||
## Type mapping
|
||||
|
||||
| Template type | ASN.1 |
|
||||
|---------------|--------|
|
||||
| `int` | `BigInt` (`INTEGER (0..MAX)`) |
|
||||
| `uint8` … `uint128` | `Uint8` … `Uint128` |
|
||||
| `bool` | `BOOLEAN` |
|
||||
| `binary` | `OCTET STRING` |
|
||||
| `id` | `Id` |
|
||||
| `[T]` | `SEQUENCE OF T` |
|
||||
| `{T1,...,Tn}` | anonymous `SEQUENCE { c1 T1, ... }` |
|
||||
| `#{items := [{f,T},...]}` | anonymous `SEQUENCE { f T, ... }` |
|
||||
|
||||
Field names are lowerCamelCase (`gas_price` → `gasPrice`). Type names are
|
||||
UpperCamelCase with a version suffix (`spend_tx` + vsn 1 → `SpendTxV1`).
|
||||
|
||||
## Discovery of templates
|
||||
|
||||
This module does **not** scan the tree for all templates. Callers (or a later
|
||||
harvester in gajumaru) are expected to gather `{Tag, Vsn, Template}` triples
|
||||
from `serialization_template/1` and `gmser_chain_objects:tag/1`.
|
||||
|
||||
## Related
|
||||
|
||||
- Template language: `doc/static.md`, `gmserialization.erl`
|
||||
- Tag registry: `gmser_chain_objects:tag/1`, `rev_tag/1`
|
||||
- Earlier ASN.1 experiments: `asn1/`, `doc/asn1_compact.md`
|
||||
+2
-1
@@ -59,7 +59,8 @@ The template 'language' is defined by these types:
|
||||
```erlang
|
||||
-type template() :: [{field_name(), type()}].
|
||||
-type field_name() :: atom().
|
||||
-type type() :: 'int'
|
||||
-type type() :: 'int' % bignum (non-negative, for amounts etc. up to 10^30 Pucks)
|
||||
| 'uint128' | 'uint64' | 'uint32' | 'uint16' | 'uint8'
|
||||
| 'bool'
|
||||
| 'binary'
|
||||
| 'id' %% As defined in aec_id.erl
|
||||
|
||||
@@ -36,6 +36,7 @@
|
||||
| transaction
|
||||
| tx_hash
|
||||
| account_pubkey
|
||||
| {account_pubkey, 0..6}
|
||||
| account_seckey
|
||||
| associate_chain
|
||||
| entry
|
||||
@@ -107,8 +108,12 @@ safe_decode_keypair(#{<<"pub">> := EncPub, <<"priv">> := EncPriv}) ->
|
||||
|
||||
-spec encode(known_type(), payload() | gmser_id:id()) -> encoded().
|
||||
encode(id_hash, Payload) ->
|
||||
{IdType, Val} = gmser_id:specialize(Payload),
|
||||
encode(id2type(IdType), Val);
|
||||
case gmser_id:to_map(Payload) of
|
||||
#{type := account, subtype := SubT, value := Val} ->
|
||||
encode({account_pubkey, SubT}, Val);
|
||||
#{type := IdType, value := Val} ->
|
||||
encode(id2type(IdType), Val)
|
||||
end;
|
||||
encode(Type, Payload) ->
|
||||
case type_size_check(Type, Payload) of
|
||||
ok ->
|
||||
@@ -237,17 +242,16 @@ id2type(associate_chain) -> associate_chain;
|
||||
id2type(channel) -> channel;
|
||||
id2type(commitment) -> commitment;
|
||||
id2type(contract) -> contract_pubkey;
|
||||
id2type(contract_source) -> contract_source;
|
||||
id2type(name) -> name;
|
||||
id2type(native_token) -> native_token;
|
||||
id2type(entry) -> entry.
|
||||
|
||||
type2id({account_pubkey, SubT}) -> {account, SubT};
|
||||
type2id(account_pubkey) -> account;
|
||||
type2id(associate_chain) -> associate_chain;
|
||||
type2id(channel) -> channel;
|
||||
type2id(commitment) -> commitment;
|
||||
type2id(contract_pubkey) -> contract;
|
||||
type2id(contract_source) -> contract_source;
|
||||
type2id(name) -> name;
|
||||
type2id(native_token) -> native_token;
|
||||
type2id(entry) -> entry.
|
||||
@@ -266,6 +270,7 @@ type2enc(contract_store_value) -> ?BASE64;
|
||||
type2enc(contract_source) -> ?BASE64;
|
||||
type2enc(transaction) -> ?BASE64;
|
||||
type2enc(tx_hash) -> ?BASE58;
|
||||
type2enc({account_pubkey, _}) -> ?BASE58;
|
||||
type2enc(account_pubkey) -> ?BASE58;
|
||||
type2enc(account_seckey) -> ?BASE58;
|
||||
type2enc(associate_chain) -> ?BASE58;
|
||||
@@ -298,6 +303,13 @@ type2pfx(contract_store_value) -> <<"cv">>;
|
||||
type2pfx(contract_source) -> <<"cx">>;
|
||||
type2pfx(transaction) -> <<"tx">>;
|
||||
type2pfx(tx_hash) -> <<"th">>;
|
||||
type2pfx({account_pubkey,0}) -> <<"a0">>;
|
||||
type2pfx({account_pubkey,1}) -> <<"a1">>;
|
||||
type2pfx({account_pubkey,2}) -> <<"a2">>;
|
||||
type2pfx({account_pubkey,3}) -> <<"a3">>;
|
||||
type2pfx({account_pubkey,4}) -> <<"a4">>;
|
||||
type2pfx({account_pubkey,5}) -> <<"a5">>;
|
||||
type2pfx({account_pubkey,6}) -> <<"a6">>;
|
||||
type2pfx(account_pubkey) -> <<"ak">>;
|
||||
type2pfx(account_seckey) -> <<"sk">>;
|
||||
type2pfx(associate_chain) -> <<"ac">>;
|
||||
@@ -329,6 +341,13 @@ pfx2type(<<"ct">>) -> contract_pubkey;
|
||||
pfx2type(<<"cx">>) -> contract_source;
|
||||
pfx2type(<<"tx">>) -> transaction;
|
||||
pfx2type(<<"th">>) -> tx_hash;
|
||||
pfx2type(<<"a0">>) -> {account_pubkey, 0};
|
||||
pfx2type(<<"a1">>) -> {account_pubkey, 1};
|
||||
pfx2type(<<"a2">>) -> {account_pubkey, 2};
|
||||
pfx2type(<<"a3">>) -> {account_pubkey, 3};
|
||||
pfx2type(<<"a4">>) -> {account_pubkey, 4};
|
||||
pfx2type(<<"a5">>) -> {account_pubkey, 5};
|
||||
pfx2type(<<"a6">>) -> {account_pubkey, 6};
|
||||
pfx2type(<<"ak">>) -> account_pubkey;
|
||||
pfx2type(<<"sk">>) -> account_seckey;
|
||||
pfx2type(<<"ac">>) -> associate_chain;
|
||||
@@ -363,6 +382,7 @@ byte_size_for_type(contract_source) -> not_applicable;
|
||||
byte_size_for_type(transaction) -> not_applicable;
|
||||
byte_size_for_type(tx_hash) -> 32;
|
||||
byte_size_for_type(account_pubkey) -> 32;
|
||||
byte_size_for_type({account_pubkey, _}) -> 32;
|
||||
byte_size_for_type(account_seckey) -> 32;
|
||||
byte_size_for_type(associate_chain) -> 32;
|
||||
byte_size_for_type(signature) -> 64;
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2026, QPQ AG (experiment)
|
||||
%%% @doc
|
||||
%%% Thin RLP production layer driven by ASN.1-shaped values.
|
||||
%%%
|
||||
%%% This module implements a "thin translation" from structures that
|
||||
%%% mirror the ASN.1 definitions in asn1/GajumaruSerialization.asn
|
||||
%%% to the exact same RLP wire format produced by the legacy
|
||||
%%% gmserialization + gmser_rlp + gmser_chain_objects stack.
|
||||
%%%
|
||||
%%% Goal:
|
||||
%%% - Use ASN.1 as a formal, multi-language-friendly schema.
|
||||
%%% - Keep the compact legacy RLP on the wire (no DER bloat).
|
||||
%%% - Provide a reference implementation that other languages can
|
||||
%%% port (types from ASN.1 compiler + this thin RLP emitter).
|
||||
%%%
|
||||
%%% The layer does NOT use the ASN.1 BER/DER codec at runtime for
|
||||
%%% the wire format. It walks ASN.1-like Erlang terms and emits
|
||||
%%% RLP using the same rules as gmserialization:encode_field/2
|
||||
%%% and gmser_rlp:encode/1.
|
||||
%%%
|
||||
%%% Supported shapes (matching the ASN.1 value notation):
|
||||
%%% {'GajumaruData', Tag, Vsn, Content}
|
||||
%%% Content is a CHOICE:
|
||||
%%% {templateFields, [ {'TemplateField', Name, Value}, ... ]}
|
||||
%%% {signedTx, {'SignedTx', Sigs, Tx}}
|
||||
%%% {account, {'Account', Foo, Bar}}
|
||||
%%% ...
|
||||
%%% Value is one of:
|
||||
%%% {bigIntValue, integer()} % bignum (the original "int" for Pucks etc.)
|
||||
%%% {uint128Value, integer()}
|
||||
%%% {uint64Value, integer()}
|
||||
%%% {uint32Value, integer()}
|
||||
%%% {uint16Value, integer()}
|
||||
%%% {uint8Value, integer()}
|
||||
%%% {binaryValue, binary()}
|
||||
%%% {boolValue, boolean()}
|
||||
%%% {listValue, [Value]}
|
||||
%%% {tupleValue, [Value]} % or tuple, both accepted
|
||||
%%% {idValue, ...} % basic support
|
||||
%%%
|
||||
%%% For templateFields (used for generic/static equivalence), field
|
||||
%%% *names* are ignored on the wire (matching legacy static behavior
|
||||
%%% where only values are sent in template order).
|
||||
%%%
|
||||
%%% Equivalence with legacy is the primary contract of this module.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
|
||||
-module(gmser_asn1_rlp).
|
||||
-vsn("0.1.0-experiment").
|
||||
|
||||
-export([encode/1]).
|
||||
|
||||
%% For tests and other-language ports, these helpers are useful
|
||||
-export([encode_basic/2,
|
||||
encode_asn1_value/1]).
|
||||
|
||||
-ifdef(TEST).
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
-endif.
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
|
||||
-spec encode(term()) -> binary().
|
||||
encode({'GajumaruData', Tag, Vsn, Content}) ->
|
||||
TagB = encode_basic(int, Tag),
|
||||
VsnB = encode_basic(int, Vsn),
|
||||
Payload = encode_content(Content),
|
||||
gmser_rlp:encode([TagB, VsnB | Payload]);
|
||||
encode(Other) ->
|
||||
error({unsupported_asn1_top_level, Other}).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Internal: content (the CHOICE after tag/vsn)
|
||||
%%%===================================================================
|
||||
|
||||
encode_content({templateFields, FieldList}) when is_list(FieldList) ->
|
||||
%% For wire compatibility with legacy static serialization we
|
||||
%% emit only the values (in order). Names are not sent on the wire.
|
||||
[encode_asn1_value(Val) || {_, _Name, Val} <- FieldList];
|
||||
|
||||
encode_content({signedTx, {'SignedTx', Sigs, Tx}}) ->
|
||||
SigsEnc = [encode_basic(binary, S) || S <- Sigs],
|
||||
TxEnc = encode_basic(binary, Tx),
|
||||
[SigsEnc, TxEnc];
|
||||
|
||||
encode_content({account, {'Account', Foo, Bar}}) ->
|
||||
[encode_basic(int, Foo), encode_basic(binary, Bar)];
|
||||
|
||||
encode_content({contract, Contract}) ->
|
||||
encode_contract(Contract);
|
||||
|
||||
encode_content(Other) ->
|
||||
error({unsupported_content_choice, Other}).
|
||||
|
||||
encode_contract({v1, {'ContractV1', Hash, TypeInfo, ByteCode}}) ->
|
||||
[encode_basic(binary, Hash),
|
||||
[encode_type_info_v1(TI) || TI <- TypeInfo],
|
||||
encode_basic(binary, ByteCode)];
|
||||
|
||||
encode_contract({v2, {'ContractV2', Hash, TypeInfo, ByteCode, CompilerVsn}}) ->
|
||||
[encode_basic(binary, Hash),
|
||||
[encode_type_info_v1(TI) || TI <- TypeInfo],
|
||||
encode_basic(binary, ByteCode),
|
||||
encode_basic(binary, CompilerVsn)];
|
||||
|
||||
encode_contract({v3, {'ContractV3', Hash, TypeInfo, ByteCode, CompilerVsn, Payable}}) ->
|
||||
[encode_basic(binary, Hash),
|
||||
[encode_type_info_v3(TI) || TI <- TypeInfo],
|
||||
encode_basic(binary, ByteCode),
|
||||
encode_basic(binary, CompilerVsn),
|
||||
encode_basic(bool, Payable)].
|
||||
|
||||
encode_type_info_v1({_TypeInfoV1, H, N, A, O}) ->
|
||||
[encode_basic(binary, H),
|
||||
encode_basic(binary, N),
|
||||
encode_basic(binary, A),
|
||||
encode_basic(binary, O)];
|
||||
encode_type_info_v1(T) when is_tuple(T), tuple_size(T) =:= 4 ->
|
||||
%% Accept plain 4-tuple as well for convenience
|
||||
[encode_basic(binary, element(I, T)) || I <- lists:seq(1,4)].
|
||||
|
||||
encode_type_info_v3({_TypeInfoV3, H, N, P, A, O}) ->
|
||||
[encode_basic(binary, H),
|
||||
encode_basic(binary, N),
|
||||
encode_basic(bool, P),
|
||||
encode_basic(binary, A),
|
||||
encode_basic(binary, O)];
|
||||
encode_type_info_v3(T) when is_tuple(T), tuple_size(T) =:= 5 ->
|
||||
%% TypeInfoV3 layout: binary, binary, bool, binary, binary
|
||||
[encode_basic(binary, element(1,T)),
|
||||
encode_basic(binary, element(2,T)),
|
||||
encode_basic(bool, element(3,T)),
|
||||
encode_basic(binary, element(4,T)),
|
||||
encode_basic(binary, element(5,T))].
|
||||
|
||||
%%%===================================================================
|
||||
%%% Value encoding (recursive, mirrors ASN.1 Value CHOICE)
|
||||
%%%===================================================================
|
||||
|
||||
-spec encode_asn1_value(term()) -> binary() | [term()].
|
||||
encode_asn1_value({bigIntValue, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({uint128Value, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({uint64Value, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({uint32Value, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({uint16Value, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({uint8Value, I}) -> encode_basic(int, I);
|
||||
encode_asn1_value({binaryValue, B}) -> encode_basic(binary, B);
|
||||
encode_asn1_value({boolValue, B}) -> encode_basic(bool, B);
|
||||
encode_asn1_value({listValue, L}) when is_list(L) ->
|
||||
[encode_asn1_value(E) || E <- L];
|
||||
encode_asn1_value({tupleValue, T}) when is_list(T) ->
|
||||
[encode_asn1_value(E) || E <- T];
|
||||
encode_asn1_value({tupleValue, T}) when is_tuple(T) ->
|
||||
[encode_asn1_value(E) || E <- tuple_to_list(T)];
|
||||
encode_asn1_value({idValue, {'Id', Type, Val}}) ->
|
||||
%% Basic support: encode as the legacy 33-byte id form if possible,
|
||||
%% otherwise fall back to treating the value as binary.
|
||||
try
|
||||
Id = gmser_id:create(decode_id_tag(Type), Val),
|
||||
gmser_id:encode(Id)
|
||||
catch _:_ ->
|
||||
encode_basic(binary, Val)
|
||||
end;
|
||||
encode_asn1_value({idValue, Bin}) when is_binary(Bin) ->
|
||||
%% Convenience: bare 33-byte id value
|
||||
encode_basic(binary, Bin);
|
||||
encode_asn1_value(Other) ->
|
||||
error({unsupported_asn1_value, Other}).
|
||||
|
||||
decode_id_tag(1) -> account;
|
||||
decode_id_tag(2) -> name;
|
||||
decode_id_tag(3) -> commitment;
|
||||
decode_id_tag(5) -> contract;
|
||||
decode_id_tag(6) -> channel;
|
||||
decode_id_tag(7) -> associate_chain;
|
||||
decode_id_tag(8) -> native_token;
|
||||
decode_id_tag(9) -> entry;
|
||||
decode_id_tag(T) when is_integer(T) -> error({unknown_id_tag, T}).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Basic encoders matching gmserialization rules
|
||||
%%%===================================================================
|
||||
|
||||
-spec encode_basic(atom(), term()) -> binary().
|
||||
encode_basic(int, X) when is_integer(X), X >= 0 ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_basic(binary, X) when is_binary(X) ->
|
||||
X;
|
||||
encode_basic(bool, true) -> <<1:8>>;
|
||||
encode_basic(bool, false) -> <<0:8>>;
|
||||
encode_basic(id, Val) ->
|
||||
try gmser_id:encode(Val)
|
||||
catch _:_ -> error({illegal, id, Val})
|
||||
end;
|
||||
encode_basic(Type, Val) ->
|
||||
error({unsupported_basic_type, Type, Val}).
|
||||
|
||||
%%%===================================================================
|
||||
%%% EUnit equivalence tests
|
||||
%%%===================================================================
|
||||
|
||||
-ifdef(TEST).
|
||||
|
||||
%% These tests assert that encoding an ASN.1-shaped value produces
|
||||
%% *exactly* the same bytes as the legacy gmserialization stack.
|
||||
%% This is the key property for a thin RLP production layer.
|
||||
|
||||
equivalence_simple_fields_test() ->
|
||||
T = [{foo, uint32}, {bar, binary}],
|
||||
V = [{foo, 1}, {bar, <<2>>}],
|
||||
Legacy = gmser_chain_objects:serialize(account, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"foo">>, {uint32Value, 1}},
|
||||
{'TemplateField', <<"bar">>, {binaryValue, <<2>>}}
|
||||
]}},
|
||||
New = encode(Asn1),
|
||||
?assertEqual(Legacy, New),
|
||||
%% Also check we can roundtrip via legacy decoder
|
||||
Dec = gmser_chain_objects:deserialize(account, 1, T, New),
|
||||
?assertEqual(V, Dec).
|
||||
|
||||
equivalence_zero_and_empty_test() ->
|
||||
T = [{foo, int}, {bar, binary}],
|
||||
V = [{foo, 0}, {bar, <<>>}],
|
||||
Legacy = gmser_chain_objects:serialize(account, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"foo">>, {bigIntValue, 0}},
|
||||
{'TemplateField', <<"bar">>, {binaryValue, <<>>}}
|
||||
]}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
equivalence_list_field_test() ->
|
||||
T = [{xs, [int]}],
|
||||
V = [{xs, [1,2,3]}],
|
||||
Legacy = gmser_chain_objects:serialize(account, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"xs">>, {listValue, [
|
||||
{bigIntValue, 1}, {bigIntValue, 2}, {bigIntValue, 3}
|
||||
]}}
|
||||
]}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
equivalence_tuple_field_test() ->
|
||||
T = [{p, {int, binary}}],
|
||||
V = [{p, {42, <<"hi">>}}],
|
||||
Legacy = gmser_chain_objects:serialize(account, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"p">>, {tupleValue, [
|
||||
{bigIntValue, 42}, {binaryValue, <<"hi">>}
|
||||
]}}
|
||||
]}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
equivalence_signed_tx_concrete_test() ->
|
||||
T = [{signatures, [binary]}, {tx, binary}],
|
||||
V = [{signatures, [<<"sig1">>, <<"sig2">>]}, {tx, <<"txbody123">>}],
|
||||
Legacy = gmser_chain_objects:serialize(signed_tx, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 11, 1, {signedTx, {'SignedTx',
|
||||
[<<"sig1">>, <<"sig2">>], <<"txbody123">>}}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
equivalence_list_of_tuples_test() ->
|
||||
%% Corresponds to type_info style: list of 4-tuples
|
||||
T = [{type_info, [{binary, binary, binary, binary}]}],
|
||||
V = [{type_info, [
|
||||
{<<"h1">>, <<"n1">>, <<"a1">>, <<"o1">>},
|
||||
{<<"h2">>, <<"n2">>, <<"a2">>, <<"o2">>}
|
||||
]}],
|
||||
Legacy = gmser_chain_objects:serialize(account, 1, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 10, 1, {templateFields, [
|
||||
{'TemplateField', <<"type_info">>, {listValue, [
|
||||
{tupleValue, [{binaryValue,<<"h1">>},{binaryValue,<<"n1">>},
|
||||
{binaryValue,<<"a1">>},{binaryValue,<<"o1">>}]},
|
||||
{tupleValue, [{binaryValue,<<"h2">>},{binaryValue,<<"n2">>},
|
||||
{binaryValue,<<"a2">>},{binaryValue,<<"o2">>}]}
|
||||
]}}
|
||||
]}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
equivalence_contract_v3_test() ->
|
||||
T = [ {source_hash, binary}
|
||||
, {type_info, [{binary, binary, bool, binary, binary}]}
|
||||
, {byte_code, binary}
|
||||
, {compiler_version, binary}
|
||||
, {payable, bool}
|
||||
],
|
||||
TI = [{<<"h">>, <<"n">>, true, <<"a">>, <<"o">>}],
|
||||
V = [ {source_hash, <<"hash">>}
|
||||
, {type_info, TI}
|
||||
, {byte_code, <<"code">>}
|
||||
, {compiler_version, <<"vsn">>}
|
||||
, {payable, true}
|
||||
],
|
||||
Legacy = gmser_chain_objects:serialize(contract, 3, T, V),
|
||||
|
||||
Asn1 = {'GajumaruData', 40, 3, {contract, {v3, {'ContractV3',
|
||||
<<"hash">>,
|
||||
[ {<<"h">>, <<"n">>, true, <<"a">>, <<"o">>} ],
|
||||
<<"code">>,
|
||||
<<"vsn">>,
|
||||
true
|
||||
}}}},
|
||||
?assertEqual(Legacy, encode(Asn1)).
|
||||
|
||||
-endif.
|
||||
@@ -13,6 +13,8 @@
|
||||
-export([ serialize/4
|
||||
, deserialize/4
|
||||
, deserialize_type_and_vsn/1
|
||||
, tag/1
|
||||
, rev_tag/1
|
||||
]).
|
||||
|
||||
-type template() :: gmserialization:template().
|
||||
@@ -34,8 +36,14 @@ deserialize_type_and_vsn(Binary) ->
|
||||
deserialize(Type, Vsn, Template, Binary) ->
|
||||
gmserialization:deserialize(Type, tag(Type), Vsn, Template, Binary).
|
||||
|
||||
%% Numeric wire tag for a chain-object type atom (e.g. spend_tx -> 12).
|
||||
-spec tag(atom()) -> non_neg_integer().
|
||||
|
||||
%% Inverse of tag/1.
|
||||
-spec rev_tag(non_neg_integer()) -> atom().
|
||||
|
||||
%%%===================================================================
|
||||
%%% Internal functions
|
||||
%%% Tag registry
|
||||
%%%===================================================================
|
||||
|
||||
tag(account) -> 10;
|
||||
@@ -100,6 +108,8 @@ tag(ac_deposit_tx) -> 94;
|
||||
tag(ac_update_cops_tx) -> 95;
|
||||
tag(ac_rollup_tx) -> 96;
|
||||
tag(ac_proposal_tx) -> 97;
|
||||
tag(ac_receipt) -> 98;
|
||||
tag(ac_acct_state) -> 99;
|
||||
tag(key_block) -> 100;
|
||||
tag(micro_block) -> 101;
|
||||
tag(light_micro_block) -> 102;
|
||||
@@ -115,7 +125,16 @@ tag(entry) -> 140;
|
||||
tag(entry_create_tx) -> 141;
|
||||
tag(entry_transfer_tx) -> 142;
|
||||
tag(entry_destroy_tx) -> 143;
|
||||
tag(pof) -> 200.
|
||||
tag(account_key_store) -> 144;
|
||||
tag(account_create_tx) -> 145;
|
||||
tag(account_sig_store) -> 146;
|
||||
tag(auth_tx) -> 147;
|
||||
tag(proposal_gossip_tx) -> 148;
|
||||
tag(account_auth_update_tx) -> 149;
|
||||
tag(pof) -> 200;
|
||||
%% Gajumaru AC side transactions
|
||||
tag(ac_side_withdraw_tx) -> 300;
|
||||
tag(ac_side_rollup_tx) -> 301.
|
||||
|
||||
rev_tag(10) -> account;
|
||||
rev_tag(11) -> signed_tx;
|
||||
@@ -179,6 +198,8 @@ rev_tag(94) -> ac_deposit_tx;
|
||||
rev_tag(95) -> ac_update_cops_tx;
|
||||
rev_tag(96) -> ac_rollup_tx;
|
||||
rev_tag(97) -> ac_proposal_tx;
|
||||
rev_tag(98) -> ac_receipt;
|
||||
rev_tag(99) -> ac_acct_state;
|
||||
rev_tag(100) -> key_block;
|
||||
rev_tag(101) -> micro_block;
|
||||
rev_tag(102) -> light_micro_block;
|
||||
@@ -194,4 +215,13 @@ rev_tag(140) -> entry;
|
||||
rev_tag(141) -> entry_create_tx;
|
||||
rev_tag(142) -> entry_transfer_tx;
|
||||
rev_tag(143) -> entry_destroy_tx;
|
||||
rev_tag(200) -> pof.
|
||||
rev_tag(144) -> account_key_store;
|
||||
rev_tag(145) -> account_create_tx;
|
||||
rev_tag(146) -> account_sig_store;
|
||||
rev_tag(147) -> auth_tx;
|
||||
rev_tag(148) -> proposal_gossip_tx;
|
||||
rev_tag(149) -> account_auth_update_tx;
|
||||
rev_tag(200) -> pof;
|
||||
%% Gajumaru AC side transactions
|
||||
rev_tag(300) -> ac_side_withdraw_tx;
|
||||
rev_tag(301) -> ac_side_rollup_tx.
|
||||
|
||||
+52
-3
@@ -14,6 +14,9 @@
|
||||
, specialize/1
|
||||
, specialize/2
|
||||
, specialize_type/1
|
||||
, is_account/1
|
||||
, account_pubkey/1
|
||||
, to_map/1
|
||||
, is_id/1
|
||||
]).
|
||||
|
||||
@@ -29,12 +32,17 @@
|
||||
, val
|
||||
}).
|
||||
|
||||
-type tag() :: 'account'
|
||||
-type subtype() :: 0..6.
|
||||
-type id_map() :: #{ type := simple_tag()
|
||||
, subtype => subtype()
|
||||
, value := binary() }.
|
||||
|
||||
-type tag() :: {'account', subtype()} | simple_tag().
|
||||
-type simple_tag() :: 'account'
|
||||
| 'associate_chain'
|
||||
| 'channel'
|
||||
| 'commitment'
|
||||
| 'contract'
|
||||
| 'contract_source'
|
||||
| 'name'
|
||||
| 'native_token'
|
||||
| 'entry'.
|
||||
@@ -57,7 +65,8 @@
|
||||
___TAG___ =:= contract;
|
||||
___TAG___ =:= channel;
|
||||
___TAG___ =:= associate_chain;
|
||||
___TAG___ =:= entry
|
||||
___TAG___ =:= entry;
|
||||
___TAG___ =:= native_token
|
||||
).
|
||||
-define(IS_VAL(___VAL___), byte_size(___VAL___) =:= 32).
|
||||
|
||||
@@ -69,6 +78,8 @@
|
||||
create(Tag, Val) when ?IS_TAG(Tag), ?IS_VAL(Val) ->
|
||||
#id{ tag = Tag
|
||||
, val = Val};
|
||||
create({account,I}, Val) when is_binary(Val), I >= 0, I =< 6 ->
|
||||
#id{ tag = {account, I}, val = Val};
|
||||
create(Tag, Val) when ?IS_VAL(Val) ->
|
||||
error({illegal_tag, Tag});
|
||||
create(Tag, Val) when ?IS_TAG(Tag)->
|
||||
@@ -78,28 +89,66 @@ create(Tag, Val) ->
|
||||
|
||||
|
||||
-spec specialize(id()) -> {tag(), val()}.
|
||||
specialize(#id{tag = {Tag,_}, val = Val}) ->
|
||||
{Tag, Val};
|
||||
specialize(#id{tag = Tag, val = Val}) ->
|
||||
{Tag, Val}.
|
||||
|
||||
-spec specialize(id(), tag()) -> val().
|
||||
specialize(#id{tag = {Tag, _}, val = Val}, Tag) when is_binary(Val) ->
|
||||
Val;
|
||||
specialize(#id{tag = Tag, val = Val}, Tag) when ?IS_TAG(Tag), ?IS_VAL(Val) ->
|
||||
Val.
|
||||
|
||||
-spec specialize_type(id()) -> tag().
|
||||
specialize_type(#id{tag = {Tag, _}}) when ?IS_TAG(Tag) ->
|
||||
Tag;
|
||||
specialize_type(#id{tag = Tag}) when ?IS_TAG(Tag) ->
|
||||
Tag.
|
||||
|
||||
-spec is_account(id() | term()) -> boolean().
|
||||
is_account(#id{tag = account}) ->
|
||||
true;
|
||||
is_account(#id{tag = {account, _}}) ->
|
||||
true;
|
||||
is_account(_) ->
|
||||
false.
|
||||
|
||||
-spec account_pubkey(id()) -> val().
|
||||
account_pubkey(#id{tag = account, val = Val}) when ?IS_VAL(Val) ->
|
||||
Val;
|
||||
account_pubkey(#id{tag = {account, _}, val = Val}) when ?IS_VAL(Val) ->
|
||||
Val.
|
||||
|
||||
-spec to_map(id()) -> id_map().
|
||||
to_map(#id{tag = {Tag, SubType}, val = Val}) when ?IS_TAG(Tag) ->
|
||||
#{ type => Tag
|
||||
, subtype => SubType
|
||||
, value => Val };
|
||||
to_map(#id{tag = Tag, val = Val}) when ?IS_TAG(Tag) ->
|
||||
#{ type => Tag
|
||||
, value => Val }.
|
||||
|
||||
|
||||
-spec is_id(term()) -> boolean().
|
||||
is_id(#id{}) -> true;
|
||||
is_id(_) -> false.
|
||||
|
||||
-spec encode(id()) -> binary().
|
||||
encode(#id{tag = {account, N}, val = Val}) when N =< 2#111_1111 ->
|
||||
Ext = 2#1000_0000 bor N,
|
||||
<<Ext:8, Val/binary>>;
|
||||
encode(#id{tag = Tag, val = Val}) ->
|
||||
Res = <<(encode_tag(Tag)):?TAG_SIZE/unit:8, Val/binary>>,
|
||||
true = ?SERIALIZED_SIZE =:= byte_size(Res),
|
||||
Res.
|
||||
|
||||
-spec decode(binary()) -> id().
|
||||
decode(<<Ext:8, Rest/binary>>) when Ext >= 2#1000_0000 ->
|
||||
%% Extended account id type
|
||||
Type = Ext band 2#0111_1111,
|
||||
#id{ tag = {account, Type}
|
||||
, val = Rest };
|
||||
decode(<<Tag:?TAG_SIZE/unit:8, Val:?PUB_SIZE/binary>>) ->
|
||||
#id{ tag = decode_tag(Tag)
|
||||
, val = Val}.
|
||||
|
||||
@@ -0,0 +1,387 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2026, QPQ AG
|
||||
%%% @doc
|
||||
%%% Export static serialization templates as ASN.1 type definitions.
|
||||
%%%
|
||||
%%% This is the portable schema surface for the template language in
|
||||
%%% {@link gmserialization}: given a chain-object tag, version and
|
||||
%%% field template, emit ASN.1 that other languages can compile to
|
||||
%%% typed headers. The wire format remains RLP; ASN.1 is the abstract
|
||||
%%% model, not BER/DER on the chain.
|
||||
%%%
|
||||
%%% Minimal API (discovery of all templates is left to callers):
|
||||
%%% <pre>
|
||||
%%% Template = aec_spend_tx:serialization_template(1),
|
||||
%%% Tag = gmser_chain_objects:tag(spend_tx),
|
||||
%%% {ok, Asn1} = gmser_schema_export:object_to_asn1(Tag, 1, Template).
|
||||
%%% </pre>
|
||||
%%%
|
||||
%%% Or assemble a full module from several objects:
|
||||
%%% <pre>
|
||||
%%% gmser_schema_export:module_to_asn1(
|
||||
%%% 'GajumaruChainObjects',
|
||||
%%% [{spend_tx, Tag, 1, Template},
|
||||
%%% {signed_tx, 11, 1, SignedTemplate}]).
|
||||
%%% </pre>
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(gmser_schema_export).
|
||||
-vsn("0.1.0").
|
||||
|
||||
-export([ object_to_asn1/3
|
||||
, object_to_asn1/4
|
||||
, module_to_asn1/2
|
||||
, module_to_asn1/3
|
||||
, write_module/3
|
||||
, write_module/4
|
||||
, type_name/2
|
||||
, type_name/3
|
||||
]).
|
||||
|
||||
-export_type([ object_spec/0
|
||||
, export_opts/0
|
||||
]).
|
||||
|
||||
-type template() :: gmserialization:template().
|
||||
-type type_name() :: atom() | string() | binary().
|
||||
|
||||
%% {Tag, Vsn, Template} | {TypeName, Tag, Vsn, Template}
|
||||
-type object_spec() ::
|
||||
{non_neg_integer(), non_neg_integer(), template()}
|
||||
| {type_name(), non_neg_integer(), non_neg_integer(), template()}.
|
||||
|
||||
-type export_opts() ::
|
||||
#{ type_name => type_name()
|
||||
, include_tag_vsn => boolean() %% default true
|
||||
}.
|
||||
|
||||
%%%===================================================================
|
||||
%%% API
|
||||
%%%===================================================================
|
||||
|
||||
%% @doc Emit ASN.1 type definitions for one object (no module wrapper).
|
||||
-spec object_to_asn1(non_neg_integer(), non_neg_integer(), template()) ->
|
||||
{ok, TypeName :: string(), Defs :: iodata()}.
|
||||
object_to_asn1(Tag, Vsn, Template) ->
|
||||
object_to_asn1(Tag, Vsn, Template, #{}).
|
||||
|
||||
-spec object_to_asn1(non_neg_integer(), non_neg_integer(), template(),
|
||||
export_opts()) ->
|
||||
{ok, TypeName :: string(), Defs :: iodata()}.
|
||||
object_to_asn1(Tag, Vsn, Template, Opts) when is_map(Opts) ->
|
||||
assert_template(Template),
|
||||
TypeName = case maps:get(type_name, Opts, undefined) of
|
||||
undefined -> type_name(Tag, Vsn);
|
||||
Name -> normalize_type_name(Name, Vsn)
|
||||
end,
|
||||
IncludeTagVsn = maps:get(include_tag_vsn, Opts, true),
|
||||
Fields = case IncludeTagVsn of
|
||||
true ->
|
||||
[{tag, {fixed_int, Tag}},
|
||||
{vsn, {fixed_int, Vsn}}
|
||||
| Template];
|
||||
false ->
|
||||
Template
|
||||
end,
|
||||
{ok, Body} = emit_sequence(TypeName, Fields),
|
||||
{ok, TypeName, Body}.
|
||||
|
||||
%% @doc Emit a complete ASN.1 module for a list of objects.
|
||||
-spec module_to_asn1(type_name(), [object_spec()]) -> {ok, iodata()}.
|
||||
module_to_asn1(ModuleName, Objects) ->
|
||||
module_to_asn1(ModuleName, Objects, #{}).
|
||||
|
||||
-spec module_to_asn1(type_name(), [object_spec()], export_opts()) ->
|
||||
{ok, iodata()}.
|
||||
module_to_asn1(ModuleName, Objects, Opts) when is_list(Objects), is_map(Opts) ->
|
||||
Mod = asn1_type_name(ModuleName),
|
||||
{TypeParts, IndexLines} =
|
||||
lists:mapfoldl(
|
||||
fun(Spec, Acc) ->
|
||||
{Tag, Vsn, Template, NameOpt} = expand_spec(Spec),
|
||||
ObjOpts = case NameOpt of
|
||||
undefined -> Opts;
|
||||
N -> Opts#{type_name => N}
|
||||
end,
|
||||
{ok, TN, Defs} = object_to_asn1(Tag, Vsn, Template, ObjOpts),
|
||||
Index = io_lib:format(
|
||||
"-- tag ~w vsn ~w -> ~s~n", [Tag, Vsn, TN]),
|
||||
{[Defs, $\n], [Index | Acc]}
|
||||
end,
|
||||
[],
|
||||
Objects),
|
||||
Header = [
|
||||
"-- Generated by gmser_schema_export.\n",
|
||||
"-- Abstract syntax for static gmserialization templates.\n",
|
||||
"-- Wire encoding remains RLP (see gmser_rlp / gmserialization);\n",
|
||||
"-- this module is for portable types / codegen, not BER on-chain.\n",
|
||||
"\n",
|
||||
Mod, " DEFINITIONS\n",
|
||||
" AUTOMATIC TAGS ::=\n",
|
||||
"BEGIN\n",
|
||||
"\n",
|
||||
"EXPORTS ALL;\n",
|
||||
"\n",
|
||||
common_types(),
|
||||
"\n",
|
||||
"-- ============================================================\n",
|
||||
"-- Generated object types\n",
|
||||
"-- ============================================================\n",
|
||||
"\n"
|
||||
],
|
||||
Index = [
|
||||
"-- ============================================================\n",
|
||||
"-- Tag / version index\n",
|
||||
"-- ============================================================\n",
|
||||
lists:reverse(IndexLines),
|
||||
"\n"
|
||||
],
|
||||
Footer = "END\n",
|
||||
{ok, [Header, TypeParts, Index, Footer]}.
|
||||
|
||||
%% @doc Write a complete ASN.1 module to a file.
|
||||
-spec write_module(file:filename(), type_name(), [object_spec()]) ->
|
||||
ok | {error, term()}.
|
||||
write_module(Filename, ModuleName, Objects) ->
|
||||
write_module(Filename, ModuleName, Objects, #{}).
|
||||
|
||||
-spec write_module(file:filename(), type_name(), [object_spec()],
|
||||
export_opts()) ->
|
||||
ok | {error, term()}.
|
||||
write_module(Filename, ModuleName, Objects, Opts) ->
|
||||
{ok, Iodata} = module_to_asn1(ModuleName, Objects, Opts),
|
||||
file:write_file(Filename, Iodata).
|
||||
|
||||
%% @doc Derive an ASN.1 type name from tag and version.
|
||||
%% Uses {@link gmser_chain_objects:rev_tag/1} when the tag is registered,
|
||||
%% otherwise `Object{Tag}V{Vsn}`.
|
||||
-spec type_name(non_neg_integer(), non_neg_integer()) -> string().
|
||||
type_name(Tag, Vsn) ->
|
||||
Base =
|
||||
try gmser_chain_objects:rev_tag(Tag) of
|
||||
Atom when is_atom(Atom) -> atom_to_list(Atom)
|
||||
catch
|
||||
error:function_clause ->
|
||||
"Object" ++ integer_to_list(Tag);
|
||||
error:{case_clause, _} ->
|
||||
"Object" ++ integer_to_list(Tag)
|
||||
end,
|
||||
normalize_type_name(Base, Vsn).
|
||||
|
||||
-spec type_name(type_name(), non_neg_integer(), non_neg_integer()) -> string().
|
||||
type_name(Name, _Tag, Vsn) ->
|
||||
normalize_type_name(Name, Vsn).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Spec expansion
|
||||
%%%===================================================================
|
||||
|
||||
expand_spec({Tag, Vsn, Template})
|
||||
when is_integer(Tag), is_integer(Vsn), is_list(Template) ->
|
||||
{Tag, Vsn, Template, undefined};
|
||||
expand_spec({Name, Tag, Vsn, Template})
|
||||
when is_integer(Tag), is_integer(Vsn), is_list(Template) ->
|
||||
{Tag, Vsn, Template, Name};
|
||||
expand_spec(Other) ->
|
||||
error({bad_object_spec, Other}).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Template → ASN.1 SEQUENCE
|
||||
%%%===================================================================
|
||||
|
||||
emit_sequence(TypeName, Fields) ->
|
||||
Lines =
|
||||
[io_lib:format("~s ::= SEQUENCE {~n", [TypeName])] ++
|
||||
field_lines(Fields) ++
|
||||
["}\n"],
|
||||
{ok, Lines}.
|
||||
|
||||
field_lines([]) ->
|
||||
[];
|
||||
field_lines([{Name, Type}]) ->
|
||||
[io_lib:format(" ~s ~s~n",
|
||||
[asn1_field_name(Name), type_to_asn1(Type)])];
|
||||
field_lines([{Name, Type} | Rest]) ->
|
||||
[io_lib:format(" ~s ~s,~n",
|
||||
[asn1_field_name(Name), type_to_asn1(Type)])
|
||||
| field_lines(Rest)].
|
||||
|
||||
%% Map one template type term to an ASN.1 type expression (string/iodata).
|
||||
type_to_asn1(int) -> "BigInt";
|
||||
type_to_asn1(uint128) -> "Uint128";
|
||||
type_to_asn1(uint64) -> "Uint64";
|
||||
type_to_asn1(uint32) -> "Uint32";
|
||||
type_to_asn1(uint16) -> "Uint16";
|
||||
type_to_asn1(uint8) -> "Uint8";
|
||||
type_to_asn1(bool) -> "BOOLEAN";
|
||||
type_to_asn1(binary) -> "OCTET STRING";
|
||||
type_to_asn1(id) -> "Id";
|
||||
type_to_asn1({fixed_int, N}) when is_integer(N), N >= 0 ->
|
||||
io_lib:format("INTEGER (~w)", [N]);
|
||||
type_to_asn1([ElemType]) ->
|
||||
%% SEQUENCE OF T (list of any length)
|
||||
io_lib:format("SEQUENCE OF ~s", [type_to_asn1(ElemType)]);
|
||||
type_to_asn1(Type) when is_tuple(Type) ->
|
||||
%% Fixed-arity tuple → anonymous SEQUENCE with c1..cN
|
||||
Types = tuple_to_list(Type),
|
||||
Components =
|
||||
lists:map(
|
||||
fun({I, T}) ->
|
||||
io_lib:format("~sc~w ~s",
|
||||
[indent_spacer(), I, type_to_asn1(T)])
|
||||
end,
|
||||
lists:zip(lists:seq(1, length(Types)), Types)),
|
||||
["SEQUENCE {\n",
|
||||
join_components(Components),
|
||||
"\n }"];
|
||||
type_to_asn1(#{items := Items}) when is_list(Items) ->
|
||||
%% Static map / record: named fields, order preserved, no keys on wire
|
||||
Components =
|
||||
[io_lib:format("~s~s ~s",
|
||||
[indent_spacer(),
|
||||
asn1_field_name(Name),
|
||||
type_to_asn1(T)])
|
||||
|| {Name, T} <- Items],
|
||||
["SEQUENCE {\n",
|
||||
join_components(Components),
|
||||
"\n }"];
|
||||
type_to_asn1(Other) ->
|
||||
error({unsupported_template_type, Other}).
|
||||
|
||||
indent_spacer() ->
|
||||
" ".
|
||||
|
||||
join_components([]) ->
|
||||
"";
|
||||
join_components([C]) ->
|
||||
C;
|
||||
join_components([C | Rest]) ->
|
||||
[C, ",\n", join_components(Rest)].
|
||||
|
||||
%%%===================================================================
|
||||
%%% Naming
|
||||
%%%===================================================================
|
||||
|
||||
%% "spend_tx" / spend_tx / <<"spend_tx">> + vsn 1 -> "SpendTxV1"
|
||||
normalize_type_name(Name, Vsn) when is_atom(Name) ->
|
||||
normalize_type_name(atom_to_list(Name), Vsn);
|
||||
normalize_type_name(Name, Vsn) when is_binary(Name) ->
|
||||
normalize_type_name(binary_to_list(Name), Vsn);
|
||||
normalize_type_name(Name, Vsn) when is_list(Name), is_integer(Vsn), Vsn >= 0 ->
|
||||
Base = asn1_type_name(Name),
|
||||
%% Avoid double-appending version if caller already passed SpendTxV1
|
||||
case lists:suffix("V" ++ integer_to_list(Vsn), Base) of
|
||||
true -> Base;
|
||||
false -> Base ++ "V" ++ integer_to_list(Vsn)
|
||||
end.
|
||||
|
||||
%% Upper camel case, strip non alphanumerics: spend_tx -> SpendTx
|
||||
asn1_type_name(Name) when is_atom(Name) ->
|
||||
asn1_type_name(atom_to_list(Name));
|
||||
asn1_type_name(Name) when is_binary(Name) ->
|
||||
asn1_type_name(binary_to_list(Name));
|
||||
asn1_type_name(Name) when is_list(Name) ->
|
||||
Parts = split_name(Name),
|
||||
lists:flatten([uppercase_first(P) || P <- Parts, P =/= ""]).
|
||||
|
||||
%% Field names: lower camel case (ASN.1 values start with lowercase;
|
||||
%% hyphens/underscores avoided for Java friendliness).
|
||||
asn1_field_name(Name) when is_atom(Name) ->
|
||||
asn1_field_name(atom_to_list(Name));
|
||||
asn1_field_name(Name) when is_binary(Name) ->
|
||||
asn1_field_name(binary_to_list(Name));
|
||||
asn1_field_name(Name) when is_list(Name) ->
|
||||
case split_name(Name) of
|
||||
[] ->
|
||||
error({bad_field_name, Name});
|
||||
[First | Rest] ->
|
||||
lists:flatten([lowercase_first(First)
|
||||
| [uppercase_first(P) || P <- Rest, P =/= ""]])
|
||||
end.
|
||||
|
||||
split_name(Name) ->
|
||||
%% Split on '_' or '-'
|
||||
split_name(Name, [], []).
|
||||
|
||||
split_name([], Acc, Cur) ->
|
||||
lists:reverse(case Cur of
|
||||
[] -> Acc;
|
||||
_ -> [lists:reverse(Cur) | Acc]
|
||||
end);
|
||||
split_name([C | Rest], Acc, Cur) when C =:= $_; C =:= $- ->
|
||||
case Cur of
|
||||
[] -> split_name(Rest, Acc, []);
|
||||
_ -> split_name(Rest, [lists:reverse(Cur) | Acc], [])
|
||||
end;
|
||||
split_name([C | Rest], Acc, Cur) ->
|
||||
split_name(Rest, Acc, [C | Cur]).
|
||||
|
||||
uppercase_first([C | Rest]) when C >= $a, C =< $z ->
|
||||
[C - ($a - $A) | Rest];
|
||||
uppercase_first(Other) ->
|
||||
Other.
|
||||
|
||||
lowercase_first([C | Rest]) when C >= $A, C =< $Z ->
|
||||
[C + ($a - $A) | Rest];
|
||||
lowercase_first(Other) ->
|
||||
Other.
|
||||
|
||||
%%%===================================================================
|
||||
%%% Common ASN.1 types (shared vocabulary)
|
||||
%%%===================================================================
|
||||
|
||||
common_types() ->
|
||||
"-- ============================================================\n"
|
||||
"-- Common types (gmserialization template vocabulary)\n"
|
||||
"-- ============================================================\n"
|
||||
"\n"
|
||||
"Id ::= SEQUENCE {\n"
|
||||
" type INTEGER (0..255),\n"
|
||||
" value OCTET STRING (SIZE (32))\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"-- Non-negative bignum (template type 'int', e.g. Pucks amounts)\n"
|
||||
"BigInt ::= INTEGER (0..MAX)\n"
|
||||
"\n"
|
||||
"Uint8 ::= INTEGER (0..255)\n"
|
||||
"Uint16 ::= INTEGER (0..65535)\n"
|
||||
"Uint32 ::= INTEGER (0..4294967295)\n"
|
||||
"Uint64 ::= INTEGER (0..18446744073709551615)\n"
|
||||
"Uint128 ::= INTEGER (0..340282366920938463463374607431768211455)\n"
|
||||
"\n".
|
||||
|
||||
%%%===================================================================
|
||||
%%% Validation
|
||||
%%%===================================================================
|
||||
|
||||
assert_template(Template) when is_list(Template) ->
|
||||
lists:foreach(
|
||||
fun({Name, Type}) when is_atom(Name) ->
|
||||
assert_type(Type);
|
||||
(Other) ->
|
||||
error({bad_template_field, Other})
|
||||
end,
|
||||
Template);
|
||||
assert_template(Other) ->
|
||||
error({bad_template, Other}).
|
||||
|
||||
assert_type(int) -> ok;
|
||||
assert_type(uint128) -> ok;
|
||||
assert_type(uint64) -> ok;
|
||||
assert_type(uint32) -> ok;
|
||||
assert_type(uint16) -> ok;
|
||||
assert_type(uint8) -> ok;
|
||||
assert_type(bool) -> ok;
|
||||
assert_type(binary) -> ok;
|
||||
assert_type(id) -> ok;
|
||||
assert_type([T]) -> assert_type(T);
|
||||
assert_type(T) when is_tuple(T) ->
|
||||
lists:foreach(fun assert_type/1, tuple_to_list(T));
|
||||
assert_type(#{items := Items}) when is_list(Items) ->
|
||||
lists:foreach(
|
||||
fun({Name, T}) when is_atom(Name) -> assert_type(T);
|
||||
(Other) -> error({bad_map_item, Other})
|
||||
end,
|
||||
Items);
|
||||
assert_type(Other) ->
|
||||
error({unsupported_template_type, Other}).
|
||||
+41
-1
@@ -29,7 +29,12 @@
|
||||
|
||||
-type template() :: [{field_name(), type()}].
|
||||
-type field_name() :: atom().
|
||||
-type type() :: 'int'
|
||||
-type type() :: 'int' % bignum (non-negative, arbitrary size; used for Pucks amounts etc. up to 10^30)
|
||||
| 'uint128'
|
||||
| 'uint64'
|
||||
| 'uint32'
|
||||
| 'uint16'
|
||||
| 'uint8'
|
||||
| 'bool'
|
||||
| 'binary'
|
||||
| 'id' %% As defined in aec_id.erl
|
||||
@@ -118,6 +123,16 @@ encode_field(#{items := Items}, Map) ->
|
||||
encode_field(Type, T) when tuple_size(Type) =:= tuple_size(T) ->
|
||||
Zipped = lists:zip(tuple_to_list(Type), tuple_to_list(T)),
|
||||
[encode_field(X, Y) || {X, Y} <- Zipped];
|
||||
encode_field(uint128, X) when is_integer(X), X >= 0, X < (1 bsl 128) ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(uint64, X) when is_integer(X), X >= 0, X < (1 bsl 64) ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(uint32, X) when is_integer(X), X >= 0, X < (1 bsl 32) ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(uint16, X) when is_integer(X), X >= 0, X < (1 bsl 16) ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(uint8, X) when is_integer(X), X >= 0, X < (1 bsl 8) ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(int, X) when is_integer(X), X >= 0 ->
|
||||
binary:encode_unsigned(X);
|
||||
encode_field(binary, X) when is_binary(X) -> X;
|
||||
@@ -141,6 +156,31 @@ decode_field(#{items := Items}, List) when length(List) =:= length(Items) ->
|
||||
decode_field(Type, List) when length(List) =:= tuple_size(Type) ->
|
||||
Zipped = lists:zip(tuple_to_list(Type), List),
|
||||
list_to_tuple([decode_field(X, Y) || {X, Y} <- Zipped]);
|
||||
decode_field(uint128, X) when is_binary(X) ->
|
||||
I = binary:decode_unsigned(X),
|
||||
if I < (1 bsl 128) -> I;
|
||||
true -> error({illegal, uint128, X})
|
||||
end;
|
||||
decode_field(uint64, X) when is_binary(X) ->
|
||||
I = binary:decode_unsigned(X),
|
||||
if I < (1 bsl 64) -> I;
|
||||
true -> error({illegal, uint64, X})
|
||||
end;
|
||||
decode_field(uint32, X) when is_binary(X) ->
|
||||
I = binary:decode_unsigned(X),
|
||||
if I < (1 bsl 32) -> I;
|
||||
true -> error({illegal, uint32, X})
|
||||
end;
|
||||
decode_field(uint16, X) when is_binary(X) ->
|
||||
I = binary:decode_unsigned(X),
|
||||
if I < (1 bsl 16) -> I;
|
||||
true -> error({illegal, uint16, X})
|
||||
end;
|
||||
decode_field(uint8, X) when is_binary(X) ->
|
||||
I = binary:decode_unsigned(X),
|
||||
if I < (1 bsl 8) -> I;
|
||||
true -> error({illegal, uint8, X})
|
||||
end;
|
||||
decode_field(int, <<0:8, X/binary>> = B) when X =/= <<>> ->
|
||||
error({illegal, int, B});
|
||||
decode_field(int, X) when is_binary(X) -> binary:decode_unsigned(X);
|
||||
|
||||
@@ -194,7 +194,16 @@ known_types() ->
|
||||
Forms = get_forms(),
|
||||
[{type, _, union, Types}] =
|
||||
[Def || {attribute, _, type, {known_type, Def, []}} <- Forms],
|
||||
[Name || {atom,_, Name} <- Types].
|
||||
lists:flatmap(fun known_type_entry/1, Types).
|
||||
|
||||
known_type_entry({atom, _, Name}) ->
|
||||
[Name];
|
||||
known_type_entry({type, _, tuple,
|
||||
[{atom, _, account_pubkey},
|
||||
{type, _, range, [{integer, _, Lo}, {integer, _, Hi}]}]}) ->
|
||||
[{account_pubkey, N} || N <- lists:seq(Lo, Hi)];
|
||||
known_type_entry(Other) ->
|
||||
error({unsupported_known_type, Other}).
|
||||
|
||||
mapped_prefixes() ->
|
||||
Forms = get_forms(),
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
-module(gmser_id_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
-define(PUBKEY, <<12345:32/unit:8>>).
|
||||
|
||||
is_account_test() ->
|
||||
{"is_account recognizes standard and extended account ids",
|
||||
fun() ->
|
||||
?assert(gmser_id:is_account(gmser_id:create(account, ?PUBKEY))),
|
||||
?assert(gmser_id:is_account(gmser_id:create({account, 0}, ?PUBKEY))),
|
||||
?assert(gmser_id:is_account(gmser_id:create({account, 5}, ?PUBKEY))),
|
||||
?assertNot(gmser_id:is_account(gmser_id:create(contract, ?PUBKEY))),
|
||||
?assertNot(gmser_id:is_account(not_an_id))
|
||||
end}.
|
||||
|
||||
account_pubkey_test() ->
|
||||
{"account_pubkey returns the 32-byte account hash",
|
||||
fun() ->
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create(account, ?PUBKEY))),
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create({account, 3}, ?PUBKEY))),
|
||||
?assertEqual(?PUBKEY,
|
||||
gmser_id:account_pubkey(gmser_id:create({account, 6}, ?PUBKEY)))
|
||||
end}.
|
||||
|
||||
account_pubkey_matches_specialize_test() ->
|
||||
{"account_pubkey agrees with specialize/2 for account ids",
|
||||
fun() ->
|
||||
Id = gmser_id:create({account, 2}, ?PUBKEY),
|
||||
?assertEqual(gmser_id:specialize(Id, account),
|
||||
gmser_id:account_pubkey(Id))
|
||||
end}.
|
||||
|
||||
extended_account_roundtrip_test() ->
|
||||
{"extended account ids round-trip through encode/decode",
|
||||
fun() ->
|
||||
Id = gmser_id:create({account, 4}, ?PUBKEY),
|
||||
?assert(gmser_id:is_account(Id)),
|
||||
Id1 = gmser_id:decode(gmser_id:encode(Id)),
|
||||
?assertEqual(?PUBKEY, gmser_id:account_pubkey(Id1)),
|
||||
?assertEqual(account, gmser_id:specialize_type(Id1))
|
||||
end}.
|
||||
@@ -0,0 +1,185 @@
|
||||
%%%-------------------------------------------------------------------
|
||||
%%% @copyright (C) 2026, QPQ AG
|
||||
%%% @doc
|
||||
%%% EUnit tests for gmser_schema_export.
|
||||
%%% @end
|
||||
%%%-------------------------------------------------------------------
|
||||
-module(gmser_schema_export_tests).
|
||||
|
||||
-include_lib("eunit/include/eunit.hrl").
|
||||
|
||||
%% Spend template as in aec_spend_tx (kept local so tests do not
|
||||
%% depend on aecore).
|
||||
spend_template() ->
|
||||
[ {sender_id, id}
|
||||
, {recipient_id, id}
|
||||
, {amount, int}
|
||||
, {gas_price, int}
|
||||
, {gas, int}
|
||||
, {ttl, int}
|
||||
, {nonce, int}
|
||||
, {payload, binary}
|
||||
].
|
||||
|
||||
signed_tx_template() ->
|
||||
[ {signatures, [binary]}
|
||||
, {transaction, binary}
|
||||
].
|
||||
|
||||
name_update_v1_template() ->
|
||||
[ {account_id, id}
|
||||
, {nonce, int}
|
||||
, {name_id, id}
|
||||
, {name_ttl, int}
|
||||
, {pointers, [{binary, id}]}
|
||||
, {client_ttl, int}
|
||||
, {gas_price, int}
|
||||
, {gas, int}
|
||||
, {ttl, int}
|
||||
].
|
||||
|
||||
match(Bin, Re) when is_binary(Bin) ->
|
||||
re:run(Bin, Re, [{capture, none}]).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Naming
|
||||
%%%===================================================================
|
||||
|
||||
type_name_from_registered_tag_test() ->
|
||||
Tag = gmser_chain_objects:tag(spend_tx),
|
||||
?assertEqual(12, Tag),
|
||||
?assertEqual("SpendTxV1", gmser_schema_export:type_name(Tag, 1)).
|
||||
|
||||
type_name_from_atom_test() ->
|
||||
?assertEqual("SpendTxV1",
|
||||
gmser_schema_export:type_name(spend_tx, 12, 1)),
|
||||
?assertEqual("SignedTxV1",
|
||||
gmser_schema_export:type_name(<<"signed_tx">>, 11, 1)).
|
||||
|
||||
type_name_unknown_tag_test() ->
|
||||
?assertEqual("Object9999V2", gmser_schema_export:type_name(9999, 2)).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Single object export
|
||||
%%%===================================================================
|
||||
|
||||
spend_object_export_test() ->
|
||||
Tag = gmser_chain_objects:tag(spend_tx),
|
||||
{ok, "SpendTxV1", Defs} =
|
||||
gmser_schema_export:object_to_asn1(Tag, 1, spend_template()),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"SpendTxV1 ::= SEQUENCE \\{">>)),
|
||||
?assertEqual(match, match(Text, <<"tag INTEGER \\(12\\)">>)),
|
||||
?assertEqual(match, match(Text, <<"vsn INTEGER \\(1\\)">>)),
|
||||
?assertEqual(match, match(Text, <<"senderId Id">>)),
|
||||
?assertEqual(match, match(Text, <<"recipientId Id">>)),
|
||||
?assertEqual(match, match(Text, <<"amount BigInt">>)),
|
||||
?assertEqual(match, match(Text, <<"gasPrice BigInt">>)),
|
||||
?assertEqual(match, match(Text, <<"payload OCTET STRING">>)),
|
||||
%% last field must not have a trailing comma before closing brace
|
||||
?assertEqual(match, match(Text, <<"payload OCTET STRING\n}">>)).
|
||||
|
||||
spend_without_tag_vsn_fields_test() ->
|
||||
Tag = gmser_chain_objects:tag(spend_tx),
|
||||
{ok, "SpendTxV1", Defs} =
|
||||
gmser_schema_export:object_to_asn1(
|
||||
Tag, 1, spend_template(), #{include_tag_vsn => false}),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(nomatch, match(Text, <<"tag INTEGER">>)),
|
||||
?assertEqual(match, match(Text, <<"senderId Id">>)).
|
||||
|
||||
explicit_type_name_test() ->
|
||||
{ok, "MySpendV1", Defs} =
|
||||
gmser_schema_export:object_to_asn1(
|
||||
12, 1, spend_template(), #{type_name => "MySpend"}),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"MySpendV1 ::= SEQUENCE">>)).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Nested types
|
||||
%%%===================================================================
|
||||
|
||||
list_and_tuple_export_test() ->
|
||||
Tag = gmser_chain_objects:tag(name_update_tx),
|
||||
{ok, TypeName, Defs} =
|
||||
gmser_schema_export:object_to_asn1(Tag, 1, name_update_v1_template()),
|
||||
?assertEqual("NameUpdateTxV1", TypeName),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"pointers SEQUENCE OF SEQUENCE \\{">>)),
|
||||
?assertEqual(match, match(Text, <<"c1 OCTET STRING">>)),
|
||||
?assertEqual(match, match(Text, <<"c2 Id">>)).
|
||||
|
||||
signed_tx_list_field_test() ->
|
||||
Tag = gmser_chain_objects:tag(signed_tx),
|
||||
{ok, "SignedTxV1", Defs} =
|
||||
gmser_schema_export:object_to_asn1(Tag, 1, signed_tx_template()),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"signatures SEQUENCE OF OCTET STRING">>)),
|
||||
?assertEqual(match, match(Text, <<"transaction OCTET STRING">>)).
|
||||
|
||||
map_items_export_test() ->
|
||||
Template = [{body, #{items => [{foo, int}, {bar, binary}]}}],
|
||||
{ok, _, Defs} =
|
||||
gmser_schema_export:object_to_asn1(
|
||||
10, 1, Template, #{type_name => account, include_tag_vsn => false}),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"body SEQUENCE \\{">>)),
|
||||
?assertEqual(match, match(Text, <<"foo BigInt">>)),
|
||||
?assertEqual(match, match(Text, <<"bar OCTET STRING">>)).
|
||||
|
||||
uint_types_export_test() ->
|
||||
Template = [{a, uint8}, {b, uint16}, {c, uint32},
|
||||
{d, uint64}, {e, uint128}, {f, bool}],
|
||||
{ok, _, Defs} =
|
||||
gmser_schema_export:object_to_asn1(
|
||||
1, 1, Template,
|
||||
#{type_name => "Uints", include_tag_vsn => false}),
|
||||
Text = iolist_to_binary(Defs),
|
||||
?assertEqual(match, match(Text, <<"a Uint8">>)),
|
||||
?assertEqual(match, match(Text, <<"b Uint16">>)),
|
||||
?assertEqual(match, match(Text, <<"c Uint32">>)),
|
||||
?assertEqual(match, match(Text, <<"d Uint64">>)),
|
||||
?assertEqual(match, match(Text, <<"e Uint128">>)),
|
||||
?assertEqual(match, match(Text, <<"f BOOLEAN">>)).
|
||||
|
||||
%%%===================================================================
|
||||
%%% Full module
|
||||
%%%===================================================================
|
||||
|
||||
module_export_test() ->
|
||||
Objects =
|
||||
[ {spend_tx,
|
||||
gmser_chain_objects:tag(spend_tx),
|
||||
1,
|
||||
spend_template()}
|
||||
, {gmser_chain_objects:tag(signed_tx), 1, signed_tx_template()}
|
||||
],
|
||||
{ok, Iodata} =
|
||||
gmser_schema_export:module_to_asn1('GajumaruChainObjects', Objects),
|
||||
Text = iolist_to_binary(Iodata),
|
||||
?assertEqual(match, match(Text, <<"GajumaruChainObjects DEFINITIONS">>)),
|
||||
?assertEqual(match, match(Text, <<"Id ::= SEQUENCE">>)),
|
||||
?assertEqual(match, match(Text, <<"BigInt ::= INTEGER">>)),
|
||||
?assertEqual(match, match(Text, <<"SpendTxV1 ::= SEQUENCE">>)),
|
||||
?assertEqual(match, match(Text, <<"SignedTxV1 ::= SEQUENCE">>)),
|
||||
?assertEqual(match, match(Text, <<"tag 12 vsn 1 -> SpendTxV1">>)),
|
||||
?assertEqual(match, match(Text, <<"tag 11 vsn 1 -> SignedTxV1">>)),
|
||||
?assertEqual(match, re:run(Text, <<"^END">>, [{capture, none}, multiline])).
|
||||
|
||||
write_module_test() ->
|
||||
Tmp = filename:join(
|
||||
filename:basedir(user_cache, "gmser_schema_export"),
|
||||
"gmser_schema_export_test.asn"),
|
||||
ok = filelib:ensure_dir(Tmp),
|
||||
Objects =
|
||||
[{gmser_chain_objects:tag(spend_tx), 1, spend_template()}],
|
||||
?assertEqual(ok, gmser_schema_export:write_module(
|
||||
Tmp, 'GajumaruChainObjects', Objects)),
|
||||
{ok, Bin} = file:read_file(Tmp),
|
||||
?assertEqual(match, match(Bin, <<"SpendTxV1 ::= SEQUENCE">>)),
|
||||
ok = file:delete(Tmp).
|
||||
|
||||
bad_template_test() ->
|
||||
?assertError({unsupported_template_type, float},
|
||||
gmser_schema_export:object_to_asn1(
|
||||
1, 1, [{x, float}])).
|
||||
Reference in New Issue
Block a user