-
Notifications
You must be signed in to change notification settings - Fork 2
/
AdaptorErc20.go
324 lines (291 loc) · 10.5 KB
/
AdaptorErc20.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
/*
This file is part of go-palletone.
go-palletone is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
go-palletone 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
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with go-palletone. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* @author PalletOne core developers <[email protected]>
* @date 2018-2019
*/
package ethadaptor
import (
"errors"
"fmt"
"math/big"
"strconv"
"github.com/btcsuite/btcutil/base58"
"github.com/ethereum/go-ethereum/common"
"github.com/palletone/adaptor"
)
const EMPTY_ADDRESS = "0x0000000000000000000000000000000000000000"
type AdaptorErc20 struct {
NetID int
RPCParams
//lockContractAddress string
}
func NewAdaptorErc20(netID int, rPCParams RPCParams) *AdaptorErc20 {
return &AdaptorErc20{netID, rPCParams}
}
func NewAdaptorErc20Testnet() *AdaptorErc20 {
return &AdaptorErc20{
NetID: NETID_TEST,
RPCParams: RPCParams{Rawurl: "https://ropsten.infura.io",
TxQueryUrl: "https://api-ropsten.etherscan.io/api?apikey=VYSBPQ383RJXM7HBQVTIK5NGIG8ZYVV6T6"},
//lockContractAddress: "0x4d736ed88459b2db85472aab13a9d0ce2a6ea676",
}
}
func NewAdaptorErc20Mainnet() *AdaptorErc20 {
return &AdaptorErc20{
NetID: NETID_MAIN,
RPCParams: RPCParams{Rawurl: "https://mainnet.infura.io",
TxQueryUrl: "https://api.etherscan.io/api?apikey=VYSBPQ383RJXM7HBQVTIK5NGIG8ZYVV6T6"},
//lockContractAddress: "0x1989a21eb0f28063e47e6b448e8d76774bc9b493",
}
}
/*IUtility*/
//创建一个新的私钥
func (aerc20 *AdaptorErc20) NewPrivateKey(input *adaptor.NewPrivateKeyInput) (*adaptor.NewPrivateKeyOutput, error) {
prikey, err := NewPrivateKey(aerc20.NetID)
if err != nil {
return nil, err
}
result := adaptor.NewPrivateKeyOutput{PrivateKey: prikey}
return &result, nil
}
//根据私钥创建公钥
func (aerc20 *AdaptorErc20) GetPublicKey(input *adaptor.GetPublicKeyInput) (*adaptor.GetPublicKeyOutput, error) {
pubkey, err := GetPublicKey(input.PrivateKey)
if err != nil {
return nil, err
}
result := adaptor.GetPublicKeyOutput{PublicKey: pubkey}
return &result, nil
}
//根据Key创建地址
func (aerc20 *AdaptorErc20) GetAddress(key *adaptor.GetAddressInput) (*adaptor.GetAddressOutput, error) {
addr, err := PubKeyToAddress(key.Key)
if err != nil {
return nil, err
}
result := adaptor.GetAddressOutput{Address: addr}
return &result, nil
}
func GetMappAddr(addr *adaptor.GetPalletOneMappingAddressInput, rpcParams *RPCParams) (
*adaptor.GetPalletOneMappingAddressOutput, error) {
const MapAddrABI = `[
{
"constant": true,
"inputs": [
{
"name": "ptnAddr",
"type": "address"
}
],
"name": "getMapEthAddr",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "addr",
"type": "address"
}
],
"name": "getMapPtnAddr",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]`
var result adaptor.GetPalletOneMappingAddressOutput
var input adaptor.QueryContractInput
input.ContractAddress = addr.MappingDataSource
input.Extra = []byte(MapAddrABI)
if len(addr.ChainAddress) != 0 { //ETH地址
//
input.Function = "getMapPtnAddr"
input.Args = append(input.Args, []byte(addr.ChainAddress))
//
ret := new(string)
err := QueryContractCall(&input, rpcParams, ret)
if err != nil {
return nil, err
}
resultStr := *ret
fmt.Println("resultStr", resultStr)
if len(resultStr) == 0 || resultStr == EMPTY_ADDRESS {
return nil, adaptor.ErrNotFound
}
result.PalletOneAddress = resultStr
} else { //PTN地址 P开头
input.Function = "getMapEthAddr"
if addr.PalletOneAddress[0] != byte('P') {
return nil, fmt.Errorf("PalletOne address must start with 'P'")
}
addrBytes, _, err := base58.CheckDecode(addr.PalletOneAddress[1:])
if err != nil {
return nil, err
}
addrHex := fmt.Sprintf("%x", addrBytes)
input.Args = append(input.Args, []byte(addrHex))
//
ret := new(common.Address)
err = QueryContractCall(&input, rpcParams, ret)
if err != nil {
return nil, err
}
resultStr := ret.String()
//fmt.Println("resultStr", resultStr)
if len(resultStr) == 0 || resultStr == EMPTY_ADDRESS {
return nil, adaptor.ErrNotFound
}
result.ChainAddress = resultStr
}
return &result, nil
}
func (aerc20 *AdaptorErc20) GetPalletOneMappingAddress(addrInput *adaptor.GetPalletOneMappingAddressInput) (
*adaptor.GetPalletOneMappingAddressOutput, error) {
if len(addrInput.MappingDataSource) == 0 {
return nil, errors.New("you must set mapping contract address in MappingDataSource of addrInput")
}
return GetMappAddr(addrInput, &aerc20.RPCParams)
}
//对一条交易进行签名,并返回签名结果
func (aerc20 *AdaptorErc20) SignTransaction(input *adaptor.SignTransactionInput) (
*adaptor.SignTransactionOutput, error) {
if 'm' == input.Transaction[0] {
inputNew := &adaptor.SignMessageInput{}
inputNew.PrivateKey = input.PrivateKey
inputNew.Message = input.Transaction[1:]
result, err := SignMessage(inputNew)
if err != nil {
return nil, err
}
return &adaptor.SignTransactionOutput{Signature: result.Signature}, nil
}
return SignTransaction(input)
}
func (aerc20 *AdaptorErc20) HashMessage(input *adaptor.HashMessageInput) (*adaptor.HashMessageOutput, error) {
return HashMessage(input)
}
//对一条消息进行签名
func (aerc20 *AdaptorErc20) SignMessage(input *adaptor.SignMessageInput) (*adaptor.SignMessageOutput, error) {
return SignMessage(input)
}
//对签名进行验证
func (aerc20 *AdaptorErc20) VerifySignature(input *adaptor.VerifySignatureInput) (
*adaptor.VerifySignatureOutput, error) {
return VerifySignature(input)
}
//将未签名的原始交易与签名进行绑定,返回一个签名后的交易
func (aerc20 *AdaptorErc20) BindTxAndSignature(input *adaptor.BindTxAndSignatureInput) (
*adaptor.BindTxAndSignatureOutput, error) {
return BindTxAndSignature(input)
}
//根据交易内容,计算交易Hash
func (aerc20 *AdaptorErc20) CalcTxHash(input *adaptor.CalcTxHashInput) (*adaptor.CalcTxHashOutput, error) {
return CalcTxHash(input)
}
//将签名后的交易广播到网络中,如果发送交易需要手续费,指定最多支付的手续费
func (aerc20 *AdaptorErc20) SendTransaction(input *adaptor.SendTransactionInput) (
*adaptor.SendTransactionOutput, error) {
return SendTransaction(input, &aerc20.RPCParams)
}
//根据交易ID获得交易的基本信息
func (aerc20 *AdaptorErc20) GetTxBasicInfo(input *adaptor.GetTxBasicInfoInput) (
*adaptor.GetTxBasicInfoOutput, error) {
return GetTxBasicInfo(input, &aerc20.RPCParams, aerc20.NetID)
}
/*ICryptoCurrency*/
//获取某地址下持有某资产的数量,返回数量为该资产的最小单位
func (aerc20 *AdaptorErc20) GetBalance(input *adaptor.GetBalanceInput) (*adaptor.GetBalanceOutput, error) {
const ERC20ABI = "[{\"constant\":true,\"inputs\":[{\"name\":\"who\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]"
var inputQuery adaptor.QueryContractInput
inputQuery.ContractAddress = input.Asset
inputQuery.Function = "balanceOf"
inputQuery.Args = append(inputQuery.Args, []byte(input.Address))
inputQuery.Extra = []byte(ERC20ABI)
//
resultQuery, err := QueryContract(&inputQuery, &aerc20.RPCParams)
if err != nil {
return nil, err
}
balanceStr := string(resultQuery.QueryResult)
balanceAmt := new(big.Int)
balanceAmt.SetString(balanceStr[1:len(balanceStr)-1], 10)
var result = &adaptor.GetBalanceOutput{}
result.Balance = adaptor.AmountAsset{Amount: balanceAmt, Asset: input.Asset}
return result, nil
}
//获取某资产的小数点位数
func (aerc20 *AdaptorErc20) GetAssetDecimal(asset *adaptor.GetAssetDecimalInput) (
*adaptor.GetAssetDecimalOutput, error) {
const ERC20ABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]"
var input adaptor.QueryContractInput
input.ContractAddress = asset.Asset
input.Function = "decimals"
input.Extra = []byte(ERC20ABI)
//
resultQuery, err := QueryContract(&input, &aerc20.RPCParams)
if err != nil {
return nil, err
}
decimalStr := string(resultQuery.QueryResult)
decimal, _ := strconv.ParseUint(decimalStr[1:len(decimalStr)-1], 10, 64)
var result adaptor.GetAssetDecimalOutput
result.Decimal = uint(decimal)
return &result, nil
}
//创建一个转账交易,但是未签名
func (aerc20 *AdaptorErc20) CreateTransferTokenTx(input *adaptor.CreateTransferTokenTxInput) (
*adaptor.CreateTransferTokenTxOutput, error) {
return CreateTx(input) //add m and pack, return msg
}
//获取某个地址对某种Token的交易历史,支持分页和升序降序排列
func (aerc20 *AdaptorErc20) GetAddrTxHistory(input *adaptor.GetAddrTxHistoryInput) (*adaptor.GetAddrTxHistoryOutput,
error) {
return GetAddrErc20TxHistoryHTTP(aerc20.TxQueryUrl, input) // use web api
}
//根据交易ID获得对应的转账交易
func (aerc20 *AdaptorErc20) GetTransferTx(input *adaptor.GetTransferTxInput) (*adaptor.GetTransferTxOutput, error) {
return GetTransferTx(input, &aerc20.RPCParams, aerc20.NetID, true)
}
//创建一个多签地址,该地址必须要满足signCount个签名才能解锁
func (aerc20 *AdaptorErc20) CreateMultiSigAddress(input *adaptor.CreateMultiSigAddressInput) (
*adaptor.CreateMultiSigAddressOutput, error) {
//return &adaptor.CreateMultiSigAddressOutput{Address: aerc20.lockContractAddress}, nil
return nil, errors.New("please deploy multi-sign contract yourself")
}
//构造一个从多签地址付出Token的交易
func (aerc20 *AdaptorErc20) CreateMultiSigPayoutTx(input *adaptor.CreateMultiSigPayoutTxInput) (
*adaptor.CreateMultiSigPayoutTxOutput, error) {
//return &adaptor.CreateMultiSigAddressOutput{Address: aerc20.lockContractAddress}, nil
return nil, errors.New("not implement")
}
//获取最新区块头
func (aerc20 *AdaptorErc20) GetBlockInfo(input *adaptor.GetBlockInfoInput) (*adaptor.GetBlockInfoOutput, error) {
return GetBlockInfo(input, &aerc20.RPCParams)
}