forked from jimilai/fabric
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
66 lines (58 loc) · 1.8 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
const Client = require('fabric-client')
const fs = require('fs')
const path = require('path')
const mspDir = '../network/crypto-config/peerOrganizations/org1.example.com/users/Admin@org1.example.com/msp/'
function loadKeyAndCert(){
let ret = {}
let keys = fs.readdirSync(path.join(mspDir,'keystore'))
if(keys.length === 0) throw "no key found"
ret.privateKeyPEM = fs.readFileSync(path.join(mspDir,'keystore',keys[0]),'utf8')
let certs = fs.readdirSync(path.join(mspDir,'signcerts'))
if(certs.length === 0) throw "no cert found"
ret.signedCertPEM = fs.readFileSync(path.join(mspDir,'signcerts',certs[0]),'utf8')
return ret
}
async function init(){
let client = new Client
let user = await client.createUser({
username: 'admin',
mspid: 'Org1MSP',
cryptoContent: loadKeyAndCert(),
skipPersistence: true
})
client.setUserContext(user,true)
let channel = client.newChannel('ch1')
let peer = client.newPeer('grpc://127.0.0.1:7051',{})
channel.addPeer(peer)
let orderer = client.newOrderer('grpc://127.0.0.1:7050',{})
channel.addOrderer(orderer)
return {client,channel,orderer,peer}
}
async function query(ctx,ccid,method){
let req = {
chaincodeId: ccid,
fcn: method,
args: Array.prototype.slice.call(arguments,3)
}
return await ctx.channel.queryByChaincode(req)
}
async function invoke(ctx,ccid,method){
let req = {
chaincodeId: ccid,
fcn: method,
args: Array.prototype.slice.call(arguments,3),
txId: ctx.client.newTransactionID()
}
let rsp = await ctx.channel.sendTransactionProposal(req)
let ret = await ctx.channel.sendTransaction({
proposalResponses: rsp[0],
proposal: rsp[1]
})
return ret
}
async function main(){
let ctx = await init()
console.log(await query(ctx,'wizcc'))
console.log(await invoke(ctx,'wizcc'))
}
main()