-
Notifications
You must be signed in to change notification settings - Fork 58
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add examples #1
Comments
I'm not sure I wanted to commit this but since I did, it might be useful until I write a better example: https://github.com/hsanjuan/ipfs-lite/blob/d2191f217f81879baf5e5d09d51d5233628ac430/test/ipfs-lite.go |
@hsanjuan The example is giving me the following output
What's the error message being caused by? |
hey @postables that's probably because some of the default bootstrap peers are offline or don't work when it tries to connect to them. Maybe worth reducing this to a warning... however it does connect correctly to other bootstrap peers and as you see it was able to fetch the content from somewhere. |
ah okay that makes sense, thanks! |
Getting
What is going wrong here? Can the code be changed so that the default bootstrap peer list from https://docs.ipfs.io/guides/examples/bootstrap/ is used? |
Hi, the problem is libp2p was upgraded to only allow 2048 rsa keys and the default bootstrappers use 1024 ones. You can either:
I'm going to update the example with the first fix though... |
Maybe it would be a good idea to extend the example to also write (add) something to IPFS.
|
Line-breaks?
…-------- Original Message --------
On Oct 28, 2019, 21:13, probonopd wrote:
Maybe it would be a good idea to extend the example to also write (add) something to IPFS.
But wait, why are we getting a different CID for the "Hello World"?
package main
// This example launches an IPFS-Lite peer, adds a hello-world to
// and fetches a hello-world hash from the IPFS network.
import (
"context"
"fmt"
"io/ioutil"
"strings"
ipfslite "github.com/hsanjuan/ipfs-lite"
"github.com/ipfs/go-cid"
corecrypto "github.com/libp2p/go-libp2p-core/crypto"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/multiformats/go-multiaddr"
)
func main() {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// Bootstrappers are using 1024 keys. See:
// ipfs/infra#378
corecrypto.MinRsaKeyBits = 1024
ds, err := ipfslite.BadgerDatastore("test")
if err != nil {
panic(err)
}
priv, _, err := crypto.GenerateKeyPair(crypto.RSA, 2048)
if err != nil {
panic(err)
}
listen, _ := multiaddr.NewMultiaddr("/ip4/0.0.0.0/tcp/4005")
h, dht, err := ipfslite.SetupLibp2p(
ctx,
priv,
nil,
[]multiaddr.Multiaddr{listen},
)
if err != nil {
panic(err)
}
lite, err := ipfslite.New(ctx, ds, h, dht, nil)
if err != nil {
panic(err)
}
lite.Bootstrap(ipfslite.DefaultBootstrapPeers())
fmt.Println("Adding file")
n, err := lite.AddFile(ctx, strings.NewReader("Hello World"), nil)
if err != nil {
panic(err)
}
fmt.Println(n.Cid())
// Why don't we get "QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u"?
fmt.Println("")
cc := "QmWATWQ7fVPP2EFGu71UkfnqhYXDYH566qy47CnJDgvs8u"
fmt.Println("Getting", cc)
c, _ := cid.Decode(cc)
rsc, err := lite.GetFile(ctx, c)
if err != nil {
panic(err)
}
defer rsc.Close()
content, err := ioutil.ReadAll(rsc)
if err != nil {
panic(err)
}
fmt.Println(string(content))
}
—
You are receiving this because you were mentioned.
Reply to this email directly, [view it on GitHub](#1?email_source=notifications&email_token=AAH2XTQ3ESJEMRX5GKIEXVDQQ5BWFA5CNFSM4HAOEN2KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOECOHQ2I#issuecomment-547125353), or [unsubscribe](https://github.com/notifications/unsubscribe-auth/AAH2XTTT5C7JVVTL4YOPHE3QQ5BWFANCNFSM4HAOEN2A).
|
To answer my own question, we don't get a "Qm..." hash because there is apparently a new has format now that does not begin with "Qm...". Follow-up question: |
@probonopd ah, yes I guess it uses CIDv1 by default, instead of v0.
You need to keep the peer (the example) running then.. I think adding |
Can you please post a working example? This does not seem to work for me:
|
@probonopd that seems enough, but mind that your nodes needs to be online for a while to become well connected and should be reachable by others etc. |
Unfortunately it did not seem to work for me. I also cannot access the file from the browser on localhost. |
what if you manually connect your local ipfs daemon to The example does not run the mDNS discovery service so it can only be reached from dht and that may not work very well for an spontaneous peer (and the ports should be open, maybe even extra addresses should be added to the peerstore etc). |
Do I need to have a local ipfs daemon running in addition to my Go program? (I want to embed p2p file serving into my Go program, without the need for additional software to be installed on the user's machine.) |
When you said you cannot access the file from the browser on localhost I assumed you were using the local gateway provided by a running ipfs daemon. You mean you were using the You don't need to have the local daemon, but it's a way to test, for example, what the Go program peer is reporting as addresses, whether it is reachable from an external IP etc. |
I was trying And is there a way to see the "health" of my program's connection to the ipfs network? |
Ah no, that port is not an http endpoint, it's a libp2p endpoint. What you were trying to access is a gateway, which ipfs-lite does not provide.
No.. you need to keep your node running, and when the gateway looks up for the hash in the DHT it will eventually find that your node is a provider and try to connect to it on the addresses that the node is reporting. A good question here is what addresses is the example node reporting and I'm not sure. It may not be reporting any external address. Normally, if you want to use your host properly, you enable a bunch of things like NAT hole punching and autodiscovery. You can peek through here for an example on how initialization of a host with autonat, circuit etc is done: https://github.com/ipfs/ipfs-cluster/blob/e713969a806f6a60171e00a405060b827ba8c84c/clusterhost.go I will try to improve the example (or the helper function) to do all this, but I'll need a couple of days for that... |
Ah, I was hoping that this would be done automagically by IPFS, like ipfs-desktop seemingly does (because using it I was able to host files successfully in no time).
That will be much appreciated. Thanks for the great work you are doing 👍 |
Yes, so the difference here is that ipfs-lite is the bare minimum and lets you provide your own preconfigured libp2p host, which you may use for a bunch of other things. I'd say that how the libp2p host is setup is a bit out of the scope of this library, but you're right that it should help a bit more (since it does provide helpers and tries to be useful). Note that you could also run a full go-ipfs, with ALL the things the daemon running behind ipfs-desktop would provide. In fact, instructions were added recently: https://github.com/ipfs/go-ipfs/tree/master/docs/examples/go-ipfs-as-a-library |
@probonopd I added a variable |
Thank you. However it is still not working for me; is there a way to increase verbosity? I can download but seemingly not upload/seed. |
Can you share your peer address, and the hash of the content you are seeding? |
How can I know my peer address? |
The address should be something like /ip4//tcp/4005/p2p/<whatever h.ID() prints> |
Updated #1 (comment), this is the output:
|
What is the public ip? |
This project looks promising :)
It would be nice to add some examples to how to use it
The text was updated successfully, but these errors were encountered: