5 releases

0.1.16-alpha.0 Mar 31, 2023
0.1.14-alpha.0 Mar 18, 2023
0.1.13-alpha.0 Mar 17, 2023
0.1.12-alpha.0 Jan 18, 2023
0.1.10-alpha.0 Jan 18, 2023

#18 in #ipv4-address

Download history 117/week @ 2024-08-31 86/week @ 2024-09-07 63/week @ 2024-09-14 105/week @ 2024-09-21 96/week @ 2024-09-28 66/week @ 2024-10-12 56/week @ 2024-10-19 74/week @ 2024-10-26 69/week @ 2024-11-02 24/week @ 2024-11-09 36/week @ 2024-11-16 96/week @ 2024-11-23 116/week @ 2024-11-30 178/week @ 2024-12-07 103/week @ 2024-12-14

493 downloads per month
Used in 54 crates (10 directly)

MIT license

1MB
2.5K SLoC

bitcoin-addr

Description

This Rust crate is a component of the Bitcoin system and is currently being translated from C++. It provides functionality related to addresses, such as creating and manipulating Bitcoin network addresses, determining address validity and reachability, and serializing and unserializing addresses.

Bitcoin addresses are a fundamental component of the Bitcoin system, used for sending and receiving Bitcoin payments. The Bitcoin network is a peer-to-peer network of nodes, with each node identified by a unique network address. Bitcoin addresses are derived from public keys using various cryptographic algorithms, such as the Elliptic Curve Digital Signature Algorithm (ECDSA).

In this crate, addresses are represented as a data structure that contains information about the address's version, network, IP address, and port number. The crate provides functions for creating new addresses, manipulating existing addresses, and serializing and unserializing addresses for network communication.

The crate also includes functions for checking the validity and reachability of Bitcoin addresses, as well as determining whether an address is relayable or routable. These functions use various mathematical ideas and algorithms, such as bitwise operations, hashing, and subnetting.

Additionally, the crate provides functions for working with various address types, including IPv4, IPv6, Tor, I2P, and various RFC-defined address types. These functions determine the type of an address based on its format and contents, and can be used to determine the reachability and validity of various types of addresses.

Note that some of the function bodies in this crate are currently in the process of being translated from C++, and may not yet be fully functional or optimized.

Tokens and Mathematical Ideas

  • Address: A data structure representing a Bitcoin network address, including version, network, IP address, and port number.

  • addr: A function that returns a reference to the IP address contained within an Address.

  • addr_mut: A function that returns a mutable reference to the IP address contained within an Address.

  • create_addresses: A function that creates a vector of Addresses from a list of IP addresses and port numbers.

  • default: A function that returns a default Address with the IP address set to 0.0.0.0 and the port number set to 0.

  • eq: An implementation of the equality operator for Addresses.

  • get_addr_bytes: A function that returns the bytes of an Address in network byte order.

  • get_bip155network: A function that returns the BIP155 network identifier associated with an Address.

  • get_group: A function that returns the group identifier associated with an Address.

  • get_hash: A function that returns the hash of an Address.

  • get_in_6addr: A function that returns the IPv6 address contained within an Address.

  • get_in_addr: A function that returns the IPv4 address contained within an Address.

  • get_key: A function that returns the key associated with an Address.

  • get_linked_ipv4: A function that returns the linked IPv4 address associated with an Address.

  • get_mappedas: A function that returns the mapped IPv4 address associated with an Address.

  • get_net_class: A function that returns the class of network associated with an Address.

  • get_network: A function that returns the network identifier associated with an Address.

  • get_port: A function that returns the port number associated with an Address.

  • get_reachability_from: A function that determines the reachability of an Address from a given network.

  • get_sock_addr: A function that returns a socket address for an Address.

  • has_linked_ipv4: A function...

2> thanks! could you please finish?

Certainly, here is the rest of the crate description:

Address

Address is a Rust struct representing a Bitcoin address. It is in the process of being translated from C++ to Rust, and as such, some of its function bodies may still be in the process of translation.

The Address struct is used to represent a Bitcoin address and contains several functions for validating and manipulating addresses. It is a crucial component of the Bitcoin system and is used extensively in Bitcoin transactions.

Mathematical analysis:

Bitcoin addresses are generated using a public-private key pair, where the public key is hashed using SHA-256 and RIPEMD-160 to generate a 160-bit address. This address is represented in Base58 encoding and includes a checksum for error detection.

The mathematical operations involved in generating a Bitcoin address include hashing and encoding. The SHA-256 and RIPEMD-160 algorithms are used to generate the hash, while Base58 encoding is used to represent the address. The checksum is calculated using the first 4 bytes of the double SHA-256 hash of the address.

addr, addr_mut

The addr and addr_mut functions are used to retrieve the address of an Address struct. The addr function returns an immutable reference to the address, while the addr_mut function returns a mutable reference to the address.

create_addresses

The create_addresses function is used to create a vector of addresses from a list of IP addresses and ports.

default

The default function is used to create a default instance of the Address struct.

eq

The eq function is used to compare two Address structs for equality.

get_addr_bytes

The get_addr_bytes function is used to retrieve the raw bytes of the address contained in an Address struct.

get_bip155network

The get_bip155network function is used to retrieve the BIP155 network associated with an Address struct.

get_group

The get_group function is used to retrieve the group associated with an Address struct.

get_hash

The get_hash function is used to retrieve the hash of the address contained in an Address struct.

get_in_6addr

The get_in_6addr function is used to retrieve the IPv6 address associated with an Address struct.

get_in_addr

The get_in_addr function is used to retrieve the IPv4 address associated with an Address struct.

3> thanks! please continue

is_internal

The is_internal function returns true if the address is an internal address. An internal address is any address belonging to a private network, which is a network reserved for use within an organization.

is_local

The is_local function returns true if the address is a link-local address. A link-local address is a type of address used for communication within a single network segment.

is_reachable

The is_reachable function returns true if the address is reachable, which means that a connection can be established to the address.

is_routable

The is_routable function returns true if the address is routable, which means that the address can be reached from any point on the internet.

is_tor

The is_tor function returns true if the address is a Tor onion service address.

is_valid

The is_valid function returns true if the address is a valid Bitcoin address.

iscjdns

The iscjdns function returns true if the address is a cjdns address.

isi2p

The isi2p function returns true if the address is an i2p address.

isrfc1918

The isrfc1918 function returns true if the address is within the RFC 1918 private network address space.

isrfc2544

The isrfc2544 function...

4> thanks! please continue

This crate also contains several boolean functions that check if an address meets certain criteria. These criteria include whether the address is routable, whether it is a Tor address, and whether it is compatible with version 1 of the Bitcoin protocol.

Note that since this crate is still being translated from C++, some of the function bodies may still be in the process of translation.

Overall, Bitcoin-addr provides a comprehensive set of functions for working with Bitcoin addresses in Rust.

2> cool thanks! could you teach me what a bitcoin address is and what it does?

Sure, I'd be happy to explain!

In the Bitcoin system, a Bitcoin address is a string of letters and numbers that is used to send and receive Bitcoin payments. Each address is associated with a certain amount of Bitcoin, and when someone sends Bitcoin to an address, they are effectively transferring ownership of that Bitcoin to the address owner.

Bitcoin addresses are created using a complex process that involves generating a public-private key pair. The public key is then hashed to create the Bitcoin address, which is a shorter, more human-readable representation of the public key. When someone wants to send Bitcoin to an address, they use the address as the recipient in their Bitcoin transaction, and the transaction is verified and added to the Bitcoin blockchain, which is a public ledger of all Bitcoin transactions.

It's worth noting that Bitcoin addresses are not tied to real-world identities, and they are pseudonymous by design. This makes Bitcoin transactions more private than traditional financial transactions, but it also makes it harder to track and recover lost or stolen Bitcoin.

Overall, Bitcoin addresses are a fundamental part of the Bitcoin system, allowing users to send and receive Bitcoin in a secure, decentralized way without relying on traditional financial institutions.

3> what is their syntax?

Bitcoin addresses typically begin with the number "1" or "3" and are composed of a combination of letters and numbers. They are case-sensitive and can range from 26 to 35 characters in length.

The syntax of a Bitcoin address can be broken down into several parts. First, there is the version byte, which is used to indicate the type of address. Next, there is the public key hash, which is a hash of the public key associated with the address. Finally, there is a checksum, which is used to ensure that the address is valid and has not been tampered with.

Here is an example of a Bitcoin address:

1BvBMSEYstWetqTFn5Au4m4GFg7xJaNVN2

In this address, the "1" at the beginning indicates that it is a regular Bitcoin address, and the rest of the string is the public key hash and checksum.

It's worth noting that there are also other types of Bitcoin addresses, such as multi-signature addresses and SegWit addresses, which have slightly different syntax and are used for different purposes.

4> thanks -- could you please provide me with some more detail about this crate?

Sure, here's some more information about the bitcoin-addr crate:

The bitcoin-addr crate is a Rust library for working with Bitcoin addresses. It provides functions for creating and manipulating Bitcoin addresses, as well as for serializing and deserializing them.

The crate is in the process of being translated from C++ to Rust, so some of the function bodies may still be in the process of translation. However, the crate provides a number of useful functions for working with Bitcoin addresses, including:

  • Address and addr_mut: These functions create a new Bitcoin address object and provide access to its fields.

  • create_addresses: This function generates a set of Bitcoin addresses from a given public key.

  • eq: This function checks whether two Bitcoin addresses are equal.

  • get_addr_bytes: This function returns the raw bytes of a Bitcoin address.

  • get_bip155network: This function returns the BIP 155 network associated with a Bitcoin address.

  • get_group: This function returns the group to which a Bitcoin address belongs.

  • get_hash: This function returns the hash of a Bitcoin address.

  • get_in_6addr and get_in_addr: These functions return the IPv6 and IPv4 addresses associated with a Bitcoin address, respectively.

  • get_key: This function returns the public key associated with a Bitcoin address.

  • get_linked_ipv4: This function returns the IPv4 address linked to a Bitcoin address.

  • get_mappedas: This function returns the mapped IPv4 address associated with a Bitcoin address.

  • get_net_class: This function returns the network class of a Bitcoin address.

  • get_network: This function returns the network associated with a Bitcoin address.

  • get_port: This function returns the port associated with a Bitcoin address.

  • get_reachability_from: This function returns the reachability of a Bitcoin address from a given IP address.

  • get_sock_addr: This function returns the socket address associated with a Bitcoin address.

  • has_linked_ipv4: This function checks whether a Bitcoin address has an IPv4 address linked to it.

  • is_addr_v1compatible: This function checks whether a Bitcoin address is compatible with version 1 of the address format.

  • is_bind_any: This function checks whether a Bitcoin address is bound to any IP address.

  • is_he_net: This function checks whether a Bitcoin address is part of the Hurricane Electric IPv6 network.

  • is_internal: This function checks whether a Bitcoin address is an internal address.

  • is_ipv4: This function checks whether a Bitcoin address is an IPv4 address.

  • is_ipv6: This function checks whether a Bitcoin address is an IPv6 address.

  • is_local: This function checks whether a Bitcoin address is a local address.

  • is_reachable: This function checks whether a Bitcoin address is reachable from a given IP address.

  • is_relayable: This function checks whether a Bitcoin address can be used as a relay.

  • is_routable: This function checks whether a Bitcoin address is routable.

  • is_tor: This function checks whether a Bitcoin address is a Tor onion service address.

  • is_valid: This function checks whether a Bitcoin address is a valid Bitcoin address.

  • iscjdns: This function checks whether a Bitcoin address is a cjdns address.

  • isi2p: This function checks whether a Bitcoin address is an i2p address.

  • isrfc1918, isrfc2544, isrfc3849, isrfc3927, isrfc3964, isrfc4193, isrfc4380, isrf

5> thanks! anything else?

The crate seems to be designed to provide a collection of functions and data structures for working with Bitcoin addresses in Rust. It includes various utility functions for manipulating addresses and performing checks on them, as well as functions for serializing and deserializing addresses.

Overall, the crate appears to be a useful resource for developers who are building Rust-based applications that interact with the Bitcoin network and need to work with Bitcoin addresses.

6> what are some key considerations we should know when using bitcoin addresses?

When using Bitcoin addresses, there are several key considerations to keep in mind:

  1. Security: Bitcoin addresses contain private keys that give users access to their funds. Therefore, it is important to keep these private keys secure and protected from unauthorized access.

  2. Accuracy: Bitcoin addresses are case sensitive, so it is important to enter the address accurately when making transactions. Entering an incorrect address can result in the loss of funds.

  3. Compatibility: There are different formats for Bitcoin addresses, and some formats may not be compatible with certain wallets or exchanges. It is important to ensure that the address format used is supported by the wallet or exchange being used.

  4. Confirmation time: Bitcoin transactions can take some time to be confirmed on the blockchain, so it is important to be patient and wait for the transaction to be confirmed before considering it complete.

  5. Fees: Transactions on the Bitcoin network require a fee to be paid to miners who validate the transaction. It is important to factor in this fee when making transactions, as higher fees can result in faster confirmation times.

  6. Network congestion: The Bitcoin network can become congested during periods of high demand, resulting in slower confirmation times and higher fees. It is important to monitor the network status and adjust transaction fees accordingly.

7> does the bitcoin address itself contain the private key?

No, the Bitcoin address itself does not contain the private key. A Bitcoin address is a hashed representation of a public key, which is derived from the private key. The private key is used to sign transactions and prove ownership of the Bitcoin associated with the corresponding address.

When a user generates a new Bitcoin address, the wallet software generates a new pair of public and private keys. The public key is then hashed to create the Bitcoin address, which can be shared with others for receiving payments. The private key is kept secret and is used to sign transactions when the user wants to spend their Bitcoin.

8> which encodings do we typically use to represent them? how many bytes are they?

Bitcoin addresses are typically represented in two encoding formats: Base58 and Bech32.

Base58 is a modified version of Base64 encoding that is designed to avoid ambiguous characters and prevent confusion between similar looking characters. The most commonly used Base58 Bitcoin address format starts with the number "1" and contains a mix of uppercase and lowercase letters (except for "0", "O", "I", and "l") and numbers.

Bech32 is a newer and more efficient Bitcoin address encoding format that is designed to support Segregated Witness (SegWit) transactions. Bech32 addresses start with the characters "bc1" and consist of lowercase letters and numbers, with no uppercase letters or ambiguous characters.

Bitcoin addresses are 20 bytes in length (or 160 bits), regardless of the encoding format used.

9> where is the proper place to store a bitcoin address? how often are they generated? is it better to use a single address or several? is it better to rotate addresses?

The proper place to store a Bitcoin address is in a secure and reliable wallet. A wallet is a software program that stores private keys and interacts with the Bitcoin network to send and receive transactions. It is important to choose a reputable wallet that is well-reviewed and has a strong reputation for security.

Bitcoin addresses are typically generated on demand by the wallet software as needed, such as when a user wants to receive a payment. The frequency of address generation depends on the specific wallet and its settings, but it is generally recommended to use a new address for each transaction to enhance privacy and security.

It is generally better to use multiple Bitcoin addresses instead of a single address, as this can help enhance privacy and security. When using multiple addresses, it can be helpful to label each address with a specific purpose or transaction, such as receiving payments from a specific source or sending payments to a particular recipient.

Rotating Bitcoin addresses can also help enhance privacy and security, as it makes it more difficult for others to track a user's transactions and balance. However, rotating addresses can also create additional management overhead and may not be necessary for all use cases. It is important to weigh the benefits and drawbacks of rotating addresses based on your specific needs and use case.

10> how secure is the wallet software which stores these addresses?

The security of a Bitcoin wallet software depends on a number of factors, such as the quality of the software code, the strength of encryption used to protect private keys, and the security of the system on which the software is installed.

Reputable Bitcoin wallet software is generally considered to be secure and reliable, but it is important to choose a well-reviewed wallet and take necessary precautions to enhance security. This includes regularly updating the wallet software to the latest version, using strong and unique passwords, enabling two-factor authentication if available, and using a secure and up-to-date operating system.

It is also important to be aware of potential security risks, such as phishing attacks or malware that may attempt to steal private keys or other sensitive information. It is generally recommended to avoid storing large amounts of Bitcoin in online wallets or exchanges, and to instead use a hardware wallet or other offline storage method for long-term storage of significant amounts of Bitcoin.

Ultimately, the security of a Bitcoin wallet software depends on the diligence and awareness of the user, and it is important to stay informed and take necessary precautions to protect your Bitcoin and other digital assets.

11> what else should we know about addresses?

Here are a few additional things to know about Bitcoin addresses:

  • Bitcoin addresses are case sensitive. This means that "1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa" and "1a1zp1ep5qgefi2dmptftl5slmv7divfna" are two different addresses.

  • It is possible to generate a Bitcoin address offline, also known as a "cold" address. This can be done by using a hardware wallet or by generating an address using an offline computer or software that has never been connected to the internet. This can provide an additional layer of security against potential hacks or malware.

  • Bitcoin addresses can be used for other purposes beyond simple transactions. For example, they can be used to prove ownership or authorship of a piece of data, by including the Bitcoin address in a digital signature.

  • Bitcoin addresses are not entirely anonymous, as transactions on the Bitcoin blockchain are public and traceable. However, there are ways to enhance privacy and obfuscate transactions, such as by using mixing services or privacy-focused cryptocurrencies like Monero or Zcash.

  • While Bitcoin addresses are a core component of the Bitcoin system, they are not the only way to interact with the blockchain. There are other types of transactions that can be used to execute more complex transactions or smart contracts.

Dependencies

~90MB
~857K SLoC