Skip to content

Commit

Permalink
Merge pull request #82 from 0chain/ashu/ssr-support
Browse files Browse the repository at this point in the history
  • Loading branch information
ashuvssut authored May 18, 2024
2 parents fd1758b + 1bbb1ac commit afe3ab1
Show file tree
Hide file tree
Showing 11 changed files with 4,071 additions and 91 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
2 changes: 1 addition & 1 deletion example/next
2 changes: 1 addition & 1 deletion lib/js-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zerochain/zus-sdk",
"version": "0.5.11",
"version": "0.5.12",
"description": "[![GitHub license](https://img.shields.io/badge/license-MIT-blue.svg)](./LICENSE) [![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](./CONTRIBUTING.md)",
"scripts": {
"start": "node src/example.js",
Expand Down
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/[email protected]/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
8 changes: 5 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
"private": true,
"workspaces": [
"lib/js-sdk/",
"example/website"
"example/*"
],
"scripts": {
"dev:site": "cd ./example/website && yarn dev",
"dev:lib": "cd ./lib/js-sdk && yarn dev",
"dev": "concurrently \"yarn dev:site\" \"yarn dev:lib\"",
"dev:next": "cd ./example/next && yarn dev",
"dev": "concurrently \"yarn dev:site\" \"yarn dev:lib\" \"yarn dev:next\"",
"build:site": "cd ./example/website && yarn build",
"build:lib": "cd ./lib/js-sdk && yarn build",
"build": "yarn build:site && yarn build:lib",
"build:next": "cd ./example/next && yarn build",
"build": "yarn build:site && yarn build:lib && yarn build:next",
"publish": "yarn changeset publish"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit afe3ab1

Please sign in to comment.