Передайте переменные типа byte [] в импортированную FFI DLL в nodeJS - PullRequest
0 голосов
/ 27 февраля 2020

Ниже приведена выдержка из csharp реализации клиента Wireguard, использующего DLL. (https://github.com/WireGuard/wireguard-windows/tree/master/embeddable-dll-service)

/* SPDX-License-Identifier: MIT
 *
 * Copyright (C) 2019 WireGuard LLC. All Rights Reserved.
 */

using System;
using System.Runtime.InteropServices;

namespace Tunnel
{
    public class Keypair
    {
        public readonly string Public;
        public readonly string Private;

        private Keypair(string pub, string priv)
        {
            Public = pub;
            Private = priv;
        }

        [DllImport("tunnel.dll", EntryPoint = "WireGuardGenerateKeypair", CallingConvention = CallingConvention.Cdecl)]
        private static extern bool WireGuardGenerateKeypair(byte[] publicKey, byte[] privateKey);

        public static Keypair Generate()
        {
            var publicKey = new byte[32];
            var privateKey = new byte[32];
            WireGuardGenerateKeypair(publicKey, privateKey);
            return new Keypair(Convert.ToBase64String(publicKey), Convert.ToBase64String(privateKey));
        }
    }
}

Теперь я хочу перенести этот код на nodejs, используя ту же DLL. Я так далеко от этого примера проекта .

/*
Node 10 and lower
    npm i ffi
    https://www.npmjs.com/package/ffi
    https://github.com/node-ffi/node-ffi
Node 11 and higher
    npm i @saleae/ffi
    https://www.npmjs.com/package/@saleae/ffi
    https://github.com/lxe/node-ffi/tree/node-12
*/

// Import dependencies
const ffi = require("@saleae/ffi");

var ref = require('ref');

// Convert JSString to CString
function TEXT(text) {
    return Buffer.from(`${text}\0`, "ucs2");
}

// Import tunnel
const tunnel = new ffi.Library("wireguardfiles/tunnel", {
    "WireGuardGenerateKeypair": [
        "bool", ["String", "String"]
    ]
});

var publickey = "string";
var privatekey = "string";

var result = tunnel.WireGuardGenerateKeypair(publickey, privatekey)

process.stdout.write(result);

Как видите, это явно неправильно, поскольку я использую String вместо byte []. Это потому, что я получил ошибку TypeError: could not determine a proper "type" from: byte[].

Любая идея о том, как передать аргументы byte [] в DLL из nodejs?

Спасибо!

...