2020-09-10 12:33:41 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-09-18 12:36:13 +00:00
|
|
|
"strings"
|
2020-09-10 12:33:41 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type testCase struct {
|
|
|
|
decoded []byte
|
|
|
|
encoded string
|
|
|
|
}
|
|
|
|
|
|
|
|
// B32Decode takes a Crockford Base32 string and converts it
|
|
|
|
// to a byte array.
|
|
|
|
func B32Decode(input string) []byte {
|
|
|
|
output, error := encoder.DecodeString(input)
|
2020-10-28 13:06:56 +00:00
|
|
|
check(error, "Error decoding Base32 string %s", input)
|
2020-09-10 12:33:41 +00:00
|
|
|
return output
|
|
|
|
}
|
2020-09-18 12:36:13 +00:00
|
|
|
|
2020-10-15 12:30:30 +00:00
|
|
|
func decodeMhash(input string) []byte {
|
|
|
|
return []byte(B32Decode(input[5:]))
|
|
|
|
}
|
|
|
|
|
2020-11-08 01:29:36 +00:00
|
|
|
func isBlob(input string) bool {
|
|
|
|
if len(input) < 5 {
|
|
|
|
return false
|
|
|
|
}
|
2020-12-10 13:16:20 +00:00
|
|
|
return input[0:5] == BlobSigil
|
2020-11-08 01:29:36 +00:00
|
|
|
}
|
|
|
|
|
2020-09-18 12:36:13 +00:00
|
|
|
func validateMhash(input string) string {
|
2020-12-10 13:16:20 +00:00
|
|
|
array := strings.Split(input, ".")
|
|
|
|
if len(array) != 2 {
|
2020-09-24 23:43:11 +00:00
|
|
|
panicf("Expected '%s' to be an mHash", input)
|
2020-09-18 12:36:13 +00:00
|
|
|
}
|
2020-12-10 13:16:20 +00:00
|
|
|
switch array[0] + "." {
|
2020-09-18 12:36:13 +00:00
|
|
|
case BlobSigil, MessageSigil, PeerSigil:
|
|
|
|
return input
|
|
|
|
}
|
|
|
|
msg := "Expected left side of Mhash dot to be one of %s, %s, %s. Got: %s"
|
2020-12-10 13:16:20 +00:00
|
|
|
panicf(msg, BlobSigil, MessageSigil, PeerSigil, array[0])
|
2020-09-18 12:36:13 +00:00
|
|
|
return input
|
|
|
|
}
|