11# Copper Multicodec
22
3- Copper Multicodec is a Java library that implements [ Multicodec] ( https://github.com/multiformats/multicodec ) and [ Multihash] ( https://github.com/multiformats/multihash ) , enabling efficient encoding and decoding of data.
3+ A multicodec is a ** self-describing** format: encoded data always begins
4+ with a varint code that unambiguously identifies its format and intended
5+ purpose.
6+
7+ This design eliminates guesswork, ensures interoperability between systems,
8+ and makes it possible to introduce new formats without breaking existing
9+ implementations.
10+
11+ The multicodec identifier tells applications how the data is meant to be
12+ interpreted and processed.
13+
14+ Each multicodec is tagged to reflect its purpose - for example, hash functions (e.g., ` sha2-256 ` ), cryptography keys (e.g., ` ed25519-pub ` ), and other identifiers used across the multiformats ecosystem.
15+
16+ Copper Multicodec is a Java library implementing
17+ [ Multicodec] ( https://github.com/multiformats/multicodec ) and
18+ [ Multihash] ( https://github.com/multiformats/multihash ) , enabling efficient
19+ encoding and decoding of self-describing data formats.
420
521[ ![ Java 8 CI] ( https://github.com/filip26/copper-multicodec/actions/workflows/java8-build.yml/badge.svg )] ( https://github.com/filip26/copper-multicodec/actions/workflows/java8-build.yml )
622[ ![ Quality Gate Status] ( https://sonarcloud.io/api/project_badges/measure?project=filip26_copper-multicodec&metric=alert_status )] ( https://sonarcloud.io/summary/new_code?id=filip26_copper-multicodec )
@@ -10,14 +26,12 @@ Copper Multicodec is a Java library that implements [Multicodec](https://github.
1026
1127## Features
1228
13- - ** Static Codec Registry:** Predefined codecs for efficient access.
14- - ** Direct Static Access to Codecs:** Eliminates runtime searches for codecs.
15- - ** Custom Codec Support:** Allows customization of supported codecs.
16- - ** Multihash Support:** Provides compatibility with multihash encoding.
17- - ** Unsigned VarInt Support:** Handles unsigned variable-length integers.
18- - ** Zero Third-Party Dependencies:** Ensures a lightweight and self-contained implementation.
19-
20- [ Supported Codecs] ( https://github.com/filip26/copper-multicodec/tree/main/src/main/java/com/apicatalog/multicodec/codec )
29+ - Static codec registry with predefined codecs for efficient access
30+ - Direct static access to codecs without runtime lookups
31+ - Support for custom codecs through extension
32+ - Multihash compatibility
33+ - Unsigned VarInt (UVarInt) encoding and decoding
34+ - Zero third-party dependencies for a lightweight, self-contained implementation
2135
2236## Examples
2337
@@ -41,8 +55,9 @@ var decoder = MulticodecDecoder.getInstance(codecs...);
4155byte [] decoded = decoder. decode(encoded);
4256
4357/* or check if encoding is supported */
44- Multicodec codec = decoder. getCodec(encoded). orElseThrow(() - > new IllegalArgumentException (" Unsupported codec." ));
45- byte [] decoded = codec. decode(encoded);
58+ byte [] decoded = decoder. getCodec(encoded)
59+ .map(codec - > codec. decode(encoded))
60+ .orElseThrow(() - > new IllegalArgumentException (" Unsupported codec." ));
4661
4762/* or directly when only one codec is supported */
4863byte [] decoded = KeyCodec . P521_PUBLIC_KEY. decode(encoded);
@@ -65,9 +80,9 @@ var registry = MulticodecRegistry.getInstance(Tag.Key, Tag.Hash);
6580var registry = MulticodecRegistry . getInstance(codecs. .. );
6681
6782/* get codec */
68- var codec = registry. getCodec(code). orElseThrow(() - > new IllegalArgumentException ( " Unsupported codec. " ));
69- byte [] encoded = codec. encode(input);
70-
83+ byte [] encoded = registry. getCodec(code)
84+ .map( codec - > codec . encode(input))
85+ .orElseThrow(() - > new IllegalArgumentException ( " Unsupported codec. " ));
7186```
7287
7388### Multihash
@@ -80,8 +95,9 @@ var decoder = MulticodecDecoder.getInstance(Tag.Multihash);
8095byte [] decoded = decoder. decode(encoded);
8196
8297/* or check if supported */
83- var codec = decoder. get(encoded). orElseThrow(() - > new IllegalArgumentException (" Unsupported multihash." ));
84- byte [] decoded = codec. decode(encoded);
98+ byte [] decoded = decoder. getCodec(encoded)
99+ .map(codec - > codec. decode(encoded))
100+ .orElseThrow(() - > new IllegalArgumentException (" Unsupported multihash." ));
85101
86102/* or directly */
87103byte [] decoded = MultihashCodec . SHA2_384. decode(encoded);
@@ -97,12 +113,11 @@ if (MultihashCodec.SHA2_384.isEncoded(encoded)) {
97113var registry = MulticodecRegistry . getInstance(Tag . Multihash );
98114
99115/* encode an input as multihash */
100- var codec = registry. get (code). orElseThrow(() - > new IllegalArgumentException ( " Unsupported multihash. " ));
101- byte [] encoded = codec. encode(input);
102-
116+ byte [] encoded = registry. getCodec (code)
117+ .map( codec - > codec . encode(input))
118+ .orElseThrow(() - > new IllegalArgumentException ( " Unsupported multihash. " ));
103119```
104120
105-
106121## Installation
107122
108123### Maven
@@ -117,6 +132,27 @@ To include Copper Multicodec in your project, add the following dependency to yo
117132</dependency >
118133```
119134
135+ ## ld-cli
136+
137+ [ ld-cli] ( https://github.com/filip26/ld-cli ) is a command-line utility for
138+ working with multiformats including multibase, multicodec, and multihash,
139+ as well as JSON-LD and related specifications.
140+
141+ It provides encoding, decoding, detection, analysis, and format conversion
142+ features, making it useful for inspecting identifiers, testing content
143+ addressing, and integrating multiformats into development workflows.
144+
145+ ### Example
146+
147+ Detect and analyze a multibase + multicodec value
148+ ``` bash
149+ > ld-cli multicodec --analyze --multibase <<< ' z6MkmM42vxfqZQsv4ehtTjFFxQ4sQKS2w6WR7emozFAn5cxu'
150+
151+ Multibase: name=base58btc, prefix=z, length=58 chars
152+ Multicodec: name=ed25519-pub, code=237, varint=[0xED,0x01], tag=Key, status=Draft
153+ Length: 32 bytes
154+ ```
155+
120156## Contributing
121157
122158All PR's welcome!
@@ -131,8 +167,14 @@ Fork and clone the project repository.
131167> mvn clean package
132168```
133169
134- ## Additional Resources
135- - [ Copper Multibase ] ( https://github.com/filip26/copper-multibase )
170+ ## Resources
171+ - [ Codecs Registry ] ( https://github.com/multiformats/multicodec/blob/master/table.csv )
136172- [ Multicodec] ( https://github.com/multiformats/multicodec )
137173- [ Multihash] ( https://github.com/multiformats/multihash )
138174- [ unsigned-varint] ( https://github.com/multiformats/unsigned-varint )
175+ - [ Copper Multibase] ( https://github.com/filip26/copper-multibase )
176+
177+ ## Commercial Support
178+
179+ Commercial support and consulting are available.
180+ For inquiries, please contact: filip26@gmail.com
0 commit comments