Esempio n. 1
0
func VerifyMedia(b []byte) (AlexandriaMedia, map[string]interface{}, error) {

	var v AlexandriaMedia
	var i interface{}
	var m map[string]interface{}

	if !strings.HasPrefix(string(b), `{ "alexandria-media"`) &&
		!strings.HasPrefix(string(b), `{"alexandria-media"`) &&
		!strings.HasPrefix(string(b), `{"media-data"`) &&
		!strings.HasPrefix(string(b), `{ "media-data"`) {
		return v, nil, ErrWrongPrefix
	}

	if !utility.IsJSON(string(b)) {
		return v, m, ErrNotJSON
	}

	// fmt.Printf("Attempting to verify alexandria-media JSON...")
	err := json.Unmarshal(b, &v)
	if err != nil {
		return v, m, err
	}

	err = json.Unmarshal(b, &i)
	if err != nil {
		return v, m, err
	}

	m = i.(map[string]interface{})
	var signature string

	// check the JSON object root key
	// find the signature string
	for key, val := range m {
		if key == "signature" {
			signature = val.(string)
		} else {
			if key != MEDIA_ROOT_KEY {
				return v, m, errors.New("can't verify media - JSON object root key doesn't match accepted value")
			}
		}
	}

	// fmt.Printf("*** debug: JSON object root matches, printing v:\n%v\n*** /debug ***\n", v)
	err = checkRequiredMediaFields(v, signature)
	if err != nil {
		return v, m, err
	}

	// verify signature was created by this address
	// signature pre-image for media is <torrenthash>-<publisher>-<timestamp>
	val, _ := utility.CheckSignature(v.AlexandriaMedia.Publisher, signature, v.AlexandriaMedia.Torrent+"-"+v.AlexandriaMedia.Publisher+"-"+strconv.FormatInt(v.AlexandriaMedia.Timestamp, 10))
	if val == false {
		return v, m, ErrBadSignature
	}

	// fmt.Println(" -- VERIFIED --")
	return v, m, nil

}
Esempio n. 2
0
func VerifyPublisher(b []byte) (AlexandriaPublisher, error) {

	var v AlexandriaPublisher
	var i interface{}
	var m map[string]interface{}

	if !strings.HasPrefix(string(b), `{ "alexandria-publisher"`) &&
		!strings.HasPrefix(string(b), `{"alexandria-publisher"`) {
		return v, ErrWrongPrefix
	}

	// fmt.Printf("Attempting to verify alexandria-publisher JSON...")

	if !utility.IsJSON(string(b)) {
		return v, ErrNotJSON
	}

	err := json.Unmarshal(b, &v)
	if err != nil {
		return v, err
	}

	errr := json.Unmarshal(b, &i)
	if errr != nil {
		return v, err
	}

	m = i.(map[string]interface{})
	var signature string

	// check the JSON object root key
	// find the signature string
	for key, val := range m {
		if key == "signature" {
			signature = val.(string)
		} else {
			if key != PUBLISHER_ROOT_KEY {
				return v, errors.New("can't verify publisher - JSON object root key doesn't match accepted value")
			}
		}
	}

	// verify signature
	if v.Signature != signature {
		return v, ErrBadSignature
	}

	// verify signature was created by this address
	// signature pre-image for publisher is <name>-<address>-<timestamp>
	val, _ := utility.CheckSignature(v.AlexandriaPublisher.Address, signature, v.AlexandriaPublisher.Name+"-"+v.AlexandriaPublisher.Address+"-"+strconv.FormatInt(v.AlexandriaPublisher.Timestamp, 10))
	if val == false {
		return v, ErrBadSignature
	}

	// fmt.Println(" -- VERIFIED --")
	return v, nil

}
Esempio n. 3
0
func VerifyOIP041(s string, block int) (Oip041, error) {
	if block < 1997454 {
		return Oip041{}, ErrTooEarly
	}

	if !utility.IsJSON(s) {
		return Oip041{}, ErrNotJSON
	}

	dec, err := DecodeOIP041(s)
	if err != nil {
		return dec, err
	}
	if dec.Signature == "" {
		return dec, ErrBadSignature
	}
	// ToDo: Validate signature

	return dec, nil
}
Esempio n. 4
0
func VerifyDeactivation(b []byte) (AlexandriaDeactivation, error) {

	var v AlexandriaDeactivation
	var i interface{}
	var m map[string]interface{}

	if !strings.HasPrefix(string(b), `{"alexandria-deactivation"`) {
		return v, errors.New("Not alexandria-deactivation")
	}

	if !utility.IsJSON(string(b)) {
		return v, ErrNotJSON
	}

	err := json.Unmarshal(b, &v)
	if err != nil {
		return v, err
	}

	errr := json.Unmarshal(b, &i)
	if errr != nil {
		return v, err
	}

	m = i.(map[string]interface{})
	var signature string

	// check the JSON object root key
	// find the signature string
	for key, val := range m {
		if key == "signature" {
			signature = val.(string)
		} else {
			if key != DEACTIVATION_ROOT_KEY {
				return v, errors.New("can't verify deactivation - JSON object root key doesn't match accepted value")
			}
		}
	}

	// verify txid
	rt := regexp.MustCompile("^[a-fA-F0-9]*$")
	if !rt.MatchString(v.AlexandriaDeactivation.Txid) || len(v.AlexandriaDeactivation.Txid) != 64 {
		return v, errors.New("can't verify deactivation - txid in incorrect format")
	}

	// verify address
	ra := regexp.MustCompile("^[a-zA-Z0-9]*$")
	if !ra.MatchString(v.AlexandriaDeactivation.Address) || len(v.AlexandriaDeactivation.Address) != 34 {
		return v, errors.New("can't verify deactivation - address in incorrect format")
	}

	// verify signature
	if v.Signature != signature {
		return v, ErrBadSignature
	}

	// verify signature was created by this address
	// signature pre-image for deactivation is <address>-<txid>
	val, _ := utility.CheckSignature(v.AlexandriaDeactivation.Address, signature, v.AlexandriaDeactivation.Address+"-"+v.AlexandriaDeactivation.Txid)
	if val == false {
		return v, ErrBadSignature
	}

	return v, nil

}