@zerochain/sdk
Version:
The Züs JS SDK is a JavaScript client library that provides a convenient interface for interacting with the Züs Network. It allows developers to perform various operations such as creating and managing allocations, uploading and downloading files, executi
224 lines (216 loc) • 8.51 kB
text/typescript
declare const LOG_CODES: readonly ["WASM_LOADING", "WASM_LOADED", "OUTDATED_WASM_VERSION", "WASM_TYPE_MISMATCH_RETRY", "INVALID_WASM_CONFIG", "WASM_TYPE_MISMATCH"];
type LogData = {
message: string;
data?: any;
code: (typeof LOG_CODES)[number];
};
type OnLog = (type: 'info' | 'error' | 'warn' | 'debug', logData: LogData) => void;
type WasmType = 'normal' | 'enterprise';
/**
* Sleep is used when awaiting for Go Wasm to initialize.
* It uses the lowest possible sane delay time (via requestAnimationFrame).
* However, if the window is not focused, requestAnimationFrame never returns.
* A timeout will ensure to be called after 50 ms, regardless of whether or not
* the tab is in focus.
*/
declare const sleep: (ms?: number) => Promise<number | void>;
/**
* Signs a hash using BLS signature scheme.
*
* @param hash The hash to be signed.
* @returns The serialized signature in hexadecimal format.
*/
declare function blsSign(hash: string, secretKey: string): Promise<string>;
/**
* Verifies a BLS signature against a given hash.
*
* @param signature The serialized BLS signature.
* @param hash The hash to verify the signature against.
* @returns A boolean indicating whether the signature is valid or not.
*/
declare function blsVerify(signature: string, hash: string): Promise<boolean>;
/**
* Verifies a BLS signature against a given hash and public key.
*
* @param pk The public key.
* @param signature The serialized BLS signature.
* @param hash The hash to verify the signature against.
* @returns A boolean indicating whether the signature is valid or not.
*/
declare function blsVerifyWith(pk: string, signature: string, hash: string): Promise<boolean>;
/**
* Adds a signature to an existing signature.
*
* @param secretKey The secret key.
* @param signature The serialized BLS signature.
* @param hash The hash to verify the signature against.
*
* @returns The serialized signature in hexadecimal format.
*/
declare function blsAddSignature(secretKey: string, signature: string, hash: string): Promise<string>;
declare function createObjectURL(buf: ArrayBuffer, mimeType: string): Promise<string>;
type JsProxyMethods = {
sign: typeof blsSign;
verify: typeof blsVerify;
verifyWith: typeof blsVerifyWith;
addSignature: typeof blsAddSignature;
createObjectURL: typeof createObjectURL;
sleep: typeof sleep;
};
type UploadObject = {
allocationId: string;
remotePath: string;
file: File;
thumbnailBytes: Uint8Array;
encrypt: boolean;
isUpdate: boolean;
isRepair: boolean;
numBlocks: number;
callback: (totalBytes: number, completedBytes: number, error: any) => void;
/** @deprecated */
webstreaming: boolean;
};
/**
* Performs a bulk upload of multiple files.
*
* @param options An array of upload options for each file.
*
* @deprecated use `multiUplaod` methods instead
*/
declare function bulkUpload(options: UploadObject[]): Promise<any>;
/**
* Sets the wallet information in the bridge and the Go instance.
*
* @param bls The BLS object from bls-wasm script.
* @param clientID The client ID / wallet ID.
* @param clientKey The client key.
* @param peerPublicKey The peer public key.
* @param sk The serialized secret key.
* @param pk The serialized public key.
* @param mnemonic The mnemonic.
* @param isSplit Whether the wallet has split keys enabled or not.
*/
declare function setWallet(bls: any, // TODO: check bls
clientID: string, clientKey: string, peerPublicKey: string, sk: string, pk: string, mnemonic: string, isSplit: boolean): Promise<void>;
type SdkProxyMethods = {
/** @deprecated use `multiUplaod` methods instead */
bulkUpload: typeof bulkUpload;
setWallet: typeof setWallet;
};
declare global {
interface Window {
__zcn_wasm__?: Bridge | undefined;
Go: new () => any;
/** Add `<script src="https://cdn.jsdelivr.net/gh/herumi/bls-wasm@v1.0.0/browser/bls.js"></script>` before accessing this */
bls?: any;
newGoWasm?: any;
createWasmPromise?: Promise<any> | undefined;
[key: string]: any;
}
}
type Bridge = {
wasmType?: WasmType;
__wasm_initialized__?: boolean;
__config__?: Config;
glob: {
index: number;
};
/** walletId is available after setWallet method succeeds */
walletId?: string;
/** secretKey is available after setWallet method succeeds */
secretKey?: any;
/** peerPublicKey is available after setWallet method succeeds */
peerPublicKey?: any;
jsProxy: {
/** BLS object is available when setWallet method is called */
bls?: any;
/** secretKey is available when setWallet method is called */
secretKey?: any;
/** secretKey is available when setWallet method is called */
publicKey?: any;
/** publicKey is available when setWallet method is called */
pubkeyStr?: string;
/** isSplit is set when setWallet method is called */
isSplit?: boolean;
} & {
[K in keyof JsProxyMethods]: JsProxyMethods[K];
};
/** proxy object for go to expose its methods */
sdk: any;
/** `bridge.__proxy__` is avilable when createWasm method is called */
__proxy__?: {
/** `bridge.__proxy__.sdk`: Proxy object for accessing SDK methods. */
sdk: {
[key: string]: any;
};
/** `bridge.__proxy__.jsProxy`: Proxy object that Exposes JS methods for go */
jsProxy: {};
} & {
[K in keyof SdkProxyMethods]: SdkProxyMethods[K];
};
};
type Config = {
/**
* `wasmBaseUrl` is the base URL of your enterprise-zcn.wasm & zcn.wasm
*
* Example: if `wasmBaseUrl` is `https://example.com/wasm` then WASM files
* should be located at:
* - `https://example.com/wasm/enterprise-zcn.wasm` for Enterprise WASM
* - `https://example.com/wasm/zcn.wasm` for Standard WASM
*/
wasmBaseUrl?: string;
zus?: ZusConfig;
/** **NOTE**: Only needed if you are using the SDK for uploading files. */
md5WorkerUrl?: string;
/** @default `true` */
isWasmGzipped?: boolean;
} & ({
useCachedWasm?: false;
cacheConfig?: never;
} | {
useCachedWasm: true;
cacheConfig: CacheConfig;
});
type CacheConfig = {
enterpriseGosdkVersion: string;
enterpriseWasmUrl?: string;
standardGosdkVersion: string;
standardWasmUrl?: string;
};
type ZusConfig = {
cdnUrl?: string;
};
type SetIsWasmLoaded = (isLoaded: boolean) => void;
type GoInstance = InstanceType<Window['Go']>;
type WasmLoaderOptions = {
onLog?: OnLog | undefined;
debounceTimeout?: number | undefined;
setIsWasmLoaded?: ((isWasmLoaded: boolean) => void) | undefined;
getRetries: () => number;
incrementRetries: () => void;
resetRetries: () => void;
};
type InitializeWasm = (config: Config, isSwitchingWasm?: boolean) => Promise<void> | undefined;
/** Updates the "desired mode" value of the GoSDK WASM. */
declare const updateWasmMode: (desiredAllocationType: WasmType) => void;
/** Returns the "desired mode" value of the GoSDK WASM. */
declare const getDesiredMode: () => WasmType;
/**
* Checks if the desired GoSDK WASM mode is initialized. Returns `true` if the WASM is initialized, `false` otherwise.
* @param onLog Optional logger, e.g., console.log
*/
declare const isDesiredWasmInitialized: (onLog?: OnLog) => boolean;
/**
* Waits until the GoSDK WASM is loaded and initialized.
*
* Unlike `checkIfWasmLoaded` method, this method doesn't check if the desired WASM mode is initialized. It just returns a promise that will only resolve once the GoSDK WASM is loaded and initialized.
*
* @param onLog Optional logger, e.g., console.log
*/
declare const awaitWasmLoad: (onLog?: OnLog) => Promise<void>;
/**
* `checkIfWasmLoaded` Waits for the "desired mode" GoSDK WASM to be loaded and initialized.
* @returns {Promise<boolean>} Returns a promise that resolves to `true` until the "desired mode" GoSDK WASM is loaded and initialized. If the desired WASM mode is not initialized, it resolves to `false`.
*/
declare const checkIfWasmLoaded: () => Promise<boolean>;
export { type Bridge as B, type Config as C, type GoInstance as G, type InitializeWasm as I, type OnLog as O, type SetIsWasmLoaded as S, type WasmType as W, type WasmLoaderOptions as a, bulkUpload as b, awaitWasmLoad as c, checkIfWasmLoaded as d, getDesiredMode as g, isDesiredWasmInitialized as i, setWallet as s, updateWasmMode as u };