Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: WebStuffs on April 03, 2024, 03:30:46 PM



Title: HELP! Loss of funds / Invalid address
Post by: WebStuffs on April 03, 2024, 03:30:46 PM
Hi all,

I have been recently working on making a payment processor for myself. I found this script, that seemed to generate all of the basic types of Bitcoin addresses. I tested it, and I was able to use the legacy addresses just fine, so I kept going with it.

Some time into testing, I started generating addresses, including the bech and segwith accesses (bc and 3 prefixes), and sent some transactions to those addresses. I noticed that the bech address was invalid, and realized that I may have made a mistake. I tried importing the private keys I had generated to see if I can access the bech and segwith addresses, and to my horror the addresses are different to what the script generated.

Here is the repo: https://github.com/BRO200BS/Bitcoin-Address-Generator

For example, here is a key I just generated:

'private_key': '188ffa83fc4f665b4fb24460d560540e635ea824ad1576e27644f7835600c85b'
'WIF': '5J16zh1Mktz1iWEavJFT8nVRBscDCLQDzChYTeYxU33cQDRnty5'

'p2pkh_address': '1EcW6UVqRCpsww7RA8moBjx6RypYnnraDA'
'compressed_p2pkh_address': '16cSkuPktoD5u9aN3ttnNf9YYb1jvK4Sjw'
'p2sh_address': '3JKyqNxb5Wb9uh5NUX4S5PhMZTddEiY92n'
'bech32_address': 'bc1qq2tvlagcm8zdwf9h0mjhzu9m6s3q84w8kpsj83wvj'

As you can see, the bech address is starting with "bc1qq" which doesnt seem to be valid. I also had no luck in getting my private key to resolve to the p2sh address.

I tried using this service to see what the output would be:

https://secretscan.org/PrivateKeySegwit

Inserting the private key above to the site above yields the following address: 32kecU8BJMWCiDbCxCR4EaYc3fZx5bDPpy, likewise when I import the compressed WIF to my Electrum, it results in this address not the one generated by my script.

I had already sent some coins to my seemingly invalid p2sh/segwit addresses - are these coins now entirely lost?

Thanks to anyone who might be able to assist me!


Title: Re: HELP! Loss of funds / Invalid address
Post by: Frankolala on April 03, 2024, 03:58:32 PM
It is certain that your coins are lost. This is because coins sent to the wrong bitcoin wallet address cannot be retrieve unless, the owner of that wallet choose to send them back to the address that sent him those coins. In your own case, you sent your coins to an invalid address, it is lost forever.

There is always a warning that bitcoin transaction is irreversible. Sorry for your lost mate. Be careful next time to make sure that you are sending your coins to the right wallet address.


Title: Re: HELP! Loss of funds / Invalid address
Post by: BitMaxz on April 03, 2024, 04:08:04 PM
I had already sent some coins to my seemingly invalid p2sh/segwit addresses - are these coins now entirely lost?


It's lost if you send it to an address that you don't fully control.

How did you import it to Electrum?

If you imported the private key to Electrum in a wrong way it would gives you a different address usually legacy address.

If I were you try adding the script type before importing the private key check the guide below as sample

Code:
p2wpkh: bc1......
p2wpkh-p2sh: 3.......
p2pkh: 1....

Next time use BTCpayserver as your payment processor or blockonomics you don't need to develop anything you just need to import your wallet master public key and all invoice(BTC addresses) generated should be came from your master public key that you fully control.


Title: Re: HELP! Loss of funds / Invalid address
Post by: Findingnemo on April 03, 2024, 05:23:03 PM

AFAIK, one master key can generate all three types of addresses, and the reason why you see two different address is because you are using two different derivation path.

As long as the address generated using that private keys should have the funds that you send, please try importing the private key again.


Title: Re: HELP! Loss of funds / Invalid address
Post by: mamuu on April 03, 2024, 06:00:15 PM
Hi all,

I have been recently working on making a payment processor for myself. I found this script, that seemed to generate all of the basic types of Bitcoin addresses. I tested it, and I was able to use the legacy addresses just fine, so I kept going with it.

Some time into testing, I started generating addresses, including the bech and segwith accesses (bc and 3 prefixes), and sent some transactions to those addresses. I noticed that the bech address was invalid, and realized that I may have made a mistake. I tried importing the private keys I had generated to see if I can access the bech and segwith addresses, and to my horror the addresses are different to what the script generated.

Here is the repo: https://github.com/BRO200BS/Bitcoin-Address-Generator

For example, here is a key I just generated:

'private_key': '188ffa83fc4f665b4fb24460d560540e635ea824ad1576e27644f7835600c85b'
'WIF': '5J16zh1Mktz1iWEavJFT8nVRBscDCLQDzChYTeYxU33cQDRnty5'

'p2pkh_address': '1EcW6UVqRCpsww7RA8moBjx6RypYnnraDA'
'compressed_p2pkh_address': '16cSkuPktoD5u9aN3ttnNf9YYb1jvK4Sjw'
'p2sh_address': '3JKyqNxb5Wb9uh5NUX4S5PhMZTddEiY92n'
'bech32_address': 'bc1qq2tvlagcm8zdwf9h0mjhzu9m6s3q84w8kpsj83wvj'

As you can see, the bech address is starting with "bc1qq" which doesnt seem to be valid. I also had no luck in getting my private key to resolve to the p2sh address.

I tried using this service to see what the output would be:

https://secretscan.org/PrivateKeySegwit

Inserting the private key above to the site above yields the following address: 32kecU8BJMWCiDbCxCR4EaYc3fZx5bDPpy, likewise when I import the compressed WIF to my Electrum, it results in this address not the one generated by my script.

I had already sent some coins to my seemingly invalid p2sh/segwit addresses - are these coins now entirely lost?

Thanks to anyone who might be able to assist me!
The correct information of the private key in the example you give is below.
I suggest you use a correct library, I use pycoin (http://"https://github.com/pycoin/pycoin") for python

get well soon, thank you

Code:
input                        : 5J16zh1Mktz1iWEavJFT8nVRBscDCLQDzChYTeYxU33cQDRnty5
network                      : Bitcoin mainnet
symbol                       : BTC
secret exponent              : 11109896491172774014105289341808672370464295922053509351635184062993478502491
 hex                         : 188ffa83fc4f665b4fb24460d560540e635ea824ad1576e27644f7835600c85b
wif                          : Kx3TTdaackSeAC2xiF8nMMnRkzNQq2dWKUdwrck3pL2XFTaYX3f5
 uncompressed                : 5J16zh1Mktz1iWEavJFT8nVRBscDCLQDzChYTeYxU33cQDRnty5
public pair x                : 90629257113961921745629633883391754337878636139328260909933221655819498999864
public pair y                : 88699594181249121212269429001654019420539278288000816039192792995060957725698
 x as hex                    : c85e577b31823438840964c8aa545e3a348a283f5ee66e70cbb478ed3136d038
 y as hex                    : c41a30f30173e2d1ff34f444c11e90ff97cbba3bc3fc05a60658ff361a664402
y parity                     : even
key pair as sec              : 02c85e577b31823438840964c8aa545e3a348a283f5ee66e70cbb478ed3136d038
 uncompressed                : 04c85e577b31823438840964c8aa545e3a348a283f5ee66e70cbb478ed3136d038\
                                 c41a30f30173e2d1ff34f444c11e90ff97cbba3bc3fc05a60658ff361a664402
hash160                      : 3d8cb03b39d2e1a7202279e0f678afa477b3e1f3
 uncompressed                : 95507b09c46da9af57aaa398dc2ebcc79b164810
Bitcoin address              : 16cSkuPktoD5u9aN3ttnNf9YYb1jvK4Sjw
Bitcoin address uncompressed : 1EcW6UVqRCpsww7RA8moBjx6RypYnnraDA
Bitcoin segwit address       : bc1q8kxtqwee6ts6wgpz08s0v79053mm8c0n82rvxm
p2sh segwit                  : 32kecU8BJMWCiDbCxCR4EaYc3fZx5bDPpy
 corresponding p2sh script   : 00143d8cb03b39d2e1a7202279e0f678afa477b3e1f3


Title: Re: HELP! Loss of funds / Invalid address
Post by: BlackHatCoiner on April 03, 2024, 08:12:53 PM
As you can see, the bech address is starting with "bc1qq" which doesnt seem to be valid. I also had no luck in getting my private key to resolve to the p2sh address.
It is an invalid address.

I had already sent some coins to my seemingly invalid p2sh/segwit addresses - are these coins now entirely lost?
None of the addresses you've shared have coins. I assume you meant - 3JKyqNxb5Wb9uh5NUX4S5PhMZTddEiY92n - as p2sh-segwit, and as anyone can notice from a block explorer it has never received any coins.

In your own case, you sent your coins to an invalid address, it is lost forever.
You can't send coins to an invalid address. An address is (usually) an encoded version of the witness public key hash. If the address is invalid, then it means decoding it will not result in an acceptable locking script.


Title: Re: HELP! Loss of funds / Invalid address
Post by: DaveF on April 03, 2024, 08:53:38 PM
As of 2 months ago this was a known issue with that script:

https://github.com/BRO200BS/Bitcoin-Address-Generator/issues

and as @BlackHatCoiner pointed out you can't send coins to an invalid address so there is something else going on.

What was the actual address that you generated that had coins sent to it?
And do you have the TXID of that transaction?

-Dave


Title: Re: HELP! Loss of funds / Invalid address
Post by: apogio on April 04, 2024, 06:31:10 AM
The following warning is a day late and a dollar short.

Let me share my point of view, having written some similar script  (https://bitcointalk.org/index.php?topic=5483173.0)myself: Don't! Don't! Don't use scripts that are not properly tested, qualified and used.

Open source is fantastic because it allows many pairs of eyes to review the same code. However, it's not a panacea. Most of the time, these pairs of eyes distinguish corner cases and issues. In your particular occasion, as DaveF said above, there was an open issue in the code. So, when using open source, we should at least check:
1. the open issues.
2. the number of forks / stars.
3. the number and quality of open merge requests.
4. the number of contributors.


Title: Re: HELP! Loss of funds / Invalid address
Post by: NotATether on April 04, 2024, 07:42:45 AM
Seems to generate the correct legacy address in ZPyWallet too:

Code:
>>> p = zpywallet.PrivateKey.from_hex('188ffa83fc4f665b4fb24460d560540e635ea824ad1576e27644f7835600c85b')
>>> a.public_key.base58_address()
'16cSkuPktoD5u9aN3ttnNf9YYb1jvK4Sjw'
>>> a.public_key.base58_address(compressed=False)
'1EcW6UVqRCpsww7RA8moBjx6RypYnnraDA' # <-- correct
>>> a.public_key.bech32_address()
'bc1q8kxtqwee6ts6wgpz08s0v79053mm8c0n82rvxm'
>>> a.public_key.bech32_address(compressed=False)
'bc1qj4g8kzwydk5674a25wvdct4uc7d3vjqsk8t77q'

I do not have a P2SH generation routine to test against.

Neither of the above bech32 addresses match up with yours even slightly. How are you creating the bech32 addresses? I use Pieter Wuille's reference implementation. I think you are also using the same one.

However, your P2SH and Bech32 address generation routines are wrong:

Code:
    # Get P2SH address
    redeem_script = '21' + compressed_public_key + 'ac'
    hash160 = RIPEMD160.new()
    hash160.update(SHA256.new(bytes.fromhex(redeem_script)).digest())
    script_hash = '05' + hash160.hexdigest()
    checksum = SHA256.new(SHA256.new(bytes.fromhex(script_hash)).digest()).hexdigest()[:8]
    p2sh_address = base58.b58encode(bytes.fromhex(script_hash + checksum))

    # Get Bech32 address
    witness_program = bytes([0x00, 0x14]) + hash160.digest() # <-- !!!
    bech32_address = bech32_encode('bc', convertbits(witness_program, 8, 5))

First of all, your script hash is supposed to be just the public key by itself, not the entire redeem script. The opcodes are only to be added right before the Base58 encode. (those opcodes are not even bytes, you made them ASCII characters.)

Your second mistake is not resetting the hash160 when you are done so you are using the already incorrect P2SH script hash to create the bech32 address...

And even the bech32 calculation itself also has another mistake, you are adding 0x00 0x14, the witness program at the beginning of the address.

Why are you adding the witness program into the bech32 address? You are not supposed to do that.

Anything that's supposed to be a script opcode is not supposed to be encoded into an address. This is why it's making an invalid qq address. You did not lose any funds unless your coins were sent to the invalid P2SH address, which is the reason why I don't like P2SH-P2WPKH in the first place. Why are you debugging with mainnet addresses anyway? Testnet would've caught this error in a less expensive way.


Title: Re: HELP! Loss of funds / Invalid address
Post by: brainless on April 07, 2024, 02:35:55 PM
Hi all,

I have been recently working on making a payment processor for myself. I found this script, that seemed to generate all of the basic types of Bitcoin addresses. I tested it, and I was able to use the legacy addresses just fine, so I kept going with it.

Some time into testing, I started generating addresses, including the bech and segwith accesses (bc and 3 prefixes), and sent some transactions to those addresses. I noticed that the bech address was invalid, and realized that I may have made a mistake. I tried importing the private keys I had generated to see if I can access the bech and segwith addresses, and to my horror the addresses are different to what the script generated.

Here is the repo: https://github.com/BRO200BS/Bitcoin-Address-Generator

For example, here is a key I just generated:

'private_key': '188ffa83fc4f665b4fb24460d560540e635ea824ad1576e27644f7835600c85b'
'WIF': '5J16zh1Mktz1iWEavJFT8nVRBscDCLQDzChYTeYxU33cQDRnty5'

'p2pkh_address': '1EcW6UVqRCpsww7RA8moBjx6RypYnnraDA'
'compressed_p2pkh_address': '16cSkuPktoD5u9aN3ttnNf9YYb1jvK4Sjw'
'p2sh_address': '3JKyqNxb5Wb9uh5NUX4S5PhMZTddEiY92n'
'bech32_address': 'bc1qq2tvlagcm8zdwf9h0mjhzu9m6s3q84w8kpsj83wvj'

As you can see, the bech address is starting with "bc1qq" which doesnt seem to be valid. I also had no luck in getting my private key to resolve to the p2sh address.

I tried using this service to see what the output would be:

https://secretscan.org/PrivateKeySegwit

Inserting the private key above to the site above yields the following address: 32kecU8BJMWCiDbCxCR4EaYc3fZx5bDPpy, likewise when I import the compressed WIF to my Electrum, it results in this address not the one generated by my script.

I had already sent some coins to my seemingly invalid p2sh/segwit addresses - are these coins now entirely lost?

Thanks to anyone who might be able to assist me!
What address from ? You sent funds ?


Title: Re: HELP! Loss of funds / Invalid address
Post by: Rabbitqt on April 08, 2024, 09:23:17 AM
It's lost, and honestly, it sounded like you didn't know what you were doing.