Skip to content

Commit fa6516e

Browse files
committed
proof: update parsing to return error if unknown even type encountered
1 parent 53904cc commit fa6516e

File tree

5 files changed

+122
-7
lines changed

5 files changed

+122
-7
lines changed

proof/meta.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242
// minted asset to express the decimal display of the minted asset.
4343
MetadataDecDisplayKey = "decimal_display"
4444

45-
// maxDecDisplay is the maximum value of decimal display that a user can
45+
// MaxDecDisplay is the maximum value of decimal display that a user can
4646
// define when minting assets. Since the uint64 max value has 19 decimal
4747
// places we will allow for a max of 12 decimal places.
4848
MaxDecDisplay = uint32(12)
@@ -101,7 +101,8 @@ type MetaReveal struct {
101101
Data []byte
102102
}
103103

104-
// A subset of Integer that excludes int8, since we never use it in practice.
104+
// SizableInteger is a subset of Integer that excludes int8, since we never use
105+
// it in practice.
105106
type SizableInteger interface {
106107
constraints.Unsigned | ~int | ~int16 | ~int32 | ~int64
107108
}
@@ -365,5 +366,9 @@ func (m *MetaReveal) Decode(r io.Reader) error {
365366
if err != nil {
366367
return err
367368
}
368-
return stream.Decode(r)
369+
370+
// Note, we can't use the DecodeP2P method here, because the meta data
371+
// itself can be larger than 65k bytes. But we impose limits in the
372+
// individual decoding functions.
373+
return asset.TlvStrictDecode(stream, r, KnownMetaRevealTypes)
369374
}

proof/proof.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ func (p *Proof) Decode(r io.Reader) error {
427427
// Note, we can't use the DecodeP2P method here, because the additional
428428
// inputs records might be larger than 64k each. Instead, we add
429429
// individual limits to each record.
430-
return stream.Decode(r)
430+
return asset.TlvStrictDecode(stream, r, KnownProofTypes)
431431
}
432432

433433
// Record returns a TLV record that can be used to encode/decode a Proof to/from

proof/proof_test.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -939,6 +939,31 @@ func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
939939
buf.Bytes(),
940940
)
941941

942+
// Make sure the proof in the test vectors doesn't use
943+
// a record type we haven't marked as known/supported
944+
// yet. If the following check fails, you need to update
945+
// the KnownProofTypes set.
946+
for _, record := range p.EncodeRecords() {
947+
require.Contains(
948+
tt, KnownProofTypes, record.Type(),
949+
)
950+
}
951+
952+
checkTaprootProofTypes(tt, p.InclusionProof)
953+
for i := range p.ExclusionProofs {
954+
checkTaprootProofTypes(tt, p.ExclusionProofs[i])
955+
}
956+
957+
if p.MetaReveal != nil {
958+
metaRecords := p.MetaReveal.EncodeRecords()
959+
for _, records := range metaRecords {
960+
require.Contains(
961+
tt, KnownMetaRevealTypes,
962+
records.Type(),
963+
)
964+
}
965+
}
966+
942967
// Create nice diff if things don't match.
943968
if !areEqual {
944969
expectedProof := &Proof{}
@@ -986,6 +1011,48 @@ func runBIPTestVector(t *testing.T, testVectors *TestVectors) {
9861011
}
9871012
}
9881013

1014+
// checkTaprootProofTypes ensures that the taproot proof contains only known
1015+
// TLV types.
1016+
func checkTaprootProofTypes(t *testing.T, p TaprootProof) {
1017+
for _, record := range p.EncodeRecords() {
1018+
require.Contains(t, KnownTaprootProofTypes, record.Type())
1019+
}
1020+
1021+
if p.CommitmentProof != nil {
1022+
for _, record := range p.CommitmentProof.EncodeRecords() {
1023+
require.Contains(
1024+
t, KnownCommitmentProofTypes, record.Type(),
1025+
)
1026+
1027+
tap := p.CommitmentProof.TaprootAssetProof
1028+
types := commitment.KnownTaprootAssetProofTypes
1029+
for _, record := range tap.Records() {
1030+
require.Contains(
1031+
t, types, record.Type(),
1032+
)
1033+
}
1034+
1035+
if p.CommitmentProof.AssetProof != nil {
1036+
ap := p.CommitmentProof.AssetProof
1037+
types := commitment.KnownAssetProofTypes
1038+
for _, record := range ap.Records() {
1039+
require.Contains(
1040+
t, types, record.Type(),
1041+
)
1042+
}
1043+
}
1044+
}
1045+
}
1046+
1047+
if p.TapscriptProof != nil {
1048+
for _, record := range p.TapscriptProof.EncodeRecords() {
1049+
require.Contains(
1050+
t, KnownTapscriptProofTypes, record.Type(),
1051+
)
1052+
}
1053+
}
1054+
}
1055+
9891056
func init() {
9901057
logWriter := build.NewRotatingLogWriter()
9911058
logger := logWriter.GenSubLogger(Subsystem, func() {})

proof/records.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/btcsuite/btcd/wire"
88
"github.com/lightninglabs/taproot-assets/asset"
99
"github.com/lightninglabs/taproot-assets/commitment"
10+
"github.com/lightninglabs/taproot-assets/fn"
1011
"github.com/lightningnetwork/lnd/tlv"
1112
)
1213

@@ -45,6 +46,45 @@ const (
4546
MetaRevealDataType tlv.Type = 2
4647
)
4748

49+
// KnownProofTypes is a set of all known proof TLV types. This set is asserted
50+
// to be complete by a check in the BIP test vector unit tests.
51+
var KnownProofTypes = fn.NewSet(
52+
VersionType, PrevOutType, BlockHeaderType, AnchorTxType,
53+
TxMerkleProofType, AssetLeafType, InclusionProofType,
54+
ExclusionProofsType, SplitRootProofType, MetaRevealType,
55+
AdditionalInputsType, ChallengeWitnessType, BlockHeightType,
56+
GenesisRevealType, GroupKeyRevealType,
57+
)
58+
59+
// KnownTaprootProofTypes is a set of all known Taproot proof TLV types. This
60+
// set is asserted to be complete by a check in the BIP test vector unit tests.
61+
var KnownTaprootProofTypes = fn.NewSet(
62+
TaprootProofOutputIndexType, TaprootProofInternalKeyType,
63+
TaprootProofCommitmentProofType, TaprootProofTapscriptProofType,
64+
)
65+
66+
// KnownCommitmentProofTypes is a set of all known commitment proof TLV types.
67+
// This set is asserted to be complete by a check in the BIP test vector unit
68+
// tests.
69+
var KnownCommitmentProofTypes = fn.NewSet(
70+
commitment.ProofAssetProofType, commitment.ProofTaprootAssetProofType,
71+
CommitmentProofTapSiblingPreimageType,
72+
)
73+
74+
// KnownTapscriptProofTypes is a set of all known Tapscript proof TLV types.
75+
// This set is asserted to be complete by a check in the BIP test vector unit
76+
// tests.
77+
var KnownTapscriptProofTypes = fn.NewSet(
78+
TapscriptProofTapPreimage1, TapscriptProofTapPreimage2,
79+
TapscriptProofBip86,
80+
)
81+
82+
// KnownMetaRevealTypes is a set of all known meta reveal TLV types. This set is
83+
// asserted to be complete by a check in the BIP test vector unit tests.
84+
var KnownMetaRevealTypes = fn.NewSet(
85+
MetaRevealEncodingType, MetaRevealDataType,
86+
)
87+
4888
func VersionRecord(version *TransitionVersion) tlv.Record {
4989
return tlv.MakeStaticRecord(
5090
VersionType, version, 4, VersionEncoder, VersionDecoder,

proof/taproot.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ func (p *CommitmentProof) Decode(r io.Reader) error {
7474
if err != nil {
7575
return err
7676
}
77-
return stream.Decode(r)
77+
78+
return asset.TlvStrictDecodeP2P(stream, r, KnownCommitmentProofTypes)
7879
}
7980

8081
// TapscriptProof represents a proof of a Taproot output not including a
@@ -139,7 +140,8 @@ func (p *TapscriptProof) Decode(r io.Reader) error {
139140
if err != nil {
140141
return err
141142
}
142-
return stream.Decode(r)
143+
144+
return asset.TlvStrictDecodeP2P(stream, r, KnownTapscriptProofTypes)
143145
}
144146

145147
// TaprootProof represents a proof that reveals the partial contents to a
@@ -208,7 +210,8 @@ func (p *TaprootProof) Decode(r io.Reader) error {
208210
if err != nil {
209211
return err
210212
}
211-
return stream.DecodeP2P(r)
213+
214+
return asset.TlvStrictDecodeP2P(stream, r, KnownTaprootProofTypes)
212215
}
213216

214217
// deriveTaprootKey derives the taproot key backing a Taproot Asset commitment.

0 commit comments

Comments
 (0)