Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
dckc opened this issue Jun 15, 2020 · 7 comments
Closed

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

dckc opened this issue Jun 15, 2020 · 7 comments

Comments

@dckc
Copy link

dckc commented Jun 15, 2020

I'd like to do:

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

Is this feasible without a bundler?

@tgrospic
Copy link
Owner

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.

// Serialize deploy data for signing
const dd = new DeployData()
dd.setTerm(term)
dd.setTimestamp(timestamp)
dd.setPhloprice(phloprice)
dd.setPhlolimit(phlolimit)
dd.setValidafterblocknumber(validafterblocknumber)
const deploySerialized = dd.serializeBinary()

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
Copy link
Author

dckc commented Jun 15, 2020

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

@dckc
Copy link
Author

dckc commented Jun 22, 2020

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
Copy link
Author

dckc commented Jun 22, 2020

looks like ES6 module support is still on the todo/wish list for grpc-web: grpc/grpc-web#535

@tgrospic
Copy link
Owner

@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
Copy link
Owner

@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
Copy link
Author

dckc commented Jun 25, 2020

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.

@dckc dckc closed this as completed Jun 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants