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

Commit 8cb1bd1

Browse files
authored
Merge pull request #1762 from OpenBazaar/abstractions
Add conversions functions for backwards compatibility
2 parents ae14d9b + c3f91da commit 8cb1bd1

20 files changed

+633
-144
lines changed

core/completion.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,18 @@ func (n *OpenBazaarNode) CompleteOrder(orderRatings *OrderRatings, contract *pb.
165165
oc.Ratings = append(oc.Ratings, rating)
166166
}
167167

168-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
168+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
169+
if err != nil {
170+
return err
171+
}
172+
173+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
169174
if err != nil {
170175
return err
171176
}
172177

173178
// Payout order if moderated and not disputed
174-
if contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED && contract.DisputeResolution == nil {
179+
if order.Payment.Method == pb.Order_Payment_MODERATED && contract.DisputeResolution == nil {
175180
var ins []wallet.TransactionInput
176181
outValue := new(big.Int)
177182
for _, r := range records {
@@ -204,7 +209,7 @@ func (n *OpenBazaarNode) CompleteOrder(orderRatings *OrderRatings, contract *pb.
204209
Value: *outValue,
205210
}
206211

207-
chaincode, err := hex.DecodeString(contract.BuyerOrder.Payment.Chaincode)
212+
chaincode, err := hex.DecodeString(order.Payment.Chaincode)
208213
if err != nil {
209214
return err
210215
}
@@ -216,11 +221,12 @@ func (n *OpenBazaarNode) CompleteOrder(orderRatings *OrderRatings, contract *pb.
216221
if err != nil {
217222
return err
218223
}
219-
redeemScript, err := hex.DecodeString(contract.BuyerOrder.Payment.RedeemScript)
224+
redeemScript, err := hex.DecodeString(order.Payment.RedeemScript)
220225
if err != nil {
221226
return err
222227
}
223-
n, ok := new(big.Int).SetString(contract.VendorOrderFulfillment[0].Payout.BigPayoutFeePerByte, 10)
228+
fulfillment := repo.ToV5OrderFulfillment(contract.VendorOrderFulfillment[0])
229+
n, ok := new(big.Int).SetString(fulfillment.Payout.BigPayoutFeePerByte, 10)
224230
if !ok {
225231
return errors.New("invalid payout fee per byte value")
226232
}
@@ -304,7 +310,11 @@ func (n *OpenBazaarNode) ReleaseFundsAfterTimeout(contract *pb.RicardianContract
304310
} else if active {
305311
return ErrPrematureReleaseOfTimedoutEscrowFunds
306312
}
307-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
313+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
314+
if err != nil {
315+
return err
316+
}
317+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
308318
if err != nil {
309319
return err
310320
}
@@ -346,7 +356,7 @@ func (n *OpenBazaarNode) ReleaseFundsAfterTimeout(contract *pb.RicardianContract
346356
}
347357
}
348358

349-
chaincode, err := hex.DecodeString(contract.BuyerOrder.Payment.Chaincode)
359+
chaincode, err := hex.DecodeString(order.Payment.Chaincode)
350360
if err != nil {
351361
return err
352362
}
@@ -358,7 +368,7 @@ func (n *OpenBazaarNode) ReleaseFundsAfterTimeout(contract *pb.RicardianContract
358368
if err != nil {
359369
return err
360370
}
361-
redeemScript, err := hex.DecodeString(contract.BuyerOrder.Payment.RedeemScript)
371+
redeemScript, err := hex.DecodeString(order.Payment.RedeemScript)
362372
if err != nil {
363373
return err
364374
}

core/confirmation.go

Lines changed: 36 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/hex"
66
"errors"
77
"fmt"
8+
"github.com/OpenBazaar/openbazaar-go/repo"
89
"math/big"
910
"strings"
1011
"time"
@@ -28,7 +29,12 @@ func (n *OpenBazaarNode) NewOrderConfirmation(contract *pb.RicardianContract, ad
2829
}
2930
oc.OrderID = orderID
3031

31-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
32+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
3238
if err != nil {
3339
return nil, err
3440
}
@@ -45,13 +51,13 @@ func (n *OpenBazaarNode) NewOrderConfirmation(contract *pb.RicardianContract, ad
4551
oc.Timestamp = ts
4652

4753
oc.RatingSignatures = []*pb.RatingSignature{}
48-
if contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED {
54+
if order.Payment.Method == pb.Order_Payment_MODERATED {
4955
for _, listing := range contract.VendorListings {
5056
metadata := new(pb.RatingSignature_TransactionMetadata)
5157
metadata.ListingSlug = listing.Slug
52-
metadata.ModeratorKey = contract.BuyerOrder.Payment.ModeratorKey
58+
metadata.ModeratorKey = order.Payment.ModeratorKey
5359

54-
if contract.BuyerOrder.Version > 0 {
60+
if order.Version > 0 {
5561
metadata.ListingTitle = listing.Item.Title
5662
if len(listing.Item.Images) > 0 {
5763
metadata.Thumbnail = &pb.RatingSignature_TransactionMetadata_Image{
@@ -79,10 +85,10 @@ func (n *OpenBazaarNode) NewOrderConfirmation(contract *pb.RicardianContract, ad
7985

8086
oc.RatingSignatures = append(oc.RatingSignatures, rs)
8187
}
82-
oc.PaymentAddress = contract.BuyerOrder.Payment.Address
88+
oc.PaymentAddress = order.Payment.Address
8389
}
8490

85-
oc.BigRequestedAmount = contract.BuyerOrder.Payment.BigAmount
91+
oc.BigRequestedAmount = order.Payment.BigAmount
8692
contract.VendorOrderConfirmation = oc
8793
contract, err = n.SignOrderConfirmation(contract)
8894
if err != nil {
@@ -190,12 +196,16 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
190196
if err != nil {
191197
return fmt.Errorf("marshal timestamp: %s", err.Error())
192198
}
193-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
199+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
200+
if err != nil {
201+
return err
202+
}
203+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
194204
if err != nil {
195205
return err
196206
}
197207
rejectMsg.Timestamp = ts
198-
if contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED {
208+
if order.Payment.Method == pb.Order_Payment_MODERATED {
199209
var ins []wallet.TransactionInput
200210
outValue := *big.NewInt(0)
201211
for _, r := range records {
@@ -219,7 +229,7 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
219229
}
220230
}
221231

222-
refundAddress, err := wal.DecodeAddress(contract.BuyerOrder.RefundAddress)
232+
refundAddress, err := wal.DecodeAddress(order.RefundAddress)
223233
if err != nil {
224234
return fmt.Errorf("decode refund address: %s", err.Error())
225235
}
@@ -228,7 +238,7 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
228238
Value: outValue,
229239
}
230240

231-
chaincode, err := hex.DecodeString(contract.BuyerOrder.Payment.Chaincode)
241+
chaincode, err := hex.DecodeString(order.Payment.Chaincode)
232242
if err != nil {
233243
return fmt.Errorf("decode buyer chaincode: %s", err.Error())
234244
}
@@ -240,11 +250,11 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
240250
if err != nil {
241251
return fmt.Errorf("generate child key: %s", err.Error())
242252
}
243-
redeemScript, err := hex.DecodeString(contract.BuyerOrder.Payment.RedeemScript)
253+
redeemScript, err := hex.DecodeString(order.Payment.RedeemScript)
244254
if err != nil {
245255
return fmt.Errorf("generate child key: %s", err.Error())
246256
}
247-
fee, ok := new(big.Int).SetString(contract.BuyerOrder.BigRefundFee, 10)
257+
fee, ok := new(big.Int).SetString(order.BigRefundFee, 10)
248258
if !ok {
249259
return errors.New("invalid refund fee value")
250260
}
@@ -259,7 +269,7 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
259269
}
260270
rejectMsg.Sigs = sigs
261271
}
262-
err = n.SendReject(contract.BuyerOrder.BuyerID.PeerID, rejectMsg)
272+
err = n.SendReject(order.BuyerID.PeerID, rejectMsg)
263273
if err != nil {
264274
return fmt.Errorf("sending rejection: %s", err.Error())
265275
}
@@ -275,18 +285,24 @@ func (n *OpenBazaarNode) ValidateOrderConfirmation(contract *pb.RicardianContrac
275285
if err != nil {
276286
return err
277287
}
278-
if contract.VendorOrderConfirmation.OrderID != orderID {
288+
289+
orderConfirmation := repo.ToV5OrderConfirmation(contract.VendorOrderConfirmation)
290+
291+
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
292+
if err != nil {
293+
return err
294+
}
295+
if orderConfirmation.OrderID != orderID {
279296
return errors.New("vendor's response contained invalid order ID")
280297
}
281-
if contract.VendorOrderConfirmation.BigRequestedAmount != contract.BuyerOrder.Payment.BigAmount {
282-
return fmt.Errorf("vendor requested an amount different from what we calculated: want %s, got %s",
283-
contract.BuyerOrder.Payment.BigAmount, contract.VendorOrderConfirmation.BigRequestedAmount)
298+
if orderConfirmation.BigRequestedAmount != order.Payment.BigAmount {
299+
return errors.New("vendor requested an amount different from what we calculated")
284300
}
285-
wal, err := n.Multiwallet.WalletForCurrencyCode(contract.BuyerOrder.Payment.AmountCurrency.Code)
301+
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
286302
if err != nil {
287303
return err
288304
}
289-
if contract.BuyerOrder.Payment.Method == pb.Order_Payment_MODERATED {
305+
if order.Payment.Method == pb.Order_Payment_MODERATED {
290306
for _, sig := range contract.VendorOrderConfirmation.RatingSignatures {
291307
exists := false
292308
for _, listing := range contract.VendorListings {
@@ -303,7 +319,7 @@ func (n *OpenBazaarNode) ValidateOrderConfirmation(contract *pb.RicardianContrac
303319
return err
304320
}
305321

306-
if !bytes.Equal(sig.Metadata.ModeratorKey, contract.BuyerOrder.Payment.ModeratorKey) {
322+
if !bytes.Equal(sig.Metadata.ModeratorKey, order.Payment.ModeratorKey) {
307323
return errors.New("rating signature does not contain moderatory key")
308324
}
309325
ser, err := proto.Marshal(sig.Metadata)

0 commit comments

Comments
 (0)