Last active
June 11, 2019 16:54
-
-
Save NSkelsey/2a976105f72ef37d742e to your computer and use it in GitHub Desktop.
Periodic Pointcoin sender
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"github.com/PointCoin/btcnet" | |
"github.com/PointCoin/btcrpcclient" | |
"github.com/PointCoin/btcutil" | |
"github.com/PointCoin/btcwire" | |
"time" | |
) | |
var firstAddr = flag.String("from", "", "The first addr to send to.") | |
var secondAddr = flag.String("to", "", "The second addr to send to.") | |
var wait = flag.Int("wait", 30, "The seconds between each transaction.") | |
const ( | |
// This should match your settings in pointcoind.conf | |
rpcuser = "alexander" | |
rpcpass = "kuck" | |
// This file should exist if pointcoind was setup correctly | |
cert = "/root/.wallet/rpc.cert" | |
) | |
func decodeAddr(s string) (btcutil.Address, error) { | |
addr, err := btcutil.DecodeAddress(s, &btcnet.MainNetParams) | |
if err != nil { | |
return nil, err | |
} | |
return addr, nil | |
} | |
// setupRpcClient handles establishing a connection to the pointcoind using | |
// the provided parameters. This function will cease with an error if pointcoind or wallet | |
// are not running. | |
func setupRpcClient(cfile string, rpcuser string, rpcpass string) *btcrpcclient.Client { | |
// Get the raw bytes of the certificate required by the rpcclient. | |
cert, err := ioutil.ReadFile(cfile) | |
if err != nil { | |
s := fmt.Sprintf("setupRpcClient failed with: %s\n", err) | |
log.Fatal(s) | |
} | |
// Setup the RPC client | |
connCfg := &btcrpcclient.ConnConfig{ | |
Host: "127.0.0.1:8332", | |
User: rpcuser, | |
Pass: rpcpass, | |
Certificates: cert, | |
// Use the websocket endpoint to keep the connection alive | |
// in the event we want to do polling. | |
Endpoint: "ws", | |
} | |
client, err := btcrpcclient.New(connCfg, nil) | |
if err != nil { | |
s := fmt.Sprintf("setupRpcClient failed with: %s\n", err) | |
log.Fatal(s) | |
} | |
// Test the connection to see if we can really connect | |
_, err = client.GetInfo() | |
if err != nil { | |
log.Fatal(err) | |
s := fmt.Sprintf("setupRpcClient failed with: %s\n", err) | |
log.Fatal(s) | |
} | |
return client | |
} | |
func main() { | |
flag.Parse() | |
client := setupRpcClient(cert, rpcuser, rpcpass) | |
to, err := decodeAddr(*firstAddr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
from, err := decodeAddr(*secondAddr) | |
if err != nil { | |
log.Fatal(err) | |
} | |
amnt, _ := btcutil.NewAmount(3.1415926) | |
ticker := time.NewTicker(time.Second * time.Duration(*wait)) | |
forward := true | |
for { | |
// Unlock the rpc wallet with the current passpharse for 60 seconds | |
err := client.WalletPassphrase("password", 60) | |
if err != nil { | |
log.Fatal(err) | |
} | |
var resp *btcwire.ShaHash | |
// Send pointcoin either to the 'to' address or back to the 'from' address. | |
if forward { | |
resp, err = client.SendToAddress(to, amnt) | |
} else { | |
resp, err = client.SendToAddress(from, amnt) | |
} | |
if err != nil { | |
log.Println(err) | |
} | |
log.Printf("Sent a tx: %s!\n", resp) | |
forward = !forward | |
// Wait for the ticker to fire. This prevents us from sending too many requests too fast. | |
<-ticker.C | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment