10 "github.com/davecgh/go-spew/spew"
12 "golang.org/x/crypto/sha3"
14 "github.com/bytom/crypto/ed25519"
15 "github.com/bytom/crypto/ed25519/chainkd"
16 "github.com/bytom/encoding/json"
17 "github.com/bytom/errors"
18 "github.com/bytom/protocol/bc"
19 "github.com/bytom/protocol/bc/types"
20 "github.com/bytom/protocol/vm"
21 "github.com/bytom/protocol/vm/vmutil"
22 "github.com/bytom/testutil"
25 type testAction bc.AssetAmount
27 func (t testAction) Build(ctx context.Context, b *TemplateBuilder) error {
28 in := types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), *t.AssetId, t.Amount, 0, nil)
29 tplIn := &SigningInstruction{}
31 err := b.AddInput(in, tplIn)
35 return b.AddOutput(types.NewTxOutput(*t.AssetId, t.Amount, []byte("change")))
38 func newControlProgramAction(assetAmt bc.AssetAmount, script []byte) *controlProgramAction {
39 return &controlProgramAction{
40 AssetAmount: assetAmt,
45 func TestBuild(t *testing.T) {
46 ctx := context.Background()
48 assetID1 := bc.NewAssetID([32]byte{1})
49 assetID2 := bc.NewAssetID([32]byte{2})
52 newControlProgramAction(bc.AssetAmount{AssetId: &assetID2, Amount: 6}, []byte("dest")),
53 testAction(bc.AssetAmount{AssetId: &assetID1, Amount: 5}),
55 expiryTime := time.Now().Add(time.Minute)
56 got, err := Build(ctx, nil, actions, expiryTime, 0)
58 testutil.FatalErr(t, err)
62 Transaction: types.NewTx(types.TxData{
65 Inputs: []*types.TxInput{
66 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), assetID1, 5, 0, nil),
68 Outputs: []*types.TxOutput{
69 types.NewTxOutput(assetID2, 6, []byte("dest")),
70 types.NewTxOutput(assetID1, 5, []byte("change")),
73 SigningInstructions: []*SigningInstruction{{
74 WitnessComponents: []witnessComponent{},
78 if !testutil.DeepEqual(got.Transaction.TxData, want.Transaction.TxData) {
79 t.Errorf("got tx:\n%s\nwant tx:\n%s", spew.Sdump(got.Transaction.TxData), spew.Sdump(want.Transaction.TxData))
82 if !testutil.DeepEqual(got.SigningInstructions, want.SigningInstructions) {
83 t.Errorf("got signing instructions:\n\t%#v\nwant signing instructions:\n\t%#v", got.SigningInstructions, want.SigningInstructions)
87 func TestSignatureWitnessMaterialize(t *testing.T) {
88 privkey1, pubkey1, err := chainkd.NewXKeys(nil)
92 privkey2, pubkey2, err := chainkd.NewXKeys(nil)
96 privkey3, pubkey3, err := chainkd.NewXKeys(nil)
100 issuanceProg, _ := vmutil.P2SPMultiSigProgram([]ed25519.PublicKey{pubkey1.PublicKey(), pubkey2.PublicKey(), pubkey3.PublicKey()}, 2)
101 assetID := bc.ComputeAssetID(issuanceProg, 1, &bc.EmptyStringHash)
102 outscript := mustDecodeHex("76a914c5d128911c28776f56baaac550963f7b88501dc388c0")
103 unsigned := types.NewTx(types.TxData{
105 Inputs: []*types.TxInput{
106 types.NewIssuanceInput([]byte{1}, 100, issuanceProg, nil, nil),
108 Outputs: []*types.TxOutput{
109 types.NewTxOutput(assetID, 100, outscript),
114 Transaction: unsigned,
117 builder := vmutil.NewBuilder()
118 builder.AddData(h.Bytes())
119 builder.AddOp(vm.OP_TXSIGHASH).AddOp(vm.OP_EQUAL)
120 prog, _ := builder.Build()
121 msg := sha3.Sum256(prog)
122 sig1 := privkey1.Sign(msg[:])
123 sig2 := privkey2.Sign(msg[:])
124 sig3 := privkey3.Sign(msg[:])
132 // Test with more signatures than required, in correct order
133 tpl.SigningInstructions = []*SigningInstruction{{
134 WitnessComponents: []witnessComponent{
140 DerivationPath: []json.HexBytes{{0, 0, 0, 0}},
144 DerivationPath: []json.HexBytes{{0, 0, 0, 0}},
148 DerivationPath: []json.HexBytes{{0, 0, 0, 0}},
152 Sigs: []json.HexBytes{sig1, sig2, sig3},
156 err = materializeWitnesses(tpl)
158 testutil.FatalErr(t, err)
160 got := tpl.Transaction.Inputs[0].Arguments()
161 if !testutil.DeepEqual(got, want) {
162 t.Errorf("got input witness %v, want input witness %v", got, want)
165 // Test with exact amount of signatures required, in correct order
166 component := tpl.SigningInstructions[0].WitnessComponents[0].(*SignatureWitness)
167 component.Sigs = []json.HexBytes{sig1, sig2}
168 err = materializeWitnesses(tpl)
170 testutil.FatalErr(t, err)
172 got = tpl.Transaction.Inputs[0].Arguments()
173 if !testutil.DeepEqual(got, want) {
174 t.Errorf("got input witness %v, want input witness %v", got, want)
178 func mustDecodeHex(str string) []byte {
179 data, err := hex.DecodeString(str)
186 func TestCheckBlankCheck(t *testing.T) {
192 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
197 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
198 Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 3, nil)},
203 Inputs: []*types.TxInput{
204 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil),
205 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.NewAssetID([32]byte{1}), 5, 0, nil),
207 Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
212 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
213 Outputs: []*types.TxOutput{
214 types.NewTxOutput(bc.AssetID{}, math.MaxInt64, nil),
215 types.NewTxOutput(bc.AssetID{}, 7, nil),
221 Inputs: []*types.TxInput{
222 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil),
223 types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, math.MaxInt64, 0, nil),
229 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
230 Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
235 Outputs: []*types.TxOutput{types.NewTxOutput(bc.AssetID{}, 5, nil)},
240 Inputs: []*types.TxInput{types.NewSpendInput(nil, bc.NewHash([32]byte{0xff}), bc.AssetID{}, 5, 0, nil)},
241 Outputs: []*types.TxOutput{types.NewTxOutput(bc.NewAssetID([32]byte{1}), 5, nil)},
246 for _, c := range cases {
247 got := checkBlankCheck(c.tx)
248 if errors.Root(got) != c.want {
249 t.Errorf("checkUnsafe(%+v) err = %v want %v", c.tx, errors.Root(got), c.want)