ipld / frisbii

An experimental minimal IPLD data provider for the IPFS network
Other
14 stars 2 forks source link

How to configure persisted private key (provider identity) #132

Closed bajtos closed 2 months ago

bajtos commented 2 months ago

By default, Frisbii generates a new identity (a new private key) at every startup.

I can see there is code to load the identity from a file:

https://github.com/ipld/frisbii/blob/80d5db6673925d896cc1aef8925066a9845f67d3/internal/util/util.go#L89-L116

What is the exact file path of the key file, and how can I generate its content?

//cc @rvagg

rvagg commented 2 months ago

confDir is ~/.frisbii. If you look below LoadPrivKey you'll see ConfigDir which figures out where that is. Currently no way to override that, but it wouldn't be hard.

So, if ~/.frisbii/key exists then it'll just use that.

I just happen to have some code in my local frisbii repo that I used to make a vanity peerID; you can either use this to just make a new one from scratch or modify it to make your own vanity peerID. go run pid.go and let it run until you see one you're happy with then kill it. The output has peerID,privKeyHex per line. Take the hex and write it as binary to ~/.frisbii/key (echo "68656c6c6f" | xxd -r -p > ~/.frisbii/key) and it should be good to go.

import (
        "crypto/rand"
        "encoding/hex"
        "fmt"
        "strings"
        "sync"
        "sync/atomic"

        crypto "github.com/libp2p/go-libp2p/core/crypto"
        peer "github.com/libp2p/go-libp2p/core/peer"
)

func main() {
        var rounds int64
        // start 6 goroutines
        var wg sync.WaitGroup
        for i := 0; i < 6; i++ {
                wg.Add(1)
                go func() {
                        defer wg.Done()
                        for {
                                atomic.AddInt64(&rounds, 1)
                                privKey, _, err := crypto.GenerateEd25519Key(rand.Reader)
                                if err != nil {
                                        panic(err)
                                }
                                id, err := peer.IDFromPrivateKey(privKey)
                                if err != nil {
                                        panic(err)
                                }
                                if isGood(id.String()) {
                                        data, err := crypto.MarshalPrivateKey(privKey)
                                        if err != nil {
                                                panic(err)
                                        }
                                        fmt.Printf("%s,%s\n", id.String(), hex.EncodeToString(data))
                                }
                        }
                }()
        }
        wg.Wait()
}

func isGood(idstr string) bool {
        return strings.HasSuffix(idstr, "rvagg")
}
bajtos commented 2 months ago

Thank you, @rvagg, this was very helpful!

I needed to run few more commands to compile your code snippet, I am describing them below for future readers:

go mod init
go get github.com/libp2p/go-libp2p/core/crypto
go get github.com/libp2p/go-libp2p/core/crypto

It would be awesome if the Frisbii CLI could accept the private key via an environment variable (see The Twelve-Factor App >> Config).

I figured out how to use the filesystem-based config in our Docker+Fly.io deployment (see https://github.com/filecoin-station/frisbii-on-fly/pull/2), so we don't need the env-based config anymore. 🤷🏻