Skip to content

Conversation

tcharding
Copy link
Member

Users may wish to ask of an address 'what kind of address is this?' We have the NetworkKind struct that abstracts over the answer but currently no API to ask the question.

The address may have been parsed or constructed and weather the network has been checked already is immaterial. Hence we add the function for both NetworkChecked and NetworkUnchecked addresses.

Fix: #4247

@github-actions github-actions bot added the C-bitcoin PRs modifying the bitcoin crate label Mar 17, 2025
@tcharding
Copy link
Member Author

This patch can be trivially backported to the 0.32.x branch with git cherry-pick 147afd87a.

Users may wish to ask of an address 'what kind of address is this?' We
have the `NetworkKind` struct that abstracts over the answer but
currently no API to ask the question.

The address may have been parsed or constructed and weather the network
has been checked already is immaterial. Hence we add the function for
both `NetworkChecked` and `NetworkUnchecked` addresses.

Fix: rust-bitcoin#4247
@tcharding tcharding force-pushed the 03-17-network-kind branch from 147afd8 to f4f79f8 Compare March 17, 2025 01:27
Copy link
Member

@apoelstra apoelstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f4f79f8; successfully ran local tests

Copy link
Collaborator

@Kixunil Kixunil left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK f4f79f8

KnownHrp::Regtest => NetworkKind::Test,
}
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel somewhat uneasy about this conversion since it's lossy. But not really wrong.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KnownHrp is a larger type than NetworkKind so it's in the type signature that it'll be lossy.

P2sh { hash: _, ref network } => *network,
Segwit { program: _, ref hrp } => NetworkKind::from(*hrp),
}
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth also having is_mainnet method (and possibly also is_a_test).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm leaning toward not because these methods are one-liners (once this method is in) and it's extra API surface.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, these are common in Rust libraries. But maybe it's enough to have them on NetworkKind so once can call network_kind().is_mainnet().

@apoelstra apoelstra merged commit 78538bc into rust-bitcoin:master Mar 17, 2025
24 checks passed
@tcharding tcharding deleted the 03-17-network-kind branch April 14, 2025 04:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bitcoin PRs modifying the bitcoin crate Needs Backport
Projects
None yet
Development

Successfully merging this pull request may close these issues.

A way to get a NetworkKind from an Address
3 participants