Skip to content
This repository was archived by the owner on Mar 5, 2020. It is now read-only.

Made some improvements for easier developer experience #36

Merged
merged 1 commit into from
Sep 20, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 52 additions & 25 deletions bios/bios.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,40 +57,23 @@ func (b *BIOS) Boot() error {
var genesisData string
var pubKey ecc.PublicKey
var privKey string
if b.ReuseGenesis {
ephemeralPrivateKey, err := readPrivKeyFromFile("genesis.key")
if err != nil {
return err
}

b.EphemeralPrivateKey = ephemeralPrivateKey
pubKey = ephemeralPrivateKey.PublicKey()
b.EphemeralPublicKey = pubKey
privKey = ephemeralPrivateKey.String()
err = b.setEphemeralKeypair()
if err != nil {
return err
}

pubKey = b.EphemeralPublicKey
privKey = b.EphemeralPrivateKey.String()

if b.ReuseGenesis {
genesisData, err = b.LoadGenesisFromFile(pubKey.String())
if err != nil {
return err
}

b.Log.Printf("REUSING previously generated ephemeral keys:\n\n\tPublic key: %s\n\tPrivate key: %s..%s\n\n", pubKey, privKey[:4], privKey[len(privKey)-4:])

} else {
ephemeralPrivateKey, err := b.GenerateEphemeralPrivKey()
if err != nil {
return err
}

b.EphemeralPrivateKey = ephemeralPrivateKey
pubKey = ephemeralPrivateKey.PublicKey()
b.EphemeralPublicKey = pubKey
privKey = ephemeralPrivateKey.String()

// b.TargetNetAPI.Debug = true

genesisData = b.GenerateGenesisJSON(pubKey.String())

b.Log.Printf("Generated ephemeral keys:\n\n\tPublic key: %s\n\tPrivate key: %s..%s\n\n", pubKey, privKey[:4], privKey[len(privKey)-4:])
b.writeToFile("genesis.pub", pubKey.String())
b.writeToFile("genesis.key", privKey)
}
Expand Down Expand Up @@ -171,6 +154,50 @@ func (b *BIOS) Boot() error {
return nil
}

func (b *BIOS) setEphemeralKeypair() error {
if _, ok := b.BootSequence.Keys["ephemeral"]; ok {
cnt := b.BootSequence.Keys["ephemeral"]
privKey, err := ecc.NewPrivateKey(strings.TrimSpace(string(cnt)))
if err != nil {
return fmt.Errorf("unable to correctly decode ephemeral private key %q: %s", cnt, err)
}

b.EphemeralPrivateKey = privKey
b.EphemeralPublicKey = privKey.PublicKey()

b.logEphemeralKey("Using user provider custom ephemeral keys from boot sequence")
} else if b.ReuseGenesis {
genesisPrivateKey, err := readPrivKeyFromFile("genesis.key")
if err != nil {
return err
}

b.EphemeralPrivateKey = genesisPrivateKey
b.EphemeralPublicKey = genesisPrivateKey.PublicKey()

b.logEphemeralKey("REUSING previously generated ephemeral keys from genesis")
} else {
ephemeralPrivateKey, err := b.GenerateEphemeralPrivKey()
if err != nil {
return err
}

b.EphemeralPrivateKey = ephemeralPrivateKey
b.EphemeralPublicKey = ephemeralPrivateKey.PublicKey()

b.logEphemeralKey("Generated ephemeral keys")
}

return nil
}

func (b *BIOS) logEphemeralKey(tag string) {
pubKey := b.EphemeralPublicKey.String()
privKey := b.EphemeralPrivateKey.String()

b.Log.Printf("%s:\n\n\tPublic key: %s\n\tPrivate key: %s..%s\n\n", tag, pubKey, privKey[:4], privKey[len(privKey)-4:])
}

func (b *BIOS) RunChainValidation() (bool, error) {
bootSeqMap := ActionMap{}
bootSeq := []*eos.Action{}
Expand Down
5 changes: 3 additions & 2 deletions bios/bootseq.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

type BootSeq struct {
Contents []*ContentRef `json:"contents"`
BootSequence []*OperationType `json:"boot_sequence"`
Keys map[string]string `json:"keys"`
Contents []*ContentRef `json:"contents"`
BootSequence []*OperationType `json:"boot_sequence"`
}

func ReadBootSeq(filename string) (out *BootSeq, err error) {
Expand Down
54 changes: 43 additions & 11 deletions bios/contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"io/ioutil"
"net/http"
"net/url"
"os"
"path/filepath"

Expand Down Expand Up @@ -45,25 +46,26 @@ func (b *BIOS) ensureCacheExists() error {
}

func (b *BIOS) DownloadURL(ref string, hash string) error {
if b.isInCache(ref) {
//net.Log.Printf("ipfs ref: %q in cache\n", ref)
if hash != "" && b.isInCache(ref) {
return nil
}

b.Log.Printf("Downloading and caching content from %q\n", ref)
cnt, err := b.downloadURL(ref)
cnt, err := b.downloadRef(ref)
if err != nil {
return err
}

h := sha256.New()
_, _ = h.Write(cnt)
contentHash := hex.EncodeToString(h.Sum(nil))
if hash != "" {
h := sha256.New()
_, _ = h.Write(cnt)
contentHash := hex.EncodeToString(h.Sum(nil))

if contentHash != hash {
return fmt.Errorf("hash in boot sequence [%q] not equal to computed hash on downloaded file [%q]", hash, contentHash)
if contentHash != hash {
return fmt.Errorf("hash in boot sequence [%q] not equal to computed hash on downloaded file [%q]", hash, contentHash)
}
}

b.Log.Printf("Caching content from %q.\n", ref)
if err := b.writeToCache(ref, cnt); err != nil {
return err
}
Expand All @@ -73,8 +75,38 @@ func (b *BIOS) DownloadURL(ref string, hash string) error {
return nil
}

func (b *BIOS) downloadURL(destURL string) ([]byte, error) {
req, err := http.NewRequest("GET", destURL, nil)
func (b *BIOS) downloadRef(ref string) ([]byte, error) {
b.Log.Printf("Downloading content from %q.\n", ref)
if _, err := os.Stat(ref); err == nil {
return b.downloadLocalFile(ref)
}

destURL, err := url.Parse(ref)
if err != nil {
return nil, fmt.Errorf("ref %q is not a valid URL: %s", ref, err)
}

switch destURL.Scheme {
case "file":
return b.downloadFileURL(destURL)
case "http", "https":
return b.downloadHTTPURL(destURL)
default:
return nil, fmt.Errorf("don't know how to handle scheme %q (from ref %q)", destURL.Scheme, destURL)
}
}

func (b *BIOS) downloadLocalFile(ref string) ([]byte, error) {
return ioutil.ReadFile(ref)
}

func (b *BIOS) downloadFileURL(destURL *url.URL) ([]byte, error) {
fmt.Printf("Path %s, Raw path: %s\n", destURL.Path, destURL.RawPath)
return []byte{}, nil
}

func (b *BIOS) downloadHTTPURL(destURL *url.URL) ([]byte, error) {
req, err := http.NewRequest("GET", destURL.String(), nil)
if err != nil {
return nil, err
}
Expand Down