UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

204 lines 8.02 kB
import WindowCWISubstrate from './substrates/window.CWI.js'; import XDMSubstrate from './substrates/XDM.js'; import WalletWireTransceiver from './substrates/WalletWireTransceiver.js'; import HTTPWalletWire from './substrates/HTTPWalletWire.js'; import HTTPWalletJSON from './substrates/HTTPWalletJSON.js'; import ReactNativeWebView from './substrates/ReactNativeWebView.js'; const MAX_XDM_RESPONSE_WAIT = 200; /** * The SDK is how applications communicate with wallets over a communications substrate. */ export default class WalletClient { substrate; originator; constructor(substrate = 'auto', originator) { if (substrate === 'Cicada') { substrate = new WalletWireTransceiver(new HTTPWalletWire(originator)); } if (substrate === 'window.CWI') substrate = new WindowCWISubstrate(); if (substrate === 'XDM') substrate = new XDMSubstrate(); if (substrate === 'json-api') substrate = new HTTPWalletJSON(originator); if (substrate === 'react-native') substrate = new ReactNativeWebView(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 WindowCWISubstrate(); await checkSub(); this.substrate = sub; } catch (e) { // XDM failed, try the next one... try { sub = new XDMSubstrate(); await checkSub(MAX_XDM_RESPONSE_WAIT); this.substrate = sub; } catch (e) { // HTTP wire failed, move on... try { sub = new WalletWireTransceiver(new HTTPWalletWire(this.originator)); await checkSub(); this.substrate = sub; } catch (e) { // HTTP Wire failed, attempt the next... try { sub = new HTTPWalletJSON(this.originator); await checkSub(); this.substrate = sub; } catch (e) { // HTTP JSON failed, attempt the next... try { sub = new ReactNativeWebView(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); } } //# sourceMappingURL=WalletClient.js.map