Skip to content
This repository was archived by the owner on Mar 28, 2023. It is now read-only.

Commit cffc9c8

Browse files
committed
Don't set v5 fields when sending v4 contract
1 parent 67699a1 commit cffc9c8

File tree

7 files changed

+84
-33
lines changed

7 files changed

+84
-33
lines changed

api/jsonapi.go

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,9 +1675,14 @@ func (i *jsonAPIHandler) POSTOrderCancel(w http.ResponseWriter, r *http.Request)
16751675
ErrorResponse(w, http.StatusNotFound, "order not found")
16761676
return
16771677
}
1678+
v5order, err := repo.ToV5Order(contract.BuyerOrder, nil)
1679+
if err != nil {
1680+
ErrorResponse(w, http.StatusNotFound, "order not found")
1681+
return
1682+
}
16781683

16791684
// TODO: Remove once broken contracts are migrated
1680-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
1685+
lookupCoin := v5order.Payment.AmountCurrency.Code
16811686
_, err = i.node.LookupCurrency(lookupCoin)
16821687
if err != nil {
16831688
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, can.OrderID)
@@ -2022,8 +2027,14 @@ func (i *jsonAPIHandler) POSTOrderComplete(w http.ResponseWriter, r *http.Reques
20222027
return
20232028
}
20242029

2030+
v5order, err := repo.ToV5Order(contract.BuyerOrder, nil)
2031+
if err != nil {
2032+
ErrorResponse(w, http.StatusNotFound, "order not found")
2033+
return
2034+
}
2035+
20252036
// TODO: Remove once broken contracts are migrated
2026-
lookupCoin := contract.BuyerOrder.Payment.AmountCurrency.Code
2037+
lookupCoin := v5order.Payment.AmountCurrency.Code
20272038
_, err = i.node.LookupCurrency(lookupCoin)
20282039
if err != nil {
20292040
log.Warningf("invalid BuyerOrder.Payment.Coin (%s) on order (%s)", lookupCoin, or.OrderID)

core/order.go

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,14 @@ func (n *OpenBazaarNode) Purchase(data *repo.PurchaseData) (orderID string, paym
165165
payment.Method = pb.Order_Payment_ADDRESS_REQUEST
166166

167167
contract.BuyerOrder.Payment = payment
168-
payment.AmountCurrency = &pb.CurrencyDefinition{
169-
Code: defn.Code.String(),
170-
Divisibility: uint32(defn.Divisibility),
168+
if contract.VendorListings[0].Metadata.Version >= repo.ListingVersion {
169+
payment.AmountCurrency = &pb.CurrencyDefinition{
170+
Code: defn.Code.String(),
171+
Divisibility: uint32(defn.Divisibility),
172+
}
173+
} else {
174+
payment.Coin = defn.Code.String()
171175
}
172-
payment.Coin = defn.Code.String()
173176

174177
// Calculate payment amount
175178
total, err := n.CalculateOrderTotal(contract)
@@ -181,8 +184,11 @@ func (n *OpenBazaarNode) Purchase(data *repo.PurchaseData) (orderID string, paym
181184
return "", "", retCurrency, false, ErrSpendAmountIsDust
182185
}
183186

184-
payment.BigAmount = total.String()
185-
payment.Amount = total.Uint64()
187+
if contract.VendorListings[0].Metadata.Version >= repo.ListingVersion {
188+
payment.BigAmount = total.String()
189+
} else {
190+
payment.Amount = total.Uint64()
191+
}
186192

187193
contract, err = n.SignOrder(contract)
188194
if err != nil {
@@ -236,17 +242,24 @@ func prepareModeratedOrderContract(data *repo.PurchaseData, n *OpenBazaarNode, c
236242
return nil, errors.New("moderator does not accept our currency")
237243
}
238244
contract.BuyerOrder.Payment = payment
239-
payment.AmountCurrency = &pb.CurrencyDefinition{
240-
Code: defn.Code.String(),
241-
Divisibility: uint32(defn.Divisibility),
245+
if contract.VendorListings[0].Metadata.Version >= repo.ListingVersion {
246+
payment.AmountCurrency = &pb.CurrencyDefinition{
247+
Code: defn.Code.String(),
248+
Divisibility: uint32(defn.Divisibility),
249+
}
250+
} else {
251+
payment.Coin = defn.Code.String()
242252
}
243-
payment.Coin = defn.Code.String()
253+
244254
total, err := n.CalculateOrderTotal(contract)
245255
if err != nil {
246256
return nil, err
247257
}
248-
payment.BigAmount = total.String()
249-
payment.Amount = total.Uint64()
258+
if contract.VendorListings[0].Metadata.Version >= repo.ListingVersion {
259+
payment.BigAmount = total.String()
260+
} else {
261+
payment.Amount = total.Uint64()
262+
}
250263
contract.BuyerOrder.Payment = payment
251264

252265
fpb := wal.GetFeePerByte(wallet.NORMAL)
@@ -345,7 +358,12 @@ func processOnlineDirectOrder(resp *pb.Message, n *OpenBazaarNode, wal wallet.Wa
345358
if err != nil {
346359
return "", "", *big.NewInt(0), false, err
347360
}
348-
total, ok := new(big.Int).SetString(contract.BuyerOrder.Payment.BigAmount, 10)
361+
362+
v5Order, err := repo.ToV5Order(contract.BuyerOrder, nil)
363+
if err != nil {
364+
return "", "", *big.NewInt(0), false, err
365+
}
366+
total, ok := new(big.Int).SetString(v5Order.Payment.BigAmount, 10)
349367
if !ok {
350368
return "", "", *big.NewInt(0), false, errors.New("invalid payment amount")
351369
}
@@ -355,8 +373,12 @@ func processOnlineDirectOrder(resp *pb.Message, n *OpenBazaarNode, wal wallet.Wa
355373
func processOfflineDirectOrder(n *OpenBazaarNode, wal wallet.Wallet, contract *pb.RicardianContract, payment *pb.Order_Payment) (string, string, big.Int, error) {
356374
// Vendor offline
357375
// Change payment code to direct
376+
v5Order, err := repo.ToV5Order(contract.BuyerOrder, nil)
377+
if err != nil {
378+
return "", "", *big.NewInt(0), err
379+
}
358380

359-
total, ok := new(big.Int).SetString(contract.BuyerOrder.Payment.BigAmount, 10)
381+
total, ok := new(big.Int).SetString(v5Order.Payment.BigAmount, 10)
360382
if !ok {
361383
return "", "", *big.NewInt(0), errors.New("invalid payment amount")
362384
}
@@ -372,7 +394,7 @@ func processOfflineDirectOrder(n *OpenBazaarNode, wal wallet.Wallet, contract *p
372394
/* Generate a payment address using the first child key derived from the buyer's
373395
and vendors's masterPubKeys and a random chaincode. */
374396
chaincode := make([]byte, 32)
375-
_, err := rand.Read(chaincode)
397+
_, err = rand.Read(chaincode)
376398
if err != nil {
377399
return "", "", *big.NewInt(0), err
378400
}
@@ -823,11 +845,16 @@ func (n *OpenBazaarNode) EstimateOrderTotal(data *repo.PurchaseData) (*big.Int,
823845

824846
// CancelOfflineOrder - cancel order
825847
func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, records []*wallet.TransactionRecord) error {
848+
v5Order, err := repo.ToV5Order(contract.BuyerOrder, nil)
849+
if err != nil {
850+
return err
851+
}
852+
826853
orderID, err := n.CalcOrderID(contract.BuyerOrder)
827854
if err != nil {
828855
return err
829856
}
830-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
857+
wal, err := n.Multiwallet.WalletForCurrencyCode(v5Order.Payment.AmountCurrency.Code)
831858
if err != nil {
832859
return err
833860
}
@@ -857,7 +884,7 @@ func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, reco
857884
return errors.New("cannot cancel order because utxo has already been spent")
858885
}
859886

860-
chaincode, err := hex.DecodeString(contract.BuyerOrder.Payment.Chaincode)
887+
chaincode, err := hex.DecodeString(v5Order.Payment.Chaincode)
861888
if err != nil {
862889
return err
863890
}
@@ -869,11 +896,11 @@ func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, reco
869896
if err != nil {
870897
return err
871898
}
872-
redeemScript, err := hex.DecodeString(contract.BuyerOrder.Payment.RedeemScript)
899+
redeemScript, err := hex.DecodeString(v5Order.Payment.RedeemScript)
873900
if err != nil {
874901
return err
875902
}
876-
refundAddress, err := wal.DecodeAddress(contract.BuyerOrder.RefundAddress)
903+
refundAddress, err := wal.DecodeAddress(v5Order.RefundAddress)
877904
if err != nil {
878905
return err
879906
}

qa/cancel_direct_offline.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_test(self):
3939
with open('testdata/'+ self.vendor_version +'/listing.json') as listing_file:
4040
listing_json = json.load(listing_file, object_pairs_hook=OrderedDict)
4141
listing_json["metadata"]["acceptedCurrencies"] = ["t" + self.cointype]
42-
if self.vendor_version == 4:
42+
if self.vendor_version == "v4":
4343
listing_json["metadata"]["priceCurrency"] = "t" + self.cointype
4444
else:
4545
listing_json["item"]["priceCurrency"]["code"] = "t" + self.cointype
@@ -108,7 +108,7 @@ def run_test(self):
108108
"feeLevel": "NORMAL",
109109
"requireAssociateOrder": False
110110
}
111-
if self.buyer_version == 4:
111+
if self.buyer_version == "v4":
112112
spend["amount"] = payment_amount
113113
spend["wallet"] = "T" + self.cointype
114114

qa/complete_direct_online.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def run_test(self):
3939
with open('testdata/'+ self.vendor_version +'/listing.json') as listing_file:
4040
listing_json = json.load(listing_file, object_pairs_hook=OrderedDict)
4141
listing_json["metadata"]["acceptedCurrencies"] = ["T" + self.cointype]
42-
if self.vendor_version == 4:
42+
if self.vendor_version == "v4":
4343
listing_json["metadata"]["priceCurrency"] = "t" + self.cointype
4444
else:
4545
listing_json["item"]["priceCurrency"]["code"] = "t" + self.cointype
@@ -110,7 +110,7 @@ def run_test(self):
110110
"feeLevel": "NORMAL",
111111
"requireAssociateOrder": False
112112
}
113-
if self.buyer_version == 4:
113+
if self.buyer_version == "v4":
114114
spend["amount"] = payment_amount
115115
spend["wallet"] = "T" + self.cointype
116116

@@ -144,7 +144,7 @@ def run_test(self):
144144
raise TestFailure("CompleteDirectOnlineTest - FAIL: Alice failed to detect payment")
145145
if resp["funded"] == False:
146146
raise TestFailure("CompleteDirectOnlineTest - FAIL: Alice incorrectly saved as unfunded")
147-
147+
148148
# alice send order fulfillment
149149
with open('testdata/'+ self.vendor_version +'/fulfillment.json') as fulfillment_file:
150150
fulfillment_json = json.load(fulfillment_file, object_pairs_hook=OrderedDict)

qa/purchase_direct_online.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ def run_test(self):
158158
raise TestFailure("PurchaseDirectOnlineTest - FAIL: Purchase POST failed with incorrect reason: %s", resp["reason"])
159159
if resp["code"] != "ERR_INSUFFICIENT_INVENTORY":
160160
raise TestFailure("PurchaseDirectOnlineTest - FAIL: Purchase POST failed with incorrect code: %s", resp["code"])
161-
if resp["remainingInventory"] != "6":
161+
if int(resp["remainingInventory"]) != 6:
162162
raise TestFailure("PurchaseDirectOnlineTest - FAIL: Purchase POST failed with incorrect remainingInventory: %d", resp["remainingInventory"])
163163

164164
print("PurchaseDirectOnlineTest - PASS")

repo/buyer_order.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@ import (
88

99
// ToV5Order scans through the order looking for any deprecated fields and turns them into their v5 counterpart.
1010
func ToV5Order(order *pb.Order, lookupFunc func(currencyCode string) (CurrencyDefinition, error)) (*pb.Order, error) {
11+
if lookupFunc == nil {
12+
lookupFunc = AllCurrencies().Lookup
13+
}
1114
newOrder := proto.Clone(order).(*pb.Order)
1215

1316
if order.RefundFee != 0 && order.BigRefundFee == "" {

wallet/listeners/transaction_listener.go

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,17 @@ func (l *TransactionListener) processSalePayment(txid string, output wallet.Tran
225225
if err != nil {
226226
return
227227
}
228+
order, err := repo.ToV5Order(contract.BuyerOrder, nil)
229+
if err != nil {
230+
log.Error(err)
231+
return
232+
}
228233
if !funded {
229-
currencyValue, err := repo.NewCurrencyValueWithLookup(contract.BuyerOrder.Payment.BigAmount, contract.BuyerOrder.Payment.AmountCurrency.Code)
234+
currencyValue, err := repo.NewCurrencyValueWithLookup(order.Payment.BigAmount, order.Payment.AmountCurrency.Code)
230235
if err != nil {
231236
log.Errorf("Failed parsing CurrencyValue for (%s, %s): %s",
232-
contract.BuyerOrder.Payment.BigAmount,
233-
contract.BuyerOrder.Payment.AmountCurrency.Code,
237+
order.Payment.BigAmount,
238+
order.Payment.AmountCurrency.Code,
234239
err.Error(),
235240
)
236241
return
@@ -322,8 +327,13 @@ func (l *TransactionListener) processPurchasePayment(txid string, output wallet.
322327
if err != nil {
323328
return
324329
}
330+
order, err := repo.ToV5Order(contract.BuyerOrder, nil)
331+
if err != nil {
332+
log.Error(err)
333+
return
334+
}
325335
if !funded {
326-
requestedAmount, _ := new(big.Int).SetString(contract.BuyerOrder.Payment.BigAmount, 10)
336+
requestedAmount, _ := new(big.Int).SetString(order.Payment.BigAmount, 10)
327337
if funding.Cmp(requestedAmount) >= 0 {
328338
log.Debugf("Payment for purchase %s detected", orderId)
329339
funded = true
@@ -337,7 +347,7 @@ func (l *TransactionListener) processPurchasePayment(txid string, output wallet.
337347
}
338348
}
339349
}
340-
def, err := repo.AllCurrencies().Lookup(contract.BuyerOrder.Payment.AmountCurrency.Code)
350+
def, err := repo.AllCurrencies().Lookup(order.Payment.AmountCurrency.Code)
341351
if err != nil {
342352
log.Errorf("Error looking up currency: %s", err)
343353
return
@@ -352,7 +362,7 @@ func (l *TransactionListener) processPurchasePayment(txid string, output wallet.
352362
Type: "payment",
353363
OrderId: orderId,
354364
FundingTotal: cv,
355-
CoinType: contract.BuyerOrder.Payment.AmountCurrency.Code,
365+
CoinType: order.Payment.AmountCurrency.Code,
356366
}
357367
l.broadcast <- n
358368
err = l.db.Notifications().PutRecord(repo.NewNotification(n, time.Now(), false))

0 commit comments

Comments
 (0)