Bitcoin Forum

Bitcoin => Development & Technical Discussion => Topic started by: varChar on September 29, 2014, 06:50:53 PM



Title: What is possible to validate on a public key
Post by: varChar on September 29, 2014, 06:50:53 PM
Read here: https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses (https://en.bitcoin.it/wiki/Technical_background_of_version_1_Bitcoin_addresses) that it is between 25 and 34 characters. Is this correct? Is there anything more that is possible to check that it is a correct public key?


Title: Re: What is possible to validate on a public key
Post by: TimS on September 29, 2014, 08:31:31 PM
No, you can't check if the address represents a valid public key, because you can't reverse the hashing that turned the public key into the address.

You can check if a given string is a valid base58check encoding of some data, see what version byte was attached to the address, and how long the actual data encoded is. But the data itself is an inscrutable 20 bytes.


Title: Re: What is possible to validate on a public key
Post by: hhanh00 on September 30, 2014, 01:45:17 AM
Well, a hash is 20 byte long and the public key is 'nearly' 32 byte long. So essentially, there are thousands of public keys for any address. But of course, you can't check whether someone knows the matching private key.


Title: Re: What is possible to validate on a public key
Post by: varChar on September 30, 2014, 06:06:19 AM
Ok. Perhaps I discribed it wrong. But not to see if it is a valid public key. More if it is a public key. Thing like that it is >= 25 && <= 34, begins with 1 or 3(?). And things like that.
But perhaps it's enough to see if it is a valid base58check as you said. Didn't know of that. So Perhaps it's what Im searching for. Thanks


Title: Re: What is possible to validate on a public key
Post by: ForgottenPassword on September 30, 2014, 06:09:00 AM
If you are using Bitcoin Core you can use the validateaddress API call.


Title: Re: What is possible to validate on a public key
Post by: varChar on September 30, 2014, 08:19:48 AM
If you are using Bitcoin Core you can use the validateaddress API call.

No it's a c# project where the user will enter a public key.


Title: Re: What is possible to validate on a public key
Post by: ForgottenPassword on September 30, 2014, 08:33:49 AM
If you are using Bitcoin Core you can use the validateaddress API call.

No it's a c# project where the user will enter a public key.

Perhaps you are mixed up, a Bitcoin address and a public key are two different things.

A bitcoin address looks like this:
1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN

A public key looks like this:
04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b8dec5235a0f a8722476c7709c02559e3aa73aa03918ba2d492eea75abea235

If you want to validate a bitcoin address, you can check the length, check if it begins with a 1 or 3, check if it is valid base58 and check if the checksum is valid. Here is a Ruby script that does all of this which you can use as a reference:

https://gist.github.com/alexandrz/4491729

If you want to check if a public key is valid, you can also check the length and check that it is hex. There is no checksum on ecdsa pubkeys. see post below.


Title: Re: What is possible to validate on a public key
Post by: hhanh00 on September 30, 2014, 08:54:29 AM
Ok. Perhaps I discribed it wrong. But not to see if it is a valid public key. More if it is a public key. Thing like that it is >= 25 && <= 34, begins with 1 or 3(?). And things like that.
But perhaps it's enough to see if it is a valid base58check as you said. Didn't know of that. So Perhaps it's what Im searching for. Thanks

You can convert back to Hex. Then verify the checksum and the version byte. The later is what differentiates a bitcoin main address from a testnet address or a altcoin address.

Perhaps you are mixed up, a Bitcoin address and a public key are two different things.

A bitcoin address looks like this:
1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN

A public key looks like this:
04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b8dec5235a0f a8722476c7709c02559e3aa73aa03918ba2d492eea75abea235

If you want to check if a public key is valid, you can also check the length and check that it is hex. There is no checksum on ecdsa pubkeys.

There are some checks you can do on a public key.
There are two types of pub keys in bitcoin: compressed or uncompressed. They are 33 and 65 byte long respectively.
If it begins with 04, it's uncompressed. If it's 02 or 03, it is compressed.
So you can check the first byte.
If it's uncompressed, the next 32 + 32 bytes is the X and Y coordinates of a point on the Y^2 = X^3 + 7 curve. You can check that too
Note that it must be done modulo N = FFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141
If it's compressed, only the X coordinate is given. It must be lower than N

This can be done by your EC crypto library when you decode the pubkey.


Title: Re: What is possible to validate on a public key
Post by: varChar on September 30, 2014, 01:42:01 PM
If you are using Bitcoin Core you can use the validateaddress API call.

No it's a c# project where the user will enter a public key.

Perhaps you are mixed up, a Bitcoin address and a public key are two different things.

A bitcoin address looks like this:
1HZwkjkeaoZfTSaJxDw6aKkxp45agDiEzN

A public key looks like this:
04a34b99f22c790c4e36b2b3c2c35a36db06226e41c692fc82b8b56ac1c540c5bd5b8dec5235a0f a8722476c7709c02559e3aa73aa03918ba2d492eea75abea235

If you want to validate a bitcoin address, you can check the length, check if it begins with a 1 or 3, check if it is valid base58 and check if the checksum is valid. Here is a Ruby script that does all of this which you can use as a reference:

https://gist.github.com/alexandrz/4491729

If you want to check if a public key is valid, you can also check the length and check that it is hex. There is no checksum on ecdsa pubkeys. see post below.

Thank you! It's the address I want to check. Actually thought that the address was the public key.