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"
21 dbm "github.com/tendermint/tmlibs/db"
24 const dirPath = "pseudohsm/testdata/pseudo"
26 func TestHSM(t *testing.T) {
27 ctx := context.Background()
28 config := cfg.DefaultConfig()
29 tc := dbm.NewDB("txdb", config.DBBackend, config.DBDir())
30 store := txdb.NewStore(tc)
32 var accounts *account.Manager
33 var assets *asset.Registry
34 var pinStore *pin.Store
36 genesisBlock := &legacy.Block{
37 BlockHeader: legacy.BlockHeader{},
38 Transactions: []*legacy.Tx{},
40 genesisBlock.UnmarshalText(consensus.InitBlock())
41 txPool := protocol.NewTxPool()
42 chain, err := protocol.NewChain(ctx, genesisBlock.Hash(), store, txPool, nil)
46 accUTXODB := dbm.NewDB("accountutxos", config.DBBackend, config.DBDir())
47 pinStore = pin.NewStore(accUTXODB)
49 err = pinStore.LoadAll(ctx)
53 accountsDB := dbm.NewDB("account", config.DBBackend, config.DBDir())
54 accounts = account.NewManager(accountsDB, chain, pinStore)
56 assetsDB := dbm.NewDB("asset", config.DBBackend, config.DBDir())
57 assets = asset.NewRegistry(assetsDB, chain)
59 hsm, err := pseudohsm.New(dirPath)
63 xpub1, err := hsm.XCreate("xpub1", "password")
67 xpub2, err := hsm.XCreate("xpub2", "password")
72 acct1, err := accounts.Create(ctx, []chainkd.XPub{xpub1.XPub}, 1, "acc1", nil, "")
76 acct2, err := accounts.Create(ctx, []chainkd.XPub{xpub2.XPub}, 1, "acc2", nil, "")
81 assetDef1 := map[string]interface{}{"foo": 1}
82 assetDef2 := map[string]interface{}{"foo": 2}
84 asset1, err := assets.Define(ctx, []chainkd.XPub{xpub1.XPub}, 1, assetDef1, "foo1", nil, "")
88 asset2, err := assets.Define(ctx, []chainkd.XPub{xpub2.XPub}, 1, assetDef2, "foo2", nil, "")
93 issue1 := txbuilder.Action(assets.NewIssueAction(bc.AssetAmount{AssetId: &asset1.AssetID, Amount: 100}, nil))
94 issue2 := txbuilder.Action(assets.NewIssueAction(bc.AssetAmount{AssetId: &asset2.AssetID, Amount: 200}, nil))
95 spend1 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset1.AssetID, Amount: 100}, acct1.ID, nil)
96 spend2 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset2.AssetID, Amount: 200}, acct2.ID, nil)
98 tmpl, err := txbuilder.Build(ctx, nil, []txbuilder.Action{issue1, issue2, spend1, spend2}, time.Now().Add(time.Minute))
102 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) {
103 sigBytes, err := hsm.XSign(xpub, path, data[:], password)
109 fmt.Printf("###data: %v#####", *tmpl)
112 c := prottest.NewChain(t)
113 assets := asset.NewRegistry(db, c, pinStore)
114 accounts å:= account.NewManager(db, c, pinStore)
115 coretest.CreatePins(ctx, t, pinStore)
116 accounts.IndexAccounts(query.NewIndexer(db, c, pinStore))
117 go accounts.ProcessBlocks(ctx)
119 coretest.SignTxTemplate(t, ctx, tmpl, &testutil.TestXPrv)
120 err = txbuilder.FinalizeTx(ctx, c, g, tmpl.Transaction)
125 // Make a block so that UTXOs from the above tx are available to spend.
126 prottest.MakeBlock(t, c, g.PendingTxs())
127 <-pinStore.PinWaiter(account.PinName, c.Height())
129 xferSrc1 := accounts.NewSpendAction(bc.AssetAmount{AssetId: &asset1ID, Amount: 10}, acct1.ID, nil, nil)
130 xferSrc2 := accounts.NewSpendAction(bc.AssetAmount{AssetId: &asset2ID, Amount: 20}, acct2.ID, nil, nil)
131 xferDest1 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset2ID, Amount: 20}, acct1.ID, nil)
132 xferDest2 := accounts.NewControlAction(bc.AssetAmount{AssetId: &asset1ID, Amount: 10}, acct2.ID, nil)
133 tmpl, err = txbuilder.Build(ctx, nil, []txbuilder.Action{xferSrc1, xferSrc2, xferDest1, xferDest2}, time.Now().Add(time.Minute))