Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ipfs): Add coreapi create methods
- Loading branch information
Showing
8 changed files
with
879 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package ipfsutil | ||
|
||
import ( | ||
"context" | ||
"crypto/rand" | ||
"encoding/base64" | ||
|
||
ipfs_datastore "github.com/ipfs/go-datastore" | ||
ipfs_datastoresync "github.com/ipfs/go-datastore/sync" | ||
ipfs_cfg "github.com/ipfs/go-ipfs-config" | ||
ipfs_core "github.com/ipfs/go-ipfs/core" | ||
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi" | ||
ipfs_node "github.com/ipfs/go-ipfs/core/node" | ||
ipfs_libp2p "github.com/ipfs/go-ipfs/core/node/libp2p" | ||
ipfs_repo "github.com/ipfs/go-ipfs/repo" | ||
ipfs_interface "github.com/ipfs/interface-go-ipfs-core" | ||
|
||
libp2p_ci "github.com/libp2p/go-libp2p-crypto" // nolint:staticcheck | ||
libp2p_peer "github.com/libp2p/go-libp2p-peer" // nolint:staticcheck | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
func NewInMemoryCoreAPI(ctx context.Context) (ipfs_interface.CoreAPI, error) { | ||
cfg, err := createBuildConfig() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create ipfs build config") | ||
} | ||
|
||
node, err := ipfs_core.NewNode(ctx, cfg) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create a new ipfs node") | ||
} | ||
|
||
return ipfs_coreapi.NewCoreAPI(node) | ||
} | ||
|
||
func createBuildConfig() (*ipfs_node.BuildCfg, error) { | ||
ds := ipfs_datastore.NewMapDatastore() | ||
repo, err := createRepo(ipfs_datastoresync.MutexWrap(ds)) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create ipfs repo") | ||
} | ||
|
||
routing := ipfs_libp2p.DHTOption | ||
hostopts := ipfs_libp2p.DefaultHostOption | ||
return &ipfs_node.BuildCfg{ | ||
Online: true, | ||
Permanent: true, | ||
DisableEncryptedConnections: false, | ||
NilRepo: false, | ||
Routing: routing, | ||
Host: hostopts, | ||
Repo: repo, | ||
}, nil | ||
} | ||
|
||
func createRepo(dstore ipfs_repo.Datastore) (ipfs_repo.Repo, error) { | ||
c := ipfs_cfg.Config{} | ||
priv, pub, err := libp2p_ci.GenerateKeyPairWithReader(libp2p_ci.RSA, 2048, rand.Reader) // nolint:staticcheck | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to create ipfs build config") | ||
} | ||
|
||
pid, err := libp2p_peer.IDFromPublicKey(pub) // nolint:staticcheck | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to convert public key to PeerID") | ||
} | ||
|
||
privkeyb, err := priv.Bytes() | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to get serialized private key") | ||
} | ||
|
||
c.Bootstrap = ipfs_cfg.DefaultBootstrapAddresses | ||
c.Addresses.Swarm = []string{ | ||
"/ip4/0.0.0.0/tcp/4001", | ||
"/ip6/0.0.0.0/tcp/4001", | ||
} | ||
c.Identity.PeerID = pid.Pretty() | ||
c.Identity.PrivKey = base64.StdEncoding.EncodeToString(privkeyb) | ||
|
||
return &ipfs_repo.Mock{ | ||
D: dstore, | ||
C: c, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package ipfsutil | ||
|
||
import ( | ||
"context" | ||
|
||
ipfs_core "github.com/ipfs/go-ipfs/core" | ||
ipfs_coreapi "github.com/ipfs/go-ipfs/core/coreapi" | ||
ipfs_mock "github.com/ipfs/go-ipfs/core/mock" | ||
ipfs_interface "github.com/ipfs/interface-go-ipfs-core" | ||
|
||
libp2p_mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
type MockedCoreAPI interface { | ||
ipfs_interface.CoreAPI | ||
|
||
MockNetwork() libp2p_mocknet.Mocknet | ||
MockNode() *ipfs_core.IpfsNode | ||
} | ||
|
||
func NewMockCoreAPI(ctx context.Context) (MockedCoreAPI, error) { | ||
mocknet := libp2p_mocknet.New(ctx) | ||
node, err := ipfs_core.NewNode(ctx, &ipfs_core.BuildCfg{ | ||
Online: true, | ||
Host: ipfs_mock.MockHostOption(mocknet), | ||
ExtraOpts: map[string]bool{ | ||
"pubsub": true, | ||
}, | ||
}) | ||
|
||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to mocked ipfs node") | ||
} | ||
|
||
coreapi, err := ipfs_coreapi.NewCoreAPI(node) | ||
if err != nil { | ||
return nil, errors.Wrap(err, "failed to init coreapi") | ||
} | ||
|
||
return &mockedCoreAPI{coreapi, mocknet, node}, nil | ||
} | ||
|
||
type mockedCoreAPI struct { | ||
ipfs_interface.CoreAPI | ||
|
||
mocknet libp2p_mocknet.Mocknet | ||
node *ipfs_core.IpfsNode | ||
} | ||
|
||
func (m *mockedCoreAPI) MockNetwork() libp2p_mocknet.Mocknet { | ||
return m.mocknet | ||
} | ||
|
||
func (m *mockedCoreAPI) MockNode() *ipfs_core.IpfsNode { | ||
return m.node | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters