github.com/keybase/client/go@v0.0.0-20240309051027-028f7c731f8b/tools/sep7uri/sep7uri.go (about)

     1  package main
     2  
     3  import (
     4  	"flag"
     5  	"fmt"
     6  	"net/url"
     7  	"os"
     8  
     9  	"github.com/keybase/stellarnet"
    10  )
    11  
    12  var destination string
    13  var amount string
    14  var assetCode string
    15  var assetIssuer string
    16  var key string
    17  var domain string
    18  var message string
    19  var xdr string
    20  var memo string
    21  var memoType string
    22  var callbackURL string
    23  
    24  func main() {
    25  	parseFlags()
    26  	uri := run()
    27  	fmt.Println(uri)
    28  }
    29  
    30  func parseFlags() {
    31  	flag.StringVar(&destination, "to", "", "destination stellar address")
    32  	flag.StringVar(&destination, "t", "", "destination stellar address (shorthand)")
    33  	flag.StringVar(&amount, "amount", "", "number of XLM to send to destination")
    34  	flag.StringVar(&amount, "a", "", "number of XLM to send to destination (shorthand)")
    35  	flag.StringVar(&key, "key", "", "secret stellar key for signing")
    36  	flag.StringVar(&key, "k", "", "secret stellar key for signing (shorthand)")
    37  	flag.StringVar(&domain, "domain", "", "origin domain for the request")
    38  	flag.StringVar(&domain, "d", "", "origin domain for the request (shorthand)")
    39  	flag.StringVar(&message, "message", "", "message to include")
    40  	flag.StringVar(&message, "m", "", "message to include (shorthand)")
    41  	flag.StringVar(&xdr, "xdr", "", "base64-encoded xdr transaction envelope")
    42  	flag.StringVar(&memo, "memo", "", "public memo")
    43  	flag.StringVar(&memoType, "memo-type", "", "MEMO_TEXT, MEMO_ID, MEMO_HASH, MEMO_RETURN")
    44  	flag.StringVar(&assetCode, "asset-code", "", "destination asset code")
    45  	flag.StringVar(&assetIssuer, "asset-issuer", "", "destination asset issuer")
    46  	flag.StringVar(&callbackURL, "callback-url", "", "callback URL")
    47  
    48  	flag.Parse()
    49  
    50  	if xdr != "" {
    51  		if destination != "" {
    52  			fmt.Fprintln(os.Stderr, "cannot specify xdr and destination")
    53  			flag.PrintDefaults()
    54  			os.Exit(1)
    55  		}
    56  		if amount != "" {
    57  			fmt.Fprintln(os.Stderr, "cannot specify xdr and amount")
    58  			flag.PrintDefaults()
    59  			os.Exit(1)
    60  		}
    61  	} else if destination == "" {
    62  		fmt.Fprintln(os.Stderr, "destination or xdr is required")
    63  		flag.PrintDefaults()
    64  		os.Exit(1)
    65  	}
    66  
    67  	if key == "" || domain == "" {
    68  		fmt.Fprintln(os.Stderr, "key and domain are required")
    69  		flag.PrintDefaults()
    70  		os.Exit(1)
    71  	}
    72  
    73  	if memo != "" && memoType == "" {
    74  		memoType = "MEMO_TEXT"
    75  	}
    76  }
    77  
    78  func run() string {
    79  	var op string
    80  	if xdr != "" {
    81  		op = "tx"
    82  	} else {
    83  		op = "pay"
    84  	}
    85  
    86  	u, err := url.Parse("web+stellar:" + op)
    87  	if err != nil {
    88  		fmt.Fprintln(os.Stderr, err)
    89  		os.Exit(1)
    90  	}
    91  	q := u.Query()
    92  	q.Set("origin_domain", domain)
    93  	if message != "" {
    94  		q.Set("msg", message)
    95  	}
    96  
    97  	switch op {
    98  	case "pay":
    99  		q.Set("destination", destination)
   100  		if amount != "" {
   101  			q.Set("amount", amount)
   102  		}
   103  		if memo != "" {
   104  			q.Set("memo", memo)
   105  			q.Set("memo_type", memoType)
   106  		}
   107  		if assetCode != "" && assetIssuer != "" {
   108  			q.Set("asset_code", assetCode)
   109  			q.Set("asset_issuer", assetIssuer)
   110  		}
   111  	case "tx":
   112  		q.Set("xdr", xdr)
   113  	}
   114  
   115  	if callbackURL != "" {
   116  		q.Set("callback", "url:"+callbackURL)
   117  	}
   118  
   119  	u.RawQuery = q.Encode()
   120  
   121  	seed, err := stellarnet.NewSeedStr(key)
   122  	if err != nil {
   123  		fmt.Fprintln(os.Stderr, err)
   124  		os.Exit(1)
   125  	}
   126  
   127  	signed, _, err := stellarnet.SignStellarURI(u.String(), seed)
   128  	if err != nil {
   129  		fmt.Fprintln(os.Stderr, err)
   130  		os.Exit(1)
   131  	}
   132  	return signed
   133  }