#peer-id #bittorrent #minimalist #u8 #byte #across #tdyne

tdyne-peer-id

Basic Peer ID type used across BitTorrent related libraries

3 stable releases

1.0.2 Oct 6, 2023

#846 in Network programming

Download history 412/week @ 2024-01-02 265/week @ 2024-01-09 372/week @ 2024-01-16 590/week @ 2024-01-23 447/week @ 2024-01-30 224/week @ 2024-02-06 304/week @ 2024-02-13 179/week @ 2024-02-20 180/week @ 2024-02-27 142/week @ 2024-03-05 248/week @ 2024-03-12 644/week @ 2024-03-19 370/week @ 2024-03-26 320/week @ 2024-04-02 517/week @ 2024-04-09 349/week @ 2024-04-16

1,672 downloads per month
Used in tdyne-peer-id-registry

MIT/Apache

9KB
87 lines

Base type for BitTorrent peer IDs

tdyne_peer_id is a newtype for BitTorrent peer IDs, represented as [u8; 20]. It's intentionally kept very minimalist to minimise the possibility of backwards-incompatible changes.

[dependencies]
tdyne-peer-id = "1"

Example

use tdyne_peer_id::{PeerId, BadPeerIdLengthError};

fn main() {
    let byte_array: &[u8; 20] = b"-TR0000-*\x00\x01d7xkqq04n";
    let byte_slice: &[u8] = b"-TR0000-*\x00\x01d7xkqq04n";
    let short_byte_slice: &[u8] = b"-TR0000-";

    // creating a PeerId from an array is simple
    let peer_id = PeerId::from(b"-TR0000-*\x00\x01d7xkqq04n");
    assert_eq!(peer_id.to_string(), "-TR0000-???d7xkqq04n".to_string());

    // you can also create PeerId from a byte slice if its 20 bytes long
    _ = PeerId::try_from(byte_slice).expect("matching lengths");

    // …if it's not, you get an error
    let error = BadPeerIdLengthError(short_byte_slice.len());
    assert_eq!(PeerId::try_from(short_byte_slice).expect_err("lengths don't match"), error);
}

Libraries and projects using tdyne_peer_id

tdyne_peer_id is opensourced from TORRENTDYNE, a BitTorrent diagnostic service.

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps