dappDevelopment

package
v0.3.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 27, 2018 License: MIT Imports: 18 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DAppStream = cli.Command{
	Name:      "dapp:stream",
	ArgsUsage: "[DApp file path] [signing key path]",
	Action: func(c *cli.Context) error {

		dAppFileName := c.Args().Get(0)
		if _, err := os.Stat(dAppFileName); err != nil {
			fmt.Println("DApp file does not exist")
			return nil
		}

		signingKey := c.Args().Get(1)
		if _, err := os.Stat(signingKey); err != nil {
			fmt.Println("signing key does not exist")
			return nil
		}

		content, err := ioutil.ReadFile(signingKey)
		if err != nil {
			panic(err)
		}

		p := pui.Prompt{
			Label: "Enter password for signing key encryption",
			Mask:  '*',
		}
		pw, err := p.Run()
		if err != nil {
			panic(err)
		}
		sk, err := sk.Decrypt(content, []byte(pw))
		if err != nil {
			panic(err)
		}

		h, err := lp2p.New(context.Background(), lp2p.Defaults, config)
		if err != nil {
			panic(err)
		}

		h.SetStreamHandler("/dapp-development/0.0.0", func(stream net.Stream) {

			fmt.Println("got stream")

			writer := bufio.NewWriter(stream)
			reader := bufio.NewReader(stream)
			protoEnc := protoMc.Multicodec(nil).Encoder(writer)
			protoDec := protoMc.Multicodec(nil).Decoder(reader)

			w := fsw.New()
			w.SetMaxEvents(1)
			w.FilterOps(fsw.Write)

			go func() {
				for {
					select {
					case <-w.Event:

						content, err := ioutil.ReadFile(dAppFileName)
						if err != nil {
							panic(err)
							return
						}

						dAppRep := dapp.JsonRepresentation{}
						if err := json.Unmarshal(content, &dAppRep); err != nil {
							panic(err)
							return
						}

						dAppRep.SignaturePublicKey = sk.PublicKey
						hashOfDApp, err := dAppRep.Hash()
						if err != nil {
							panic(err)
						}

						dAppSignature := sk.Sign(hashOfDApp)
						if err != nil {
							panic(err)
						}
						dAppRep.Signature = dAppSignature

						rawDAppRep, err := dAppRep.Marshal()
						if err != nil {
							panic(err)
						}

						err = protoEnc.Encode(&pb.Message{
							Type: pb.Message_DApp,
							DApp: rawDAppRep,
						})
						if err != nil {
							panic(err)
						}

						if err := writer.Flush(); err != nil {
							panic(err)
							return
						}

					case err := <-w.Error:
						fmt.Println(err)
					case <-w.Closed:
						return
					}
				}
			}()

			go func() {

				for {

					msg := pb.Message{}
					if err := protoDec.Decode(&msg); err != nil {
						fmt.Println(err)
					}
					fmt.Println(string(msg.Log))

				}

			}()

			w.Add(dAppFileName)
			w.Start(time.Millisecond * 100)

		})

		for _, addr := range h.Addrs() {
			a := fmt.Sprintf("%s/ipfs/%s", addr.String(), h.ID().Pretty())
			qrterminal.Generate(a, qrterminal.M, os.Stdout)
			fmt.Println(a, "\n\n\n")
		}

		select {}

		return nil
	},
}

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL