Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | 57x 57x 57x 2x 2x 2x 8x 8x 8x 8x 8x 8x 8x 1x 3x 2x 3x 2x 2x 1x 1x | /**
* StorageClient.ts
*
* A client-side "remoted" WalletStorage that fulfills the WalletStorage interface
* by sending JSON-RPC calls to a configured remote WalletStorageServer.
*/
import { AbortActionArgs, AbortActionResult, InternalizeActionArgs, InternalizeActionResult, ListActionsArgs, ListActionsResult, ListCertificatesResult, ListOutputsArgs, ListOutputsResult, RelinquishCertificateArgs, RelinquishOutputArgs, WalletInterface } from '@bsv/sdk'
import { sdk, table } from "../../index.client";
import { AuthFetch } from '@bsv/sdk';
// We import the base interface:
import { WalletStorageManager } from "../WalletStorageManager" // Adjust this import path to where your local interface is declared
export class StorageClient implements sdk.WalletStorageProvider {
private endpointUrl: string
private nextId = 1
private authClient: AuthFetch
// Track ephemeral (in-memory) "settings" if you wish to align with isAvailable() checks
public settings?: table.Settings
constructor(wallet: WalletInterface, endpointUrl: string) {
this.authClient = new AuthFetch(wallet)
this.endpointUrl = endpointUrl
}
isStorageProvider(): boolean { 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.
*/
private async rpcCall<T>(method: string, params: unknown[]): Promise<T> {
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)
})
Iif (!response.ok) {
throw new Error(`WalletStorageClient rpcCall: network error ${response.status} ${response.statusText}`)
}
const json = await response.json()
Iif (json.error) {
const { code, message, data } = json.error
const err = new Error(`RPC Error: ${message}`)
// You could attach more info here if you like:
; (err as any).code = code
; (err as any).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(): boolean {
// We'll just say "yes" if we have settings
return !!this.settings
}
async makeAvailable(): Promise<table.Settings> {
if (!this.settings) {
this.settings = await this.rpcCall<table.Settings>("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(): Promise<void> {
return this.rpcCall<void>("destroy", [])
}
async migrate(storageName: string, storageIdentityKey: string): Promise<string> {
return this.rpcCall<string>("migrate", [storageName])
}
getServices(): sdk.WalletServices {
// 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: sdk.WalletServices): void {
// Typically no-op for remote client
// Because "services" are usually local definitions of Dojo or P2P connections.
// If you want an advanced scenario, adapt it here.
//
}
async internalizeAction(
auth: sdk.AuthId,
args: InternalizeActionArgs,
): Promise<InternalizeActionResult> {
return this.rpcCall<InternalizeActionResult>("internalizeAction", [auth, args])
}
async createAction(
auth: sdk.AuthId,
args: sdk.ValidCreateActionArgs,
): Promise<sdk.StorageCreateActionResult> {
return this.rpcCall<sdk.StorageCreateActionResult>("createAction", [auth, args])
}
async processAction(
auth: sdk.AuthId,
args: sdk.StorageProcessActionArgs,
): Promise<sdk.StorageProcessActionResults> {
return this.rpcCall<sdk.StorageProcessActionResults>("processAction", [auth, args])
}
async abortAction(
auth: sdk.AuthId,
args: AbortActionArgs,
): Promise<AbortActionResult> {
return this.rpcCall<AbortActionResult>("abortAction", [auth, args])
}
async findOrInsertUser(
identityKey
): Promise<{ user: table.User; isNew: boolean; }> {
return this.rpcCall<{ user: table.User; isNew: boolean }>("findOrInsertUser", [identityKey])
}
async findOrInsertSyncStateAuth(
auth: sdk.AuthId,
storageIdentityKey: string,
storageName: string
): Promise<{ syncState: table.SyncState; isNew: boolean; }> {
return this.rpcCall<{ syncState: table.SyncState; isNew: boolean }>("findOrInsertSyncStateAuth", [auth, storageIdentityKey, storageName])
}
async insertCertificateAuth(
auth: sdk.AuthId,
certificate: table.CertificateX
): Promise<number> {
return this.rpcCall<number>("insertCertificateAuth", [auth, certificate])
}
_settings?: table.Settings
getSettings(): table.Settings {
Iif (!this._settings) {
throw new sdk.WERR_INVALID_OPERATION('call makeAvailable at least once before getSettings')
}
return this._settings!
}
async listActions(
auth: sdk.AuthId,
args: ListActionsArgs,
): Promise<ListActionsResult> {
return this.rpcCall<ListActionsResult>("listActions", [auth, args])
}
async listOutputs(
auth: sdk.AuthId,
args: ListOutputsArgs,
): Promise<ListOutputsResult> {
return this.rpcCall<ListOutputsResult>("listOutputs", [auth, args])
}
async listCertificates(
auth: sdk.AuthId,
args: sdk.ValidListCertificatesArgs,
): Promise<ListCertificatesResult> {
return this.rpcCall<ListCertificatesResult>("listCertificates", [auth, args])
}
async findCertificatesAuth(
auth: sdk.AuthId,
args: sdk.FindCertificatesArgs
): Promise<table.Certificate[]> {
return this.rpcCall<table.Certificate[]>("findCertificatesAuth", [auth, args])
}
async findOutputBasketsAuth(
auth: sdk.AuthId,
args: sdk.FindOutputBasketsArgs
): Promise<table.OutputBasket[]> {
return this.rpcCall<table.OutputBasket[]>("findOutputBaskets", [auth, args])
}
async findOutputsAuth(
auth: sdk.AuthId,
args: sdk.FindOutputsArgs
): Promise<table.Output[]> {
return this.rpcCall<table.Output[]>("findOutputsAuth", [auth, args])
}
findProvenTxReqs(
args: sdk.FindProvenTxReqsArgs
): Promise<table.ProvenTxReq[]> {
return this.rpcCall<table.ProvenTxReq[]>("findProvenTxReqs", [args])
}
async relinquishCertificate(
auth: sdk.AuthId,
args: RelinquishCertificateArgs
): Promise<number> {
return this.rpcCall<number>("relinquishCertificate", [auth, args])
}
async relinquishOutput(
auth: sdk.AuthId,
args: RelinquishOutputArgs
): Promise<number> {
return this.rpcCall<number>("relinquishOutput", [auth, args])
}
async processSyncChunk(
args: sdk.RequestSyncChunkArgs,
chunk: sdk.SyncChunk
): Promise<sdk.ProcessSyncChunkResult> {
return this.rpcCall<sdk.ProcessSyncChunkResult>("processSyncChunk", [args, chunk])
}
async getSyncChunk(
args: sdk.RequestSyncChunkArgs
): Promise<sdk.SyncChunk> {
return this.rpcCall<sdk.SyncChunk>("getSyncChunk", [args])
}
async updateProvenTxReqWithNewProvenTx(
args: sdk.UpdateProvenTxReqWithNewProvenTxArgs
): Promise<sdk.UpdateProvenTxReqWithNewProvenTxResult> {
return this.rpcCall<sdk.UpdateProvenTxReqWithNewProvenTxResult>("updateProvenTxReqWithNewProvenTx", [args])
}
async setActive(
auth: sdk.AuthId,
newActiveStorageIdentityKey: string
): Promise<number> {
return this.rpcCall<number>("setActive", [auth, newActiveStorageIdentityKey])
}
}
|