Skip to content

Commit

Permalink
refactor: delegate signing to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
humblenginr committed Sep 2, 2024
1 parent 8600889 commit 12a0867
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
3 changes: 1 addition & 2 deletions client/usif/webui/sendtx.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,7 @@ func sign_transaction(wrs *rmtsrv.WebsocketServer)func (w http.ResponseWriter, r
}

// Non-multisig transaction ...
st.Tx2Sign = string(tx.Serialize())

st.Tx2Sign = fmt.Sprintf("%x", tx.Serialize())
msg := rmtcmn.Msg{Type: rmtcmn.SignTransaction, Payload: st}

error := wsjson.Write(context.Background(), wrs.Conn, msg)
Expand Down
2 changes: 1 addition & 1 deletion client/usif/webui/webui.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func SSEHandler(wrs *rmtserver.WebsocketServer) func(w http.ResponseWriter, r *h
flusher.Flush()
case <-ticker.C:
// Send a keep-alive comment to prevent connection timeout
fmt.Fprintf(w, ": keep-alive\n\n")
fmt.Fprintf(w, "data: keep-alive\n\n")
flusher.Flush()
}
}
Expand Down
1 change: 1 addition & 0 deletions client/www/templates/send.html
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
source.onmessage = (event) => {
console.log("OnMessage Called:")
console.log({event: event, data: event.data})
if(event.data == "keep-alive") return
if(confirm("Received a transaction from remote wallet. Do you want to push to the mempool?")){
pushtx(event.data)
}
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
nhooyr.io/websocket v1.8.11 h1:f/qXNc2/3DpoSZkHt1DQu6rj4zGC8JmkkLkWss0MgN0=
nhooyr.io/websocket v1.8.11/go.mod h1:rN9OFWIUwuxg4fR5tELlYC04bXYowCP9GX47ivo2l+c=
Binary file modified remote-wallet/gateway/gateway
Binary file not shown.
15 changes: 3 additions & 12 deletions remote-wallet/gateway/main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package main

import (
"bufio"
"context"
"fmt"
"os"
"strings"
"time"

"github.com/piotrnar/gocoin/remote-wallet/common"
Expand Down Expand Up @@ -54,21 +51,15 @@ func main(){
}
switch msg.Type {
case common.SignTransaction:
reader := bufio.NewReader(os.Stdin)
fmt.Print("Received a request to sign a transaction. Do you want to confirm(yes/no): ")
text, _ := reader.ReadString('\n')
txSignResp := common.Msg{}
if strings.TrimRight(text, "\n") == "no" {
rawHex, err := h.SignTransaction(msg.Payload)
if err != nil {
fmt.Println(err)
txSignResp.Type = common.InternalError
txSignResp.Payload = common.SignTransactionRejectedError()
writer.Write(txSignResp)
continue
}
rawHex, err := h.SignTransaction(msg.Payload)
if err != nil {
fmt.Println(err)
return
}
txSignResp.Type = common.SignedTransactionRawHex
txSignResp.Payload = rawHex
err = writer.Write(txSignResp)
Expand Down
17 changes: 12 additions & 5 deletions remote-wallet/gateway/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"fmt"


"bytes"
"encoding/hex"
"os"
"os/exec"
Expand All @@ -30,7 +29,12 @@ var BalanceFolderName = "balance"
func (h *MsgHandler) createNecessaryFiles(payload common.SignTransactionRequestPayload) error {
// create tx2sign.txt
tx2signFilePath := fmt.Sprintf("%s/%s", h.WalletFolderPath, Tx2SignFileName)
err := os.WriteFile(tx2signFilePath, []byte(payload.Tx2Sign), os.ModePerm)
file, err := os.Create(tx2signFilePath)
if err != nil {
return err
}
fmt.Println("writing string: ", payload.Tx2Sign)
_, err = file.WriteString(payload.Tx2Sign)
if err != nil {
return err
}
Expand Down Expand Up @@ -76,13 +80,16 @@ func(h *MsgHandler) SignTransaction(payload interface{}) (string, error) {
args := parseWalletCommandArgs(p.PayCmd)
// set custom name for generated signed transaction file
args = append(args, "-txfn="+SignedTransactionFileName)
args = append(args, "-prompt")
fmt.Println("printing args, ", args)
cmd := exec.Command(h.WalletBinaryPath, args...)
// set wallet folder as the directory of execution
cmd.Dir = h.WalletFolderPath
// set a buffer as the stdout
fmt.Printf("Running the command: %s\n", p.PayCmd)
out := bytes.NewBuffer(make([]byte, 0))
cmd.Stdout = out
fmt.Printf("Running the command: %s\n", cmd.Path)
// out := bytes.NewBuffer(make([]byte, 0))
cmd.Stdout = os.Stdout
cmd.Stdin = os.Stdin
err = cmd.Run()
if err != nil {
return "", err
Expand Down

0 comments on commit 12a0867

Please sign in to comment.