UNPKG

@bsv/sdk

Version:

BSV Blockchain Software Development Kit

137 lines 5.04 kB
import { WERR_REVIEW_ACTIONS } from '../WERR_REVIEW_ACTIONS.js'; import { toOriginHeader } from './utils/toOriginHeader.js'; export default class HTTPWalletJSON { baseUrl; httpClient; originator; api; // Fixed `any` types constructor(originator, baseUrl = 'http://localhost:3321', httpClient = fetch) { this.baseUrl = baseUrl; this.originator = originator; this.httpClient = httpClient; // Detect if we're in a browser environment const isBrowser = typeof window !== 'undefined' && typeof document !== 'undefined' && window?.origin !== 'file://'; this.api = async (call, args) => { // In browser environments, let the browser handle Origin header automatically // In Node.js environments, we need to set it manually if originator is provided const origin = !isBrowser && this.originator ? toOriginHeader(this.originator, 'http') : undefined; if (!isBrowser && origin === undefined) { console.error('Originator is required in Node.js environments'); } const res = await (await httpClient(`${this.baseUrl}/${call}`, { method: 'POST', headers: { Accept: 'application/json', 'Content-Type': 'application/json', ...(origin ? { Origin: origin } : {}), ...(origin ? { Originator: origin } : {}), }, body: JSON.stringify(args) })); const data = await res.json(); // Check the HTTP status on the original response if (!res.ok) { if (res.status === 400 && data.isError && data.code === 5) { const err = new WERR_REVIEW_ACTIONS(data.reviewActionResults, data.sendWithResults, data.txid, data.tx, data.noSendChange); throw err; } else { const err = { call, args, message: data.message ?? `HTTP Client error ${res.status}` }; throw new Error(JSON.stringify(err)); } } return data; }; } async createAction(args) { return await this.api('createAction', args); } async signAction(args) { return await this.api('signAction', args); } async abortAction(args) { return await this.api('abortAction', args); } async listActions(args) { return await this.api('listActions', args); } async internalizeAction(args) { return await this.api('internalizeAction', args); } async listOutputs(args) { return await this.api('listOutputs', args); } async relinquishOutput(args) { return await this.api('relinquishOutput', args); } async getPublicKey(args) { return await this.api('getPublicKey', args); } async revealCounterpartyKeyLinkage(args) { return await this.api('revealCounterpartyKeyLinkage', args); } async revealSpecificKeyLinkage(args) { return await this.api('revealSpecificKeyLinkage', args); } async encrypt(args) { return await this.api('encrypt', args); } async decrypt(args) { return await this.api('decrypt', args); } async createHmac(args) { return await this.api('createHmac', args); } async verifyHmac(args) { return await this.api('verifyHmac', args); } async createSignature(args) { return await this.api('createSignature', args); } async verifySignature(args) { return await this.api('verifySignature', args); } async acquireCertificate(args) { return await this.api('acquireCertificate', args); } async listCertificates(args) { return await this.api('listCertificates', args); } async proveCertificate(args) { return await this.api('proveCertificate', args); } async relinquishCertificate(args) { return await this.api('relinquishCertificate', args); } async discoverByIdentityKey(args) { return await this.api('discoverByIdentityKey', args); } async discoverByAttributes(args) { return await this.api('discoverByAttributes', args); } async isAuthenticated(args) { return await this.api('isAuthenticated', args); } async waitForAuthentication(args) { return await this.api('waitForAuthentication', args); } async getHeight(args) { return await this.api('getHeight', args); } async getHeaderForHeight(args) { return await this.api('getHeaderForHeight', args); } async getNetwork(args) { return await this.api('getNetwork', args); } async getVersion(args) { return await this.api('getVersion', args); } } //# sourceMappingURL=HTTPWalletJSON.js.map