-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathexecute.go
165 lines (142 loc) · 4.85 KB
/
execute.go
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
157
158
159
160
161
162
163
164
165
package main
import (
"bytes"
"context"
"encoding/hex"
"errors"
"fmt"
"time"
"github.com/gcash/bchd/bchrpc/pb"
"github.com/gcash/bchd/chaincfg/chainhash"
"github.com/gcash/bchd/txscript"
"github.com/gcash/bchd/wire"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
)
// Execute holds the options to the execute command.
type Execute struct {
Transaction string `short:"t" long:"tx" description:"the full transaction hex or BCH mainnet txid. If only a txid is provided the transaction will be looked up via the RPC server."`
InputIndex int `short:"i" long:"idx" description:"the input index to debug"`
InputAmount int64 `short:"a" long:"amt" description:"the amount of the input (in satoshis) we're debugging. This can be omitted if the transaction is in the BCH blockchain as it will be looked up via the RPC server."`
ScriptPubkey string `short:"s" long:"pkscript" description:"the input's scriptPubkey. This can be omitted if the transaction is in the BCH blockchain as it will be looked up via the RPC server."`
RPCServer string `long:"rpcserver" description:"A hostname:port for a gRPC API to use to fetch the transaction and scriptPubkey if not providing through the options."`
}
// Execute will run the Execute command. This executes the script, prints
// the result and exists.
func (x *Execute) Execute(_ []string) error {
var (
txBytes []byte
scriptPubkey []byte
client pb.BchrpcClient
err error
)
if txid, err := chainhash.NewHashFromStr(x.Transaction); err == nil {
conn, err := grpc.Dial(x.RPCServer, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
if err != nil {
return err
}
client = pb.NewBchrpcClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, err := client.GetRawTransaction(ctx, &pb.GetRawTransactionRequest{
Hash: txid[:],
})
if err != nil {
return err
}
txBytes = resp.Transaction
} else {
txBytes, err = hex.DecodeString(x.Transaction)
if err != nil {
return err
}
}
tx := &wire.MsgTx{}
if err := tx.BchDecode(bytes.NewReader(txBytes), wire.ProtocolVersion, wire.BaseEncoding); err != nil {
return err
}
if len(tx.TxIn) == 0 {
return errors.New("transaction has no inputs")
}
if x.ScriptPubkey == "" {
if client == nil {
conn, err := grpc.Dial(x.RPCServer, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
if err != nil {
return err
}
client = pb.NewBchrpcClient(conn)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, err := client.GetTransaction(ctx, &pb.GetTransactionRequest{
Hash: tx.TxIn[x.InputIndex].PreviousOutPoint.Hash[:],
})
if err != nil {
return err
}
scriptPubkey = resp.Transaction.Outputs[tx.TxIn[x.InputIndex].PreviousOutPoint.Index].PubkeyScript
} else {
scriptPubkey, err = hex.DecodeString(x.ScriptPubkey)
if err != nil {
return err
}
}
if x.InputAmount == 0 {
if client == nil {
conn, err := grpc.Dial(x.RPCServer, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
if err != nil {
return err
}
client = pb.NewBchrpcClient(conn)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, err := client.GetTransaction(ctx, &pb.GetTransactionRequest{
Hash: tx.TxIn[x.InputIndex].PreviousOutPoint.Hash[:],
})
if err != nil {
return err
}
x.InputAmount = resp.Transaction.Outputs[tx.TxIn[x.InputIndex].PreviousOutPoint.Index].Value
}
utxoCache := txscript.NewUtxoCache()
for i, txIn := range tx.TxIn {
if client == nil {
conn, err := grpc.Dial(x.RPCServer, grpc.WithTransportCredentials(credentials.NewClientTLSFromCert(nil, "")))
if err != nil {
return err
}
client = pb.NewBchrpcClient(conn)
}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*10)
defer cancel()
resp, err := client.GetTransaction(ctx, &pb.GetTransactionRequest{
Hash: txIn.PreviousOutPoint.Hash[:],
})
if err != nil {
return err
}
output := resp.Transaction.Outputs[txIn.PreviousOutPoint.Index]
txOut := wire.TxOut{}
txOut.Value = output.Value
txOut.PkScript = output.PubkeyScript
txOut.TokenData = wire.TokenData{}
if output.CashToken != nil {
copy(txOut.TokenData.CategoryID[:], output.CashToken.CategoryId)
txOut.TokenData.BitField = output.CashToken.Bitfield[0]
txOut.TokenData.Amount = output.CashToken.Amount
txOut.TokenData.Commitment = output.CashToken.Commitment
}
utxoCache.AddEntry(i, txOut)
}
flags := txscript.StandardVerifyFlags | txscript.ScriptAllowCashTokens
vm, err := txscript.NewEngine(scriptPubkey, tx, x.InputIndex, flags, nil, nil, utxoCache, x.InputAmount)
if err != nil {
return err
}
if err := vm.Execute(); err != nil {
return err
}
fmt.Println("Success!!!")
return nil
}