From 6970964ccfc595517fc1c6a6f2563597d6a225ef Mon Sep 17 00:00:00 2001
From: librelois <elois@ifee.fr>
Date: Sat, 9 May 2020 19:58:05 +0200
Subject: [PATCH] [ref] neon: migrate javascript binding code into pure
 typescript

---
 .gitignore                       |  5 +++--
 neon/lib/crypto.js               | 20 --------------------
 neon/lib/crypto.ts               | 17 +++++++++++++++++
 neon/lib/index.js                |  4 ----
 neon/lib/index.ts                |  3 +++
 neon/lib/wot.js                  | 16 ----------------
 neon/lib/wot.ts                  | 12 ++++++++++++
 neon/{lib => native}/crypto.d.ts | 11 ++---------
 neon/{lib => native}/index.d.ts  |  3 +--
 neon/{lib => native}/wot.d.ts    | 10 +++-------
 10 files changed, 41 insertions(+), 60 deletions(-)
 delete mode 100644 neon/lib/crypto.js
 create mode 100644 neon/lib/crypto.ts
 delete mode 100644 neon/lib/index.js
 create mode 100644 neon/lib/index.ts
 delete mode 100644 neon/lib/wot.js
 create mode 100644 neon/lib/wot.ts
 rename neon/{lib => native}/crypto.d.ts (66%)
 rename neon/{lib => native}/index.d.ts (79%)
 rename neon/{lib => native}/wot.d.ts (90%)

diff --git a/.gitignore b/.gitignore
index 1808fde75..155f50e27 100644
--- a/.gitignore
+++ b/.gitignore
@@ -36,14 +36,15 @@ coverage/
 # typecode
 typedoc/
 
+# files generated by tsc
 /index.js*
 /index.d.ts
 /server.js*
 /server.d.ts
-app/**/*.js*
+*/**/*.js*
 app/**/*.d.ts
+neon/lib/*.d.ts
 test/**/*.d.ts
-test/**/*.js*
 
 # files generated by neon tests
 test2.bin.gz
diff --git a/neon/lib/crypto.js b/neon/lib/crypto.js
deleted file mode 100644
index 18537334a..000000000
--- a/neon/lib/crypto.js
+++ /dev/null
@@ -1,20 +0,0 @@
-const addon = require('../native/index.node');
-
-const { Ed25519Signator, generateRandomSeed, seedToSecretKey, sha256, verify } = addon;
-
-class KeyPairBuilder {
-
-    static fromSeed(seed) {
-        return new Ed25519Signator(seed);
-    }
-
-    static fromSecretKey(secretKey) {
-        return new Ed25519Signator(secretKey);
-    }
-
-    static random() {
-        return addon.generateRandomSeed();
-    }
-}
-
-module.exports = { Ed25519Signator, KeyPairBuilder, generateRandomSeed, seedToSecretKey, sha256, verify };
diff --git a/neon/lib/crypto.ts b/neon/lib/crypto.ts
new file mode 100644
index 000000000..5643b375f
--- /dev/null
+++ b/neon/lib/crypto.ts
@@ -0,0 +1,17 @@
+
+import { Ed25519Signator, generateRandomSeed } from "../native";
+
+export class KeyPairBuilder {
+
+    static fromSeed(seed: Buffer): Ed25519Signator {
+        return new Ed25519Signator(seed);
+    }
+
+    static fromSecretKey(secretKey: string): Ed25519Signator {
+        return new Ed25519Signator(secretKey);
+    }
+
+    static random(): Ed25519Signator {
+        return new Ed25519Signator(generateRandomSeed());
+    }
+}
diff --git a/neon/lib/index.js b/neon/lib/index.js
deleted file mode 100644
index 7ebb99a4c..000000000
--- a/neon/lib/index.js
+++ /dev/null
@@ -1,4 +0,0 @@
-const { Wot, WotBuilder } = require('./wot');
-const { Ed25519Signator, KeyPairBuilder, generateRandomSeed, seedToSecretKey, sha256, verify } = require('./crypto');
-
-module.exports = { Ed25519Signator, KeyPairBuilder, generateRandomSeed, seedToSecretKey, sha256, verify, Wot, WotBuilder };
diff --git a/neon/lib/index.ts b/neon/lib/index.ts
new file mode 100644
index 000000000..48fdad95c
--- /dev/null
+++ b/neon/lib/index.ts
@@ -0,0 +1,3 @@
+export { Ed25519Signator, generateRandomSeed, seedToSecretKey, sha256, verify, Wot } from "../native";
+export { KeyPairBuilder } from "./crypto";
+export { WotBuilder } from "./wot";
diff --git a/neon/lib/wot.js b/neon/lib/wot.js
deleted file mode 100644
index f2ae9703d..000000000
--- a/neon/lib/wot.js
+++ /dev/null
@@ -1,16 +0,0 @@
-const addon = require('../native/index.node');
-
-const { Wot } = addon;
-
-class WotBuilder {
-
-    static fromWot(wot) {
-        return new Wot(wot.toBytes());
-    }
-
-    static fromFile(filePath) {
-        return new Wot(filePath)
-    }
-}
-
-module.exports = { Wot, WotBuilder };
diff --git a/neon/lib/wot.ts b/neon/lib/wot.ts
new file mode 100644
index 000000000..e62942455
--- /dev/null
+++ b/neon/lib/wot.ts
@@ -0,0 +1,12 @@
+import { Wot } from "../native";
+
+export class WotBuilder {
+
+    static fromWot(wot: Wot): Wot {
+        return new Wot(wot.toBytes());
+    }
+
+    static fromFile(filePath: string): Wot {
+        return new Wot(filePath)
+    }
+}
diff --git a/neon/lib/crypto.d.ts b/neon/native/crypto.d.ts
similarity index 66%
rename from neon/lib/crypto.d.ts
rename to neon/native/crypto.d.ts
index d1a18ea9a..ba6c22595 100644
--- a/neon/lib/crypto.d.ts
+++ b/neon/native/crypto.d.ts
@@ -1,16 +1,9 @@
 /* tslint:disable */
 
-export class KeyPairBuilder {
-
-    static fromSeed(seed: Buffer): Ed25519Signator;
-
-    static fromSecretKey(secretKey: string): Ed25519Signator;
-
-    static random(): Ed25519Signator;
-}
-
 export class Ed25519Signator {
 
+    constructor(seedOrSecretKey: Buffer | string);
+
     getPublicKey(): string;
 
     sign(message: Buffer | string): string;
diff --git a/neon/lib/index.d.ts b/neon/native/index.d.ts
similarity index 79%
rename from neon/lib/index.d.ts
rename to neon/native/index.d.ts
index 227ff4931..e31c8cbdf 100644
--- a/neon/lib/index.d.ts
+++ b/neon/native/index.d.ts
@@ -3,7 +3,6 @@
 import * as _crypto from './crypto';
 import * as _wot from './wot';
 
-export import KeyPairBuilder = _crypto.KeyPairBuilder;
 export import Ed25519Signator = _crypto.Ed25519Signator;
 export import generateRandomSeed = _crypto.generateRandomSeed;
 export import seedToSecretKey = _crypto.seedToSecretKey;
@@ -11,4 +10,4 @@ export import sha256 = _crypto.sha256;
 export import verify = _crypto.verify;
 
 export import Wot = _wot.Wot;
-export import WotBuilder = _wot.WotBuilder;
+export import DetailedDistance = _wot.DetailedDistance;
diff --git a/neon/lib/wot.d.ts b/neon/native/wot.d.ts
similarity index 90%
rename from neon/lib/wot.d.ts
rename to neon/native/wot.d.ts
index 86ebf1264..fe40b3646 100644
--- a/neon/lib/wot.d.ts
+++ b/neon/native/wot.d.ts
@@ -9,14 +9,8 @@ export class DetailedDistance {
     isOutdistanced: number;
 }
 
-export class WotBuilder {
-    static fromWot(wot: Wot): Wot;
-
-    static fromFile(filePath: string): Wot;
-}
-
 export class Wot {
-    constructor(maxCert: number);
+    constructor(maxCertOrFilePathOrBytes: number | string | Buffer);
 
     clear(): void;
 
@@ -66,5 +60,7 @@ export class Wot {
 
     writeInFile(file_path: string): boolean;
 
+    toBytes(): Buffer;
+
     dump(): string;
 }
-- 
GitLab