{"id":29562219,"url":"https://github.com/edjcase/motoko_car","last_synced_at":"2026-02-06T02:05:06.268Z","repository":{"id":304444787,"uuid":"1018156552","full_name":"edjCase/motoko_car","owner":"edjCase","description":"A Motoko library for encoding/decoding a CAR file from IPLD","archived":false,"fork":false,"pushed_at":"2025-08-01T20:04:58.000Z","size":9,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2025-08-01T22:30:17.348Z","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}},"created_at":"2025-07-11T17:48:40.000Z","updated_at":"2025-08-01T20:05:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"39abe95f-d91d-4d9b-81b4-7013c7216640","html_url":"https://github.com/edjCase/motoko_car","commit_stats":null,"previous_names":["edjcase/motoko_car"],"tags_count":0,"template":false,"template_full_name":"edjCase/motoko-library-template","purl":"pkg:github/edjCase/motoko_car","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_car","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_car/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_car/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_car/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/edjCase","download_url":"https://codeload.github.com/edjCase/motoko_car/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/edjCase%2Fmotoko_car/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29145565,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-06T01:13:33.096Z","status":"online","status_checked_at":"2026-02-06T02:00:08.092Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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-07-18T17:10:54.958Z","updated_at":"2026-02-06T02:05:06.263Z","avatar_url":"https://github.com/edjCase.png","language":"Motoko","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Motoko CAR Library\n\n[![MOPS](https://img.shields.io/badge/MOPS-car-blue)](https://mops.one/car)\n[![License](https://img.shields.io/badge/license-MIT-blue.svg)](https://github.com/edjCase/motoko_car/blob/main/LICENSE)\n\nA Motoko library for working with Content Addressable Archive (CAR) files used in IPFS, IPLD, and AT Protocol. CAR files are a binary format for storing and transmitting content-addressed data, containing both a header with metadata and a sequence of blocks with their corresponding Content Identifiers (CIDs).\n\n## Package\n\n### MOPS\n\n```bash\nmops add car\n```\n\nTo set up MOPS package manager, follow the instructions from the [MOPS Site](https://mops.one)\n\n## Quick Start\n\n### Import\n\n```motoko\nimport CAR \"mo:car\"\nimport CID \"mo:cid\"\n```\n\n### Example 1: Creating and Encoding a CAR File\n\n```motoko\n// Create some sample blocks\nlet blocks = [\n  {\n    cid = #v1({\n      codec = #dagCbor;\n      hash = \"\\F8\\8B\\C8\\53\\80\\4C\\F2\\94\\FE\\41\\7E\\4F\\A8\\30\\28\\68\\9F\\CD\\B1\\B1\\59\\2C\\51\\02\\E1\\47\\4D\\BC\\20\\0F\\AB\\8B\";\n      hashAlgorithm = #sha2256;\n    });\n    data = \"\\A2\\64\\6C\\69\\6E\\6B\\D8\\2A\\58\\23\\00\\12\\20\\02\\AC\\EC\\C5\";\n  },\n  {\n    cid = #v0({\n      hash = \"\\02\\AC\\EC\\C5\\DE\\24\\38\\EA\\41\\26\\A3\\01\\0E\\CB\\1F\\8A\\59\\9C\\8E\\FF\\22\\FF\\F1\\A1\\DC\\FF\\E9\\99\\B2\\7F\\D3\\DE\";\n    });\n    data = \"\\12\\2E\\0A\\24\\01\\55\\12\\20\\B6\\FB\\D6\\75\\F9\\8E\";\n  }\n];\n\n// Create a CAR file with header and blocks\nlet carFile : CAR.File = {\n  header = {\n    version = 1;\n    roots = [\n      #v1({\n        codec = #dagCbor;\n        hash = \"\\F8\\8B\\C8\\53\\80\\4C\\F2\\94\\FE\\41\\7E\\4F\\A8\\30\\28\\68\\9F\\CD\\B1\\B1\\59\\2C\\51\\02\\E1\\47\\4D\\BC\\20\\0F\\AB\\8B\";\n        hashAlgorithm = #sha2256;\n      })\n    ];\n  };\n  blocks = blocks;\n};\n\n// Encode to bytes\nlet carBytes = CAR.toBytes(carFile);\nDebug.print(\"CAR file encoded to \" # Nat.toText(carBytes.size()) # \" bytes\");\n```\n\n### Example 2: Parsing a CAR File from Bytes\n\n```motoko\n// Parse CAR file from bytes\nswitch (CAR.fromBytes(carBytes.vals())) {\n  case (#ok(parsedFile)) {\n    Debug.print(\"Successfully parsed CAR file\");\n    Debug.print(\"Version: \" # Nat.toText(parsedFile.header.version));\n    Debug.print(\"Root CIDs: \" # Nat.toText(parsedFile.header.roots.size()));\n    Debug.print(\"Blocks: \" # Nat.toText(parsedFile.blocks.size()));\n  };\n  case (#err(error)) {\n    Debug.print(\"Failed to parse CAR file: \" # error);\n  };\n};\n```\n\n### Example 3: Working with Multiple Root CIDs\n\n```motoko\n// Create a CAR file with multiple root CIDs\nlet multiRootCarFile : CAR.File = {\n  header = {\n    version = 1;\n    roots = [\n      #v1({\n        codec = #dagCbor;\n        hash = \"\\F8\\8B\\C8\\53\\80\\4C\\F2\\94\\FE\\41\\7E\\4F\\A8\\30\\28\\68\\9F\\CD\\B1\\B1\\59\\2C\\51\\02\\E1\\47\\4D\\BC\\20\\0F\\AB\\8B\";\n        hashAlgorithm = #sha2256;\n      }),\n      #v1({\n        codec = #dagCbor;\n        hash = \"\\69\\EA\\07\\40\\F9\\80\\7A\\28\\F4\\D9\\32\\C6\\2E\\7C\\1C\\83\\BE\\05\\5E\\55\\07\\2C\\90\\26\\6A\\B3\\E7\\9D\\F6\\3A\\36\\5B\";\n        hashAlgorithm = #sha2256;\n      })\n    ];\n  };\n  blocks = [\n    // ... your blocks here\n  ];\n};\n\n// Process each root CID\nfor (rootCid in multiRootCarFile.header.roots.vals()) {\n  switch (rootCid) {\n    case (#v0(cidV0)) {\n      Debug.print(\"Root CIDv0: \" # CID.V0.toText(cidV0));\n    };\n    case (#v1(cidV1)) {\n      Debug.print(\"Root CIDv1: \" # CID.V1.toText(cidV1, #base32));\n    };\n  };\n};\n```\n\n## Features\n\n### CAR File Format Support\n\n- **CAR v1**: Full support for Content Addressable Archive format version 1\n- **Header encoding/decoding**: Using DAG-CBOR format with version and root CIDs\n- **Block storage**: Efficient storage of content-addressed blocks with their CIDs\n- **Varint encoding**: LEB128 variable-length integer encoding for sizes\n\n### CID Compatibility\n\n- **CIDv0**: Legacy format support for IPFS compatibility\n- **CIDv1**: Modern format with multiple codecs and hash algorithms\n- **Mixed CID versions**: Support for files containing both CIDv0 and CIDv1 blocks\n\n### Supported Codecs\n\n- `#raw`: Raw binary data blocks\n- `#dagPb`: DAG-PB (Protocol Buffers) for IPFS compatibility\n- `#dagCbor`: DAG-CBOR for structured data\n\n### Supported Hash Algorithms\n\n- `#sha2256`: SHA-256 (32 bytes) - most common\n- `#sha2512`: SHA-512 (64 bytes)\n\n## Use Cases\n\n### IPFS Integration\n\nCAR files are the standard format for importing/exporting data from IPFS nodes and for data transfer between IPFS systems.\n\n### AT Protocol (Bluesky)\n\nCAR files are used extensively in the AT Protocol for repository synchronization and data exchange.\n\n### Content Distribution\n\nEfficient packaging of related content-addressed blocks for distribution and storage.\n\n### Archival Storage\n\nLong-term storage of content-addressed data with merkle-tree verification.\n\n## API Reference\n\n### Types\n\n```motoko\n// Main CAR file structure\npublic type File = {\n    header : Header;\n    blocks : [Block];\n};\n\n// Individual block with CID and data\npublic type Block = {\n    cid : CID.CID;  // Content Identifier\n    data : Blob;    // Raw block data\n};\n\n// CAR file header\npublic type Header = {\n    version : Nat;      // CAR format version (currently 1)\n    roots : [CID.CID]; // Array of root CIDs\n};\n```\n\n### Main Functions\n\n```motoko\n// Encode CAR file to binary format\npublic func toBytes(file : File) : [Nat8];\n\n// Parse CAR file from binary format\npublic func fromBytes(bytes : Iter.Iter\u003cNat8\u003e) : Result.Result\u003cFile, Text\u003e;\n```\n\n### Binary Format\n\nThe CAR binary format consists of:\n\n1. **Header Length**: LEB128-encoded length of the header\n2. **Header Data**: DAG-CBOR encoded header containing version and root CIDs\n3. **Blocks**: Sequence of blocks, each with:\n   - Block size (LEB128-encoded)\n   - CID bytes\n   - Block data\n\n### Error Handling\n\nThe library provides detailed error messages for:\n\n- Invalid header format or missing fields\n- Malformed CID data\n- Incorrect varint encoding\n- DAG-CBOR parsing errors\n\n## Dependencies\n\nThis library depends on several Motoko packages:\n\n- `mo:cid` - Content Identifier support\n- `mo:dag-cbor` - DAG-CBOR encoding/decoding\n- `mo:core` - Core utilities (Result, Iter, etc.)\n- `mo:leb128` - Variable-length integer encoding\n- `mo:xtended-iter` - Extended iterator utilities\n- `mo:buffer` - Buffer utilities\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_car","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fedjcase%2Fmotoko_car","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fedjcase%2Fmotoko_car/lists"}