{"id":20388362,"url":"https://github.com/erwin-kok/multiformat","last_synced_at":"2025-04-12T10:38:03.542Z","repository":{"id":184408061,"uuid":"563427446","full_name":"erwin-kok/multiformat","owner":"erwin-kok","description":"Protocol implementation of https://multiformats.io/","archived":false,"fork":false,"pushed_at":"2024-09-28T11:31:06.000Z","size":358,"stargazers_count":2,"open_issues_count":1,"forks_count":2,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-26T05:33:12.687Z","etag":null,"topics":["ipfs","libp2p","multiformat","p2p"],"latest_commit_sha":null,"homepage":"","language":"Kotlin","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/erwin-kok.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-11-08T15:36:15.000Z","updated_at":"2024-09-28T11:31:10.000Z","dependencies_parsed_at":null,"dependency_job_id":"def11cad-85e7-4834-9340-d04043d078bd","html_url":"https://github.com/erwin-kok/multiformat","commit_stats":null,"previous_names":["erwin-kok/multiformat"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erwin-kok%2Fmultiformat","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erwin-kok%2Fmultiformat/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erwin-kok%2Fmultiformat/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/erwin-kok%2Fmultiformat/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/erwin-kok","download_url":"https://codeload.github.com/erwin-kok/multiformat/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248555511,"owners_count":21123916,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ipfs","libp2p","multiformat","p2p"],"created_at":"2024-11-15T03:09:25.186Z","updated_at":"2025-04-12T10:38:03.511Z","avatar_url":"https://github.com/erwin-kok.png","language":"Kotlin","funding_links":[],"categories":[],"sub_categories":[],"readme":"# multiformat\nSelf-describing values for Future-proofing\n\n[![ci](https://github.com/erwin-kok/multiformat/actions/workflows/ci.yaml/badge.svg)](https://github.com/erwin-kok/multiformat/actions/workflows/ci.yaml)\n[![Maven Central](https://img.shields.io/maven-central/v/org.erwinkok.multiformat/multiformat)](https://central.sonatype.com/artifact/org.erwinkok.result/result-monad)\n[![Kotlin](https://img.shields.io/badge/kotlin-1.9.0-blue.svg?logo=kotlin)](http://kotlinlang.org)\n[![License](https://img.shields.io/github/license/erwin-kok/multiformat.svg)](https://github.com/erwin-kok/multiformat/blob/master/LICENSE)\n\n## Usage\n\nKotlin DSL:\n\n```kotlin\nrepositories {\n    mavenCentral()\n}\n\ndependencies {\n    implementation(\"org.erwinkok.multiformat:multiformat:$latest\")\n}\n```\n\n## Introduction\n\nThis project implements various protocols defined at: https://multiformats.io/\n\nNotably, the following protocols are implemented:\n\n- [multiaddr](https://github.com/multiformats/multiaddr): network addresses\n- [multibase](https://github.com/multiformats/multibase): base encodings\n- [multicodec](https://github.com/multiformats/multicodec): serialization codes\n- [multihash](https://github.com/multiformats/multihash): cryptographic hashes\n- [multistream-select](https://github.com/multiformats/multistream-select): Friendly protocol multiplexing.\n\nNext to this, it also implements Cid: https://github.com/multiformats/cid\n\n\n## Using the Result Monad\n\nThis project is using the [result-monad](https://github.com/erwin-kok/result-monad)\n\nThis means that (almost) all methods of this project return a `Result\u003c...\u003e`. The caller can check whether an error was generated, \nor it can use the value. For example:\n\n```kotlin\nval connection = createConnection()\n    .getOrElse {\n        log.error { \"Could not create connection: ${errorMessage(it)}\" }\n        return Err(it)\n    }\n\nconnection.write(...)\n\n\nfun createConnection(): Result\u003cConnection\u003e {\n  ...\n}\n```\n\nThe advantage is that it is easier (at least for me) to track the flow of the code and to handle correct/error cases. \nThe disadvantage of exceptions is, is that you do not know which statement in a try-block generated the exception. For \nexample:\n\n```kotlin\nvar connection: Connection? = null\ntry {\n    ...\n    connection = createConnection()\n    ...\n    methodThrowingException()\n    ...\n} catch (e: Exception) {\n    if (connection != null) {\n        connection.close()\n    }\n}\n```\n\nIn the catch-block you do not know where the exception was thrown: before or after creating the connection. This means \nthat you do not know if the connection should be closed, or not. Of course there are many ways to solve this.\n\nWith the result-monad you can do something like:\n```kotlin\nval connection = createConnection()\n    .getOrElse {\n        log.error { \"Could not create connection: ${errorMessage(it)}\" }\n        return Err(it)\n    }\n...\nmethodGeneratingError()\n    .onFailure {\n        connection.close()\n        return\n    }\n...\n```\n\n## Usage\n\nA (very) brief description on how to use multiformats:\n\n...but please also look at the various tests.\n\n### multiaddr\n\n```kotlin\nval addr1 = Multiaddress.fromString(\"/ip4/127.0.0.1/p2p/QmcgpsyWgH8Y8ajJz1Cu72KnS5uo2Aa2LpzU7kinSupNKC/tcp/1234\")\n    .getOrElse {\n        log.error { \"Could not parse Multiaddress: ${errorMessage(it)}\" }\n        return Err(it)\n    }\nval ip6Addr = Multiaddress.fromString(\"/ip6/2001:8a0:7ac5:4201:3ac9:86ff:fe31:7095\").getOrThrow()\nval tcpAddr = Multiaddress.fromString(\"/tcp/8000\").getOrThrow()\nval webAddr = Multiaddress.fromString(\"/ws\").getOrThrow()\nval actual1 = Multiaddress.fromString(\"/\").expectNoErrors()\n    .encapsulate(ip6Addr).expectNoErrors()\n    .encapsulate(tcpAddr).expectNoErrors()\n    .encapsulate(webAddr).expectNoErrors()\n    .toString()\n```\n\n### multibase\n\n```kotlin\nval multibase = Multibase.encode(\"base16\", \"foobar\".toByteArray()).getOrThrow()\nval bytes = Multibase.decode(\"f666f6f626172\").getOrThrow()\n```\n\n### multicodec\n```kotlin\nval codec = Multicodec.nameToType(\"cidv2\")\n```\n\n### multihash\n```kotlin\nval multihash = Multihash.fromBase58(\"QmPfjpVaf593UQJ9a5ECvdh2x17XuJYG5Yanv5UFnH3jPE\")\n```\n\n### multistream-select\n\n```kotlin\nval selected = MultistreamMuxer.selectOneOf(setOf(\"/a\", \"/b\", \"/c\"), connection)\n    .getOrElse {\n        log.error { \"Error selecting protocol: ${errorMessage(it)}\" }\n        return Err(it)\n    }\n```\n\n\n## Sub-modules\n\nThis project has three submodules:\n\n```shell\ngit submodule add https://github.com/multiformats/multicodec src/main/kotlin/org/erwinkok/multiformat/spec/multicodec\ngit submodule add https://github.com/multiformats/multibase src/main/kotlin/org/erwinkok/multiformat/spec/multibase\ngit submodule add https://github.com/multiformats/multihash src/main/kotlin/org/erwinkok/multiformat/spec/multihash\n```\n\nThese are the official specifications repositories, which are used here for auto-generation code and or verifying the \ntest results are according to spec.\n\n## Contributing\n\nBug reports and pull requests are welcome on [GitHub](https://github.com/erwin-kok/multiformat).\n\n## Contact\n\nIf you want to contact me, please write an e-mail to: [erwin.kok(at)protonmail.com](mailto:erwin.kok@protonmail.com)\n\n## License\n\nThis project is licensed under the BSD-3-Clause license, see [`LICENSE`](LICENSE) file for more details. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferwin-kok%2Fmultiformat","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ferwin-kok%2Fmultiformat","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ferwin-kok%2Fmultiformat/lists"}