Search code examples
bittorrentlibtorrent

Libtorrent Get File by Merkle hash


How can I request file knowing its Merkle hash via libtorrent? I'll be glad to see a piece of code or maybe just description of the logic. Do I need to download .torrent file based on the Merkle hash


Solution

  • First of all, there are two BEPs definining merkle-tree based torrents. BEP30 and BEP52. Neither enables sharing a file based on its merkle hash alone as they require some additional data to build a minimal viable torrent such as the piece size or the file size. To support multiple files, file names and other things some additional metadata is needed which ultimately gets encapsulated in the infohash. So torrent clients generally deal in infohashes, not file hashes.

    You can bypass some of these requirements by choosing a fixed filename and fixed piece size and then generate a canonical metadata representation for your file. In libtorrent that could be done through the create_torrent() API by creating a v2-only torrent (BEP52 without backwards compatibility).