OSDN Git Service

versoin1.1.9 (#594)
[bytom/vapor.git] / test / mock / crosschain_tx.go
1 package mock
2
3 import (
4         "math/rand"
5         "time"
6
7         log "github.com/sirupsen/logrus"
8
9         "github.com/bytom/vapor/blockchain/txbuilder"
10         "github.com/bytom/vapor/consensus"
11         "github.com/bytom/vapor/crypto/ed25519/chainkd"
12         "github.com/bytom/vapor/protocol/bc"
13         "github.com/bytom/vapor/protocol/bc/types"
14 )
15
16 func NewCrosschainTx(privateKey string) *types.Tx {
17         var xprv chainkd.XPrv
18         if err := xprv.UnmarshalText([]byte(privateKey)); err != nil {
19                 log.WithField("err", err).Panic("fail on xprv UnmarshalText")
20         }
21
22         //generate the tx
23         randomHash := [32]byte{}
24         rand.Seed(time.Now().UnixNano())
25         rand.Read(randomHash[:])
26         builder := txbuilder.NewBuilder(time.Now())
27         input := types.NewCrossChainInput(nil, bc.NewHash(randomHash), *consensus.BTMAssetID, 10000000, 1, 1, []byte{}, []byte{})
28         if err := builder.AddInput(input, &txbuilder.SigningInstruction{}); err != nil {
29                 log.WithField("err", err).Panic("fail on add builder input")
30         }
31
32         output := types.NewIntraChainOutput(*consensus.BTMAssetID, 10000000, []byte{0x00, 0x14, 0xd4, 0x88, 0xda, 0x3b, 0x78, 0x1a, 0xa4, 0xe1, 0xdb, 0x9b, 0x7b, 0x27, 0x4d, 0xf1, 0x04, 0x33, 0x54, 0x83, 0x0b, 0x07})
33         if err := builder.AddOutput(output); err != nil {
34                 log.WithField("err", err).Panic("fail on add builder output")
35         }
36
37         tpl, _, err := builder.Build()
38         if err != nil {
39                 log.WithField("err", err).Panic("fail on add builder tx")
40         }
41
42         //sign tx
43         signHash := tpl.Hash(0).Byte32()
44         sign := xprv.Sign(signHash[:])
45         tpl.Transaction.SetInputArguments(0, [][]byte{sign})
46
47         data, err := tpl.Transaction.TxData.MarshalText()
48         if err != nil {
49                 log.WithField("err", err).Panic("fail on unmarshal tx")
50         }
51
52         tpl.Transaction.TxData.SerializedSize = uint64(len(data) / 2)
53         tpl.Transaction.Tx.SerializedSize = uint64(len(data) / 2)
54         return tpl.Transaction
55 }