Skip to content

Commit 0feae9f

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 1c4c49e commit 0feae9f

File tree

1 file changed

+72
-17
lines changed

1 file changed

+72
-17
lines changed

rpcserver.go

Lines changed: 72 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7227,13 +7227,28 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
72277227
pReq := req.PaymentRequest
72287228
ctx := stream.Context()
72297229

7230-
// Do some preliminary checks on the asset ID and make sure we have any
7231-
// balance for that asset.
7232-
if len(req.AssetId) != sha256.Size {
7233-
return fmt.Errorf("asset ID must be 32 bytes")
7230+
var specifier asset.Specifier
7231+
7232+
switch {
7233+
case len(req.AssetId) > 0 && len(req.GroupKey) > 0:
7234+
return fmt.Errorf("cannot set both asset id and group key")
7235+
7236+
case len(req.AssetId) > 0:
7237+
if len(req.AssetId) != sha256.Size {
7238+
return fmt.Errorf("asset ID must be 32 bytes")
7239+
}
7240+
var assetID asset.ID
7241+
copy(assetID[:], req.AssetId)
7242+
specifier = asset.NewSpecifierFromId(assetID)
7243+
7244+
case len(req.GroupKey) > 0:
7245+
groupKey, err := btcec.ParsePubKey(req.GroupKey)
7246+
if err != nil {
7247+
return fmt.Errorf("failed to parse group key: %w",
7248+
err)
7249+
}
7250+
specifier = asset.NewSpecifierFromGroupKey(*groupKey)
72347251
}
7235-
var assetID asset.ID
7236-
copy(assetID[:], req.AssetId)
72377252

72387253
// Now that we know we have at least _some_ asset balance, we'll figure
72397254
// out what kind of payment this is, so we can determine _how many_
@@ -7358,7 +7373,26 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
73587373
peerPubKey = &parsedKey
73597374
}
73607375

7361-
specifier := asset.NewSpecifierFromId(assetID)
7376+
var rpcSpecifier rfqrpc.AssetSpecifier
7377+
7378+
switch {
7379+
case specifier.HasId():
7380+
assetID := specifier.UnwrapIdToPtr()
7381+
rpcSpecifier = rfqrpc.AssetSpecifier{
7382+
Id: &rfqrpc.AssetSpecifier_AssetId{
7383+
AssetId: assetID[:],
7384+
},
7385+
}
7386+
7387+
case specifier.HasGroupPubKey():
7388+
groupKey := specifier.UnwrapGroupKeyToPtr()
7389+
groupKeyBytes := groupKey.SerializeCompressed()
7390+
rpcSpecifier = rfqrpc.AssetSpecifier{
7391+
Id: &rfqrpc.AssetSpecifier_GroupKey{
7392+
GroupKey: groupKeyBytes,
7393+
},
7394+
}
7395+
}
73627396

73637397
// We can now query the asset channels we have.
73647398
assetChan, err := r.rfqChannel(
@@ -7384,14 +7418,10 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
73847418

73857419
resp, err := r.AddAssetSellOrder(
73867420
ctx, &rfqrpc.AddAssetSellOrderRequest{
7387-
AssetSpecifier: &rfqrpc.AssetSpecifier{
7388-
Id: &rfqrpc.AssetSpecifier_AssetId{
7389-
AssetId: assetID[:],
7390-
},
7391-
},
7392-
PaymentMaxAmt: uint64(paymentMaxAmt),
7393-
Expiry: uint64(expiry.Unix()),
7394-
PeerPubKey: peerPubKey[:],
7421+
AssetSpecifier: &rpcSpecifier,
7422+
PaymentMaxAmt: uint64(paymentMaxAmt),
7423+
Expiry: uint64(expiry.Unix()),
7424+
PeerPubKey: peerPubKey[:],
73957425
TimeoutSeconds: uint32(
73967426
rfq.DefaultTimeout.Seconds(),
73977427
),
@@ -7474,9 +7504,34 @@ func (r *rpcServer) SendPayment(req *tchrpc.SendPaymentRequest,
74747504
"for keysend payment")
74757505
}
74767506

7477-
balances := []*rfqmsg.AssetBalance{
7478-
rfqmsg.NewAssetBalance(assetID, req.AssetAmount),
7507+
var balances []*rfqmsg.AssetBalance
7508+
7509+
switch {
7510+
case specifier.HasId():
7511+
balances = []*rfqmsg.AssetBalance{
7512+
rfqmsg.NewAssetBalance(
7513+
*specifier.UnwrapIdToPtr(),
7514+
req.AssetAmount,
7515+
),
7516+
}
7517+
7518+
case specifier.HasGroupPubKey():
7519+
groupKey := specifier.UnwrapGroupKeyToPtr()
7520+
groupHash := sha256.Sum256(
7521+
groupKey.SerializeCompressed(),
7522+
)
7523+
7524+
// We can't distribute the amount over distinct asset ID
7525+
// balances, so we provide the total amount under the
7526+
// dummy asset ID that is produced by hashing the group
7527+
// key.
7528+
balances = []*rfqmsg.AssetBalance{
7529+
rfqmsg.NewAssetBalance(
7530+
groupHash, req.AssetAmount,
7531+
),
7532+
}
74797533
}
7534+
74807535
htlc := rfqmsg.NewHtlc(balances, fn.None[rfqmsg.ID]())
74817536

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

0 commit comments

Comments
 (0)