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.

77 lines
2.1 KiB

  1. package address
  2. import "strings"
  3. import "math/big"
  4. // all characters in the base58
  5. const BASE58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
  6. var base58Lookup = map[string]int{
  7. "1": 0, "2": 1, "3": 2, "4": 3, "5": 4, "6": 5, "7": 6, "8": 7,
  8. "9": 8, "A": 9, "B": 10, "C": 11, "D": 12, "E": 13, "F": 14, "G": 15,
  9. "H": 16, "J": 17, "K": 18, "L": 19, "M": 20, "N": 21, "P": 22, "Q": 23,
  10. "R": 24, "S": 25, "T": 26, "U": 27, "V": 28, "W": 29, "X": 30, "Y": 31,
  11. "Z": 32, "a": 33, "b": 34, "c": 35, "d": 36, "e": 37, "f": 38, "g": 39,
  12. "h": 40, "i": 41, "j": 42, "k": 43, "m": 44, "n": 45, "o": 46, "p": 47,
  13. "q": 48, "r": 49, "s": 50, "t": 51, "u": 52, "v": 53, "w": 54, "x": 55,
  14. "y": 56, "z": 57,
  15. }
  16. var bigBase = big.NewInt(58)
  17. func encodeChunk(raw []byte, padding int) (result string) {
  18. remainder := new(big.Int)
  19. remainder.SetBytes(raw)
  20. bigZero := new(big.Int)
  21. for remainder.Cmp(bigZero) > 0 {
  22. current := new(big.Int)
  23. remainder.DivMod(remainder, bigBase, current)
  24. result = string(BASE58[current.Int64()]) + result
  25. }
  26. if len(result) < padding {
  27. result = strings.Repeat("1", (padding-len(result))) + result
  28. }
  29. return
  30. }
  31. func decodeChunk(encoded string) (result []byte) {
  32. bigResult := big.NewInt(0)
  33. currentMultiplier := big.NewInt(1)
  34. tmp := new(big.Int)
  35. for i := len(encoded) - 1; i >= 0; i-- {
  36. tmp.SetInt64(int64(base58Lookup[string(encoded[i])]))
  37. tmp.Mul(currentMultiplier, tmp)
  38. bigResult.Add(bigResult, tmp)
  39. currentMultiplier.Mul(currentMultiplier, bigBase)
  40. }
  41. result = bigResult.Bytes()
  42. return
  43. }
  44. func EncodeDeroBase58(data ...[]byte) (result string) {
  45. var combined []byte
  46. for _, item := range data {
  47. combined = append(combined, item...)
  48. }
  49. length := len(combined)
  50. rounds := length / 8
  51. for i := 0; i < rounds; i++ {
  52. result += encodeChunk(combined[i*8:(i+1)*8], 11)
  53. }
  54. if length%8 > 0 {
  55. result += encodeChunk(combined[rounds*8:], 7)
  56. }
  57. return
  58. }
  59. func DecodeDeroBase58(data string) (result []byte) {
  60. length := len(data)
  61. rounds := length / 11
  62. for i := 0; i < rounds; i++ {
  63. result = append(result, decodeChunk(data[i*11:(i+1)*11])...)
  64. }
  65. if length%11 > 0 {
  66. result = append(result, decodeChunk(data[rounds*11:])...)
  67. }
  68. return
  69. }