Ejemplo n.º 1
0
// This example demonstrates creating a script which pays to a bitcoin address.
// It also prints the created script hex and uses the DisasmString function to
// display the disassembled script.
func ExamplePayToAddrScript() {
	// Parse the address to send the coins to into a btcutil.Address
	// which is useful to ensure the accuracy of the address and determine
	// the address type.  It is also required for the upcoming call to
	// PayToAddrScript.
	addressStr := "12gpXQVcCL2qhTNQgyLVdCFG2Qs2px98nV"
	address, err := btcutil.DecodeAddress(addressStr, &chaincfg.MainNetParams)
	if err != nil {
		fmt.Println(err)
		return
	}

	// Create a public key script that pays to the address.
	script, err := txscript.PayToAddrScript(address)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Printf("Script Hex: %x\n", script)

	disasm, err := txscript.DisasmString(script)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("Script Disassembly:", disasm)

	// Output:
	// Script Hex: 76a914128004ff2fcaf13b2b91eb654b1dc2b674f7ec6188ac
	// Script Disassembly: OP_DUP OP_HASH160 128004ff2fcaf13b2b91eb654b1dc2b674f7ec61 OP_EQUALVERIFY OP_CHECKSIG
}
Ejemplo n.º 2
0
//this is a debug func
func get_tx_info(tx *wire.MsgTx, block_index uint64) (source,
	destination string, btc_amount, fee uint64, data []string) {
	var bFound bool = false

	for _, value := range tx.TxOut {
		nettype := &chaincfg.MainNetParams
		if conf.MainNet {
			nettype = &chaincfg.MainNetParams
		} else {
			nettype = &chaincfg.RegressionNetParams
		}
		_, Address, _, _ := txscript.ExtractPkScriptAddrs(value.PkScript, nettype)

		if len(Address) != 0 {
			if Address[0].String() == conf.WISHINGWALLADDRESS {

				bFound = true
				continue
			}
		}
		if bFound == true {
			tempasm, _ := txscript.DisasmString(value.PkScript)

			message := strings.Split(tempasm, " ")

			merge := message[1] + message[2]
			data = append(data, merge)
		}
	}
	if bFound == true {
		destination = conf.WISHINGWALLADDRESS
	} else {
		var temp []string
		return "", "", 0, 0, temp
	}

	//get source address

	if tx.TxIn[0].PreviousOutPoint.Index == 0 {
		source = string(block_index)
	} else {
		SourceTx, _ := bitcoinchain.GetRawTransaction(tx.TxIn[0].PreviousOutPoint.Hash.String())

		if SourceTx == nil {
			source = "Unkonw"
		} else {
			for _, prevalue := range SourceTx.Vout {

				if prevalue.N == tx.TxIn[0].PreviousOutPoint.Index {
					source = prevalue.ScriptPubKey.Addresses[0]
				}
			}
		}
	}

	return source, destination, 0, 0, data
}