Skip to content

Commit afd6d2f

Browse files
committed
define NFTContractDecoder interface
1 parent e79bff0 commit afd6d2f

File tree

5 files changed

+129
-23
lines changed

5 files changed

+129
-23
lines changed

common/math.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,4 @@ func BytesToDecimals(bits []byte, decimals int32) decimal.Decimal {
6767
amount := new(big.Int)
6868
amount.SetBytes(bits)
6969
return decimal.NewFromBigInt(amount, 0).Shift(-decimals)
70-
}
70+
}

common/math_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ func TestStringNumToBigIntWithDecimals(t *testing.T) {
2626
t.Logf("num int64 : %v\n", num.Int64())
2727
newnum := BigIntToDecimals(num, 8)
2828
t.Logf("newnum : %v\n", newnum.String())
29-
}
29+
}

crypto/crypto_utils_test.go

+31-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package crypto
22

33
import (
4+
"encoding/base64"
45
"encoding/hex"
56
"encoding/json"
67
"fmt"
@@ -42,7 +43,7 @@ func TestEncryptJSON(t *testing.T) {
4243

4344
params := map[string]interface{}{
4445
"name": "chance",
45-
"age": 18,
46+
"age": 18,
4647
}
4748

4849
plainText, _ := json.Marshal(params)
@@ -57,7 +58,7 @@ func TestEncryptJSON(t *testing.T) {
5758
//1bf9d9ff6bbaf559e6b74479b0e770b480de59c5aa9ff044478834a0b1ec6986
5859
enbase, _ := base58.Decode(base)
5960
fmt.Printf("enbase hex(%d): %s\n", len(enbase), hex.EncodeToString(enbase))
60-
raw, _ :=AESDecrypt(enbase, key)
61+
raw, _ := AESDecrypt(enbase, key)
6162
fmt.Printf("raw = %s\n", string(raw))
6263
}
6364

@@ -77,18 +78,41 @@ func TestHmac(t *testing.T) {
7778
}
7879

7980
func TestBlake2b(t *testing.T) {
80-
data, _ := hex.DecodeString("0500000000000000010200000000000000393000002e16000094260000000b000000000000000000000001000000000020000000")
81+
data, _ := hex.DecodeString("")
8182
hash := owcrypt.Hash(data, 32, owcrypt.HASH_ALG_BLAKE2B)
8283
fmt.Printf("hash: %s\n", hex.EncodeToString(hash))
8384
//ca2cc09e84e78d7bbcd58004721aec0526904e0379508e1e87716d85597e1862
8485
//ca2cc09e84e78d7bbcd58004721aec0526904e0379508e1e87716d85597e1862
8586
}
8687

8788
func TestED25519(t *testing.T) {
88-
key, _ := hex.DecodeString("1ece69a7de1136e08bdf48e972ae79b06ea86b502fea4a959aa3401e7af977af")
89+
key, _ := hex.DecodeString("")
8990
pubkey, _ := owcrypt.GenPubkey(key, owcrypt.ECC_CURVE_ED25519_NORMAL)
90-
signature, _:= hex.DecodeString("4f024f27d1b5e54356744fda6e9474c29907d8431a4b9918b6ff1a7dfd1361ad60947a9b09aa9756a99c5165d42f7f4637ec8cfb5bbdf7d5a35052c204ae720e")
91+
signature, _ := hex.DecodeString("")
92+
msg, _ := hex.DecodeString("")
9193
fmt.Printf("pubkey: %s \n", hex.EncodeToString(pubkey))
92-
flag := owcrypt.Verify(pubkey, nil, []byte("hello word"), signature, owcrypt.ECC_CURVE_ED25519_NORMAL)
94+
flag := owcrypt.Verify(pubkey, nil, msg, signature, owcrypt.ECC_CURVE_ED25519_NORMAL)
9395
fmt.Printf("flag: %d \n", flag)
94-
}
96+
}
97+
98+
func TestBase64(t *testing.T) {
99+
base64Str := ""
100+
bs, err := base64.StdEncoding.DecodeString(base64Str)
101+
if err != nil {
102+
t.Errorf("base64 decode failed")
103+
return
104+
}
105+
prv := bs[16:]
106+
fmt.Printf("hex: %s \n", hex.EncodeToString(prv))
107+
}
108+
109+
func TestSECP256R1(t *testing.T) {
110+
key, _ := hex.DecodeString("")
111+
pubkey, _ := owcrypt.GenPubkey(key, owcrypt.ECC_CURVE_SECP256R1)
112+
pubkey = owcrypt.PointCompress(pubkey, owcrypt.ECC_CURVE_SECP256R1)
113+
fmt.Printf("pubkey: %s \n", hex.EncodeToString(pubkey))
114+
msg, _ := hex.DecodeString("111111111111111111111111111111111111111111111111")
115+
sig, _, ret := owcrypt.Signature(key, nil, msg, owcrypt.ECC_CURVE_SECP256R1)
116+
fmt.Printf("ret: %d \n", ret)
117+
fmt.Printf("sig: %s \n", hex.EncodeToString(sig))
118+
}

openwallet/contract.go

+8-14
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,14 @@ import (
2323
"github.com/blocktree/openwallet/v2/crypto"
2424
)
2525

26+
const (
27+
// 0:未知协议,1:ERC20协议,2:ERC721协议,3:ERC1155协议
28+
InterfaceTypeUnknown = ""
29+
InterfaceTypeERC20 = "erc20"
30+
InterfaceTypeERC721 = "erc721"
31+
InterfaceTypeERC1155 = "erc1155"
32+
)
33+
2634
type SmartContract struct {
2735
ContractID string `json:"contractID" storm:"id"` //计算ID:base64(sha256({symbol}_{address})) 主链symbol
2836
Symbol string `json:"symbol"` //主币的symbol
@@ -97,20 +105,6 @@ type SmartContractReceipt struct {
97105
Status string `json:"status"` //@required 链上状态,0:失败,1:成功
98106
Reason string `json:"reason"` //失败原因,失败状态码
99107
ExtParam string `json:"extParam"` //扩展参数,用于调用智能合约,json结构
100-
101-
/*
102-
例如:ETH 智能合约调用参数
103-
{
104-
"gasPrice": "0.000002", //自定义费率
105-
"gasLimit": "50000000", //自定义燃料上限
106-
"gasUsed": "32234", //实际使用燃料数
107-
"senderAddress": "0x1234567abcdeffdcba4321", //支付交易单的地址
108-
"contractAddress": "0xdeffdcba43211234567abc", //合约地址
109-
"amount": "0.001", //转入合约主币数量
110-
"callData": "deffdcba43211234567abc", //调用方法的ABI编码
111-
"nonce": 1, //地址账户交易序号
112-
}
113-
*/
114108
}
115109

116110
func (tx *SmartContractReceipt) GenWxID() {

openwallet/nft.go

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2022 The OpenWallet Authors
3+
* This file is part of the OpenWallet library.
4+
*
5+
* The OpenWallet library is free software: you can redistribute it and/or modify
6+
* it under the terms of the GNU Lesser General Public License as published by
7+
* the Free Software Foundation, either version 3 of the License, or
8+
* (at your option) any later version.
9+
*
10+
* The OpenWallet library is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU Lesser General Public License for more details.
14+
*/
15+
16+
package openwallet
17+
18+
type NFT struct {
19+
Symbol string `json:"symbol"` //@required 主币的symbol
20+
Address string `json:"address"`
21+
Token string `json:"token"` //@required NFT的symbol
22+
Protocol string `json:"protocol"`
23+
Name string `json:"name"`
24+
TokenID string `json:"tokenID"` //@required
25+
}
26+
27+
type NFTBalance struct {
28+
Contract *NFT
29+
Balance *string
30+
}
31+
32+
// NFTEventType
33+
const (
34+
NFTEventTypeTransferred = 0 //转账
35+
NFTEventTypeMinted = 1 //铸造
36+
NFTEventTypeBurned = 2 //销毁
37+
)
38+
39+
type NFTTransfer struct {
40+
Tokens []NFT `json:"tokens"` //@required nft
41+
Operator string `json:"operator"` //required 被授权转账的操作者
42+
From string `json:"from"` //@required 发送者
43+
To string `json:"to"` //@required 接受者
44+
Amounts []string `json:"amounts"` //@required erc1155 token有数量
45+
EventType uint64 `json:"eventType"` //@required
46+
}
47+
48+
type NFTApproval struct {
49+
Token *NFT `json:"token"` //@required nft
50+
Owner string `json:"owner"` //@required nft拥有者
51+
Operator string `json:"operator"` //required 被授权的操作者
52+
IsAll bool `json:"isAll"` //@required 是否全部NFT,true:token = null,false,token = *NFT
53+
}
54+
55+
//NFTContractDecoder NFT智能合约解析器
56+
type NFTContractDecoder interface {
57+
//GetNFTBalanceByAddress 查询地址NFT余额列表
58+
GetNFTBalanceByAddress(contract []*NFT, owner []string) ([]*NFTBalance, *Error)
59+
//GetNFTOwnerByTokenID 查询地址token的拥有者
60+
GetNFTOwnerByTokenID(contract *NFT) (string, *Error)
61+
//GetMetaDataOfNFT 查询NFT的MetaData
62+
GetMetaDataOfNFT(contract *NFT) (string, *Error)
63+
//GetNFTTransfer 从event解析NFT转账信息
64+
GetNFTTransfer(event *SmartContractEvent) (*NFTTransfer, *Error)
65+
}
66+
67+
type NFTContractDecoderBase struct {
68+
}
69+
70+
//GetNFTBalanceByAddress 查询地址NFT余额列表
71+
func (decoder *NFTContractDecoderBase) GetNFTBalanceByAddress(contract []*NFT, owner []string) ([]*NFTBalance, *Error) {
72+
return nil, Errorf(ErrSystemException, "GetNFTBalanceByAddress not implement")
73+
}
74+
75+
//GetNFTOwnerByTokenID 查询地址token的拥有者
76+
func (decoder *NFTContractDecoderBase) GetNFTOwnerByTokenID(contract *NFT) (string, *Error) {
77+
return "", Errorf(ErrSystemException, "GetNFTBalanceByAddress not implement")
78+
}
79+
80+
//GetMetaDataOfNFT 查询NFT的MetaData
81+
func (decoder *NFTContractDecoderBase) GetMetaDataOfNFT(contract *NFT) (string, *Error) {
82+
return "", Errorf(ErrSystemException, "GetNFTBalanceByAddress not implement")
83+
}
84+
85+
//GetNFTTransfer 从event解析NFT转账信息
86+
func (decoder *NFTContractDecoderBase) GetNFTTransfer(event *SmartContractEvent) (*NFTTransfer, *Error) {
87+
return nil, Errorf(ErrSystemException, "GetNFTBalanceByAddress not implement")
88+
}

0 commit comments

Comments
 (0)