diff --git a/.gitignore b/.gitignore index 6d5abbc..4691172 100644 --- a/.gitignore +++ b/.gitignore @@ -13,5 +13,8 @@ /GUI/Front/.idea /GUI/Front/*.go *.css +*.js +*.html test/ +*.map diff --git a/GUI/App/Background/tracker.go b/GUI/App/Background/tracker.go index d79b74d..619e24a 100644 --- a/GUI/App/Background/tracker.go +++ b/GUI/App/Background/tracker.go @@ -6,24 +6,29 @@ import ( "github.com/brokenbydefault/Nanollet/Storage" "github.com/brokenbydefault/Nanollet/Numbers" "github.com/brokenbydefault/Nanollet/Node" - "net" "github.com/brokenbydefault/Nanollet/Node/Packets" "time" + "github.com/brokenbydefault/Nanollet/Node/Peer" ) var Connection Node.Node func init() { - Connection = &Node.Server{ - Peers: &Storage.PeerStorage, - Transactions: &Storage.TransactionStorage, - Header: Storage.Configuration.Node.Header, - PublishHandler: PublishHandler, + Connection = Node.NewServer( + Storage.Configuration.Node.Header, + &Storage.PeerStorage, + &Storage.TransactionStorage, + ) + + Handler := Node.NewHandler(Connection) + Handler.PublishHandler = PublishHandler + + if err := Handler.Start(); err != nil { + panic(err) } - go Connection.Start() } -func PublishHandler(srv *Node.Server, _ *net.UDPAddr, rHeader *Packets.Header, msg []byte) { +func PublishHandler(node Node.Node, _ *Peer.Peer, rHeader *Packets.Header, msg []byte) { packet := new(Packets.PushPackage) if err := packet.Decode(rHeader, msg); err != nil { @@ -40,7 +45,7 @@ func PublishHandler(srv *Node.Server, _ *net.UDPAddr, rHeader *Packets.Header, m return } - srv.Transactions.Add(packet.Transaction) + node.Transactions().Add(packet.Transaction) } func StartAddress(w *DOM.Window) error { diff --git a/GUI/App/DOM/change.go b/GUI/App/DOM/change.go index 94828a1..bd00721 100644 --- a/GUI/App/DOM/change.go +++ b/GUI/App/DOM/change.go @@ -1,5 +1,3 @@ -// +build !js - package DOM import ( diff --git a/GUI/App/DOM/get.go b/GUI/App/DOM/get.go index 6c40d4e..108d0cb 100644 --- a/GUI/App/DOM/get.go +++ b/GUI/App/DOM/get.go @@ -2,6 +2,12 @@ package DOM +import ( + "bytes" + "os" + "io" +) + func (el *Element) GetAttr(name string) (result string, err error) { return el.el.Attr(name) } @@ -55,3 +61,36 @@ func (dom *DOM) GetBytesValueOf(css string) (result []byte, err error) { return input.GetBytesValue() } +func (el *Element) GetFile() (io.Reader, error) { + input, err := el.GetStringValue() + if err != nil || input == "" { + return nil, ErrInvalidElement + } + + file, err := os.Open(input[7:]) + if err != nil { + return nil, err + } + + defer file.Close() + + if stat, err := file.Stat(); err != nil || stat.IsDir() { + return nil, ErrInvalidElement + } + + r := bytes.NewBuffer(nil) + if _, err := io.Copy(r, file); err != nil { + return nil, err + } + + return r, nil +} + +func (dom *DOM) GetFileOf(css string) (io.Reader, error) { + input, err := dom.SelectFirstElement(css) + if err != nil { + return nil, err + } + + return input.GetFile() +} diff --git a/GUI/App/DOM/get_js.go b/GUI/App/DOM/get_js.go index 5786ad7..19c11a2 100644 --- a/GUI/App/DOM/get_js.go +++ b/GUI/App/DOM/get_js.go @@ -5,6 +5,9 @@ package DOM import ( "honnef.co/go/js/dom" "strings" + "io" + "bytes" + "github.com/gopherjs/gopherjs/js" ) func (el *Element) GetAttr(name string) (result string, err error) { @@ -25,11 +28,20 @@ func (el *Element) GetText() (result string, err error) { } func (el *Element) GetStringValue() (result string, err error) { - if strings.ToUpper(el.el.TagName()) == "TEXTAREA" { - return el.el.(*dom.HTMLTextAreaElement).Value, nil + if e, ok := el.el.(*dom.HTMLTextAreaElement); ok { + return e.Value, nil } - return el.el.(*dom.HTMLInputElement).Value, nil + e, ok := el.el.(*dom.HTMLInputElement) + if !ok { + return "", nil + } + + if t := strings.ToUpper(e.Type); (t == "CHECKBOX" || t == "OPTION") && !e.Checked { + return "", nil + } + + return e.Value, nil } func (dom *DOM) GetStringValueOf(css string) (result string, err error) { @@ -54,3 +66,28 @@ func (dom *DOM) GetBytesValueOf(css string) (result []byte, err error) { return input.GetBytesValue() } + +func (el *Element) GetFile() (io.Reader, error) { + input, ok := el.el.(*dom.HTMLInputElement) + if !ok { + return nil, ErrInvalidElement + } + + var b = make(chan io.Reader) + fileReader := js.Global.Get("FileReader").New() + fileReader.Set("onload", func() { + b <- bytes.NewReader(js.Global.Get("Uint8Array").New(fileReader.Get("result")).Interface().([]byte)) + }) + fileReader.Call("readAsArrayBuffer", input.Files()[0].Object) + + return <-b, nil +} + +func (dom *DOM) GetFileOf(css string) (io.Reader, error) { + input, err := dom.SelectFirstElement(css) + if err != nil { + return nil, err + } + + return input.GetFile() +} diff --git a/GUI/App/DOM/init.go b/GUI/App/DOM/init.go index e2da26f..6e27a4c 100644 --- a/GUI/App/DOM/init.go +++ b/GUI/App/DOM/init.go @@ -9,7 +9,7 @@ func (w *Window) InitApplication(app Application) { w.StartApplication(app) if !app.HaveSidebar() { - button, err := w.root.SelectFirstElement(`.control button[id="`+ app.Name() +`"]`) + button, err := w.root.SelectFirstElement(`.control button[id="`+ strings.Title(app.Name()) +`"]`) if err == nil { DestroyHTML(button.el) } @@ -17,9 +17,9 @@ func (w *Window) InitApplication(app Application) { return } - button, err := w.root.SelectFirstElement(`.control button[id="`+ app.Name() +`"]`) + button, err := w.root.SelectFirstElement(`.control button[id="`+ strings.Title(app.Name()) +`"]`) if err != nil { - panic(fmt.Sprintf("element %s was not found", `.control button[id="`+ app.Name() +`"]`)) + panic(fmt.Sprintf("element %s was not found", `.control button[id="`+ strings.Title(app.Name()) +`"]`)) } button.On(Click, func(class string) { @@ -52,7 +52,7 @@ func (w *Window) ViewApplication(app Application) error { el.SetAttr("class", "") } - w.root.ApplyForAll(".application#"+app.Name()+" button", ShowElement) + w.root.ApplyForAll(".application#"+strings.Title(app.Name())+" button", ShowElement) return w.ViewPage(app.Pages()[0]) } diff --git a/GUI/App/DOM/modifier_js.go b/GUI/App/DOM/modifier_js.go index c4d2f95..e4761c4 100644 --- a/GUI/App/DOM/modifier_js.go +++ b/GUI/App/DOM/modifier_js.go @@ -19,6 +19,9 @@ func (el *Element) SetText(text string) error { } func (el *Element) SetValue(value string) error { + if _, ok := el.el.(*dom.HTMLTextAreaElement); ok { + el.SetText(value) + } el.el.SetNodeValue(value) return nil } diff --git a/GUI/App/account.go b/GUI/App/account.go index 05efc95..983e136 100644 --- a/GUI/App/account.go +++ b/GUI/App/account.go @@ -1,5 +1,3 @@ -// +build !js - package App import ( @@ -99,7 +97,7 @@ func (c *PageGenerate) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { panic(err) } - Storage.PermanentStorage.AddSeedFY(sf) + Storage.PersistentStorage.AddSeedFY(sf) w.ViewPage(new(PagePassword)) } @@ -131,7 +129,7 @@ func (c *PageImport) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { return } - Storage.PermanentStorage.AddSeedFY(sf) + Storage.PersistentStorage.AddSeedFY(sf) w.ViewPage(new(PagePassword)) dom.ApplyFor(".seed", DOM.ClearValue) @@ -154,7 +152,7 @@ func (c *PagePassword) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { return } - seedfy := Storage.PermanentStorage.SeedFY + seedfy := Storage.PersistentStorage.SeedFY need2FA, err := dom.GetStringValueOf(".ask2fa") if err == nil && need2FA != "" { @@ -177,7 +175,10 @@ func (c *PageMFA) Name() string { func (c *PageMFA) OnView(w *DOM.Window, dom *DOM.DOM) { sk := Ephemeral.NewEphemeral() - requester, response := TwoFactor.NewRequesterServer(&sk, Storage.PermanentStorage.AllowedKeys) + requester, response, err := TwoFactor.NewRequesterServer(&sk, Storage.PersistentStorage.AllowedKeys) + if err != nil { + panic(err) + } qr, err := requester.QRCode(300, color.RGBA{220, 220, 223, 1}) if err != nil { @@ -191,7 +192,7 @@ func (c *PageMFA) OnView(w *DOM.Window, dom *DOM.DOM) { go func() { for resp := range response { //@TODO Notify the user to allow or not the key - Storage.PermanentStorage.AddAllowedKey(resp.Capsule.Device) + Storage.PersistentStorage.AddAllowedKey(resp.Capsule.Device) Storage.AccessStorage.Token = resp.Capsule.Token c.OnContinue(w, dom, "") @@ -207,7 +208,7 @@ func (c *PageMFA) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { return } - seedfy := Storage.PermanentStorage.SeedFY + seedfy := Storage.PersistentStorage.SeedFY Storage.AccessStorage.Seed = seedfy.RecoverSeed(Storage.AccessStorage.Password, Storage.AccessStorage.Token[:]) copy(Storage.AccessStorage.Token[:], make([]byte, len(Storage.AccessStorage.Token))) diff --git a/GUI/App/nanofy.go b/GUI/App/nanofy.go index 9e441a4..12731ef 100644 --- a/GUI/App/nanofy.go +++ b/GUI/App/nanofy.go @@ -1,5 +1,3 @@ -// +build !js - package App import ( @@ -8,7 +6,6 @@ import ( "github.com/brokenbydefault/Nanollet/GUI/App/DOM" "github.com/brokenbydefault/Nanollet/Storage" "github.com/brokenbydefault/Nanollet/Wallet" - "os" "github.com/brokenbydefault/Nanollet/Node" "github.com/brokenbydefault/Nanollet/Block" ) @@ -41,23 +38,12 @@ func (c *PageSign) OnView(w *DOM.Window, dom *DOM.DOM) { } func (c *PageSign) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { - filePath, err := dom.GetStringValueOf(".filepath") - if filePath == "" || err != nil { - return - } - - file, err := os.Open(filePath[7:]) + file, err := dom.GetFileOf(".filepath") if err != nil { DOM.UpdateNotification(w, "There was a problem opening the file") return } - stats, err := file.Stat() - if err != nil || stats.IsDir() { - DOM.UpdateNotification(w, "There was a problem opening the file") - return - } - previous, ok := Storage.TransactionStorage.GetByHash(&Storage.AccountStorage.Frontier) if !ok { DOM.UpdateNotification(w, "Previous block not found") @@ -102,10 +88,8 @@ func (c *PageVerify) OnView(w *DOM.Window, dom *DOM.DOM) { } func (c *PageVerify) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { - addr, _ := dom.GetStringValueOf(".address") - filePath, err := dom.GetStringValueOf(".filepath") - if addr == "" || filePath == "" || err != nil { + if addr == "" { return } @@ -115,18 +99,12 @@ func (c *PageVerify) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { return } - file, err := os.Open(filePath[7:]) + file, err := dom.GetFileOf(".filepath") if err != nil { DOM.UpdateNotification(w, "There was a problem opening the file") return } - stats, err := file.Stat() - if err != nil || stats.IsDir() { - DOM.UpdateNotification(w, "Only files can be signed") - return - } - txs, err := Node.GetHistory(Background.Connection, &pk, nil) if err != nil { DOM.UpdateNotification(w, "There was a problem retrieving the information") diff --git a/GUI/App/nanollet.go b/GUI/App/nanollet.go index 706c1f6..dda32e9 100644 --- a/GUI/App/nanollet.go +++ b/GUI/App/nanollet.go @@ -1,5 +1,3 @@ -// +build !js - package App import ( @@ -70,20 +68,19 @@ func (c *PageWallet) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { var dest Wallet.PublicKey switch { case Wallet.Address(addrOrAlias).IsValid(): - if dest, err = Wallet.Address(addrOrAlias).GetPublicKey(); err != nil { - DOM.UpdateNotification(w, "The address is wrong") - return - } + dest, err = Wallet.Address(addrOrAlias).GetPublicKey() case OpenCAP.Address(addrOrAlias).IsValid(): - if dest, err = OpenCAP.Address(addrOrAlias).GetPublicKey(); err != nil { - DOM.UpdateNotification(w, "The address was not found") - return - } + dest, err = OpenCAP.Address(addrOrAlias).GetPublicKey() default: DOM.UpdateNotification(w, "The address invalid or it's not supported") return } + if err != nil { + DOM.UpdateNotification(w, "The address is wrong or was not found") + return + } + if !Util.StringIsNumeric(whole) || !Util.StringIsNumeric(decimal) { DOM.UpdateNotification(w, "The given amount is incorrect") return @@ -179,6 +176,11 @@ func (c *PageRepresentative) OnContinue(w *DOM.Window, dom *DOM.DOM, _ string) { return } + if Util.IsEmpty(Storage.AccountStorage.Frontier[:]) { + Storage.AccountStorage.Representative = representative + return + } + blk, err := Block.CreateUniversalChangeBlock(&Storage.AccountStorage.SecretKey, representative, Storage.AccountStorage.Balance, Storage.AccountStorage.Frontier) if err != nil { DOM.UpdateNotification(w, "There was a problem creating a block") diff --git a/GUI/App/settings.go b/GUI/App/settings.go index f1ac7db..04a3541 100644 --- a/GUI/App/settings.go +++ b/GUI/App/settings.go @@ -34,7 +34,7 @@ func (c *PageSeed) OnView(w *DOM.Window, dom *DOM.DOM) { return } - seedbox.SetValue(Storage.PermanentStorage.SeedFY.String()) + seedbox.SetValue(Storage.PersistentStorage.SeedFY.String()) } func (c *PageSeed) OnContinue(w *DOM.Window, dom *DOM.DOM, action string) { diff --git a/GUI/Front/html/0_base.html b/GUI/Front/html/0_base.html index 713f03c..4290fa2 100644 --- a/GUI/Front/html/0_base.html +++ b/GUI/Front/html/0_base.html @@ -3,7 +3,7 @@