9 "github.com/bytom/blockchain/account"
10 "github.com/bytom/blockchain/asset"
11 "github.com/bytom/blockchain/pin"
12 "github.com/bytom/blockchain/pseudohsm"
13 "github.com/bytom/blockchain/txbuilder"
14 "github.com/bytom/blockchain/txdb"
15 cfg "github.com/bytom/config"
16 "github.com/bytom/consensus"
17 "github.com/bytom/crypto/ed25519/chainkd"
18 "github.com/bytom/protocol"
19 "github.com/bytom/protocol/bc"
20 "github.com/bytom/protocol/bc/legacy"
22 dbm "github.com/tendermint/tmlibs/db"
25 const dirPath = "pseudohsm/testdata/pseudo"
27 func TestHSM(t *testing.T) {
28 ctx := context.Background()
29 config := cfg.DefaultConfig()
30 tc := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
31 store := txdb.NewStore(tc)
33 var accounts *account.Manager
34 var assets *asset.Registry
35 var pinStore *pin.Store
37 genesisBlock := &legacy.Block{
38 BlockHeader: legacy.BlockHeader{},
39 Transactions: []*legacy.Tx{},
41 genesisBlock.UnmarshalText(consensus.InitBlock())
43 txPool := protocol.NewTxPool()
44 chain, err := protocol.NewChain(genesisBlock.Hash(), store, txPool)
49 // add gensis block info
50 if err := chain.SaveBlock(genesisBlock); err != nil {
53 // parse block and apply
54 if err := chain.ConnectBlock(genesisBlock); err != nil {
58 accUTXODB := dbm.NewDB("accountutxos", config.DBBackend, config.DBDir())
59 pinStore = pin.NewStore(accUTXODB)
61 err = pinStore.LoadAll(ctx)
65 accountsDB := dbm.NewDB("account", config.DBBackend, config.DBDir())
66 accounts = account.NewManager(accountsDB, chain, pinStore)
67 //accounts.IndexAccounts(query.NewIndexer(accountsDB, chain))
69 assetsDB := dbm.NewDB("asset", config.DBBackend, config.DBDir())
70 assets = asset.NewRegistry(assetsDB, chain)
72 hsm, err := pseudohsm.New(dirPath)
76 xpub1, err := hsm.XCreate("xpub1", "password")
80 xpub2, err := hsm.XCreate("xpub2", "password")
85 acct1, err := accounts.Create(ctx, []chainkd.XPub{xpub1.XPub}, 1, "acc1", nil, "")
89 acct2, err := accounts.Create(ctx, []chainkd.XPub{xpub2.XPub}, 1, "acc2", nil, "")
94 assetDef1 := map[string]interface{}{"foo": 1}
95 assetDef2 := map[string]interface{}{"foo": 2}
97 asset1, err := assets.Define(ctx, []chainkd.XPub{xpub1.XPub}, 1, assetDef1, "foo1", nil, "")
101 asset2, err := assets.Define(ctx, []chainkd.XPub{xpub2.XPub}, 1, assetDef2, "foo2", nil, "")
106 issue1 := txbuilder.Action(assets.NewIssueAction(bc.AssetAmount{AssetId: &asset1.AssetID, Amount: 100}, nil))
107 issue2 := txbuilder.Action(assets.NewIssueAction(bc.AssetAmount{AssetId: &asset2.AssetID, Amount: 200}, nil))
108 spend1 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset1.AssetID, Amount: 100}, acct1.ID, nil)
109 spend2 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset2.AssetID, Amount: 200}, acct2.ID, nil)
111 tmpl, err := txbuilder.Build(ctx, nil, []txbuilder.Action{issue1, issue2, spend1, spend2}, time.Now().Add(time.Minute))
115 //go accounts.ProcessBlocks(ctx)
117 err = txbuilder.Sign(ctx, tmpl, []chainkd.XPub{xpub1.XPub, xpub2.XPub}, "password", func(_ context.Context, xpub chainkd.XPub, path [][]byte, data [32]byte, password string) ([]byte, error) {
118 sigBytes, err := hsm.XSign(xpub, path, data[:], password)
124 fmt.Printf("###data: %v#####", *tmpl)
126 //err = txbuilder.FinalizeTx(ctx, chain, tmpl.Transaction)
131 // generate block without nouce
132 b, err := mining.NewBlockTemplate(chain, txPool, []byte{})
137 for i := uint64(0); i <= 10000000000000; i++ {
140 if consensus.CheckProofOfWork(&hash, b.Bits) {
145 if _, err = chain.ProcessBlock(b); err != nil {
149 <-pinStore.PinWaiter(account.PinName, chain.Height())
152 c := prottest.NewChain(t)
153 assets := asset.NewRegistry(db, c, pinStore)
154 accounts å:= account.NewManager(db, c, pinStore)
155 coretest.CreatePins(ctx, t, pinStore)
156 accounts.IndexAccounts(query.NewIndexer(db, c, pinStore))
157 go accounts.ProcessBlocks(ctx)
159 coretest.SignTxTemplate(t, ctx, tmpl, &testutil.TestXPrv)
160 err = txbuilder.FinalizeTx(ctx, c, g, tmpl.Transaction)
165 // Make a block so that UTXOs from the above tx are available to spend.
166 prottest.MakeBlock(t, c, g.PendingTxs())
167 <-pinStore.PinWaiter(account.PinName, c.Height())
169 xferSrc1 := accounts.NewSpendAction(bc.AssetAmount{AssetId: &asset1ID, Amount: 10}, acct1.ID, nil, nil)
170 xferSrc2 := accounts.NewSpendAction(bc.AssetAmount{AssetId: &asset2ID, Amount: 20}, acct2.ID, nil, nil)
171 xferDest1 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset2ID, Amount: 20}, acct1.ID, nil)
172 xferDest2 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset1ID, Amount: 10}, acct2.ID, nil)
173 tmpl, err = txbuilder.Build(ctx, nil, []txbuilder.Action{xferSrc1, xferSrc2, xferDest1, xferDest2}, time.Now().Add(time.Minute))