SoFunction
Updated on 2025-03-03

Go language practical combat to implement Bitcoin address verification steps

Steps to generate Bitcoin address from the public key

  1. Take a random 32-bit random number as the private key
  2. Using the produced random numbers, the public key is generated using the elliptical encryption algorithm.
  3. Calculate the sha256 hash value of the public key
  4. Calculate the RIPEMD-160 hash value
  5. Step 4 plus version number (bitcoin is 0x00)
  6. Take the sha256 hash value twice for the result of step 5
  7. Take the first four bytes of the previous result
  8. Add the result of step 7 to the result of step 1 as a verification
  9. Use base58 to change the result of step 8 to get the address

The generated address code is as follows

func (w Wallet) GetAddress() []byte {
    pubKeyHash := HashPubKey()

    versionedPayload := append([]byte{version}, pubKeyHash...)
    checksum := checksum(versionedPayload)

    fullPayload := append(versionedPayload, checksum...)
    address := Base58Encode(fullPayload)

    return address
}
func HashPubKey(pubKey []byte) []byte {
    publicSHA256 := sha256.Sum256(pubKey)

    RIPEMD160Hasher := ()
    _, err := (publicSHA256[:])
    publicRIPEMD160 := (nil)

    return publicRIPEMD160
}

func checksum(payload []byte) []byte {
    firstSHA := sha256.Sum256(payload)
    secondSHA := sha256.Sum256(firstSHA[:])

    return secondSHA[:addressChecksumLen]
}

Verify Bitcoin

Is the address correct?

addressChecksumLen:=4
func ValidateAddress(address string) bool {
    pubKeyHash := Base58Decode([]byte(address))
    actualChecksum := pubKeyHash[len(pubKeyHash)-addressChecksumLen:]
    version := pubKeyHash[0]
    pubKeyHash = pubKeyHash[1 : len(pubKeyHash)-addressChecksumLen]
    targetChecksum := checksum(append([]byte{version}, pubKeyHash...))
    return (actualChecksum, targetChecksum) == 0
}

Base58Decode decodes the bitcoin address, then takes the last four check bits actualChecksum, and uses pubKeyHash that removes the check bit to calculate the check bit and the check bit of the address to verify the correctness of the address. The functions used are:

func checksum(payload []byte) []  //Use two shah256 to find the check bit byte {
    firstSHA := sha256.Sum256(payload)
    secondSHA := sha256.Sum256(firstSHA[:])

    return secondSHA[:addressChecksumLen]
}

This is a decoded function, and there is already a lot of existing code support, so I won't explain it

func Base58Decode(input []byte) []byte {
    result := (0)
    zeroBytes := 0

    for b := range input {
        if b == 0x00 {
            zeroBytes++
        }
    }

    payload := input[zeroBytes:]
    for _, b := range payload {
        charIndex := (b58Alphabet, b)
        (result, (58))
        (result, (int64(charIndex)))
    }

    decoded := ()
    decoded = append(([]byte{byte(0x00)}, zeroBytes), decoded...)

    return decoded
}

The above is the detailed content of the actual implementation of Bitcoin address verification steps in Go language. For more information about Bitcoin address verification in Go language, please pay attention to my other related articles!