Skip to content
This repository has been archived by the owner on Feb 22, 2024. It is now read-only.

Implement grpc-web #4

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand All @@ -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"
}
}
80 changes: 80 additions & 0 deletions src/client/grpc-web.js
Original file line number Diff line number Diff line change
@@ -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;
220 changes: 220 additions & 0 deletions src/protocol/api/api_pb_service.js
Original file line number Diff line number Diff line change
@@ -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;

18 changes: 8 additions & 10 deletions test/client.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
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)
}
});

it('list witnesses', async () => {
let witnesses = await client.getWitnesses();

for (let witness of witnesses) {
console.log("witness", witness.getUrl());
console.log("witness", witness.url);
}
});

Expand All @@ -32,16 +30,16 @@ describe('client', () => {

for (let account of accounts) {
console.log("account", {
balance: account.getBalance(),
name: bytesToString(account.getAccountName()),
balance: account.balance,
name: bytesToString(account.accountName),
});
}
});

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);
}
});
});