*Add new utilities on plugin api

*add access to compact-encoding via binaryEncoding
*add access to b4a via bufferEncoding
*add crypto utility with createHash helper
This commit is contained in:
Derrick Hammer 2023-01-07 23:34:06 -05:00
parent 72c663795a
commit 8d52e40f20
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 40 additions and 0 deletions

View File

@ -19,6 +19,7 @@ import {
import { get as getSSl, SSLManager } from "./ssl.js"; import { get as getSSl, SSLManager } from "./ssl.js";
import type { HDKey } from "micro-ed25519-hdkey"; import type { HDKey } from "micro-ed25519-hdkey";
import corePlugins from "../plugins"; import corePlugins from "../plugins";
import Util from "./plugin/util";
let pluginAPIManager: PluginAPIManager; let pluginAPIManager: PluginAPIManager;
let pluginAPI: PluginAPI; let pluginAPI: PluginAPI;
@ -51,6 +52,12 @@ class PluginAPI extends EventEmitter2 {
this._swarm = swarm; this._swarm = swarm;
} }
private _util: Util = new Util();
get util(): Util {
return this._util;
}
private _swarm: any; private _swarm: any;
get swarm(): any { get swarm(): any {

View File

@ -0,0 +1,19 @@
import Crypto from "./util/crypto";
import b4a from "b4a";
// @ts-ignore
import c from "compact-encoding";
export default class Util {
private _crypto: Crypto = new Crypto();
get crypto(): Crypto {
return this._crypto;
}
get bufferEncoding(): typeof b4a {
return b4a;
}
get binaryEncoding(): typeof c {
return c;
}
}

View File

@ -0,0 +1,14 @@
// @ts-ignore
import sodium from "sodium-universal";
import { getPluginAPI } from "../../plugin";
export default class Crypto {
createHash(data: string): Buffer {
const b4a = getPluginAPI().util.bufferEncoding;
const buffer = b4a.from(data);
let hash = b4a.allocUnsafe(32) as Buffer;
sodium.crypto_generichash(hash, buffer);
return hash;
}
}