lntemp+itest: refactor testNodeSignVerify

This commit is contained in:
yyforyongyu 2022-08-04 00:40:47 +08:00
parent 1938e42f4e
commit 4104a72b3a
No known key found for this signature in database
GPG key ID: 9BCD95C4FF296868
4 changed files with 54 additions and 56 deletions

View file

@ -355,3 +355,29 @@ func (h *HarnessRPC) GetTransactions() *lnrpc.TransactionDetails {
return resp
}
// SignMessage makes a RPC call to node's SignMessage and asserts.
func (h *HarnessRPC) SignMessage(msg []byte) *lnrpc.SignMessageResponse {
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
defer cancel()
req := &lnrpc.SignMessageRequest{Msg: msg}
resp, err := h.LN.SignMessage(ctxt, req)
require.NoErrorf(h, err, "SignMessage rpc call failed")
return resp
}
// VerifyMessage makes a RPC call to node's VerifyMessage and asserts.
func (h *HarnessRPC) VerifyMessage(msg []byte,
sig string) *lnrpc.VerifyMessageResponse {
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
defer cancel()
req := &lnrpc.VerifyMessageRequest{Msg: msg, Signature: sig}
resp, err := h.LN.VerifyMessage(ctxt, req)
require.NoErrorf(h, err, "VerifyMessage failed")
return resp
}

View file

@ -99,4 +99,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
Name: "reject onward htlc",
TestFunc: testRejectHTLC,
},
{
Name: "node sign verify",
TestFunc: testNodeSignVerify,
},
}

View file

@ -624,78 +624,50 @@ func testRejectHTLC(ht *lntemp.HarnessTest) {
ht.CloseChannel(carol, chanPointCarol)
}
func testNodeSignVerify(net *lntest.NetworkHarness, t *harnessTest) {
ctxb := context.Background()
// testNodeSignVerify checks that only connected nodes are allowed to perform
// signing and verifying messages.
func testNodeSignVerify(ht *lntemp.HarnessTest) {
chanAmt := funding.MaxBtcFundingAmount
pushAmt := btcutil.Amount(100000)
alice, bob := ht.Alice, ht.Bob
// Create a channel between alice and bob.
aliceBobCh := openChannelAndAssert(
t, net, net.Alice, net.Bob,
lntest.OpenChannelParams{
aliceBobCh := ht.OpenChannel(
alice, bob, lntemp.OpenChannelParams{
Amt: chanAmt,
PushAmt: pushAmt,
},
)
aliceMsg := []byte("alice msg")
// alice signs "alice msg" and sends her signature to bob.
sigReq := &lnrpc.SignMessageRequest{Msg: aliceMsg}
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
sigResp, err := net.Alice.SignMessage(ctxt, sigReq)
if err != nil {
t.Fatalf("SignMessage rpc call failed: %v", err)
}
aliceMsg := []byte("alice msg")
sigResp := alice.RPC.SignMessage(aliceMsg)
aliceSig := sigResp.Signature
// bob verifying alice's signature should succeed since alice and bob are
// connected.
verifyReq := &lnrpc.VerifyMessageRequest{Msg: aliceMsg, Signature: aliceSig}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
verifyResp, err := net.Bob.VerifyMessage(ctxt, verifyReq)
if err != nil {
t.Fatalf("VerifyMessage failed: %v", err)
}
if !verifyResp.Valid {
t.Fatalf("alice's signature didn't validate")
}
if verifyResp.Pubkey != net.Alice.PubKeyStr {
t.Fatalf("alice's signature doesn't contain alice's pubkey.")
}
// bob verifying alice's signature should succeed since alice and bob
// are connected.
verifyResp := bob.RPC.VerifyMessage(aliceMsg, aliceSig)
require.True(ht, verifyResp.Valid, "alice's signature didn't validate")
require.Equal(ht, verifyResp.Pubkey, alice.PubKeyStr,
"alice's signature doesn't contain alice's pubkey.")
// carol is a new node that is unconnected to alice or bob.
carol := net.NewNode(t.t, "Carol", nil)
defer shutdownAndAssert(net, t, carol)
carolMsg := []byte("carol msg")
carol := ht.NewNode("Carol", nil)
// carol signs "carol msg" and sends her signature to bob.
sigReq = &lnrpc.SignMessageRequest{Msg: carolMsg}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
sigResp, err = carol.SignMessage(ctxt, sigReq)
if err != nil {
t.Fatalf("SignMessage rpc call failed: %v", err)
}
carolMsg := []byte("carol msg")
sigResp = carol.RPC.SignMessage(carolMsg)
carolSig := sigResp.Signature
// bob verifying carol's signature should fail since they are not connected.
verifyReq = &lnrpc.VerifyMessageRequest{Msg: carolMsg, Signature: carolSig}
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
verifyResp, err = net.Bob.VerifyMessage(ctxt, verifyReq)
if err != nil {
t.Fatalf("VerifyMessage failed: %v", err)
}
if verifyResp.Valid {
t.Fatalf("carol's signature should not be valid")
}
if verifyResp.Pubkey != carol.PubKeyStr {
t.Fatalf("carol's signature doesn't contain her pubkey")
}
// bob verifying carol's signature should fail since they are not
// connected.
verifyResp = bob.RPC.VerifyMessage(carolMsg, carolSig)
require.False(ht, verifyResp.Valid, "carol's signature didn't validate")
require.Equal(ht, verifyResp.Pubkey, carol.PubKeyStr,
"carol's signature doesn't contain alice's pubkey.")
// Close the channel between alice and bob.
closeChannelAndAssert(t, net, net.Alice, aliceBobCh, false)
ht.CloseChannel(alice, aliceBobCh)
}
// testAbandonChannel abandones a channel and asserts that it is no

View file

@ -125,10 +125,6 @@ var allTestCases = []*testCase{
name: "node announcement",
test: testNodeAnnouncement,
},
{
name: "node sign verify",
test: testNodeSignVerify,
},
{
name: "derive shared key",
test: testDeriveSharedKey,