forked from arkade-os/emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtx.go
More file actions
100 lines (82 loc) · 3.38 KB
/
Copy pathtx.go
File metadata and controls
100 lines (82 loc) · 3.38 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
package application
import (
"context"
"errors"
"fmt"
"github.com/ArkLabsHQ/introspector/pkg/arkade"
"github.com/btcsuite/btcd/btcutil/psbt"
log "github.com/sirupsen/logrus"
)
// SubmitTx aims to execute arkade scripts on offchain ark transactions
// execution of the script runs only on ark tx, if valid, the associated checkpoint tx
func (s *service) SubmitTx(ctx context.Context, tx OffchainTx) (*OffchainTx, error) {
arkPtx := tx.ArkTx
// index checkpoints by txid for easy lookup while signing ark transaction
indexedCheckpoints := make(map[string]*psbt.Packet) // txid => checkpoint psbt
for _, checkpoint := range tx.Checkpoints {
indexedCheckpoints[checkpoint.UnsignedTx.TxID()] = checkpoint
}
// preserve original checkpoint order for deterministic response
orderedCheckpointTxids := make([]string, 0, len(tx.Checkpoints))
for _, checkpoint := range tx.Checkpoints {
orderedCheckpointTxids = append(orderedCheckpointTxids, checkpoint.UnsignedTx.TxID())
}
prevoutFetcher, err := computePrevoutFetcher(arkPtx)
if err != nil {
return nil, fmt.Errorf("failed to create prevout fetcher: %w", err)
}
// Parse IntrospectorPacket from the transaction's OP_RETURN output
packet, err := arkade.FindIntrospectorPacket(arkPtx.UnsignedTx)
if err != nil {
return nil, fmt.Errorf("failed to parse introspector packet: %w", err)
}
if len(packet) == 0 {
return nil, fmt.Errorf("no introspector packet found in transaction")
}
signerPublicKey := s.signer.secretKey.PubKey()
var nSigned = 0
for _, entry := range packet {
inputIndex := int(entry.Vin)
script, err := arkade.ReadArkadeScript(arkPtx, signerPublicKey, entry)
if err != nil {
// there may be input/entry pairs attributed to a different signer
if errors.Is(err, arkade.ErrTweakedArkadePubKeyNotFound) && len(arkPtx.Inputs) > 1 {
continue
}
return nil, fmt.Errorf("failed to read arkade script: %w vin=%d", err, inputIndex)
}
log.Debugf("executing arkade script: %x", script.Script())
if err := script.Execute(arkPtx.UnsignedTx, prevoutFetcher, inputIndex); err != nil {
return nil, fmt.Errorf("failed to execute arkade script: %w vin=%d", err, inputIndex)
}
log.Debugf("execution of %x succeeded", script.Script())
if err := s.signer.signInput(arkPtx, inputIndex, script.Hash(), prevoutFetcher); err != nil {
return nil, fmt.Errorf("failed to sign input %d: %w", inputIndex, err)
}
// search for checkpoint
inputTxid := arkPtx.UnsignedTx.TxIn[inputIndex].PreviousOutPoint.Hash.String()
checkpointPtx, ok := indexedCheckpoints[inputTxid]
if !ok {
return nil, fmt.Errorf("checkpoint not found for input %d", inputIndex)
}
checkpointPrevoutFetcher, err := computePrevoutFetcher(checkpointPtx)
if err != nil {
return nil, fmt.Errorf("failed to create prevout fetcher for checkpoint: %w", err)
}
if err := s.signer.signInput(checkpointPtx, 0, script.Hash(), checkpointPrevoutFetcher); err != nil {
return nil, fmt.Errorf("failed to sign checkpoint input %d: %w", inputIndex, err)
}
nSigned++
}
if nSigned == 0 {
return nil, fmt.Errorf("failed to find any valid input/entry pairs")
}
signedCheckpointTxs := make([]*psbt.Packet, 0, len(orderedCheckpointTxids))
for _, txid := range orderedCheckpointTxids {
signedCheckpointTxs = append(signedCheckpointTxs, indexedCheckpoints[txid])
}
return &OffchainTx{
ArkTx: arkPtx,
Checkpoints: signedCheckpointTxs,
}, nil
}