From 28bceb23c82fcab8dc1d2fd15b72e1b630449e51 Mon Sep 17 00:00:00 2001 From: ziggie Date: Sat, 3 Dec 2022 16:35:04 +0100 Subject: [PATCH] walletrpc: add test sign/verify methods Test the introduced methods sign/verify messages for single addresses --- lntemp/rpc/wallet_kit.go | 32 ++++++++++ lntest/itest/list_on_test.go | 4 ++ lntest/itest/lnd_misc_test.go | 117 ++++++++++++++++++++++++++++++++++ 3 files changed, 153 insertions(+) diff --git a/lntemp/rpc/wallet_kit.go b/lntemp/rpc/wallet_kit.go index 402f30681..1d0a12858 100644 --- a/lntemp/rpc/wallet_kit.go +++ b/lntemp/rpc/wallet_kit.go @@ -12,6 +12,11 @@ import ( // WalletKitClient related RPCs. // ===================== +type ( + SignReq *walletrpc.SignMessageWithAddrResponse + VerifyResp *walletrpc.VerifyMessageWithAddrResponse +) + // FinalizePsbt makes a RPC call to node's ListUnspent and asserts. func (h *HarnessRPC) ListUnspent( req *walletrpc.ListUnspentRequest) *walletrpc.ListUnspentResponse { @@ -125,6 +130,33 @@ func (h *HarnessRPC) ListAddresses( return key } +// SignMessageWithAddr makes a RPC call to the SignMessageWithAddr and asserts. +func (h *HarnessRPC) SignMessageWithAddr( + req *walletrpc.SignMessageWithAddrRequest) SignReq { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + key, err := h.WalletKit.SignMessageWithAddr(ctxt, req) + h.NoError(err, "SignMessageWithAddr") + + return key +} + +// VerifyMessageWithAddr makes a RPC call to +// the VerifyMessageWithAddr and asserts. +func (h *HarnessRPC) VerifyMessageWithAddr( + req *walletrpc.VerifyMessageWithAddrRequest) VerifyResp { + + ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) + defer cancel() + + key, err := h.WalletKit.VerifyMessageWithAddr(ctxt, req) + h.NoError(err, "VerifyMessageWithAddr") + + return key +} + // ListSweeps makes a ListSweeps RPC call to the node's WalletKit client. func (h *HarnessRPC) ListSweeps(verbose bool) *walletrpc.ListSweepsResponse { ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout) diff --git a/lntest/itest/list_on_test.go b/lntest/itest/list_on_test.go index 3bd12f9d3..14d8b02d7 100644 --- a/lntest/itest/list_on_test.go +++ b/lntest/itest/list_on_test.go @@ -501,4 +501,8 @@ var allTestCasesTemp = []*lntemp.TestCase{ Name: "custom message", TestFunc: testCustomMessage, }, + { + Name: "sign verify message with addr", + TestFunc: testSignVerifyMessageWithAddr, + }, } diff --git a/lntest/itest/lnd_misc_test.go b/lntest/itest/lnd_misc_test.go index 9d80c9b3e..04274fb35 100644 --- a/lntest/itest/lnd_misc_test.go +++ b/lntest/itest/lnd_misc_test.go @@ -1073,3 +1073,120 @@ func assertChannelConstraintsEqual(ht *lntemp.HarnessTest, require.Equal(ht, want.MaxAcceptedHtlcs, got.MaxAcceptedHtlcs, "MaxAcceptedHtlcs mismatched") } + +// testSignVerifyMessageWithAddr tests signing and also verifying a signature +// on a message with a provided address. +func testSignVerifyMessageWithAddr(ht *lntemp.HarnessTest) { + // Using different nodes to sign the message and verify the signature. + alice, bob := ht.Alice, ht.Bob + + // Test an lnd wallet created P2WKH address. + respAddr := alice.RPC.NewAddress(&lnrpc.NewAddressRequest{ + Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH, + }) + + aliceMsg := []byte("alice msg") + + respSig := alice.RPC.SignMessageWithAddr( + &walletrpc.SignMessageWithAddrRequest{ + Msg: aliceMsg, + Addr: respAddr.Address, + }, + ) + + respValid := bob.RPC.VerifyMessageWithAddr( + &walletrpc.VerifyMessageWithAddrRequest{ + Msg: aliceMsg, + Signature: respSig.Signature, + Addr: respAddr.Address, + }, + ) + + require.True(ht, respValid.Valid, "alice's signature didn't validate") + + // Test an lnd wallet created NP2WKH address. + respAddr = alice.RPC.NewAddress(&lnrpc.NewAddressRequest{ + Type: lnrpc.AddressType_NESTED_PUBKEY_HASH, + }) + + respSig = alice.RPC.SignMessageWithAddr( + &walletrpc.SignMessageWithAddrRequest{ + Msg: aliceMsg, + Addr: respAddr.Address, + }, + ) + + respValid = bob.RPC.VerifyMessageWithAddr( + &walletrpc.VerifyMessageWithAddrRequest{ + Msg: aliceMsg, + Signature: respSig.Signature, + Addr: respAddr.Address, + }, + ) + + require.True(ht, respValid.Valid, "alice's signature didn't validate") + + // Test an lnd wallet created P2TR address. + respAddr = alice.RPC.NewAddress(&lnrpc.NewAddressRequest{ + Type: lnrpc.AddressType_TAPROOT_PUBKEY, + }) + + respSig = alice.RPC.SignMessageWithAddr( + &walletrpc.SignMessageWithAddrRequest{ + Msg: aliceMsg, + Addr: respAddr.Address, + }, + ) + + respValid = bob.RPC.VerifyMessageWithAddr( + &walletrpc.VerifyMessageWithAddrRequest{ + Msg: aliceMsg, + Signature: respSig.Signature, + Addr: respAddr.Address, + }, + ) + + require.True(ht, respValid.Valid, "alice's signature didn't validate") + + // Test verifying a signature with an external P2PKH address. + // P2PKH address type is not supported by the lnd wallet therefore + // using an external source (bitcoin-core) for address and + // signature creation. + externalMsg := []byte("external msg") + externalAddr := "msS5c4VihSiJ64QzvMMEmWh6rYBnuWo2xH" + + // Base64 encoded signature created with bitcoin-core regtest. + externalSig := "H5DqqM7Cc8xZnYBr7j3gD4XD+AuQsim9Un/IxBrrhBA7I9//" + + "3exuQRg+u7HpwG65yobPsew6RMUteyuxyNkLF5E=" + + respValid = alice.RPC.VerifyMessageWithAddr( + &walletrpc.VerifyMessageWithAddrRequest{ + Msg: externalMsg, + Signature: externalSig, + Addr: externalAddr, + }, + ) + + require.True(ht, respValid.Valid, "external signature didn't validate") + + // Test verifying a signature with a different address which + // initially was used to create the following signature. + // externalAddr is a valid legacy P2PKH bitcoin address created + // with bitcoin-core. + externalAddr = "mugbg8CqFe9CbdrYjFTkMhmL3JxuEXkNbY" + + // Base64 encoded signature created with bitcoin-core regtest but with + // the address msS5c4VihSiJ64QzvMMEmWh6rYBnuWo2xH. + externalSig = "H5DqqM7Cc8xZnYBr7j3gD4XD+AuQsim9Un/IxBrrhBA7I9//" + + "3exuQRg+u7HpwG65yobPsew6RMUteyuxyNkLF5E=" + + respValid = alice.RPC.VerifyMessageWithAddr( + &walletrpc.VerifyMessageWithAddrRequest{ + Msg: externalMsg, + Signature: externalSig, + Addr: externalAddr, + }, + ) + + require.False(ht, respValid.Valid, "external signature did validate") +}