Use elements RPC library (#225)

* Use elements RPC library

// Closes #218

Signed-off-by: Julian Strobl <jmastr@mailbox.org>
This commit is contained in:
Julian Strobl 2023-12-12 12:14:19 +01:00 committed by GitHub
parent 2b19b8194b
commit 7b381f3313
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 56 deletions

2
go.mod
View File

@ -21,7 +21,7 @@ require (
github.com/grpc-ecosystem/grpc-gateway v1.16.0 github.com/grpc-ecosystem/grpc-gateway v1.16.0
github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2 github.com/grpc-ecosystem/grpc-gateway/v2 v2.15.2
github.com/planetmint/planetmint-go/lib v0.2.1 github.com/planetmint/planetmint-go/lib v0.2.1
github.com/rddl-network/elements-rpc v0.2.0 github.com/rddl-network/elements-rpc v0.3.0
github.com/spf13/cast v1.5.0 github.com/spf13/cast v1.5.0
github.com/spf13/cobra v1.6.1 github.com/spf13/cobra v1.6.1
github.com/spf13/pflag v1.0.5 github.com/spf13/pflag v1.0.5

4
go.sum
View File

@ -889,8 +889,8 @@ github.com/rakyll/statik v0.1.7/go.mod h1:AlZONWzMtEnMs7W4e/1LURLiI49pIMmp6V9Ung
github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rcrowley/go-metrics v0.0.0-20181016184325-3113b8401b8a/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 h1:N/ElC8H3+5XpJzTSTfLsJV/mx9Q9g7kxmchpfZyxgzM=
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4= github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqnVzQkZxMG4s8nGwiZ5l3QUCyqpo9Y+/ZMZ9VjZe4=
github.com/rddl-network/elements-rpc v0.2.0 h1:xZCbNZaeVYO2gVnwYUF25/aPKcCYcOwjUZM0hb1C/xI= github.com/rddl-network/elements-rpc v0.3.0 h1:riPjQWy/aUMeDnOPpRgMB8ddCVmvQX7XVWBYCi/BZ5Q=
github.com/rddl-network/elements-rpc v0.2.0/go.mod h1:WOSYDMhq+V74lReSInnSejbdEyGI8hiQZSn4cSoFuxo= github.com/rddl-network/elements-rpc v0.3.0/go.mod h1:WOSYDMhq+V74lReSInnSejbdEyGI8hiQZSn4cSoFuxo=
github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg= github.com/rogpeppe/fastuuid v0.0.0-20150106093220-6724a57986af/go.mod h1:XWv6SoW27p1b0cqNHllgS5HIMJraePCO15w5zCzIWYg=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ= github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4=

View File

@ -1,70 +1,40 @@
package util package util
import ( import (
"bytes"
"encoding/json"
"errors"
"os/exec"
"strconv"
"strings" "strings"
"github.com/planetmint/planetmint-go/config" "github.com/planetmint/planetmint-go/config"
elements "github.com/rddl-network/elements-rpc"
) )
type ReissueResult struct { func ReissueAsset(reissueTx string) (txID string, err error) {
Txid string `json:"txid"`
Vin int `json:"vin"`
}
func ReissueAsset(reissueTx string) (txid string, err error) {
conf := config.GetConfig() conf := config.GetConfig()
url := conf.GetRPCURL()
cmdArgs := strings.Split(reissueTx, " ") cmdArgs := strings.Split(reissueTx, " ")
cmd := exec.Command("/usr/local/bin/elements-cli", "-rpcpassword="+conf.RPCPassword,
"-rpcuser="+conf.RPCUser, "-rpcport="+strconv.Itoa(conf.RPCPort), "-rpcconnect="+conf.RPCHost,
cmdArgs[0], cmdArgs[1], cmdArgs[2])
var stdout, stderr bytes.Buffer result, err := elements.ReissueAsset(url, []string{cmdArgs[1], cmdArgs[2]})
cmd.Stdout = &stdout if err != nil {
cmd.Stderr = &stderr return
err = cmd.Run()
errstr := stderr.String()
if err != nil || len(errstr) > 0 {
err = errors.New("reissuance of RDDL failed: " + errstr)
} else {
var txobj ReissueResult
err = json.Unmarshal(stdout.Bytes(), &txobj)
if err == nil {
txid = txobj.Txid
} }
} txID = result.TxID
return txid, err return
} }
func DistributeAsset(address string, amount string) (txid string, err error) { func DistributeAsset(address string, amount string) (txID string, err error) {
conf := config.GetConfig() conf := config.GetConfig()
cmd := exec.Command("/usr/local/bin/elements-cli", "-rpcpassword="+conf.RPCPassword, url := conf.GetRPCURL()
"-rpcuser="+conf.RPCUser, "-rpcport="+strconv.Itoa(conf.RPCPort), "-rpcconnect="+conf.RPCHost,
"sendtoaddress", address, amount, "", "", "false", "true", "null", "\"unset\"", "false",
"\""+conf.ReissuanceAsset+"\"")
var stdout, stderr bytes.Buffer txID, err = elements.SendToAddress(url, []string{
cmd.Stdout = &stdout address,
cmd.Stderr = &stderr `"` + amount + `"`,
`""`,
err = cmd.Run() `""`,
errstr := stderr.String() "false",
"true",
if err != nil || len(errstr) > 0 { "null",
errormessage := "distribution of RDDL failed for " + address `"unset"`,
err = errors.New(errormessage) "false",
} else { `"` + conf.ReissuanceAsset + `"`,
var txobj ReissueResult })
err = json.Unmarshal(stdout.Bytes(), &txobj) return
if err == nil {
txid = txobj.Txid
}
}
return txid, err
} }