From 6de921ccfb0ffb0105f600adf59d86ac89bc2f9e Mon Sep 17 00:00:00 2001 From: banasa44 Date: Tue, 22 Jul 2025 18:13:00 +0200 Subject: [PATCH] feat(samples): add go examples, secure signing and send a memo --- _code-samples/secure-signing/go/go.mod | 19 ++++ _code-samples/secure-signing/go/go.sum | 45 +++++++++ _code-samples/secure-signing/go/main.go | 42 ++++++++ _code-samples/send-a-memo/go/go.mod | 24 +++++ _code-samples/send-a-memo/go/go.sum | 58 +++++++++++ _code-samples/send-a-memo/go/rpc/main.go | 115 +++++++++++++++++++++ _code-samples/send-a-memo/go/ws/main.go | 121 +++++++++++++++++++++++ 7 files changed, 424 insertions(+) create mode 100644 _code-samples/secure-signing/go/go.mod create mode 100644 _code-samples/secure-signing/go/go.sum create mode 100644 _code-samples/secure-signing/go/main.go create mode 100644 _code-samples/send-a-memo/go/go.mod create mode 100644 _code-samples/send-a-memo/go/go.sum create mode 100644 _code-samples/send-a-memo/go/rpc/main.go create mode 100644 _code-samples/send-a-memo/go/ws/main.go diff --git a/_code-samples/secure-signing/go/go.mod b/_code-samples/secure-signing/go/go.mod new file mode 100644 index 0000000000..5bb66b65af --- /dev/null +++ b/_code-samples/secure-signing/go/go.mod @@ -0,0 +1,19 @@ +module github.com/XRPLF + +go 1.23.0 + +toolchain go1.23.10 + +require github.com/Peersyst/xrpl-go v0.1.11 + +require ( + github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect + github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect + github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect + github.com/decred/dcrd/crypto/ripemd160 v1.0.2 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect + github.com/tyler-smith/go-bip32 v1.0.0 // indirect + github.com/tyler-smith/go-bip39 v1.1.0 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/crypto v0.23.0 // indirect +) diff --git a/_code-samples/secure-signing/go/go.sum b/_code-samples/secure-signing/go/go.sum new file mode 100644 index 0000000000..6746307d70 --- /dev/null +++ b/_code-samples/secure-signing/go/go.sum @@ -0,0 +1,45 @@ +github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e h1:ahyvB3q25YnZWly5Gq1ekg6jcmWaGj/vG/MhF4aisoc= +github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUqhHd//musdITWjFvNTHn90WG9bMLBEPQZ17Cmlpw= +github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec h1:1Qb69mGp/UtRPn422BH4/Y4Q3SLUrD9KHuDkm8iodFc= +github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U= +github.com/Peersyst/xrpl-go v0.1.11 h1:P6r/gHxRnbAtAdPmzNHz/7zpsdfvwh0SS+QI2JNT44w= +github.com/Peersyst/xrpl-go v0.1.11/go.mod h1:CBRM3/soqNeeL2Jx6USVUtECqulZVUoq3UxZKMz9hdw= +github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ= +github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= +github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e h1:0XBUw73chJ1VYSsfvcPvVT7auykAJce9FpRr10L6Qhw= +github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:P13beTBKr5Q18lJe1rIoLUqjM+CB1zYrRg44ZqGuQSA= +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/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/crypto/ripemd160 v1.0.2 h1:TvGTmUBHDU75OHro9ojPLK+Yv7gDl2hnUvRocRCjsys= +github.com/decred/dcrd/crypto/ripemd160 v1.0.2/go.mod h1:uGfjDyePSpa75cSQLzNdVmWlbQMBuiJkvXw/MNKRY4M= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +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/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tyler-smith/go-bip32 v1.0.0 h1:sDR9juArbUgX+bO/iblgZnMPeWY1KZMUC2AFUJdv5KE= +github.com/tyler-smith/go-bip32 v1.0.0/go.mod h1:onot+eHknzV4BVPwrzqY5OoVpyCvnwD7lMawL5aQupE= +github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8= +github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/crypto v0.0.0-20170613210332-850760c427c5/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54= +launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM= diff --git a/_code-samples/secure-signing/go/main.go b/_code-samples/secure-signing/go/main.go new file mode 100644 index 0000000000..daca4f3bd8 --- /dev/null +++ b/_code-samples/secure-signing/go/main.go @@ -0,0 +1,42 @@ +package main + +import ( + "fmt" + + "github.com/Peersyst/xrpl-go/pkg/crypto" + "github.com/Peersyst/xrpl-go/xrpl/transaction" + "github.com/Peersyst/xrpl-go/xrpl/transaction/types" + "github.com/Peersyst/xrpl-go/xrpl/wallet" +) + +func main() { + w, err := wallet.New(crypto.ED25519()) + if err != nil { + panic(err) + } + + p := &transaction.Payment{ + BaseTx: transaction.BaseTx{ + Account: types.Address(w.GetAddress()), + Fee: types.XRPCurrencyAmount(13), + Sequence: 1, + Flags: 2147483648, + LastLedgerSequence: 7835923, + }, + Destination: "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe", + Amount: types.XRPCurrencyAmount(10000), + DeliverMax: types.XRPCurrencyAmount(10000), + } + + flattenedTx := p.Flatten() + + fmt.Println("Payment object created:", flattenedTx) + + signedTx, txHash, err := w.Sign(flattenedTx) + if err != nil { + panic(err) + } + + fmt.Println("Transaction signed successfully:", signedTx) + fmt.Println("Transaction hash:", txHash) +} diff --git a/_code-samples/send-a-memo/go/go.mod b/_code-samples/send-a-memo/go/go.mod new file mode 100644 index 0000000000..fa9fde5d08 --- /dev/null +++ b/_code-samples/send-a-memo/go/go.mod @@ -0,0 +1,24 @@ +module github.com/XRPLF + +go 1.23.0 + +toolchain go1.23.10 + +require github.com/Peersyst/xrpl-go v0.1.11 + +require ( + github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e // indirect + github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec // indirect + github.com/btcsuite/btcd/btcec/v2 v2.3.4 // indirect + github.com/decred/dcrd/crypto/ripemd160 v1.0.2 // indirect + github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 // indirect + github.com/gorilla/websocket v1.5.0 // indirect + github.com/json-iterator/go v1.1.12 // indirect + github.com/mitchellh/mapstructure v1.5.0 // indirect + github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect + github.com/modern-go/reflect2 v1.0.2 // indirect + github.com/tyler-smith/go-bip32 v1.0.0 // indirect + github.com/tyler-smith/go-bip39 v1.1.0 // indirect + github.com/ugorji/go/codec v1.2.11 // indirect + golang.org/x/crypto v0.23.0 // indirect +) diff --git a/_code-samples/send-a-memo/go/go.sum b/_code-samples/send-a-memo/go/go.sum new file mode 100644 index 0000000000..8d8522a619 --- /dev/null +++ b/_code-samples/send-a-memo/go/go.sum @@ -0,0 +1,58 @@ +github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e h1:ahyvB3q25YnZWly5Gq1ekg6jcmWaGj/vG/MhF4aisoc= +github.com/FactomProject/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:kGUqhHd//musdITWjFvNTHn90WG9bMLBEPQZ17Cmlpw= +github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec h1:1Qb69mGp/UtRPn422BH4/Y4Q3SLUrD9KHuDkm8iodFc= +github.com/FactomProject/btcutilecc v0.0.0-20130527213604-d3a63a5752ec/go.mod h1:CD8UlnlLDiqb36L110uqiP2iSflVjx9g/3U9hCI4q2U= +github.com/Peersyst/xrpl-go v0.1.11 h1:P6r/gHxRnbAtAdPmzNHz/7zpsdfvwh0SS+QI2JNT44w= +github.com/Peersyst/xrpl-go v0.1.11/go.mod h1:CBRM3/soqNeeL2Jx6USVUtECqulZVUoq3UxZKMz9hdw= +github.com/btcsuite/btcd/btcec/v2 v2.3.4 h1:3EJjcN70HCu/mwqlUsGK8GcNVyLVxFDlWurTXGPFfiQ= +github.com/btcsuite/btcd/btcec/v2 v2.3.4/go.mod h1:zYzJ8etWJQIv1Ogk7OzpWjowwOdXY1W/17j2MW85J04= +github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e h1:0XBUw73chJ1VYSsfvcPvVT7auykAJce9FpRr10L6Qhw= +github.com/cmars/basen v0.0.0-20150613233007-fe3947df716e/go.mod h1:P13beTBKr5Q18lJe1rIoLUqjM+CB1zYrRg44ZqGuQSA= +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/decred/dcrd/crypto/blake256 v1.0.1 h1:7PltbUIQB7u/FfZ39+DGa/ShuMyJ5ilcvdfma9wOH6Y= +github.com/decred/dcrd/crypto/blake256 v1.0.1/go.mod h1:2OfgNZ5wDpcsFmHmCK5gZTPcCXqlm2ArzUIkw9czNJo= +github.com/decred/dcrd/crypto/ripemd160 v1.0.2 h1:TvGTmUBHDU75OHro9ojPLK+Yv7gDl2hnUvRocRCjsys= +github.com/decred/dcrd/crypto/ripemd160 v1.0.2/go.mod h1:uGfjDyePSpa75cSQLzNdVmWlbQMBuiJkvXw/MNKRY4M= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0 h1:rpfIENRNNilwHwZeG5+P150SMrnNEcHYvcCuK6dPZSg= +github.com/decred/dcrd/dcrec/secp256k1/v4 v4.3.0/go.mod h1:v57UDF4pDQJcEfFUCRop3lJL149eHGSe9Jvczhzjo/0= +github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc= +github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs= +github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc= +github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= +github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= +github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= +github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= +github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc= +github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q= +github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M= +github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk= +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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.1.5-0.20170601210322-f6abca593680/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= +github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= +github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= +github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/tyler-smith/go-bip32 v1.0.0 h1:sDR9juArbUgX+bO/iblgZnMPeWY1KZMUC2AFUJdv5KE= +github.com/tyler-smith/go-bip32 v1.0.0/go.mod h1:onot+eHknzV4BVPwrzqY5OoVpyCvnwD7lMawL5aQupE= +github.com/tyler-smith/go-bip39 v1.1.0 h1:5eUemwrMargf3BSLRRCalXT93Ns6pQJIjYQN2nyfOP8= +github.com/tyler-smith/go-bip39 v1.1.0/go.mod h1:gUYDtqQw1JS3ZJ8UWVcGTGqqr6YIN3CWg+kkNaLt55U= +github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU= +github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= +golang.org/x/crypto v0.0.0-20170613210332-850760c427c5/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= +launchpad.net/gocheck v0.0.0-20140225173054-000000000087 h1:Izowp2XBH6Ya6rv+hqbceQyw/gSGoXfH/UPoTGduL54= +launchpad.net/gocheck v0.0.0-20140225173054-000000000087/go.mod h1:hj7XX3B/0A+80Vse0e+BUHsHMTEhd0O4cpUHr/e/BUM= diff --git a/_code-samples/send-a-memo/go/rpc/main.go b/_code-samples/send-a-memo/go/rpc/main.go new file mode 100644 index 0000000000..0842b072bd --- /dev/null +++ b/_code-samples/send-a-memo/go/rpc/main.go @@ -0,0 +1,115 @@ +package main + +import ( + "encoding/hex" + "fmt" + "strconv" + + "github.com/Peersyst/xrpl-go/xrpl/currency" + "github.com/Peersyst/xrpl-go/xrpl/faucet" + "github.com/Peersyst/xrpl-go/xrpl/rpc" + transactions "github.com/Peersyst/xrpl-go/xrpl/transaction" + "github.com/Peersyst/xrpl-go/xrpl/transaction/types" + "github.com/Peersyst/xrpl-go/xrpl/wallet" +) + +func main() { + w, err := wallet.FromSeed("sEdSMVV4dJ1JbdBxmakRR4Puu3XVZz2", "") + if err != nil { + fmt.Println(err) + return + } + + receiverWallet, err := wallet.FromSeed("sEd7d8Ci9nevdLCeUMctF3uGXp9WQqJ", "") + if err != nil { + fmt.Println(err) + return + } + + cfg, err := rpc.NewClientConfig( + "https://s.altnet.rippletest.net:51234/", + rpc.WithFaucetProvider(faucet.NewTestnetFaucetProvider()), + ) + if err != nil { + panic(err) + } + + client := rpc.NewClient(cfg) + + balance, err := client.GetXrpBalance(w.GetAddress()) + + if err != nil || balance == "0" { + fmt.Println("⏳ Funding wallet...") + err = client.FundWallet(&w) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("💸 Wallet funded") + } + + balance, _ = client.GetXrpBalance(w.GetAddress()) + + fmt.Printf("💸 Balance: %s\n", balance) + + amount, err := currency.XrpToDrops("1") + if err != nil { + fmt.Println(err) + return + } + + amountUint, err := strconv.ParseUint(amount, 10, 64) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("⏳ Sending payment...") + payment := transactions.Payment{ + BaseTx: transactions.BaseTx{ + Account: types.Address(w.GetAddress()), + Memos: []types.MemoWrapper{ + { + Memo: types.Memo{ + MemoData: hex.EncodeToString([]byte("Hello, World!")), + MemoFormat: hex.EncodeToString([]byte("plain")), + MemoType: hex.EncodeToString([]byte("message")), + }, + }, + { + Memo: types.Memo{ + MemoData: hex.EncodeToString([]byte("Hello, World 2!")), + MemoFormat: hex.EncodeToString([]byte("text/plain")), + MemoType: hex.EncodeToString([]byte("message2")), + }, + }, + }, + }, + Destination: types.Address(receiverWallet.GetAddress()), + Amount: types.XRPCurrencyAmount(amountUint), + } + + flatTx := payment.Flatten() + + err = client.Autofill(&flatTx) + if err != nil { + fmt.Println(err) + return + } + + txBlob, _, err := w.Sign(flatTx) + if err != nil { + fmt.Println(err) + return + } + + response, err := client.SubmitTxBlobAndWait(txBlob, true) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("✅ Payment submitted") + fmt.Printf("🌐 Hash: %s\n", response.Hash.String()) + fmt.Printf("🌐 Validated: %t\n", response.Validated) +} diff --git a/_code-samples/send-a-memo/go/ws/main.go b/_code-samples/send-a-memo/go/ws/main.go new file mode 100644 index 0000000000..4fdc94f8df --- /dev/null +++ b/_code-samples/send-a-memo/go/ws/main.go @@ -0,0 +1,121 @@ +package main + +import ( + "encoding/hex" + "fmt" + "strconv" + + "github.com/Peersyst/xrpl-go/xrpl/currency" + "github.com/Peersyst/xrpl-go/xrpl/faucet" + transactions "github.com/Peersyst/xrpl-go/xrpl/transaction" + "github.com/Peersyst/xrpl-go/xrpl/transaction/types" + "github.com/Peersyst/xrpl-go/xrpl/wallet" + "github.com/Peersyst/xrpl-go/xrpl/websocket" +) + +func main() { + w, err := wallet.FromSeed("sEdSMVV4dJ1JbdBxmakRR4Puu3XVZz2", "") + if err != nil { + fmt.Println(err) + return + } + + receiverWallet, err := wallet.FromSeed("sEd7d8Ci9nevdLCeUMctF3uGXp9WQqJ", "") + if err != nil { + fmt.Println(err) + return + } + + client := websocket.NewClient( + websocket.NewClientConfig(). + WithHost("wss://s.altnet.rippletest.net:51233"). + WithFaucetProvider(faucet.NewTestnetFaucetProvider()), + ) + defer client.Disconnect() + + fmt.Println("⏳ Connecting to server...") + if err := client.Connect(); err != nil { + fmt.Println(err) + return + } + + fmt.Println("✅ Connected to server") + fmt.Println() + + balance, err := client.GetXrpBalance(w.GetAddress()) + + if err != nil || balance == "0" { + fmt.Println("⏳ Funding wallet...") + err = client.FundWallet(&w) + if err != nil { + fmt.Println(err) + return + } + fmt.Println("💸 Wallet funded") + } + + balance, _ = client.GetXrpBalance(w.GetAddress()) + + fmt.Printf("💸 Balance: %s\n", balance) + + amount, err := currency.XrpToDrops("1") + if err != nil { + fmt.Println(err) + return + } + + amountUint, err := strconv.ParseUint(amount, 10, 64) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("⏳ Sending payment...") + payment := transactions.Payment{ + BaseTx: transactions.BaseTx{ + Account: types.Address(w.GetAddress()), + Memos: []types.MemoWrapper{ + { + Memo: types.Memo{ + MemoData: hex.EncodeToString([]byte("Hello, World!")), + MemoFormat: hex.EncodeToString([]byte("plain")), + MemoType: hex.EncodeToString([]byte("message")), + }, + }, + { + Memo: types.Memo{ + MemoData: hex.EncodeToString([]byte("Hello, World 2!")), + MemoFormat: hex.EncodeToString([]byte("text/plain")), + MemoType: hex.EncodeToString([]byte("message2")), + }, + }, + }, + }, + Destination: types.Address(receiverWallet.GetAddress()), + Amount: types.XRPCurrencyAmount(amountUint), + } + + flatTx := payment.Flatten() + + err = client.Autofill(&flatTx) + if err != nil { + fmt.Println(err) + return + } + + txBlob, _, err := w.Sign(flatTx) + if err != nil { + fmt.Println(err) + return + } + + response, err := client.SubmitTxBlobAndWait(txBlob, true) + if err != nil { + fmt.Println(err) + return + } + + fmt.Println("✅ Payment submitted") + fmt.Printf("🌐 Hash: %s\n", response.Hash.String()) + fmt.Printf("🌐 Validated: %t\n", response.Validated) +}