From 4945b9d285e43ea16c9eb75df42f38f454d4ac0e Mon Sep 17 00:00:00 2001 From: Roy van Kaathoven Date: Sat, 14 Apr 2018 00:33:10 +0200 Subject: [PATCH] implement grpc-web --- package.json | 5 +- src/client/grpc-web.js | 80 +++++++++++ src/protocol/api/api_pb_service.js | 220 +++++++++++++++++++++++++++++ test/client.js | 18 ++- 4 files changed, 312 insertions(+), 11 deletions(-) create mode 100644 src/client/grpc-web.js create mode 100644 src/protocol/api/api_pb_service.js diff --git a/package.json b/package.json index 513f58f..8ed2b45 100644 --- a/package.json +++ b/package.json @@ -8,6 +8,7 @@ "test": "mocha", "protoc:install": "cd protocol && ./install-protobuf.sh", "protoc:generate": "./protocol/protobuf-3.5.1/src/.libs/protoc --proto_path=protocol/protobuf-3.5.1/src --proto_path=protocol --grpc_out=./src/protocol --plugin=protoc-gen-grpc=`which grpc_tools_node_protoc_plugin` --js_out=import_style=commonjs,binary:src/protocol protocol/core/*.proto protocol/api/*.proto", + "protoc:generate-js": "./protocol/protobuf-3.5.1/src/.libs/protoc --proto_path=protocol/protobuf-3.5.1/src --proto_path=protocol --plugin=protoc-gen-ts=./node_modules/.bin/protoc-gen-ts --ts_out=service=true:src/protocol --js_out=import_style=commonjs,binary:src/protocol protocol/core/*.proto protocol/api/*.proto", "test:watch": "mocha --watch" }, "repository": { @@ -28,8 +29,10 @@ "grpc": "^1.10.0", "grpc-caller": "^0.4.0", "grpc-tools": "^1.6.6", + "grpc-web-client": "^0.5.0", "js-sha3": "^0.7.0", "mocha": "^5.0.5", - "qs": "^6.5.1" + "qs": "^6.5.1", + "ts-protoc-gen": "^0.5.2" } } diff --git a/src/client/grpc-web.js b/src/client/grpc-web.js new file mode 100644 index 0000000..2dc1ccf --- /dev/null +++ b/src/client/grpc-web.js @@ -0,0 +1,80 @@ +const {grpc} = require("grpc-web-client"); +const {EmptyMessage, NumberMessage} = require("../protocol/api/api_pb"); +const {Wallet} = require("../protocol/api/api_pb_service"); + +class GRPCWebClient { + + constructor(options = {}) { + this.url = options.url || `http://localhost:8080`; + } + + makeRequest(method, request) { + return new Promise(resolve => { + grpc.unary(method, { + request: request, + host: this.url, + onEnd: res => { + const { status, statusMessage, headers, message, trailers } = res; + if (status === grpc.Code.OK && message) { + resolve(message.toObject()); + } + } + }); + }); + } + + /** + * Retrieve all connected witnesses + * + * @returns {Promise<*>} + */ + async getWitnesses() { + let {witnessesList} = await this.makeRequest(Wallet.ListWitnesses, new EmptyMessage()); + return witnessesList; + } + + /** + * Retrieve all connected nodes + * + * @returns {Promise<*>} + */ + async getNodes() { + let {nodesList} = await this.makeRequest(Wallet.ListNodes, new EmptyMessage()); + return nodesList; + } + + /** + * Retrieve all accounts + * + * @returns {Promise<*>} + */ + async getAccounts() { + let {accountsList} = await this.makeRequest(Wallet.ListAccounts, new EmptyMessage()); + return accountsList; + } + + /** + * Retrieves a block by the given number + * + * @param {number} number block number + * @returns {Promise<*>} + */ + async getBlockByNum(number) { + let message = new NumberMessage(); + message.setNum(number); + return await this.makeRequest(Wallet.GetBlockByNum, message); + } + + async getLatestBlock() { + let message = new NumberMessage(); + message.setNum(-1); + return await this.makeRequest(Wallet.GetBlockByNum, message); + } + + async getTotalNumberOfTransactions() { + let {num} = await this.makeRequest(Wallet.TotalTransaction, new EmptyMessage()); + return num; + } +} + +module.exports = GRPCWebClient; diff --git a/src/protocol/api/api_pb_service.js b/src/protocol/api/api_pb_service.js new file mode 100644 index 0000000..3810523 --- /dev/null +++ b/src/protocol/api/api_pb_service.js @@ -0,0 +1,220 @@ +// package: protocol +// file: api/api.proto + +var api_api_pb = require("../api/api_pb"); +var core_Tron_pb = require("../core/Tron_pb"); +var core_Contract_pb = require("../core/Contract_pb"); + +var Wallet = (function () { + function Wallet() {} + Wallet.serviceName = "protocol.Wallet"; + return Wallet; +}()); + +Wallet.GetAccount = { + methodName: "GetAccount", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Tron_pb.Account, + responseType: core_Tron_pb.Account +}; + +Wallet.CreateTransaction = { + methodName: "CreateTransaction", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.TransferContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.BroadcastTransaction = { + methodName: "BroadcastTransaction", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Tron_pb.Transaction, + responseType: api_api_pb.Return +}; + +Wallet.ListAccounts = { + methodName: "ListAccounts", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: api_api_pb.AccountList +}; + +Wallet.UpdateAccount = { + methodName: "UpdateAccount", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.AccountUpdateContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.CreateAccount = { + methodName: "CreateAccount", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.AccountCreateContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.VoteWitnessAccount = { + methodName: "VoteWitnessAccount", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.VoteWitnessContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.CreateAssetIssue = { + methodName: "CreateAssetIssue", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.AssetIssueContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.ListWitnesses = { + methodName: "ListWitnesses", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: api_api_pb.WitnessList +}; + +Wallet.UpdateWitness = { + methodName: "UpdateWitness", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.WitnessUpdateContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.CreateWitness = { + methodName: "CreateWitness", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.WitnessCreateContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.TransferAsset = { + methodName: "TransferAsset", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.TransferAssetContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.ParticipateAssetIssue = { + methodName: "ParticipateAssetIssue", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Contract_pb.ParticipateAssetIssueContract, + responseType: core_Tron_pb.Transaction +}; + +Wallet.ListNodes = { + methodName: "ListNodes", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: api_api_pb.NodeList +}; + +Wallet.GetAssetIssueList = { + methodName: "GetAssetIssueList", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: api_api_pb.AssetIssueList +}; + +Wallet.GetAssetIssueByAccount = { + methodName: "GetAssetIssueByAccount", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: core_Tron_pb.Account, + responseType: api_api_pb.AssetIssueList +}; + +Wallet.GetAssetIssueByName = { + methodName: "GetAssetIssueByName", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.BytesMessage, + responseType: core_Contract_pb.AssetIssueContract +}; + +Wallet.GetNowBlock = { + methodName: "GetNowBlock", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: core_Tron_pb.Block +}; + +Wallet.GetBlockByNum = { + methodName: "GetBlockByNum", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.NumberMessage, + responseType: core_Tron_pb.Block +}; + +Wallet.TotalTransaction = { + methodName: "TotalTransaction", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: api_api_pb.NumberMessage +}; + +Wallet.GetDynamicProperties = { + methodName: "GetDynamicProperties", + service: Wallet, + requestStream: false, + responseStream: false, + requestType: api_api_pb.EmptyMessage, + responseType: core_Tron_pb.DynamicProperties +}; + +exports.Wallet = Wallet; + +var Database = (function () { + function Database() {} + Database.serviceName = "protocol.Database"; + return Database; +}()); + +exports.Database = Database; + +var Network = (function () { + function Network() {} + Network.serviceName = "protocol.Network"; + return Network; +}()); + +exports.Network = Network; + diff --git a/test/client.js b/test/client.js index 6169fe4..ab84e92 100644 --- a/test/client.js +++ b/test/client.js @@ -1,21 +1,19 @@ const {bytesToString} = require("../src/utils/bytes"); -const {GrpcClient} = require("../src"); +const GRPCWebClient = require("../src/client/grpc-web"); const assert = require("assert"); describe('client', () => { - return; - - let client = new GrpcClient({ + let client = new GRPCWebClient({ hostname: "localhost", - port: 50051, + port: 8080, }); it('list nodes', async () => { let nodes = await client.getNodes(); for (let node of nodes) { - console.log("node", node.getAddress().getHost_asB64()) + console.log("node", node.address) } }); @@ -23,7 +21,7 @@ describe('client', () => { let witnesses = await client.getWitnesses(); for (let witness of witnesses) { - console.log("witness", witness.getUrl()); + console.log("witness", witness.url); } }); @@ -32,8 +30,8 @@ describe('client', () => { for (let account of accounts) { console.log("account", { - balance: account.getBalance(), - name: bytesToString(account.getAccountName()), + balance: account.balance, + name: bytesToString(account.accountName), }); } }); @@ -41,7 +39,7 @@ describe('client', () => { it('retrieve block by number', async () => { for (let blockNumber = 0; blockNumber < 10; blockNumber++) { let block = await client.getBlockByNumber(blockNumber); - assert.equal(block.getBlockHeader().getRawData().getNumber(), blockNumber); + assert.equal(block.blockHeader.rawData.number, blockNumber); } }); });