/
ethTypes.go
127 lines (103 loc) · 2.7 KB
/
ethTypes.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
package auth
import (
"encoding/json"
"errors"
"fmt"
"github.com/gogo/protobuf/proto"
"github.com/tendermint/tendermint/crypto"
"github.com/oracleNetworkProtocol/plugchain-sdk-go/codec"
codectypes "github.com/oracleNetworkProtocol/plugchain-sdk-go/codec/types"
sdk "github.com/oracleNetworkProtocol/plugchain-sdk-go/types"
)
var _ Account = (*EthAccount)(nil)
// GetAddress - Implements sdk.AccountI.
func (acc EthAccount) GetAddress() sdk.AccAddress {
addr, _ := sdk.AccAddressFromBech32(acc.Address)
return addr
}
// SetAddress - Implements sdk.AccountI.
func (acc *EthAccount) SetAddress(addr sdk.AccAddress) error {
if len(acc.Address) != 0 {
return errors.New("cannot override BaseAccount address")
}
acc.Address = addr.String()
return nil
}
// GetPubKey - Implements sdk.AccountI.
func (acc EthAccount) GetPubKey() (pk crypto.PubKey) {
if acc.PubKey == nil {
return nil
}
content, ok := acc.PubKey.GetCachedValue().(crypto.PubKey)
if !ok {
return nil
}
return content
}
// SetPubKey - Implements sdk.AccountI.
func (acc *EthAccount) SetPubKey(pubKey crypto.PubKey) error {
if pubKey == nil {
acc.PubKey = nil
} else {
protoMsg, ok := pubKey.(proto.Message)
if !ok {
return sdk.Wrap(fmt.Errorf("err invalid key, can't proto encode %T", protoMsg))
}
any, err := codectypes.NewAnyWithValue(protoMsg)
if err != nil {
return err
}
acc.PubKey = any
}
return nil
}
// GetAccountNumber - Implements AccountI
func (acc EthAccount) GetAccountNumber() uint64 {
return acc.AccountNumber
}
// SetAccountNumber - Implements AccountI
func (acc *EthAccount) SetAccountNumber(accNumber uint64) error {
acc.AccountNumber = accNumber
return nil
}
// GetSequence - Implements sdk.AccountI.
func (acc EthAccount) GetSequence() uint64 {
return acc.Sequence
}
// SetSequence - Implements sdk.AccountI.
func (acc *EthAccount) SetSequence(seq uint64) error {
acc.Sequence = seq
return nil
}
func (acc EthAccount) String() string {
out, _ := json.Marshal(acc)
return string(out)
}
// Convert return a sdk.BaseAccount
func (acc *EthAccount) Convert() interface{} {
// error don't use it
return nil
}
// Convert return a sdk.BaseAccount
// in order to unpack pubKey so not use Convert()
func (acc *EthAccount) ConvertAccount(cdc codec.Marshaler) interface{} {
account := sdk.BaseAccount{
Address: acc.Address,
AccountNumber: acc.AccountNumber,
Sequence: acc.Sequence,
}
var pkStr string
if acc.PubKey == nil {
return account
}
var pk crypto.PubKey
if err := cdc.UnpackAny(acc.PubKey, &pk); err != nil {
return sdk.BaseAccount{}
}
pkStr, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeAccPub, pk)
if err != nil {
panic(err)
}
account.PubKey = pkStr
return account
}