Exemplo n.º 1
0
func main() {
	// flag.StringVar(&rawtx, "rawtx", "", "rawtx")
	// flag.Parse()

	tx := skycoin.Transaction{}
	b, err := hex.DecodeString(os.Args[1])
	if err != nil {
		log.Fatal(err)
	}

	if err := tx.Deserialize(bytes.NewBuffer(b)); err != nil {
		log.Fatal(err)
	}

	readable := ToReadable(&tx)

	v, err := json.MarshalIndent(readable, "", "    ")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println(string(v))
}
Exemplo n.º 2
0
// CreateRawTx creates raw transaction
func (cn coinEx) CreateRawTx(txIns []coin.TxIn, getKey coin.GetPrivKey, txOuts interface{}) (string, error) {
	tx := skycoin.Transaction{}
	for _, in := range txIns {
		tx.PushInput(cipher.MustSHA256FromHex(in.Txid))
	}

	s := reflect.ValueOf(txOuts)
	if s.Kind() != reflect.Slice {
		return "", errors.New("error tx out type")
	}
	outs := make([]interface{}, s.Len())
	for i := 0; i < s.Len(); i++ {
		outs[i] = s.Index(i).Interface()
	}

	if len(outs) > 2 {
		return "", errors.New("out address more than 2")
	}

	for _, o := range outs {
		out := o.(skycoin.TxOut)
		if (out.Coins % 1e6) != 0 {
			return "", fmt.Errorf("%s coins must be multiple of 1e6", cn.Name)
		}
		tx.PushOutput(out.Address, out.Coins, out.Hours)
	}

	keys := make([]cipher.SecKey, len(txIns))
	for i, in := range txIns {
		s, err := getKey(in.Address)
		if err != nil {
			return "", fmt.Errorf("get private key failed:%v", err)
		}
		k, err := cipher.SecKeyFromHex(s)
		if err != nil {
			return "", fmt.Errorf("invalid private key:%v", err)
		}
		keys[i] = k
	}

	tx.SignInputs(keys)
	tx.UpdateHeader()

	d, err := tx.Serialize()
	if err != nil {
		return "", err
	}
	return hex.EncodeToString(d), nil
}