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

Commit 55f8fdb

Browse files
committed
Update codebase for option wallet interfaces
1 parent c0caabc commit 55f8fdb

File tree

12 files changed

+105
-101
lines changed

12 files changed

+105
-101
lines changed

api/jsonapi.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3381,7 +3381,12 @@ func (i *jsonAPIHandler) POSTBumpFee(w http.ResponseWriter, r *http.Request) {
33813381
ErrorResponse(w, http.StatusBadRequest, "transaction not found in any wallet")
33823382
return
33833383
}
3384-
newTxid, err := wal.BumpFee(txHash.String())
3384+
feeBumper, ok := wal.(wallet.WalletCanBumpFee)
3385+
if !ok {
3386+
ErrorResponse(w, http.StatusBadRequest, "wallet does not support bumping fees")
3387+
return
3388+
}
3389+
newTxid, err := feeBumper.BumpFee(txHash.String())
33853390
if err != nil {
33863391
if err == spvwallet.BumpFeeAlreadyConfirmedError {
33873392
ErrorResponse(w, http.StatusBadRequest, err.Error())

core/completion.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ func (n *OpenBazaarNode) CompleteOrder(orderRatings *OrderRatings, contract *pb.
177177

178178
// Payout order if moderated and not disputed
179179
if order.Payment.Method == pb.Order_Payment_MODERATED && contract.DisputeResolution == nil {
180+
escrowWallet, ok := wal.(wallet.EscrowWallet)
181+
if !ok {
182+
return errors.New("wallet does not support escrow")
183+
}
180184
var ins []wallet.TransactionInput
181185
outValue := new(big.Int)
182186
for _, r := range records {
@@ -230,7 +234,7 @@ func (n *OpenBazaarNode) CompleteOrder(orderRatings *OrderRatings, contract *pb.
230234
if !ok {
231235
return errors.New("invalid payout fee per byte value")
232236
}
233-
buyerSignatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *n)
237+
buyerSignatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *n)
234238
if err != nil {
235239
return err
236240
}
@@ -318,6 +322,10 @@ func (n *OpenBazaarNode) ReleaseFundsAfterTimeout(contract *pb.RicardianContract
318322
if err != nil {
319323
return err
320324
}
325+
escrowWallet, ok := wal.(wallet.EscrowWallet)
326+
if !ok {
327+
return errors.New("wallet does not support escrow")
328+
}
321329
defn, err := repo.AllCurrencies().Lookup(order.Payment.AmountCurrency.Code)
322330
if err != nil {
323331
log.Errorf("Failed ReleaseFundsAfterTimeout(): %s", err.Error())
@@ -382,7 +390,7 @@ func (n *OpenBazaarNode) ReleaseFundsAfterTimeout(contract *pb.RicardianContract
382390
if err != nil {
383391
return err
384392
}
385-
_, err = wal.SweepAddress(txInputs, nil, vendorKey, &redeemScript, wallet.NORMAL)
393+
_, err = escrowWallet.SweepAddress(txInputs, nil, vendorKey, &redeemScript, wallet.NORMAL)
386394
if err != nil {
387395
return err
388396
}

core/confirmation.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ func (n *OpenBazaarNode) ConfirmOfflineOrder(oldState pb.OrderState, contract *p
127127
}
128128

129129
if confirmedContract.BuyerOrder.Payment.Method != pb.Order_Payment_MODERATED {
130+
escrowWallet, ok := wal.(wallet.EscrowWallet)
131+
if !ok {
132+
return errors.New("wallet does not support escrow")
133+
}
130134
// Sweep the temp address into our wallet
131135
var txInputs []wallet.TransactionInput
132136
for _, r := range records {
@@ -176,7 +180,7 @@ func (n *OpenBazaarNode) ConfirmOfflineOrder(oldState pb.OrderState, contract *p
176180
recoverState()
177181
return err
178182
}
179-
_, err = wal.SweepAddress(txInputs, nil, vendorKey, &redeemScript, wallet.NORMAL)
183+
_, err = escrowWallet.SweepAddress(txInputs, nil, vendorKey, &redeemScript, wallet.NORMAL)
180184
if err != nil {
181185
recoverState()
182186
return err
@@ -213,6 +217,10 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
213217
}
214218
rejectMsg.Timestamp = ts
215219
if order.Payment.Method == pb.Order_Payment_MODERATED {
220+
escrowWallet, ok := wal.(wallet.EscrowWallet)
221+
if !ok {
222+
return errors.New("wallet does not support escrow")
223+
}
216224
var ins []wallet.TransactionInput
217225
outValue := *big.NewInt(0)
218226
for _, r := range records {
@@ -265,7 +273,7 @@ func (n *OpenBazaarNode) RejectOfflineOrder(contract *pb.RicardianContract, reco
265273
if !ok {
266274
return errors.New("invalid refund fee value")
267275
}
268-
signatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *fee)
276+
signatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *fee)
269277
if err != nil {
270278
return fmt.Errorf("generate multisig: %s", err.Error())
271279
}

core/disputes.go

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,10 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
592592
if err != nil {
593593
return fmt.Errorf("currency (%s) not supported by wallet", preferredOrder.Payment.AmountCurrency.Code)
594594
}
595+
escrowWallet, ok := wal.(wallet.EscrowWallet)
596+
if !ok {
597+
return errors.New("wallet does not support escrow")
598+
}
595599

596600
// Create outputs using full value. We will subtract the fee off each output later.
597601
outMap := make(map[string]wallet.TransactionOutput)
@@ -743,7 +747,7 @@ func (n *OpenBazaarNode) CloseDispute(orderID string, buyerPercentage, vendorPer
743747
return err
744748
}
745749

746-
sigs, err := wal.CreateMultisigSignature(inputs, outs, moderatorKey, redeemScriptBytes, *big.NewInt(0))
750+
sigs, err := escrowWallet.CreateMultisigSignature(inputs, outs, moderatorKey, redeemScriptBytes, *big.NewInt(0))
747751
if err != nil {
748752
return err
749753
}
@@ -982,6 +986,11 @@ func (n *OpenBazaarNode) ValidateCaseContract(contract *pb.RicardianContract) []
982986
validationErrors = append(validationErrors, "Contract uses a coin not found in wallet")
983987
return validationErrors
984988
}
989+
escrowWallet, ok := wal.(wallet.EscrowWallet)
990+
if !ok {
991+
validationErrors = append(validationErrors, "Wallet does not support escrow")
992+
return validationErrors
993+
}
985994
chaincode, err := hex.DecodeString(order.Payment.Chaincode)
986995
if err != nil {
987996
validationErrors = append(validationErrors, "Error validating bitcoin address and redeem script")
@@ -1008,7 +1017,7 @@ func (n *OpenBazaarNode) ValidateCaseContract(contract *pb.RicardianContract) []
10081017
return validationErrors
10091018
}
10101019
timeout, _ := time.ParseDuration(strconv.Itoa(int(contract.VendorListings[0].Metadata.EscrowTimeoutHours)) + "h")
1011-
addr, redeemScript, err := wal.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
1020+
addr, redeemScript, err := escrowWallet.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
10121021
if err != nil {
10131022
validationErrors = append(validationErrors, "Error generating multisig script")
10141023
return validationErrors
@@ -1150,6 +1159,10 @@ func (n *OpenBazaarNode) ReleaseFunds(contract *pb.RicardianContract, records []
11501159
if err != nil {
11511160
return err
11521161
}
1162+
escrowWallet, ok := wal.(wallet.EscrowWallet)
1163+
if !ok {
1164+
return errors.New("wallet does not support escrow")
1165+
}
11531166

11541167
// Create outputs
11551168
var outputs []wallet.TransactionOutput
@@ -1222,7 +1235,7 @@ func (n *OpenBazaarNode) ReleaseFunds(contract *pb.RicardianContract, records []
12221235
return err
12231236
}
12241237

1225-
mySigs, err := wal.CreateMultisigSignature(inputs, outputs, signingKey, redeemScriptBytes, *big.NewInt(0))
1238+
mySigs, err := escrowWallet.CreateMultisigSignature(inputs, outputs, signingKey, redeemScriptBytes, *big.NewInt(0))
12261239
if err != nil {
12271240
return err
12281241
}
@@ -1249,7 +1262,7 @@ func (n *OpenBazaarNode) ReleaseFunds(contract *pb.RicardianContract, records []
12491262
peerID := order.BuyerID.PeerID
12501263

12511264
// Build, sign, and broadcast transaction
1252-
txnID, err := wal.Multisign(inputs, outputs, mySigs, moderatorSigs, redeemScriptBytes, *big.NewInt(0), true)
1265+
txnID, err := escrowWallet.Multisign(inputs, outputs, mySigs, moderatorSigs, redeemScriptBytes, *big.NewInt(0), true)
12531266
if err != nil {
12541267
return err
12551268
}

core/fulfillment.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ func (n *OpenBazaarNode) FulfillOrder(fulfillment *pb.OrderFulfillment, contract
3737
if err != nil {
3838
return err
3939
}
40+
escrowWallet, ok := wal.(wallet.EscrowWallet)
41+
if !ok {
42+
return errors.New("wallet does not support escrow")
43+
}
4044
currentAddress := wal.CurrentAddress(wallet.EXTERNAL)
4145
payout.PayoutAddress = currentAddress.String()
4246
f := wal.GetFeePerByte(wallet.NORMAL)
@@ -79,7 +83,7 @@ func (n *OpenBazaarNode) FulfillOrder(fulfillment *pb.OrderFulfillment, contract
7983
if !ok {
8084
return errors.New("invalid payout fee value")
8185
}
82-
signatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *fee)
86+
signatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *fee)
8387
if err != nil {
8488
return err
8589
}

core/order.go

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ func (n *OpenBazaarNode) Purchase(data *repo.PurchaseData) (orderID string, paym
212212
}
213213

214214
func prepareModeratedOrderContract(data *repo.PurchaseData, n *OpenBazaarNode, contract *pb.RicardianContract, wal wallet.Wallet) (*pb.RicardianContract, error) {
215+
escrowWallet, ok := wal.(wallet.EscrowWallet)
216+
if !ok {
217+
return nil, errors.New("wallet does not support escrow")
218+
}
215219
if data.Moderator == n.IpfsNode.Identity.Pretty() {
216220
return nil, errors.New("cannot select self as moderator")
217221
}
@@ -303,7 +307,7 @@ func prepareModeratedOrderContract(data *repo.PurchaseData, n *OpenBazaarNode, c
303307
if err != nil {
304308
return nil, err
305309
}
306-
addr, redeemScript, err := wal.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
310+
addr, redeemScript, err := escrowWallet.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
307311
if err != nil {
308312
return nil, err
309313
}
@@ -379,6 +383,10 @@ func processOnlineDirectOrder(resp *pb.Message, n *OpenBazaarNode, wal wallet.Wa
379383
}
380384

381385
func processOfflineDirectOrder(n *OpenBazaarNode, wal wallet.Wallet, contract *pb.RicardianContract, payment *pb.Order_Payment) (string, string, big.Int, error) {
386+
escrowWallet, ok := wal.(wallet.EscrowWallet)
387+
if !ok {
388+
return "", "", big.Int{}, errors.New("wallet does not support escrow")
389+
}
382390
// Vendor offline
383391
// Change payment code to direct
384392
v5Order, err := repo.ToV5Order(contract.BuyerOrder, nil)
@@ -414,7 +422,7 @@ func processOfflineDirectOrder(n *OpenBazaarNode, wal wallet.Wallet, contract *p
414422
if err != nil {
415423
return "", "", *big.NewInt(0), err
416424
}
417-
addr, redeemScript, err := wal.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey}, 1, time.Duration(0), nil)
425+
addr, redeemScript, err := escrowWallet.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey}, 1, time.Duration(0), nil)
418426
if err != nil {
419427
return "", "", *big.NewInt(0), err
420428
}
@@ -1095,6 +1103,10 @@ func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, reco
10951103
if err != nil {
10961104
return err
10971105
}
1106+
escrowWallet, ok := wal.(wallet.EscrowWallet)
1107+
if !ok {
1108+
return errors.New("wallet does not support escrow")
1109+
}
10981110
// Sweep the temp address into our wallet
10991111
var utxos []wallet.TransactionInput
11001112
for _, r := range records {
@@ -1141,7 +1153,7 @@ func (n *OpenBazaarNode) CancelOfflineOrder(contract *pb.RicardianContract, reco
11411153
if err != nil {
11421154
return err
11431155
}
1144-
_, err = wal.SweepAddress(utxos, &refundAddress, buyerKey, &redeemScript, wallet.NORMAL)
1156+
_, err = escrowWallet.SweepAddress(utxos, &refundAddress, buyerKey, &redeemScript, wallet.NORMAL)
11451157
if err != nil {
11461158
return err
11471159
}
@@ -1838,6 +1850,10 @@ func (n *OpenBazaarNode) ValidateDirectPaymentAddress(order *pb.Order) error {
18381850
if err != nil {
18391851
return err
18401852
}
1853+
escrowWallet, ok := wal.(wallet.EscrowWallet)
1854+
if !ok {
1855+
return errors.New("wallet does not support escrow")
1856+
}
18411857
mECKey, err := n.MasterPrivateKey.ECPubKey()
18421858
if err != nil {
18431859
return err
@@ -1850,7 +1866,7 @@ func (n *OpenBazaarNode) ValidateDirectPaymentAddress(order *pb.Order) error {
18501866
if err != nil {
18511867
return err
18521868
}
1853-
addr, redeemScript, err := wal.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey}, 1, time.Duration(0), nil)
1869+
addr, redeemScript, err := escrowWallet.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey}, 1, time.Duration(0), nil)
18541870
if err != nil {
18551871
return err
18561872
}
@@ -1870,6 +1886,10 @@ func (n *OpenBazaarNode) ValidateModeratedPaymentAddress(order *pb.Order, timeou
18701886
if err != nil {
18711887
return err
18721888
}
1889+
escrowWallet, ok := wal.(wallet.EscrowWallet)
1890+
if !ok {
1891+
return errors.New("wallet does not support escrow")
1892+
}
18731893
ipnsPath := ipfspath.FromString(order.Payment.Moderator + "/profile.json")
18741894
profileBytes, err := ipfs.ResolveThenCat(n.IpfsNode, ipnsPath, time.Minute, n.IPNSQuorumSize, true)
18751895
if err != nil {
@@ -1912,7 +1932,7 @@ func (n *OpenBazaarNode) ValidateModeratedPaymentAddress(order *pb.Order, timeou
19121932
if !bytes.Equal(order.Payment.ModeratorKey, modPub.SerializeCompressed()) {
19131933
return errors.New("invalid moderator key")
19141934
}
1915-
addr, redeemScript, err := wal.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
1935+
addr, redeemScript, err := escrowWallet.GenerateMultisigScript([]hd.ExtendedKey{*buyerKey, *vendorKey, *moderatorKey}, 2, timeout, vendorKey)
19161936
if err != nil {
19171937
return err
19181938
}

core/refunds.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@ func (n *OpenBazaarNode) RefundOrder(contract *pb.RicardianContract, records []*
3535
if err != nil {
3636
return err
3737
}
38+
escrowWallet, ok := wal.(wallet.EscrowWallet)
39+
if !ok {
40+
return errors.New("wallet does not support escrow")
41+
}
3842
if order.Payment.Method == pb.Order_Payment_MODERATED {
3943
var ins []wallet.TransactionInput
4044
outValue := big.NewInt(0)
@@ -76,7 +80,7 @@ func (n *OpenBazaarNode) RefundOrder(contract *pb.RicardianContract, records []*
7680
return err
7781
}
7882
f, _ := new(big.Int).SetString(order.BigRefundFee, 10)
79-
signatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *f)
83+
signatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *f)
8084
if err != nil {
8185
return err
8286
}

net/service/handlers.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,10 @@ func (service *OpenBazaarService) handleReject(p peer.ID, pmes *pb.Message, opti
731731
}
732732

733733
if order.Payment.Method != pb.Order_Payment_MODERATED {
734+
escrowWallet, ok := wal.(wallet.EscrowWallet)
735+
if !ok {
736+
return nil, errors.New("wallet does not support escrow")
737+
}
734738
// Sweep the address into our wallet
735739
var txInputs []wallet.TransactionInput
736740
for _, r := range records {
@@ -774,11 +778,15 @@ func (service *OpenBazaarService) handleReject(p peer.ID, pmes *pb.Message, opti
774778
if err != nil {
775779
return nil, err
776780
}
777-
_, err = wal.SweepAddress(txInputs, &refundAddress, buyerKey, &redeemScript, wallet.NORMAL)
781+
_, err = escrowWallet.SweepAddress(txInputs, &refundAddress, buyerKey, &redeemScript, wallet.NORMAL)
778782
if err != nil {
779783
return nil, err
780784
}
781785
} else {
786+
escrowWallet, ok := wal.(wallet.EscrowWallet)
787+
if !ok {
788+
return nil, errors.New("wallet does not support escrow")
789+
}
782790
var ins []wallet.TransactionInput
783791
outValue := big.NewInt(0)
784792
for _, r := range records {
@@ -822,7 +830,7 @@ func (service *OpenBazaarService) handleReject(p peer.ID, pmes *pb.Message, opti
822830
if !ok {
823831
return nil, errors.New("invalid amount")
824832
}
825-
buyerSignatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *fee)
833+
buyerSignatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *fee)
826834
if err != nil {
827835
return nil, err
828836
}
@@ -831,7 +839,7 @@ func (service *OpenBazaarService) handleReject(p peer.ID, pmes *pb.Message, opti
831839
sig := wallet.Signature{InputIndex: s.InputIndex, Signature: s.Signature}
832840
vendorSignatures = append(vendorSignatures, sig)
833841
}
834-
_, err = wal.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *fee, true)
842+
_, err = escrowWallet.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *fee, true)
835843
if err != nil {
836844
return nil, err
837845
}
@@ -926,6 +934,10 @@ func (service *OpenBazaarService) handleRefund(p peer.ID, pmes *pb.Message, opti
926934
}
927935

928936
if order.Payment.Method == pb.Order_Payment_MODERATED {
937+
escrowWallet, ok := wal.(wallet.EscrowWallet)
938+
if !ok {
939+
return nil, errors.New("wallet does not support escrow")
940+
}
929941
var ins []wallet.TransactionInput
930942
outValue := big.NewInt(0)
931943
for _, r := range records {
@@ -969,7 +981,7 @@ func (service *OpenBazaarService) handleRefund(p peer.ID, pmes *pb.Message, opti
969981
if !ok {
970982
return nil, errors.New("invalid amount")
971983
}
972-
buyerSignatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *fee)
984+
buyerSignatures, err := escrowWallet.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, buyerKey, redeemScript, *fee)
973985
if err != nil {
974986
return nil, err
975987
}
@@ -978,7 +990,7 @@ func (service *OpenBazaarService) handleRefund(p peer.ID, pmes *pb.Message, opti
978990
sig := wallet.Signature{InputIndex: s.InputIndex, Signature: s.Signature}
979991
vendorSignatures = append(vendorSignatures, sig)
980992
}
981-
_, err = wal.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *fee, true)
993+
_, err = escrowWallet.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *fee, true)
982994
if err != nil {
983995
return nil, err
984996
}
@@ -1191,6 +1203,10 @@ func (service *OpenBazaarService) handleOrderCompletion(p peer.ID, pmes *pb.Mess
11911203
return nil, err
11921204
}
11931205
if order.Payment.Method == pb.Order_Payment_MODERATED && state != pb.OrderState_DISPUTED && state != pb.OrderState_DECIDED && state != pb.OrderState_RESOLVED && state != pb.OrderState_PAYMENT_FINALIZED {
1206+
escrowWallet, ok := wal.(wallet.EscrowWallet)
1207+
if !ok {
1208+
return nil, errors.New("wallet does not support escrow")
1209+
}
11941210
var ins []wallet.TransactionInput
11951211
outValue := big.NewInt(0)
11961212
for _, r := range records {
@@ -1243,7 +1259,7 @@ func (service *OpenBazaarService) handleOrderCompletion(p peer.ID, pmes *pb.Mess
12431259
if !ok {
12441260
return nil, errors.New("invalid amount")
12451261
}
1246-
_, err = wal.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *payoutFee, true)
1262+
_, err = escrowWallet.Multisign(ins, []wallet.TransactionOutput{output}, buyerSignatures, vendorSignatures, redeemScript, *payoutFee, true)
12471263
if err != nil {
12481264
if err.Error() == "ERROR_INSUFFICIENT_FUNDS" {
12491265
err0 := service.node.Datastore.Messages().Put(

0 commit comments

Comments
 (0)