Skip to content

Commit

Permalink
Merge branch 'dev' of github.com:0chain/zus-js-sdk into dev
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuvssut committed May 18, 2024
2 parents e9b70e8 + afe3ab1 commit 4997295
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .changeset/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"access": "restricted",
"baseBranch": "main",
"updateInternalDependencies": "patch",
"ignore": ["website"]
"ignore": ["website", "next"]
}
5 changes: 5 additions & 0 deletions .changeset/shaggy-spiders-agree.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@zerochain/zus-sdk": patch
---

SSR support
10 changes: 6 additions & 4 deletions lib/js-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import { sha3_256 } from "js-sha3";
import { Buffer } from "buffer";
import { AccountEntity, Bridge, UploadObject } from "./types";

let bls = getBls();
let goWasm: Bridge["__proxy__"] | undefined = undefined;
const getWasm = () => {
if (!goWasm) throw new Error("WASM not initialized. call init first");
Expand All @@ -39,11 +38,12 @@ const log = (...args: any) => shouldShowLogs && console.log(...args);
*
* @param config The configuration parameters for initializing the SDK.
*/
export const init = async (config: string[]) => {
export const init = async (config: (string | number)[]) => {
if (!config.length) throw new Error("Missing configuration parameters");

log("config", config);
domain = config[1]?.replace("https://", "")?.replace("http://", "")?.replace("/dns", "") || "";
const blockWorkerUrl = String(config[1]);
domain = blockWorkerUrl?.replace("https://", "")?.replace("http://", "")?.replace("/dns", "") || "";

const wasm = await createWasm();
log("wasm", wasm);
Expand All @@ -53,7 +53,7 @@ export const init = async (config: string[]) => {
log(...config);
await wasm.sdk.init(...config);

bls = getBls();
const bls = getBls();
await bls.init(bls.BN254);

goWasm = wasm;
Expand Down Expand Up @@ -107,6 +107,7 @@ export const getBalanceWasm = async (clientId: string) => {
export const setWallet = async (clientID: string, privateKey: string, publicKey: string, mnemonic: string) => {
log("START setWallet", { clientID, privateKey, publicKey, mnemonic });
const goWasm = getWasm();
const bls = getBls();
await goWasm.setWallet(bls, clientID, privateKey, publicKey, mnemonic);
log("END setWallet", { clientID, privateKey, publicKey, mnemonic });
};
Expand Down Expand Up @@ -801,6 +802,7 @@ const createWalletKeys = async (userMnemonic?: string) => {
const seed = await bip39.mnemonicToSeed(mnemonic, "0chain-client-split-key");
const buffer = new Uint8Array(seed);

const bls = getBls();
const blsSecret = new bls.SecretKey();
bls.setRandFunc(buffer);
blsSecret.setLittleEndian(buffer);
Expand Down
8 changes: 7 additions & 1 deletion lib/js-sdk/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ declare global {
}
}

export const globalCtx = window || globalThis || self;
export const globalCtx = () => {
if (typeof window !== "undefined") return window || globalThis || self;
else {
console.error("Window object not available");
return {} as Window;
}
};

export type Bridge = {
glob: {
Expand Down
4 changes: 2 additions & 2 deletions lib/js-sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -739,13 +739,13 @@ export const getTransactionResponse = (data: TxnData) => {
};

export const getBls = () => {
if (!globalCtx.bls) {
if (!globalCtx().bls) {
throw new Error(
'window.bls unavailable. Add `<script src="https://cdn.jsdelivr.net/gh/herumi/bls-wasm@v1.0.0/browser/bls.js"></script>` before accessing this'
);
}

return globalCtx.bls;
return globalCtx().bls;
};

/**
Expand Down
8 changes: 6 additions & 2 deletions lib/js-sdk/src/zcn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
import { Bridge, UploadObject, globalCtx } from "./types";
import { hexStringToByte } from "./utils";

const g = globalCtx;

/** BRIDGE SETUP START */

/**
Expand Down Expand Up @@ -91,6 +89,8 @@ const maxTime = 10 * 1000;
* @returns Bridge object. This is an easier way to refer to the Go WASM object.
*/
const getBridge = (): Bridge => {
const g = globalCtx();

const currBridge = g.__zcn_wasm__;
if (currBridge) return currBridge;

Expand Down Expand Up @@ -133,6 +133,7 @@ const getProxy = (bridge: Bridge) => {
* @returns A promise that resolves to the upload result.
*/
async function bulkUpload(options: UploadObject[]) {
const g = globalCtx();
const bridge = getBridge();

const start = bridge.glob.index;
Expand Down Expand Up @@ -263,6 +264,8 @@ async function setWallet(bls: any, clientID: string, sk: string, pk: string, mne
* @param go The Go instance.
*/
async function loadWasm(go: InstanceType<Window["Go"]>) {
const g = globalCtx();

// If instantiateStreaming doesn't exists, polyfill/create it on top of instantiate
if (!WebAssembly?.instantiateStreaming) {
WebAssembly.instantiateStreaming = async (resp, importObject) => {
Expand Down Expand Up @@ -293,6 +296,7 @@ export async function createWasm() {
return bridge.__proxy__;
}

const g = globalCtx();
const go = new g.Go();

loadWasm(go);
Expand Down

0 comments on commit 4997295

Please sign in to comment.