Пример #1
0
// Decode decodes the provided StrKey into a raw value, checking the checksum
// and ensuring the expected VersionByte (the version parameter) is the value
// actually encoded into the provided src string.
func Decode(expected VersionByte, src string) ([]byte, error) {
	if err := checkValidVersionByte(expected); err != nil {
		return nil, err
	}

	raw, err := base32.StdEncoding.DecodeString(src)
	if err != nil {
		return nil, err
	}

	if len(raw) < 3 {
		return nil, errors.Errorf("encoded value is %d bytes; minimum valid length is 3", len(raw))
	}

	// decode into components
	version := VersionByte(raw[0])
	vp := raw[0 : len(raw)-2]
	payload := raw[1 : len(raw)-2]
	checksum := raw[len(raw)-2:]

	// ensure version byte is expected
	if version != expected {
		return nil, ErrInvalidVersionByte
	}

	// ensure checksum is valid
	if err := crc16.Validate(vp, checksum); err != nil {
		return nil, err
	}

	// if we made it through the gaunlet, return the decoded value
	return payload, nil
}
Пример #2
0
func initDriver(cfg Config) (federation.Driver, error) {
	var dialect string

	switch cfg.Database.Type {
	case "mysql":
		dialect = "mysql"
	case "postgres":
		dialect = "postgres"
	case "sqlite3":
		dialect = "sqlite3"
	default:
		return nil, errors.Errorf("Invalid db type: %s", cfg.Database.Type)
	}

	repo, err := db.Open(dialect, cfg.Database.URL)
	if err != nil {
		return nil, errors.Wrap(err, "db open failed")
	}

	sqld := federation.SQLDriver{
		DB:                repo.DB.DB, // unwrap the repo to the bare *sql.DB instance,
		LookupRecordQuery: cfg.Queries.Federation,
	}

	if cfg.Queries.ReverseFederation == "" {
		return &sqld, nil
	}

	rsqld := federation.ReverseSQLDriver{
		SQLDriver:                sqld,
		LookupReverseRecordQuery: cfg.Queries.ReverseFederation,
	}

	return &rsqld, nil
}
Пример #3
0
// getJSON populates `dest` with the contents at `url`, provided the request
// succeeds and the json can be successfully decoded.
func (c *Client) getJSON(url string, dest interface{}) error {
	hresp, err := c.HTTP.Get(url)
	if err != nil {
		return errors.Wrap(err, "http get errored")
	}

	defer hresp.Body.Close()

	if !(hresp.StatusCode >= 200 && hresp.StatusCode < 300) {
		return errors.Errorf("http get failed with (%d) status code", hresp.StatusCode)
	}

	err = json.NewDecoder(hresp.Body).Decode(dest)
	if err != nil {
		return errors.Wrap(err, "json decode errored")
	}

	return nil
}