github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/tools/getxlm/getxlm.go (about) 1 package main 2 3 import ( 4 "crypto/rand" 5 "flag" 6 "fmt" 7 "io" 8 "log" 9 "net/http" 10 "os/exec" 11 12 "github.com/buger/jsonparser" 13 ) 14 15 var howMany int 16 var destination string 17 var bin string 18 var home string 19 20 func main() { 21 flag.IntVar(&howMany, "n", 5, "number of accounts to create to fund base account") 22 flag.StringVar(&destination, "d", "", "destination account") 23 flag.StringVar(&bin, "bin", "/usr/local/bin/keybase", "keybase binary path") 24 flag.StringVar(&home, "home", "/tmp", "home directory") 25 flag.Parse() 26 if howMany <= 0 { 27 flag.PrintDefaults() 28 log.Fatal("n must be > 0") 29 } 30 if destination == "" { 31 flag.PrintDefaults() 32 log.Fatal("destination account required") 33 } 34 35 for i := 0; i < howMany; i++ { 36 _, err := createWalletUser() 37 if err != nil { 38 log.Printf("error creating wallet user: %s", err) 39 continue 40 } 41 } 42 } 43 44 func createStandardUser() (string, error) { 45 logout() 46 buf := make([]byte, 6) 47 if _, err := rand.Read(buf); err != nil { 48 return "", err 49 } 50 username := fmt.Sprintf("ad_%x", buf) 51 cmd := exec.Command(bin, "-home", home, "signup", "-batch", "-username", username, "-passphrase", "00000000", "-no-email", "-invite-code", "202020202020202020202020") 52 if out, err := cmd.CombinedOutput(); err != nil { 53 fmt.Printf("signup error: %s\n", err) 54 fmt.Printf("output: %s\n", string(out)) 55 return "", err 56 } 57 return username, nil 58 } 59 60 func createWalletUser() (string, error) { 61 username, err := createStandardUser() 62 if err != nil { 63 return "", err 64 } 65 66 // accept the disclaimer 67 cmd := exec.Command(bin, "-home", home, "wallet", "api", "-m", `{"method": "setup-wallet"}`) 68 if out, err := cmd.CombinedOutput(); err != nil { 69 fmt.Printf("setup-wallet error: %s\n", err) 70 fmt.Printf("output: %s\n", string(out)) 71 return "", err 72 } 73 74 // get the account id 75 in := fmt.Sprintf("{\"method\": \"lookup\", \"params\": {\"options\": {\"name\": %q}}}", username) 76 cmd = exec.Command(bin, "-home", home, "wallet", "api", "-m", in) 77 out, err := cmd.CombinedOutput() 78 if err != nil { 79 fmt.Printf("lookup error: %s\n", err) 80 return "", err 81 } 82 fmt.Printf("output: %s\n", string(out)) 83 acctID, err := jsonparser.GetString(out, "result", "accountID") 84 if err != nil { 85 return "", err 86 } 87 fmt.Printf("accountID: %s\n", acctID) 88 fu := fmt.Sprintf("https://friendbot.stellar.org/?addr=%s", acctID) 89 resp, err := http.Get(fu) 90 if err != nil { 91 return "", err 92 } 93 defer resp.Body.Close() 94 body, err := io.ReadAll(resp.Body) 95 fmt.Printf("friendbot body: %s\n", string(body)) 96 if err != nil { 97 return "", err 98 } 99 100 // send most of it to destination 101 in = fmt.Sprintf("{\"method\": \"send\", \"params\": {\"options\": {\"recipient\": %q, \"amount\": \"9900\"}}}", destination) 102 cmd = exec.Command(bin, "-home", home, "wallet", "api", "-m", in) 103 out, err = cmd.CombinedOutput() 104 if err != nil { 105 fmt.Printf("wallet send error: %s\n", err) 106 return "", err 107 } 108 fmt.Printf("output: %s\n", string(out)) 109 110 return username, nil 111 } 112 113 func logout() { 114 cmd := exec.Command(bin, "-home", home, "logout") 115 if out, err := cmd.CombinedOutput(); err != nil { 116 fmt.Printf("logout error: %s\n", err) 117 fmt.Printf("output: %s\n", string(out)) 118 } 119 }