Skip to content

Commit fe5827a

Browse files
authored
Use multi payments for batch api (keybase#16728)
* Revendor stellarnet * Add submitMultiPayment to remote protocol * Change SubmitMulti result * Add useMulti flag to BatchLocal * Submit the multi payment * Implement prepareDirectOp * Implement prepareRelayOp * Removed DisplayAmount, DisplayCurrency from Op * Fix mock
1 parent b415621 commit fe5827a

22 files changed

Lines changed: 1042 additions & 137 deletions

File tree

‎go/client/wallet_api_handler.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ func (w *walletAPIHandler) batch(ctx context.Context, c Call, wr io.Writer) erro
147147
arg := stellar1.BatchLocalArg{
148148
BatchID: opts.BatchID,
149149
TimeoutSecs: opts.Timeout,
150+
UseMulti: true,
150151
}
151152
for _, p := range opts.Payments {
152153
arg.Payments = append(arg.Payments, stellar1.BatchPaymentArg{Recipient: p.Recipient, Amount: p.Amount, Message: p.Message})

‎go/protocol/stellar1/local.go‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/protocol/stellar1/remote.go‎

Lines changed: 118 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎go/stellar/batch_multi.go‎

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,212 @@
1+
package stellar
2+
3+
import (
4+
"encoding/base64"
5+
"fmt"
6+
"time"
7+
8+
"github.com/keybase/client/go/libkb"
9+
"github.com/keybase/client/go/protocol/keybase1"
10+
"github.com/keybase/client/go/protocol/stellar1"
11+
"github.com/keybase/client/go/stellar/relays"
12+
"github.com/keybase/client/go/stellar/remote"
13+
"github.com/keybase/client/go/stellar/stellarcommon"
14+
"github.com/keybase/stellarnet"
15+
"github.com/stellar/go/keypair"
16+
)
17+
18+
type multiOp struct {
19+
Recipient stellar1.AccountID
20+
Amount string
21+
CreateAccount bool
22+
Op stellar1.PaymentOp
23+
}
24+
25+
// BatchMulti sends a batch of payments from the user to multiple recipients in
26+
// a single multi-operation transaction.
27+
func BatchMulti(mctx libkb.MetaContext, walletState *WalletState, arg stellar1.BatchLocalArg) (res stellar1.BatchResultLocal, err error) {
28+
mctx = mctx.WithLogTag("BMULT=" + arg.BatchID)
29+
30+
startTime := time.Now()
31+
res.StartTime = stellar1.ToTimeMs(startTime)
32+
defer func() {
33+
if res.EndTime == 0 {
34+
res.EndTime = stellar1.ToTimeMs(time.Now())
35+
}
36+
}()
37+
38+
// look up sender account
39+
senderAccountID, senderSeed, err := LookupSenderSeed(mctx)
40+
if err != nil {
41+
return res, err
42+
}
43+
44+
mctx.Debug("Batch sender account ID: %s", senderAccountID)
45+
mctx.Debug("Batch size: %d", len(arg.Payments))
46+
47+
results := make([]stellar1.BatchPaymentResult, len(arg.Payments))
48+
var multiOps []multiOp
49+
for i, payment := range arg.Payments {
50+
results[i] = stellar1.BatchPaymentResult{
51+
Username: libkb.NewNormalizedUsername(payment.Recipient).String(),
52+
}
53+
recipient, err := LookupRecipient(mctx, stellarcommon.RecipientInput(payment.Recipient), false /* isCLI for identify purposes */)
54+
if err != nil {
55+
mctx.Debug("LookupRecipient error: %s", err)
56+
makeResultError(&results[i], err)
57+
continue
58+
}
59+
60+
if recipient.AccountID == nil {
61+
mop, err := prepareRelayOp(mctx, payment, recipient)
62+
if err != nil {
63+
makeResultError(&results[i], err)
64+
continue
65+
}
66+
multiOps = append(multiOps, mop)
67+
} else {
68+
mop, err := prepareDirectOp(mctx, walletState, payment, recipient)
69+
if err != nil {
70+
makeResultError(&results[i], err)
71+
continue
72+
}
73+
multiOps = append(multiOps, mop)
74+
}
75+
}
76+
77+
baseFee := walletState.BaseFee(mctx)
78+
sp, unlock := NewSeqnoProvider(mctx, walletState)
79+
defer unlock()
80+
tx := stellarnet.NewBaseTx(stellarnet.AddressStr(senderAccountID), sp, baseFee)
81+
var post stellar1.PaymentMultiPost
82+
83+
// add all the prepared ops here
84+
for _, mop := range multiOps {
85+
if mop.CreateAccount {
86+
tx.AddCreateAccountOp(stellarnet.AddressStr(mop.Recipient), mop.Amount)
87+
} else {
88+
tx.AddPaymentOp(stellarnet.AddressStr(mop.Recipient), mop.Amount)
89+
}
90+
post.Operations = append(post.Operations, mop.Op)
91+
}
92+
93+
// sign the tx
94+
sr, err := tx.Sign(senderSeed)
95+
if err != nil {
96+
return res, err
97+
}
98+
post.SignedTransaction = sr.Signed
99+
post.FromDeviceID = mctx.ActiveDevice().DeviceID()
100+
101+
// submit it
102+
submitRes, err := walletState.SubmitMultiPayment(mctx.Ctx(), post)
103+
if err != nil {
104+
// make all the results have an error
105+
for _, r := range results {
106+
makeResultError(&r, err)
107+
}
108+
} else {
109+
// make all ther results have success
110+
now := stellar1.ToTimeMs(time.Now())
111+
for i := 0; i < len(results); i++ {
112+
results[i].TxID = submitRes.TxID
113+
results[i].Status = stellar1.PaymentStatus_COMPLETED
114+
results[i].EndTime = now
115+
}
116+
}
117+
118+
res.Payments = results
119+
120+
return res, nil
121+
}
122+
123+
func prepareDirectOp(mctx libkb.MetaContext, remoter remote.Remoter, payment stellar1.BatchPaymentArg, recipient stellarcommon.Recipient) (multiOp, error) {
124+
op := multiOp{
125+
Recipient: stellar1.AccountID(recipient.AccountID.String()),
126+
Amount: payment.Amount,
127+
}
128+
129+
funded, err := isAccountFunded(mctx.Ctx(), remoter, op.Recipient)
130+
if err != nil {
131+
return op, err
132+
}
133+
134+
if !funded {
135+
if isAmountLessThanMin(payment.Amount, minAmountCreateAccountXLM) {
136+
return op, fmt.Errorf("you must send at least %s XLM to fund the account for %s", minAmountCreateAccountXLM, payment.Recipient)
137+
}
138+
op.CreateAccount = true
139+
}
140+
141+
op.Op = stellar1.PaymentOp{
142+
To: &recipient.User.UV,
143+
Direct: &stellar1.DirectOp{},
144+
}
145+
146+
if len(payment.Message) > 0 {
147+
noteClear := stellar1.NoteContents{
148+
Note: payment.Message,
149+
}
150+
var recipientUv *keybase1.UserVersion
151+
if recipient.User != nil {
152+
recipientUv = &recipient.User.UV
153+
}
154+
op.Op.Direct.NoteB64, err = NoteEncryptB64(mctx, noteClear, recipientUv)
155+
if err != nil {
156+
return op, err
157+
}
158+
}
159+
160+
return op, nil
161+
}
162+
163+
func prepareRelayOp(mctx libkb.MetaContext, payment stellar1.BatchPaymentArg, recipient stellarcommon.Recipient) (multiOp, error) {
164+
op := multiOp{
165+
Amount: payment.Amount,
166+
CreateAccount: true,
167+
}
168+
if isAmountLessThanMin(payment.Amount, minAmountRelayXLM) {
169+
return op, fmt.Errorf("you must send at least %s XLM to fund the account for %s", minAmountRelayXLM, payment.Recipient)
170+
}
171+
172+
appKey, teamID, err := relays.GetKey(mctx, recipient)
173+
if err != nil {
174+
return op, err
175+
}
176+
177+
relayKp, err := keypair.Random()
178+
if err != nil {
179+
return op, err
180+
}
181+
relayAccountID, err := stellarnet.NewAddressStr(relayKp.Address())
182+
if err != nil {
183+
return op, err
184+
}
185+
186+
op.Recipient = stellar1.AccountID(relayAccountID.String())
187+
188+
enc, err := relays.Encrypt(stellar1.RelayContents{
189+
Sk: stellar1.SecretKey(relayKp.Seed()),
190+
Note: payment.Message,
191+
}, appKey)
192+
if err != nil {
193+
return op, err
194+
}
195+
pack, err := libkb.MsgpackEncode(enc)
196+
if err != nil {
197+
return op, err
198+
}
199+
boxB64 := base64.StdEncoding.EncodeToString(pack)
200+
201+
op.Op = stellar1.PaymentOp{
202+
To: &recipient.User.UV,
203+
Relay: &stellar1.RelayOp{
204+
ToAssertion: string(recipient.Input),
205+
RelayAccount: stellar1.AccountID(relayAccountID.String()),
206+
TeamID: teamID,
207+
BoxB64: boxB64,
208+
},
209+
}
210+
211+
return op, nil
212+
}

0 commit comments

Comments
 (0)