Example #1
0
//this func is just a check
func checkAddr(addr string, w io.Writer) error {
	c := cclient.NewClient(DefaultNodeRPCAddr, REQUEST_TYPE)
	if addr == "" {
		_, err := c.ListAccounts()
		IfExit(err)
		return nil
	} else {
		addrBytes, err := hex.DecodeString(addr)
		if err != nil {
			IfExit(fmt.Errorf("Addr %s is improper hex: %v", addr, err))
		}
		r, err := c.GetAccount(addrBytes)
		IfExit(err)
		if r == nil {
			IfExit(fmt.Errorf("Account %X does not exist", addrBytes))
		}
		r2 := r.Account
		if r2 == nil {
			IfExit(fmt.Errorf("Account %X does not exist", addrBytes))
		}
	}
	//TODO get more infos (like check if they have perms!)
	//something like: w.Write([]byte("Permission denied, invalid address\n"))
	return nil
}
Example #2
0
func get_account() {
	addrHex, err := Prompt("Enter the address of the account in HEX (e.g. 9FCBA7F840A0BFEBBE755E853C9947270A912D04):\n> ", "")
	if err != nil {
		Exit(Fmt("Error: %v", err))
	}
	cli := cclient.NewClient("http://localhost:46657", "JSONRPC")
	address, err := hex.DecodeString(addrHex)
	if err != nil {
		Exit(Fmt("Address was not hex: %v", addrHex))
	}
	res, err := cli.GetAccount(address)
	if res == nil {
		Exit(Fmt("Account does not exist: %X", address))
	}
	if err != nil {
		Exit(Fmt("Error fetching account: %v", err))
	}
	acc := res.Account

	fmt.Printf(`
Address:     %X
PubKey:      %v
Sequence:    %v
Balance:     %v
Permissions: %v
`,
		acc.Address,
		acc.PubKey,
		acc.Sequence,
		acc.Balance,
		acc.Permissions)
}
Example #3
0
// need to think of the name spacing carefullly
func ListAllTheNames() ([]string, error) {
	//GET chain URL
	url := os.Getenv("MINTX_NODE_ADDR")

	c := cclient.NewClient(url, "HTTP")

	res, err := c.ListNames()
	if err != nil {
		return nil, fmt.Errorf("error calling c.LN(): %v", err)
	}

	names := make([]string, len(res.Names))

	for i, name := range res.Names {
		names[i] = name.Name
	}
	return names, nil
}
Example #4
0
//TODO cleanup -> func GetHashFromFileName()
func GetInfos(fileName string) (string, error) {
	c := cclient.NewClient(DefaultNodeRPCAddr, REQUEST_TYPE)
	if fileName == "" {
		//to support an endpoint that lists available files
		_, err := c.ListNames()
		IfExit(err)
		/*res := make([]string, len(names.Names))
		i := 0
		for n := range names.Names {
			res[i] = n.Entry.Name
			i += 1
		}
		result := string.Join(res, "\n")*/
		return "", nil
	} else {
		n, err := c.GetName(fileName)
		IfExit(err)

		name := n.Entry.Data
		return name, nil
	}
}
Example #5
0
// Create a new client for the node at the given addr
func NewNodeClient(addr string) *NodeClient {
	return &NodeClient{
		rpc: cclient.NewClient("http://"+addr, "JSONRPC"),
		ws:  cclient.NewWSClient("ws://" + addr + "/events"),
	}
}
Example #6
0
var (
	rpcAddr       = "127.0.0.1:36657" // Not 46657
	requestAddr   = "http://" + rpcAddr + "/"
	websocketAddr = "ws://" + rpcAddr + "/websocket"

	node *nm.Node

	mempoolCount = 0

	// make keys
	user = makeUsers(5)

	chainID string

	clients = map[string]cclient.Client{
		"JSONRPC": cclient.NewClient(requestAddr, "JSONRPC"),
		"HTTP":    cclient.NewClient(requestAddr, "HTTP"),
	}
)

// deterministic account generation, synced with genesis file in config/tendermint_test/config.go
func makeUsers(n int) []*acm.PrivAccount {
	accounts := []*acm.PrivAccount{}
	for i := 0; i < n; i++ {
		secret := ("mysecret" + strconv.Itoa(i))
		user := acm.GenPrivAccountFromSecret(secret)
		accounts = append(accounts, user)
	}
	return accounts
}
Example #7
0
func send_tx() {

	// Get PrivAccount
	var privAccount *acm.PrivAccount
	secret := getString("Enter your secret, or just hit <Enter> to enter a private key in HEX.\n> ")
	if secret == "" {
		privKeyBytes := getByteSliceFromHex("Enter your private key in HEX (e.g. E353CAD81134A301A542AEBE2D2E4EF1A64A145117EC72743AE9C9D171A4AA69F3A7DD670A9E9307AAED000D97D5B3C07D90276BFCEEDA5ED11DA089A4E87A81):\n> ")
		privAccount = acm.GenPrivAccountFromPrivKeyBytes(privKeyBytes)
	} else {
		// Auto-detect private key hex
		if len(secret) == 128 {
			privKeyBytes, err := hex.DecodeString(secret)
			if err == nil {
				fmt.Println("Detected priv-key bytes...")
				privAccount = acm.GenPrivAccountFromPrivKeyBytes(privKeyBytes)
			} else {
				fmt.Println("That's a long seed...")
				privAccount = acm.GenPrivAccountFromSecret(secret)
			}
		} else {
			privAccount = acm.GenPrivAccountFromSecret(secret)
		}
	}
	pubKey := privAccount.PubKey

	// Get account data
	cli := cclient.NewClient("http://localhost:46657", "JSONRPC")
	res, err := cli.GetAccount(privAccount.Address)
	if err != nil {
		Exit(Fmt("Error fetching account: %v", err))
	}
	if res == nil {
		Exit(Fmt("No account was found with that secret/private-key"))
	}
	inputAcc := res.Account
	fmt.Printf(`
Source account:
Address:     %X
PubKey:      %v
Sequence:    %v
Balance:     %v
Permissions: %v
`,
		inputAcc.Address,
		pubKey,
		inputAcc.Sequence,
		inputAcc.Balance,
		inputAcc.Permissions)

	output := getByteSliceFromHex("\nEnter the output address in HEX:\n> ")
	amount := getInt64("Enter the amount to send:\n> ")

	// Construct transaction
	tx := types.NewSendTx()
	tx.AddInputWithNonce(pubKey, amount, inputAcc.Sequence+1)
	tx.AddOutput(output, amount)
	tx.Inputs[0].Signature = privAccount.Sign(config.GetString("chain_id"), tx)
	fmt.Println("Signed SendTx!: ", tx)

	// Sign up for events
	wsCli := cclient.NewWSClient("ws://localhost:46657/websocket")
	wsCli.Start()
	err = wsCli.Subscribe(types.EventStringAccInput(inputAcc.Address))
	if err != nil {
		Exit(Fmt("Error subscribing to account send event: %v", err))
	}

	// Broadcast transaction
	_, err = cli.BroadcastTx(tx)
	if err != nil {
		Exit(Fmt("Error broadcasting transaction: %v", err))
	}
	fmt.Println("Waiting for confirmation...")

	_ = <-wsCli.EventsCh
	fmt.Println("Confirmed.")
}