2016-02-03 10:37:29 +03:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
2016-02-25 04:27:29 +03:00
|
|
|
"sort"
|
2016-02-03 10:37:29 +03:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
2016-05-15 17:17:44 +03:00
|
|
|
"github.com/roasbeef/btcd/chaincfg"
|
|
|
|
"github.com/roasbeef/btcutil"
|
2016-02-03 10:37:29 +03:00
|
|
|
|
|
|
|
"github.com/lightningnetwork/lnd/uspv"
|
|
|
|
)
|
|
|
|
|
|
|
|
/* this is a CLI shell for testing out LND. Right now it's only for uspv
|
|
|
|
testing. It can send and receive coins.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const (
|
|
|
|
keyFileName = "testkey.hex"
|
|
|
|
headerFileName = "headers.bin"
|
2016-02-11 00:22:15 +03:00
|
|
|
dbFileName = "utxo.db"
|
2016-02-03 10:37:29 +03:00
|
|
|
// this is my local testnet node, replace it with your own close by.
|
|
|
|
// Random internet testnet nodes usually work but sometimes don't, so
|
|
|
|
// maybe I should test against different versions out there.
|
2016-02-11 00:22:15 +03:00
|
|
|
SPVHostAdr = "127.0.0.1:28333"
|
2016-02-03 10:37:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2016-05-23 08:04:05 +03:00
|
|
|
Params = &chaincfg.SegNet4Params
|
2016-02-03 10:37:29 +03:00
|
|
|
SCon uspv.SPVCon // global here for now
|
|
|
|
)
|
|
|
|
|
2016-02-23 05:24:56 +03:00
|
|
|
func shell(SPVHostAdr string, Params *chaincfg.Params) {
|
2016-02-03 10:37:29 +03:00
|
|
|
fmt.Printf("LND spv shell v0.0\n")
|
|
|
|
fmt.Printf("Not yet well integrated, but soon.\n")
|
|
|
|
|
|
|
|
// read key file (generate if not found)
|
|
|
|
rootPriv, err := uspv.ReadKeyFileToECPriv(keyFileName, Params)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
// setup TxStore first (before spvcon)
|
|
|
|
Store := uspv.NewTxStore(rootPriv, Params)
|
|
|
|
// setup spvCon
|
|
|
|
|
|
|
|
SCon, err = uspv.OpenSPV(
|
2016-02-11 00:22:15 +03:00
|
|
|
SPVHostAdr, headerFileName, dbFileName, &Store, true, false, Params)
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tip, err := SCon.TS.GetDBSyncHeight() // ask for sync height
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
if tip == 0 { // DB has never been used, set to birthday
|
2016-02-22 12:41:40 +03:00
|
|
|
tip = 21900 // hardcoded; later base on keyfile date?
|
2016-02-03 10:37:29 +03:00
|
|
|
err = SCon.TS.SetDBSyncHeight(tip)
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-14 23:50:20 +03:00
|
|
|
// once we're connected, initiate headers sync
|
|
|
|
err = SCon.AskForHeaders()
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
2016-02-03 10:37:29 +03:00
|
|
|
|
|
|
|
// main shell loop
|
|
|
|
for {
|
|
|
|
// setup reader with max 4K input chars
|
|
|
|
reader := bufio.NewReaderSize(os.Stdin, 4000)
|
|
|
|
fmt.Printf("LND# ") // prompt
|
|
|
|
msg, err := reader.ReadString('\n') // input finishes on enter key
|
|
|
|
if err != nil {
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
cmdslice := strings.Fields(msg) // chop input up on whitespace
|
|
|
|
if len(cmdslice) < 1 {
|
|
|
|
continue // no input, just prompt again
|
|
|
|
}
|
|
|
|
fmt.Printf("entered command: %s\n", msg) // immediate feedback
|
|
|
|
err = Shellparse(cmdslice)
|
|
|
|
if err != nil { // only error should be user exit
|
|
|
|
log.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shellparse parses user input and hands it to command functions if matching
|
|
|
|
func Shellparse(cmdslice []string) error {
|
|
|
|
var err error
|
|
|
|
var args []string
|
|
|
|
cmd := cmdslice[0]
|
|
|
|
if len(cmdslice) > 1 {
|
|
|
|
args = cmdslice[1:]
|
|
|
|
}
|
|
|
|
if cmd == "exit" || cmd == "quit" {
|
|
|
|
return fmt.Errorf("User exit")
|
|
|
|
}
|
|
|
|
|
|
|
|
// help gives you really terse help. Just a list of commands.
|
|
|
|
if cmd == "help" {
|
|
|
|
err = Help(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("help error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// adr generates a new address and displays it
|
|
|
|
if cmd == "adr" {
|
|
|
|
err = Adr(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("adr error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// bal shows the current set of utxos, addresses and score
|
|
|
|
if cmd == "bal" {
|
|
|
|
err = Bal(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("bal error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// send sends coins to the address specified
|
|
|
|
if cmd == "send" {
|
|
|
|
err = Send(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("send error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-23 03:32:58 +03:00
|
|
|
if cmd == "fan" {
|
|
|
|
err = Fan(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("fan error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
if cmd == "sweep" {
|
|
|
|
err = Sweep(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("sweep error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-05 12:59:40 +03:00
|
|
|
if cmd == "txs" {
|
|
|
|
err = Txs(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("txs error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-07 06:15:35 +03:00
|
|
|
if cmd == "blk" {
|
|
|
|
err = Blk(args)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("blk error: %s\n", err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-02-03 10:37:29 +03:00
|
|
|
fmt.Printf("Command not recognized. type help for command list.\n")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-05 12:59:40 +03:00
|
|
|
func Txs(args []string) error {
|
|
|
|
alltx, err := SCon.TS.GetAllTxs()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for i, tx := range alltx {
|
|
|
|
fmt.Printf("tx %d %s\n", i, uspv.TxToString(tx))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-07 06:15:35 +03:00
|
|
|
func Blk(args []string) error {
|
|
|
|
if SCon.RBytes == 0 {
|
|
|
|
return fmt.Errorf("Can't check block, spv connection broken")
|
|
|
|
}
|
|
|
|
if len(args) == 0 {
|
|
|
|
return fmt.Errorf("must specify height")
|
|
|
|
}
|
|
|
|
height, err := strconv.ParseInt(args[0], 10, 32)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// request most recent block just to test
|
|
|
|
err = SCon.AskForOneBlock(int32(height))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:37:29 +03:00
|
|
|
// Bal prints out your score.
|
|
|
|
func Bal(args []string) error {
|
|
|
|
if SCon.TS == nil {
|
|
|
|
return fmt.Errorf("Can't get balance, spv connection broken")
|
|
|
|
}
|
|
|
|
fmt.Printf(" ----- Account Balance ----- \n")
|
2016-02-25 04:27:29 +03:00
|
|
|
rawUtxos, err := SCon.TS.GetAllUtxos()
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
var allUtxos uspv.SortableUtxoSlice
|
|
|
|
for _, utxo := range rawUtxos {
|
|
|
|
allUtxos = append(allUtxos, *utxo)
|
|
|
|
}
|
|
|
|
// smallest and unconfirmed last (because it's reversed)
|
|
|
|
sort.Sort(sort.Reverse(allUtxos))
|
|
|
|
|
2016-02-24 13:37:52 +03:00
|
|
|
var score, confScore int64
|
2016-02-03 10:37:29 +03:00
|
|
|
for i, u := range allUtxos {
|
2016-02-19 11:32:22 +03:00
|
|
|
fmt.Printf("\tutxo %d height %d %s key:%d amt %d",
|
2016-02-03 10:37:29 +03:00
|
|
|
i, u.AtHeight, u.Op.String(), u.KeyIdx, u.Value)
|
2016-02-19 11:32:22 +03:00
|
|
|
if u.IsWit {
|
|
|
|
fmt.Printf(" WIT")
|
|
|
|
}
|
|
|
|
fmt.Printf("\n")
|
2016-02-03 10:37:29 +03:00
|
|
|
score += u.Value
|
2016-02-24 13:37:52 +03:00
|
|
|
if u.AtHeight != 0 {
|
|
|
|
confScore += u.Value
|
|
|
|
}
|
2016-02-03 10:37:29 +03:00
|
|
|
}
|
|
|
|
height, _ := SCon.TS.GetDBSyncHeight()
|
|
|
|
|
2016-02-25 04:27:29 +03:00
|
|
|
atx, err := SCon.TS.GetAllTxs()
|
|
|
|
|
|
|
|
stxos, err := SCon.TS.GetAllStxos()
|
|
|
|
|
2016-02-03 10:37:29 +03:00
|
|
|
for i, a := range SCon.TS.Adrs {
|
2016-02-19 12:24:23 +03:00
|
|
|
wa, err := btcutil.NewAddressWitnessPubKeyHash(
|
|
|
|
a.PkhAdr.ScriptAddress(), Params)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("address %d %s OR %s\n", i, a.PkhAdr.String(), wa.String())
|
2016-02-19 11:32:22 +03:00
|
|
|
}
|
2016-02-25 04:27:29 +03:00
|
|
|
fmt.Printf("Total known txs: %d\n", len(atx))
|
|
|
|
fmt.Printf("Known utxos: %d\tPreviously spent txos: %d\n",
|
|
|
|
len(allUtxos), len(stxos))
|
2016-02-24 13:37:52 +03:00
|
|
|
fmt.Printf("Total coin: %d confirmed: %d\n", score, confScore)
|
2016-02-03 10:37:29 +03:00
|
|
|
fmt.Printf("DB sync height: %d\n", height)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Adr makes a new address.
|
|
|
|
func Adr(args []string) error {
|
2016-02-19 11:32:22 +03:00
|
|
|
|
2016-02-19 12:24:23 +03:00
|
|
|
// if there's an arg, make 10 adrs
|
2016-02-19 11:32:22 +03:00
|
|
|
if len(args) > 0 {
|
|
|
|
for i := 0; i < 10; i++ {
|
2016-02-19 12:24:23 +03:00
|
|
|
_, err := SCon.TS.NewAdr()
|
2016-02-19 11:32:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-23 03:32:58 +03:00
|
|
|
if len(args) > 1 {
|
|
|
|
for i := 0; i < 1000; i++ {
|
|
|
|
_, err := SCon.TS.NewAdr()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
|
2016-02-19 12:24:23 +03:00
|
|
|
// always make one
|
|
|
|
a, err := SCon.TS.NewAdr()
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-19 11:32:22 +03:00
|
|
|
fmt.Printf("made new address %s\n",
|
|
|
|
a.String())
|
2016-02-03 10:37:29 +03:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-25 04:27:29 +03:00
|
|
|
// Sweep sends every confirmed uxto in your wallet to an address.
|
|
|
|
// it does them all individually to there are a lot of txs generated.
|
|
|
|
// syntax: sweep adr
|
|
|
|
func Sweep(args []string) error {
|
|
|
|
if len(args) < 2 {
|
|
|
|
return fmt.Errorf("sweep syntax: sweep adr")
|
|
|
|
}
|
|
|
|
|
|
|
|
adr, err := btcutil.DecodeAddress(args[0], SCon.TS.Param)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Printf("error parsing %s as address\t", args[0])
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
numTxs, err := strconv.ParseInt(args[1], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if numTxs < 1 {
|
|
|
|
return fmt.Errorf("can't send %d txs", numTxs)
|
|
|
|
}
|
|
|
|
|
|
|
|
rawUtxos, err := SCon.TS.GetAllUtxos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var allUtxos uspv.SortableUtxoSlice
|
|
|
|
for _, utxo := range rawUtxos {
|
|
|
|
allUtxos = append(allUtxos, *utxo)
|
|
|
|
}
|
|
|
|
// smallest and unconfirmed last (because it's reversed)
|
|
|
|
sort.Sort(sort.Reverse(allUtxos))
|
|
|
|
|
|
|
|
for i, u := range allUtxos {
|
|
|
|
if u.AtHeight != 0 {
|
|
|
|
err = SCon.SendOne(allUtxos[i], adr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
numTxs--
|
|
|
|
if numTxs == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("spent all confirmed utxos; not enough by %d\n", numTxs)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-02-23 03:32:58 +03:00
|
|
|
// Fan generates a bunch of fanout. Only for testing, can be expensive.
|
2016-02-24 13:37:52 +03:00
|
|
|
// syntax: fan adr numOutputs valOutputs witty
|
2016-02-23 03:32:58 +03:00
|
|
|
func Fan(args []string) error {
|
|
|
|
if len(args) < 3 {
|
2016-02-24 13:37:52 +03:00
|
|
|
return fmt.Errorf("fan syntax: fan adr numOutputs valOutputs")
|
2016-02-23 03:32:58 +03:00
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
adr, err := btcutil.DecodeAddress(args[0], SCon.TS.Param)
|
2016-02-23 03:32:58 +03:00
|
|
|
if err != nil {
|
2016-02-24 13:37:52 +03:00
|
|
|
fmt.Printf("error parsing %s as address\t", args[0])
|
2016-02-23 03:32:58 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
numOutputs, err := strconv.ParseInt(args[1], 10, 64)
|
2016-02-23 03:32:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
valOutputs, err := strconv.ParseInt(args[2], 10, 64)
|
2016-02-23 03:32:58 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
adrs := make([]btcutil.Address, numOutputs)
|
|
|
|
amts := make([]int64, numOutputs)
|
|
|
|
|
|
|
|
for i := int64(0); i < numOutputs; i++ {
|
2016-02-24 13:37:52 +03:00
|
|
|
adrs[i] = adr
|
2016-02-23 03:32:58 +03:00
|
|
|
amts[i] = valOutputs + i
|
|
|
|
}
|
|
|
|
return SCon.SendCoins(adrs, amts)
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:37:29 +03:00
|
|
|
// Send sends coins.
|
|
|
|
func Send(args []string) error {
|
2016-02-07 06:15:35 +03:00
|
|
|
if SCon.RBytes == 0 {
|
|
|
|
return fmt.Errorf("Can't send, spv connection broken")
|
|
|
|
}
|
2016-02-03 10:37:29 +03:00
|
|
|
// get all utxos from the database
|
|
|
|
allUtxos, err := SCon.TS.GetAllUtxos()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
var score int64 // score is the sum of all utxo amounts. highest score wins.
|
|
|
|
// add all the utxos up to get the score
|
|
|
|
for _, u := range allUtxos {
|
|
|
|
score += u.Value
|
|
|
|
}
|
|
|
|
|
|
|
|
// score is 0, cannot unlock 'send coins' acheivement
|
|
|
|
if score == 0 {
|
|
|
|
return fmt.Errorf("You don't have money. Work hard.")
|
|
|
|
}
|
|
|
|
// need args, fail
|
|
|
|
if len(args) < 2 {
|
2016-02-24 13:37:52 +03:00
|
|
|
return fmt.Errorf("need args: ssend address amount(satoshis) wit?")
|
2016-02-18 10:16:17 +03:00
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
adr, err := btcutil.DecodeAddress(args[0], SCon.TS.Param)
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
2016-02-24 13:37:52 +03:00
|
|
|
fmt.Printf("error parsing %s as address\t", args[0])
|
2016-02-03 10:37:29 +03:00
|
|
|
return err
|
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
amt, err := strconv.ParseInt(args[1], 10, 64)
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-02-24 13:37:52 +03:00
|
|
|
if amt < 1000 {
|
|
|
|
return fmt.Errorf("can't send %d, too small", amt)
|
|
|
|
}
|
|
|
|
|
2016-02-03 10:37:29 +03:00
|
|
|
fmt.Printf("send %d to address: %s \n",
|
|
|
|
amt, adr.String())
|
2016-02-18 10:16:17 +03:00
|
|
|
|
2016-02-23 01:35:01 +03:00
|
|
|
var adrs []btcutil.Address
|
|
|
|
var amts []int64
|
|
|
|
|
|
|
|
adrs = append(adrs, adr)
|
|
|
|
amts = append(amts, amt)
|
|
|
|
err = SCon.SendCoins(adrs, amts)
|
2016-02-03 10:37:29 +03:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func Help(args []string) error {
|
|
|
|
fmt.Printf("commands:\n")
|
|
|
|
fmt.Printf("help adr bal send exit\n")
|
|
|
|
return nil
|
|
|
|
}
|