-
Notifications
You must be signed in to change notification settings - Fork 7
/
WaykiCallContractTx.go
66 lines (54 loc) · 1.53 KB
/
WaykiCallContractTx.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
package commons
import (
"bytes"
"encoding/hex"
"github.com/btcsuite/btcutil"
hash2 "github.com/WaykiChain/wicc-wallet-utils-go/commons/hash"
)
type WaykiCallContractTx struct {
WaykiBaseSignTx
AppId *UserIdWraper //user regid or user key id or app regid
Fees uint64
Values uint64 //transfer amount
Contract []byte
}
func (tx WaykiCallContractTx) SignTx(wifKey *btcutil.WIF) string {
buf := bytes.NewBuffer([]byte{})
writer := NewWriterHelper(buf)
writer.WriteByte(byte(tx.TxType))
writer.WriteVarInt(tx.Version)
writer.WriteVarInt(tx.ValidHeight)
if(tx.UserId!=nil){
writer.WriteUserId(tx.UserId)
}else if(tx.PubKey!=nil){
writer.WritePubKeyId(tx.PubKey)
}
writer.WriteUserId(tx.AppId)
writer.WriteVarInt(int64(tx.Fees))
writer.WriteVarInt(int64(tx.Values))
writer.WriteBytes(tx.Contract)
signedBytes := tx.doSignTx(wifKey)
writer.WriteBytes(signedBytes)
rawTx := hex.EncodeToString(buf.Bytes())
return rawTx
}
func (tx WaykiCallContractTx) doSignTx(wifKey *btcutil.WIF) []byte {
buf := bytes.NewBuffer([]byte{})
writer := NewWriterHelper(buf)
writer.WriteVarInt(tx.Version)
writer.WriteByte(byte(tx.TxType))
writer.WriteVarInt(tx.ValidHeight)
if(tx.UserId!=nil){
writer.WriteUserId(tx.UserId)
}else if(tx.PubKey!=nil){
writer.WritePubKeyId(tx.PubKey)
}
writer.WriteUserId(tx.AppId)
writer.WriteVarInt(int64(tx.Fees))
writer.WriteVarInt(int64(tx.Values))
writer.WriteBytes(tx.Contract)
hash := hash2.DoubleHash256(buf.Bytes())
key := wifKey.PrivKey
ss, _ := key.Sign(hash)
return ss.Serialize()
}