github.com/bytom/bytom@v1.1.2-0.20221014091027-bbcba3df6075/test/integration/standard_transaction_test.go (about)

     1  package integration
     2  
     3  import (
     4  	"encoding/json"
     5  	"io/ioutil"
     6  	"os"
     7  	"testing"
     8  
     9  	"github.com/bytom/bytom/account"
    10  	"github.com/bytom/bytom/blockchain/pseudohsm"
    11  	"github.com/bytom/bytom/blockchain/signers"
    12  	"github.com/bytom/bytom/crypto/ed25519/chainkd"
    13  	dbm "github.com/bytom/bytom/database/leveldb"
    14  	"github.com/bytom/bytom/protocol/bc/types"
    15  	"github.com/bytom/bytom/protocol/validation"
    16  	"github.com/bytom/bytom/test"
    17  )
    18  
    19  func TestP2PKH(t *testing.T) {
    20  	dirPath, err := ioutil.TempDir(".", "TestP2PKH")
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	defer os.RemoveAll(dirPath)
    25  
    26  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    27  	defer os.RemoveAll("temp")
    28  
    29  	chain, _, _, err := test.MockChain(testDB)
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  
    34  	accountManager := account.NewManager(testDB, chain)
    35  	hsm, err := pseudohsm.New(dirPath)
    36  	if err != nil {
    37  		t.Fatal(err)
    38  	}
    39  
    40  	xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
    41  	if err != nil {
    42  		t.Fatal(err)
    43  	}
    44  
    45  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, "testAccount", signers.BIP0044)
    46  	if err != nil {
    47  		t.Fatal(err)
    48  	}
    49  
    50  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
    51  	if err != nil {
    52  		t.Fatal(err)
    53  	}
    54  	utxo := test.MockUTXO(controlProg)
    55  	tpl, tx, err := test.MockTx(utxo, testAccount)
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  
    60  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
    61  		t.Fatal(err)
    62  	}
    63  
    64  	tx.SerializedSize = 1
    65  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
    66  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
    67  		t.Fatal(err)
    68  	}
    69  }
    70  
    71  func TestBip0032P2PKH(t *testing.T) {
    72  	dirPath, err := ioutil.TempDir(".", "TestP2PKH")
    73  	if err != nil {
    74  		t.Fatal(err)
    75  	}
    76  	defer os.RemoveAll(dirPath)
    77  
    78  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
    79  	defer os.RemoveAll("temp")
    80  
    81  	chain, _, _, err := test.MockChain(testDB)
    82  	if err != nil {
    83  		t.Fatal(err)
    84  	}
    85  
    86  	accountManager := account.NewManager(testDB, chain)
    87  	hsm, err := pseudohsm.New(dirPath)
    88  	if err != nil {
    89  		t.Fatal(err)
    90  	}
    91  
    92  	xpub, _, err := hsm.XCreate("TestP2PKH", "password", "en")
    93  	if err != nil {
    94  		t.Fatal(err)
    95  	}
    96  
    97  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub.XPub}, 1, "testAccount1", signers.BIP0044)
    98  	if err != nil {
    99  		t.Fatal(err)
   100  	}
   101  
   102  	signer, err := signers.Create("account", []chainkd.XPub{xpub.XPub}, 1, 2, signers.BIP0032)
   103  	if err != nil {
   104  		t.Fatal(err)
   105  	}
   106  
   107  	testAccount.Signer = signer
   108  	rawAccount, err := json.Marshal(testAccount)
   109  	if err != nil {
   110  		t.Fatal(err)
   111  	}
   112  
   113  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   114  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   115  	if err != nil {
   116  		t.Fatal(err)
   117  	}
   118  
   119  	utxo := test.MockUTXO(controlProg)
   120  	tpl, tx, err := test.MockTx(utxo, testAccount)
   121  	if err != nil {
   122  		t.Fatal(err)
   123  	}
   124  
   125  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   126  		t.Fatal(err)
   127  	}
   128  
   129  	tx.SerializedSize = 1
   130  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
   131  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
   132  		t.Fatal(err)
   133  	}
   134  }
   135  
   136  func TestP2SH(t *testing.T) {
   137  	dirPath, err := ioutil.TempDir(".", "TestP2SH")
   138  	if err != nil {
   139  		t.Fatal(err)
   140  	}
   141  	defer os.RemoveAll(dirPath)
   142  
   143  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   144  	defer os.RemoveAll("temp")
   145  
   146  	chain, _, _, err := test.MockChain(testDB)
   147  	if err != nil {
   148  		t.Fatal(err)
   149  	}
   150  
   151  	accountManager := account.NewManager(testDB, chain)
   152  	hsm, err := pseudohsm.New(dirPath)
   153  	if err != nil {
   154  		t.Fatal(err)
   155  	}
   156  
   157  	xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
   158  	if err != nil {
   159  		t.Fatal(err)
   160  	}
   161  
   162  	xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
   163  	if err != nil {
   164  		t.Fatal(err)
   165  	}
   166  
   167  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   168  	if err != nil {
   169  		t.Fatal(err)
   170  	}
   171  
   172  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   173  	if err != nil {
   174  		t.Fatal(err)
   175  	}
   176  
   177  	utxo := test.MockUTXO(controlProg)
   178  	tpl, tx, err := test.MockTx(utxo, testAccount)
   179  	if err != nil {
   180  		t.Fatal(err)
   181  	}
   182  
   183  	// the number of sign transaction is equal to the count of xpub for account
   184  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   185  		t.Fatal(err)
   186  	}
   187  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   188  		t.Fatal(err)
   189  	}
   190  
   191  	tx.SerializedSize = 1
   192  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
   193  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
   194  		t.Fatal(err)
   195  	}
   196  }
   197  
   198  func TestBip0032P2SH(t *testing.T) {
   199  	dirPath, err := ioutil.TempDir(".", "TestP2SH")
   200  	if err != nil {
   201  		t.Fatal(err)
   202  	}
   203  	defer os.RemoveAll(dirPath)
   204  
   205  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   206  	defer os.RemoveAll("temp")
   207  
   208  	chain, _, _, err := test.MockChain(testDB)
   209  	if err != nil {
   210  		t.Fatal(err)
   211  	}
   212  
   213  	accountManager := account.NewManager(testDB, chain)
   214  	hsm, err := pseudohsm.New(dirPath)
   215  	if err != nil {
   216  		t.Fatal(err)
   217  	}
   218  
   219  	xpub1, _, err := hsm.XCreate("TestP2SH1", "password", "en")
   220  	if err != nil {
   221  		t.Fatal(err)
   222  	}
   223  
   224  	xpub2, _, err := hsm.XCreate("TestP2SH2", "password", "en")
   225  	if err != nil {
   226  		t.Fatal(err)
   227  	}
   228  
   229  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   230  	if err != nil {
   231  		t.Fatal(err)
   232  	}
   233  	signer, err := signers.Create("account", []chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, 2, signers.BIP0032)
   234  	if err != nil {
   235  		t.Fatal(err)
   236  	}
   237  
   238  	testAccount.Signer = signer
   239  	rawAccount, err := json.Marshal(testAccount)
   240  	if err != nil {
   241  		t.Fatal(err)
   242  	}
   243  
   244  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   245  
   246  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   247  	if err != nil {
   248  		t.Fatal(err)
   249  	}
   250  
   251  	utxo := test.MockUTXO(controlProg)
   252  	tpl, tx, err := test.MockTx(utxo, testAccount)
   253  	if err != nil {
   254  		t.Fatal(err)
   255  	}
   256  
   257  	// the number of sign transaction is equal to the count of xpub for account
   258  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   259  		t.Fatal(err)
   260  	}
   261  	if _, err := test.MockSign(tpl, hsm, "password"); err != nil {
   262  		t.Fatal(err)
   263  	}
   264  
   265  	tx.SerializedSize = 1
   266  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
   267  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
   268  		t.Fatal(err)
   269  	}
   270  }
   271  
   272  func TestMutilNodeSign(t *testing.T) {
   273  	dirPath, err := ioutil.TempDir(".", "TestMutilNodeSign")
   274  	if err != nil {
   275  		t.Fatal(err)
   276  	}
   277  	defer os.RemoveAll(dirPath)
   278  
   279  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   280  	defer os.RemoveAll("temp")
   281  
   282  	chain, _, _, err := test.MockChain(testDB)
   283  	if err != nil {
   284  		t.Fatal(err)
   285  	}
   286  
   287  	accountManager := account.NewManager(testDB, chain)
   288  	hsm, err := pseudohsm.New(dirPath)
   289  	if err != nil {
   290  		t.Fatal(err)
   291  	}
   292  
   293  	xpub1, _, err := hsm.XCreate("TestMutilNodeSign1", "password1", "en")
   294  	if err != nil {
   295  		t.Fatal(err)
   296  	}
   297  
   298  	xpub2, _, err := hsm.XCreate("TestMutilNodeSign2", "password2", "en")
   299  	if err != nil {
   300  		t.Fatal(err)
   301  	}
   302  
   303  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   304  	if err != nil {
   305  		t.Fatal(err)
   306  	}
   307  
   308  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   309  	if err != nil {
   310  		t.Fatal(err)
   311  	}
   312  
   313  	utxo := test.MockUTXO(controlProg)
   314  	tpl, tx, err := test.MockTx(utxo, testAccount)
   315  	if err != nil {
   316  		t.Fatal(err)
   317  	}
   318  
   319  	if finishSign, err := test.MockSign(tpl, hsm, "password"); err != nil {
   320  		t.Fatal(err)
   321  	} else if finishSign == true {
   322  		t.Fatal("sign progress is finish, but either xpub1 nor xpub2 is signed")
   323  	}
   324  
   325  	if finishSign, err := test.MockSign(tpl, hsm, "password1"); err != nil {
   326  		t.Fatal(err)
   327  	} else if finishSign == true {
   328  		t.Fatal("sign progress is finish, but xpub2 is not signed")
   329  	}
   330  
   331  	if finishSign, err := test.MockSign(tpl, hsm, "password2"); err != nil {
   332  		t.Fatal(err)
   333  	} else if finishSign == false {
   334  		t.Fatal("sign progress is not finish,  but both xpub1 and xpub2 is signed")
   335  	}
   336  
   337  	tx.SerializedSize = 1
   338  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
   339  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
   340  		t.Fatal(err)
   341  	}
   342  }
   343  
   344  func TestBip0032MutilNodeSign(t *testing.T) {
   345  	dirPath, err := ioutil.TempDir(".", "TestMutilNodeSign")
   346  	if err != nil {
   347  		t.Fatal(err)
   348  	}
   349  	defer os.RemoveAll(dirPath)
   350  
   351  	testDB := dbm.NewDB("testdb", "leveldb", "temp")
   352  	defer os.RemoveAll("temp")
   353  
   354  	chain, _, _, err := test.MockChain(testDB)
   355  	if err != nil {
   356  		t.Fatal(err)
   357  	}
   358  
   359  	accountManager := account.NewManager(testDB, chain)
   360  	hsm, err := pseudohsm.New(dirPath)
   361  	if err != nil {
   362  		t.Fatal(err)
   363  	}
   364  
   365  	xpub1, _, err := hsm.XCreate("TestMutilNodeSign1", "password1", "en")
   366  	if err != nil {
   367  		t.Fatal(err)
   368  	}
   369  
   370  	xpub2, _, err := hsm.XCreate("TestMutilNodeSign2", "password2", "en")
   371  	if err != nil {
   372  		t.Fatal(err)
   373  	}
   374  
   375  	testAccount, err := accountManager.Create([]chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, "testAccount", signers.BIP0044)
   376  	if err != nil {
   377  		t.Fatal(err)
   378  	}
   379  
   380  	signer, err := signers.Create("account", []chainkd.XPub{xpub1.XPub, xpub2.XPub}, 2, 2, signers.BIP0032)
   381  	if err != nil {
   382  		t.Fatal(err)
   383  	}
   384  
   385  	testAccount.Signer = signer
   386  	rawAccount, err := json.Marshal(testAccount)
   387  	if err != nil {
   388  		t.Fatal(err)
   389  	}
   390  
   391  	testDB.Set(account.Key(testAccount.ID), rawAccount)
   392  
   393  	controlProg, err := accountManager.CreateAddress(testAccount.ID, false)
   394  	if err != nil {
   395  		t.Fatal(err)
   396  	}
   397  
   398  	utxo := test.MockUTXO(controlProg)
   399  	tpl, tx, err := test.MockTx(utxo, testAccount)
   400  	if err != nil {
   401  		t.Fatal(err)
   402  	}
   403  
   404  	if finishSign, err := test.MockSign(tpl, hsm, "password"); err != nil {
   405  		t.Fatal(err)
   406  	} else if finishSign == true {
   407  		t.Fatal("sign progress is finish, but either xpub1 nor xpub2 is signed")
   408  	}
   409  
   410  	if finishSign, err := test.MockSign(tpl, hsm, "password1"); err != nil {
   411  		t.Fatal(err)
   412  	} else if finishSign == true {
   413  		t.Fatal("sign progress is finish, but xpub2 is not signed")
   414  	}
   415  
   416  	if finishSign, err := test.MockSign(tpl, hsm, "password2"); err != nil {
   417  		t.Fatal(err)
   418  	} else if finishSign == false {
   419  		t.Fatal("sign progress is not finish,  but both xpub1 and xpub2 is signed")
   420  	}
   421  
   422  	tx.SerializedSize = 1
   423  	converter := func(prog []byte) ([]byte, error) { return nil, nil }
   424  	if _, err = validation.ValidateTx(types.MapTx(tx), test.MockBlock(), converter); err != nil {
   425  		t.Fatal(err)
   426  	}
   427  }