当前位置:   article > 正文

GO语言,区块链私钥公钥生成以及钱包地址生成_go 区块链的钱包怎么生成

go 区块链的钱包怎么生成
/*GetAddress
todo 6、区块链地址的生成
根据钱包publicKey生成地址
1、获取钱包公钥publicKey
2、sha256加密publicKey => hash1
3、hash1 再进行 ripemd160算法加密 => 20字节 公钥publicKeyHash
4、拼接Version => 左半段 21字节(leftBytes)
5、右半段 进行两次sha256 => checkSum
6、取checkSum前4个字节 => checkRight
7、与左半段21字节(leftBytes) + checkRight 拼接 => 25Bytes
8、25Bytes 进行base58()编码 => address
*/

  1. package main
  2. import (
  3. "crypto/ecdsa"
  4. "crypto/elliptic"
  5. "crypto/rand"
  6. "crypto/sha256"
  7. "fmt"
  8. "github.com/iocn-io/ripemd160"
  9. "github.com/mr-tron/base58/base58"
  10. "log"
  11. )
  12. func main() {
  13. /*
  14. todo 5、调用区块链钱包,查看公私钥
  15. */
  16. var wallet = NewWalletKeuPair()
  17. fmt.Printf("Private:%s\n",wallet.PrivateKey)
  18. fmt.Println("-------------")
  19. fmt.Printf("PublickKey : %x\n",wallet.PublicKey)
  20. /*
  21. todo 7、获取区块链钱包地址
  22. */
  23. address := wallet.GetAddress()
  24. fmt.Printf("区块链钱包地址为:%s\n",address)
  25. }
  26. /*
  27. todo 1、创建一个项目
  28. todo 2、创建一个文件夹,创建一个address.go 文件
  29. */
  30. /*WalletKeyPair
  31. todo 3、创建区块链钱包结构体
  32. */
  33. type WalletKeyPair struct {
  34. PrivateKey *ecdsa.PrivateKey // 私钥
  35. PublicKey []byte // 公钥
  36. }
  37. /*NewWalletKeuPair
  38. todo 4、生成区块链钱包公私钥
  39. 返回区块链钱包
  40. */
  41. func NewWalletKeuPair() *WalletKeyPair {
  42. // 第一步,生成私钥
  43. privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
  44. if err != nil {
  45. log.Panic(err)
  46. }
  47. publickeyRow := privateKey.PublicKey
  48. // 第二步:拼接得到公钥
  49. publicKey := append(publickeyRow.X.Bytes(), publickeyRow.Y.Bytes()...)
  50. // 第三步:组装钱包地址的私钥公钥。并返回
  51. walletKeyPair := WalletKeyPair{
  52. privateKey,
  53. publicKey,
  54. }
  55. return &walletKeyPair
  56. }
  57. /*GetAddress
  58. todo 6、区块链地址的生成
  59. 根据钱包publicKey生成地址
  60. 1、获取钱包公钥publicKey
  61. 2、sha256加密publicKey => hash1
  62. 3、hash1 再进行 ripemd160算法加密 => 20字节 公钥publicKeyHash
  63. 4、拼接Version => 左半段 21字节(leftBytes)
  64. 5、右半段 进行两次sha256 => checkSum
  65. 6、取checkSum前4个字节 => checkRight
  66. 7、与左半段21字节(leftBytes) + checkRight 拼接 => 25Bytes
  67. 8、25Bytes 进行base58()编码 => address
  68. */
  69. func (wallet *WalletKeyPair) GetAddress() string {
  70. //1、获取钱包公钥publicKey
  71. publicKey := wallet.PublicKey
  72. //2、sha256加密publicKey => hash1
  73. hash1 := sha256.Sum256(publicKey)
  74. //3、hash1 再进行 ripemd160算法加密 => 20字节 公钥publicKeyHash
  75. rip160Haher := ripemd160.New()
  76. _, err := rip160Haher.Write(hash1[:])
  77. if err != nil {
  78. log.Panic(err)
  79. }
  80. publicKeyHash := rip160Haher.Sum(nil)
  81. //4、拼接Version => 左半段 21字节(leftBytes)
  82. version := 0x00
  83. leftBytes := append([]byte{byte(version)}, publicKeyHash...)
  84. //5、右半段 进行两次sha256 => checkSum
  85. first := sha256.Sum256(leftBytes)
  86. second := sha256.Sum256(first[:])
  87. //6、取checkSum前4个字节 => checkRight
  88. // 4个字节
  89. checkRight := second[0:4]
  90. // 7、与左半段21字节(leftBytes) + checkRight 拼接 => 25Bytes
  91. t25Bytes := append(leftBytes, checkRight...)
  92. fmt.Printf("25字节: %+v\n",t25Bytes)
  93. //8、25Bytes 进行base58()编码 => address
  94. // 返回地址
  95. address := base58.Encode(t25Bytes)
  96. return address
  97. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/931577
推荐阅读
相关标签
  

闽ICP备14008679号