@@ -14,11 +14,13 @@ import (
14
14
"syscall"
15
15
"time"
16
16
17
+ "github.com/hyperledger/fabric/bccsp/sw"
18
+ "github.com/pkg/errors"
19
+
17
20
docker "github.com/fsouza/go-dockerclient"
18
21
"github.com/golang/protobuf/proto"
19
22
"github.com/hyperledger/fabric/integration/nwo"
20
- mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
21
- peercommon "github.com/hyperledger/fabric/peer/common"
23
+ "github.com/hyperledger/fabric/msp"
22
24
"github.com/hyperledger/fabric/protos/common"
23
25
"github.com/hyperledger/fabric/protos/token"
24
26
tk "github.com/hyperledger/fabric/token"
@@ -37,6 +39,7 @@ var _ = Describe("Token EndToEnd", func() {
37
39
38
40
tokensToIssue []* token.TokenToIssue
39
41
expectedTokenTransaction * token.TokenTransaction
42
+ expectedUnspentTokens * token.UnspentTokens
40
43
)
41
44
42
45
BeforeEach (func () {
@@ -60,6 +63,16 @@ var _ = Describe("Token EndToEnd", func() {
60
63
},
61
64
}
62
65
66
+ expectedUnspentTokens = & token.UnspentTokens {
67
+ Tokens : []* token.TokenOutput {
68
+ {
69
+ Quantity : 119 ,
70
+ Type : "ABC123" ,
71
+ Id : []byte ("ledger-id" ),
72
+ },
73
+ },
74
+ }
75
+
63
76
var err error
64
77
testDir , err = ioutil .TempDir ("" , "token-e2e" )
65
78
Expect (err ).NotTo (HaveOccurred ())
@@ -94,6 +107,13 @@ var _ = Describe("Token EndToEnd", func() {
94
107
client , err = docker .NewClientFromEnv ()
95
108
Expect (err ).NotTo (HaveOccurred ())
96
109
110
+ peer := network .Peer ("Org1" , "peer1" )
111
+ // Get recipient's identity
112
+ recipient , err := getIdentity (network , peer , "User2" , "Org1MSP" )
113
+ Expect (err ).NotTo (HaveOccurred ())
114
+ tokensToIssue [0 ].Recipient = recipient
115
+ expectedTokenTransaction .GetPlainAction ().GetPlainImport ().Outputs [0 ].Owner = recipient
116
+
97
117
networkRunner := network .NetworkGroupRunner ()
98
118
process = ifrit .Invoke (networkRunner )
99
119
Eventually (process .Ready ()).Should (BeClosed ())
@@ -118,6 +138,14 @@ var _ = Describe("Token EndToEnd", func() {
118
138
119
139
By ("issuing tokens" )
120
140
RunIssueRequest (tClient , tokensToIssue , expectedTokenTransaction )
141
+
142
+ By ("list tokens" )
143
+ config = getClientConfig (network , peer , orderer , "testchannel" , "User2" , "Org1MSP" )
144
+ signingIdentity , err = getSigningIdentity (config .MSPInfo .MSPConfigPath , config .MSPInfo .MSPID , config .MSPInfo .MSPType )
145
+ Expect (err ).NotTo (HaveOccurred ())
146
+ tClient , err = tokenclient .NewClient (* config , signingIdentity )
147
+ Expect (err ).NotTo (HaveOccurred ())
148
+ RunListTokens (tClient , expectedUnspentTokens )
121
149
})
122
150
})
123
151
})
@@ -140,6 +168,19 @@ func RunIssueRequest(c *tokenclient.Client, tokens []*token.TokenToIssue, expect
140
168
Expect (tokenTxid ).To (Equal (txid ))
141
169
}
142
170
171
+ func RunListTokens (c * tokenclient.Client , expectedUnspentTokens * token.UnspentTokens ) []* token.TokenOutput {
172
+ tokenOutputs , err := c .ListTokens ()
173
+ Expect (err ).NotTo (HaveOccurred ())
174
+
175
+ // unmarshall CommandResponse and verify the result
176
+ Expect (len (tokenOutputs )).To (Equal (len (expectedUnspentTokens .Tokens )))
177
+ for i , token := range expectedUnspentTokens .Tokens {
178
+ Expect (tokenOutputs [i ].Type ).To (Equal (token .Type ))
179
+ Expect (tokenOutputs [i ].Quantity ).To (Equal (token .Quantity ))
180
+ }
181
+ return tokenOutputs
182
+ }
183
+
143
184
func getClientConfig (n * nwo.Network , peer * nwo.Peer , orderer * nwo.Orderer , channelId , user , mspID string ) * tokenclient.ClientConfig {
144
185
mspDir := n .PeerUserMSPDir (peer , user )
145
186
peerAddr := n .PeerAddress (peer , nwo .ListenPort )
@@ -174,12 +215,39 @@ func getClientConfig(n *nwo.Network, peer *nwo.Peer, orderer *nwo.Orderer, chann
174
215
return & config
175
216
}
176
217
218
+ func getIdentity (n * nwo.Network , peer * nwo.Peer , user , mspId string ) ([]byte , error ) {
219
+ orderer := n .Orderer ("orderer" )
220
+ config := getClientConfig (n , peer , orderer , "testchannel" , user , mspId )
221
+
222
+ localMSP , err := LoadLocalMSPAt (config .MSPInfo .MSPConfigPath , config .MSPInfo .MSPID , config .MSPInfo .MSPType )
223
+ if err != nil {
224
+ return nil , err
225
+ }
226
+
227
+ signer , err := localMSP .GetDefaultSigningIdentity ()
228
+ if err != nil {
229
+ return nil , err
230
+ }
231
+
232
+ creator , err := signer .Serialize ()
233
+ if err != nil {
234
+ return nil , err
235
+ }
236
+
237
+ return creator , nil
238
+ }
239
+
177
240
func getSigningIdentity (mspConfigPath , mspID , mspType string ) (tk.SigningIdentity , error ) {
178
- peercommon .InitCrypto (mspConfigPath , mspID , mspType )
179
- signingIdentity , err := mspmgmt .GetLocalMSP ().GetDefaultSigningIdentity ()
241
+ mspInstance , err := LoadLocalMSPAt (mspConfigPath , mspID , mspType )
180
242
if err != nil {
181
243
return nil , err
182
244
}
245
+
246
+ signingIdentity , err := mspInstance .GetDefaultSigningIdentity ()
247
+ if err != nil {
248
+ return nil , err
249
+ }
250
+
183
251
return signingIdentity , nil
184
252
}
185
253
@@ -195,3 +263,28 @@ func updateConfigtx(network *nwo.Network) error {
195
263
output := bytes .Replace (input , []byte ("CAPABILITY_PLACEHOLDER: false" ), []byte ("V1_4_FABTOKEN_EXPERIMENTAL: true" ), - 1 )
196
264
return ioutil .WriteFile (filepath , output , 0644 )
197
265
}
266
+
267
+ // LoadLocalMSPAt loads an MSP whose configuration is stored at 'dir', and whose
268
+ // id and type are the passed as arguments.
269
+ func LoadLocalMSPAt (dir , id , mspType string ) (msp.MSP , error ) {
270
+ if mspType != "bccsp" {
271
+ return nil , errors .Errorf ("invalid msp type, expected 'bccsp', got %s" , mspType )
272
+ }
273
+ conf , err := msp .GetLocalMspConfig (dir , nil , id )
274
+ if err != nil {
275
+ return nil , err
276
+ }
277
+ ks , err := sw .NewFileBasedKeyStore (nil , filepath .Join (dir , "keystore" ), true )
278
+ if err != nil {
279
+ return nil , err
280
+ }
281
+ thisMSP , err := msp .NewBccspMspWithKeyStore (msp .MSPv1_0 , ks )
282
+ if err != nil {
283
+ return nil , err
284
+ }
285
+ err = thisMSP .Setup (conf )
286
+ if err != nil {
287
+ return nil , err
288
+ }
289
+ return thisMSP , nil
290
+ }
0 commit comments