-
Notifications
You must be signed in to change notification settings - Fork 0
/
addressbook.go
93 lines (78 loc) · 2.74 KB
/
addressbook.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
// Copyright (c) 2019 Laszlo Sari
//
// FileTribe 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.
//
// FileTribe 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 this program. If not, see <https://www.gnu.org/licenses/>.
//
package common
import (
"github.com/ethereum/go-ethereum/accounts/abi/bind"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/contracts/chequebook"
"github.com/pkg/errors"
"github.com/aliras1/FileTribe/collections"
"github.com/aliras1/FileTribe/eth/gen/Account"
ethapp "github.com/aliras1/FileTribe/eth/gen/FileTribeDApp"
"github.com/aliras1/FileTribe/ipfs"
"github.com/aliras1/FileTribe/tribecrypto"
)
// AddressBook is a cache for fellow users' P2P contact data
type AddressBook struct {
accToContactMap *collections.Map
backend chequebook.Backend
app *ethapp.FileTribeDApp
ipfs ipfs.IIpfs
}
// NewAddressBook creates a new AddressBook
func NewAddressBook(backend chequebook.Backend, app *ethapp.FileTribeDApp, ipfs ipfs.IIpfs) *AddressBook {
return &AddressBook{
backend: backend,
app: app,
ipfs: ipfs,
accToContactMap: collections.NewConcurrentMap(),
}
}
// Get tries to retrieve the P2P contact data of a user
func (a *AddressBook) Get(accAddr ethcommon.Address) (*Contact, error) {
var c *Contact
cInt := a.accToContactMap.Get(accAddr)
if cInt == nil {
_c, err := a.getContactFromEth(accAddr)
if err != nil {
return nil, errors.Wrap(err, "could not get contact")
}
c = _c
} else {
c = cInt.(*Contact)
}
return c, nil
}
func (a *AddressBook) getContactFromEth(accAddr ethcommon.Address) (*Contact, error) {
acc, err := Account.NewAccount(accAddr, a.backend)
if err != nil {
return nil, errors.Wrap(err, "could not create account instance")
}
name, err := acc.Name(&bind.CallOpts{Pending: true})
if err != nil {
return nil, errors.Wrap(err, "could not get account name")
}
ipfsID, err := acc.IpfsId(&bind.CallOpts{Pending: true})
if err != nil {
return nil, errors.Wrap(err, "could not get ipfs peer id")
}
owner, err := acc.Owner(&bind.CallOpts{Pending: true})
if err != nil {
return nil, errors.Wrap(err, "could not get owner")
}
contact := NewContact(owner, accAddr, name, ipfsID, tribecrypto.AnonymPublicKey{}, a.ipfs)
return contact, nil
}