Skip to content

Commit 1585a2d

Browse files
committed
rfqmsg: generate ID for SCID alias into custom range
A recent change to the lnd SCID alias RPCs requires us to generate SCID aliases in a certain range. Because we derive the alias from the randomly generated RFQ ID, we need to make sure we derive a random ID that can be successfully transformed into a valid SCID alias.
1 parent eeadc03 commit 1585a2d

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

rfqmsg/buy_request.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rfqmsg
22

33
import (
4-
"crypto/rand"
54
"fmt"
65

76
"github.com/btcsuite/btcd/btcec/v2"
@@ -54,8 +53,7 @@ func NewBuyRequest(peer route.Vertex, assetID *asset.ID,
5453
suggestedAssetRate fn.Option[rfqmath.BigIntFixedPoint]) (*BuyRequest,
5554
error) {
5655

57-
var id [32]byte
58-
_, err := rand.Read(id[:])
56+
id, err := NewID()
5957
if err != nil {
6058
return nil, fmt.Errorf("unable to generate random "+
6159
"quote request id: %w", err)

rfqmsg/messages.go

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package rfqmsg
22

33
import (
4+
"crypto/rand"
45
"crypto/sha256"
56
"encoding/binary"
67
"encoding/hex"
@@ -9,6 +10,7 @@ import (
910
"math"
1011

1112
"github.com/lightninglabs/taproot-assets/rfqmath"
13+
"github.com/lightningnetwork/lnd/aliasmgr"
1214
"github.com/lightningnetwork/lnd/lnwire"
1315
"github.com/lightningnetwork/lnd/routing/route"
1416
"github.com/lightningnetwork/lnd/tlv"
@@ -17,9 +19,45 @@ import (
1719
// SerialisedScid is a serialised short channel id (SCID).
1820
type SerialisedScid uint64
1921

20-
// ID is the identifier for a RFQ message.
22+
// ID is the identifier for a RFQ message. A new ID _MUST_ be created using the
23+
// NewID constructor to make sure it can be transformed into a valid SCID alias.
2124
type ID [32]byte
2225

26+
// NewID generates a new random ID that can be transformed into a valid SCID
27+
// alias that is in the allowed range for lnd.
28+
func NewID() (ID, error) {
29+
// We make sure we don't loop endlessly in case we can't find a valid
30+
// ID. We should never reach this limit in practice, the chances for
31+
// finding a valid ID are very high.
32+
const maxNumTries = 10e6
33+
var (
34+
id ID
35+
numTries int
36+
)
37+
38+
for {
39+
_, err := rand.Read(id[:])
40+
if err != nil {
41+
return id, err
42+
}
43+
44+
// We make sure that when deriving the SCID alias from the ID,
45+
// we get a valid alias. If not, we try again.
46+
scid := lnwire.NewShortChanIDFromInt(uint64(id.Scid()))
47+
if aliasmgr.IsAlias(scid) {
48+
break
49+
}
50+
51+
numTries++
52+
53+
if numTries >= maxNumTries {
54+
return id, errors.New("unable to find valid ID")
55+
}
56+
}
57+
58+
return id, nil
59+
}
60+
2361
// String returns the string representation of the ID.
2462
func (id ID) String() string {
2563
return hex.EncodeToString(id[:])
@@ -33,7 +71,9 @@ func (id ID) Scid() SerialisedScid {
3371
scidBytes := id[24:]
3472

3573
scidInteger := binary.BigEndian.Uint64(scidBytes)
36-
return SerialisedScid(scidInteger)
74+
scid := lnwire.NewShortChanIDFromInt(scidInteger)
75+
76+
return SerialisedScid(scid.ToUint64())
3777
}
3878

3979
// Record returns a TLV record that can be used to encode/decode an ID to/from a

rfqmsg/messages_test.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,16 @@ import (
1111
"github.com/stretchr/testify/require"
1212
)
1313

14+
// TestNewID tests that we can easily derive 1000 new IDs without any errors.
15+
func TestNewID(t *testing.T) {
16+
const numIDs = 1000
17+
18+
for range numIDs {
19+
_, err := NewID()
20+
require.NoError(t, err)
21+
}
22+
}
23+
1424
// TestTlvFixedPoint tests encoding and decoding of the TlvFixedPoint struct.
1525
func TestTlvFixedPoint(t *testing.T) {
1626
// This is the test case structure which will be encoded and decoded.

rfqmsg/sell_request.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package rfqmsg
22

33
import (
4-
"crypto/rand"
54
"fmt"
65

76
"github.com/btcsuite/btcd/btcec/v2"
@@ -56,8 +55,7 @@ func NewSellRequest(peer route.Vertex, assetID *asset.ID,
5655
suggestedAssetRate fn.Option[rfqmath.BigIntFixedPoint]) (*SellRequest,
5756
error) {
5857

59-
var id [32]byte
60-
_, err := rand.Read(id[:])
58+
id, err := NewID()
6159
if err != nil {
6260
return nil, fmt.Errorf("unable to generate random id: %w", err)
6361
}

0 commit comments

Comments
 (0)