This repository has been archived by the owner on Apr 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wrapper.go
152 lines (127 loc) · 4.19 KB
/
wrapper.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
package hello
import (
"bytes"
"encoding/hex"
"encoding/json"
"fmt"
neo "multicrypt/Neo"
network "multicrypt/Neo/RPC"
cl "multicrypt/Neo/Transactions/ClaimTransaction"
ct "multicrypt/Neo/Transactions/ContractTransaction"
ci "multicrypt/Neo/Transactions/InvocationTransaction"
"strings"
"time"
fixed8 "multicrypt/Utils/Fixed8"
"strconv"
)
// This class could be cleaned up, there is not really a standard for gomobile sdk's
var Coin *neo.Coin
var Contract ct.ContractTransaction
// This class will act as a wrapper, so that the binder does not complain about types
func NewAccount() string {
return Coin.GenerateMnemonic()
}
func GenerateAddress(mnemonic string, external, internal int) string {
prKey := Coin.GeneratePrivateKey(mnemonic, external, internal)
address := Coin.PubKeyToAddress(prKey.PublicKey())
return address
}
// Prepare refers to converting it from a json to a golang struct
func PrepareTransactionAndSign(transType int, transInJson []byte, keysJsonData []byte, mnemonic string) string {
fmt.Println(transType)
if transType == 128 {
return prepareContractTransactionAndSign(transInJson, mnemonic)
} else if transType == 2 {
return prepareClaimTransactionAndSign(transInJson, keysJsonData, mnemonic)
} else if transType == 209 {
}
return prepareContractTransactionAndSign(transInJson, mnemonic)
}
// Due to the script having any type and Swift not allowing me to encode a type of any. We will have seperate function for invocation
// Ideally would want a better solution
func PrepareInvocationTransactionAndSign(operation string, transInJson []byte, scriptInJson []byte, keysJsonData []byte, mnemonic string) string {
tx := ci.CreateInvocationTransaction()
json.Unmarshal(transInJson, &tx)
if operation == "transfer" {
var s scriptB
json.Unmarshal(scriptInJson, &s)
from := s.Args[0].(string)
to := s.Args[1].(string)
tx.AppendScriptAttribute(from)
amount := int64(s.Args[2].(float64)) // when int is passed through, it is converted to float64
// TODO: test this for large numbers, Alt: send through only strings and use strconv
fmt.Println("amount in go", amount)
tx.Script.SetScriptHash(s.Scripthash)
tx.TransferTokens(from, to, amount)
}
var keys Keys
json.Unmarshal(keysJsonData, &keys)
for _, k := range keys.Keys {
key := strings.Split(k, "-")
External, _ := strconv.Atoi(key[0])
Index, _ := strconv.Atoi(key[1])
tx.AddPrivateKey(mnemonic, External, Index)
}
tx.Script.SetOperation(operation)
tx.AddAttributes(uint16(0xff), []byte(time.Now().String()))
tx.SignTransaction(mnemonic)
buf := new(bytes.Buffer)
err := tx.SerialiseTransaction(buf, true)
if err != nil {
return ""
}
return tx.GetHash() + hex.EncodeToString(buf.Bytes())
}
type scriptB struct {
Scripthash string
Operation string
Args []interface{}
}
type Keys struct {
Keys []string
}
// TODO: refactor, let transactions conform to an interface then
// unmarshall in if statement and pass them to one function to sign
func prepareContractTransactionAndSign(transInJson []byte, mnemonic string) string {
tx := ct.CreateContractTransaction()
json.Unmarshal(transInJson, &tx)
tx.SignTransaction(mnemonic)
buf := new(bytes.Buffer)
err := tx.SerialiseTransaction(buf, true)
if err != nil {
return err.Error()
}
return tx.GetHash() + hex.EncodeToString(buf.Bytes())
}
func prepareClaimTransactionAndSign(transInJson []byte, keysJsonData []byte, mnemonic string) string {
tx := cl.CreateClaimTransaction()
json.Unmarshal(transInJson, &tx)
var keys Keys
json.Unmarshal(keysJsonData, &keys)
for _, k := range keys.Keys {
key := strings.Split(k, "-")
External, _ := strconv.Atoi(key[0])
Index, _ := strconv.Atoi(key[1])
tx.AddPrivateKey(mnemonic, External, Index)
}
tx.SignTransaction(mnemonic)
buf := new(bytes.Buffer)
err := tx.SerialiseTransaction(buf, true)
if err != nil {
return ""
}
return tx.GetHash() + hex.EncodeToString(buf.Bytes())
}
func StringToFixed8(s string) string {
num, err := fixed8.Fixed8DecodeString(s)
if err != nil {
return "0"
}
return strconv.FormatInt(int64(num), 10)
}
func SendTransaction(url string, transactionData string) bool {
client := network.Rpc{}
client.Url = url
res := client.SendTransaction(transactionData)
return res
}