Esempio n. 1
0
func PlaceBid(bid *bitwrk.RawBid, identity *bitcoin.KeyPair) (bidId string, err error) {
	var nonce string
	if _nonce, err := GetNonce(); err != nil {
		return "", err
	} else {
		nonce = _nonce
	}

	articleString := bid.Article.FormString()
	priceString := normalize(bid.Price.String())
	bidTypeString := bid.Type.FormString()
	document := fmt.Sprintf(
		"article=%s&type=%s&price=%s&address=%s&nonce=%s",
		articleString,
		bidTypeString,
		priceString,
		identity.GetAddress(),
		nonce)
	signature, err := identity.SignMessage(document, rand.Reader)
	if err != nil {
		err = fmt.Errorf("Error signing message: %v", err)
		return
	}

	resp, err := postFormToServer("bid", document+"&signature="+url.QueryEscape(signature))
	if resp != nil && resp.StatusCode == http.StatusSeeOther && resp.Header.Get("X-Bid-Key") != "" {
		bidId = resp.Header.Get("X-Bid-Key")
		err = resp.Body.Close()
	} else if resp != nil {
		more, _ := ioutil.ReadAll(resp.Body)
		err = fmt.Errorf("Got status: %#v\nResponse: %v", resp.Status, string(more))
	}

	return
}
Esempio n. 2
0
func SendTxMessage(txId string, identity *bitcoin.KeyPair, arguments map[string]string) error {
	arguments["txid"] = txId

	keys := make([]string, len(arguments))
	i := 0
	for k, _ := range arguments {
		keys[i] = k
		i++
	}
	sort.Strings(keys)

	// Prepare document for signature. Document consists of alphabetically sorted
	// query arguments, plus "txid"
	document := make([]byte, 0, 100)
	for _, k := range keys {
		if len(document) > 0 {
			document = append(document, '&')
		}
		document = append(document, (k + "=" + normalize(arguments[k]))...)
	}

	signature := ""
	if s, err := identity.SignMessage(string(document), rand.Reader); err != nil {
		return err
	} else {
		signature = s
	}

	query := string(document) +
		"&signature=" + url.QueryEscape(signature) +
		"&address=" + url.QueryEscape(identity.GetAddress())
	if r, err := postFormToServer("tx/"+txId, query); r != nil {
		defer r.Body.Close()
		if r.StatusCode == http.StatusSeeOther {
			// Success! Do nothing.
		} else {
			buf := make([]byte, 1024)
			n, _ := io.ReadFull(r.Body, buf)
			return fmt.Errorf("Unexpected reply status posting form to server: %v (%v)",
				r.Status, string(buf[:n]))
		}
	} else {
		return fmt.Errorf("Error posting form to server: %v", err)
	}

	return nil
}