Author

Topic: how to get Bech32 bitcoin format adress? (Read 171 times)

legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
March 10, 2021, 07:23:10 AM
#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.
member
Activity: 96
Merit: 36
March 10, 2021, 06: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?



member
Activity: 96
Merit: 36
March 10, 2021, 03:24:39 AM
#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))
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
March 10, 2021, 02: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?
member
Activity: 96
Merit: 36
March 10, 2021, 02: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);
legendary
Activity: 1568
Merit: 6660
bitcoincleanup.com / bitmixlist.org
March 08, 2021, 11:33:41 AM
#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.
member
Activity: 96
Merit: 36
March 08, 2021, 02:00:59 AM
#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
Jump to: