Skip to content

Commit ee8aac2

Browse files
committed
rfqmsg: populate transfer type field in request message
1 parent e4bbef0 commit ee8aac2

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

rfqmsg/messages.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,63 @@ func (id *ID) Record() tlv.Record {
9090
return tlv.MakeStaticRecord(0, id, recordSize, IdEncoder, IdDecoder)
9191
}
9292

93+
// TransferType defines the type of transaction which will be performed if the
94+
// quote request leads to an accepted agreement.
95+
type TransferType uint8
96+
97+
const (
98+
// UnspecifiedTransferType represents an undefined or transfer type.
99+
UnspecifiedTransferType TransferType = 0
100+
101+
// PayInvoiceTransferType indicates that the requesting peer wants to
102+
// pay a Lightning Network invoice using a taproot asset.
103+
PayInvoiceTransferType TransferType = 1
104+
105+
// RecvPaymentTransferType indicates that the requesting peer wants
106+
// to receive taproot asset funds linked to a Lightning Network invoice.
107+
RecvPaymentTransferType TransferType = 2
108+
)
109+
110+
// Record returns a TLV record that can be used to encode/decode a transfer type
111+
// to/from a TLV stream.
112+
//
113+
// NOTE: This is part of the tlv.RecordProducer interface.
114+
func (t *TransferType) Record() tlv.Record {
115+
// Note that we set the type here as zero, as when used with a
116+
// tlv.RecordT, the type param will be used as the type.
117+
return tlv.MakeStaticRecord(
118+
0, t, 1, TransferTypeEncoder, TransferTypeDecoder,
119+
)
120+
}
121+
122+
// TransferTypeEncoder is a function that can be used to encode a TransferType
123+
// to a writer.
124+
func TransferTypeEncoder(w io.Writer, val any, buf *[8]byte) error {
125+
if transferType, ok := val.(*TransferType); ok {
126+
transferTypeInt := uint8(*transferType)
127+
return tlv.EUint8(w, &transferTypeInt, buf)
128+
}
129+
130+
return tlv.NewTypeForEncodingErr(val, "TransferType")
131+
}
132+
133+
// TransferTypeDecoder is a function that can be used to decode a TransferType
134+
// from a reader.
135+
func TransferTypeDecoder(r io.Reader, val any, buf *[8]byte, l uint64) error {
136+
if transferType, ok := val.(*TransferType); ok {
137+
var transferTypeInt uint8
138+
err := tlv.DUint8(r, &transferTypeInt, buf, l)
139+
if err != nil {
140+
return err
141+
}
142+
143+
*transferType = TransferType(transferTypeInt)
144+
return nil
145+
}
146+
147+
return tlv.NewTypeForDecodingErr(val, "TransferType", l, 8)
148+
}
149+
93150
// AssetRate represents the exchange rate of an asset to BTC, encapsulating
94151
// both the rate in fixed-point format and an expiration timestamp.
95152
//

rfqmsg/request.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,9 @@ type requestWireMsgData struct {
6464
// ID is the unique identifier of the quote request.
6565
ID tlv.RecordT[tlv.TlvType2, ID]
6666

67-
// TODO(ffranr): Add transfer type field with TLV type 4.
67+
// TransferType defines the type of transaction which will be performed
68+
// if the quote request leads to an accepted agreement.
69+
TransferType tlv.RecordT[tlv.TlvType4, TransferType]
6870

6971
// Expiry is the Unix timestamp (in seconds) when the quote expires.
7072
// The quote becomes invalid after this time.
@@ -129,6 +131,7 @@ type requestWireMsgData struct {
129131
func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
130132
version := tlv.NewRecordT[tlv.TlvType0](q.Version)
131133
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
134+
transferType := tlv.NewRecordT[tlv.TlvType4](RecvPaymentTransferType)
132135

133136
// Set the expiry to the default request lifetime unless an asset rate
134137
// hint is provided.
@@ -179,6 +182,7 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
179182
return requestWireMsgData{
180183
Version: version,
181184
ID: id,
185+
TransferType: transferType,
182186
Expiry: expiryTlv,
183187
InAssetID: inAssetID,
184188
InAssetGroupKey: inAssetGroupKey,
@@ -194,6 +198,7 @@ func newRequestWireMsgDataFromBuy(q BuyRequest) (requestWireMsgData, error) {
194198
func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
195199
version := tlv.NewPrimitiveRecord[tlv.TlvType0](q.Version)
196200
id := tlv.NewRecordT[tlv.TlvType2](q.ID)
201+
transferType := tlv.NewRecordT[tlv.TlvType4](PayInvoiceTransferType)
197202

198203
// Set the expiry to the default request lifetime unless an asset rate
199204
// hint is provided.
@@ -247,6 +252,7 @@ func newRequestWireMsgDataFromSell(q SellRequest) (requestWireMsgData, error) {
247252
return requestWireMsgData{
248253
Version: version,
249254
ID: id,
255+
TransferType: transferType,
250256
Expiry: expiryTlv,
251257
InAssetID: inAssetID,
252258
OutAssetID: outAssetID,
@@ -328,6 +334,7 @@ func (m *requestWireMsgData) Encode(w io.Writer) error {
328334
records := []tlv.Record{
329335
m.Version.Record(),
330336
m.ID.Record(),
337+
m.TransferType.Record(),
331338
m.Expiry.Record(),
332339
m.MaxInAsset.Record(),
333340
}
@@ -408,6 +415,7 @@ func (m *requestWireMsgData) Decode(r io.Reader) error {
408415
tlvStream, err := tlv.NewStream(
409416
m.Version.Record(),
410417
m.ID.Record(),
418+
m.TransferType.Record(),
411419
m.Expiry.Record(),
412420

413421
inAssetID.Record(),

0 commit comments

Comments
 (0)