This repository has been archived by the owner on Mar 28, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 283
/
core.go
87 lines (72 loc) · 2.4 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
package core
import (
"path"
"github.com/OpenBazaar/openbazaar-go/repo"
"github.com/ipfs/go-ipfs/core"
"github.com/ipfs/go-ipfs/commands"
"github.com/OpenBazaar/openbazaar-go/net"
"github.com/OpenBazaar/openbazaar-go/ipfs"
"github.com/OpenBazaar/openbazaar-go/bitcoin"
"github.com/op/go-logging"
"gx/ipfs/QmbyvM8zRFDkbFdYyt1MnevUMJ62SiSGbfDFZ3Z8nkrzr4/go-libp2p-peer"
"github.com/OpenBazaar/go-ipfs/Godeps/_workspace/src/github.com/syndtr/goleveldb/leveldb/errors"
)
var log = logging.MustGetLogger("core")
var Node *OpenBazaarNode
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 publically 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 net.OfflineMessagingStorage
// TODO: Libsignal Client
// TODO: Pointer Republisher
}
// Unpin the current node repo, re-add it, then publish to ipns
func (n *OpenBazaarNode) SeedNode() error {
if err := ipfs.UnPinDir(n.Context, n.RootHash); err != nil {
return err
}
hash, aerr := ipfs.AddDirectory(n.Context, path.Join(n.RepoPath, "root"))
if aerr != nil {
return aerr
}
_, perr := ipfs.Publish(n.Context, hash)
if perr != nil {
return perr
}
n.RootHash = hash
return nil
}
// 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) {
defer func() {
if r := recover(); r != nil {
ct = nil
log.Warningf("Could not find public key for %s", peerId.Pretty())
rerr = errors.New("Could not find public key for peer")
}
}()
pubKey := n.IpfsNode.Peerstore.PubKey(peerId)
ciphertext, err := pubKey.Encrypt(message)
if err != nil {
return nil, err
}
return ciphertext, nil
}