diff --git a/src/modules/plugin.ts b/src/modules/plugin.ts index 1177589..b6689da 100644 --- a/src/modules/plugin.ts +++ b/src/modules/plugin.ts @@ -19,6 +19,7 @@ import { import { get as getSSl, SSLManager } from "./ssl.js"; import type { HDKey } from "micro-ed25519-hdkey"; import corePlugins from "../plugins"; +import Util from "./plugin/util"; let pluginAPIManager: PluginAPIManager; let pluginAPI: PluginAPI; @@ -51,6 +52,12 @@ class PluginAPI extends EventEmitter2 { this._swarm = swarm; } + private _util: Util = new Util(); + + get util(): Util { + return this._util; + } + private _swarm: any; get swarm(): any { diff --git a/src/modules/plugin/util.ts b/src/modules/plugin/util.ts new file mode 100644 index 0000000..27edc34 --- /dev/null +++ b/src/modules/plugin/util.ts @@ -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; + } +} diff --git a/src/modules/plugin/util/crypto.ts b/src/modules/plugin/util/crypto.ts new file mode 100644 index 0000000..45b5dc9 --- /dev/null +++ b/src/modules/plugin/util/crypto.ts @@ -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; + } +}