Repository navigation
Expand file tree
/
Copy pathcustom_anchor_fields.go
More file actions
86 lines (71 loc) · 2.52 KB
/
Copy pathcustom_anchor_fields.go
File metadata and controls
86 lines (71 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package tapsdk
import (
"bytes"
"fmt"
"github.com/btcsuite/btcd/psbt/v2"
"github.com/lightninglabs/taproot-assets/tappsbt"
)
// AnchorOutputTaprootAssetRoot returns the Taproot Asset commitment root
// that tapd records as a custom PSBT field on the asset-carrying outputs
// of a committed anchor transaction. The boolean reports whether the
// output carries the field. An error is returned when the PSBT cannot be
// decoded, the output index is out of range, or the recorded value is not
// a 32-byte root.
//
// Integrators that persist or relay committed anchor PSBTs can use this
// to recover an output's commitment root without depending on tapd's
// internal PSBT conventions.
func AnchorOutputTaprootAssetRoot(anchorPSBT []byte,
outputIndex int) (Hash, bool, error) {
var root Hash
packet, err := psbt.NewFromRawBytes(
bytes.NewReader(anchorPSBT), false,
)
if err != nil {
return root, false, fmt.Errorf("decoding anchor PSBT: %w", err)
}
if outputIndex < 0 || outputIndex >= len(packet.Outputs) {
return root, false, fmt.Errorf("anchor output index %d out "+
"of range, PSBT has %d outputs", outputIndex,
len(packet.Outputs))
}
value := tappsbt.ExtractCustomField(
packet.Outputs[outputIndex].Unknowns,
tappsbt.PsbtKeyTypeOutputAssetRoot,
)
if len(value) == 0 {
return root, false, nil
}
if len(value) != len(root) {
return root, false, fmt.Errorf("anchor output %d Taproot "+
"Asset root has %d bytes, want %d", outputIndex,
len(value), len(root))
}
copy(root[:], value)
return root, true, nil
}
// SetAnchorOutputTaprootAssetRoot records the given Taproot Asset
// commitment root on the anchor PSBT output at the given index, using the
// same custom PSBT field tapd writes when committing an anchor
// transaction. It returns the re-serialized PSBT. This is intended for
// template tooling and test fixtures; in normal operation tapd records
// the field itself.
func SetAnchorOutputTaprootAssetRoot(anchorPSBT []byte, outputIndex int,
root Hash) ([]byte, error) {
packet, err := psbt.NewFromRawBytes(
bytes.NewReader(anchorPSBT), false,
)
if err != nil {
return nil, fmt.Errorf("decoding anchor PSBT: %w", err)
}
if outputIndex < 0 || outputIndex >= len(packet.Outputs) {
return nil, fmt.Errorf("anchor output index %d out of "+
"range, PSBT has %d outputs", outputIndex,
len(packet.Outputs))
}
packet.Outputs[outputIndex].Unknowns = tappsbt.AddCustomField(
packet.Outputs[outputIndex].Unknowns,
tappsbt.PsbtKeyTypeOutputAssetRoot, root[:],
)
return serializePSBT(packet)
}