You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

133 lines
3.9 KiB

  1. package main
  2. import (
  3. "fmt"
  4. "github.com/tendermint/tendermint/crypto"
  5. )
  6. // SECRET
  7. var SECRET = []byte("some secret")
  8. func printEd() {
  9. priv := crypto.GenPrivKeyEd25519FromSecret(SECRET)
  10. pub := priv.PubKey().(crypto.PubKeyEd25519)
  11. sigV, err := priv.Sign([]byte("hello"))
  12. if err != nil {
  13. fmt.Println("Unexpected error:", err)
  14. }
  15. sig := sigV.(crypto.SignatureEd25519)
  16. name := "tendermint/PubKeyEd25519"
  17. length := len(pub[:])
  18. fmt.Println("### PubKeyEd25519")
  19. fmt.Println("")
  20. fmt.Println("```")
  21. fmt.Printf("// Name: %s\n", name)
  22. fmt.Printf("// PrefixBytes: 0x%X \n", pub.Bytes()[:4])
  23. fmt.Printf("// Length: 0x%X \n", length)
  24. fmt.Println("// Notes: raw 32-byte Ed25519 pubkey")
  25. fmt.Println("type PubKeyEd25519 [32]byte")
  26. fmt.Println("")
  27. fmt.Println(`func (pubkey PubKeyEd25519) Address() []byte {
  28. // NOTE: hash of the Amino encoded bytes!
  29. return RIPEMD160(AminoEncode(pubkey))
  30. }`)
  31. fmt.Println("```")
  32. fmt.Println("")
  33. fmt.Printf("For example, the 32-byte Ed25519 pubkey `%X` would be encoded as `%X`.\n\n", pub[:], pub.Bytes())
  34. fmt.Printf("The address would then be `RIPEMD160(0x%X)` or `%X`\n", pub.Bytes(), pub.Address())
  35. fmt.Println("")
  36. name = "tendermint/SignatureKeyEd25519"
  37. length = len(sig[:])
  38. fmt.Println("### SignatureEd25519")
  39. fmt.Println("")
  40. fmt.Println("```")
  41. fmt.Printf("// Name: %s\n", name)
  42. fmt.Printf("// PrefixBytes: 0x%X \n", sig.Bytes()[:4])
  43. fmt.Printf("// Length: 0x%X \n", length)
  44. fmt.Println("// Notes: raw 64-byte Ed25519 signature")
  45. fmt.Println("type SignatureEd25519 [64]byte")
  46. fmt.Println("```")
  47. fmt.Println("")
  48. fmt.Printf("For example, the 64-byte Ed25519 signature `%X` would be encoded as `%X`\n", sig[:], sig.Bytes())
  49. fmt.Println("")
  50. name = "tendermint/PrivKeyEd25519"
  51. fmt.Println("### PrivKeyEd25519")
  52. fmt.Println("")
  53. fmt.Println("```")
  54. fmt.Println("// Name:", name)
  55. fmt.Println("// Notes: raw 32-byte priv key concatenated to raw 32-byte pub key")
  56. fmt.Println("type PrivKeyEd25519 [64]byte")
  57. fmt.Println("```")
  58. }
  59. func printSecp() {
  60. priv := crypto.GenPrivKeySecp256k1FromSecret(SECRET)
  61. pub := priv.PubKey().(crypto.PubKeySecp256k1)
  62. sigV, err := priv.Sign([]byte("hello"))
  63. if err != nil {
  64. fmt.Println("Unexpected error:", err)
  65. }
  66. sig := sigV.(crypto.SignatureSecp256k1)
  67. name := "tendermint/PubKeySecp256k1"
  68. length := len(pub[:])
  69. fmt.Println("### PubKeySecp256k1")
  70. fmt.Println("")
  71. fmt.Println("```")
  72. fmt.Printf("// Name: %s\n", name)
  73. fmt.Printf("// PrefixBytes: 0x%X \n", pub.Bytes()[:4])
  74. fmt.Printf("// Length: 0x%X \n", length)
  75. fmt.Println("// Notes: OpenSSL compressed pubkey prefixed with 0x02 or 0x03")
  76. fmt.Println("type PubKeySecp256k1 [33]byte")
  77. fmt.Println("")
  78. fmt.Println(`func (pubkey PubKeySecp256k1) Address() []byte {
  79. // NOTE: hash of the raw pubkey bytes (not Amino encoded!).
  80. // Compatible with Bitcoin addresses.
  81. return RIPEMD160(SHA256(pubkey[:]))
  82. }`)
  83. fmt.Println("```")
  84. fmt.Println("")
  85. fmt.Printf("For example, the 33-byte Secp256k1 pubkey `%X` would be encoded as `%X`\n\n", pub[:], pub.Bytes())
  86. fmt.Printf("The address would then be `RIPEMD160(SHA256(0x%X))` or `%X`\n", pub[:], pub.Address())
  87. fmt.Println("")
  88. name = "tendermint/SignatureKeySecp256k1"
  89. fmt.Println("### SignatureSecp256k1")
  90. fmt.Println("")
  91. fmt.Println("```")
  92. fmt.Printf("// Name: %s\n", name)
  93. fmt.Printf("// PrefixBytes: 0x%X \n", sig.Bytes()[:4])
  94. fmt.Printf("// Length: Variable\n")
  95. fmt.Printf("// Encoding prefix: Variable\n")
  96. fmt.Println("// Notes: raw bytes of the Secp256k1 signature")
  97. fmt.Println("type SignatureSecp256k1 []byte")
  98. fmt.Println("```")
  99. fmt.Println("")
  100. fmt.Printf("For example, the Secp256k1 signature `%X` would be encoded as `%X`\n", []byte(sig[:]), sig.Bytes())
  101. fmt.Println("")
  102. name = "tendermint/PrivKeySecp256k1"
  103. fmt.Println("### PrivKeySecp256k1")
  104. fmt.Println("")
  105. fmt.Println("```")
  106. fmt.Println("// Name:", name)
  107. fmt.Println("// Notes: raw 32-byte priv key")
  108. fmt.Println("type PrivKeySecp256k1 [32]byte")
  109. fmt.Println("```")
  110. }
  111. func main() {
  112. printEd()
  113. fmt.Println("")
  114. printSecp()
  115. }