Skip to content

Commit e502363

Browse files
gijswijsguggero
authored andcommitted
multi: add new proof version TransitionV1
The proof version is bumped to indicate the usage and support of stxo style proofs.
1 parent 4c76b64 commit e502363

File tree

7 files changed

+101
-13
lines changed

7 files changed

+101
-13
lines changed

proof/append.go

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,33 @@ import (
1111
"github.com/lightninglabs/taproot-assets/commitment"
1212
)
1313

14+
// GenConfig is a struct that holds the configuration for creating Taproot Asset
15+
// proofs.
16+
type GenConfig struct {
17+
// transitionVersion is the version of the asset state transition proof
18+
// that is going to be used.
19+
transitionVersion TransitionVersion
20+
}
21+
22+
// DefaultGenConfig returns a default proof generation configuration.
23+
func DefaultGenConfig() GenConfig {
24+
return GenConfig{
25+
transitionVersion: TransitionV0,
26+
}
27+
}
28+
29+
// GenOption is a function type that can be used to modify the proof generation
30+
// configuration.
31+
type GenOption func(*GenConfig)
32+
33+
// WithVersion is an option that can be used to create a transition proof of the
34+
// given version.
35+
func WithVersion(v TransitionVersion) GenOption {
36+
return func(cfg *GenConfig) {
37+
cfg.transitionVersion = v
38+
}
39+
}
40+
1441
// TransitionParams holds the set of chain level information needed to append a
1542
// proof to an existing file for the given asset state transition.
1643
type TransitionParams struct {
@@ -42,8 +69,8 @@ type TransitionParams struct {
4269
// on-chain output, this function takes the script key of the asset to return
4370
// the proof for. This method returns both the encoded full provenance (proof
4471
// chain) and the added latest proof.
45-
func AppendTransition(blob Blob, params *TransitionParams,
46-
vCtx VerifierCtx) (Blob, *Proof, error) {
72+
func AppendTransition(blob Blob, params *TransitionParams, vCtx VerifierCtx,
73+
opts ...GenOption) (Blob, *Proof, error) {
4774

4875
// Decode the proof blob into a proper file structure first.
4976
f := NewEmptyFile(V0)
@@ -69,7 +96,7 @@ func AppendTransition(blob Blob, params *TransitionParams,
6996
}
7097

7198
// We can now create the new proof entry for the asset in the params.
72-
newProof, err := CreateTransitionProof(lastPrevOut, params)
99+
newProof, err := CreateTransitionProof(lastPrevOut, params, opts...)
73100
if err != nil {
74101
return nil, nil, fmt.Errorf("error creating transition "+
75102
"proof: %w", err)
@@ -122,10 +149,17 @@ func (p *Proof) UpdateTransitionProof(params *BaseProofParams) error {
122149

123150
// CreateTransitionProof creates a proof for an asset transition, based on the
124151
// last proof of the last asset state and the new asset in the params.
125-
func CreateTransitionProof(prevOut wire.OutPoint,
126-
params *TransitionParams) (*Proof, error) {
152+
func CreateTransitionProof(prevOut wire.OutPoint, params *TransitionParams,
153+
opts ...GenOption) (*Proof, error) {
127154

128-
proof, err := baseProof(&params.BaseProofParams, prevOut)
155+
cfg := DefaultGenConfig()
156+
for _, opt := range opts {
157+
opt(&cfg)
158+
}
159+
160+
proof, err := baseProof(
161+
&params.BaseProofParams, prevOut, cfg.transitionVersion,
162+
)
129163
if err != nil {
130164
return nil, fmt.Errorf("error creating base proofs: %w", err)
131165
}

proof/mint.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,13 +212,15 @@ type MintingBlobOption func(*mintingBlobOpts)
212212
type mintingBlobOpts struct {
213213
metaReveals map[asset.SerializedKey]*MetaReveal
214214
tapSiblingPreimage *commitment.TapscriptPreimage
215+
genConfig GenConfig
215216
}
216217

217218
// defaultMintingBlobOpts returns the default set of options for creating a
218219
// minting blob.
219220
func defaultMintingBlobOpts() *mintingBlobOpts {
220221
return &mintingBlobOpts{
221222
metaReveals: make(map[asset.SerializedKey]*MetaReveal),
223+
genConfig: DefaultGenConfig(),
222224
}
223225
}
224226

@@ -243,6 +245,14 @@ func WithSiblingPreimage(
243245
}
244246
}
245247

248+
// WithGenOption is a MintingBlobOption that allows the caller to modify the
249+
// proof generation configuration used to create the minting blob.
250+
func WithGenOption(genOpts GenOption) MintingBlobOption {
251+
return func(o *mintingBlobOpts) {
252+
genOpts(&o.genConfig)
253+
}
254+
}
255+
246256
// NewMintingBlobs takes a set of minting parameters, and produces a series of
247257
// serialized proof files, which proves the creation/existence of each of the
248258
// assets within the batch.
@@ -254,7 +264,10 @@ func NewMintingBlobs(params *MintParams, vCtx VerifierCtx,
254264
blobOpt(opts)
255265
}
256266

257-
base, err := baseProof(&params.BaseProofParams, params.GenesisPoint)
267+
base, err := baseProof(
268+
&params.BaseProofParams, params.GenesisPoint,
269+
opts.genConfig.transitionVersion,
270+
)
258271
if err != nil {
259272
return nil, err
260273
}
@@ -289,7 +302,9 @@ func NewMintingBlobs(params *MintParams, vCtx VerifierCtx,
289302

290303
// baseProof creates the basic proof template that contains all anchor
291304
// transaction related fields.
292-
func baseProof(params *BaseProofParams, prevOut wire.OutPoint) (*Proof, error) {
305+
func baseProof(params *BaseProofParams, prevOut wire.OutPoint,
306+
version TransitionVersion) (*Proof, error) {
307+
293308
// First, we'll create the merkle proof for the anchor transaction. In
294309
// this case, since all the assets were created in the same block, we
295310
// only need a single merkle proof.
@@ -306,6 +321,7 @@ func baseProof(params *BaseProofParams, prevOut wire.OutPoint) (*Proof, error) {
306321
InternalKey: params.InternalKey,
307322
}
308323
proof.ExclusionProofs = params.ExclusionProofs
324+
proof.Version = version
309325

310326
return proof, nil
311327
}

proof/mock.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ func NewTestFromProof(t testing.TB, p *Proof) *TestProof {
578578
t.Helper()
579579

580580
tp := &TestProof{
581+
Version: p.Version,
581582
PrevOut: p.PrevOut.String(),
582583
BlockHeader: NewTestFromBlockHeader(t, &p.BlockHeader),
583584
BlockHeight: p.BlockHeight,
@@ -651,6 +652,7 @@ func NewTestFromProof(t testing.TB, p *Proof) *TestProof {
651652
}
652653

653654
type TestProof struct {
655+
Version TransitionVersion `json:"version"`
654656
PrevOut string `json:"prev_out"`
655657
BlockHeader *TestBlockHeader `json:"block_header"`
656658
BlockHeight uint32 `json:"block_height"`
@@ -673,6 +675,7 @@ func (tp *TestProof) ToProof(t testing.TB) *Proof {
673675
t.Helper()
674676

675677
p := &Proof{
678+
Version: tp.Version,
676679
PrevOut: test.ParseOutPoint(t, tp.PrevOut),
677680
BlockHeader: *tp.BlockHeader.ToBlockHeader(t),
678681
BlockHeight: tp.BlockHeight,

proof/proof.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,10 @@ type TransitionVersion uint32
228228
const (
229229
// TransitionV0 is the first version of the state transition proof.
230230
TransitionV0 TransitionVersion = 0
231+
232+
// TransitionV1 is the second version of the state transition proof.
233+
// This version added STXO inclusion and exclusion proofs.
234+
TransitionV1 TransitionVersion = 1
231235
)
232236

233237
// Proof encodes all of the data necessary to prove a valid state transition for
@@ -494,11 +498,23 @@ func (p *Proof) IsUnknownVersion() bool {
494498
switch p.Version {
495499
case TransitionV0:
496500
return false
501+
case TransitionV1:
502+
return false
497503
default:
498504
return true
499505
}
500506
}
501507

508+
// IsVersionV0 returns true if the proof is of version V0.
509+
func (p *Proof) IsVersionV0() bool {
510+
return p.Version == TransitionV0
511+
}
512+
513+
// IsVersionV1 returns true if the proof is of version V1.
514+
func (p *Proof) IsVersionV1() bool {
515+
return p.Version == TransitionV1
516+
}
517+
502518
// ToChainAsset converts the proof to a ChainAsset.
503519
func (p *Proof) ToChainAsset() (asset.ChainAsset, error) {
504520
emptyAsset := asset.ChainAsset{}

proof/testdata/proof_tlv_encoding_generated.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"valid_test_cases": [
33
{
44
"proof": {
5+
"version": 0,
56
"prev_out": "8d73ec3f2525632186845c95d4491d1b3a768b7c4e0b6804951aa42655d9b95f:2092150027",
67
"block_header": {
78
"version": 0,
@@ -149,6 +150,7 @@
149150
},
150151
{
151152
"proof": {
153+
"version": 0,
152154
"prev_out": "cd36e9f7676678e7aa2d92faded7e41173fac825416fed825e117039374b7964:3197016449",
153155
"block_header": {
154156
"version": 0,
@@ -262,6 +264,7 @@
262264
},
263265
{
264266
"proof": {
267+
"version": 0,
265268
"prev_out": "098047936c9c64b74e91e98b02c9173a0c2bf9fc6568bb5389421657ffb92d01:3276740653",
266269
"block_header": {
267270
"version": 0,
@@ -409,6 +412,7 @@
409412
},
410413
{
411414
"proof": {
415+
"version": 0,
412416
"prev_out": "ba3a5aceec2c8aaaacfc5dc154086425fcd6090ec04ff5f26283efc6c65ca80d:2394154145",
413417
"block_header": {
414418
"version": 0,
@@ -522,6 +526,7 @@
522526
},
523527
{
524528
"proof": {
529+
"version": 0,
525530
"prev_out": "3a7d712e166bdfbeaa0fe2f8c1fe0aedf1a6e222f59e3bdcdc02b390baf44754:1840451127",
526531
"block_header": {
527532
"version": 0,
@@ -652,6 +657,7 @@
652657
},
653658
{
654659
"proof": {
660+
"version": 0,
655661
"prev_out": "4fbff2770eb58f0d25580a4f5ae17c02d5b55397b4926431624c583b0a7f2080:1409563238",
656662
"block_header": {
657663
"version": 0,
@@ -747,6 +753,7 @@
747753
},
748754
{
749755
"proof": {
756+
"version": 0,
750757
"prev_out": "0efd7a84af6747c045d99121ebdd8f44c9ab40aaab031f765a41fc24541c3b6b:2955392992",
751758
"block_header": {
752759
"version": 0,
@@ -860,6 +867,7 @@
860867
},
861868
{
862869
"proof": {
870+
"version": 0,
863871
"prev_out": "5c92e2b89ad1bdc176163dea42afc12d154eb95e8cca59939285764813883142:3818717915",
864872
"block_header": {
865873
"version": 0,
@@ -973,6 +981,7 @@
973981
},
974982
{
975983
"proof": {
984+
"version": 0,
976985
"prev_out": "a14c5136fd600756ff805584aa5403510d6531e066191291a585e696d511e10f:1312193188",
977986
"block_header": {
978987
"version": 0,
@@ -1076,6 +1085,7 @@
10761085
},
10771086
{
10781087
"proof": {
1088+
"version": 0,
10791089
"prev_out": "dfc122306066091f323f491a95baf7d30e4b85439851da45809354827163240d:2200192249",
10801090
"block_header": {
10811091
"version": 0,
@@ -1197,6 +1207,7 @@
11971207
},
11981208
{
11991209
"proof": {
1210+
"version": 0,
12001211
"prev_out": "7de34688825d31521f73085cddf29420b53e139a8ecad480ca0d379b5bf1350c:4074289298",
12011212
"block_header": {
12021213
"version": 0,
@@ -1320,6 +1331,7 @@
13201331
},
13211332
{
13221333
"proof": {
1334+
"version": 0,
13231335
"prev_out": "42bb7a587d976edf598dd3a5962b00cb9f048fe6b5780b2194ba1cceb7074974:2992579926",
13241336
"block_header": {
13251337
"version": 0,
@@ -1477,6 +1489,7 @@
14771489
},
14781490
{
14791491
"proof": {
1492+
"version": 0,
14801493
"prev_out": "f52bfe55e280959e40fd3182a38aae50dc0977d1d2a8dee9e4b406ea6f9c6427:1573797138",
14811494
"block_header": {
14821495
"version": 0,
@@ -1600,6 +1613,7 @@
16001613
},
16011614
{
16021615
"proof": {
1616+
"version": 0,
16031617
"prev_out": "4ce6979ab1bc2dce1e8b8992f678e1bd4132c8f4a49a233fe5d74c1c45c2c834:3986124895",
16041618
"block_header": {
16051619
"version": 0,

tappsbt/testdata/psbt_encoding_generated.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"unknown_odd_types": null
210210
},
211211
"proof": {
212+
"version": 0,
212213
"prev_out": "676678e7aa2d92faded7e41173fac825416fed825e117039374b7964976f269a:3197016449",
213214
"block_header": {
214215
"version": 1,
@@ -524,6 +525,7 @@
524525
"tr_merkle_root": "",
525526
"proof_delivery_address": "https://example.com",
526527
"proof_suffix": {
528+
"version": 0,
527529
"prev_out": "676678e7aa2d92faded7e41173fac825416fed825e117039374b7964976f269a:3197016449",
528530
"block_header": {
529531
"version": 1,
@@ -990,6 +992,7 @@
990992
"unknown_odd_types": null
991993
},
992994
"proof": {
995+
"version": 0,
993996
"prev_out": "0a7f201283b654043a6680d58fe8ba8f22c93fb09f6c47ac3f0d6fb73c32eded:2773153607",
994997
"block_header": {
995998
"version": 1,
@@ -1305,6 +1308,7 @@
13051308
"tr_merkle_root": "",
13061309
"proof_delivery_address": "https://example.com",
13071310
"proof_suffix": {
1311+
"version": 0,
13081312
"prev_out": "0a7f201283b654043a6680d58fe8ba8f22c93fb09f6c47ac3f0d6fb73c32eded:2773153607",
13091313
"block_header": {
13101314
"version": 1,

tapsend/proof.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,8 @@ type ExclusionProofGenerator func(target *proof.BaseProofParams,
114114
// finalTxPacketOutputs.
115115
func CreateProofSuffix(chainTx *wire.MsgTx, finalTxPacketOutputs []psbt.POutput,
116116
vPacket *tappsbt.VPacket, outputCommitments tappsbt.OutputCommitments,
117-
outIndex int, allAnchoredVPackets []*tappsbt.VPacket) (*proof.Proof,
118-
error) {
117+
outIndex int, allAnchoredVPackets []*tappsbt.VPacket,
118+
opts ...proof.GenOption) (*proof.Proof, error) {
119119

120120
return CreateProofSuffixCustom(
121121
chainTx, vPacket, outputCommitments, outIndex,
@@ -133,7 +133,7 @@ func CreateProofSuffix(chainTx *wire.MsgTx, finalTxPacketOutputs []psbt.POutput,
133133
target, chainTx, finalTxPacketOutputs,
134134
isAnchor,
135135
)
136-
},
136+
}, opts...,
137137
)
138138
}
139139

@@ -150,7 +150,8 @@ func CreateProofSuffix(chainTx *wire.MsgTx, finalTxPacketOutputs []psbt.POutput,
150150
func CreateProofSuffixCustom(finalTx *wire.MsgTx, vPacket *tappsbt.VPacket,
151151
outputCommitments tappsbt.OutputCommitments, outIndex int,
152152
allAnchoredVPackets []*tappsbt.VPacket,
153-
genExclusionProofs ExclusionProofGenerator) (*proof.Proof, error) {
153+
genExclusionProofs ExclusionProofGenerator,
154+
opts ...proof.GenOption) (*proof.Proof, error) {
154155

155156
inputPrevID := vPacket.Inputs[0].PrevID
156157

@@ -195,7 +196,7 @@ func CreateProofSuffixCustom(finalTx *wire.MsgTx, vPacket *tappsbt.VPacket,
195196
}
196197

197198
proofSuffix, err := proof.CreateTransitionProof(
198-
inputPrevID.OutPoint, params,
199+
inputPrevID.OutPoint, params, opts...,
199200
)
200201
if err != nil {
201202
return nil, err

0 commit comments

Comments
 (0)