Bitcoin Forum
May 18, 2024, 08:25:13 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: how to get Bech32 bitcoin format adress?  (Read 169 times)
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
March 08, 2021, 07:00:59 AM
Last edit: March 08, 2021, 07:46:49 AM by Sanka555
 #1

BY JAVA   (bitcoinj library)

if I have P2PKH and P2SH and keys for them
I make the address from 256 bits of hash

thank you very much in advance
NotATether
Legendary
*
Offline Offline

Activity: 1610
Merit: 6747


bitcoincleanup.com / bitmixlist.org


View Profile WWW
March 08, 2021, 04:33:41 PM
 #2

Here's a mock Java program I made that takes a public key in hex and prints it's bech32 address. Note that it doesn't do any error checking, or checks for well-formedness of user input.

Code:
/*
 * THIS IS MOCK CODE, IT MAY NEED TWEAKS TO RUN
 */

import org.bouncycastle.crypto.digests.RIPEMD160Digest;
import org.bitcoinj.core.Bech32;

public class Bech32Address {
        public static void main(String[] args) {
                String humanReadablePart = "bc";
                Scanner scanIn = new System.Scanner(System.in);
                System.out.print("Input public key hex, wihout leading 0x: ");
                String hexPubKey = scanIn.nextLine();
               
                // convert public key from hex to bytes
                byte[] bytesPubKey = new byte[hexPubKey.length() / 2];
                for (int i = 0; i < bytesPubKey.length; i++) {
                        int index = i * 2;
                        int j = Integer.parseInt(hexPubKey.substring(index, index + 2), 16);
                        bytePubKey[i] = (byte) j;
                }

                // SHA256 hash the public key
                MessageDigest digest = MessageDigest.getInstance("SHA-256");
                byte[] encodedhash = digest.digest(bytePubKey);

                // Hash with RIPEMD160
                RIPEMD160Digest d = new RIPEMD160Digest();
                d.update (bytePubKey, 0, bytePubKey.length);
                byte[] hash160 = new byte[d.getDigestSize()];
                d.doFinal (hash160, 0);

               // Finally, encode in bech32
               String address = Bech32.encode("bc", hash160);
               System.out.print("Bech32 address: ");
               System.out.println(address);
}
}

We essentially use the encode() method of the org.bitcoinj.core.Bech32 class and pass it the human readable part ("bc") and the bytes of the RIPEMD160 hash of your SHA256 hash. I also included the SHA256 hash generation for completeness.

It has dependencies on BouncyCastle for RIPEMD160 and BitcoinJ for Bech32.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
March 10, 2021, 07:13:37 AM
 #3

Here's a mock Java program

thank. but that's a little different. I will not be able to get the private key from such an address.
I found a JavaScript program that does exactly what I want.

Can anyone help with translating it to Java.

Very grateful in advance
Quote
let bitcoin = require('bitcoinjs-lib');
let assert = require('assert')
let bigi = require('bigi')


let NETWORK = bitcoin.networks.bitcoin;
let brainwallet = "This is my brain wallet and it is less secure than a CSPRNG source";
let hash = bitcoin.crypto.sha256(brainwallet);
let d = bigi.fromBuffer(hash);
let nkeyp = new bitcoin.ECPair(d);
let wif = nkeyp.toWIF();
let pubKey = nkeyp.getPublicKeyBuffer();
let scriptPubKey = bitcoin.script.witnessPubKeyHash.output.encode(bitcoin.crypto.hash160(pubKey))
let address = bitcoin.address.fromOutputScript(scriptPubKey);
console.log(address);
console.log(wif);
NotATether
Legendary
*
Offline Offline

Activity: 1610
Merit: 6747


bitcoincleanup.com / bitmixlist.org


View Profile WWW
March 10, 2021, 07:39:27 AM
 #4

thank. but that's a little different. I will not be able to get the private key from such an address.
I found a JavaScript program that does exactly what I want.

Can anyone help with translating it to Java.

Very grateful in advance

So let me get your request straight:

- You have a brainwallet which is hashed to SHA256, and
- You want to make a public key out of this hash which is then hashed again with RIPEMD160, then converted into Witness Data bytes, because a Bech32 address takes a human readable part like "bc" and "tb", followed by a "1" always, then the witness version in the Bech32 number system (0="q", 1="p", etc) and then the 160-bit witness data
- and then finally encode it into Bech32 using these three parts (assume Witness version is 0 and HRP is "bc")

Is this correct?

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
March 10, 2021, 08:24:39 AM
Last edit: March 10, 2021, 09:00:21 AM by Sanka555
 #5

thank. but that's a little different. I will not be able to get the private key from such an address.
I found a JavaScript program that does exactly what I want.

Can anyone help with translating it to Java.

Very grateful in advance

So let me get your request straight:

- You have a brainwallet which is hashed to SHA256, and
- You want to make a public key out of this hash which is then hashed again with RIPEMD160, then converted into Witness Data bytes, because a Bech32 address takes a human readable part like "bc" and "tb", followed by a "1" always, then the witness version in the Bech32 number system (0="q", 1="p", etc) and then the 160-bit witness data
- and then finally encode it into Bech32 using these three parts (assume Witness version is 0 and HRP is "bc")

Is this correct?

I'm trying to get from an ordinary word
3 types of addresses and, accordingly, 3 private keys to them.

I already have 2 sets. It remains to get the segwit address (b1) and a private key to it

The JS code obviously does this. But I don’t know how. Probably as you wrote. Let's check))
Sanka555 (OP)
Member
**
Offline Offline

Activity: 96
Merit: 36


View Profile
March 10, 2021, 11:53:54 AM
 #6

String segwit=SegwitAddress.fromKey(MainNetParams.get(), key).toBech32();

or
fromHash(NetworkParameters, byte [])
or fromKey(NetworkParameters, ECKey)

address received.  Grin

There is still a question with a private key.

How to get such a key from this address, which when added to blockchain.com
will give this address. (bc1....)

Now I have one key for both bc1 ... and "compressed adress".


and there is one more address (I don't have it).  Which starts with a 3. which is also a segwit but not b1... . How to get it?



NotATether
Legendary
*
Offline Offline

Activity: 1610
Merit: 6747


bitcoincleanup.com / bitmixlist.org


View Profile WWW
March 10, 2021, 12:23:10 PM
 #7

and there is one more address (I don't have it).  Which starts with a 3. which is also a segwit but not b1... . How to get it?

It depends on whether the address was hashed from a script or a public key. If the latter then you have a P2SH-P2WPKH address and you generate it the same way as legacy "1" addresses: SHA256 the public key, then RIPEMD160 that, and finally encode it in base58.

If however the address is hashed from the script, then it's a regular P2SH address and you should RIPEMD160 the script bytes to get the script's public key, and then base58 encode that.

I think that particular script you found generates a P2SH address and not a P2SH-P2WPKH Bech32 address. I found a similar version of it at https://bitcoin.stackexchange.com/a/76394/112589 but it's for multisig (P2SH-P2WSH). Maybe you adapted yours from it.

.
.BLACKJACK ♠ FUN.
█████████
██████████████
████████████
█████████████████
████████████████▄▄
░█████████████▀░▀▀
██████████████████
░██████████████
████████████████
░██████████████
████████████
███████████████░██
██████████
CRYPTO CASINO &
SPORTS BETTING
▄▄███████▄▄
▄███████████████▄
███████████████████
█████████████████████
███████████████████████
█████████████████████████
█████████████████████████
█████████████████████████
███████████████████████
█████████████████████
███████████████████
▀███████████████▀
█████████
.
Pages: [1]
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!