Skip to content

Commit

Permalink
Merge pull request #618 from dfinity/tl/bitcoin_clean_up_hashing
Browse files Browse the repository at this point in the history
Polishing the hashing part when generating a P2PKH address
  • Loading branch information
sesi200 authored Sep 25, 2023
2 parents 5f86dc5 + d3678e0 commit a77af17
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions rust/basic_bitcoin/src/basic_bitcoin/src/bitcoin_wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -276,13 +276,16 @@ fn sha256(data: &[u8]) -> Vec<u8> {
hasher.update(data);
hasher.finalize().to_vec()
}
fn ripemd160(data: &[u8]) -> Vec<u8> {
let mut hasher = ripemd::Ripemd160::new();
hasher.update(data);
hasher.finalize().to_vec()
}

// Converts a public key to a P2PKH address.
fn public_key_to_p2pkh_address(network: BitcoinNetwork, public_key: &[u8]) -> String {
// sha256 + ripmd160
let mut hasher = ripemd::Ripemd160::new();
hasher.update(sha256(public_key));
let result = hasher.finalize();
// SHA-256 & RIPEMD-160
let result = ripemd160(&sha256(public_key));

let prefix = match network {
BitcoinNetwork::Testnet | BitcoinNetwork::Regtest => 0x6f,
Expand Down

0 comments on commit a77af17

Please sign in to comment.