コード例 #1
0
ファイル: spend.go プロジェクト: JoeyWeate/Cryptocurrecny
// getArgs parses command line args and asserts that a private key and an
// address are present and correctly formatted.
func getArgs() requiredArgs {
	flag.Parse()
	if *a == "" || *k == "" || *t == "" || *v == -1 {
		fmt.Println("\nThis tool generates a bitcoin transaction that moves coins from an input to an output.\n" +
			"You must provide a key, an address, a transaction id (the hash\n" +
			"of a tx) and the index into the outputs of that tx that fund your\n" +
			"address! Use http://blockchain.info/pushtx to send the raw transaction.\n")
		flag.PrintDefaults()
		fmt.Println("")
		os.Exit(0)
	}

	pkBytes, err := hex.DecodeString(*k)
	if err != nil {
		log.Fatal(err)
	}
	privKey, _ := btcec.PrivKeyFromBytes(btcec.S256(), pkBytes)

	addr, err := btcutil.DecodeAddress(*a, &btcnet.MainNetParams)
	if err != nil {
		log.Fatal(err)
	}

	txid, err := btcwire.NewShaHashFromStr(*t)

	args := requiredArgs{
		txid:      txid,
		vout:      uint32(*v),
		toAddress: addr,
		privKey:   privKey,
	}

	return args
}
コード例 #2
0
ファイル: spend.go プロジェクト: JoeyWeate/Cryptocurrecny
// getFundingParams pulls the relevant transaction information from the json returned by blockchain.info
// To generate a new valid transaction all of the parameters of the TxOut we are
// spending from must be used.
func getFundingParams(rawtx *blockChainInfoTx, vout uint32) (*btcwire.TxOut, *btcwire.OutPoint) {
	blkChnTxOut := rawtx.Outputs[vout]

	hash, err := btcwire.NewShaHashFromStr(rawtx.Hash)
	if err != nil {
		log.Fatal(err)
	}

	// Then convert it to a btcutil amount
	amnt := btcutil.Amount(int64(blkChnTxOut.Value))

	if err != nil {
		log.Fatal(err)
	}

	outpoint := btcwire.NewOutPoint(hash, vout)

	subscript, err := hex.DecodeString(blkChnTxOut.ScriptHex)
	if err != nil {
		log.Fatal(err)
	}

	oldTxOut := btcwire.NewTxOut(int64(amnt), subscript)

	return oldTxOut, outpoint
}