-
Notifications
You must be signed in to change notification settings - Fork 25
/
rpc.go
187 lines (166 loc) · 4.38 KB
/
rpc.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package main
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
"net/http"
"time"
"github.com/MixinNetwork/mixin/common"
"github.com/MixinNetwork/mixin/crypto"
)
var httpClient *http.Client
func callRPC(node, method string, params []interface{}) ([]byte, error) {
if httpClient == nil {
httpClient = &http.Client{Timeout: 30 * time.Second}
}
body, err := json.Marshal(map[string]interface{}{
"method": method,
"params": params,
})
if err != nil {
panic(err)
}
req, err := http.NewRequest("POST", node, bytes.NewReader(body))
if err != nil {
return nil, err
}
req.Close = true
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var result struct {
Data interface{} `json:"data"`
Error interface{} `json:"error"`
}
err = json.NewDecoder(resp.Body).Decode(&result)
if err != nil {
return nil, err
}
if result.Error != nil {
return nil, fmt.Errorf("ERROR %s", result.Error)
}
return json.Marshal(result.Data)
}
type signerInput struct {
Inputs []struct {
Hash crypto.Hash `json:"hash"`
Index uint `json:"index"`
Deposit *common.DepositData `json:"deposit,omitempty"`
Keys []*crypto.Key `json:"keys"`
Mask crypto.Key `json:"mask"`
} `json:"inputs"`
Outputs []struct {
Type uint8 `json:"type"`
Script common.Script `json:"script"`
Accounts []*common.Address `json:"accounts,omitempty"`
Keys []*crypto.Key `json:"keys,omitempty"`
Mask crypto.Key `json:"mask,omitempty"`
Amount common.Integer `json:"amount"`
}
Asset crypto.Hash `json:"asset"`
Extra string `json:"extra"`
Node string `json:"-"`
}
func (raw signerInput) ReadUTXOKeys(hash crypto.Hash, index uint) (*common.UTXOKeys, error) {
utxo := &common.UTXOKeys{}
for _, in := range raw.Inputs {
if in.Hash == hash && in.Index == index && len(in.Keys) > 0 {
utxo.Keys = in.Keys
utxo.Mask = in.Mask
return utxo, nil
}
}
data, err := callRPC(raw.Node, "getutxo", []interface{}{hash.String(), index})
if err != nil {
return nil, err
}
var out common.UTXOWithLock
err = json.Unmarshal(data, &out)
if err != nil {
return nil, err
}
if out.Amount.Sign() == 0 {
return nil, fmt.Errorf("invalid input %s#%d", hash.String(), index)
}
utxo.Keys = out.Keys
utxo.Mask = out.Mask
return utxo, nil
}
func (raw signerInput) CheckDepositInput(deposit *common.DepositData, tx crypto.Hash) error {
return nil
}
func (raw signerInput) ReadLastMintDistribution(group string) (*common.MintDistribution, error) {
return nil, nil
}
func SignTransaction(account common.Address, raw signerInput) (*common.SignedTransaction, error) {
tx := common.NewTransactionV5(raw.Asset)
for _, in := range raw.Inputs {
if in.Deposit != nil {
tx.AddDepositInput(in.Deposit)
} else {
tx.AddInput(in.Hash, in.Index)
}
}
for _, out := range raw.Outputs {
if out.Type != common.OutputTypeScript {
return nil, fmt.Errorf("invalid output type %d", out.Type)
}
if out.Accounts != nil {
tx.AddRandomScriptOutput(out.Accounts, out.Script, out.Amount)
}
if out.Keys != nil {
tx.Outputs = append(tx.Outputs, &common.Output{
Type: common.OutputTypeScript,
Amount: out.Amount,
Keys: out.Keys,
Script: common.NewThresholdScript(1),
Mask: out.Mask,
})
}
}
extra, err := hex.DecodeString(raw.Extra)
if err != nil {
return nil, err
}
tx.Extra = extra
signed := &common.SignedTransaction{Transaction: *tx}
for i := range signed.Inputs {
err := signed.SignInput(raw, i, []*common.Address{&account})
if err != nil {
return nil, err
}
}
return signed, nil
}
func SignTransactionRaw(node string, account common.Address, rawStr string) (string, error) {
var raw signerInput
err := json.Unmarshal([]byte(rawStr), &raw)
if err != nil {
return "", err
}
raw.Node = node
tx, err := SignTransaction(account, raw)
if err != nil {
return "", err
}
d := &common.VersionedTransaction{SignedTransaction: *tx}
return hex.EncodeToString(d.Marshal()), nil
}
func SendRawTransaction(node string, raw string) (string, error) {
data, err := callRPC(node, "sendrawtransaction", []interface{}{raw})
if err != nil {
return "", err
}
var resp struct {
Hash string `json:"hash"`
}
err = json.Unmarshal(data, &resp)
if err != nil {
return "", err
}
return resp.Hash, nil
}