0

i need to create chrome extension that can call c++ from js so i use nodeapi and to achieve that I used the node-gyp

package.json

{
  "name": "test",
  "version": "0.13.0",
  "description": "test",
  "main": "./index.js",
  "files": [
    "index.js"
  ],
  "engines": {
    "node": ">=12.0.0"
  },
  "scripts": {
    "test": "node index.js",
    "build": "node-gyp rebuild",
    "clean": "node-gyp clean",
    "install": "prebuild-install --runtime napi || node-gyp rebuild"
  },
  "dependencies": {
    "bindings": "^1.5.0",
    "browserify": "^17.0.0"
    "node-addon-api": "^3.0.2",
    "prebuild-install": "^6.0.0"
  },
  "devDependencies": {
    "babel-eslint": "^10.1.0",
    "chai": "^4.1.2",
    "chai-spies": "^1.0.0",
    "eslint": "^6.8.0",
    "eslint-plugin-import": "^2.20.1",
    "eslint-plugin-json": "^1.2.0",
    "eslint-plugin-mocha": "^6.2.2",
    "mocha": "^5.0.4",
    "node-gyp": "^9.1.0",
    "prebuild": "^10.0.1",
    "rimraf": "^2.6.2"
  },
  "gypfile": true
}

binding.gyp

{
  "targets": [
    {
        'target_name': 'hello',
        'sources': [ 'hello.cc' ],
        'defines': [
            '_LARGEFILE_SOURCE',
            '_FILE_OFFSET_BITS=64',
            'NAPI_DISABLE_CPP_EXCEPTIONS'
        ],
        'cflags!': ['-ansi', '-fno-exceptions' ],
        'cflags_cc!': [ '-fno-exceptions' ],
        'cflags': ['-g', '-exceptions'],
        'cflags_cc': ['-g', '-exceptions'],
        'include_dirs': [
          "<!@(node -p \"require('node-addon-api').include\")"
        ],
        'dependencies': [
          "<!(node -p \"require('node-addon-api').gyp\")"
        ],
    }, 
  ]
}

hello.cc

#include <napi.h>
Napi::String Method(const Napi::CallbackInfo& info) {
  Napi::Env env = info.Env();
  return Napi::String::New(env, "Hello Calling From Cpp");
}

Napi::Object Init(Napi::Env env, Napi::Object exports) {
  exports.Set(Napi::String::New(env, "hellocc"), Napi::Function::New(env, Method));
  return exports;
}

NODE_API_MODULE(hello, Init)

index.js

 'use strict'
 var addon = require('bindings')('hello');
 function testingfromjs(){
     return addon.hellocc();
 }
exports.testingfromjs= testingfromjs;

the problem is it works fine with commands like node index.js but it didn't work when it build as chrome extension i imported it as dependency and use this as required in the main.js of chrome extension but it didn't work

Ahmed Shams
  • 338
  • 1
  • 9
  • `node-api` and `node-gyp` are, as the name suggests, specific to Node.js. To use compiled code in browsers, you probably need to look at [WebAssembly](https://developer.mozilla.org/en-US/docs/WebAssembly/C_to_wasm). – robertklep Aug 07 '22 at 10:45
  • i am develope on metamask extension that already use node.js – Ahmed Shams Aug 07 '22 at 11:55
  • I can't find any compiled parts in the MM extension. – robertklep Aug 07 '22 at 12:46
  • it is already use node-hide which is based on node-gyp beside most of it's dependencies are nodejs – Ahmed Shams Aug 07 '22 at 13:10
  • [Which compiled dependencies](https://github.com/MetaMask/metamask-extension/blob/develop/package.json) are used directly (i.e. not as part of the build process) in the extension? – robertklep Aug 07 '22 at 13:23
  • i already need my dependency to be part of build process not used directly i need to be used as ledger and trezor dependencies – Ahmed Shams Aug 07 '22 at 13:35
  • Trezor support in the extension uses the [WebUSB API](https://wicg.github.io/webusb/), not compiled code. – robertklep Aug 07 '22 at 13:44
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/247098/discussion-between-ahmed-shams-and-robertklep). – Ahmed Shams Aug 07 '22 at 14:18
  • ledger use hw-transport-node-hid-noevent and this is use node-gyp and napi and so on what i need to do to make our keyring (JS) communicate with our SDK (c++) this works fine on keyring level but when it build with metamask it crashed – Ahmed Shams Aug 07 '22 at 15:00
  • `hw-transport-node-hid-noevent ` is Node.js specific, it's not going to work in an extension. I'll refer to my first comment again: `node-api` and `node-gyp` are specific to Node.js. – robertklep Aug 07 '22 at 15:56
  • Are you sure that metamask doesn't use gyp to run c code? – Ahmed Shams Aug 11 '22 at 08:46
  • 1
    The metamask _extension_ doesn't use any native (compiled) modules. – robertklep Aug 11 '22 at 08:57
  • what?! so how it communicate with the hardware wallets SDKs? like trezor or ledger or laticce – Ahmed Shams Aug 11 '22 at 09:28
  • I already told you in a previous comment. – robertklep Aug 11 '22 at 10:22

0 Answers0