forked from OpenBazaar/openbazaar-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refunds.go
165 lines (158 loc) · 4.51 KB
/
refunds.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package core
import (
"encoding/hex"
"errors"
"github.com/OpenBazaar/openbazaar-go/repo"
"math/big"
"strings"
"time"
"github.com/OpenBazaar/openbazaar-go/pb"
"github.com/OpenBazaar/wallet-interface"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
)
// RefundOrder - refund buyer
func (n *OpenBazaarNode) RefundOrder(contract *pb.RicardianContract, records []*wallet.TransactionRecord) error {
refundMsg := new(pb.Refund)
orderID, err := n.CalcOrderID(contract.BuyerOrder)
if err != nil {
return err
}
refundMsg.OrderID = orderID
ts, err := ptypes.TimestampProto(time.Now())
if err != nil {
return err
}
refundMsg.Timestamp = ts
order, err := repo.ToV5Order(contract.BuyerOrder, n.LookupCurrency)
if err != nil {
return err
}
wal, err := n.Multiwallet.WalletForCurrencyCode(order.Payment.AmountCurrency.Code)
if err != nil {
return err
}
if order.Payment.Method == pb.Order_Payment_MODERATED {
var ins []wallet.TransactionInput
outValue := big.NewInt(0)
for _, r := range records {
if !r.Spent && r.Value.Cmp(big.NewInt(0)) > 0 {
outpointHash, err := hex.DecodeString(strings.TrimPrefix(r.Txid, "0x"))
if err != nil {
return err
}
outValue = new(big.Int).Add(outValue, &r.Value)
in := wallet.TransactionInput{OutpointIndex: r.Index, OutpointHash: outpointHash, Value: r.Value}
ins = append(ins, in)
}
}
refundAddress, err := wal.DecodeAddress(order.RefundAddress)
if err != nil {
return err
}
output := wallet.TransactionOutput{
Address: refundAddress,
Value: *outValue,
}
chaincode, err := hex.DecodeString(order.Payment.Chaincode)
if err != nil {
return err
}
mECKey, err := n.MasterPrivateKey.ECPrivKey()
if err != nil {
return err
}
vendorKey, err := wal.ChildKey(mECKey.Serialize(), chaincode, true)
if err != nil {
return err
}
redeemScript, err := hex.DecodeString(order.Payment.RedeemScript)
if err != nil {
return err
}
f, _ := new(big.Int).SetString(order.BigRefundFee, 10)
signatures, err := wal.CreateMultisigSignature(ins, []wallet.TransactionOutput{output}, vendorKey, redeemScript, *f)
if err != nil {
return err
}
var sigs []*pb.BitcoinSignature
for _, s := range signatures {
pbSig := &pb.BitcoinSignature{Signature: s.Signature, InputIndex: s.InputIndex}
sigs = append(sigs, pbSig)
}
refundMsg.Sigs = sigs
} else {
outValue := big.NewInt(0)
for _, r := range records {
if r.Value.Cmp(big.NewInt(0)) > 0 {
outValue = new(big.Int).Add(outValue, &r.Value)
}
}
refundAddr, err := wal.DecodeAddress(order.RefundAddress)
if err != nil {
return err
}
txid, err := wal.Spend(*outValue, refundAddr, wallet.NORMAL, orderID, false)
if err != nil {
return err
}
txinfo := new(pb.Refund_TransactionInfo)
txinfo.Txid = txid.String()
txinfo.BigValue = outValue.String()
txinfo.ValueCurrency = contract.BuyerOrder.Payment.AmountCurrency
refundMsg.RefundTransaction = txinfo
}
contract.Refund = refundMsg
contract, err = n.SignRefund(contract)
if err != nil {
return err
}
err = n.SendRefund(order.BuyerID.PeerID, contract)
if err != nil {
// TODO: do we retry a failed refund send?
log.Error(err)
}
err = n.Datastore.Sales().Put(orderID, *contract, pb.OrderState_REFUNDED, true)
if err != nil {
log.Error(err)
}
return nil
}
// SignRefund - add signature to refund
func (n *OpenBazaarNode) SignRefund(contract *pb.RicardianContract) (*pb.RicardianContract, error) {
serializedRefund, err := proto.Marshal(contract.Refund)
if err != nil {
return contract, err
}
s := new(pb.Signature)
s.Section = pb.Signature_REFUND
guidSig, err := n.IpfsNode.PrivateKey.Sign(serializedRefund)
if err != nil {
return contract, err
}
s.SignatureBytes = guidSig
contract.Signatures = append(contract.Signatures, s)
return contract, nil
}
// VerifySignaturesOnRefund - verify signatures on refund
func (n *OpenBazaarNode) VerifySignaturesOnRefund(contract *pb.RicardianContract) error {
if err := verifyMessageSignature(
contract.Refund,
contract.VendorListings[0].VendorID.Pubkeys.Identity,
contract.Signatures,
pb.Signature_REFUND,
contract.VendorListings[0].VendorID.PeerID,
); err != nil {
switch err.(type) {
case noSigError:
return errors.New("contract does not contain a signature for the refund")
case invalidSigError:
return errors.New("vendor's guid signature on contact failed to verify")
case matchKeyError:
return errors.New("public key in order does not match reported vendor ID")
default:
return err
}
}
return nil
}