Skip to content

Commit

Permalink
feat: working QR code generated
Browse files Browse the repository at this point in the history
  • Loading branch information
HashMapsData2Value committed Oct 23, 2024
1 parent b451328 commit f9d3fde
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 28 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ bin
go.work
go.work.sum

# IDE
.vscode/

# env file
.env

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.22.0
toolchain go1.23.1

require (
github.com/algonode/algourl v0.0.0-20241019213625-74154582e3d5
github.com/algorandfoundation/algourl v0.0.0-20241023193235-8bbf72ad0b37
github.com/charmbracelet/bubbles v0.20.0
github.com/charmbracelet/bubbletea v1.1.1
github.com/charmbracelet/lipgloss v0.13.0
Expand All @@ -20,7 +20,6 @@ require (
require gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c // indirect

require (
github.com/algorand/go-algorand-sdk v1.24.0
github.com/algorand/go-algorand-sdk/v2 v2.6.0
github.com/algorand/go-codec/codec v1.1.10 // indirect
github.com/algorandfoundation/go-tinyqr v0.0.0-20241018103413-2082a3d637eb // indirect
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
github.com/RaveNoX/go-jsoncommentstrip v1.0.0/go.mod h1:78ihd09MekBnJnxpICcwzCMzGrKSKYe4AqU6PDYYpjk=
github.com/algonode/algourl v0.0.0-20241019213625-74154582e3d5 h1:OXuvUO08ZCvwLsUdiGgs/fBn51Hynoe+zsERunzCP6k=
github.com/algonode/algourl v0.0.0-20241019213625-74154582e3d5/go.mod h1:ge/QtZi9BNjnp/WFOsiyX3OeJgHzxMNr1PIz+If2aAM=
github.com/algorand/go-algorand-sdk v1.24.0 h1:mi8vqjXMC5nU87snq4vxHi+NgPR0thtZHRLA16FKZMM=
github.com/algorand/go-algorand-sdk v1.24.0/go.mod h1:WEeJcctOHMzDFTgVJ6GT8BLUo9DbFTT47S+Kzx7ffXQ=
github.com/algorand/go-algorand-sdk/v2 v2.6.0 h1:pfL8lloEi26l6PwAFicmPUguWgKpy1eZZTMlQcci5h0=
github.com/algorand/go-algorand-sdk/v2 v2.6.0/go.mod h1:4ayerzjoWChm3kuVhbgFgURTbaYTtlj0c41eP3av5lw=
github.com/algorand/go-codec/codec v1.1.10 h1:zmWYU1cp64jQVTOG8Tw8wa+k0VfwgXIPbnDfiVa+5QA=
github.com/algorand/go-codec/codec v1.1.10/go.mod h1:YkEx5nmr/zuCeaDYOIhlDg92Lxju8tj2d2NrYqP7g7k=
github.com/algorandfoundation/algourl v0.0.0-20241023193235-8bbf72ad0b37 h1:zt00SDlTcFiYPvq+Wo/ZDmRwM2gBRW/VhOQ/zaplBAk=
github.com/algorandfoundation/algourl v0.0.0-20241023193235-8bbf72ad0b37/go.mod h1:o91gtX3JDwJpS10P5cm6dwUsArtNtX+3kDshhelHTJ8=
github.com/algorandfoundation/go-tinyqr v0.0.0-20241018103413-2082a3d637eb h1:NFKjd5BKatUpYUXiofxvWd0VqVoinsVzugtQw/W+vKs=
github.com/algorandfoundation/go-tinyqr v0.0.0-20241018103413-2082a3d637eb/go.mod h1:j4+scGl0Oz2rw+bnoPxfmvregASzMapI5ydPrGzTDBU=
github.com/apapsch/go-jsonmerge/v2 v2.0.0 h1:axGnT1gRIfimI7gJifB699GoE/oq+F2MU7Dml6nw9rQ=
Expand Down
46 changes: 26 additions & 20 deletions ui/pages/transaction/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ package transaction
import (
"context"

encoder "github.com/algonode/algourl/encoder"
msgpack "github.com/algorand/go-algorand-sdk/encoding/msgpack"
types "github.com/algorand/go-algorand-sdk/v2/types"
"github.com/algorand/go-algorand-sdk/v2/types"
"github.com/algorandfoundation/algourl/encoder"
"github.com/algorandfoundation/hack-tui/api"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
Expand All @@ -19,8 +18,9 @@ func (m ViewModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
return m.HandleMessage(msg)
}

func (m ViewModel) UpdateTxnURLAndQRCode() error {
func (m *ViewModel) UpdateTxnURLAndQRCode() error {

// Get Online Status of Account
var format api.AccountInformationParamsFormat = "json"
r, err := m.Client.AccountInformationWithResponse(
context.Background(),
Expand All @@ -33,29 +33,30 @@ func (m ViewModel) UpdateTxnURLAndQRCode() error {
return err
}

goOnline := false
isOnline := false
if r.JSON200.Status == "Online" {
goOnline = true
isOnline = true
}

// Construct Transaction
tx := types.Transaction{}

senderAddress, err := types.DecodeAddress(m.Data.Address)
if err != nil {
return err
}

if goOnline {
if isOnline { // TX take account online
var stateProofPk types.MerkleVerifier
copy(stateProofPk[:], (*m.Data.Key.StateProofKey)[:])

tx = types.Transaction{
Type: types.KeyRegistrationTx,
Header: types.Header{
Sender: senderAddress,
Fee: 0,
FirstValid: types.Round(*m.Data.EffectiveFirstValid),
LastValid: types.Round(*m.Data.EffectiveLastValid),
Sender: senderAddress,
Fee: 0, //TODO: get proper fee
//FirstValid: types.Round(*m.Data.EffectiveFirstValid),
//LastValid: types.Round(*m.Data.EffectiveLastValid),
GenesisHash: types.Digest(m.NetworkParams.GenesisHash),
GenesisID: m.NetworkParams.Network,
},
Expand All @@ -69,22 +70,26 @@ func (m ViewModel) UpdateTxnURLAndQRCode() error {
},
}

} else {
} else { // TX to take account offline
tx = types.Transaction{
Type: types.KeyRegistrationTx,
Header: types.Header{
Sender: senderAddress,
Fee: 0,
FirstValid: types.Round(*m.Data.EffectiveFirstValid),
LastValid: types.Round(*m.Data.EffectiveLastValid),
Sender: senderAddress,
Fee: 0, //TODO: get proper fee
//FirstValid: types.Round(*m.Data.EffectiveFirstValid), //TODO: Determine if this is needed
//LastValid: types.Round(*m.Data.EffectiveLastValid),
GenesisHash: types.Digest(m.NetworkParams.GenesisHash),
GenesisID: m.NetworkParams.Network,
},
}
}

encodedTxn := msgpack.Encode(tx)
kr, err := encoder.MakeQRKeyRegRequest(encodedTxn)
// Construct QR Code

kr, err := encoder.MakeQRKeyRegRequest(
encoder.RawTxn{
Txn: tx,
})

if err != nil {
return err
Expand All @@ -106,8 +111,9 @@ func (m ViewModel) HandleMessage(msg tea.Msg) (ViewModel, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
// When the participation key updates, set the models data
case api.ParticipationKey:
m.Data = msg

case *api.ParticipationKey:
m.Data = *msg

err := m.UpdateTxnURLAndQRCode()
if err != nil {
Expand Down
17 changes: 16 additions & 1 deletion ui/pages/transaction/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type ViewModel struct {
// Participation Key
Data api.ParticipationKey

// Network/Genesis id and Genesis Hash
// Genesis ID and Genesis Hash
NetworkParams NetworkParameters

// client is the API client
Expand All @@ -42,12 +42,27 @@ type ViewModel struct {

// New creates and instance of the ViewModel with a default controls.Model
func New(state *internal.StateModel, client *api.ClientWithResponses) ViewModel {

// // Open the file
// file, err := os.Open("utx.bytes")
// if err != nil {
// fmt.Printf("Error opening file: %v\n", err)
// panic(err)
// }
// defer file.Close()

// encodedTxn, err := io.ReadAll(file)
// kr, err := encoder.MakeQRKeyRegRequest(encodedTxn)
// qrCode, err := kr.ProduceQRCode()

return ViewModel{
Client: client,
NetworkParams: NetworkParameters{
Network: state.Status.Network,
GenesisHash: state.Status.GenesisHash,
},
// urlTxn: kr.String(),
// asciiQR: qrCode,
controls: controls.New(" (a)ccounts | (k)eys | " + green.Render("(t)xn ")),
}
}
10 changes: 9 additions & 1 deletion ui/pages/transaction/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,21 @@ func (m ViewModel) View() string {
Foreground(lipgloss.Color("15")).
Background(lipgloss.Color("0"))

urlStyle := lipgloss.NewStyle().
Width(m.Width - 2)

red := lipgloss.NewStyle().Foreground(lipgloss.Color("9"))

if m.asciiQR == "" || m.urlTxn == "" {
return lipgloss.JoinVertical(
lipgloss.Left,
"No QR Code available",
m.asciiQR,
m.urlTxn,
"No QR Code or TxnURL available",
"\n",
m.controls.View())
}

if m.QRWontFit {
return lipgloss.JoinVertical(
lipgloss.Left,
Expand All @@ -26,6 +33,7 @@ func (m ViewModel) View() string {
}
return lipgloss.JoinVertical(
lipgloss.Left,
urlStyle.Render(m.urlTxn),
qrStyle.Render(m.asciiQR),
m.controls.View())
}

0 comments on commit f9d3fde

Please sign in to comment.