UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

208 lines 8.49 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const window_CWI_js_1 = __importDefault(require("./substrates/window.CWI.js")); const XDM_js_1 = __importDefault(require("./substrates/XDM.js")); const WalletWireTransceiver_js_1 = __importDefault(require("./substrates/WalletWireTransceiver.js")); const HTTPWalletWire_js_1 = __importDefault(require("./substrates/HTTPWalletWire.js")); const HTTPWalletJSON_js_1 = __importDefault(require("./substrates/HTTPWalletJSON.js")); const ReactNativeWebView_js_1 = __importDefault(require("./substrates/ReactNativeWebView.js")); const MAX_XDM_RESPONSE_WAIT = 200; /** * The SDK is how applications communicate with wallets over a communications substrate. */ class WalletClient { constructor(substrate = 'auto', originator) { if (substrate === 'Cicada') { substrate = new WalletWireTransceiver_js_1.default(new HTTPWalletWire_js_1.default(originator)); } if (substrate === 'window.CWI') substrate = new window_CWI_js_1.default(); if (substrate === 'XDM') substrate = new XDM_js_1.default(); if (substrate === 'json-api') substrate = new HTTPWalletJSON_js_1.default(originator); if (substrate === 'react-native') substrate = new ReactNativeWebView_js_1.default(originator); this.substrate = substrate; this.originator = originator; } async connectToSubstrate() { if (typeof this.substrate === 'object') { return; // substrate is already connected } let sub; const checkSub = async (timeout) => { let result; if (typeof timeout === 'number') { result = await Promise.race([ sub.getVersion({}), new Promise((_resolve, reject) => setTimeout(() => reject(new Error('Timed out.')), timeout)) ]); } else { result = await sub.getVersion({}); } if (typeof result !== 'object' || typeof result.version !== 'string') { throw new Error('Failed to use substrate.'); } }; try { sub = new window_CWI_js_1.default(); await checkSub(); this.substrate = sub; } catch (e) { // XDM failed, try the next one... try { sub = new XDM_js_1.default(); await checkSub(MAX_XDM_RESPONSE_WAIT); this.substrate = sub; } catch (e) { // HTTP wire failed, move on... try { sub = new WalletWireTransceiver_js_1.default(new HTTPWalletWire_js_1.default(this.originator)); await checkSub(); this.substrate = sub; } catch (e) { // HTTP Wire failed, attempt the next... try { sub = new HTTPWalletJSON_js_1.default(this.originator); await checkSub(); this.substrate = sub; } catch (e) { // HTTP JSON failed, attempt the next... try { sub = new ReactNativeWebView_js_1.default(this.originator); await checkSub(); this.substrate = sub; } catch (e) { // No comms. Tell the user to install a BSV wallet. throw new Error('No wallet available over any communication substrate. Install a BSV wallet today!'); } } } } } } async createAction(args) { await this.connectToSubstrate(); return await this.substrate.createAction(args, this.originator); } async signAction(args) { await this.connectToSubstrate(); return await this.substrate.signAction(args, this.originator); } async abortAction(args) { await this.connectToSubstrate(); return await this.substrate.abortAction(args, this.originator); } async listActions(args) { await this.connectToSubstrate(); return await this.substrate.listActions(args, this.originator); } async internalizeAction(args) { await this.connectToSubstrate(); return await this.substrate.internalizeAction(args, this.originator); } async listOutputs(args) { await this.connectToSubstrate(); return await this.substrate.listOutputs(args, this.originator); } async relinquishOutput(args) { await this.connectToSubstrate(); return await this.substrate.relinquishOutput(args, this.originator); } async getPublicKey(args) { await this.connectToSubstrate(); return await this.substrate.getPublicKey(args, this.originator); } async revealCounterpartyKeyLinkage(args) { await this.connectToSubstrate(); return await this.substrate.revealCounterpartyKeyLinkage(args, this.originator); } async revealSpecificKeyLinkage(args) { await this.connectToSubstrate(); return await this.substrate.revealSpecificKeyLinkage(args, this.originator); } async encrypt(args) { await this.connectToSubstrate(); return await this.substrate.encrypt(args, this.originator); } async decrypt(args) { await this.connectToSubstrate(); return await this.substrate.decrypt(args, this.originator); } async createHmac(args) { await this.connectToSubstrate(); return await this.substrate.createHmac(args, this.originator); } async verifyHmac(args) { await this.connectToSubstrate(); return await this.substrate.verifyHmac(args, this.originator); } async createSignature(args) { await this.connectToSubstrate(); return await this.substrate.createSignature(args, this.originator); } async verifySignature(args) { await this.connectToSubstrate(); return await this.substrate.verifySignature(args, this.originator); } async acquireCertificate(args) { await this.connectToSubstrate(); return await this.substrate.acquireCertificate(args, this.originator); } async listCertificates(args) { await this.connectToSubstrate(); return await this.substrate.listCertificates(args, this.originator); } async proveCertificate(args) { await this.connectToSubstrate(); return await this.substrate.proveCertificate(args, this.originator); } async relinquishCertificate(args) { await this.connectToSubstrate(); return await this.substrate.relinquishCertificate(args, this.originator); } async discoverByIdentityKey(args) { await this.connectToSubstrate(); return await this.substrate.discoverByIdentityKey(args, this.originator); } async discoverByAttributes(args) { await this.connectToSubstrate(); return await this.substrate.discoverByAttributes(args, this.originator); } async isAuthenticated(args = {}) { await this.connectToSubstrate(); return await this.substrate.isAuthenticated(args, this.originator); } async waitForAuthentication(args = {}) { await this.connectToSubstrate(); return await this.substrate.waitForAuthentication(args, this.originator); } async getHeight(args = {}) { await this.connectToSubstrate(); return await this.substrate.getHeight(args, this.originator); } async getHeaderForHeight(args) { await this.connectToSubstrate(); return await this.substrate.getHeaderForHeight(args, this.originator); } async getNetwork(args = {}) { await this.connectToSubstrate(); return await this.substrate.getNetwork(args, this.originator); } async getVersion(args = {}) { await this.connectToSubstrate(); return await this.substrate.getVersion(args, this.originator); } } exports.default = WalletClient; //# sourceMappingURL=WalletClient.js.map