@dignetwork/dig-sdk 0.0.1-alpha.98 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -7
- package/README.md +267 -0
- package/dist/adapters.cjs +315 -0
- package/dist/adapters.cjs.map +1 -0
- package/dist/adapters.d.cts +20 -0
- package/dist/adapters.d.ts +20 -0
- package/dist/adapters.js +306 -0
- package/dist/adapters.js.map +1 -0
- package/dist/dev-shim-Bgw8X2E9.d.cts +118 -0
- package/dist/dev-shim-Bgw8X2E9.d.ts +118 -0
- package/dist/dig-client-entry-BIzuZ7Rs.d.cts +263 -0
- package/dist/dig-client-entry-BIzuZ7Rs.d.ts +263 -0
- package/dist/dig-client.cjs +330 -0
- package/dist/dig-client.cjs.map +1 -0
- package/dist/dig-client.d.cts +1 -0
- package/dist/dig-client.d.ts +1 -0
- package/dist/dig-client.js +319 -0
- package/dist/dig-client.js.map +1 -0
- package/dist/index.cjs +754 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +192 -0
- package/dist/index.d.ts +192 -5
- package/dist/index.js +731 -19
- package/dist/index.js.map +1 -0
- package/dist/next.cjs +326 -0
- package/dist/next.cjs.map +1 -0
- package/dist/next.d.cts +23 -0
- package/dist/next.d.ts +23 -0
- package/dist/next.js +319 -0
- package/dist/next.js.map +1 -0
- package/dist/spend.cjs +14 -0
- package/dist/spend.cjs.map +1 -0
- package/dist/spend.d.cts +1 -0
- package/dist/spend.d.ts +1 -0
- package/dist/spend.js +3 -0
- package/dist/spend.js.map +1 -0
- package/dist/vite.cjs +338 -0
- package/dist/vite.cjs.map +1 -0
- package/dist/vite.d.cts +35 -0
- package/dist/vite.d.ts +35 -0
- package/dist/vite.js +332 -0
- package/dist/vite.js.map +1 -0
- package/package.json +87 -59
- package/vendor/PROVENANCE.md +102 -0
- package/vendor/dig_client.d.ts +150 -0
- package/vendor/dig_client.mjs +805 -0
- package/vendor/dig_client_bg.wasm +0 -0
- package/dist/DataIntegrityTree/DataIntegrityTree.d.ts +0 -163
- package/dist/DataIntegrityTree/DataIntegrityTree.d.ts.map +0 -1
- package/dist/DataIntegrityTree/DataIntegrityTree.js +0 -707
- package/dist/DataIntegrityTree/DataLayerError.d.ts +0 -6
- package/dist/DataIntegrityTree/DataLayerError.d.ts.map +0 -1
- package/dist/DataIntegrityTree/DataLayerError.js +0 -17
- package/dist/DataIntegrityTree/index.d.ts +0 -2
- package/dist/DataIntegrityTree/index.d.ts.map +0 -1
- package/dist/DataIntegrityTree/index.js +0 -17
- package/dist/DigNetwork/ContentServer.d.ts +0 -33
- package/dist/DigNetwork/ContentServer.d.ts.map +0 -1
- package/dist/DigNetwork/ContentServer.js +0 -308
- package/dist/DigNetwork/DigChallenge.d.ts +0 -82
- package/dist/DigNetwork/DigChallenge.d.ts.map +0 -1
- package/dist/DigNetwork/DigChallenge.js +0 -144
- package/dist/DigNetwork/DigNetwork.d.ts +0 -14
- package/dist/DigNetwork/DigNetwork.d.ts.map +0 -1
- package/dist/DigNetwork/DigNetwork.js +0 -169
- package/dist/DigNetwork/DigPeer.d.ts +0 -24
- package/dist/DigNetwork/DigPeer.d.ts.map +0 -1
- package/dist/DigNetwork/DigPeer.js +0 -118
- package/dist/DigNetwork/IncentiveServer.d.ts +0 -13
- package/dist/DigNetwork/IncentiveServer.d.ts.map +0 -1
- package/dist/DigNetwork/IncentiveServer.js +0 -76
- package/dist/DigNetwork/PropagationServer.d.ts +0 -75
- package/dist/DigNetwork/PropagationServer.d.ts.map +0 -1
- package/dist/DigNetwork/PropagationServer.js +0 -536
- package/dist/DigNetwork/index.d.ts +0 -4
- package/dist/DigNetwork/index.d.ts.map +0 -1
- package/dist/DigNetwork/index.js +0 -19
- package/dist/blockchain/DataStore.d.ts +0 -67
- package/dist/blockchain/DataStore.d.ts.map +0 -1
- package/dist/blockchain/DataStore.js +0 -413
- package/dist/blockchain/DataStoreSerializer.d.ts +0 -52
- package/dist/blockchain/DataStoreSerializer.d.ts.map +0 -1
- package/dist/blockchain/DataStoreSerializer.js +0 -109
- package/dist/blockchain/FullNodePeer.d.ts +0 -31
- package/dist/blockchain/FullNodePeer.d.ts.map +0 -1
- package/dist/blockchain/FullNodePeer.js +0 -295
- package/dist/blockchain/ServerCoin.d.ts +0 -29
- package/dist/blockchain/ServerCoin.d.ts.map +0 -1
- package/dist/blockchain/ServerCoin.js +0 -321
- package/dist/blockchain/Wallet.d.ts +0 -28
- package/dist/blockchain/Wallet.d.ts.map +0 -1
- package/dist/blockchain/Wallet.js +0 -253
- package/dist/blockchain/coins.d.ts +0 -6
- package/dist/blockchain/coins.d.ts.map +0 -1
- package/dist/blockchain/coins.js +0 -102
- package/dist/blockchain/index.d.ts +0 -8
- package/dist/blockchain/index.d.ts.map +0 -1
- package/dist/blockchain/index.js +0 -36
- package/dist/index.d.ts.map +0 -1
- package/dist/prompts.d.ts +0 -21
- package/dist/prompts.d.ts.map +0 -1
- package/dist/prompts.js +0 -149
- package/dist/types.d.ts +0 -64
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/utils/ContentScanner.d.ts +0 -63
- package/dist/utils/ContentScanner.d.ts.map +0 -1
- package/dist/utils/ContentScanner.js +0 -175
- package/dist/utils/Environment.d.ts +0 -18
- package/dist/utils/Environment.d.ts.map +0 -1
- package/dist/utils/Environment.js +0 -85
- package/dist/utils/FileCache.d.ts +0 -11
- package/dist/utils/FileCache.d.ts.map +0 -1
- package/dist/utils/FileCache.js +0 -58
- package/dist/utils/FileTransfer.d.ts +0 -47
- package/dist/utils/FileTransfer.d.ts.map +0 -1
- package/dist/utils/FileTransfer.js +0 -209
- package/dist/utils/NconfManager.d.ts +0 -11
- package/dist/utils/NconfManager.d.ts.map +0 -1
- package/dist/utils/NconfManager.js +0 -56
- package/dist/utils/StoreArchiveManager.d.ts +0 -45
- package/dist/utils/StoreArchiveManager.d.ts.map +0 -1
- package/dist/utils/StoreArchiveManager.js +0 -153
- package/dist/utils/asyncPool.d.ts +0 -9
- package/dist/utils/asyncPool.d.ts.map +0 -1
- package/dist/utils/asyncPool.js +0 -23
- package/dist/utils/config.d.ts +0 -68
- package/dist/utils/config.d.ts.map +0 -1
- package/dist/utils/config.js +0 -223
- package/dist/utils/connectionUtils.d.ts +0 -10
- package/dist/utils/connectionUtils.d.ts.map +0 -1
- package/dist/utils/connectionUtils.js +0 -18
- package/dist/utils/credentialsUtils.d.ts +0 -8
- package/dist/utils/credentialsUtils.d.ts.map +0 -1
- package/dist/utils/credentialsUtils.js +0 -122
- package/dist/utils/directoryUtils.d.ts +0 -9
- package/dist/utils/directoryUtils.d.ts.map +0 -1
- package/dist/utils/directoryUtils.js +0 -110
- package/dist/utils/encryption.d.ts +0 -8
- package/dist/utils/encryption.d.ts.map +0 -1
- package/dist/utils/encryption.js +0 -55
- package/dist/utils/hashUtils.d.ts +0 -8
- package/dist/utils/hashUtils.d.ts.map +0 -1
- package/dist/utils/hashUtils.js +0 -44
- package/dist/utils/index.d.ts +0 -14
- package/dist/utils/index.d.ts.map +0 -1
- package/dist/utils/index.js +0 -29
- package/dist/utils/merkle.d.ts +0 -2
- package/dist/utils/merkle.d.ts.map +0 -1
- package/dist/utils/merkle.js +0 -28
- package/dist/utils/network.d.ts +0 -2
- package/dist/utils/network.d.ts.map +0 -1
- package/dist/utils/network.js +0 -54
- package/dist/utils/spinnerUtils.d.ts +0 -2
- package/dist/utils/spinnerUtils.d.ts.map +0 -1
- package/dist/utils/spinnerUtils.js +0 -47
- package/dist/utils/ssl.d.ts +0 -5
- package/dist/utils/ssl.d.ts.map +0 -1
- package/dist/utils/ssl.js +0 -50
- package/dist/utils/validationUtils.d.ts +0 -9
- package/dist/utils/validationUtils.d.ts.map +0 -1
- package/dist/utils/validationUtils.js +0 -55
package/dist/index.js
CHANGED
|
@@ -1,20 +1,732 @@
|
|
|
1
|
-
|
|
2
|
-
var
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
// src/methods.ts
|
|
2
|
+
var WALLET_METHODS = [
|
|
3
|
+
"chip0002_connect",
|
|
4
|
+
"chip0002_chainId",
|
|
5
|
+
"chip0002_getPublicKeys",
|
|
6
|
+
"chip0002_getAssetCoins",
|
|
7
|
+
"chip0002_getAssetBalance",
|
|
8
|
+
"chip0002_signCoinSpends",
|
|
9
|
+
"chip0002_signMessage",
|
|
10
|
+
"chia_getAddress",
|
|
11
|
+
"chia_signMessageByAddress",
|
|
12
|
+
"chia_takeOffer"
|
|
13
|
+
];
|
|
14
|
+
var SIGN_METHODS = [
|
|
15
|
+
"chia_signMessageByAddress",
|
|
16
|
+
"chip0002_signMessage"
|
|
17
|
+
];
|
|
18
|
+
var DEFAULT_CHAIN = "chia:mainnet";
|
|
19
|
+
|
|
20
|
+
// src/provider/injected.ts
|
|
21
|
+
function getInjectedProvider() {
|
|
22
|
+
const g = globalThis;
|
|
23
|
+
return typeof g !== "undefined" ? g.chia : void 0;
|
|
24
|
+
}
|
|
25
|
+
function isInjectedAvailable(opts = {}) {
|
|
26
|
+
const p = getInjectedProvider();
|
|
27
|
+
if (!p) return false;
|
|
28
|
+
if (opts.anyChia) return typeof p.request === "function";
|
|
29
|
+
return !!p.isDIG;
|
|
30
|
+
}
|
|
31
|
+
var INJECTED_TOPIC = "injected";
|
|
32
|
+
var InjectedTransport = class {
|
|
33
|
+
constructor(provider, chain = DEFAULT_CHAIN) {
|
|
34
|
+
this.backend = "injected";
|
|
35
|
+
this.topic = INJECTED_TOPIC;
|
|
36
|
+
this.provider = provider;
|
|
37
|
+
this.chain = chain;
|
|
38
|
+
}
|
|
39
|
+
/** Connect: ask the native wallet to approve this origin. Blocks until approved/rejected. */
|
|
40
|
+
async connect(eager = false) {
|
|
41
|
+
if (typeof this.provider.connect === "function") {
|
|
42
|
+
await this.provider.connect(eager);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
// The native wallet implements the full canonical set (it returns Sage-shaped responses), so
|
|
46
|
+
// support is a static allowlist — no per-session negotiation as with WalletConnect.
|
|
47
|
+
supports(method) {
|
|
48
|
+
return WALLET_METHODS.includes(method);
|
|
49
|
+
}
|
|
50
|
+
async request(method, params) {
|
|
51
|
+
if (!this.supports(method)) {
|
|
52
|
+
throw new Error(`The DIG Browser wallet does not support "${method}".`);
|
|
53
|
+
}
|
|
54
|
+
return this.provider.request({ method, params });
|
|
55
|
+
}
|
|
56
|
+
async disconnect() {
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
// src/provider/wc-retry.ts
|
|
61
|
+
function isTransientPublishError(err) {
|
|
62
|
+
const msg = (err instanceof Error ? err.message : typeof err === "string" ? err : "").toLowerCase();
|
|
63
|
+
if (!msg) return false;
|
|
64
|
+
return msg.includes("publish") || msg.includes("failed or timed out to publish") || msg.includes("websocket connection failed") || msg.includes("connection closed") || msg.includes("socket stalled") || msg.includes("request reset");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
// src/provider/walletconnect.ts
|
|
68
|
+
var WalletConnectTransport = class _WalletConnectTransport {
|
|
69
|
+
constructor(client, session, chain, requestTimeoutMs) {
|
|
70
|
+
this.backend = "walletconnect";
|
|
71
|
+
this.client = client;
|
|
72
|
+
this.topic = session.topic;
|
|
73
|
+
this.chain = chain;
|
|
74
|
+
this.requestTimeoutMs = requestTimeoutMs;
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Initialize SignClient, open a pairing (emitting the URI via `onUri`), and resolve to a
|
|
78
|
+
* connected transport once the user approves in Sage. Requires `@walletconnect/sign-client`.
|
|
79
|
+
*/
|
|
80
|
+
static async connect(options) {
|
|
81
|
+
const chain = options.chain ?? DEFAULT_CHAIN;
|
|
82
|
+
const timeout = options.requestTimeoutMs ?? 6e4;
|
|
83
|
+
const SignClient = await loadSignClient();
|
|
84
|
+
const client = await SignClient.init({
|
|
85
|
+
logger: "error",
|
|
86
|
+
projectId: options.projectId,
|
|
87
|
+
metadata: options.metadata
|
|
88
|
+
});
|
|
89
|
+
const { uri, approval } = await client.connect({
|
|
90
|
+
// optionalNamespaces ONLY — Sage rejects requiredNamespaces.
|
|
91
|
+
optionalNamespaces: {
|
|
92
|
+
chia: { methods: WALLET_METHODS, chains: [chain], events: [] }
|
|
93
|
+
}
|
|
94
|
+
});
|
|
95
|
+
if (uri && options.onUri) options.onUri(uri);
|
|
96
|
+
const session = await approval();
|
|
97
|
+
return new _WalletConnectTransport(client, session, chain, timeout);
|
|
98
|
+
}
|
|
99
|
+
/**
|
|
100
|
+
* Restore an existing session that grants a sign method (so message signing actually reaches
|
|
101
|
+
* Sage). Returns a transport or null if none qualifies. Requires `@walletconnect/sign-client`.
|
|
102
|
+
*/
|
|
103
|
+
static async restore(options) {
|
|
104
|
+
const chain = options.chain ?? DEFAULT_CHAIN;
|
|
105
|
+
const timeout = options.requestTimeoutMs ?? 6e4;
|
|
106
|
+
const SignClient = await loadSignClient();
|
|
107
|
+
const client = await SignClient.init({
|
|
108
|
+
logger: "error",
|
|
109
|
+
projectId: options.projectId,
|
|
110
|
+
metadata: options.metadata
|
|
111
|
+
});
|
|
112
|
+
const sessions = client.session.getAll();
|
|
113
|
+
for (let i = sessions.length - 1; i >= 0; i--) {
|
|
114
|
+
const s = sessions[i];
|
|
115
|
+
const methods = s.namespaces?.chia?.methods ?? [];
|
|
116
|
+
if (SIGN_METHODS.some((m) => methods.includes(m))) {
|
|
117
|
+
return new _WalletConnectTransport(client, s, chain, timeout);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
return null;
|
|
121
|
+
}
|
|
122
|
+
sessionMethods() {
|
|
123
|
+
try {
|
|
124
|
+
return this.client.session.get(this.topic)?.namespaces?.chia?.methods ?? [];
|
|
125
|
+
} catch {
|
|
126
|
+
return [];
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
// True iff the active session granted `method` (empty list = unknown ⇒ treated as granted).
|
|
130
|
+
supports(method) {
|
|
131
|
+
const m = this.sessionMethods();
|
|
132
|
+
return m.length === 0 || m.includes(method);
|
|
133
|
+
}
|
|
134
|
+
async request(method, params) {
|
|
135
|
+
if (!this.supports(method)) {
|
|
136
|
+
throw new Error(
|
|
137
|
+
`Your wallet session does not grant "${method}". Disconnect and reconnect your wallet to refresh the session (and make sure Sage is up to date).`
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
const MAX_ATTEMPTS = 3;
|
|
141
|
+
let lastErr;
|
|
142
|
+
for (let i = 0; i < MAX_ATTEMPTS; i++) {
|
|
143
|
+
try {
|
|
144
|
+
return await this.attempt(method, params);
|
|
145
|
+
} catch (e) {
|
|
146
|
+
lastErr = e;
|
|
147
|
+
if (i < MAX_ATTEMPTS - 1 && isTransientPublishError(e)) {
|
|
148
|
+
await sleep(1200 * (i + 1));
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
throw e;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
throw lastErr;
|
|
155
|
+
}
|
|
156
|
+
attempt(method, params) {
|
|
157
|
+
let t;
|
|
158
|
+
const timeout = new Promise((_, rej) => {
|
|
159
|
+
t = setTimeout(
|
|
160
|
+
() => rej(new Error("Sage did not respond \u2014 open the Sage app and try again.")),
|
|
161
|
+
this.requestTimeoutMs
|
|
162
|
+
);
|
|
163
|
+
});
|
|
164
|
+
return Promise.race([
|
|
165
|
+
this.client.request({ topic: this.topic, chainId: this.chain, request: { method, params } }),
|
|
166
|
+
timeout
|
|
167
|
+
]).finally(() => clearTimeout(t));
|
|
168
|
+
}
|
|
169
|
+
async disconnect() {
|
|
170
|
+
try {
|
|
171
|
+
await this.client.disconnect({
|
|
172
|
+
topic: this.topic,
|
|
173
|
+
reason: { code: 6e3, message: "bye" }
|
|
174
|
+
});
|
|
175
|
+
} catch {
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
function sleep(ms) {
|
|
180
|
+
return new Promise((r) => setTimeout(r, ms));
|
|
181
|
+
}
|
|
182
|
+
async function loadSignClient() {
|
|
183
|
+
try {
|
|
184
|
+
const mod = await import('@walletconnect/sign-client');
|
|
185
|
+
const SignClient = mod.default ?? mod;
|
|
186
|
+
if (!SignClient || typeof SignClient.init !== "function") {
|
|
187
|
+
throw new Error("unexpected @walletconnect/sign-client shape");
|
|
188
|
+
}
|
|
189
|
+
return SignClient;
|
|
190
|
+
} catch {
|
|
191
|
+
throw new Error(
|
|
192
|
+
"WalletConnect fallback requires the optional peer dependency '@walletconnect/sign-client'. Install it (npm i @walletconnect/sign-client) to use the WalletConnect\u2192Sage transport."
|
|
193
|
+
);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
// src/provider/methods.ts
|
|
198
|
+
function with0x(hex) {
|
|
199
|
+
if (!hex) return hex ?? null;
|
|
200
|
+
return hex.startsWith("0x") ? hex : `0x${hex}`;
|
|
201
|
+
}
|
|
202
|
+
function strip0x(h) {
|
|
203
|
+
return String(h ?? "").replace(/^0x/i, "").toLowerCase();
|
|
204
|
+
}
|
|
205
|
+
async function getAddress(request) {
|
|
206
|
+
const resp = await request("chia_getAddress", {});
|
|
207
|
+
if (typeof resp === "string") return resp;
|
|
208
|
+
const r = resp;
|
|
209
|
+
return r?.address ?? r?.data?.address ?? null;
|
|
210
|
+
}
|
|
211
|
+
async function getPublicKeys(request) {
|
|
212
|
+
const resp = await request("chip0002_getPublicKeys", { limit: 500, offset: 0 });
|
|
213
|
+
if (Array.isArray(resp)) return resp;
|
|
214
|
+
const r = resp;
|
|
215
|
+
return r?.publicKeys ?? r?.public_keys ?? r?.keys ?? [];
|
|
216
|
+
}
|
|
217
|
+
function normalizeSig(resp) {
|
|
218
|
+
const r = resp;
|
|
219
|
+
const publicKey = typeof r === "object" && r ? r.publicKey ?? r.public_key ?? r.pubkey ?? null : null;
|
|
220
|
+
const signature = typeof r === "string" ? r : r ? r.signature ?? r.aggregatedSignature ?? r.aggregated_signature ?? null : null;
|
|
221
|
+
return { publicKey: with0x(publicKey), signature };
|
|
222
|
+
}
|
|
223
|
+
async function signMessage(request, supports, message, address) {
|
|
224
|
+
if (supports("chia_signMessageByAddress")) {
|
|
225
|
+
return normalizeSig(await request("chia_signMessageByAddress", { message, address }));
|
|
226
|
+
}
|
|
227
|
+
const keys = await getPublicKeys(request);
|
|
228
|
+
const publicKey = keys[0];
|
|
229
|
+
if (!publicKey) throw new Error("Wallet returned no keys to sign with.");
|
|
230
|
+
const sig = normalizeSig(await request("chip0002_signMessage", { message, publicKey }));
|
|
231
|
+
return { publicKey: sig.publicKey ?? with0x(publicKey), signature: sig.signature };
|
|
232
|
+
}
|
|
233
|
+
async function signCoinSpends(request, coinSpends) {
|
|
234
|
+
const resp = await request("chip0002_signCoinSpends", { coinSpends, partialSign: true });
|
|
235
|
+
if (typeof resp === "string") return resp;
|
|
236
|
+
const r = resp;
|
|
237
|
+
return r?.signature ?? r?.aggregatedSignature ?? r?.aggregated_signature ?? "";
|
|
238
|
+
}
|
|
239
|
+
async function takeOffer(request, supports, offer, fee = 0) {
|
|
240
|
+
if (!supports("chia_takeOffer")) {
|
|
241
|
+
throw new Error("Your wallet session does not support taking offers. Reconnect your wallet.");
|
|
242
|
+
}
|
|
243
|
+
return request("chia_takeOffer", { offer, fee });
|
|
244
|
+
}
|
|
245
|
+
function balanceFrom(resp) {
|
|
246
|
+
const raw = resp == null ? null : typeof resp === "object" ? resp.confirmed ?? resp.spendable ?? resp.confirmedWalletBalance ?? resp.confirmed_wallet_balance ?? resp.balance ?? resp.data?.confirmed ?? null : resp;
|
|
247
|
+
if (raw == null) return null;
|
|
248
|
+
try {
|
|
249
|
+
return BigInt(raw).toString();
|
|
250
|
+
} catch {
|
|
251
|
+
return null;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
async function getXchBalance(request) {
|
|
255
|
+
return balanceFrom(await request("chip0002_getAssetBalance", { type: null, assetId: null }));
|
|
256
|
+
}
|
|
257
|
+
async function getCatBalance(request, assetIdHex) {
|
|
258
|
+
return balanceFrom(
|
|
259
|
+
await request("chip0002_getAssetBalance", { type: "cat", assetId: strip0x(assetIdHex) })
|
|
260
|
+
);
|
|
261
|
+
}
|
|
262
|
+
async function getXchCoins(request, limit = 100) {
|
|
263
|
+
const resp = await request("chip0002_getAssetCoins", {
|
|
264
|
+
type: null,
|
|
265
|
+
assetId: null,
|
|
266
|
+
includedLocked: false,
|
|
267
|
+
offset: 0,
|
|
268
|
+
limit
|
|
269
|
+
});
|
|
270
|
+
return Array.isArray(resp) ? resp : resp?.coins ?? [];
|
|
271
|
+
}
|
|
272
|
+
async function getCatCoins(request, assetIdHex, limit = 200) {
|
|
273
|
+
const resp = await request("chip0002_getAssetCoins", {
|
|
274
|
+
type: "cat",
|
|
275
|
+
assetId: strip0x(assetIdHex),
|
|
276
|
+
includedLocked: false,
|
|
277
|
+
offset: 0,
|
|
278
|
+
limit
|
|
279
|
+
});
|
|
280
|
+
return Array.isArray(resp) ? resp : resp?.coins ?? [];
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
// src/provider/chia-provider.ts
|
|
284
|
+
var ChiaProvider = class _ChiaProvider {
|
|
285
|
+
constructor(transport) {
|
|
286
|
+
this.transport = transport;
|
|
287
|
+
}
|
|
288
|
+
/** Which transport is backing this provider. */
|
|
289
|
+
get backend() {
|
|
290
|
+
return this.transport.backend;
|
|
291
|
+
}
|
|
292
|
+
/** The connected session descriptor. */
|
|
293
|
+
get session() {
|
|
294
|
+
return {
|
|
295
|
+
backend: this.transport.backend,
|
|
296
|
+
chain: this.transport.chain,
|
|
297
|
+
topic: this.transport.topic,
|
|
298
|
+
address: this.cachedAddress ?? null
|
|
299
|
+
};
|
|
300
|
+
}
|
|
301
|
+
/** True iff a method is granted by the active session/transport. */
|
|
302
|
+
supports(method) {
|
|
303
|
+
return this.transport.supports(method);
|
|
304
|
+
}
|
|
305
|
+
/** Escape hatch: issue a raw CHIP-0002 request through the active transport. */
|
|
306
|
+
request(method, params = {}) {
|
|
307
|
+
return this.transport.request(method, params);
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Connect a wallet using the selection policy. With `mode: "auto"` (default) it prefers an
|
|
311
|
+
* injected DIG wallet and falls back to WalletConnect.
|
|
312
|
+
*/
|
|
313
|
+
static async connect(options = {}) {
|
|
314
|
+
const mode = options.mode ?? "auto";
|
|
315
|
+
const chain = options.chain ?? DEFAULT_CHAIN;
|
|
316
|
+
const tryInjected = async () => {
|
|
317
|
+
if (!isInjectedAvailable({ anyChia: options.acceptAnyInjected })) return null;
|
|
318
|
+
const provider = getInjectedProvider();
|
|
319
|
+
const transport = new InjectedTransport(provider, chain);
|
|
320
|
+
await transport.connect();
|
|
321
|
+
return new _ChiaProvider(transport);
|
|
322
|
+
};
|
|
323
|
+
const tryWalletConnect = async () => {
|
|
324
|
+
if (!options.walletConnect) {
|
|
325
|
+
throw new Error(
|
|
326
|
+
"WalletConnect options are required to connect via WalletConnect (pass { walletConnect: { projectId, metadata, onUri } })."
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
const transport = await WalletConnectTransport.connect({
|
|
330
|
+
...options.walletConnect,
|
|
331
|
+
chain
|
|
332
|
+
});
|
|
333
|
+
return new _ChiaProvider(transport);
|
|
334
|
+
};
|
|
335
|
+
if (mode === "injected") {
|
|
336
|
+
const injected = await tryInjected();
|
|
337
|
+
if (!injected) {
|
|
338
|
+
throw new Error(
|
|
339
|
+
"No injected DIG wallet found. Open this page in the DIG Browser, or use mode 'auto'."
|
|
340
|
+
);
|
|
341
|
+
}
|
|
342
|
+
return injected;
|
|
343
|
+
}
|
|
344
|
+
if (mode === "walletconnect") return tryWalletConnect();
|
|
345
|
+
return await tryInjected() ?? await tryWalletConnect();
|
|
346
|
+
}
|
|
347
|
+
/** Wrap an already-constructed transport (advanced / restored sessions). */
|
|
348
|
+
static fromTransport(transport) {
|
|
349
|
+
return new _ChiaProvider(transport);
|
|
350
|
+
}
|
|
351
|
+
// ---- normalized CHIP-0002 surface -------------------------------------------------------------
|
|
352
|
+
/** The wallet's receive address (cached after first read). */
|
|
353
|
+
async getAddress() {
|
|
354
|
+
if (this.cachedAddress === void 0) {
|
|
355
|
+
this.cachedAddress = await getAddress((m, p) => this.transport.request(m, p));
|
|
356
|
+
}
|
|
357
|
+
return this.cachedAddress;
|
|
358
|
+
}
|
|
359
|
+
/** The wallet's (synthetic) public keys. */
|
|
360
|
+
getPublicKeys() {
|
|
361
|
+
return getPublicKeys((m, p) => this.transport.request(m, p));
|
|
362
|
+
}
|
|
363
|
+
/**
|
|
364
|
+
* Sign a UTF-8 message with the wallet BLS key. `address` defaults to the wallet's own address.
|
|
365
|
+
* Returns `{ publicKey, signature }`.
|
|
366
|
+
*/
|
|
367
|
+
async signMessage(message, address) {
|
|
368
|
+
const addr = address ?? await this.getAddress() ?? "";
|
|
369
|
+
return signMessage(
|
|
370
|
+
(m, p) => this.transport.request(m, p),
|
|
371
|
+
(m) => this.transport.supports(m),
|
|
372
|
+
message,
|
|
373
|
+
addr
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
/** Sign raw CHIP-0035 coin spends (partialSign). Returns the aggregated signature hex. */
|
|
377
|
+
signCoinSpends(coinSpends) {
|
|
378
|
+
return signCoinSpends((m, p) => this.transport.request(m, p), coinSpends);
|
|
379
|
+
}
|
|
380
|
+
/** Accept a Chia offer string (e.g. an NFT offer). */
|
|
381
|
+
takeOffer(offer, fee = 0) {
|
|
382
|
+
return takeOffer(
|
|
383
|
+
(m, p) => this.transport.request(m, p),
|
|
384
|
+
(m) => this.transport.supports(m),
|
|
385
|
+
offer,
|
|
386
|
+
fee
|
|
387
|
+
);
|
|
388
|
+
}
|
|
389
|
+
/** Spendable XCH balance (mojos, string) or null. */
|
|
390
|
+
getXchBalance() {
|
|
391
|
+
return getXchBalance((m, p) => this.transport.request(m, p));
|
|
392
|
+
}
|
|
393
|
+
/** Spendable balance (base units, string) for a CAT `assetIdHex` (the tail hash), or null. */
|
|
394
|
+
getCatBalance(assetIdHex) {
|
|
395
|
+
return getCatBalance((m, p) => this.transport.request(m, p), assetIdHex);
|
|
396
|
+
}
|
|
397
|
+
/** Unspent XCH coins for funding a spend. */
|
|
398
|
+
getXchCoins(limit) {
|
|
399
|
+
return getXchCoins((m, p) => this.transport.request(m, p), limit);
|
|
400
|
+
}
|
|
401
|
+
/** Unspent CAT coins for `assetIdHex`. */
|
|
402
|
+
getCatCoins(assetIdHex, limit) {
|
|
403
|
+
return getCatCoins((m, p) => this.transport.request(m, p), assetIdHex, limit);
|
|
404
|
+
}
|
|
405
|
+
/** Disconnect the wallet (best-effort). */
|
|
406
|
+
disconnect() {
|
|
407
|
+
return this.transport.disconnect();
|
|
408
|
+
}
|
|
409
|
+
};
|
|
410
|
+
function connectWallet(options) {
|
|
411
|
+
return ChiaProvider.connect(options);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
// src/loader.ts
|
|
415
|
+
var DIG_CLIENT_WASM_SHA256 = "ff486be806f908a2a90780e499a04dbd34e10e3b97be0470cb9ee841a1e49e77";
|
|
416
|
+
var _config = {};
|
|
417
|
+
var _ready = null;
|
|
418
|
+
function configureWasm(config) {
|
|
419
|
+
_config = { ...config };
|
|
420
|
+
_ready = null;
|
|
421
|
+
}
|
|
422
|
+
var isNode = typeof process !== "undefined" && !!process.versions?.node;
|
|
423
|
+
async function sha256Hex(bytes) {
|
|
424
|
+
if (isNode) {
|
|
425
|
+
const { createHash } = await import('crypto');
|
|
426
|
+
const view = bytes instanceof ArrayBuffer ? new Uint8Array(bytes) : new Uint8Array(
|
|
427
|
+
bytes.buffer,
|
|
428
|
+
bytes.byteOffset,
|
|
429
|
+
bytes.byteLength
|
|
430
|
+
);
|
|
431
|
+
return createHash("sha256").update(view).digest("hex");
|
|
432
|
+
}
|
|
433
|
+
const digest = await crypto.subtle.digest("SHA-256", bytes);
|
|
434
|
+
return [...new Uint8Array(digest)].map((b) => b.toString(16).padStart(2, "0")).join("");
|
|
435
|
+
}
|
|
436
|
+
function assertIntegrity(hex) {
|
|
437
|
+
if (_config.skipIntegrity) return;
|
|
438
|
+
if (hex !== DIG_CLIENT_WASM_SHA256) {
|
|
439
|
+
throw new Error(
|
|
440
|
+
`dig-client wasm integrity check failed \u2014 refusing to run unverified crypto (expected ${DIG_CLIENT_WASM_SHA256}, got ${hex}).`
|
|
441
|
+
);
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
async function loadNode() {
|
|
445
|
+
const { readFile } = await import('fs/promises');
|
|
446
|
+
const { fileURLToPath, pathToFileURL } = await import('url');
|
|
447
|
+
const path = await import('path');
|
|
448
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
449
|
+
const vendorDir = path.resolve(here, "..", "vendor");
|
|
450
|
+
const wasmPath = path.join(vendorDir, "dig_client_bg.wasm");
|
|
451
|
+
const gluePath = path.join(vendorDir, "dig_client.mjs");
|
|
452
|
+
const buf = await readFile(wasmPath);
|
|
453
|
+
const bytes = new Uint8Array(buf.byteLength);
|
|
454
|
+
bytes.set(buf);
|
|
455
|
+
return { glueHref: pathToFileURL(gluePath).href, bytes };
|
|
456
|
+
}
|
|
457
|
+
async function loadBrowser() {
|
|
458
|
+
const glueHref = _config.glueUrl ?? new URL("../vendor/dig_client.mjs", import.meta.url).href;
|
|
459
|
+
let bytes;
|
|
460
|
+
if (_config.wasmBytes) {
|
|
461
|
+
bytes = _config.wasmBytes;
|
|
462
|
+
} else {
|
|
463
|
+
const wasmUrl = _config.wasmUrl ?? new URL("../vendor/dig_client_bg.wasm", import.meta.url).href;
|
|
464
|
+
const res = await fetch(wasmUrl);
|
|
465
|
+
if (!res.ok) throw new Error(`dig-client wasm fetch failed (${res.status})`);
|
|
466
|
+
bytes = await res.arrayBuffer();
|
|
467
|
+
}
|
|
468
|
+
return { glueHref, bytes };
|
|
469
|
+
}
|
|
470
|
+
function loadDigClientWasm() {
|
|
471
|
+
if (_ready) return _ready;
|
|
472
|
+
_ready = (async () => {
|
|
473
|
+
const { glueHref, bytes } = isNode ? await loadNode() : await loadBrowser();
|
|
474
|
+
assertIntegrity(await sha256Hex(bytes));
|
|
475
|
+
const mod = await import(
|
|
476
|
+
/* @vite-ignore */
|
|
477
|
+
glueHref
|
|
478
|
+
);
|
|
479
|
+
await mod.default({ module_or_path: bytes });
|
|
480
|
+
return mod;
|
|
481
|
+
})().catch((e) => {
|
|
482
|
+
_ready = null;
|
|
483
|
+
throw e;
|
|
484
|
+
});
|
|
485
|
+
return _ready;
|
|
486
|
+
}
|
|
487
|
+
|
|
488
|
+
// src/urn.ts
|
|
489
|
+
var URN_RE = /^urn:dig:chia:([0-9a-fA-F]{64})(?::([0-9a-fA-F]{64}))?\/(.+?)(?:\?salt=([0-9a-fA-F]+))?$/;
|
|
490
|
+
function parseUrn(raw) {
|
|
491
|
+
const s = String(raw ?? "").trim();
|
|
492
|
+
const m = URN_RE.exec(s);
|
|
493
|
+
if (!m) {
|
|
494
|
+
throw new Error(
|
|
495
|
+
"Not a valid dig URN (expected urn:dig:chia:<store-id>[:<root>]/<path>[?salt=<hex>])."
|
|
496
|
+
);
|
|
497
|
+
}
|
|
498
|
+
return {
|
|
499
|
+
storeId: m[1].toLowerCase(),
|
|
500
|
+
root: m[2] ? m[2].toLowerCase() : null,
|
|
501
|
+
resourceKey: m[3],
|
|
502
|
+
salt: m[4] ? m[4].toLowerCase() : null
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
function isUrn(raw) {
|
|
506
|
+
try {
|
|
507
|
+
parseUrn(raw);
|
|
508
|
+
return true;
|
|
509
|
+
} catch {
|
|
510
|
+
return false;
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
function reconstructUrn(storeId, resourceKey) {
|
|
514
|
+
const key = resourceKey && resourceKey.length > 0 ? resourceKey : "index.html";
|
|
515
|
+
return `urn:dig:chia:${storeId.toLowerCase()}/${key}`;
|
|
516
|
+
}
|
|
517
|
+
function reconstructUrnWithRoot(storeId, root, resourceKey) {
|
|
518
|
+
const key = resourceKey && resourceKey.length > 0 ? resourceKey : "index.html";
|
|
519
|
+
return `urn:dig:chia:${storeId.toLowerCase()}:${root.toLowerCase()}/${key}`;
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
// src/dig-client.ts
|
|
523
|
+
var DEFAULT_RPC = "https://rpc.dig.net";
|
|
524
|
+
var RPC_CHUNK_BYTES = 3 * 1024 * 1024;
|
|
525
|
+
function b64ToBytes(b64) {
|
|
526
|
+
if (typeof atob === "function") {
|
|
527
|
+
const bin = atob(b64);
|
|
528
|
+
const out = new Uint8Array(bin.length);
|
|
529
|
+
for (let i = 0; i < bin.length; i++) out[i] = bin.charCodeAt(i);
|
|
530
|
+
return out;
|
|
531
|
+
}
|
|
532
|
+
return new Uint8Array(globalThis.Buffer.from(b64, "base64"));
|
|
533
|
+
}
|
|
534
|
+
var DigClient = class {
|
|
535
|
+
constructor(options = {}) {
|
|
536
|
+
this.rpc = options.rpc ?? DEFAULT_RPC;
|
|
537
|
+
this.fetchImpl = options.fetch ?? (typeof fetch === "function" ? fetch.bind(globalThis) : undefinedFetch());
|
|
538
|
+
}
|
|
539
|
+
/** Load (and SRI-verify) the read-crypto wasm. Exposed for callers that want the raw functions. */
|
|
540
|
+
async wasm() {
|
|
541
|
+
return loadDigClientWasm();
|
|
542
|
+
}
|
|
543
|
+
/** `retrieval_key = SHA-256(canonical rootless URN)`, lowercase hex. */
|
|
544
|
+
async retrievalKey(storeId, resourceKey) {
|
|
545
|
+
return (await this.wasm()).retrievalKey(storeId, resourceKey);
|
|
546
|
+
}
|
|
547
|
+
/** Derive the per-URN AES-256-GCM-SIV key, lowercase hex. `salt` for a private store. */
|
|
548
|
+
async deriveKey(storeId, resourceKey, salt) {
|
|
549
|
+
return (await this.wasm()).deriveKey(storeId, resourceKey, salt ?? void 0);
|
|
550
|
+
}
|
|
551
|
+
/** Verify served `ciphertext` is included under `root` via the base64 merkle `proof`. */
|
|
552
|
+
async verifyInclusion(ciphertext, proof, root) {
|
|
553
|
+
return (await this.wasm()).verifyInclusion(ciphertext, proof, root);
|
|
554
|
+
}
|
|
555
|
+
/** Reconstruct the canonical rootless URN whose SHA-256 is the retrieval key. */
|
|
556
|
+
async reconstructUrn(storeId, resourceKey) {
|
|
557
|
+
return (await this.wasm()).reconstructUrn(storeId, resourceKey);
|
|
558
|
+
}
|
|
559
|
+
/**
|
|
560
|
+
* Derive, client-side, the two root-independent keys a URN maps to (retrieval + decryption).
|
|
561
|
+
* Nothing is sent to the network — pure local derivation via the wasm.
|
|
562
|
+
*/
|
|
563
|
+
async deriveUrnKeys(input) {
|
|
564
|
+
const parsed = parseUrn(input.urn);
|
|
565
|
+
const wasm = await this.wasm();
|
|
566
|
+
const effSalt = input.salt ?? parsed.salt ?? void 0;
|
|
567
|
+
return {
|
|
568
|
+
storeId: parsed.storeId,
|
|
569
|
+
root: parsed.root,
|
|
570
|
+
resourceKey: parsed.resourceKey,
|
|
571
|
+
salt: effSalt ?? null,
|
|
572
|
+
retrievalKey: wasm.retrievalKey(parsed.storeId, parsed.resourceKey),
|
|
573
|
+
decryptionKey: wasm.deriveKey(parsed.storeId, parsed.resourceKey, effSalt)
|
|
574
|
+
};
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* Fetch + verify + decrypt one resource by URN. `root` is the on-chain generation root to verify
|
|
578
|
+
* against (resolved by the caller from the chain); when omitted, the root embedded in the URN is
|
|
579
|
+
* used. Returns the bytes plus advisory `verified`/`decrypted` flags. NEVER throws "not found"
|
|
580
|
+
* (presence is unknowable) — it throws only on a transport failure.
|
|
581
|
+
*/
|
|
582
|
+
async read(input, opts = {}) {
|
|
583
|
+
const parsed = parseUrn(input.urn);
|
|
584
|
+
const effSalt = input.salt ?? parsed.salt ?? null;
|
|
585
|
+
const effRoot = input.root ?? parsed.root ?? null;
|
|
586
|
+
if (!effRoot) {
|
|
587
|
+
throw new Error(
|
|
588
|
+
"a confirmed on-chain root is required to read content (pass { root } or use a root-pinned URN)"
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
return this.readResource(
|
|
592
|
+
{ storeId: parsed.storeId, resourceKey: parsed.resourceKey, root: effRoot, salt: effSalt },
|
|
593
|
+
opts
|
|
594
|
+
);
|
|
595
|
+
}
|
|
596
|
+
/** As `read`, but decoding the plaintext to a UTF-8 string when it decrypts (else throws). */
|
|
597
|
+
async readText(input, opts = {}) {
|
|
598
|
+
const r = await this.read(input, opts);
|
|
599
|
+
if (!r.decrypted) {
|
|
600
|
+
throw new Error(
|
|
601
|
+
"resource did not decrypt under this URN \u2014 wrong store/key/salt, or a decoy response"
|
|
602
|
+
);
|
|
603
|
+
}
|
|
604
|
+
return new TextDecoder().decode(r.bytes);
|
|
605
|
+
}
|
|
606
|
+
/**
|
|
607
|
+
* Read by explicit (storeId, resourceKey, root, salt) rather than a URN string. The oblivious
|
|
608
|
+
* download primitive the URN read is built on.
|
|
609
|
+
*/
|
|
610
|
+
async readResource(input, opts = {}) {
|
|
611
|
+
const rpc = opts.rpc ?? this.rpc;
|
|
612
|
+
const wasm = await this.wasm();
|
|
613
|
+
const rk = wasm.retrievalKey(input.storeId, input.resourceKey);
|
|
614
|
+
const { ciphertext, proof, chunkLens } = await this.fetchCiphertext(
|
|
615
|
+
input.storeId,
|
|
616
|
+
rk,
|
|
617
|
+
input.root,
|
|
618
|
+
rpc
|
|
619
|
+
);
|
|
620
|
+
let verified = false;
|
|
621
|
+
try {
|
|
622
|
+
verified = !!wasm.verifyInclusion(ciphertext, proof, input.root);
|
|
623
|
+
} catch {
|
|
624
|
+
verified = false;
|
|
625
|
+
}
|
|
626
|
+
const keyHex = wasm.deriveKey(input.storeId, input.resourceKey, input.salt ?? void 0);
|
|
627
|
+
try {
|
|
628
|
+
const bytes = decryptResourceChunks(wasm, keyHex, ciphertext, chunkLens);
|
|
629
|
+
return {
|
|
630
|
+
storeId: input.storeId,
|
|
631
|
+
root: input.root,
|
|
632
|
+
resourceKey: input.resourceKey,
|
|
633
|
+
salt: input.salt ?? null,
|
|
634
|
+
bytes,
|
|
635
|
+
verified,
|
|
636
|
+
decrypted: true
|
|
637
|
+
};
|
|
638
|
+
} catch {
|
|
639
|
+
return {
|
|
640
|
+
storeId: input.storeId,
|
|
641
|
+
root: input.root,
|
|
642
|
+
resourceKey: input.resourceKey,
|
|
643
|
+
salt: input.salt ?? null,
|
|
644
|
+
bytes: ciphertext,
|
|
645
|
+
verified,
|
|
646
|
+
decrypted: false
|
|
647
|
+
};
|
|
648
|
+
}
|
|
649
|
+
}
|
|
650
|
+
// Stream the FULL ciphertext for a resource from the RPC by retrieval key, reassembling 3-MiB
|
|
651
|
+
// chunks. A null result is a TRANSPORT failure, never a presence judgment.
|
|
652
|
+
async fetchCiphertext(storeId, rk, root, rpc) {
|
|
653
|
+
let offset = 0;
|
|
654
|
+
let total = null;
|
|
655
|
+
let buf = null;
|
|
656
|
+
let proof = "";
|
|
657
|
+
let chunkLens = null;
|
|
658
|
+
for (; ; ) {
|
|
659
|
+
const r = await this.rpcCall(rpc, "dig.getContent", {
|
|
660
|
+
store_id: storeId,
|
|
661
|
+
root,
|
|
662
|
+
retrieval_key: rk,
|
|
663
|
+
offset,
|
|
664
|
+
length: RPC_CHUNK_BYTES
|
|
665
|
+
});
|
|
666
|
+
if (!r) throw new Error("The content network returned no data for this request.");
|
|
667
|
+
if (total === null) {
|
|
668
|
+
total = r.total_length >>> 0;
|
|
669
|
+
buf = new Uint8Array(total);
|
|
670
|
+
}
|
|
671
|
+
if (chunkLens === null && Array.isArray(r.chunk_lens)) {
|
|
672
|
+
chunkLens = r.chunk_lens.map((n) => n >>> 0);
|
|
673
|
+
}
|
|
674
|
+
const chunk = b64ToBytes(r.ciphertext ?? "");
|
|
675
|
+
const at = r.offset >>> 0;
|
|
676
|
+
buf.set(chunk.subarray(0, Math.max(0, Math.min(chunk.length, total - at))), at);
|
|
677
|
+
if (r.inclusion_proof) proof = r.inclusion_proof;
|
|
678
|
+
if (r.complete || r.next_offset == null) break;
|
|
679
|
+
offset = r.next_offset >>> 0;
|
|
680
|
+
}
|
|
681
|
+
return { ciphertext: buf ?? new Uint8Array(0), proof, chunkLens };
|
|
682
|
+
}
|
|
683
|
+
// One JSON-RPC 2.0 call. Throws on transport failure or a JSON-RPC error; returns `result`.
|
|
684
|
+
async rpcCall(rpc, method, params) {
|
|
685
|
+
let res;
|
|
686
|
+
try {
|
|
687
|
+
res = await this.fetchImpl(rpc, {
|
|
688
|
+
method: "POST",
|
|
689
|
+
headers: { "content-type": "application/json" },
|
|
690
|
+
body: JSON.stringify({ jsonrpc: "2.0", id: 1, method, params })
|
|
691
|
+
});
|
|
692
|
+
} catch {
|
|
693
|
+
throw new Error("Could not reach the content network. Check your connection and try again.");
|
|
694
|
+
}
|
|
695
|
+
if (!res.ok) throw new Error(`dig RPC ${method} failed (${res.status})`);
|
|
696
|
+
const json = await res.json();
|
|
697
|
+
if (json && json.error) throw new Error(`dig RPC ${method}: ${json.error.message ?? "error"}`);
|
|
698
|
+
return json ? json.result ?? null : null;
|
|
699
|
+
}
|
|
15
700
|
};
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
701
|
+
function decryptResourceChunks(wasm, keyHex, ciphertext, chunkLens) {
|
|
702
|
+
const lens = chunkLens && chunkLens.length ? chunkLens : [ciphertext.length];
|
|
703
|
+
const total = lens.reduce((a, n) => a + n, 0);
|
|
704
|
+
if (total !== ciphertext.length) {
|
|
705
|
+
throw new Error("served ciphertext length does not match chunk lengths");
|
|
706
|
+
}
|
|
707
|
+
if (lens.length === 1) return wasm.decryptChunk(keyHex, ciphertext);
|
|
708
|
+
const parts = [];
|
|
709
|
+
let p = 0;
|
|
710
|
+
for (const len of lens) {
|
|
711
|
+
parts.push(wasm.decryptChunk(keyHex, ciphertext.subarray(p, p + len)));
|
|
712
|
+
p += len;
|
|
713
|
+
}
|
|
714
|
+
const out = new Uint8Array(parts.reduce((a, x) => a + x.length, 0));
|
|
715
|
+
let q = 0;
|
|
716
|
+
for (const part of parts) {
|
|
717
|
+
out.set(part, q);
|
|
718
|
+
q += part.length;
|
|
719
|
+
}
|
|
720
|
+
return out;
|
|
721
|
+
}
|
|
722
|
+
function undefinedFetch() {
|
|
723
|
+
return (() => {
|
|
724
|
+
throw new Error(
|
|
725
|
+
"No global fetch available. Pass { fetch } to DigClient (Node < 18 needs a fetch polyfill)."
|
|
726
|
+
);
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
|
|
730
|
+
export { ChiaProvider, DEFAULT_CHAIN, DEFAULT_RPC, DIG_CLIENT_WASM_SHA256, DigClient, INJECTED_TOPIC, InjectedTransport, SIGN_METHODS, WALLET_METHODS, WalletConnectTransport, configureWasm, connectWallet, getInjectedProvider, isInjectedAvailable, isTransientPublishError, isUrn, loadDigClientWasm, parseUrn, reconstructUrn, reconstructUrnWithRoot };
|
|
731
|
+
//# sourceMappingURL=index.js.map
|
|
732
|
+
//# sourceMappingURL=index.js.map
|