mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-03-04 09:48:19 +01:00
walletrpc: add test sign/verify methods
Test the introduced methods sign/verify messages for single addresses
This commit is contained in:
parent
7b68289a7a
commit
28bceb23c8
3 changed files with 153 additions and 0 deletions
|
@ -12,6 +12,11 @@ import (
|
||||||
// WalletKitClient related RPCs.
|
// WalletKitClient related RPCs.
|
||||||
// =====================
|
// =====================
|
||||||
|
|
||||||
|
type (
|
||||||
|
SignReq *walletrpc.SignMessageWithAddrResponse
|
||||||
|
VerifyResp *walletrpc.VerifyMessageWithAddrResponse
|
||||||
|
)
|
||||||
|
|
||||||
// FinalizePsbt makes a RPC call to node's ListUnspent and asserts.
|
// FinalizePsbt makes a RPC call to node's ListUnspent and asserts.
|
||||||
func (h *HarnessRPC) ListUnspent(
|
func (h *HarnessRPC) ListUnspent(
|
||||||
req *walletrpc.ListUnspentRequest) *walletrpc.ListUnspentResponse {
|
req *walletrpc.ListUnspentRequest) *walletrpc.ListUnspentResponse {
|
||||||
|
@ -125,6 +130,33 @@ func (h *HarnessRPC) ListAddresses(
|
||||||
return key
|
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.
|
// ListSweeps makes a ListSweeps RPC call to the node's WalletKit client.
|
||||||
func (h *HarnessRPC) ListSweeps(verbose bool) *walletrpc.ListSweepsResponse {
|
func (h *HarnessRPC) ListSweeps(verbose bool) *walletrpc.ListSweepsResponse {
|
||||||
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
|
ctxt, cancel := context.WithTimeout(h.runCtx, DefaultTimeout)
|
||||||
|
|
|
@ -501,4 +501,8 @@ var allTestCasesTemp = []*lntemp.TestCase{
|
||||||
Name: "custom message",
|
Name: "custom message",
|
||||||
TestFunc: testCustomMessage,
|
TestFunc: testCustomMessage,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
Name: "sign verify message with addr",
|
||||||
|
TestFunc: testSignVerifyMessageWithAddr,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
|
@ -1073,3 +1073,120 @@ func assertChannelConstraintsEqual(ht *lntemp.HarnessTest,
|
||||||
require.Equal(ht, want.MaxAcceptedHtlcs, got.MaxAcceptedHtlcs,
|
require.Equal(ht, want.MaxAcceptedHtlcs, got.MaxAcceptedHtlcs,
|
||||||
"MaxAcceptedHtlcs mismatched")
|
"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")
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue