Skip to content

Commit 75a67e6

Browse files
committed
rpcserver: SendPayment uses groupkey
We have taken care of the groupkey RFQ negotiation in previous commits. All we need now to support sending a payment over a group of assets, is to propagate the user specifier groupkey to the corresponding fields.
1 parent b5fc58a commit 75a67e6

File tree

1 file changed

+44
-17
lines changed

1 file changed

+44
-17
lines changed

rpcserver.go

Lines changed: 44 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7246,19 +7246,27 @@ func (r *rpcServer) EncodeCustomRecords(_ context.Context,
72467246
func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
72477247
stream tchrpc.TaprootAssetChannels_SendPaymentServer) error {
72487248

7249+
if len(req.AssetId) > 0 && len(req.GroupKey) > 0 {
7250+
return fmt.Errorf("cannot set both asset id and group key")
7251+
}
7252+
72497253
if req.PaymentRequest == nil {
72507254
return fmt.Errorf("payment request must be specified")
72517255
}
72527256
pReq := req.PaymentRequest
72537257
ctx := stream.Context()
72547258

7255-
// Do some preliminary checks on the asset ID and make sure we have any
7256-
// balance for that asset.
7257-
if len(req.AssetId) != sha256.Size {
7258-
return fmt.Errorf("asset ID must be 32 bytes")
7259+
assetID, groupKey, err := parseAssetSpecifier(
7260+
req.AssetId, "", req.GroupKey, "",
7261+
)
7262+
if err != nil {
7263+
return err
7264+
}
7265+
7266+
specifier, err := asset.NewExclusiveSpecifier(assetID, groupKey)
7267+
if err != nil {
7268+
return err
72597269
}
7260-
var assetID asset.ID
7261-
copy(assetID[:], req.AssetId)
72627270

72637271
// Now that we know we have at least _some_ asset balance, we'll figure
72647272
// out what kind of payment this is, so we can determine _how many_
@@ -7383,7 +7391,7 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
73837391
peerPubKey = &parsedKey
73847392
}
73857393

7386-
specifier := asset.NewSpecifierFromId(assetID)
7394+
rpcSpecifier := marshalAssetSpecifier(specifier)
73877395

73887396
// We can now query the asset channels we have.
73897397
assetChan, err := r.rfqChannel(
@@ -7409,14 +7417,10 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
74097417

74107418
resp, err := r.AddAssetSellOrder(
74117419
ctx, &rfqrpc.AddAssetSellOrderRequest{
7412-
AssetSpecifier: &rfqrpc.AssetSpecifier{
7413-
Id: &rfqrpc.AssetSpecifier_AssetId{
7414-
AssetId: assetID[:],
7415-
},
7416-
},
7417-
PaymentMaxAmt: uint64(paymentMaxAmt),
7418-
Expiry: uint64(expiry.Unix()),
7419-
PeerPubKey: peerPubKey[:],
7420+
AssetSpecifier: &rpcSpecifier,
7421+
PaymentMaxAmt: uint64(paymentMaxAmt),
7422+
Expiry: uint64(expiry.Unix()),
7423+
PeerPubKey: peerPubKey[:],
74207424
TimeoutSeconds: uint32(
74217425
rfq.DefaultTimeout.Seconds(),
74227426
),
@@ -7499,9 +7503,32 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
74997503
"for keysend payment")
75007504
}
75017505

7502-
balances := []*rfqmsg.AssetBalance{
7503-
rfqmsg.NewAssetBalance(assetID, req.AssetAmount),
7506+
var balances []*rfqmsg.AssetBalance
7507+
7508+
switch {
7509+
case specifier.HasId():
7510+
balances = []*rfqmsg.AssetBalance{
7511+
rfqmsg.NewAssetBalance(
7512+
*specifier.UnwrapIdToPtr(),
7513+
req.AssetAmount,
7514+
),
7515+
}
7516+
7517+
case specifier.HasGroupPubKey():
7518+
groupKey := specifier.UnwrapGroupKeyToPtr()
7519+
groupKeyX := schnorr.SerializePubKey(groupKey)
7520+
7521+
// We can't distribute the amount over distinct asset ID
7522+
// balances, so we provide the total amount under the
7523+
// dummy asset ID that is produced by hashing the group
7524+
// key.
7525+
balances = []*rfqmsg.AssetBalance{
7526+
rfqmsg.NewAssetBalance(
7527+
asset.ID(groupKeyX), req.AssetAmount,
7528+
),
7529+
}
75047530
}
7531+
75057532
htlc := rfqmsg.NewHtlc(balances, fn.None[rfqmsg.ID]())
75067533

75077534
// We'll now map the HTLC struct into a set of TLV records,

0 commit comments

Comments
 (0)