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.

125 lines
3.8 KiB

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