Bitcoin Forum
May 21, 2024, 12:21:31 PM *
News: Latest Bitcoin Core release: 27.0 [Torrent]
 
  Home Help Search Login Register More  
  Show Posts
Pages: [1]
1  Bitcoin / Development & Technical Discussion / private key to public key generation bitcoin vb.net gives unknown error on: February 25, 2020, 10:08:26 AM
I'm trying to use secp256k1 eliptic curve to get the public key from private key but it gives me an unknown error error

Quote
Private Sub Button13_Click(sender As Object, e As EventArgs) Handles Button13.Click
    Dim private_key = "68040878110175628235481263019639686"
    'public key should be Nr6MbFUfMovKCX4vd5YpQnRYsN4rq6pNPEEBKmicAEwwuYLpJrt5LsRvfvR2G8pJ5rMchEMWDYJ7rdY GY7PjxHEa
    Dim public_key As String


    Using eliptic As New ECDsaCng()
        eliptic.HashAlgorithm = CngAlgorithm.ECDsaP256

        Dim data() As Byte = Encoding.UTF8.GetBytes(private_key)
        Dim key As Byte() = eliptic.SignData(data)
        public_key = key.ToString
    End Using

    TextBox15.Text = public_key
End Sub
2  Bitcoin / Development & Technical Discussion / Re: How to generate the hash of public key in Bitcoin on: February 25, 2020, 05:02:28 AM
updated with code, now gets an error
3  Bitcoin / Development & Technical Discussion / Re: How to generate the hash of public key in Bitcoin on: February 24, 2020, 10:06:09 AM
Public keys are not hexadecimal, this is an actual bitcoin public address :
Quote
mwZVuA4D5TSSymdXZza4S1YhZFnH2fcaeG
 see it's not a hexadecimal

there is a difference between "public key" and an "address" or if you like public address. public key is an x,y coordinate of a point located on the elliptic curve while address is a base58 encoding of hash of the public key.
what you posted here is a testnet address and it is using base58 encoding. (you still should use Unicode to decode this)

however public keys are usually presented in base-16 or hexadecimal format. additionally in your code the variable called public_key is indeed a public key and is indeed using hexadecimal encoding.

Thank you for the explanation I didn't know that, but I changed the code to convert it to byte array from hex string. then it gives me this error. I think it's because of the odd number of characters of the public key
Quote
System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string.
Parameter name: length'

4  Bitcoin / Development & Technical Discussion / Re: How to generate the hash of public key in Bitcoin on: February 24, 2020, 08:53:09 AM
Public keys are not hexadecimal, this is an actual bitcoin public address :
Quote
mwZVuA4D5TSSymdXZza4S1YhZFnH2fcaeG
  see it's not a hexadecimal
5  Bitcoin / Development & Technical Discussion / How to generate the hash of public key in Bitcoin on: February 24, 2020, 08:14:35 AM
I'm trying to hash the public key with SHA256 and then ripemed160 and get the hash. but it gives the error "System.ArgumentOutOfRangeException: 'Index and length must refer to a location within the string. Parameter name: length'" I think it's because of the odd number of characters of the public key.

Public key: 2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737

Hash should be: 93ce48570b55c42c2af816aeaba06cfee1224fae

Code:
Imports System.Security.Cryptography
Imports System.Text
Imports System.Globalization
Imports System.Numerics

Private Function HexStringToByteArray(ByVal shex As String) As Byte()
    Dim B As Byte() = Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
    Return Enumerable.Range(0, shex.Length).Where(Function(x) x Mod 2 = 0).[Select](Function(x) Convert.ToByte(shex.Substring(x, 2), 16)).ToArray()
End Function


Private Sub Button12_Click(sender As Object, e As EventArgs) Handles Button12.Click

Dim public_key as String = "2b4632d08485ff1df2db55b9dafd23347d1c47a457072a1e87be26896549a8737"
   Dim pubkey_hash As String

    Dim sha256 As SHA256 = SHA256Managed.Create()
      Dim bytes As Byte() = HexStringToByteArray(public_key)
    Dim hash As Byte() = sha256.ComputeHash(bytes)
    Dim stringBuilder1 As New StringBuilder()

    For i As Integer = 0 To hash.Length - 1
        stringBuilder1.Append(hash(i).ToString("X2"))
    Next

    bytes = HexStringToByteArray(stringBuilder1.ToString())

    Dim ripemd160 As RIPEMD160 = RIPEMD160Managed.Create()
    Dim hash_160 As Byte() = ripemd160.ComputeHash(bytes)

    Dim stringBuilder2 As New StringBuilder()

    For i As Integer = 0 To hash_160.Length - 1
        stringBuilder2.Append(hash_160(i).ToString("X2"))
    Next

    Dim arr() As Char = stringBuilder2.ToString

    pubkey_hash = arr.tostring
    TextBox14.Text = pubkey_hash


   End Sub


what I want to achieve is : bitcoin Public key --> SHA256 --> RIPEMD160 = final hash
6  Bitcoin / Development & Technical Discussion / Re: Generating the candidate block and merkle root on: February 19, 2020, 05:59:40 AM
so to hash it as a candidate block what should I do? concatenate all the information and double SHA256 it and convert to little endian?

Code:
(hash (null) & index (UINT32_MAX) & script bytes & height & coinbase script & sequence) --> convert to little endian? 

you should start learning from transactions first then move to blocks, merkle root and then mining. scroll above and read about a transaction structure and its serialization. https://bitcoin.org/en/developer-reference#raw-transaction-format

what you use in merkle root is the transaction hashes that is calculated by computing the double SHA256 of the serialized transaction (version | txInCount | {txin1 | txin2|...} | txOutCount | {txout1 | txout2 |...} | locktime) + witness and its flag if available.

then to compute merkle root you put hashes together 2 by 2 and build a "tree" until you reach 1 hash as the "root" and put that root in the header before hashing the header. https://bitcoin.org/en/developer-reference#merkle-trees
for example a block with 5 transactions:
Code:
tx1 | tx2
          -> hash1
tx3 | tx4            -> root
          -> hash2
tx5 | tx5


Thank you for your reply,

I followed your link https://bitcoin.org/en/developer-reference#raw-transaction-format and still I don't see anywhere that I can place my public wallet address in the following raw transaction format.

Code:
version 	
tx_in count
tx_in
tx_out count
tx_out
lock_time

I briefly know how to generate the merkle root from transactions. I previously watched this video of generating merkle root https://www.youtube.com/watch?v=gUwXCt1qkBU . I understand the transactions should be hashed together to generate the merkle root. What I don't know is how to generate the first transaction (coinbase transaction) which includes my wallet address in it so when I find a hash less than target, I can get the reward.

Please let me know where should I place my wallet address.
7  Bitcoin / Development & Technical Discussion / Generating the candidate block and merkle root on: February 19, 2020, 05:18:13 AM
I'm trying to develop a solo miner for bitcoin, now I'm in the stage of generating the merkle root. To make the coinbase transaction I looked at https://bitcoin.org/en/developer-reference#coinbase , but I don't understand the structure etc

as I understand, to generate the merkle root, the transactions should be taken from mempool and should be hashed plus the coinbase transaction should be placed as the first transaction when hashing.

the above link states he coinbase transaction should be as follows, this is the candidate block structure right?
Code:
hash (null)
index (UINT32_MAX)
script bytes
height
coinbase script
sequence

so to hash it as a candidate block what should I do? concatenate all the information and double SHA256 it and convert to little endian?

Code:
(hash (null) & index (UINT32_MAX) & script bytes & height & coinbase script & sequence) --> convert to little endian? 


and where do I place my public wallet address? inside the "script bytes"? Can someone shed some light here?

Code:
script bytes --> Wallet address: 18ib9rJq7LiNZTSbGL7fHdJmZgWW6QEJCD

 
Pages: [1]
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!