Skip to content

Commit a013976

Browse files
committed
itest: add integration test for BIP-0322 signatures
1 parent 4055aba commit a013976

2 files changed

Lines changed: 107 additions & 66 deletions

File tree

itest/lnd_misc_test.go

Lines changed: 35 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,73 +1058,42 @@ func testSignVerifyMessageWithAddr(ht *lntest.HarnessTest) {
10581058
alice := ht.NewNode("Alice,", nil)
10591059
bob := ht.NewNode("Bob,", nil)
10601060

1061-
// Test an lnd wallet created P2WKH address.
1062-
respAddr := alice.RPC.NewAddress(&lnrpc.NewAddressRequest{
1063-
Type: lnrpc.AddressType_WITNESS_PUBKEY_HASH,
1064-
})
1065-
10661061
aliceMsg := []byte("alice msg")
1062+
addrTypes := []lnrpc.AddressType{
1063+
lnrpc.AddressType_WITNESS_PUBKEY_HASH,
1064+
lnrpc.AddressType_NESTED_PUBKEY_HASH,
1065+
lnrpc.AddressType_TAPROOT_PUBKEY,
1066+
}
10671067

1068-
respSig := alice.RPC.SignMessageWithAddr(
1069-
&walletrpc.SignMessageWithAddrRequest{
1070-
Msg: aliceMsg,
1071-
Addr: respAddr.Address,
1072-
},
1073-
)
1074-
1075-
respValid := bob.RPC.VerifyMessageWithAddr(
1076-
&walletrpc.VerifyMessageWithAddrRequest{
1077-
Msg: aliceMsg,
1078-
Signature: respSig.Signature,
1079-
Addr: respAddr.Address,
1080-
},
1081-
)
1082-
1083-
require.True(ht, respValid.Valid, "alice's signature didn't validate")
1084-
1085-
// Test an lnd wallet created NP2WKH address.
1086-
respAddr = alice.RPC.NewAddress(&lnrpc.NewAddressRequest{
1087-
Type: lnrpc.AddressType_NESTED_PUBKEY_HASH,
1088-
})
1089-
1090-
respSig = alice.RPC.SignMessageWithAddr(
1091-
&walletrpc.SignMessageWithAddrRequest{
1092-
Msg: aliceMsg,
1093-
Addr: respAddr.Address,
1094-
},
1095-
)
1096-
1097-
respValid = bob.RPC.VerifyMessageWithAddr(
1098-
&walletrpc.VerifyMessageWithAddrRequest{
1099-
Msg: aliceMsg,
1100-
Signature: respSig.Signature,
1101-
Addr: respAddr.Address,
1102-
},
1103-
)
1104-
1105-
require.True(ht, respValid.Valid, "alice's signature didn't validate")
1106-
1107-
// Test an lnd wallet created P2TR address.
1108-
respAddr = alice.RPC.NewAddress(&lnrpc.NewAddressRequest{
1109-
Type: lnrpc.AddressType_TAPROOT_PUBKEY,
1110-
})
1111-
1112-
respSig = alice.RPC.SignMessageWithAddr(
1113-
&walletrpc.SignMessageWithAddrRequest{
1114-
Msg: aliceMsg,
1115-
Addr: respAddr.Address,
1116-
},
1117-
)
1118-
1119-
respValid = bob.RPC.VerifyMessageWithAddr(
1120-
&walletrpc.VerifyMessageWithAddrRequest{
1121-
Msg: aliceMsg,
1122-
Signature: respSig.Signature,
1123-
Addr: respAddr.Address,
1124-
},
1125-
)
1126-
1127-
require.True(ht, respValid.Valid, "alice's signature didn't validate")
1068+
for _, bip322 := range []bool{true, false} {
1069+
for _, addrType := range addrTypes {
1070+
respAddr := alice.RPC.NewAddress(
1071+
&lnrpc.NewAddressRequest{
1072+
Type: addrType,
1073+
},
1074+
)
1075+
1076+
respSig := alice.RPC.SignMessageWithAddr(
1077+
&walletrpc.SignMessageWithAddrRequest{
1078+
Msg: aliceMsg,
1079+
Addr: respAddr.Address,
1080+
Bip322: bip322,
1081+
},
1082+
)
1083+
respValid := bob.RPC.VerifyMessageWithAddr(
1084+
&walletrpc.VerifyMessageWithAddrRequest{
1085+
Msg: aliceMsg,
1086+
Signature: respSig.Signature,
1087+
Addr: respAddr.Address,
1088+
},
1089+
)
1090+
require.True(
1091+
ht, respValid.Valid, "alice's signature did "+
1092+
"not validate, bip322=%v, addrType=%v",
1093+
bip322, addrType,
1094+
)
1095+
}
1096+
}
11281097

11291098
// Test verifying a signature with an external P2PKH address.
11301099
// P2PKH address type is not supported by the lnd wallet therefore
@@ -1137,7 +1106,7 @@ func testSignVerifyMessageWithAddr(ht *lntest.HarnessTest) {
11371106
externalSig := "H5DqqM7Cc8xZnYBr7j3gD4XD+AuQsim9Un/IxBrrhBA7I9//" +
11381107
"3exuQRg+u7HpwG65yobPsew6RMUteyuxyNkLF5E="
11391108

1140-
respValid = alice.RPC.VerifyMessageWithAddr(
1109+
respValid := alice.RPC.VerifyMessageWithAddr(
11411110
&walletrpc.VerifyMessageWithAddrRequest{
11421111
Msg: externalMsg,
11431112
Signature: externalSig,

itest/lnd_psbt_test.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"time"
88

99
"github.com/btcsuite/btcd/address/v2"
10+
"github.com/btcsuite/btcd/bip322"
1011
"github.com/btcsuite/btcd/btcec/v2"
1112
"github.com/btcsuite/btcd/btcec/v2/ecdsa"
1213
"github.com/btcsuite/btcd/btcec/v2/schnorr"
@@ -663,6 +664,10 @@ func testSignPsbt(ht *lntest.HarnessTest) {
663664
name: "fund and sign psbt",
664665
runner: runFundAndSignPsbt,
665666
},
667+
{
668+
name: "sign bip322 psbt",
669+
runner: runSignBip322Psbt,
670+
},
666671
}
667672

668673
for _, tc := range psbtTestRunners {
@@ -1055,6 +1060,73 @@ func runFundAndSignPsbt(ht *lntest.HarnessTest, alice *node.HarnessNode) {
10551060
}
10561061
}
10571062

1063+
// runSignBip322Psbt makes sure we can sign PSBTs that were created to sign a
1064+
// BIP322 generic message.
1065+
func runSignBip322Psbt(ht *lntest.HarnessTest, alice *node.HarnessNode) {
1066+
alice.AddToLogf("================ runSignBip322Psbt ===============")
1067+
1068+
// Derive a P2TR address from Alice's wallet.
1069+
p2trAddrResp := alice.RPC.NewAddress(&lnrpc.NewAddressRequest{
1070+
Type: lnrpc.AddressType_TAPROOT_PUBKEY,
1071+
})
1072+
p2trAddr, err := address.DecodeAddress(
1073+
p2trAddrResp.Address, harnessNetParams,
1074+
)
1075+
require.NoError(ht, err)
1076+
1077+
p2trPkScript, err := txscript.PayToAddrScript(p2trAddr)
1078+
require.NoError(ht, err)
1079+
1080+
// Create the BIP322 PSBT packet.
1081+
message := []byte("generic message signing is cool")
1082+
pkt, err := bip322.BuildToSignPacketSimple(message, p2trPkScript)
1083+
require.NoError(ht, err)
1084+
1085+
// The SignPsbt RPC needs to know the exact derivation path for the
1086+
// address, otherwise it'll ignore it.
1087+
b32, b32tr := getAddressBip32Derivation(ht, p2trAddrResp.Address, alice)
1088+
pkt.Inputs[0].Bip32Derivation = []*psbt.Bip32Derivation{b32}
1089+
pkt.Inputs[0].TaprootBip32Derivation = []*psbt.TaprootBip32Derivation{
1090+
b32tr,
1091+
}
1092+
1093+
// Create the signature using the SignPsbt RPC and check that we get
1094+
// exactly one signature back.
1095+
var pktBytes bytes.Buffer
1096+
err = pkt.Serialize(&pktBytes)
1097+
require.NoError(ht, err)
1098+
1099+
signResp := alice.RPC.SignPsbt(&walletrpc.SignPsbtRequest{
1100+
FundedPsbt: pktBytes.Bytes(),
1101+
})
1102+
require.Len(ht, signResp.SignedInputs, 1)
1103+
require.Equal(ht, signResp.SignedInputs[0], uint32(0))
1104+
1105+
signedPkt, err := psbt.NewFromRawBytes(
1106+
bytes.NewReader(signResp.SignedPsbt), false,
1107+
)
1108+
require.NoError(ht, err)
1109+
require.NotEmpty(ht, signedPkt.Inputs[0].TaprootKeySpendSig)
1110+
1111+
// Serialize and verify the signature.
1112+
sig, err := bip322.SerializeSignature(signedPkt)
1113+
require.NoError(ht, err)
1114+
1115+
respValid := alice.RPC.VerifyMessageWithAddr(
1116+
&walletrpc.VerifyMessageWithAddrRequest{
1117+
Msg: message,
1118+
Signature: sig,
1119+
Addr: p2trAddrResp.Address,
1120+
},
1121+
)
1122+
require.True(ht, respValid.Valid)
1123+
require.Equal(
1124+
ht,
1125+
walletrpc.MsgSignatureType_MSG_SIGNATURE_TYPE_BIP0322_SIMPLE,
1126+
respValid.SignatureType,
1127+
)
1128+
}
1129+
10581130
// testFundPsbt tests the FundPsbt RPC use cases that aren't covered by the PSBT
10591131
// channel funding tests above. These specifically are the use cases of funding
10601132
// a PSBT that already specifies an input but where the user still wants the

0 commit comments

Comments
 (0)