1// SPDX-License-Identifier: ISC
2// Copyright (c) 2014-2020 Bitmark Inc.
3// Use of this source code is governed by an ISC
4// license that can be found in the LICENSE file.
5
6package owner_test
7
8import (
9	"crypto/ed25519"
10	"testing"
11
12	"github.com/golang/mock/gomock"
13	"github.com/stretchr/testify/assert"
14
15	"github.com/bitmark-inc/bitmarkd/account"
16	"github.com/bitmark-inc/bitmarkd/chain"
17	"github.com/bitmark-inc/bitmarkd/merkle"
18	"github.com/bitmark-inc/bitmarkd/mode"
19	"github.com/bitmark-inc/bitmarkd/ownership"
20	"github.com/bitmark-inc/bitmarkd/reservoir"
21	"github.com/bitmark-inc/bitmarkd/rpc/fixtures"
22	"github.com/bitmark-inc/bitmarkd/rpc/mocks"
23	"github.com/bitmark-inc/bitmarkd/rpc/owner"
24	"github.com/bitmark-inc/bitmarkd/transactionrecord"
25	"github.com/bitmark-inc/logger"
26)
27
28func TestOwnerBitmarks(t *testing.T) {
29	fixtures.SetupTestLogger()
30	defer fixtures.TeardownTestLogger()
31
32	mode.Initialise(chain.Testing)
33	defer mode.Finalise()
34
35	ctl := gomock.NewController(t)
36	defer ctl.Finish()
37
38	tr := mocks.NewMockHandle(ctl)
39	a := mocks.NewMockHandle(ctl)
40	os := mocks.NewMockOwnership(ctl)
41
42	o := owner.New(
43		logger.New(fixtures.LogCategory),
44		reservoir.Handles{
45			Assets:       a,
46			Transactions: tr,
47		},
48		os,
49	)
50
51	acc := account.Account{
52		AccountInterface: &account.ED25519Account{
53			Test:      true,
54			PublicKey: fixtures.IssuerPublicKey,
55		},
56	}
57
58	arg := owner.BitmarksArguments{
59		Owner: &acc,
60		Start: 5,
61		Count: 10,
62	}
63
64	n := uint64(3)
65	ass := transactionrecord.NewAssetIdentifier([]byte{1, 2, 3, 4})
66
67	r := ownership.Record{
68		N:           1,
69		TxId:        merkle.Digest{},
70		IssueTxId:   merkle.Digest{},
71		Item:        ownership.OwnedAsset,
72		AssetId:     &ass,
73		BlockNumber: &n,
74	}
75
76	ad := transactionrecord.AssetData{
77		Name:        "test",
78		Fingerprint: "fingerprint",
79		Metadata:    "owner\x00me",
80		Registrant:  &acc,
81		Signature:   nil,
82	}
83	packed, _ := ad.Pack(&acc)
84	ad.Signature = ed25519.Sign(fixtures.IssuerPrivateKey, packed)
85	packed, _ = ad.Pack(&acc)
86
87	os.EXPECT().ListBitmarksFor(arg.Owner, arg.Start, arg.Count).Return([]ownership.Record{r}, nil).Times(1)
88	tr.EXPECT().GetNB(r.TxId[:]).Return(uint64(1), packed).Times(1)
89	a.EXPECT().GetNB(r.AssetId[:]).Return(uint64(1), packed).Times(1)
90
91	var reply owner.BitmarksReply
92	err := o.Bitmarks(&arg, &reply)
93	assert.Nil(t, err, "wrong Bitmarks")
94	assert.Equal(t, r.N+1, reply.Next, "wrong next")
95	assert.Equal(t, 1, len(reply.Data), "wrong record count")
96	assert.Equal(t, r, reply.Data[0], "wrong asset")
97	assert.Equal(t, 2, len(reply.Tx), "wrong tx count")
98	assert.Equal(t, ad, *reply.Tx[r.TxId.String()].Data.(*transactionrecord.AssetData), "wrong first record")
99	assert.Equal(t, ad, *reply.Tx[r.TxId.String()].Data.(*transactionrecord.AssetData))
100}
101