wallet-storage-client
Version:
Client only Wallet Storage
158 lines • 6.16 kB
JavaScript
"use strict";
/**
* StorageClient.ts
*
* A client-side "remoted" WalletStorage that fulfills the WalletStorage interface
* by sending JSON-RPC calls to a configured remote WalletStorageServer.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.StorageClient = void 0;
const index_client_1 = require("../../index.client");
const sdk_1 = require("@bsv/sdk");
class StorageClient {
constructor(wallet, endpointUrl) {
this.nextId = 1;
this.authClient = new sdk_1.AuthFetch(wallet);
this.endpointUrl = endpointUrl;
}
isStorageProvider() { return false; }
//////////////////////////////////////////////////////////////////////////////
// JSON-RPC helper
//////////////////////////////////////////////////////////////////////////////
/**
* Make a JSON-RPC call to the remote server.
* @param method The WalletStorage method name to call.
* @param params The array of parameters to pass to the method in order.
*/
async rpcCall(method, params) {
const id = this.nextId++;
const body = {
jsonrpc: "2.0",
method,
params,
id
};
const response = await this.authClient.fetch(this.endpointUrl, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(body)
});
if (!response.ok) {
throw new Error(`WalletStorageClient rpcCall: network error ${response.status} ${response.statusText}`);
}
const json = await response.json();
if (json.error) {
const { code, message, data } = json.error;
const err = new Error(`RPC Error: ${message}`);
err.code = code;
err.data = data;
throw err;
}
return json.result;
}
//////////////////////////////////////////////////////////////////////////////
// In a real environment, you might do lazy or real "makeAvailable" logic
// For demonstration, we assume that the remote store might return its "settings"
// and we store them locally in `this.settings`.
//////////////////////////////////////////////////////////////////////////////
isAvailable() {
// We'll just say "yes" if we have settings
return !!this.settings;
}
getSettings() {
if (!this.settings) {
throw new index_client_1.sdk.WERR_INVALID_OPERATION('call makeAvailable at least once before getSettings');
}
return this.settings;
}
async makeAvailable() {
if (!this.settings) {
this.settings = await this.rpcCall("makeAvailable", []);
}
return this.settings;
}
//////////////////////////////////////////////////////////////////////////////
//
// Implementation of all WalletStorage interface methods
// They are simple pass-thrus to rpcCall
//
// IMPORTANT: The parameter ordering must match exactly as in your interface.
//////////////////////////////////////////////////////////////////////////////
async destroy() {
return this.rpcCall("destroy", []);
}
async migrate(storageName, storageIdentityKey) {
return this.rpcCall("migrate", [storageName]);
}
getServices() {
// Typically, the client would not store or retrieve "Services" from a remote server.
// The "services" in local in-memory usage is a no-op or your own approach:
throw new Error("getServices() not implemented in remote client. This method typically is not used remotely.");
}
setServices(v) {
// Typically no-op for remote client
// Because "services" are usually local definitions to the Storage.
}
async internalizeAction(auth, args) {
return this.rpcCall("internalizeAction", [auth, args]);
}
async createAction(auth, args) {
return this.rpcCall("createAction", [auth, args]);
}
async processAction(auth, args) {
return this.rpcCall("processAction", [auth, args]);
}
async abortAction(auth, args) {
return this.rpcCall("abortAction", [auth, args]);
}
async findOrInsertUser(identityKey) {
return this.rpcCall("findOrInsertUser", [identityKey]);
}
async findOrInsertSyncStateAuth(auth, storageIdentityKey, storageName) {
return this.rpcCall("findOrInsertSyncStateAuth", [auth, storageIdentityKey, storageName]);
}
async insertCertificateAuth(auth, certificate) {
return this.rpcCall("insertCertificateAuth", [auth, certificate]);
}
async listActions(auth, vargs) {
return this.rpcCall("listActions", [auth, vargs]);
}
async listOutputs(auth, vargs) {
return this.rpcCall("listOutputs", [auth, vargs]);
}
async listCertificates(auth, vargs) {
return this.rpcCall("listCertificates", [auth, vargs]);
}
async findCertificatesAuth(auth, args) {
return this.rpcCall("findCertificatesAuth", [auth, args]);
}
async findOutputBasketsAuth(auth, args) {
return this.rpcCall("findOutputBaskets", [auth, args]);
}
async findOutputsAuth(auth, args) {
return this.rpcCall("findOutputsAuth", [auth, args]);
}
findProvenTxReqs(args) {
return this.rpcCall("findProvenTxReqs", [args]);
}
async relinquishCertificate(auth, args) {
return this.rpcCall("relinquishCertificate", [auth, args]);
}
async relinquishOutput(auth, args) {
return this.rpcCall("relinquishOutput", [auth, args]);
}
async processSyncChunk(args, chunk) {
return this.rpcCall("processSyncChunk", [args, chunk]);
}
async getSyncChunk(args) {
return this.rpcCall("getSyncChunk", [args]);
}
async updateProvenTxReqWithNewProvenTx(args) {
return this.rpcCall("updateProvenTxReqWithNewProvenTx", [args]);
}
async setActive(auth, newActiveStorageIdentityKey) {
return this.rpcCall("setActive", [auth, newActiveStorageIdentityKey]);
}
}
exports.StorageClient = StorageClient;
//# sourceMappingURL=StorageClient.js.map