{"id":28700790,"url":"https://github.com/edjcase/motoko_dag_cbor","last_synced_at":"2026-01-31T18:05:14.668Z","repository":{"id":298185963,"uuid":"999098621","full_name":"edjCase/motoko_dag_cbor","owner":"edjCase","description":"A DAG-CBOR implementation for IPLD in Motoko","archived":false,"fork":false,"pushed_at":"2025-09-19T00:08:12.000Z","size":58,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-09-19T02:29:06.744Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Motoko","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/edjCase.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,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2025-06-09T18:27:58.000Z","updated_at":"2025-09-19T00:08:13.000Z","dependencies_parsed_at":"2025-06-09T21:33:20.767Z","dependency_job_id":"4418a68a-d92d-4369-abac-25d2e369ba8f","html_url":"https://github.com/edjCase/motoko_dag_cbor","commit_stats":null,"previous_names":["edjcase/motoko_dag_cbor"],"tags_count":0,"template":false,"template_full_name":"edjCase/motoko-library-template","purl":"pkg:github/edjCase/motoko_dag_cbor","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_dag_cbor","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_dag_cbor/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_dag_cbor/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_dag_cbor/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edjCase","download_url":"https://codeload.github.com/edjCase/motoko_dag_cbor/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_dag_cbor/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":28949274,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-01-31T14:26:55.697Z","status":"ssl_error","status_checked_at":"2026-01-31T14:26:52.545Z","response_time":128,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":[],"created_at":"2025-06-14T11:41:03.582Z","updated_at":"2026-01-31T18:05:14.661Z","avatar_url":"https://github.com/edjCase.png","language":"Motoko","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Motoko DAG-CBOR\n\n[![MOPS](https://img.shields.io/badge/MOPS-dag--cbor-blue)](https://mops.one/dag-cbor)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/yourusername/motoko_dagCbor/blob/main/LICENSE)\n\nA Motoko implementation of DAG-CBOR (Deterministic CBOR) for encoding and decoding structured data with content addressing support.\n\n## Package\n\n### MOPS\n\n```bash\nmops add dag-cbor\n```\n\nTo set up MOPS package manager, follow the instructions from the [MOPS Site](https://mops.one)\n\n## What is DAG-CBOR?\n\nDAG-CBOR is a strict subset of CBOR (Concise Binary Object Representation) designed for content addressing. It ensures deterministic encoding by enforcing specific rules like sorted map keys, restricted floating-point values, and limited tag usage.\n\n## Supported Features\n\n- **Deterministic encoding/decoding** of structured data\n- **Content addressing support** with CID (Content Identifier) integration\n- **Type-safe value representation** with comprehensive error handling\n- **CBOR compliance** with DAG-CBOR restrictions:\n  - Map keys must be strings and sorted lexicographically\n  - Only tag 42 allowed (for CIDs)\n  - No IEEE 754 special values (NaN, Infinity)\n  - 64-bit integers and floats only\n- **Streaming support** with buffer-based encoding\n- **Full round-trip fidelity** between Motoko types and binary format\n\n## Quick Start\n\n### Example 1: Basic Encoding and Decoding\n\n```motoko\nimport DagCbor \"mo:dag-cbor\";\n\n// Create a simple value\nlet value : DagCbor.Value = #map([\n    (\"name\", #text(\"Alice\")),\n    (\"age\", #int(30)),\n    (\"active\", #bool(true))\n]);\n\n// Encode to bytes\nlet bytes: [Nat8] = switch (DagCbor.toBytes(value)) {\n    case (#err(error)) Runtime.trap(\"Encoding failed: \" # debug_show(error));\n    case (#ok(bytes)) bytes;\n};\n\n// Decode to value\nlet dagValue : DagCbor.Value = switch (DagCbor.fromBytes(bytes.vals())) {\n    case (#err(error)) Runtime.trap(\"Decoding failed: \" # debug_show(error));\n    case (#ok(v)) v;\n};\n```\n\n### Example 2: Buffer-based Encoding with Byte Count\n\n```motoko\nimport DagCbor \"mo:dag-cbor\";\nimport List \"mo:core/List\";\nimport Buffer \"mo:buffer\";\n\nlet value : DagCbor.Value = #text(\"Hello, World!\");\n\n// Create a buffer for streaming encoding\nlet buffer = List.emtpy\u003cNat8\u003e();\n\n// Encode to buffer and get bytes written count\nlet bytesWritten: Nat = switch (DagCbor.toBytesBuffer(Buffer.fromList(buffer), value)) {\n    case (#err(error)) Runtime.trap(\"Encoding failed: \" # debug_show(error));\n    case (#ok(count)) count;\n};\n\n// Buffer now contains the encoded data\nlet encodedBytes = List.toArray(buffer);\n```\n\n### Example 3: To/From Cbor Value\n\n````motoko\nimport DagCbor \"mo:dag-cbor\";\nimport Cbor \"mo:cbor\";\n\nlet value : DagCbor.Value = ...;\n\n// Encode to bytes\nlet cborValue: Cbor.Value = switch (DagCbor.toCbor(value)) {\n    case (#err(error)) Runtime.trap(\"toCbor failed: \" # debug_show(error));\n    case (#ok(v)) v;\n};\n\n// Decode to value\nlet dagValue : DagCbor.Value = switch (DagCbor.fromCbor(cborValue)) {\n    case (#err(error)) Runtime.trap(\"fromCbor failed: \" # debug_show(error));\n    case (#ok(v)) v;\n};\n\n## API Reference\n\n### Main Functions\n\n- **`toBytes()`** - Converts DAG-CBOR values to binary format\n- **`fromBytes()`** - Converts binary data back to DAG-CBOR values\n- **`toBytesBuffer()`** - Streams encoding to a buffer and returns bytes written count\n- **`toCbor()`** / **`fromCbor()`** - Low-level CBOR conversion functions\n\n### Types\n\n```motoko\n// Main value type supporting all DAG-CBOR data types\npublic type Value = {\n    #int : Int;           // Signed integers (64-bit range)\n    #bytes : [Nat8];      // Binary data\n    #text : Text;         // UTF-8 strings\n    #array : [Value];     // Ordered arrays\n    #map : [(Text, Value)]; // Key-value maps (keys must be strings, sorted)\n    #cid : CID.CID;           // Content Identifiers\n    #bool : Bool;         // Boolean values\n    #null_;               // Null value\n    #float : Float;       // 64-bit floating point (no NaN/Infinity)\n};\n\n// DAG-CBOR specific encoding errors\npublic type DagToCborError = {\n    #invalidValue : Text;       // Value violates DAG-CBOR rules\n    #invalidMapKey : Text;      // Map key is invalid\n    #unsortedMapKeys;           // Map keys are not sorted\n};\n\n// DAG-CBOR specific decoding errors\npublic type CborToDagError = {\n    #invalidTag : Nat64;        // Unsupported CBOR tag\n    #invalidMapKey : Text;      // Non-string map key\n    #invalidCIDFormat : Text;   // Malformed CID\n    #unsupportedPrimitive : Text; // Unsupported CBOR primitive\n    #floatConversionError : Text; // Invalid float value\n    #integerOutOfRange : Text;  // Integer outside 64-bit range\n};\n\n// Combined encoding error type\npublic type DagEncodingError = DagToCborError or {\n    #cborEncodingError : Cbor.EncodingError;\n};\n\n// Combined decoding error type\npublic type DagDecodingError = CborToDagError or {\n    #cborDecodingError : Cbor.DecodingError;\n};\n````\n\n### Functions\n\n```motoko\n// Encode a DAG-CBOR value to bytes\npublic func toBytes(value : Value) : Result.Result\u003c[Nat8], DagEncodingError\u003e;\n\n// Encode a DAG-CBOR value to an existing buffer (returns bytes written count)\npublic func toBytesBuffer(buffer : Buffer.Buffer\u003cNat8\u003e, value : Value) : Result.Result\u003cNat, DagEncodingError\u003e;\n\n// Decode bytes to a DAG-CBOR value\npublic func fromBytes(bytes : Iter.Iter\u003cNat8\u003e) : Result.Result\u003cValue, DagDecodingError\u003e;\n\n// Convert DAG-CBOR value to underlying CBOR value\npublic func toCbor(value : Value) : Result.Result\u003cCbor.Value, DagToCborError\u003e;\n\n// Convert underlying CBOR value to DAG-CBOR value\npublic func fromCbor(cborValue : Cbor.Value) : Result.Result\u003cValue, CborToDagError\u003e;\n```\n\n## DAG-CBOR Rules and Restrictions\n\nThis implementation enforces the following DAG-CBOR rules:\n\n1. **Map Keys**: Must be strings and sorted lexicographically by UTF-8 byte representation\n2. **Tags**: Only tag 42 is allowed (used for CIDs)\n3. **Integers**: Must fit in 64-bit signed range (-2^63 to 2^63-1)\n4. **Floats**: Must be 64-bit IEEE 754, no NaN, Infinity, or -Infinity\n5. **Deterministic Encoding**: Same logical value always produces identical bytes\n6. **No Duplicate Keys**: Map keys must be unique\n\n## License\n\nThis project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_dag_cbor","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedjcase%2Fmotoko_dag_cbor","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_dag_cbor/lists"}