tgrospic / rnode-grpc-js

RNode gRPC/HTTP API generator for Nodejs and the browser
https://tgrospic.github.io/rnode-grpc-js/
MIT License
9 stars 6 forks source link

how to import signDeploy etc. into a web page script module? #8

Closed dckc closed 4 years ago

dckc commented 4 years ago

I'd like to do:

<script type="module">
import { signDeploy } from '???';
</script>

Is this feasible without a bundler?

tgrospic commented 4 years ago

Short answer is no, but... First you need crypto stuff for signing and hashing, this shouldn't be hard. The bigger thing is to create byte array defined in DeployDataProto specifiction. For that you need protobuf serializer (google-protobuf) and the piece of generated code. The same thing which is used here. https://github.com/tgrospic/rnode-grpc-js/blob/a842459e0dcab3b4c4db33de110c7629c9d1564a/src/rnode-sign.js#L34-L41 And generated code should be something like this (rnode-grpc-gen/js/CasperMessage_pb.js).

proto.casper.DeployDataProto.serializeBinaryToWriter = function(message, writer) {
  var f = undefined;
  f = message.getDeployer_asU8();
  if (f.length > 0) {
    writer.writeBytes(
      1,
      f
    );
  }
  f = message.getTerm();
  if (f.length > 0) {
    writer.writeString(
      2,
      f
    );
  }
.
.
.
  f = message.getValidafterblocknumber();
  if (f !== 0) {
    writer.writeInt64(
      10,
      f
    );
  }
};
dckc commented 4 years ago

I wonder if https://jspm.io/ would help...

dckc commented 4 years ago

In my research @pika/pack kept coming up; it seems to have turned into snowpack. I thought I was having good luck until it came time to make a gRPC call:

grpc-web.js:19 Uncaught (in promise) TypeError: Cannot read property 'constructor' of undefined
    at ra (grpc-web.js:19)
    at Z.unaryCall (grpc-web.js:53)
    at _callee$ (rnode-grpc-js.js:14504)
    at tryCatch (rnode-grpc-js.js:3213)
    at Generator.invoke [as _invoke] (rnode-grpc-js.js:3442)
    at Generator.prototype.<computed> [as next] (rnode-grpc-js.js:3265)
    at asyncGeneratorStep (rnode-grpc-js.js:14246)
    at _next (rnode-grpc-js.js:14248)
    at rnode-grpc-js.js:14248
    at new Promise (<anonymous>)

debugging with packed code is no fun. :-/

dckc commented 4 years ago

looks like ES6 module support is still on the todo/wish list for grpc-web: https://github.com/grpc/grpc-web/issues/535

tgrospic commented 4 years ago

@dckc I'm not sure if you want to use grpc-web because you need gRPC/HTTP proxy to be able to connect to it from the browser.

RNode Web API is not using protobuf for communication. It's used only on one place to create serialized bytes by DeployDataProto specification.

I don't have better documentation for Web API then the PR. Interface with types are defined here. https://github.com/rchain/rchain/pull/2811/files#diff-508493cc346df97c0279f7e95148e39aR20 And routes are here. https://github.com/rchain/rchain/pull/2811/files#diff-bfac79137af55cd148456e7fa3a27ef9R98

tgrospic commented 4 years ago

@dckc here is the function to serialize deploy data with protobuf directly without code generation. Protobuf definition specify position. You don't need rnode-grpc-js when you are on the web.

// message DeployDataProto {
//   bytes  deployer     = 1; //public key
//   string term         = 2; //rholang source code to deploy (will be parsed into `Par`)
//   int64  timestamp    = 3; //millisecond timestamp
//   bytes  sig          = 4; //signature of (hash(term) + timestamp) using private key
//   string sigAlgorithm = 5; //name of the algorithm used to sign
//   int64 phloPrice     = 7; //phlo price
//   int64 phloLimit     = 8; //phlo limit for the deployment
//   int64 validAfterBlockNumber = 10;
// }

import jspb from 'google-protobuf'

const deployDataProtobufSerialize = deployData => {
  const { term, timestamp, phloPrice, phloLimit, validAfterBlockNumber } = deployData

  // Create binary stream writer
  const writer = new jspb.BinaryWriter()

  // Serialize fields
  writer.writeString(2, term)
  writer.writeInt64(3, timestamp)
  writer.writeInt64(7, phloPrice)
  writer.writeInt64(8, phloLimit)
  writer.writeInt64(10, validAfterBlockNumber)

  return writer.getResultBuffer()
}
dckc commented 4 years ago

Thanks!

That suggests this issue is out of scope of this repo. I wonder where it belongs. An RChain web SDK repo sure would be nice.