lntemp+itest: refactor testNodeSignVerify
This commit is contained in:
4 files changed
+54
-56
No files matched your search
@@ -355,3 +355,29 @@ func (h *HarnessRPC) GetTransactions() *lnrpc.TransactionDetails {
|
|||||||
|
|
||||||
return resp
|
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
|
||||||
|
}
|
||||||
@@ -99,4 +99,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
|||||||
Name: "reject onward htlc",
|
Name: "reject onward htlc",
|
||||||
TestFunc: testRejectHTLC,
|
TestFunc: testRejectHTLC,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "node sign verify",
|
||||||
|
TestFunc: testNodeSignVerify,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
@@ -624,78 +624,50 @@ func testRejectHTLC(ht *lntemp.HarnessTest) {
|
|||||||
ht.CloseChannel(carol, chanPointCarol)
|
ht.CloseChannel(carol, chanPointCarol)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testNodeSignVerify(net *lntest.NetworkHarness, t *harnessTest) {
|
// testNodeSignVerify checks that only connected nodes are allowed to perform
|
||||||
ctxb := context.Background()
|
// signing and verifying messages.
|
||||||
|
func testNodeSignVerify(ht *lntemp.HarnessTest) {
|
||||||
chanAmt := funding.MaxBtcFundingAmount
|
chanAmt := funding.MaxBtcFundingAmount
|
||||||
pushAmt := btcutil.Amount(100000)
|
pushAmt := btcutil.Amount(100000)
|
||||||
|
alice, bob := ht.Alice, ht.Bob
|
||||||
|
|
||||||
// Create a channel between alice and bob.
|
// Create a channel between alice and bob.
|
||||||
aliceBobCh := openChannelAndAssert(
|
aliceBobCh := ht.OpenChannel(
|
||||||
t, net, net.Alice, net.Bob,
|
alice, bob, lntemp.OpenChannelParams{
|
||||||
lntest.OpenChannelParams{
|
|
||||||
Amt: chanAmt,
|
Amt: chanAmt,
|
||||||
PushAmt: pushAmt,
|
PushAmt: pushAmt,
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
aliceMsg := []byte("alice msg")
|
|
||||||
|
|
||||||
// alice signs "alice msg" and sends her signature to bob.
|
// alice signs "alice msg" and sends her signature to bob.
|
||||||
sigReq := &lnrpc.SignMessageRequest{Msg: aliceMsg}
|
aliceMsg := []byte("alice msg")
|
||||||
ctxt, _ := context.WithTimeout(ctxb, defaultTimeout)
|
sigResp := alice.RPC.SignMessage(aliceMsg)
|
||||||
sigResp, err := net.Alice.SignMessage(ctxt, sigReq)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("SignMessage rpc call failed: %v", err)
|
|
||||||
}
|
|
||||||
aliceSig := sigResp.Signature
|
aliceSig := sigResp.Signature
|
||||||
|
|
||||||
// bob verifying alice's signature should succeed since alice and bob are
|
// bob verifying alice's signature should succeed since alice and bob
|
||||||
// connected.
|
// are connected.
|
||||||
verifyReq := &lnrpc.VerifyMessageRequest{Msg: aliceMsg, Signature: aliceSig}
|
verifyResp := bob.RPC.VerifyMessage(aliceMsg, aliceSig)
|
||||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
require.True(ht, verifyResp.Valid, "alice's signature didn't validate")
|
||||||
verifyResp, err := net.Bob.VerifyMessage(ctxt, verifyReq)
|
require.Equal(ht, verifyResp.Pubkey, alice.PubKeyStr,
|
||||||
if err != nil {
|
"alice's signature doesn't contain alice's pubkey.")
|
||||||
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.")
|
|
||||||
}
|
|
||||||
|
|
||||||
// carol is a new node that is unconnected to alice or bob.
|
// carol is a new node that is unconnected to alice or bob.
|
||||||
carol := net.NewNode(t.t, "Carol", nil)
|
carol := ht.NewNode("Carol", nil)
|
||||||
defer shutdownAndAssert(net, t, carol)
|
|
||||||
|
|
||||||
carolMsg := []byte("carol msg")
|
|
||||||
|
|
||||||
// carol signs "carol msg" and sends her signature to bob.
|
// carol signs "carol msg" and sends her signature to bob.
|
||||||
sigReq = &lnrpc.SignMessageRequest{Msg: carolMsg}
|
carolMsg := []byte("carol msg")
|
||||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
sigResp = carol.RPC.SignMessage(carolMsg)
|
||||||
sigResp, err = carol.SignMessage(ctxt, sigReq)
|
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("SignMessage rpc call failed: %v", err)
|
|
||||||
}
|
|
||||||
carolSig := sigResp.Signature
|
carolSig := sigResp.Signature
|
||||||
|
|
||||||
// bob verifying carol's signature should fail since they are not connected.
|
// bob verifying carol's signature should fail since they are not
|
||||||
verifyReq = &lnrpc.VerifyMessageRequest{Msg: carolMsg, Signature: carolSig}
|
// connected.
|
||||||
ctxt, _ = context.WithTimeout(ctxb, defaultTimeout)
|
verifyResp = bob.RPC.VerifyMessage(carolMsg, carolSig)
|
||||||
verifyResp, err = net.Bob.VerifyMessage(ctxt, verifyReq)
|
require.False(ht, verifyResp.Valid, "carol's signature didn't validate")
|
||||||
if err != nil {
|
require.Equal(ht, verifyResp.Pubkey, carol.PubKeyStr,
|
||||||
t.Fatalf("VerifyMessage failed: %v", err)
|
"carol's signature doesn't contain alice's pubkey.")
|
||||||
}
|
|
||||||
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")
|
|
||||||
}
|
|
||||||
|
|
||||||
// Close the channel between alice and bob.
|
// 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
|
// testAbandonChannel abandones a channel and asserts that it is no
|
||||||
|
|||||||
@@ -125,10 +125,6 @@ var allTestCases = []*testCase{
|
|||||||
name: "node announcement",
|
name: "node announcement",
|
||||||
test: testNodeAnnouncement,
|
test: testNodeAnnouncement,
|
||||||
},
|
},
|
||||||
{
|
|
||||||
name: "node sign verify",
|
|
||||||
test: testNodeSignVerify,
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: "derive shared key",
|
name: "derive shared key",
|
||||||
test: testDeriveSharedKey,
|
test: testDeriveSharedKey,
|
||||||
|
|||||||
Reference in new issue
Block a user