Skip to content

Instantly share code, notes, and snippets.

@michaelfig
Last active May 10, 2022 08:01
Show Gist options
  • Save michaelfig/91d6f64ab726460f8e20b9f805f59bd3 to your computer and use it in GitHub Desktop.
Save michaelfig/91d6f64ab726460f8e20b9f805f59bd3 to your computer and use it in GitHub Desktop.
cosmjs-types running in a SES compartment
diff --git a/node_modules/@protobufjs/inquire/index.js b/node_modules/@protobufjs/inquire/index.js
index 33778b5..a7aee2d 100644
--- a/node_modules/@protobufjs/inquire/index.js
+++ b/node_modules/@protobufjs/inquire/index.js
@@ -9,7 +9,7 @@ module.exports = inquire;
*/
function inquire(moduleName) {
try {
- var mod = eval("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
+ var mod = (eval)("quire".replace(/^/,"re"))(moduleName); // eslint-disable-line no-eval
if (mod && (mod.length || Object.keys(mod).length))
return mod;
} catch (e) {} // eslint-disable-line no-empty

Background

It would be useful for the @protobufjs suite to work in a Compartment. We would like this for the Agoric blockchain.

I had to patch @protobufjs/inquire so that it didn't tickle SES's direct eval rejection. Also, there were some portability problems in protobufjs/src/util/minimal.js.

The relevant issue on protobufjs is protobufjs/protobuf.js#1713.

Prep

mkdir patches
mv *.patch patches/
yarn install

Test under Node.js

node user.js

Gives the expected result:

MsgSend Uint8Array(112) [
   10,  45,  97, 103, 111, 114, 105,  99,  49,  55, 110, 100,
  122, 102,  53, 115, 103,  57, 115, 120,  50, 102, 119,  56,
  121, 122, 122, 101, 100, 100, 120, 121,  48, 104, 115, 119,
  112,  48, 116,  56, 114,  51,  53,  52,  50, 112,  52,  18,
   45,  97, 103, 111, 114, 105,  99,  49, 107, 104,  48, 118,
  104,  56, 107,  48, 120, 115, 104, 107, 112,  54,  54,  48,
  100, 108,  97, 110,  99, 100, 109,  48, 115,  51, 104, 108,
   52, 103, 100,  48, 104, 102, 122, 121,  53, 101,  26,  16,
   10,   4, 117,  98,
  ... 12 more items
]

Test under SES Compartment

yarn test

gives the same result!

diff --git a/node_modules/protobufjs/src/util/minimal.js b/node_modules/protobufjs/src/util/minimal.js
index 3c406de..59880a0 100644
--- a/node_modules/protobufjs/src/util/minimal.js
+++ b/node_modules/protobufjs/src/util/minimal.js
@@ -41,7 +41,8 @@ util.isNode = Boolean(typeof global !== "undefined"
* @memberof util
* @type {Object}
*/
-util.global = util.isNode && global
+util.global = typeof globalThis !== "undefined" && globalThis
+ || util.isNode && global
|| typeof window !== "undefined" && window
|| typeof self !== "undefined" && self
|| this; // eslint-disable-line no-invalid-this
@@ -280,13 +281,13 @@ function newError(name) {
merge(this, properties);
}
- (CustomError.prototype = Object.create(Error.prototype)).constructor = CustomError;
+ Object.defineProperty(CustomError.prototype = Object.create(Error.prototype), "constructor", { value: CustomError });
Object.defineProperty(CustomError.prototype, "name", { get: function() { return name; } });
- CustomError.prototype.toString = function toString() {
+ Object.defineProperty(CustomError.prototype, "toString", { value: function toString() {
return this.name + ": " + this.message;
- };
+ } });
return CustomError;
}
#! /usr/bin/env node
import '@endo/init/debug.js';
import bundleSource from '@endo/bundle-source';
import { importBundle } from '@endo/import-bundle';
const main = async () => {
const bundle = await bundleSource('./user.js');
console.error(bundle.endoZipBase64Sha512)
const ns1 = await importBundle(bundle, { endowments: { console }});
console.error(ns1);
};
main();
import { MsgSend } from 'cosmjs-types/cosmos/bank/v1beta1/tx.js';
const msg = MsgSend.encode({
amount: [{ denom: 'ubld', amount: '10101010' }],
fromAddress: 'agoric17ndzf5sg9sx2fw8yzzeddxy0hswp0t8r3542p4',
toAddress: 'agoric1kh0vh8k0xshkp660dlancdm0s3hl4gd0hfzy5e',
});
console.error('MsgSend', new Uint8Array(msg));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment