forked from O3Labs/neo-utils
/
mobile.go
146 lines (122 loc) · 3.58 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
package neoutils
import (
"bytes"
"encoding/binary"
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"github.com/o3labs/neo-utils/neoutils/nep6"
"github.com/o3labs/neo-utils/neoutils/o3"
"github.com/o3labs/neo-utils/neoutils/smartcontract"
)
// This class contains simplified method designed specifically for gomobile bind
// gomobile bind doesn't support slice argument or return
func utxoFromO3Platform(network string, address string) (smartcontract.Unspent, error) {
unspent := smartcontract.Unspent{
Assets: map[smartcontract.NativeAsset]*smartcontract.Balance{},
}
client := o3.DefaultO3APIClient()
if network == "test" {
client = o3.APIClientWithNEOTestnet()
}
if network == "private" {
client = o3.APIClientWithNEOPrivateNet()
}
response := client.GetNEOUTXO(address)
if response.Code != 200 {
return unspent, fmt.Errorf("Error cannot get utxo")
}
gasBalance := smartcontract.Balance{
Amount: float64(0) / float64(100000000),
UTXOs: []smartcontract.UTXO{},
}
neoBalance := smartcontract.Balance{
Amount: float64(0) / float64(100000000),
UTXOs: []smartcontract.UTXO{},
}
for _, v := range response.Result.Data {
if strings.Contains(v.Asset, string(smartcontract.GAS)) {
value, err := strconv.ParseFloat(v.Value, 64)
if err != nil {
continue
}
gasTX1 := smartcontract.UTXO{
Index: v.Index,
TXID: v.Txid,
Value: value,
}
gasBalance.UTXOs = append(gasBalance.UTXOs, gasTX1)
}
if strings.Contains(v.Asset, string(smartcontract.NEO)) {
value, err := strconv.ParseFloat(v.Value, 64)
if err != nil {
continue
}
tx := smartcontract.UTXO{
Index: v.Index,
TXID: v.Txid,
Value: value,
}
neoBalance.UTXOs = append(neoBalance.UTXOs, tx)
}
}
unspent.Assets[smartcontract.GAS] = &gasBalance
unspent.Assets[smartcontract.NEO] = &neoBalance
return unspent, nil
}
type RawTransaction struct {
TXID string
Data []byte
}
func MintTokensRawTransactionMobile(network string, scriptHash string, wif string, sendingAssetID string, amount float64, remark string, networkFeeAmountInGAS float64) (*RawTransaction, error) {
rawTransaction := &RawTransaction{}
fee := smartcontract.NetworkFeeAmount(networkFeeAmountInGAS)
nep5 := UseNEP5WithNetworkFee(scriptHash, fee)
wallet, err := GenerateFromWIF(wif)
if err != nil {
return nil, err
}
unspent, err := utxoFromO3Platform(network, wallet.Address)
if err != nil {
return nil, err
}
nativeAsset := smartcontract.NativeAssets[sendingAssetID]
if nativeAsset == "" {
return nil, fmt.Errorf("invalid assetID")
}
if amount <= 0 {
return nil, fmt.Errorf("Invalid amount. cannot be zero or less than zero")
}
data, txIDString, err := nep5.MintTokensRawTransaction(*wallet, nativeAsset, amount, unspent, remark)
if err != nil {
return nil, err
}
rawTransaction.Data = data
rawTransaction.TXID = txIDString
return rawTransaction, nil
}
func GenerateNEP6FromEncryptedKey(walletName, addressLabel, address, encryptedKey string) string {
nep6Wallet := nep6.NewNEP6WithNEP2EncryptedKey(walletName, addressLabel, address, encryptedKey)
b, err := json.Marshal(nep6Wallet)
if err != nil {
return ""
}
return string(b)
}
func SerializeTX(jsonString string) []byte {
tx := NeonJSTransaction{}
json.Unmarshal([]byte(jsonString), &tx)
log.Printf("%+v", tx)
final := NeonJSTXSerializer(tx)
return final
}
func NEOAddresstoScriptHashBigEndian(neoAddress string) string {
return NEOAddressToScriptHashWithEndian(neoAddress, binary.BigEndian)
}
func GetVarUInt(value int64) []byte {
buff := new(bytes.Buffer)
WriteVarUint(buff, uint64(value))
return buff.Bytes()
}