This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
/
core.go
118 lines (97 loc) · 3.32 KB
/
core.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
package core
import (
bstk "github.com/OpenBazaar/go-blockstackclient"
"github.com/OpenBazaar/openbazaar-go/bitcoin"
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/net"
"github.com/OpenBazaar/openbazaar-go/repo"
sto "github.com/OpenBazaar/openbazaar-go/storage"
"github.com/ipfs/go-ipfs/commands"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/routing/dht"
"github.com/op/go-logging"
"golang.org/x/net/context"
"gx/ipfs/QmRBqJF7hb8ZSpRcMwUt8hNhydWcxGEhtk81HKq6oUwKvs/go-libp2p-peer"
"path"
)
var log = logging.MustGetLogger("core")
var salt = []byte("salt")
var encVersion = make([]byte, 4)
var Node *OpenBazaarNode
var inflightPublishRequests int
type OpenBazaarNode struct {
// Context for issuing IPFS commands
Context commands.Context
// IPFS node object
IpfsNode *core.IpfsNode
// The roothash of the node directory inside the openbazaar repo.
// This directory hash is published on IPNS at our peer ID making
// the directory publicly viewable on the network.
RootHash string
// The path to the openbazaar repo in the file system.
RepoPath string
// The OpenBazaar network service for direct communication between peers
Service net.NetworkService
// Database for storing node specific data
Datastore repo.Datastore
// Websocket channel used for pushing data to the UI.
Broadcast chan []byte
// Bitcoin wallet implementation
Wallet bitcoin.BitcoinWallet
// Storage for our outgoing messages
MessageStorage sto.OfflineMessagingStorage
// A service that periodically checks the dht for outstanding messages
MessageRetriever *net.MessageRetriever
// A service that periodically republishes active pointers
PointerRepublisher *net.PointerRepublisher
// Used to resolve blockchainIDs to OpenBazaar IDs
Resolver *bstk.BlockstackClient
// A service that periodically fetches and caches the bitcoin exchange rates
ExchangeRates bitcoin.ExchangeRates
}
// Unpin the current node repo, re-add it, then publish to ipns
func (n *OpenBazaarNode) SeedNode() error {
hash, aerr := ipfs.AddDirectory(n.Context, path.Join(n.RepoPath, "root"))
if aerr != nil {
return aerr
}
go n.publish(hash)
return nil
}
func (n *OpenBazaarNode) publish(hash string) {
if inflightPublishRequests == 0 {
n.Broadcast <- []byte(`{"status": "publishing"}`)
}
var err, perr error
inflightPublishRequests++
_, err = ipfs.Publish(n.Context, hash)
if hash != n.RootHash {
perr = ipfs.UnPinDir(n.Context, n.RootHash)
n.RootHash = hash
}
inflightPublishRequests--
if inflightPublishRequests == 0 {
if err != nil || perr != nil {
log.Error(err, perr)
n.Broadcast <- []byte(`{"status": "error publishing"}`)
} else {
n.Broadcast <- []byte(`{"status": "publish complete"}`)
}
}
}
// This is a placeholder until the libsignal is operational
// For now we will just encrypt outgoing offline messages with the long lived identity key.
func (n *OpenBazaarNode) EncryptMessage(peerId peer.ID, message []byte) (ct []byte, rerr error) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
pubKey, err := n.IpfsNode.Routing.(*dht.IpfsDHT).GetPublicKey(ctx, peerId)
if err != nil {
log.Errorf("Failed to find public key for %s", peerId.Pretty())
return nil, err
}
ciphertext, err := net.Encrypt(pubKey, message)
if err != nil {
return nil, err
}
return ciphertext, nil
}