-
Notifications
You must be signed in to change notification settings - Fork 0
/
account.go
95 lines (80 loc) · 2.04 KB
/
account.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
package ethereum
import (
"encoding/hex"
"fmt"
"time"
"github.com/btcsuite/btcd/btcutil/hdkeychain"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
func GetSafeLastTxTime(rpc, address string) (time.Time, error) {
conn, abi, err := guardInit(rpc)
if err != nil {
return time.Time{}, err
}
defer conn.Close()
addr := common.HexToAddress(address)
timestamp, err := abi.SafeLastTxTime(nil, addr)
if err != nil {
return time.Time{}, err
}
t := time.Unix(timestamp.Int64(), 0)
return t, nil
}
func GetOwners(rpc, address string) ([]common.Address, error) {
conn, abi, err := safeInit(rpc, address)
if err != nil {
return nil, err
}
defer conn.Close()
os, err := abi.GetOwners(nil)
if err != nil {
return nil, err
}
return os, nil
}
func VerifyHolderKey(public string) error {
_, err := ParseEthereumCompressedPublicKey(public)
return err
}
func VerifyMessageSignature(public string, msg, sig []byte) error {
hash, err := HashMessageForSignature(hex.EncodeToString(msg))
if err != nil {
return err
}
return VerifyHashSignature(public, hash, sig)
}
func VerifyHashSignature(public string, hash, sig []byte) error {
pub, err := hex.DecodeString(public)
if err != nil {
panic(public)
}
signed := crypto.VerifySignature(pub, hash, sig[:64])
if signed {
return nil
}
return fmt.Errorf("crypto.VerifySignature(%s, %x, %x)", public, hash, sig)
}
func ParseEthereumCompressedPublicKey(public string) (*common.Address, error) {
pub, err := hex.DecodeString(public)
if err != nil {
return nil, err
}
publicKey, err := crypto.DecompressPubkey(pub)
if err != nil {
return nil, err
}
addr := crypto.PubkeyToAddress(*publicKey)
return &addr, nil
}
func ParseEthereumUncompressedPublicKey(public string) (*common.Address, error) {
xPub, _ := hdkeychain.NewKeyFromString(public)
ecPub, _ := xPub.ECPubKey()
pub := ecPub.SerializeCompressed()
publicKey, err := crypto.DecompressPubkey(pub)
if err != nil {
return nil, err
}
addr := crypto.PubkeyToAddress(*publicKey)
return &addr, nil
}