<commit_before>package core

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"math/big"
	"strconv"
	"strings"
	"time"

	ipfspath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
	crypto "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
	peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"

	"github.com/OpenBazaar/jsonpb"
	"github.com/OpenBazaar/wallet-interface"
	hd "github.com/btcsuite/btcutil/hdkeychain"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes"
	"github.com/golang/protobuf/ptypes/timestamp"

	"github.com/OpenBazaar/openbazaar-go/ipfs"
	"github.com/OpenBazaar/openbazaar-go/pb"
	"github.com/OpenBazaar/openbazaar-go/repo"
)

const (
	// We use this to check to see if the approximate fee to release funds from escrow is greater than 1/4th of the amount
	// being released. If so, we prevent the purchase from being made as it severely cuts into the vendor's profits.
	// TODO: this probably should not be hardcoded but making it adaptive requires all wallet implementations to provide this data.
	// TODO: for now, this is probably OK as it's just an approximation.

	// EscrowReleaseSize - size in bytes for escrow op
	EscrowReleaseSize = 337
	// CryptocurrencyPurchasePaymentAddressMaxLength - max permissible length for an address
	CryptocurrencyPurchasePaymentAddressMaxLength = 512
)

// GetOrder - provide API response order object by orderID
func (n *OpenBazaarNode) GetOrder(orderID string) (*pb.OrderRespApi, error) {
	var (
		err      error
		isSale   bool
		contract *pb.RicardianContract
		state    pb.OrderState
		funded   bool
		records  []*wallet.TransactionRecord
		read     bool
		//paymentCoin *repo.CurrencyCode
	)
	contract, state, funded, records, read, _, err = n.Datastore.Purchases().GetByOrderId(orderID)
	if err != nil {
		contract, state, funded, records, read, _, err = n.Datastore.Sales().GetByOrderId(orderID)
		if err != nil {
			return nil, errors.New("order not found")
		}
		isSale = true
	}
	resp := new(pb.OrderRespApi)
	resp.Contract = contract
	resp.Funded = funded
	resp.Read = read
	resp.State = state

	v5Order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
	if err != nil {
		log.Errorf("failed converting contract buyer order to v5 schema: %s", err.Error())
		return nil, err
	}
	resp.Contract.BuyerOrder = v5Order

	paymentTxs, refundTx, err := n.BuildTransactionRecords(contract, records, state)
	if err != nil {
		log.Errorf(err.Error())
		return nil, err
	}
	resp.PaymentAddressTransactions = paymentTxs
	resp.RefundAddressTransaction = refundTx

	unread, err := n.Datastore.Chat().GetUnreadCount(orderID)
	if err != nil {
		log.Errorf(err.Error())<commit_msg>[#1976] GET /ob/order/... normalizes legacy listing contract data<commit_after>package core

import (
	"bytes"
	"crypto/rand"
	"encoding/hex"
	"encoding/json"
	"errors"
	"fmt"
	"math/big"
	"strconv"
	"strings"
	"time"

	ipfspath "gx/ipfs/QmQAgv6Gaoe2tQpcabqwKXKChp2MZ7i3UXv9DqTTaxCaTR/go-path"
	crypto "gx/ipfs/QmTW4SdgBWq9GjsBsHeUx8WuGxzhgzAf88UMH2w62PC8yK/go-libp2p-crypto"
	peer "gx/ipfs/QmYVXrKrKHDC9FobgmcmshCDyWwdrfwfanNQN4oxJ9Fk3h/go-libp2p-peer"
	mh "gx/ipfs/QmerPMzPk1mJVowm8KgmoknWa4yCYvvugMPsgWmDNUvDLW/go-multihash"

	"github.com/OpenBazaar/jsonpb"
	"github.com/OpenBazaar/wallet-interface"
	hd "github.com/btcsuite/btcutil/hdkeychain"
	"github.com/golang/protobuf/proto"
	"github.com/golang/protobuf/ptypes"
	"github.com/golang/protobuf/ptypes/timestamp"

	"github.com/OpenBazaar/openbazaar-go/ipfs"
	"github.com/OpenBazaar/openbazaar-go/pb"
	"github.com/OpenBazaar/openbazaar-go/repo"
)

const (
	// We use this to check to see if the approximate fee to release funds from escrow is greater than 1/4th of the amount
	// being released. If so, we prevent the purchase from being made as it severely cuts into the vendor's profits.
	// TODO: this probably should not be hardcoded but making it adaptive requires all wallet implementations to provide this data.
	// TODO: for now, this is probably OK as it's just an approximation.

	// EscrowReleaseSize - size in bytes for escrow op
	EscrowReleaseSize = 337
	// CryptocurrencyPurchasePaymentAddressMaxLength - max permissible length for an address
	CryptocurrencyPurchasePaymentAddressMaxLength = 512
)

// GetOrder - provide API response order object by orderID
func (n *OpenBazaarNode) GetOrder(orderID string) (*pb.OrderRespApi, error) {
	var (
		err      error
		isSale   bool
		contract *pb.RicardianContract
		state    pb.OrderState
		funded   bool
		records  []*wallet.TransactionRecord
		read     bool
		//paymentCoin *repo.CurrencyCode
	)
	contract, state, funded, records, read, _, err = n.Datastore.Purchases().GetByOrderId(orderID)
	if err != nil {
		contract, state, funded, records, read, _, err = n.Datastore.Sales().GetByOrderId(orderID)
		if err != nil {
			return nil, errors.New("order not found")
		}
		isSale = true
	}

	for i, l := range contract.VendorListings {
		repoListing, err := repo.NewListingFromProtobuf(l)
		if err != nil {
			log.Errorf("failed getting contract listing: %s", err.Error())
			return nil, err
		}
		normalizedListing, err := repoListing.Normalize()
		if err != nil {
			log.Errorf("failed converting contract listing to v5 schema: %s", err.Error())
			return nil, err
		}
		contract.VendorListings[i] = normalizedListing.GetProtobuf()
	}

	resp := new(pb.OrderRespApi)
	resp.Contract = contract
	resp.Funded = funded
	resp.Read = read
	resp.State = state

	v5Order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
	if err != nil {
		log.Errorf("failed converting contract buyer order to v5 schema: %s", err.Error())
		return nil, err
	}
	resp.Contract.BuyerOrder = v5Order

	paymentTxs, refundTx, err := n.BuildTransactionRecords(contract, records, state)
	if err != nil {
		log.Errorf(err.Error())
		return nil, err
	}
	resp.PaymentAddressTransactions = paymentTxs
	resp.RefundAddressTransaction = refundTx

	unread, err := n.Datastore.Chat().GetUnreadCount(orderID)
	if err != nil {
		log.Errorf(err.Error())
{'fraction_non_alphanumeric': 0.10632090761750405, 'fraction_numerical': 0.012479740680713128, 'mean_word_length': 4.380122057541413, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6147864', 'n_tokens_mistral': 2419, 'n_tokens_neox': 2270, 'n_words': 522}