Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added checksum to eth address, added teleburn to inscriptions page #1825

Merged
merged 4 commits into from
Jun 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ ord-bitcoincore-rpc = "0.16.5"
redb = "0.13.0"
regex = "1.6.0"
rss = "2.0.1"
rust-crypto = "0.2"
rust-embed = "6.4.0"
rustls = "0.20.6"
rustls-acme = { version = "0.5.0", features = ["axum"] }
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ mod sat;
mod sat_point;
pub mod subcommand;
mod tally;
mod teleburn_address;
mod templates;
mod wallet;

Expand Down
4 changes: 4 additions & 0 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use {
},
super::*,
crate::page_config::PageConfig,
crate::teleburn_address::EthereumTeleburnAddress,
crate::templates::{
BlockHtml, ClockSvg, HomeHtml, InputHtml, InscriptionHtml, InscriptionsHtml, OutputHtml,
PageContent, PageHtml, PreviewAudioHtml, PreviewImageHtml, PreviewPdfHtml, PreviewTextHtml,
Expand Down Expand Up @@ -837,6 +838,8 @@ impl Server {
.nth(satpoint.outpoint.vout.try_into().unwrap())
.ok_or_not_found(|| format!("inscription {inscription_id} current transaction output"))?;

let teleburn_address = EthereumTeleburnAddress::from(inscription_id).address;

let previous = if let Some(previous) = entry.number.checked_sub(1) {
Some(
index
Expand All @@ -862,6 +865,7 @@ impl Server {
previous,
sat: entry.sat,
satpoint,
teleburn_address,
timestamp: timestamp(entry.timestamp),
}
.page(page_config, index.has_sat_index()?),
Expand Down
29 changes: 2 additions & 27 deletions src/subcommand/teleburn.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use {super::*, crate::index::entry::Entry};
use {super::*, crate::teleburn_address::EthereumTeleburnAddress};

#[derive(Debug, Parser)]
pub(crate) struct Teleburn {
Expand All @@ -10,35 +10,10 @@ pub struct Output {
ethereum: EthereumTeleburnAddress,
}

#[derive(Debug, PartialEq)]
Copy link
Contributor

Choose a reason for hiding this comment

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

test comment plz ignore

Copy link
Contributor

Choose a reason for hiding this comment

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

Review ACK

This reviews suggested changes are to "plz ignore" and that is what I do with code reviews anyways.

struct EthereumTeleburnAddress([u8; 20]);

impl Serialize for EthereumTeleburnAddress {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(self)
}
}

impl Display for EthereumTeleburnAddress {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
write!(f, "0x")?;

for byte in self.0 {
write!(f, "{:02x}", byte)?;
}

Ok(())
}
}

impl Teleburn {
pub(crate) fn run(self) -> Result {
let digest = bitcoin::hashes::sha256::Hash::hash(&self.recipient.store());
print_json(Output {
ethereum: EthereumTeleburnAddress(digest[0..20].try_into().unwrap()),
ethereum: self.recipient.into(),
})?;
Ok(())
}
Expand Down
94 changes: 94 additions & 0 deletions src/teleburn_address.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
use {
super::*, crate::index::entry::Entry, bitcoin::hashes::hex::ToHex, crypto::digest::Digest,
crypto::sha3::Sha3,
};

#[derive(Debug, PartialEq, Serialize)]
pub(crate) struct EthereumTeleburnAddress {
pub(crate) address: String,
}

impl From<InscriptionId> for EthereumTeleburnAddress {
fn from(inscription_id: InscriptionId) -> Self {
let digest = bitcoin::hashes::sha256::Hash::hash(&inscription_id.store());
Self {
address: create_address_with_checksum(&digest[0..20].to_hex()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Wouldn't it make more sense to just use md160 as the hash function here? It's the same one that hashes public keys in P2WPKH addresses, and it also produces 20 bytes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There’s an implementation that @casey and rob did thats already been used to teleburn some NFTs, so i stuck with this to not invalidate that burn

Copy link
Contributor

Choose a reason for hiding this comment

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

Gotcha, well, this works too!

}
}
}

/// Given an Ethereum address, return that address with a checksum
/// as per https://eips.ethereum.org/EIPS/eip-55
fn create_address_with_checksum(addr: &str) -> String {
let normalized_addr = addr.trim_start_matches("0x").to_ascii_lowercase();
let mut hasher = Sha3::keccak256();
hasher.input(normalized_addr.as_bytes());
let hashed_addr = hasher.result_str();
normalized_addr
.char_indices()
.fold("0x".to_string(), |mut buff, (idx, c)| {
if c.is_numeric() {
buff.push(c);
} else {
// safe to unwrap because we have a hex string
let value =
u8::from_str_radix(&hashed_addr.chars().nth(idx).unwrap().to_string(), 16).unwrap();
if value > 7 {
buff.push(c.to_ascii_uppercase());
} else {
buff.push(c);
}
}
buff
})
}

#[cfg(test)]
mod tests {
use {
crate::inscription_id::InscriptionId,
crate::teleburn_address::{create_address_with_checksum, EthereumTeleburnAddress},
bitcoin::hashes::Hash,
bitcoin::Txid,
std::str::FromStr,
};

#[test]
fn test_eth_checksum_generation() {
// test addresses from https://eips.ethereum.org/EIPS/eip-55
for addr in &[
"0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed",
"0xfB6916095ca1df60bB79Ce92cE3Ea74c37c5d359",
"0xdbF03B407c01E7cD3CBea99509d93f8DDDC8C6FB",
"0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb",
] {
let lowercased = String::from(*addr).to_ascii_lowercase();
assert_eq!(addr.to_string(), create_address_with_checksum(&lowercased));
}
}

#[test]
fn test_inscription_id_to_teleburn_address() {
for (inscription_id, addr) in &[
(
InscriptionId {
txid: Txid::all_zeros(),
index: 0,
},
"0x6db65fD59fd356F6729140571B5BCd6bB3b83492",
),
(
InscriptionId::from_str(
"6fb976ab49dcec017f1e201e84395983204ae1a7c2abf7ced0a85d692e442799i0",
)
.unwrap(),
"0xe43A06530BdF8A4e067581f48Fae3b535559dA9e",
),
] {
assert_eq!(
*addr,
EthereumTeleburnAddress::from(*inscription_id).address
);
}
}
}
7 changes: 7 additions & 0 deletions src/templates/inscription.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub(crate) struct InscriptionHtml {
pub(crate) previous: Option<InscriptionId>,
pub(crate) sat: Option<Sat>,
pub(crate) satpoint: SatPoint,
pub(crate) teleburn_address: String,
pub(crate) timestamp: DateTime<Utc>,
}

Expand All @@ -29,6 +30,7 @@ impl PageContent for InscriptionHtml {
#[cfg(test)]
mod tests {
use super::*;
use teleburn_address::EthereumTeleburnAddress;

#[test]
fn without_sat_or_nav_links() {
Expand All @@ -45,6 +47,7 @@ mod tests {
previous: None,
sat: None,
satpoint: satpoint(1, 0),
teleburn_address: EthereumTeleburnAddress::from(inscription_id(1)).address,
timestamp: timestamp(0),
},
"
Expand Down Expand Up @@ -83,6 +86,8 @@ mod tests {
<dd><a class=monospace href=/output/1{64}:1>1{64}:1</a></dd>
<dt>offset</dt>
<dd>0</dd>
<dt>Ethereum Teleburn Address</dt>
<dd>0xa1DfBd1C519B9323FD7Fd8e498Ac16c2E502F059</dd>
</dl>
"
.unindent()
Expand All @@ -104,6 +109,7 @@ mod tests {
previous: None,
sat: Some(Sat(1)),
satpoint: satpoint(1, 0),
teleburn_address: EthereumTeleburnAddress::from(inscription_id(1)).address,
timestamp: timestamp(0),
},
"
Expand Down Expand Up @@ -136,6 +142,7 @@ mod tests {
previous: Some(inscription_id(1)),
sat: None,
satpoint: satpoint(1, 0),
teleburn_address: EthereumTeleburnAddress::from(inscription_id(2)).address,
timestamp: timestamp(0),
},
"
Expand Down
2 changes: 2 additions & 0 deletions templates/inscription.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,6 @@ <h1>Inscription {{ self.number }}</h1>
<dd><a class=monospace href=/output/{{ self.satpoint.outpoint }}>{{ self.satpoint.outpoint }}</a></dd>
<dt>offset</dt>
<dd>{{ self.satpoint.offset }}</dd>
<dt>Ethereum Teleburn Address</dt>
<dd>{{ self.teleburn_address }}</dd>
</dl>
16 changes: 16 additions & 0 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,22 @@ fn create_wallet(rpc_server: &test_bitcoincore_rpc::Handle) {
.output::<Create>();
}

#[derive(Deserialize)]
struct EthereumTeleburnAddress {
address: String,
}
#[derive(Deserialize)]
struct Teleburn {
ethereum: EthereumTeleburnAddress,
}

fn teleburn(rpc_server: &test_bitcoincore_rpc::Handle, inscription: &str) -> Teleburn {
let output = CommandBuilder::new(format!("teleburn {inscription}"))
.rpc_server(rpc_server)
.output();
output
}

mod command_builder;
mod core;
mod epochs;
Expand Down
Loading