Exemple #1
0
func (this *State) ExchangeRateAuthorityIsValid(e interfaces.IEBEntry) bool {

	// convert the conf quthority address into a
	authorityAddress := base58.Decode(this.ExchangeRateAuthorityAddress)
	ecPubPrefix := []byte{0x59, 0x2a}

	if !bytes.Equal(authorityAddress[:2], ecPubPrefix) {
		fmt.Errorf("Invalid Entry Credit Private Address")
		return false
	}

	pub := new([32]byte)
	copy(pub[:], authorityAddress[2:34])

	// in case verify can't handle empty public key
	if this.ExchangeRateAuthorityAddress == "" {
		return false
	}
	sig := new([64]byte)
	externalIds := e.ExternalIDs()

	// check for number of ext ids
	if len(externalIds) < 1 {
		return false
	}

	copy(sig[:], externalIds[0]) // First ext id needs to be the signature of the content

	if !ed.VerifyCanonical(pub, e.GetContent(), sig) {
		return false
	}

	return true
}
func handleSearchResult(content *SearchedStruct, w http.ResponseWriter) {
	// Functions able to be used within the html
	funcMap := template.FuncMap{
		"truncate": func(s string) string {
			bytes := []byte(s)
			hash := sha256.Sum256(bytes)
			str := fmt.Sprintf(" - Bytes: %d <br /> - Hash: %x", len(bytes), hash)
			return str
		},
		"AddressFACorrect": func(s string) string {
			hash, err := primitives.HexToHash(s)
			if err != nil {
				return "There has been an error converting the address"
			}
			prefix := []byte{0x5f, 0xb1}
			addr := hash.Bytes()
			addr = append(prefix, addr[:]...)
			oneSha := sha256.Sum256(addr)
			twoSha := sha256.Sum256(oneSha[:])
			addr = append(addr, twoSha[:4]...)
			str := base58.Encode(addr)
			return str
		},
		"AddressECCorrect": func(s string) string {
			hash, err := primitives.HexToHash(s)
			if err != nil {
				return "There has been an error converting the address"
			}
			prefix := []byte{0x59, 0x2a}
			addr := hash.Bytes()
			addr = append(prefix, addr[:]...)
			oneSha := sha256.Sum256(addr)
			twoSha := sha256.Sum256(oneSha[:])
			addr = append(addr, twoSha[:4]...)
			str := base58.Encode(addr)
			return str
		},
		"TransactionAmountCorrect": func(u uint64) string {
			s := fmt.Sprintf("%d", u)
			f, err := strconv.ParseFloat(s, 64)
			if err != nil {
				return s
			}
			f = f / 1e8
			return fmt.Sprintf("%.8f", f)
		},
	}
	TemplateMutex.Lock()
	templates.Funcs(funcMap)
	files.CustomParseGlob(templates, "templates/searchresults/*.html")
	files.CustomParseFile(templates, "templates/searchresults/type/"+content.Type+".html")
	TemplateMutex.Unlock()

	var err error
	_ = err
	switch content.Type {
	case "entry":
		entry := getEntry(content.Input)
		if entry == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, entry)
		TemplateMutex.Unlock()
		return
	case "chainhead":
		arr := getAllChainEntries(content.Input)
		if arr == nil {
			break
		}
		arr[0].Content = struct {
			Head   interface{}
			Length int
		}{arr[0].Content, len(arr) - 1}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, arr)
		TemplateMutex.Unlock()
		return
	case "eblock":
		eblk := getEblock(content.Input)
		if eblk == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, eblk)
		TemplateMutex.Unlock()
		return
	case "dblock":
		dblk := getDblock(content.Input)
		if dblk == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, dblk)
		TemplateMutex.Unlock()
		return
	case "ablock":
		ablk := getAblock(content.Input)
		if ablk == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, ablk)
		TemplateMutex.Unlock()
		return
	case "fblock":
		fblk := getFblock(content.Input)
		if fblk == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, fblk)
		TemplateMutex.Unlock()
		return
	case "ecblock":
		ecblock := getECblock(content.Input)
		if ecblock == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, ecblock)
		TemplateMutex.Unlock()
		return
	case "entryack":
		entryAck := getEntryAck(content.Input)
		if entryAck == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, entryAck)
		TemplateMutex.Unlock()
		return
	case "factoidack":
		factoidAck := getFactoidAck(content.Input)
		if factoidAck == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, factoidAck)
		TemplateMutex.Unlock()
		return
	case "facttransaction":
		transaction := getFactTransaction(content.Input)
		if transaction == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, transaction)
		TemplateMutex.Unlock()
		return
	case "ectransaction":
		transaction := getEcTransaction(content.Input)
		if transaction == nil {
			break
		}
		TemplateMutex.Lock()
		err = templates.ExecuteTemplate(w, content.Type, transaction)
		TemplateMutex.Unlock()
		return
	case "EC":
		hash := base58.Decode(content.Input)
		if len(hash) < 34 {
			break
		}
		var fixed [32]byte
		copy(fixed[:], hash[2:34])
		bal := fmt.Sprintf("%d", StatePointer.FactoidState.GetECBalance(fixed))
		TemplateMutex.Lock()
		templates.ExecuteTemplate(w, content.Type,
			struct {
				Balance string
				Address string
			}{bal, content.Input})
		TemplateMutex.Unlock()
		return
	case "FA":
		hash := base58.Decode(content.Input)
		if len(hash) < 34 {
			break
		}
		var fixed [32]byte
		copy(fixed[:], hash[2:34])
		bal := fmt.Sprintf("%.8f", float64(StatePointer.FactoidState.GetFactoidBalance(fixed))/1e8)
		TemplateMutex.Lock()
		templates.ExecuteTemplate(w, content.Type,
			struct {
				Balance string
				Address string
			}{bal, content.Input})
		TemplateMutex.Unlock()
		return
	}

	TemplateMutex.Lock()
	files.CustomParseFile(templates, "templates/searchresults/type/notfound.html")
	templates.ExecuteTemplate(w, "notfound", content.Input)
	TemplateMutex.Unlock()
}
Exemple #3
0
func searchDB(searchitem string, st state.State) (bool, string) {
	if len(searchitem) < 32 {
		heightInt, err := strconv.Atoi(searchitem)
		if err != nil {
			return false, ""
		}
		height := uint32(heightInt)
		if height < DisplayState.CurrentNodeHeight {
			dbase := StatePointer.GetAndLockDB()
			dBlock, err := dbase.FetchDBlockByHeight(height)
			StatePointer.UnlockDB()
			if err != nil {
				return false, ""
			}
			resp := `{"Type":"dblockHeight","item":"` + dBlock.GetKeyMR().String() + `"}`
			return true, resp
		}
		return false, ""
	}
	switch searchitem[:2] {
	case "EC":
		hash := base58.Decode(searchitem)
		if len(hash) < 34 {
			return false, ""
		}
		var fixed [32]byte
		copy(fixed[:], hash[2:34])
		bal := fmt.Sprintf("%d", st.FactoidState.GetECBalance(fixed))
		return true, `{"Type":"EC","item":` + bal + "}"
	case "FA":
		hash := base58.Decode(searchitem)
		if len(hash) < 34 {
			return false, ""
		}
		var fixed [32]byte
		copy(fixed[:], hash[2:34])
		bal := fmt.Sprintf("%.8f", float64(st.FactoidState.GetFactoidBalance(fixed))/1e8)
		return true, `{"Type":"FA","item":` + bal + "}"
	}
	if len(searchitem) == 64 {
		hash, err := primitives.HexToHash(searchitem)
		if err != nil {
			return false, ""
		}

		// Must unlock manually when returining. Function continues to wsapi, who needs the dbase
		dbase := st.GetAndLockDB()

		// Search for Entry
		if entry, err := dbase.FetchEntry(hash); err == nil && entry != nil {
			resp := newSearchResponse("entry", entry)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for Chain
		if mr, err := dbase.FetchHeadIndexByChainID(hash); err == nil && mr != nil {
			resp := newSearchResponse("chainhead", mr)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for EBlock
		if eBlock, err := dbase.FetchEBlockByPrimary(hash); err == nil && eBlock != nil {
			resp := newSearchResponse("eblock", eBlock)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for DBlock
		if dBlock, err := dbase.FetchDBlockByPrimary(hash); err == nil && dBlock != nil {
			resp := newSearchResponse("dblock", dBlock)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for ABlock
		if aBlock, err := dbase.FetchABlock(hash); err == nil && aBlock != nil {
			resp := newSearchResponse("ablock", aBlock)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for Factoid Block
		if fBlock, err := dbase.FetchFBlock(hash); err == nil && fBlock != nil {
			resp := newSearchResponse("fblock", fBlock)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}
		// Search for Entry Credit Block
		if ecBlock, err := dbase.FetchECBlock(hash); err == nil && ecBlock != nil {
			resp := newSearchResponse("ecblock", ecBlock)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}

		// Search for Factoid Transaction
		if trans, err := dbase.FetchFactoidTransaction(hash); err == nil && trans != nil {
			resp := newSearchResponse("facttransaction", trans)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}

		// Search for Entry Credit Transaction
		if trans, err := dbase.FetchECTransaction(hash); err == nil && trans != nil {
			resp := newSearchResponse("ectransaction", trans)
			if len(resp) > 1 {
				st.UnlockDB()
				return true, resp
			}
		}

		st.UnlockDB()

		// Search for Entry Transaction
		ackReq := new(wsapi.AckRequest)
		ackReq.TxID = hash.String()
		if entryAck, err := wsapi.HandleV2EntryACK(&st, ackReq); err == nil && entryAck != nil && len(entryAck.(*wsapi.EntryStatus).EntryHash) == 64 {
			resp := newSearchResponse("entryack", nil)
			if len(resp) > 1 {
				return true, resp
			}
		}

		// Search for Factoid Transaction
		ackReq = new(wsapi.AckRequest)
		ackReq.TxID = hash.String()
		if factoidAck, err := wsapi.HandleV2FactoidACK(&st, ackReq); err == nil && factoidAck != nil && factoidAck.(*wsapi.FactoidTxStatus).BlockDate > 0 {
			resp := newSearchResponse("factoidack", nil)
			if len(resp) > 1 {
				return true, resp
			}
		}

	}

	return false, ""
}