|
| 1 | +package cln |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/hex" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/btcsuite/btcd/btcec/v2" |
| 8 | + "github.com/lightningnetwork/lnd/keychain" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | +) |
| 11 | + |
| 12 | +var ( |
| 13 | + hsmSecret = [32]byte{ |
| 14 | + 0x3f, 0x0a, 0x06, 0xc6, 0x38, 0x5b, 0x74, 0x93, |
| 15 | + 0xf7, 0x5a, 0xa0, 0x08, 0x9f, 0x31, 0x6a, 0x13, |
| 16 | + 0xbf, 0x72, 0xbe, 0xb4, 0x30, 0xe5, 0x9e, 0x71, |
| 17 | + 0xb5, 0xac, 0x5a, 0x73, 0x58, 0x1a, 0x62, 0x70, |
| 18 | + } |
| 19 | + nodeKeyBytes, _ = hex.DecodeString( |
| 20 | + "035149629152c1bee83f1e148a51400b5f24bf3e2ca53384dd801418446e" + |
| 21 | + "1f53fe", |
| 22 | + ) |
| 23 | + |
| 24 | + peerPubKeyBytes, _ = hex.DecodeString( |
| 25 | + "02678187ca43e6a6f62f9185be98a933bf485313061e6a05578bbd83c54e" + |
| 26 | + "88d460", |
| 27 | + ) |
| 28 | + peerPubKey, _ = btcec.ParsePubKey(peerPubKeyBytes) |
| 29 | + |
| 30 | + expectedFundingKeyBytes, _ = hex.DecodeString( |
| 31 | + "0326a2171c97673cc8cd7a04a043f0224c59591fc8c9de320a48f7c9b68a" + |
| 32 | + "b0ae2b", |
| 33 | + ) |
| 34 | +) |
| 35 | + |
| 36 | +func TestNodeKey(t *testing.T) { |
| 37 | + nodeKey, _, err := NodeKey(hsmSecret) |
| 38 | + require.NoError(t, err) |
| 39 | + |
| 40 | + require.Equal(t, nodeKeyBytes, nodeKey.SerializeCompressed()) |
| 41 | +} |
| 42 | + |
| 43 | +func TestFundingKey(t *testing.T) { |
| 44 | + fundingKey, _, err := DeriveKeyPair(hsmSecret, &keychain.KeyDescriptor{ |
| 45 | + PubKey: peerPubKey, |
| 46 | + KeyLocator: keychain.KeyLocator{ |
| 47 | + Family: keychain.KeyFamilyMultiSig, |
| 48 | + Index: 1, |
| 49 | + }, |
| 50 | + }) |
| 51 | + require.NoError(t, err) |
| 52 | + |
| 53 | + require.Equal( |
| 54 | + t, expectedFundingKeyBytes, fundingKey.SerializeCompressed(), |
| 55 | + ) |
| 56 | +} |
| 57 | + |
| 58 | +func TestPaymentBasePointSecret(t *testing.T) { |
| 59 | + hsmSecret2, _ := hex.DecodeString( |
| 60 | + "665b09e6fc86391f0141d957eb14ec30f8f8a58a876842792474cacc2448" + |
| 61 | + "9456", |
| 62 | + ) |
| 63 | + |
| 64 | + basePointPeerBytes, _ := hex.DecodeString( |
| 65 | + "0350aeef9f33a157953d3c3c1ef464bdf421204461959524e52e530c17f1" + |
| 66 | + "66f541", |
| 67 | + ) |
| 68 | + |
| 69 | + expectedPaymentBasePointBytes, _ := hex.DecodeString( |
| 70 | + "0339c93ca896829672510f8a4e51caef4b5f6a26f880acf5a120725a7f02" + |
| 71 | + "7b56b4", |
| 72 | + ) |
| 73 | + |
| 74 | + var hsmSecret [32]byte |
| 75 | + copy(hsmSecret[:], hsmSecret2) |
| 76 | + |
| 77 | + basepointPeer, err := btcec.ParsePubKey(basePointPeerBytes) |
| 78 | + require.NoError(t, err) |
| 79 | + |
| 80 | + nk, _, err := NodeKey(hsmSecret) |
| 81 | + require.NoError(t, err) |
| 82 | + |
| 83 | + t.Logf("Node key: %x", nk.SerializeCompressed()) |
| 84 | + |
| 85 | + fk, _, err := DeriveKeyPair(hsmSecret, &keychain.KeyDescriptor{ |
| 86 | + PubKey: basepointPeer, |
| 87 | + KeyLocator: keychain.KeyLocator{ |
| 88 | + Family: keychain.KeyFamilyMultiSig, |
| 89 | + Index: 1, |
| 90 | + }, |
| 91 | + }) |
| 92 | + require.NoError(t, err) |
| 93 | + |
| 94 | + t.Logf("Funding key: %x", fk.SerializeCompressed()) |
| 95 | + |
| 96 | + paymentBasePoint, _, err := DeriveKeyPair( |
| 97 | + hsmSecret, &keychain.KeyDescriptor{ |
| 98 | + PubKey: basepointPeer, |
| 99 | + KeyLocator: keychain.KeyLocator{ |
| 100 | + Family: keychain.KeyFamilyPaymentBase, |
| 101 | + Index: 1, |
| 102 | + }, |
| 103 | + }, |
| 104 | + ) |
| 105 | + require.NoError(t, err) |
| 106 | + |
| 107 | + require.Equal( |
| 108 | + t, expectedPaymentBasePointBytes, |
| 109 | + paymentBasePoint.SerializeCompressed(), |
| 110 | + ) |
| 111 | +} |
0 commit comments