Beispiel #1
0
func assertReadableBlockHeader(t *testing.T, rb ReadableBlockHeader,
	bh coin.BlockHeader) {
	assert.Equal(t, rb.Version, bh.Version)
	assert.Equal(t, rb.Time, bh.Time)
	assert.Equal(t, rb.BkSeq, bh.BkSeq)
	assert.Equal(t, rb.Fee, bh.Fee)
	assert.NotPanics(t, func() {
		assert.Equal(t, cipher.MustSHA256FromHex(rb.PrevHash), bh.PrevHash)
		assert.Equal(t, cipher.MustSHA256FromHex(rb.BodyHash), bh.BodyHash)
	})
	assertJSONSerializability(t, &rb)
}
Beispiel #2
0
func InitTransaction() coin.Transaction {
	var tx coin.Transaction

	output := cipher.MustSHA256FromHex("043836eb6f29aaeb8b9bfce847e07c159c72b25ae17d291f32125e7f1912e2a0")
	tx.PushInput(output)

	for i := 0; i < 100; i++ {
		addr := cipher.MustDecodeBase58Address(AddrList[i])
		tx.PushOutput(addr, 1e12, 1) // 10e6*10e6
	}
	/*
		seckeys := make([]cipher.SecKey, 1)
		seckey := ""
		seckeys[0] = cipher.MustSecKeyFromHex(seckey)
		tx.SignInputs(seckeys)
	*/

	txs := make([]cipher.Sig, 1)
	sig := "ed9bd7a31fe30b9e2d53b35154233dfdf48aaaceb694a07142f84cdf4f5263d21b723f631817ae1c1f735bea13f0ff2a816e24a53ccb92afae685fdfc06724de01"
	txs[0] = cipher.MustSigFromHex(sig)
	tx.Sigs = txs

	tx.UpdateHeader()

	err := tx.Verify()

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

	log.Printf("signature= %s", tx.Sigs[0].Hex())
	return tx
}
Beispiel #3
0
func assertReadableTransactionHeader(t *testing.T,
	rth ReadableTransactionHeader, th coin.TransactionHeader) {
	assert.Equal(t, len(rth.Sigs), len(th.Sigs))
	assert.NotPanics(t, func() {
		for i, s := range rth.Sigs {
			assert.Equal(t, cipher.MustSigFromHex(s), th.Sigs[i])
		}
		assert.Equal(t, cipher.MustSHA256FromHex(rth.Hash), th.Hash)
	})
	assertJSONSerializability(t, &rth)
}
Beispiel #4
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
}
Beispiel #5
0
func NewDeterministicWalletFromReadable(r *ReadableWallet) Wallet {
	if r.Type != DeterministicWalletType {
		log.Panic("ReadableWallet type must be Deterministic")
	}
	if len(r.Entries) != 1 {
		log.Panic("Deterministic wallets have exactly 1 entry")
	}
	seed := cipher.MustSHA256FromHex(r.Extra["seed"].(string))
	return &DeterministicWallet{
		Filename: r.Filename,
		Name:     r.Name,
		Entry:    r.Entries.ToWalletEntries().ToArray()[0],
		Seed:     DeterministicWalletSeed(seed),
	}
}
// NewTransaction create skycoin transaction.
func NewTransaction(utxos []Utxo, keys []cipher.SecKey, outs []TxOut) *Transaction {
	tx := Transaction{}
	// keys := make([]cipher.SecKey, len(utxos))
	for _, u := range utxos {
		tx.PushInput(cipher.MustSHA256FromHex(u.GetHash()))
	}

	for _, o := range outs {
		tx.PushOutput(o.Address, o.Coins, o.Hours)
	}
	// tx.Verify()

	tx.SignInputs(keys)
	tx.UpdateHeader()
	return &tx
}
Beispiel #7
0
// NewTransaction create skycoin transaction.
func newTransaction(utxos []unspentOut, keys []cipher.SecKey, outs []coin.TransactionOutput) (*coin.Transaction, error) {
	tx := coin.Transaction{}
	// keys := make([]cipher.SecKey, len(utxos))
	for _, u := range utxos {
		tx.PushInput(cipher.MustSHA256FromHex(u.Hash))
	}

	for _, o := range outs {
		if (o.Coins % 1e6) != 0 {
			return nil, errors.New("skycoin coins must be multiple of 1e6")
		}
		tx.PushOutput(o.Address, o.Coins, o.Hours)
	}
	// tx.Verify()

	tx.SignInputs(keys)
	tx.UpdateHeader()
	return &tx, nil
}
Beispiel #8
0
func assertReadableTransactionInput(t *testing.T, rti string, ti cipher.SHA256) {
	assert.NotPanics(t, func() {
		assert.Equal(t, cipher.MustSHA256FromHex(rti), ti)
	})
	assertJSONSerializability(t, &rti)
}