/
mobile.go
314 lines (224 loc) · 5.84 KB
/
mobile.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package neomobile
import (
"encoding/hex"
"encoding/json"
"math/big"
"strings"
"github.com/inwecrypto/bip39"
"github.com/inwecrypto/neogo/keystore"
"github.com/inwecrypto/neogo/nep5"
"github.com/inwecrypto/neogo/rpc"
neotx "github.com/inwecrypto/neogo/tx"
)
// Wallet neo mobile wallet
type Wallet struct {
key *keystore.Key
}
// Tx neo rawtx wrapper
type Tx struct {
Data string
ID string
}
// FromWIF create wallet from wif
func FromWIF(wif string) (*Wallet, error) {
key, err := keystore.KeyFromWIF(wif)
if err != nil {
return nil, err
}
return &Wallet{
key: key,
}, nil
}
// New create a new wallet
func New() (*Wallet, error) {
key, err := keystore.NewKey()
if err != nil {
return nil, err
}
return &Wallet{
key: key,
}, nil
}
// FromMnemonic create wallet from mnemonic
func FromMnemonic(mnemonic string) (*Wallet, error) {
dic, _ := bip39.GetDict("zh_CN")
data, err := bip39.MnemonicToByteArray(mnemonic, dic)
if err != nil {
return nil, err
}
data = data[1 : len(data)-1]
println(hex.EncodeToString(data))
key, err := keystore.KeyFromPrivateKey(data)
if err != nil {
return nil, err
}
return &Wallet{
key: key,
}, nil
}
// FromKeyStore create wallet from keystore
func FromKeyStore(ks string, password string) (*Wallet, error) {
key, err := keystore.ReadKeyStore([]byte(ks), password)
if err != nil {
return nil, err
}
return &Wallet{
key: key,
}, nil
}
// ToKeyStore write wallet to keystore format string
func (wrapper *Wallet) ToKeyStore(password string) (string, error) {
keystore, err := keystore.WriteLightScryptKeyStore(wrapper.key, password)
return string(keystore), err
}
// CreateAssertTx create assert transfer raw tx
func (wrapper *Wallet) CreateAssertTx(assert, from, to string, amount float64, unspent string) (*Tx, error) {
var utxos []*rpc.UTXO
if err := json.Unmarshal([]byte(unspent), &utxos); err != nil {
return nil, err
}
vout := []*neotx.Vout{
&neotx.Vout{
Asset: assert,
Value: neotx.MakeFixed8(amount),
Address: to,
},
}
tx := neotx.NewContractTx()
err := tx.CalcInputs(vout, utxos)
if err != nil {
return nil, err
}
// tx, err := neotx.CreateSendAssertTx(assert, from, to, amount, utxos)
// if err != nil {
// return nil, err
// }
rawtxdata, txid, err := tx.Tx().Sign(wrapper.key.PrivateKey)
return &Tx{
Data: hex.EncodeToString(rawtxdata),
ID: txid,
}, err
}
// Address get wallet address
func (wrapper *Wallet) Address() string {
return wrapper.key.Address
}
// Mnemonic gete mnemonic string
func (wrapper *Wallet) Mnemonic() (string, error) {
privateKeyBytes := wrapper.key.ToBytes()
dic, _ := bip39.GetDict("zh_CN")
println(hex.EncodeToString(privateKeyBytes))
data, err := bip39.NewMnemonic(privateKeyBytes, dic)
if err != nil {
return "", err
}
return data, nil
}
// CreateClaimTx create claim tx
func (wrapper *Wallet) CreateClaimTx(amount float64, address string, unspent string) (*Tx, error) {
var utxos []*rpc.UTXO
if err := json.Unmarshal([]byte(unspent), &utxos); err != nil {
return nil, err
}
tx := neotx.NewClaimTx()
err := tx.Claim(amount, address, utxos)
if err != nil {
return nil, err
}
rawtxdata, txid, err := tx.Tx().Sign(wrapper.key.PrivateKey)
return &Tx{
Data: hex.EncodeToString(rawtxdata),
ID: txid,
}, err
}
// MintToken .
func (wrapper *Wallet) MintToken(asset string, gas, amount float64, unspent string) (*Tx, error) {
var utxos []*rpc.UTXO
if err := json.Unmarshal([]byte(unspent), &utxos); err != nil {
return nil, err
}
scriptHash, err := hex.DecodeString(strings.TrimPrefix(asset, "0x"))
if err != nil {
return nil, err
}
scriptHash = reverseBytes(scriptHash)
address := neotx.EncodeAddress(scriptHash)
script, err := nep5.MintToken(scriptHash)
if err != nil {
return nil, err
}
tx := neotx.NewInvocationTx(script, gas)
vout := []*neotx.Vout{
&neotx.Vout{
Asset: neotx.NEOAssert,
Value: neotx.MakeFixed8(amount),
Address: address,
},
}
err = tx.CalcInputs(vout, utxos)
if err != nil {
return nil, err
}
rawtxdata, txid, err := tx.Tx().Sign(wrapper.key.PrivateKey)
return &Tx{
Data: hex.EncodeToString(rawtxdata),
ID: txid,
}, err
}
// CreateNep5Tx create nep5 transfer transaction
func (wrapper *Wallet) CreateNep5Tx(asset string, from, to string, gas float64, amount int64, unspent string) (*Tx, error) {
var utxos []*rpc.UTXO
if err := json.Unmarshal([]byte(unspent), &utxos); err != nil {
return nil, err
}
scriptHash, err := hex.DecodeString(strings.TrimPrefix(asset, "0x"))
if err != nil {
return nil, err
}
scriptHash = reverseBytes(scriptHash)
bytesOfFrom, err := hex.DecodeString(from)
if err != nil {
return nil, err
}
bytesOfFrom = reverseBytes(bytesOfFrom)
bytesOfTo, err := hex.DecodeString(to)
if err != nil {
return nil, err
}
bytesOfTo = reverseBytes(bytesOfTo)
script, err := nep5.Transfer(scriptHash, bytesOfFrom, bytesOfTo, big.NewInt(amount))
tx := neotx.NewInvocationTx(script, gas)
err = tx.CalcInputs(nil, utxos)
if err != nil {
return nil, err
}
rawtxdata, txid, err := tx.Tx().Sign(wrapper.key.PrivateKey)
return &Tx{
Data: hex.EncodeToString(rawtxdata),
ID: txid,
}, err
}
func reverseBytes(s []byte) []byte {
for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 {
s[i], s[j] = s[j], s[i]
}
return s
}
// DecodeAddress decode address
func DecodeAddress(address string) (string, error) {
bytesOfAddress, err := neotx.DecodeAddress(address)
if err != nil {
return "", err
}
bytesOfAddress = reverseBytes(bytesOfAddress)
return hex.EncodeToString(bytesOfAddress), nil
}
// EncodeAddress encode address
func EncodeAddress(address string) (string, error) {
bytesOfAddress, err := hex.DecodeString(strings.TrimPrefix(address, "0x"))
if err != nil {
return "", err
}
bytesOfAddress = reverseBytes(bytesOfAddress)
return neotx.EncodeAddress(bytesOfAddress), nil
}