feat: initial version
This commit is contained in:
parent
c8924b8f34
commit
59ece6720e
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,25 @@
|
|||
{
|
||||
"name": "@lumeweb/kernel-lavanet",
|
||||
"type": "module",
|
||||
"version": "0.1.0",
|
||||
"readme": "ERROR: No README data found!",
|
||||
"_id": "@lumeweb/kernel-lavanet@0.1.0",
|
||||
"repository": {
|
||||
"type": "git",
|
||||
"url": "gitea@git.lumeweb.com:LumeWeb/kernel-lavanet.git"
|
||||
},
|
||||
"scripts": {
|
||||
"prepare": "presetter bootstrap",
|
||||
"build": "run build",
|
||||
"semantic-release": "semantic-release"
|
||||
},
|
||||
"dependencies": {
|
||||
"@lavanet/lava-sdk": "^0.21.1",
|
||||
"@lumeweb/kernel-rpc-client": "^0.0.2-develop.13",
|
||||
"@lumeweb/libkernel": "^0.1.0-develop.36"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@lumeweb/presetter-kernel-module-preset": "^0.1.0-develop.44",
|
||||
"patch-package": "^8.0.0"
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
diff --git a/node_modules/@lavanet/lava-sdk/bin/src/badge/fetchBadge.js b/node_modules/@lavanet/lava-sdk/bin/src/badge/fetchBadge.js
|
||||
index 0c3063a..5e9cd0c 100644
|
||||
--- a/node_modules/@lavanet/lava-sdk/bin/src/badge/fetchBadge.js
|
||||
+++ b/node_modules/@lavanet/lava-sdk/bin/src/badge/fetchBadge.js
|
||||
@@ -24,6 +24,7 @@ class BadgeManager {
|
||||
this.badgeServerAddress = "";
|
||||
this.projectId = "";
|
||||
this.active = true;
|
||||
+ this.transport = transport;
|
||||
if (!options) {
|
||||
this.active = false;
|
||||
return;
|
||||
@@ -52,7 +53,7 @@ class BadgeManager {
|
||||
request: request,
|
||||
host: this.badgeServerAddress,
|
||||
metadata: this.authentication ? this.authentication : {},
|
||||
- transport: browser_1.default,
|
||||
+ transport: this.transport ?? browser_1.default,
|
||||
onMessage: (message) => {
|
||||
resolve(message);
|
||||
},
|
||||
diff --git a/node_modules/@lavanet/lava-sdk/bin/src/sdk/sdk.js b/node_modules/@lavanet/lava-sdk/bin/src/sdk/sdk.js
|
||||
index 53bfa7d..4ef0fd7 100644
|
||||
--- a/node_modules/@lavanet/lava-sdk/bin/src/sdk/sdk.js
|
||||
+++ b/node_modules/@lavanet/lava-sdk/bin/src/sdk/sdk.js
|
||||
@@ -47,9 +47,6 @@ class LavaSDK {
|
||||
if (!badge && !privateKey) {
|
||||
throw errors_1.default.errPrivKeyAndBadgeNotInitialized;
|
||||
}
|
||||
- else if (badge && privateKey) {
|
||||
- throw errors_1.default.errPrivKeyAndBadgeBothInitialized;
|
||||
- }
|
||||
// Initialize local attributes
|
||||
this.debugMode = options.debug ? options.debug : false; // enabling debug prints mainly used for development / debugging
|
||||
this.secure = options.secure !== undefined ? options.secure : true;
|
||||
@@ -148,8 +145,8 @@ class LavaSDK {
|
||||
return __awaiter(this, void 0, void 0, function* () {
|
||||
const start = performance.now();
|
||||
if (this.badgeManager.isActive()) {
|
||||
- const { wallet, privKey } = yield (0, wallet_1.createDynamicWallet)();
|
||||
- this.privKey = privKey;
|
||||
+ const wallet = yield (0, wallet_1.createWallet)(this.privKey);
|
||||
+ const account = yield wallet.getConsumerAccount();
|
||||
this.walletAddress = (yield wallet.getConsumerAccount()).address;
|
||||
const badgeResponse = yield this.fetchNewBadge();
|
||||
this.currentEpochBadge = badgeResponse.getBadge();
|
|
@ -0,0 +1,76 @@
|
|||
import { LavaSDK } from "@lavanet/lava-sdk";
|
||||
import type { ActiveQuery } from "@lumeweb/libkernel/module";
|
||||
import {
|
||||
addHandler,
|
||||
defer,
|
||||
getKey,
|
||||
handlePresentKey as handlePresentKeyModule,
|
||||
} from "@lumeweb/libkernel/module";
|
||||
import {
|
||||
createClient as createRpcClient,
|
||||
RpcNetwork,
|
||||
} from "@lumeweb/kernel-rpc-client";
|
||||
import { bytesToHex, deriveChildKey } from "@lumeweb/libweb";
|
||||
import { hyperTransport } from "./transport.js";
|
||||
|
||||
addHandler("presentKey", handlePresentKey);
|
||||
addHandler("query", handleQuery);
|
||||
|
||||
const chainInstances = new Map<string, LavaSDK>();
|
||||
const moduleReadyDefer = defer();
|
||||
let rpc: RpcNetwork;
|
||||
|
||||
async function handlePresentKey(aq: ActiveQuery) {
|
||||
handlePresentKeyModule({
|
||||
callerInput: {
|
||||
key: aq.callerInput.rootKey,
|
||||
},
|
||||
} as ActiveQuery);
|
||||
rpc = createRpcClient();
|
||||
moduleReadyDefer.resolve();
|
||||
}
|
||||
|
||||
async function handleQuery(aq: ActiveQuery) {
|
||||
if (!("chain" in aq.callerInput)) {
|
||||
aq.reject("chain missing");
|
||||
return;
|
||||
}
|
||||
if (!("query" in aq.callerInput)) {
|
||||
aq.reject("query missing");
|
||||
return;
|
||||
}
|
||||
|
||||
const { chain, query, rpcInterface = undefined } = aq.callerInput;
|
||||
|
||||
let lava: LavaSDK;
|
||||
|
||||
if (!chainInstances.has(chain)) {
|
||||
lava = await setupRelayChain(chain, rpcInterface);
|
||||
} else {
|
||||
lava = chainInstances.get(chain) as LavaSDK;
|
||||
}
|
||||
|
||||
await rpc.ready;
|
||||
|
||||
try {
|
||||
aq.respond(lava.sendRelay(query));
|
||||
} catch (e) {
|
||||
aq.reject(e);
|
||||
}
|
||||
}
|
||||
|
||||
async function setupRelayChain(chain: string, rpcInterface?: string) {
|
||||
const instance = await LavaSDK.create({
|
||||
chainID: chain,
|
||||
privateKey: bytesToHex(deriveChildKey(await getKey(), "lavanet")),
|
||||
badge: {
|
||||
// @ts-ignore
|
||||
transport: hyperTransport(rpc),
|
||||
},
|
||||
rpcInterface,
|
||||
});
|
||||
|
||||
chainInstances.set(chain, instance);
|
||||
|
||||
return instance;
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
import { grpc } from "@improbable-eng/grpc-web";
|
||||
import { RpcNetwork } from "@lumeweb/kernel-rpc-client";
|
||||
import Metadata = grpc.Metadata;
|
||||
|
||||
class HyperTransport implements grpc.Transport {
|
||||
private options: grpc.TransportOptions;
|
||||
private rpc: RpcNetwork;
|
||||
private metadata?: Metadata;
|
||||
|
||||
constructor(transportOptions: grpc.TransportOptions, rpc: RpcNetwork) {
|
||||
this.options = transportOptions;
|
||||
this.rpc = rpc;
|
||||
}
|
||||
|
||||
cancel(): void {}
|
||||
|
||||
finishSend(): void {}
|
||||
|
||||
async sendMessage(msgBytes: Uint8Array): Promise<void> {
|
||||
const req = this.rpc.simpleQuery({
|
||||
query: {
|
||||
module: "lavanet",
|
||||
method: "badge_request",
|
||||
data: {
|
||||
metadata: this.metadata,
|
||||
data: msgBytes,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
const ret = await req.result;
|
||||
if (ret.error) {
|
||||
this.options.onEnd({ message: ret.error, name: "", stack: "" });
|
||||
return;
|
||||
}
|
||||
|
||||
this.options.onChunk(ret.data);
|
||||
this.options.onEnd();
|
||||
}
|
||||
|
||||
start(metadata: Metadata): void {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
}
|
||||
|
||||
export function hyperTransport(rpc: RpcNetwork): grpc.TransportFactory {
|
||||
return (opts: grpc.TransportOptions) => {
|
||||
return new HyperTransport(opts, rpc);
|
||||
};
|
||||
}
|
Loading…
Reference in New Issue