Live video on the AT Protocol
1package aqpub
2
3import (
4 "crypto/ecdsa"
5 "math/big"
6
7 "github.com/decred/dcrd/dcrec/secp256k1"
8 "github.com/ethereum/go-ethereum/common"
9 "github.com/ethereum/go-ethereum/common/hexutil"
10 "github.com/ethereum/go-ethereum/crypto"
11)
12
13type Pub interface {
14 Address() common.Address
15 String() string
16 Equals(Pub) bool
17}
18
19type pub struct {
20 addr common.Address
21}
22
23func FromHexString(str string) (Pub, error) {
24 bs, err := hexutil.Decode(str)
25 if err != nil {
26 return nil, err
27 }
28 addr := common.BytesToAddress(bs)
29 return &pub{addr}, nil
30}
31
32func FromPublicKey(key *ecdsa.PublicKey) (Pub, error) {
33 addr := crypto.PubkeyToAddress(*key)
34 return &pub{addr}, nil
35}
36
37func FromBytes(bs []byte) (Pub, error) {
38 pubkey, err := secp256k1.ParsePubKey(bs)
39 if err != nil {
40 return nil, err
41 }
42 addr := crypto.PubkeyToAddress(*pubkey.ToECDSA())
43 return &pub{addr}, nil
44}
45
46func FromPoints(x, y *big.Int) (Pub, error) {
47 key := ecdsa.PublicKey{Curve: secp256k1.S256(), X: x, Y: y}
48 return FromPublicKey(&key)
49}
50
51func (p *pub) Address() common.Address {
52 return p.addr
53}
54
55func (p *pub) String() string {
56 addr := p.Address()
57 return hexutil.Encode(addr.Bytes())
58}
59
60func (p *pub) Equals(other Pub) bool {
61 addr1 := p.Address()
62 addr2 := other.Address()
63 return addr1.Cmp(addr2) == 0
64}