forked from arkade-os/emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.go
More file actions
156 lines (130 loc) · 3.81 KB
/
Copy pathscript.go
File metadata and controls
156 lines (130 loc) · 3.81 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package arkade
import (
"bytes"
"errors"
"fmt"
"github.com/arkade-os/arkd/pkg/ark-lib/extension"
scriptlib "github.com/arkade-os/arkd/pkg/ark-lib/script"
"github.com/btcsuite/btcd/btcec/v2"
"github.com/btcsuite/btcd/btcec/v2/schnorr"
"github.com/btcsuite/btcd/btcutil/psbt"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/wire"
)
var ErrTweakedArkadePubKeyNotFound = errors.New("tweaked arkade script public key not found in tapscript")
type ArkadeScript struct {
script []byte
hash []byte
witness wire.TxWitness
pubkey *btcec.PublicKey
tapLeaf txscript.TapLeaf
}
type ExecuteOption func(*Engine)
func WithDebugCallback(callback func(*StepInfo, *Engine) error) ExecuteOption {
return func(engine *Engine) {
engine.stepCallback = func(step *StepInfo) error {
return callback(step, engine)
}
}
}
func ReadArkadeScript(ptx *psbt.Packet, signerPublicKey *btcec.PublicKey, entry IntrospectorEntry) (*ArkadeScript, error) {
inputIndex := int(entry.Vin)
if len(ptx.Inputs) <= inputIndex {
return nil, fmt.Errorf("input index out of range")
}
input := ptx.Inputs[inputIndex]
if len(input.TaprootLeafScript) == 0 {
return nil, fmt.Errorf("input does not specify any TaprootLeafScript")
}
spendingTapscript := input.TaprootLeafScript[0]
if spendingTapscript == nil {
return nil, fmt.Errorf("input does not specify any TaprootLeafScript")
}
scriptHash := ArkadeScriptHash(entry.Script)
expectedPublicKey := ComputeArkadeScriptPublicKey(signerPublicKey, scriptHash)
expectedPublicKeyXonly := schnorr.SerializePubKey(expectedPublicKey)
closure, err := scriptlib.DecodeClosure(spendingTapscript.Script)
if err != nil {
return nil, fmt.Errorf("failed to decode tapscript: %w", err)
}
var pubkeys []*btcec.PublicKey
switch c := closure.(type) {
case *scriptlib.MultisigClosure:
pubkeys = c.PubKeys
case *scriptlib.CSVMultisigClosure:
pubkeys = c.PubKeys
case *scriptlib.CLTVMultisigClosure:
pubkeys = c.PubKeys
case *scriptlib.ConditionMultisigClosure:
pubkeys = c.PubKeys
case *scriptlib.ConditionCSVMultisigClosure:
pubkeys = c.PubKeys
default:
return nil, fmt.Errorf("unsupported closure type: %T", closure)
}
found := false
for _, pubkey := range pubkeys {
xonly := schnorr.SerializePubKey(pubkey)
if bytes.Equal(xonly, expectedPublicKeyXonly) {
found = true
break
}
}
if !found {
return nil, ErrTweakedArkadePubKeyNotFound
}
return &ArkadeScript{
script: entry.Script,
hash: scriptHash,
witness: entry.Witness,
pubkey: expectedPublicKey,
tapLeaf: txscript.NewBaseTapLeaf(spendingTapscript.Script),
}, nil
}
func (s *ArkadeScript) Execute(spendingTx *wire.MsgTx, prevoutFetcher txscript.PrevOutputFetcher, inputIndex int, opts ...ExecuteOption) error {
prevOut := prevoutFetcher.FetchPrevOutput(spendingTx.TxIn[inputIndex].PreviousOutPoint)
inputAmount := int64(0)
if prevOut != nil {
inputAmount = prevOut.Value
}
engine, err := NewEngine(
s.script,
spendingTx,
inputIndex,
txscript.NewSigCache(100),
txscript.NewTxSigHashes(spendingTx, prevoutFetcher),
inputAmount,
prevoutFetcher,
)
if err != nil {
return fmt.Errorf("failed to create engine: %w", err)
}
for _, opt := range opts {
opt(engine)
}
ext, err := extension.NewExtensionFromTx(spendingTx)
if err == nil {
if ap := ext.GetAssetPacket(); ap != nil {
engine.SetAssetPacket(ap)
}
}
if len(s.witness) > 0 {
engine.SetStack(s.witness)
}
if err := engine.Execute(); err != nil {
return fmt.Errorf("failed to execute arkade script: %w", err)
}
return nil
}
func (s *ArkadeScript) Hash() []byte {
return append([]byte(nil), s.hash...)
}
func (s *ArkadeScript) PubKey() *btcec.PublicKey {
return s.pubkey
}
func (s *ArkadeScript) TapLeaf() txscript.TapLeaf {
return s.tapLeaf
}
func (s *ArkadeScript) Script() []byte {
return append([]byte(nil), s.script...)
}