From bba9a094347bba843cfc94844748374d3e3c21ad Mon Sep 17 00:00:00 2001 From: Lee Smet Date: Mon, 10 Apr 2023 17:37:26 +0200 Subject: [PATCH] Working code Signed-off-by: Lee Smet --- go.mod | 22 +++++++ go.sum | 39 ++++++++++++ main.go | 189 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 250 insertions(+) create mode 100644 go.mod create mode 100644 go.sum create mode 100644 main.go diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..366d45d --- /dev/null +++ b/go.mod @@ -0,0 +1,22 @@ +module github.com/leesmet/signer + +go 1.20 + +require ( + github.com/davecgh/go-spew v1.1.1 // indirect + github.com/go-chi/chi v4.0.3+incompatible // indirect + github.com/go-errors/errors v0.0.0-20150906023321-a41850380601 // indirect + github.com/gorilla/schema v1.1.0 // indirect + github.com/konsorten/go-windows-terminal-sequences v1.0.1 // indirect + github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 // indirect + github.com/pkg/errors v0.9.1 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect + github.com/segmentio/go-loggly v0.5.1-0.20171222203950-eb91657e62b2 // indirect + github.com/sirupsen/logrus v1.4.1 // indirect + github.com/stellar/go v0.0.0-20230407073130-0b918125049c // indirect + github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee // indirect + github.com/stretchr/objx v0.3.0 // indirect + github.com/stretchr/testify v1.7.0 // indirect + golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect + gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..b5ebfe5 --- /dev/null +++ b/go.sum @@ -0,0 +1,39 @@ +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-chi/chi v4.0.3+incompatible h1:gakN3pDJnzZN5jqFV2TEdF66rTfKeITyR8qu6ekICEY= +github.com/go-chi/chi v4.0.3+incompatible/go.mod h1:eB3wogJHnLi3x/kFX2A+IbTBlXxmMeXJVKy9tTv1XzQ= +github.com/go-errors/errors v0.0.0-20150906023321-a41850380601 h1:jxTbmDuqQUTI6MscgbqB39vtxGfr2fi61nYIcFQUnlE= +github.com/go-errors/errors v0.0.0-20150906023321-a41850380601/go.mod h1:f4zRHt4oKfwPJE5k8C9vpYG+aDHdBFUsgrm6/TyX73Q= +github.com/gorilla/schema v1.1.0 h1:CamqUDOFUBqzrvxuz2vEwo8+SUdwsluFh7IlzJh30LY= +github.com/gorilla/schema v1.1.0/go.mod h1:kgLaKoK1FELgZqMAVxx/5cbj0kT+57qxUrAlIO2eleU= +github.com/konsorten/go-windows-terminal-sequences v1.0.1 h1:mweAR1A6xJ3oS2pRaGiHgQ4OO8tzTaLawm8vnODuwDk= +github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ= +github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739 h1:ykXz+pRRTibcSjG1yRhpdSHInF8yZY/mfn+Rz2Nd1rE= +github.com/manucorporat/sse v0.0.0-20160126180136-ee05b128a739/go.mod h1:zUx1mhth20V3VKgL5jbd1BSQcW4Fy6Qs4PZvQwRFwzM= +github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= +github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/segmentio/go-loggly v0.5.1-0.20171222203950-eb91657e62b2 h1:S4OC0+OBKz6mJnzuHioeEat74PuQ4Sgvbf8eus695sc= +github.com/segmentio/go-loggly v0.5.1-0.20171222203950-eb91657e62b2/go.mod h1:8zLRYR5npGjaOXgPSKat5+oOh+UHd8OdbS18iqX9F6Y= +github.com/sirupsen/logrus v1.4.1 h1:GL2rEmy6nsikmW0r8opw9JIRScdMF5hA8cOYLH7In1k= +github.com/sirupsen/logrus v1.4.1/go.mod h1:ni0Sbl8bgC9z8RoU9G6nDWqqs/fq4eDPysMBDgk/93Q= +github.com/stellar/go v0.0.0-20230407073130-0b918125049c h1:Sxfmay34A1CCUB4ty8rTbptFuy0PB1KPj30CheQOOc8= +github.com/stellar/go v0.0.0-20230407073130-0b918125049c/go.mod h1:DHmAo7QjGEJa0yef6NXOh+083h/S6OsdRhOwav6Om1A= +github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee h1:fbVs0xmXpBvVS4GBeiRmAE3Le70ofAqFMch1GTiq/e8= +github.com/stellar/go-xdr v0.0.0-20211103144802-8017fc4bdfee/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/objx v0.3.0 h1:NGXK3lHquSN08v5vWalVI/L8XU9hdzE/G6xsrze47As= +github.com/stretchr/objx v0.3.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE= +github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY= +github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= +golang.org/x/sys v0.0.0-20180905080454-ebe1bf3edb33/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 h1:nhht2DYV/Sn3qOayu8lM+cU1ii9sTLUeBQwQQfUHtrs= +golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/main.go b/main.go new file mode 100644 index 0000000..4b61b4a --- /dev/null +++ b/main.go @@ -0,0 +1,189 @@ +package main + +import ( + "bufio" + "encoding/hex" + "flag" + "fmt" + "io" + "os" + "strings" + "time" + + "github.com/pkg/errors" + "github.com/stellar/go/clients/horizonclient" + "github.com/stellar/go/keypair" + "github.com/stellar/go/network" + "github.com/stellar/go/txnbuild" +) + +var ( + inputFile string + outputFile string + preview bool + submit bool + walletsecret string +) + +func init() { + flag.StringVar(&inputFile, "inputfile", "payouts_to_sign.txt", "File with transactions to sign") + flag.StringVar(&outputFile, "outputfile", "payouts_signed.txt", "File to place signed transactions") + flag.BoolVar(&preview, "preview", false, "Print transactions before signing") + flag.BoolVar(&submit, "submit", false, "Submit transactions to the network after signing") + flag.StringVar(&walletsecret, "wallet-secret", "", "Secret key of the wallet to sign the transactions with") +} + +func main() { + flag.Parse() + + if walletsecret == "" { + panic("wallet secret is required") + } + + wallet, err := newWallet(walletsecret) + if err != nil { + panic("wallet secret is not valid") + } + + horizon := horizonclient.DefaultPublicNetClient + + infile, err := os.Open(inputFile) + if err != nil { + panic(fmt.Sprintf("Failed to open payouts input file %s", err)) + } + reader := bufio.NewReader(infile) + defer infile.Close() + + outFile, err := os.Create(outputFile) + if err != nil { + panic(fmt.Sprintf("Failed to open payouts output file %s", err)) + } + defer outFile.Close() + defer outFile.Sync() + + for { + line, err := reader.ReadString('\n') + if err != nil { + if errors.Is(err, io.EOF) { + break + } + panic(err) + } + if line == "" { + break + } + + xdr := strings.TrimSpace(line) + + txn := &txnbuild.Transaction{} + if err = txn.UnmarshalText([]byte(xdr)); err != nil { + panic("Cant unmarshal transaction") + } + + if err = basicValidation(txn); err != nil { + panic("transaction validation failed") + } + + if preview { + previewTxn(txn) + continue + } + + txn, err = wallet.Sign(txn) + if err != nil { + panic("could not sign transaction") + } + + if submit { + for { + previewTxn(txn) + _, err := horizon.SubmitTransaction(txn) + if err != nil { + if err, ok := err.(*horizonclient.Error); ok { + // Horizon timeout + if err.Response.StatusCode == 504 { + fmt.Println("Horizon timeout, retry request in 15 seconds") + time.Sleep(time.Second * 15) + continue + } else if err.Response.StatusCode == 404 { + fmt.Println("ERROR: Account does not exist", err) + break + } + if resCodes, ok := err.Problem.Extras["result_codes"]; ok { + if resMap, ok := resCodes.(map[string]string); ok { + if v, exists := resMap["operations"]; exists { + if strings.Contains(v, "tx_insufficient_fee") { + fmt.Println("Tx insufficient fee, retry in 30 seconds") + time.Sleep(time.Second * 30) + continue + } else if strings.Contains(v, "op_no_destination") { + fmt.Println("ERROR: Account does not exsit") + break + } + } + + } + } + fmt.Println(err) + time.Sleep(time.Second * 60) + continue + + } + fmt.Println("ERROR", err) + time.Sleep(time.Second * 60) + continue + } + break + } + } else { + xdr, err := txn.MarshalText() + if err != nil { + panic(err) + } + outFile.WriteString(string(xdr)) + outFile.WriteString("\n") + } + } + +} + +func basicValidation(txn *txnbuild.Transaction) error { + if _, ok := txn.Memo().(txnbuild.MemoHash); !ok { + return errors.New("memo must be of type memo_hash") + } + if len(txn.Operations()) != 1 { + return errors.New("transaction must contain a single operation") + } + if _, ok := txn.Operations()[0].(*txnbuild.Payment); !ok { + return errors.New("transaction operation must be of type Payment") + } + return nil +} + +func previewTxn(txn *txnbuild.Transaction) { + rawMemo := txn.Memo().(txnbuild.MemoHash) + memo := hex.EncodeToString(rawMemo[:]) + sequence := txn.SequenceNumber() + paymentOp := txn.Operations()[0].(*txnbuild.Payment) + asset := paymentOp.Asset + amount := paymentOp.Amount + dest := paymentOp.Destination + sigs := len(txn.Signatures()) + fmt.Printf("Sending %s %s to %s with memo %s (%d signatures, %d seqno)\n", amount, asset, dest, memo, sigs, sequence) +} + +type Wallet struct { + keypair *keypair.Full +} + +func newWallet(privateKey string) (*Wallet, error) { + kp, err := keypair.ParseFull(privateKey) + if err != nil { + return nil, errors.Wrap(err, "could not parse private key") + } + return &Wallet{keypair: kp}, nil +} + +func (w *Wallet) Sign(txn *txnbuild.Transaction) (*txnbuild.Transaction, error) { + return txn.Sign(network.PublicNetworkPassphrase, w.keypair) +}