-
Notifications
You must be signed in to change notification settings - Fork 12
/
omni_handler.go
468 lines (409 loc) · 14 KB
/
omni_handler.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* Copyright (C) 2018-2019 Fusion Foundation Ltd. All rights reserved.
* Copyright (C) 2018-2019 gaozhengxin@fusion.org
*
* This library is free software; you can redistribute it and/or
* modify it under the Apache License, Version 2.0.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package omni
import (
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"math/big"
"runtime/debug"
"strconv"
"strings"
"github.com/btcsuite/btcd/btcec"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcd/txscript"
"github.com/btcsuite/btcd/btcjson"
"github.com/btcsuite/btcd/wire"
"github.com/btcsuite/btcutil"
"github.com/btcsuite/btcwallet/wallet/txauthor"
"github.com/btcsuite/btcwallet/wallet/txrules"
rpcutils "github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/rpcutils"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/btc"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/config"
"github.com/fsn-dev/dcrm-walletService/crypto/dcrm/cryptocoins/types"
)
/*
This simple send transaction is for lockin test
{
"txid": "862010dc55c8a79c82c16eadc694b33fd8afaf5da4f8489b16d30e4af762d036",
"fee": "0.00000514",
"sendingaddress": "mkkKjig9MFbJfXUpWUxphnEwCTZJGdCnKp",
"referenceaddress": "misxGZ91RLiqfUwfXRfEDmv9UCEHEKRjWP",
"ismine": false,
"version": 0,
"type_int": 0,
"type": "Simple Send",
"propertyid": 112,
"divisible": true,
"amount": "10000.00000000",
"confirmations": 0
}
*/
func OMNIInit() {
allowHighFees = true
chainconfig = &chaincfg.TestNet3Params
feeRate, _ = btcutil.NewAmount(0.0001)
hashType = txscript.SigHashAll
}
var allowHighFees bool
var chainconfig = &chaincfg.TestNet3Params
var feeRate, _ = btcutil.NewAmount(0.0001)
var hashType = txscript.SigHashAll
var Properties map[string]string = map[string]string{
"OMNIOmni":"1",
"OMNITest Omni":"2",
"OMNITetherUS":"112", // there is a plurarity of token named TetherUS on testnet3
}
// 返回property id
var GetProperty func (name string) string
func RegisterPropertyGetter (callback func (name string) string) {
GetProperty = callback
}
type OmniHandler struct {
propertyName string
btcHandler *btc.BTCHandler
}
func NewOMNIHandler () *OmniHandler {
return &OmniHandler{
btcHandler: btc.NewBTCHandlerWithConfig(config.ApiGateways.OmniGateway.Host,config.ApiGateways.OmniGateway.Port,config.ApiGateways.OmniGateway.User,config.ApiGateways.OmniGateway.Passwd,config.ApiGateways.OmniGateway.Usessl),
}
}
func NewOMNIPropertyHandler (propertyname string) *OmniHandler {
if Properties[propertyname] == "" {
return nil
}
return &OmniHandler{
propertyName: propertyname,
btcHandler: btc.NewBTCHandlerWithConfig(config.ApiGateways.OmniGateway.Host,config.ApiGateways.OmniGateway.Port,config.ApiGateways.OmniGateway.User,config.ApiGateways.OmniGateway.Passwd,config.ApiGateways.OmniGateway.Usessl),
}
}
var OMNI_DEFAULT_FEE, _ = new(big.Int).SetString("10",10)
func (h *OmniHandler) GetDefaultFee() types.Value {
return types.Value{Cointype:"BTC",Val:OMNI_DEFAULT_FEE}
}
func (h *OmniHandler) PublicKeyToAddress(pubKeyHex string) (address string, err error) {
if len(pubKeyHex) != 132 && len(pubKeyHex) != 130 {
return "", errors.New("invalid public key length")
}
if pubKeyHex[:2] == "0x" || pubKeyHex[:2] == "0X" {
pubKeyHex = pubKeyHex[2:]
}
bb, err := hex.DecodeString(pubKeyHex)
if err != nil {
return
}
pubKey, err := btcec.ParsePubKey(bb, btcec.S256())
if err != nil {
return
}
b := pubKey.SerializeCompressed()
pkHash := btcutil.Hash160(b)
addressPubKeyHash, err := btcutil.NewAddressPubKeyHash(pkHash, chainconfig)
if err != nil {
return
}
address = addressPubKeyHash.EncodeAddress()
return
}
func (h *OmniHandler) BuildUnsignedTransaction(fromAddress, fromPublicKey, toAddress string, amount *big.Int, jsonstring string) (transaction interface{}, digests []string, err error) {
defer func() {
if e := recover(); e != nil {
err = fmt.Errorf("Runtime error: %v\n%v\n", e, string(debug.Stack()))
}
} ()
changeAddress := fromAddress
unspentOutputs, _, err := btc.ListUnspent_electrs(fromAddress)
if err != nil {
return
}
sourceOutputs := make(map[string][]btcjson.ListUnspentResult)
for _, unspentOutput := range unspentOutputs {
if !unspentOutput.Spendable {
continue
}
if unspentOutput.Confirmations < btc.RequiredConfirmations {
continue
}
b, _ := hex.DecodeString(unspentOutput.ScriptPubKey)
pkScript, err := txscript.ParsePkScript(b)
if err != nil {
continue
}
class := pkScript.Class().String()
if class != "pubkeyhash" {
continue
}
sourceAddressOutputs := sourceOutputs[unspentOutput.Address]
sourceOutputs[unspentOutput.Address] = append(sourceAddressOutputs, unspentOutput)
}
// 设置交易输出
var txOuts []*wire.TxOut
//c, _ := rpcutils.NewClient(omnihost, omniport, omniuser, omnipasswd, omniusessl)
// Vout 0
// 1. omni_createpayload_simplesend
propertyId := Properties[h.propertyName]
amt := ""
if amount.Cmp(big.NewInt(100000000)) == 1 {
tmp := amount.String()
amt = new(big.Int).Div(amount, big.NewInt(100000000)).String() + "." + tmp[len(tmp)-8:]
} else {
amt = strconv.FormatFloat(float64(amount.Int64())/100000000,'f',-1,64)
}
fmt.Printf("amount is %v\namt is %v\n",amount,amt)
/*req1 := `{"jsonrpc":"1.0","id":"1","method":"omni_createpayload_simplesend","params":[` + propertyId + `,"` + amt + `"]}`
fmt.Printf("======== req1 is %v ========\n", req1)
retJSON1, err := c.Send(req1)
fmt.Printf("======== retJSON1 is %v ========\n", retJSON1)
var ret1 interface{}
err = json.Unmarshal([]byte(retJSON1), &ret1)
if err != nil {
return
}
payload := ret1.(map[string]interface{})["result"].(string)*/
pid, _ := strconv.Atoi(propertyId)
pidhex := strconv.FormatInt(int64(pid), 16)
amthex := strconv.FormatUint(amount.Uint64(), 16)
pidScript := make16(pidhex)
amtScript := make16(amthex)
payload := pidScript + amtScript
fmt.Printf("payload: %v\n", payload)
if payload == "" {
err = fmt.Errorf("create payload error")
return
}
// 2. omni_createrawtx_opreturn
scriptStr := "6a146f6d6e69" + payload
script, _ := hex.DecodeString(scriptStr)
txOut := wire.NewTxOut(0, script)
txOuts = append(txOuts,txOut)
// 3. 发送 1 satoshi
toAddr, _ := btcutil.DecodeAddress(toAddress, chainconfig)
pkscript0, _ := txscript.PayToAddrScript(toAddr)
txOut0 := wire.NewTxOut(1, pkscript0)
txOuts = append(txOuts, txOut0)
if len(sourceOutputs) < 1 {
err = fmt.Errorf("cannot find p2pkh utxo")
return
}
previousOutputs := sourceOutputs[fromAddress]
targetAmount := btc.SumOutputValues(txOuts)
estimatedSize := btc.EstimateVirtualSize(0, 1, 0, txOuts, true)
targetFee := txrules.FeeForSerializeSize(feeRate, estimatedSize)
// 选择utxo作为交易输入
// *************************************************
var inputSource txauthor.InputSource
for i, _ := range previousOutputs {
inputSource = btc.MakeInputSource(previousOutputs[:i+1])
inputAmount, _, _, _, err1 := inputSource(targetAmount + targetFee)
if err1 != nil {
err = err1
return
}
if inputAmount < targetAmount+targetFee {
fmt.Printf("=========inputAmount %v, targetAmount %v, targetFee %v=========\n",inputAmount,targetAmount,targetFee)
continue
} else {
break
}
}
// *************************************************
// 设置找零
changeAddr, _ := btcutil.DecodeAddress(changeAddress, chainconfig)
changeSource := func()([]byte,error){
return txscript.PayToAddrScript(changeAddr)
}
transaction, err = btc.NewUnsignedTransaction(txOuts, feeRate, inputSource, changeSource)
if err != nil {
return
}
for idx, _ := range transaction.(*btc.AuthoredTx).Tx.TxIn {
pkscript, _ := hex.DecodeString(previousOutputs[idx].ScriptPubKey)
txhashbytes, err1 := txscript.CalcSignatureHash(pkscript, hashType, transaction.(*btc.AuthoredTx).Tx, idx)
if err1 != nil {
err = err1
return
}
txhash := hex.EncodeToString(txhashbytes)
digests = append(digests, txhash)
}
transaction.(*btc.AuthoredTx).Digests = digests
if fromPublicKey[:2] == "0x" || fromPublicKey[:2] == "0X" {
fromPublicKey = fromPublicKey[2:]
}
bb, err := hex.DecodeString(fromPublicKey)
if err != nil {
return
}
pubKey, err := btcec.ParsePubKey(bb, btcec.S256())
if err != nil {
return
}
transaction.(*btc.AuthoredTx).PubKeyData = pubKey.SerializeCompressed()
for i, txout := range transaction.(*btc.AuthoredTx).Tx.TxOut {
s := hex.EncodeToString(txout.PkScript)
fmt.Printf("script %v: %v\n", i, s)
}
return
}
func make16(in string) string {
if len(in) < 16 {
return make16("0" + in)
} else {
return in[:16]
}
}
// NOT completed, may or not work
func (h *OmniHandler) SignTransaction(hash []string, wif interface{}) (rsv []string, err error){
return h.btcHandler.SignTransaction(hash, wif)
}
// NOT completed, may or not work
func (h *OmniHandler) MakeSignedTransaction(rsv []string, transaction interface{}) (signedTransaction interface{}, err error){
return h.btcHandler.MakeSignedTransaction(rsv, transaction)
}
// NOT completed, may or not work
func (h *OmniHandler) SubmitTransaction(signedTransaction interface{}) (ret string, err error) {
c, _ := rpcutils.NewClient(config.ApiGateways.OmniGateway.Host,config.ApiGateways.OmniGateway.Port,config.ApiGateways.OmniGateway.User,config.ApiGateways.OmniGateway.Passwd,config.ApiGateways.OmniGateway.Usessl)
ret, err= btc.SendRawTransaction (c, signedTransaction.(*btc.AuthoredTx).Tx, allowHighFees)
return
}
func (h *OmniHandler) GetTransactionInfo(txhash string) (fromAddress string, txOutputs []types.TxOutput, jsonstring string, confirmed bool, fee types.Value, err error) {
defer func () {
if e := recover(); e != nil {
err = fmt.Errorf("Runtime error: %v\n%v", e, string(debug.Stack()))
return
}
} ()
fee = h.GetDefaultFee()
client, _ := rpcutils.NewClient(config.ApiGateways.OmniGateway.Host,config.ApiGateways.OmniGateway.Port,config.ApiGateways.OmniGateway.User,config.ApiGateways.OmniGateway.Passwd,config.ApiGateways.OmniGateway.Usessl)
reqstr := `{"jsonrpc":"1.0","id":"1","method":"omni_gettransaction","params":["`+txhash+`"]}`
ret, err1 := client.Send(reqstr)
if err1 != nil {
err = err1
return
}
if ret == "" {
err = fmt.Errorf("failed get transaction")
return
}
omniTx := DecodeOmniTx(ret)
if omniTx.Error != nil {
err = omniTx.Error
return
}
confirmed = (omniTx.Confirmations >= btc.RequiredConfirmations) && omniTx.Valid
//**************************
// confirmed = true
//**************************
fmt.Printf("omniTx is %+v\n", omniTx)
if h.propertyName != omniTx.PropertyName && Properties[h.propertyName] != strconv.Itoa(int(omniTx.PropertyId)) {
err = fmt.Errorf("property name does not match: have %v want %v", omniTx.PropertyName, h.propertyName)
}
fromAddress = omniTx.From
txOutput := types.TxOutput{
ToAddress:omniTx.To,
Amount:omniTx.Amount,
}
txOutputs = append(txOutputs, txOutput)
fmt.Printf("========== OMNI_GetTransactionInfo ========\n")
fmt.Printf("fromAddress: %v\ntxOutputs: %+v\n", fromAddress, txOutputs)
if omniTx.Fee != "" {
feeStr := toSatoshi(omniTx.Fee)
fee.Val, _ = new(big.Int).SetString(feeStr, 10)
}
return
}
func toSatoshi(str string) string {
if strings.Contains(str, ".") {
str = strings.Replace(str, ".", "", -1)
}
if strings.HasPrefix(str, "0") {
str = strings.TrimPrefix(str, "0")
return toSatoshi(str)
} else {
return str
}
}
func (h *OmniHandler) GetAddressBalance(address string, jsonstring string) (balance types.Balance, err error) {
defer func () {
if e := recover(); e != nil {
err = fmt.Errorf("Runtime error: %v\n%v", e, string(debug.Stack()))
return
}
} ()
propertyId := Properties[h.propertyName]
client, _ := rpcutils.NewClient(config.ApiGateways.OmniGateway.Host,config.ApiGateways.OmniGateway.Port,config.ApiGateways.OmniGateway.User,config.ApiGateways.OmniGateway.Passwd,config.ApiGateways.OmniGateway.Usessl)
reqstr := `{"jsonrpc":"1.0","id":"1","method":"omni_getbalance","params":["`+address+`",`+propertyId+`]}`
ret, err1 := client.Send(reqstr)
if err != nil {
err = err1
return
}
var retObj interface{}
json.Unmarshal([]byte(ret), &retObj)
if retObj == nil {
err = fmt.Errorf("unmarshal response string error.")
return
}
result := retObj.(map[string]interface{})["result"]
balanceStr := result.(map[string]interface{})["balance"].(string)
reservedStr := result.(map[string]interface{})["reserved"].(string)
frozenStr := result.(map[string]interface{})["frozen"].(string)
balanceStr = strings.Replace(balanceStr,".","",-1)
reservedStr = strings.Replace(reservedStr,".","",-1)
frozenStr = strings.Replace(frozenStr,".","",-1)
tokenbalance, _ := new(big.Int).SetString(balanceStr,10)
reserved, _ := new(big.Int).SetString(reservedStr,10)
frozen, _ := new(big.Int).SetString(frozenStr,10)
tokenbalance.Sub(tokenbalance.Sub(tokenbalance,reserved),frozen)
btcbalance, err := h.btcHandler.GetAddressBalance(address, "")
if err != nil {
return
}
balance = types.Balance{
CoinBalance:btcbalance.CoinBalance,
TokenBalance:types.Value{
Cointype:h.propertyName,
Val:tokenbalance,
},
}
// 由于目前测试没有OMNITetherUS币,临时用OMNI代替USDT进行测试
//"OMNIOmni":"1",
//"OMNITest Omni":"2",
// 参数cointype=USDT时实际上对应"OMNITetherUS"币种 :币种id="31", // there is a plurarity of token named TetherUS on testnet3
if h.propertyName == "OMNIOmni" {
balance.TokenBalance.Cointype = "USDT" //tmp code
}
//============================================
return
}
func (h *OmniHandler) IsToken() bool {
return true
}
func parseRPCReturn (retJSON string) (result interface{}, err error) {
var ret interface{}
json.Unmarshal([]byte(retJSON), &ret)
result = ret.(map[string]interface{})["result"]
if result == nil {
errStr := ret.(map[string]interface{})["error"]
if errStr == nil {
err = fmt.Errorf("unknown error")
return
}
err = fmt.Errorf(errStr.(string))
}
return
}