Created
September 12, 2024 06:50
-
-
Save prasincs/3c133f3fc75931c1f6c4408a6231ae54 to your computer and use it in GitHub Desktop.
Basic Bitcoin tx verifier
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"encoding/hex" | |
"fmt" | |
"github.com/btcsuite/btcd/btcec/v2" | |
"github.com/btcsuite/btcd/btcec/v2/ecdsa" | |
"github.com/btcsuite/btcd/chaincfg/chainhash" | |
) | |
func main() { | |
// working p2pk signature | |
// pubKeyHex := "0411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3" | |
// messageHex := "0100000001c997a5e56e104102fa209c6a852dd90660a20b2d9c352423edce25857fcd37040000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3acffffffff0200ca9a3b00000000434104ae1a62fe09c5f51b13905f07f06b99a2f7159b2225f374cd378d71302fa28414e7aab37397f554a7df5f142c21c1b7303b8a0626f1baded5c72a704f7e6cd84cac00286bee0000000043410411db93e1dcdb8a016b49840f8c53bc1eb68a382e97b1482ecad7b148a6909a5cb2e0eaddfb84ccf9744464f82e160bfa9b8b64f9d4c03f999b8643f656b412a3ac0000000001000000" | |
// signatureHex := "304402204e45e16932b8af514961a1d3a1a25fdf3f4f7732e9d624c6c61548ab5fb8cd410220181522ec8eca07de4860a4acdd12909d831cc56cbbac4622082221a8768d1d0901" | |
// not working p2pk signature | |
pubKeyHex := "04889fcdfd7c5430d13f1eb5f508e2e87f38d2406fad8425a824e032ccb371ef62465331e1a6334d7c3770a2ad2a958e740130343399d01dbd87426db850f9faf9" | |
messageHex := "01000000020dd7891efbf67da47c651531db8aab3144ed7a524e4ae1e30b773525e27ddd7b00000000434104889fcdfd7c5430d13f1eb5f508e2e87f38d2406fad8425a824e032ccb371ef62465331e1a6334d7c3770a2ad2a958e740130343399d01dbd87426db850f9faf9acffffffff16851666962e37a75a246101f2e340c628b1db3c045d4d3cfb2d1c0f58f97c6f0000000000ffffffff02c0c62d00000000001976a91417194e1bd175fb5b1b2a1f9d221f6f5c29e1928388ac00c817a8040000001976a91465bda9b05f7e9a8f96a7f4ba0996a877708ef90888ac0000000001000000" | |
signatureHex := "304502206f6a68710a51f77e5a1fa4d1037a23a76723724a51fd54710949e0189ee02dfa022100dad3454ade12fe84f3818e14c41ec2e02bbb154dd3136a094cdf86f67ebbe0b601" | |
// Decode the public key | |
pubKeyBytes, err := hex.DecodeString(pubKeyHex) | |
if err != nil { | |
fmt.Println("Error decoding public key:", err) | |
return | |
} | |
pubKey, err := btcec.ParsePubKey(pubKeyBytes) | |
if err != nil { | |
fmt.Println("Error parsing public key:", err) | |
return | |
} | |
// Decode the message (transaction hash) | |
messageBytes, err := hex.DecodeString(messageHex) | |
if err != nil { | |
fmt.Println("Error decoding message:", err) | |
return | |
} | |
messageHash := chainhash.DoubleHashB(messageBytes) | |
// Decode the signature | |
signatureBytes, err := hex.DecodeString(signatureHex) | |
if err != nil { | |
fmt.Println("Error decoding signature:", err) | |
return | |
} | |
signature, err := ecdsa.ParseDERSignature(signatureBytes) | |
if err != nil { | |
fmt.Println("Error parsing signature:", err) | |
return | |
} | |
fmt.Println("Message Hash:", hex.EncodeToString(messageHash)) | |
// Verify the signature | |
valid := signature.Verify(messageHash, pubKey) | |
if valid { | |
fmt.Println("Signature is valid!") | |
} else { | |
fmt.Println("Signature is invalid!") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment