@needmoretruth/nmts-cli 0.58.0 → 0.59.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/CHANGELOG.md +15 -0
- package/README.ko.md +1 -1
- package/README.md +1 -1
- package/dist/arg-options.d.ts +2 -0
- package/dist/args.js +2 -0
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/expiring.js +2 -2
- package/dist/commands/sweep.js +6 -64
- package/dist/commands/wallet-storage-extend.d.ts +23 -0
- package/dist/commands/wallet-storage-extend.js +137 -0
- package/dist/commands/wallet.d.ts +3 -0
- package/dist/commands/wallet.js +3 -1
- package/dist/extend-chain.js +19 -3
- package/dist/extend-plan.d.ts +38 -0
- package/dist/extend-plan.js +61 -0
- package/dist/help-options.js +4 -1
- package/dist/help.js +3 -0
- package/dist/main.js +1 -1
- package/dist/platform-sign.d.ts +12 -0
- package/dist/platform-sign.js +29 -1
- package/dist/portable.d.ts +1 -1
- package/dist/portable.js +1 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-codes.d.ts +14 -3
- package/dist/public-codes.js +14 -5
- package/dist/rebuild.js +9 -24
- package/dist/risk.d.ts +12 -0
- package/dist/risk.js +8 -0
- package/dist/server-objects.d.ts +21 -0
- package/dist/server-objects.js +68 -0
- package/dist/sharing.d.ts +1 -1
- package/dist/sharing.js +12 -40
- package/dist/storage-control/extend-for.d.ts +84 -0
- package/dist/storage-control/extend-for.js +204 -0
- package/dist/storage-control/extend.d.ts +57 -12
- package/dist/storage-control/extend.js +51 -45
- package/dist/storage-control.d.ts +4 -2
- package/dist/storage-control.js +4 -2
- package/docs/commands/wallet.md +13 -1
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
// A business keeping a user's files alive from its own wallet, with no key of the user's anywhere in
|
|
2
|
+
// the process: the user's delegation token says which files and records the new dates, the
|
|
3
|
+
// business's wallet holds the storage and pays.
|
|
4
|
+
//
|
|
5
|
+
// ⛔ WHO PAYS CHOSE TO PAY. This is the business spending its own WAL on storage its own wallet holds
|
|
6
|
+
// — the storage a sponsored upload left in that wallet. It is not a person's money moving on its
|
|
7
|
+
// own: a person's wallet is never the payer here, and nothing here can make it one. A person's
|
|
8
|
+
// own storage is extended only when that person approves it, every time.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ ONLY STORAGE THE PAYING WALLET HOLDS. A blob held by any other address is named and skipped
|
|
11
|
+
// before anything is signed (`refuseUnheld`). The chain would refuse it anyway, but only after a
|
|
12
|
+
// signature had been asked for, and the user's own storage is the user's to extend.
|
|
13
|
+
//
|
|
14
|
+
// ⛔ NO NMTS KEY. Everything the walk needs is on the NMTS server under the user's credential —
|
|
15
|
+
// `/v1/objects` for which rows exist, `/extend-preview` for which blobs — and on the chain. File
|
|
16
|
+
// names are inside the sealed list, which only the key opens, so a file is named here by its id.
|
|
17
|
+
//
|
|
18
|
+
// ⛔ ONE FILE, ONE TRANSACTION. The server records an extension against one item per transaction
|
|
19
|
+
// digest (its replay guard), so each file is bought and recorded on its own.
|
|
20
|
+
//
|
|
21
|
+
// ⚠ A FILE IT CANNOT EXTEND IS A ROW, NOT A FAILURE OF THE RUN. A daily job over a hundred users must
|
|
22
|
+
// not stop at the first file whose storage another wallet holds. What does stop the run is what
|
|
23
|
+
// would make the next spend wrong too: a wallet known to be short, a server that stopped writing
|
|
24
|
+
// dates down, a credential the server no longer takes.
|
|
25
|
+
import { ServerError } from "../api.js";
|
|
26
|
+
import { NmtsError } from "../errors.js";
|
|
27
|
+
import { readWindowOrRefuse } from "../extend-plan.js";
|
|
28
|
+
import { readDelegation, SCOPE_BITS } from "../platform-sign.js";
|
|
29
|
+
import { serverObjects } from "../server-objects.js";
|
|
30
|
+
import { defaultReads, planItemExtension, recordExtension } from "./extend.js";
|
|
31
|
+
/**
|
|
32
|
+
* Extend a user's files from the paying wallet. Files not due, held by another wallet, already at the
|
|
33
|
+
* network's ceiling or past their end are skipped and said so; the rest are bought one by one.
|
|
34
|
+
*
|
|
35
|
+
* ⛔ THIS SIGNS unless `dryRun` is set, and nothing it buys comes back.
|
|
36
|
+
*/
|
|
37
|
+
export async function extendFor(input, ask, seams) {
|
|
38
|
+
refuseWithoutScopes(input.token);
|
|
39
|
+
const where = { server: input.server, apiKey: input.token, network: input.network };
|
|
40
|
+
const targets = await targetsOf(input, ask.items);
|
|
41
|
+
const reads = await (seams.readChain ?? defaultReads)(input.network);
|
|
42
|
+
const window = await readWindowOrRefuse(reads, input.network);
|
|
43
|
+
const dryRun = ask.dryRun === true;
|
|
44
|
+
const result = {
|
|
45
|
+
dryRun,
|
|
46
|
+
wallet: { address: input.payer, wal: null, sui: null },
|
|
47
|
+
files: [],
|
|
48
|
+
wal: "0",
|
|
49
|
+
sui: "0",
|
|
50
|
+
stopped: null,
|
|
51
|
+
};
|
|
52
|
+
let wal = 0n;
|
|
53
|
+
let sui = 0n;
|
|
54
|
+
let walletRead = false;
|
|
55
|
+
for (const target of targets) {
|
|
56
|
+
if (target.skip !== null) {
|
|
57
|
+
result.files.push(skipped(target.id, target.skip.code, target.skip.reason));
|
|
58
|
+
continue;
|
|
59
|
+
}
|
|
60
|
+
let plan;
|
|
61
|
+
try {
|
|
62
|
+
plan = await planItemExtension(where, { id: target.id, label: target.id }, {
|
|
63
|
+
reads,
|
|
64
|
+
window,
|
|
65
|
+
address: input.payer,
|
|
66
|
+
epochs: ask.epochs,
|
|
67
|
+
now: seams.now,
|
|
68
|
+
hints: seams.hints,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (stopsTheRun(error)) {
|
|
73
|
+
result.stopped = { code: "CREDENTIAL_REFUSED", reason: message(error) };
|
|
74
|
+
break;
|
|
75
|
+
}
|
|
76
|
+
if (!(error instanceof NmtsError))
|
|
77
|
+
throw error;
|
|
78
|
+
result.files.push(skipped(target.id, codeOf(error), message(error)));
|
|
79
|
+
continue;
|
|
80
|
+
}
|
|
81
|
+
const budget = plan.budget;
|
|
82
|
+
if (!walletRead) {
|
|
83
|
+
result.wallet = { address: input.payer, wal: amountOrNull(budget.walFrost), sui: amountOrNull(budget.suiMist) };
|
|
84
|
+
walletRead = true;
|
|
85
|
+
}
|
|
86
|
+
const priced = {
|
|
87
|
+
epochs: plan.epochs,
|
|
88
|
+
bytes: plan.bytes,
|
|
89
|
+
endEpoch: plan.facts.endEpoch,
|
|
90
|
+
newEndEpoch: plan.facts.newEndEpoch,
|
|
91
|
+
wal: budget.priceFrost.toString(),
|
|
92
|
+
sui: budget.feeMist === null ? null : budget.feeMist.toString(),
|
|
93
|
+
};
|
|
94
|
+
// ⛔ A FILE THAT IS NOT RUNNING OUT IS NOT EXTENDED BY A SCHEDULE, and a dry run says what the
|
|
95
|
+
// real run would spend — so it skips the same files.
|
|
96
|
+
if (plan.stage === "later" && ask.force !== true) {
|
|
97
|
+
result.files.push({
|
|
98
|
+
...skipped(target.id, "EXTEND_NOT_DUE", "It is not near the end of its storage term."),
|
|
99
|
+
epochs: null,
|
|
100
|
+
endEpoch: plan.facts.endEpoch,
|
|
101
|
+
});
|
|
102
|
+
continue;
|
|
103
|
+
}
|
|
104
|
+
if (dryRun) {
|
|
105
|
+
result.files.push({ itemId: target.id, outcome: "priced", code: null, reason: null, digest: null, ...priced });
|
|
106
|
+
wal += budget.priceFrost;
|
|
107
|
+
sui += budget.feeMist ?? 0n;
|
|
108
|
+
continue;
|
|
109
|
+
}
|
|
110
|
+
// ⛔ A WALLET KNOWN TO BE SHORT STOPS THE RUN BEFORE THE NEXT SIGNATURE, with both numbers.
|
|
111
|
+
if (budget.shortfall !== null) {
|
|
112
|
+
result.stopped = { code: "EXTEND_SHORTFALL", reason: `${budget.shortfall} Send what is missing to ${budget.address}.` };
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
const sign = seams.sign ?? refuseUnsigned;
|
|
116
|
+
const digest = await sign({ objectIds: plan.objectIds, epochs: plan.epochs });
|
|
117
|
+
seams.onSigned?.(target.id, digest);
|
|
118
|
+
wal += budget.priceFrost;
|
|
119
|
+
sui += budget.feeMist ?? 0n;
|
|
120
|
+
const outcome = await recordExtension(where, plan, digest);
|
|
121
|
+
result.files.push({ itemId: target.id, outcome: "extended", code: null, reason: null, digest, ...priced });
|
|
122
|
+
if (!outcome.recorded) {
|
|
123
|
+
// ⛔ THE STORAGE IS EXTENDED AND PAID FOR. Going on would pay for more files whose dates the
|
|
124
|
+
// server would not write down either.
|
|
125
|
+
result.stopped = {
|
|
126
|
+
code: "EXTEND_RECORDED_LATE",
|
|
127
|
+
reason: `The storage of ${target.id} IS extended and paid for (transaction ${digest}), and the NMTS ` +
|
|
128
|
+
`server was not told, so the account goes on showing the old date. Do not extend it again. ` +
|
|
129
|
+
`Cause: ${outcome.notRecorded ?? "unknown"}`,
|
|
130
|
+
};
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
result.wal = wal.toString();
|
|
135
|
+
result.sui = sui.toString();
|
|
136
|
+
return result;
|
|
137
|
+
}
|
|
138
|
+
/** The files to visit: the ones named, or every row the user's account holds. */
|
|
139
|
+
async function targetsOf(input, named) {
|
|
140
|
+
if (named !== undefined) {
|
|
141
|
+
if (named.length === 0) {
|
|
142
|
+
throw new NmtsError("No file was named to extend.", {
|
|
143
|
+
exitCode: 2,
|
|
144
|
+
nextStep: "Nothing was signed and nothing was charged. Name item ids, or leave them out to take every file.",
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
return [...new Set(named)].map((id) => ({ id, skip: null }));
|
|
148
|
+
}
|
|
149
|
+
const listing = await serverObjects(input.server, input.token);
|
|
150
|
+
if (!listing.complete) {
|
|
151
|
+
throw new NmtsError("This account has more stored files than one run will read.", {
|
|
152
|
+
exitCode: 4,
|
|
153
|
+
nextStep: "Nothing was signed and nothing was charged. Name the item ids to extend instead.",
|
|
154
|
+
});
|
|
155
|
+
}
|
|
156
|
+
return listing.rows.map((row) => ({
|
|
157
|
+
id: row.id,
|
|
158
|
+
// ⚠ A file in the trash is on its way out. Paying to keep it is a choice somebody makes by
|
|
159
|
+
// naming it, never a schedule's default.
|
|
160
|
+
skip: row.trashed ? { code: "EXTEND_TRASHED", reason: "It is in the trash." } : null,
|
|
161
|
+
}));
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Refuse a token that could not see the files or could not record the new dates — before the first
|
|
165
|
+
* read, not after the first payment.
|
|
166
|
+
*/
|
|
167
|
+
function refuseWithoutScopes(token) {
|
|
168
|
+
const read = readDelegation(token);
|
|
169
|
+
if (read === null)
|
|
170
|
+
return;
|
|
171
|
+
const missing = ["files_read", "storage_spend"].filter((name) => (read.scope & SCOPE_BITS[name]) === 0);
|
|
172
|
+
if (missing.length === 0)
|
|
173
|
+
return;
|
|
174
|
+
throw new NmtsError(`DELEGATION_SCOPE: the user's delegation token was minted without ${missing.map((m) => `\`${m}\``).join(" and ")}.`, {
|
|
175
|
+
exitCode: 3,
|
|
176
|
+
nextStep: "Nothing was signed and nothing was charged. Mint the user a token that carries `files_read` " +
|
|
177
|
+
"and `storage_spend`: the first lists the files, the second records the new dates.",
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
/** No signer was supplied. A defect in the caller, refused before anything is spent. */
|
|
181
|
+
async function refuseUnsigned() {
|
|
182
|
+
throw new NmtsError("No signer was supplied for the paying wallet.", {
|
|
183
|
+
exitCode: 1,
|
|
184
|
+
nextStep: "Nothing was signed. This is a defect in the caller of `extendFor`.",
|
|
185
|
+
});
|
|
186
|
+
}
|
|
187
|
+
/** A credential the server refused for one file is refused for the rest. */
|
|
188
|
+
function stopsTheRun(error) {
|
|
189
|
+
return error instanceof ServerError && (error.status === 401 || error.status === 403);
|
|
190
|
+
}
|
|
191
|
+
function skipped(itemId, code, reason) {
|
|
192
|
+
return { itemId, outcome: "skipped", code, reason, epochs: null, bytes: null, endEpoch: null, newEndEpoch: null, wal: null, sui: null, digest: null };
|
|
193
|
+
}
|
|
194
|
+
/** The leading `CODE:` of a refusal, or a neutral code for one that carries none. */
|
|
195
|
+
function codeOf(error) {
|
|
196
|
+
const match = /^([A-Z][A-Z_]{3,}):/.exec(error.message);
|
|
197
|
+
return match?.[1] ?? "EXTEND_SKIPPED";
|
|
198
|
+
}
|
|
199
|
+
function message(error) {
|
|
200
|
+
return error instanceof Error ? error.message : String(error);
|
|
201
|
+
}
|
|
202
|
+
function amountOrNull(value) {
|
|
203
|
+
return value === null ? null : value.toString();
|
|
204
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { type Budget } from "../extend-budget.ts";
|
|
2
2
|
import { type DaysLeft, type ExpiryStage } from "../expiry.ts";
|
|
3
|
-
import { type
|
|
3
|
+
import { type ExtendReads, type ExtendWindow, type SignExtension } from "../extend-plan.ts";
|
|
4
4
|
import type { StorageHints } from "./hints.ts";
|
|
5
5
|
import { type AccountSettings } from "../shared/lib/drive/manifest-settings.ts";
|
|
6
6
|
/** Where to talk, what opens the list, and whose list it is. */
|
|
@@ -46,20 +46,15 @@ export interface ExtendFacts {
|
|
|
46
46
|
feeMist: string | null;
|
|
47
47
|
feeSui: string | null;
|
|
48
48
|
}
|
|
49
|
-
/** What one
|
|
50
|
-
export interface
|
|
49
|
+
/** What extending one stored file would buy, worked out from its id — no NMTS key needed. */
|
|
50
|
+
export interface ItemExtendPlan {
|
|
51
51
|
facts: ExtendFacts;
|
|
52
52
|
itemId: string;
|
|
53
|
-
/** The file's full path in the account, as the list spells it. */
|
|
54
|
-
path: string;
|
|
55
53
|
/** The blobs the transaction would name. */
|
|
56
54
|
objectIds: readonly string[];
|
|
55
|
+
/** Their sizes added up, as the chain records them — what a business's ledger charges by. */
|
|
56
|
+
bytes: number;
|
|
57
57
|
epochs: number;
|
|
58
|
-
/**
|
|
59
|
-
* ⚠ THE WALLET'S NUMBER. `facts.wallet` is that wallet's address — the one the price was measured
|
|
60
|
-
* against, and the one that signs.
|
|
61
|
-
*/
|
|
62
|
-
wallet: number;
|
|
63
58
|
/**
|
|
64
59
|
* How close this file is to running out.
|
|
65
60
|
*
|
|
@@ -69,6 +64,16 @@ export interface ExtendPlan {
|
|
|
69
64
|
*/
|
|
70
65
|
stage: ExpiryStage;
|
|
71
66
|
budget: Budget;
|
|
67
|
+
}
|
|
68
|
+
/** What one extension would buy, and everything the decision to buy it rests on. */
|
|
69
|
+
export interface ExtendPlan extends ItemExtendPlan {
|
|
70
|
+
/** The file's full path in the account, as the list spells it. */
|
|
71
|
+
path: string;
|
|
72
|
+
/**
|
|
73
|
+
* ⚠ THE WALLET'S NUMBER. `facts.wallet` is that wallet's address — the one the price was measured
|
|
74
|
+
* against, and the one that signs.
|
|
75
|
+
*/
|
|
76
|
+
wallet: number;
|
|
72
77
|
/** The sealed list's settings, as read for this price. */
|
|
73
78
|
settings: AccountSettings | undefined;
|
|
74
79
|
}
|
|
@@ -101,6 +106,36 @@ export interface ExtendPlanSeams {
|
|
|
101
106
|
* what lets a price be asked for without an agreement being asked for first.
|
|
102
107
|
*/
|
|
103
108
|
export declare function planExtension(input: ExtendInput, target: string, seams: ExtendPlanSeams): Promise<ExtendPlan>;
|
|
109
|
+
/** Where an extension is planned and recorded: the account's server, its credential and its chain. */
|
|
110
|
+
export interface ItemExtendWhere {
|
|
111
|
+
server: string;
|
|
112
|
+
/** An API key or a delegation token for the account that holds the file. No NMTS key is needed. */
|
|
113
|
+
apiKey: string;
|
|
114
|
+
network: string;
|
|
115
|
+
}
|
|
116
|
+
/** What planning one file by its id needs: the chain, read once per run, and who pays. */
|
|
117
|
+
export interface ItemPlanSeams {
|
|
118
|
+
reads: ExtendReads;
|
|
119
|
+
window: ExtendWindow;
|
|
120
|
+
/** The address that will sign. Every blob must be held by it, and its balances are read. */
|
|
121
|
+
address: string;
|
|
122
|
+
epochs?: string | number | undefined;
|
|
123
|
+
now: number;
|
|
124
|
+
hints?: StorageHints | undefined;
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Plan one file's extension from its id. The NMTS server says which blobs, the chain says how long
|
|
128
|
+
* each runs and who holds it, and the paying wallet's balances are read against the price.
|
|
129
|
+
*
|
|
130
|
+
* ⛔ NO NMTS KEY. This is what lets a business's server keep a user's files alive with the user's
|
|
131
|
+
* delegation token and its own wallet, while the key that opens the files stays on the user's
|
|
132
|
+
* device. The file is named by `item.label`: its path when a key opened the list, its id when
|
|
133
|
+
* nothing could.
|
|
134
|
+
*/
|
|
135
|
+
export declare function planItemExtension(where: ItemExtendWhere, item: {
|
|
136
|
+
id: string;
|
|
137
|
+
label: string;
|
|
138
|
+
}, seams: ItemPlanSeams): Promise<ItemExtendPlan>;
|
|
104
139
|
/** What one extension did. ⛔ By the time this exists, the storage IS extended and paid for. */
|
|
105
140
|
export interface ExtendOutcome {
|
|
106
141
|
/** The transaction digest — what the server records as the replay guard. */
|
|
@@ -130,5 +165,15 @@ export interface ExtendApplySeams {
|
|
|
130
165
|
* the plan, and calling this is the answer.
|
|
131
166
|
*/
|
|
132
167
|
export declare function applyExtension(input: ExtendInput, plan: ExtendPlan, seams?: ExtendApplySeams): Promise<ExtendOutcome>;
|
|
133
|
-
/**
|
|
134
|
-
|
|
168
|
+
/**
|
|
169
|
+
* Tell the NMTS server about an extension that is already signed and paid for.
|
|
170
|
+
*
|
|
171
|
+
* From here the storage IS extended. Recording it is bookkeeping, and a failure to record must
|
|
172
|
+
* never be reported as a failure to extend — that reading invites a second run, which pays again.
|
|
173
|
+
*/
|
|
174
|
+
export declare function recordExtension(where: ItemExtendWhere, plan: {
|
|
175
|
+
itemId: string;
|
|
176
|
+
epochs: number;
|
|
177
|
+
}, digest: string): Promise<ExtendOutcome>;
|
|
178
|
+
/** The real chain reads. Imported only when no seam was supplied — it loads the storage SDK. */
|
|
179
|
+
export declare function defaultReads(network: string): Promise<ExtendReads>;
|
|
@@ -24,7 +24,7 @@ import { buildIndex, entryAt, fullPathOf, KIND_FILE, normalisePath } from "../dr
|
|
|
24
24
|
import { NmtsError } from "../errors.js";
|
|
25
25
|
import { budgetFacts, readBudget } from "../extend-budget.js";
|
|
26
26
|
import { daysLeftUntilEpoch, stageOf } from "../expiry.js";
|
|
27
|
-
import { asExtendPreview, chooseEpochs, headroom, soonestEnd, } from "../extend-plan.js";
|
|
27
|
+
import { asExtendPreview, chooseEpochs, headroom, nothingToExtend, readWindowOrRefuse, soonestEnd, refuseUnheld, } from "../extend-plan.js";
|
|
28
28
|
import { isRecord } from "../guards.js";
|
|
29
29
|
import { readFileList } from "../manifest.js";
|
|
30
30
|
import { activeWalletOf } from "../shared/lib/drive/manifest-settings.js";
|
|
@@ -53,28 +53,44 @@ export async function planExtension(input, target, seams) {
|
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
55
|
const path = fullPathOf(buildIndex(entries), entry);
|
|
56
|
+
// ⛔ WHICH WALLET PAYS comes out of the list this run already read, before the price is measured
|
|
57
|
+
// against a balance: the address below is the one that will sign.
|
|
58
|
+
const wallet = seams.wallet ?? activeWalletOf(settings);
|
|
59
|
+
const address = seams.payer?.address ?? (await walletAddress(input.code, wallet));
|
|
60
|
+
const reads = await (seams.readChain ?? defaultReads)(input.network);
|
|
61
|
+
const item = await planItemExtension(input, { id: entry.id, label: path }, {
|
|
62
|
+
reads,
|
|
63
|
+
window: await readWindowOrRefuse(reads, input.network),
|
|
64
|
+
address,
|
|
65
|
+
epochs: seams.epochs,
|
|
66
|
+
now: seams.now,
|
|
67
|
+
hints: seams.hints,
|
|
68
|
+
});
|
|
69
|
+
return { ...item, path, wallet, settings };
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Plan one file's extension from its id. The NMTS server says which blobs, the chain says how long
|
|
73
|
+
* each runs and who holds it, and the paying wallet's balances are read against the price.
|
|
74
|
+
*
|
|
75
|
+
* ⛔ NO NMTS KEY. This is what lets a business's server keep a user's files alive with the user's
|
|
76
|
+
* delegation token and its own wallet, while the key that opens the files stays on the user's
|
|
77
|
+
* device. The file is named by `item.label`: its path when a key opened the list, its id when
|
|
78
|
+
* nothing could.
|
|
79
|
+
*/
|
|
80
|
+
export async function planItemExtension(where, item, seams) {
|
|
81
|
+
const path = item.label;
|
|
56
82
|
// ⛔ THE SERVER SAYS WHICH BLOBS, AND NOTHING ELSE. Its `expiry_epoch` is client-reported and
|
|
57
83
|
// advisory; anything that spends money reads the chain's own answer below.
|
|
58
|
-
const preview = asExtendPreview(await request(
|
|
59
|
-
token:
|
|
84
|
+
const preview = asExtendPreview(await request(where.server, `/v1/items/${encodeURIComponent(item.id)}/extend-preview`, {
|
|
85
|
+
token: where.apiKey,
|
|
60
86
|
}));
|
|
61
87
|
if (preview.targets.length === 0) {
|
|
62
|
-
throw new NmtsError(`
|
|
88
|
+
throw new NmtsError(`EXTEND_NOTHING: nothing on "${path}" can be extended from here.`, {
|
|
63
89
|
exitCode: 4,
|
|
64
90
|
nextStep: nothingToExtend(preview),
|
|
65
91
|
});
|
|
66
92
|
}
|
|
67
|
-
const reads =
|
|
68
|
-
const window = await reads.readWindow();
|
|
69
|
-
if (window === null) {
|
|
70
|
-
// ⛔ Not "nothing needs extending". The two look identical from outside and mean opposite things.
|
|
71
|
-
throw new NmtsError(`The ${input.network} storage network could not be read.`, {
|
|
72
|
-
exitCode: 1,
|
|
73
|
-
nextStep: `Nothing was signed and nothing was charged. Which epoch the network is in, and how far ` +
|
|
74
|
-
`ahead it will sell, are facts only the chain has — this will not spend against a ` +
|
|
75
|
-
`guess. Try again, or name a different Sui node in NMTS_SUI_RPC.`,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
93
|
+
const { reads, window } = seams;
|
|
78
94
|
const clock = window.clock;
|
|
79
95
|
const objectIds = preview.targets.map((t) => t.objectId);
|
|
80
96
|
const leases = await reads.readLeases(objectIds);
|
|
@@ -87,7 +103,7 @@ export async function planExtension(input, target, seams) {
|
|
|
87
103
|
}
|
|
88
104
|
const stage = stageOf(clock, endEpoch, seams.now);
|
|
89
105
|
if (stage === "lapsed") {
|
|
90
|
-
throw new NmtsError(`
|
|
106
|
+
throw new NmtsError(`EXTEND_LAPSED: the storage term for "${path}" has already ended.`, {
|
|
91
107
|
exitCode: 4,
|
|
92
108
|
nextStep: seams.hints?.lapsed ??
|
|
93
109
|
`Nothing was signed and nothing was charged. A lease is extended before it ends — once it ` +
|
|
@@ -95,6 +111,9 @@ export async function planExtension(input, target, seams) {
|
|
|
95
111
|
`Fetching the file is what says whether they can still be read.`,
|
|
96
112
|
});
|
|
97
113
|
}
|
|
114
|
+
// ⛔ BEFORE THE PRICE AND BEFORE ANY SIGNATURE: a blob this wallet does not hold cannot be
|
|
115
|
+
// extended by it, and the chain would only say so after somebody had been asked to sign.
|
|
116
|
+
refuseUnheld(leases, seams.address, path);
|
|
98
117
|
const epochs = chooseEpochs(seams.epochs, headroom(leases, clock.current, window.maxAhead));
|
|
99
118
|
const newEndEpoch = endEpoch + epochs;
|
|
100
119
|
const before = daysLeftUntilEpoch(clock, endEpoch, seams.now);
|
|
@@ -104,16 +123,12 @@ export async function planExtension(input, target, seams) {
|
|
|
104
123
|
const frost = await reads.quote(leases, epochs);
|
|
105
124
|
const cohort = Math.max(0, ...preview.targets.map((t) => t.sharedItems));
|
|
106
125
|
const unreachable = preview.treasuryParts + preview.untrackedParts;
|
|
107
|
-
|
|
108
|
-
// against a balance: the address below is the one that will sign.
|
|
109
|
-
const wallet = seams.wallet ?? activeWalletOf(settings);
|
|
110
|
-
const address = seams.payer?.address ?? (await walletAddress(input.code, wallet));
|
|
111
|
-
const budget = await readBudget(reads, { address, objectIds, epochs, priceFrost: frost });
|
|
126
|
+
const budget = await readBudget(reads, { address: seams.address, objectIds, epochs, priceFrost: frost });
|
|
112
127
|
return {
|
|
113
128
|
facts: {
|
|
114
129
|
file: path,
|
|
115
|
-
itemId:
|
|
116
|
-
network:
|
|
130
|
+
itemId: item.id,
|
|
131
|
+
network: where.network,
|
|
117
132
|
epoch: clock.current,
|
|
118
133
|
endEpoch,
|
|
119
134
|
epochs,
|
|
@@ -128,14 +143,12 @@ export async function planExtension(input, target, seams) {
|
|
|
128
143
|
partsThatCannotBeExtended: unreachable,
|
|
129
144
|
...budgetFacts(budget),
|
|
130
145
|
},
|
|
131
|
-
itemId:
|
|
132
|
-
path,
|
|
146
|
+
itemId: item.id,
|
|
133
147
|
objectIds,
|
|
148
|
+
bytes: leases.reduce((sum, lease) => sum + lease.size, 0),
|
|
134
149
|
epochs,
|
|
135
|
-
wallet,
|
|
136
150
|
stage,
|
|
137
151
|
budget,
|
|
138
|
-
settings,
|
|
139
152
|
};
|
|
140
153
|
}
|
|
141
154
|
/**
|
|
@@ -154,10 +167,17 @@ export async function applyExtension(input, plan, seams = {}) {
|
|
|
154
167
|
epochs: plan.epochs,
|
|
155
168
|
});
|
|
156
169
|
seams.onSigned?.(digest);
|
|
157
|
-
|
|
158
|
-
|
|
170
|
+
return recordExtension(input, plan, digest);
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Tell the NMTS server about an extension that is already signed and paid for.
|
|
174
|
+
*
|
|
175
|
+
* From here the storage IS extended. Recording it is bookkeeping, and a failure to record must
|
|
176
|
+
* never be reported as a failure to extend — that reading invites a second run, which pays again.
|
|
177
|
+
*/
|
|
178
|
+
export async function recordExtension(where, plan, digest) {
|
|
159
179
|
try {
|
|
160
|
-
const recorded = await request(
|
|
180
|
+
const recorded = await request(where.server, `/v1/items/${encodeURIComponent(plan.itemId)}/extended`, { method: "POST", token: where.apiKey, body: { epochs: plan.epochs, tx_digest: digest } });
|
|
161
181
|
return { digest, recorded: true, replay: isRecord(recorded) && recorded["replay"] === true, notRecorded: null };
|
|
162
182
|
}
|
|
163
183
|
catch (error) {
|
|
@@ -169,21 +189,7 @@ export async function applyExtension(input, plan, seams = {}) {
|
|
|
169
189
|
};
|
|
170
190
|
}
|
|
171
191
|
}
|
|
172
|
-
/** Why a file has nothing to extend, said as the two different things it can be. */
|
|
173
|
-
export function nothingToExtend(preview) {
|
|
174
|
-
const parts = [];
|
|
175
|
-
if (preview.treasuryParts > 0) {
|
|
176
|
-
parts.push(`${preview.treasuryParts} part${preview.treasuryParts === 1 ? " is" : "s are"} on storage NMTS ` +
|
|
177
|
-
`paid for, which this account cannot extend`);
|
|
178
|
-
}
|
|
179
|
-
if (preview.untrackedParts > 0) {
|
|
180
|
-
parts.push(`${preview.untrackedParts} part${preview.untrackedParts === 1 ? " has" : "s have"} no ` +
|
|
181
|
-
`recorded storage object, so there is nothing to name on the chain`);
|
|
182
|
-
}
|
|
183
|
-
const why = parts.length === 0 ? "The server lists no storage object for it." : `${parts.join(", and ")}.`;
|
|
184
|
-
return `Nothing was signed and nothing was charged. ${why} Opening the account in a browser shows what it is stored on.`;
|
|
185
|
-
}
|
|
186
192
|
/** The real chain reads. Imported only when no seam was supplied — it loads the storage SDK. */
|
|
187
|
-
async function defaultReads(network) {
|
|
193
|
+
export async function defaultReads(network) {
|
|
188
194
|
return (await import("../extend-chain.js")).extendReads(network);
|
|
189
195
|
}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
export type { StorageHints } from "./storage-control/hints.ts";
|
|
2
2
|
export { formatBytes, listStorage, readOrRefuse } from "./storage-control/list.ts";
|
|
3
3
|
export type { ReadWalletStorage, StorageItem, StorageListing, StorageRead, StorageStatus, } from "./storage-control/list.ts";
|
|
4
|
-
export { applyExtension,
|
|
4
|
+
export { applyExtension, planExtension } from "./storage-control/extend.ts";
|
|
5
5
|
export type { ExtendApplySeams, ExtendFacts, ExtendInput, ExtendOutcome, ExtendPlan, ExtendPlanSeams, } from "./storage-control/extend.ts";
|
|
6
|
+
export { extendFor } from "./storage-control/extend-for.ts";
|
|
7
|
+
export type { ExtendForAsk, ExtendForFile, ExtendForInput, ExtendForResult, ExtendForSeams, } from "./storage-control/extend-for.ts";
|
|
6
8
|
export { planStorageOp, reshapeStorage } from "./storage-control/reshape.ts";
|
|
7
9
|
export type { ReshapeContext, ReshapeOutcome, ReshapeSeams, StorageOp, StorageOpAsk, StorageOpPlan, StorageOpResult, StorageOpReview, StorageShape, } from "./storage-control/reshape.ts";
|
|
8
10
|
export { buyStorage } from "./storage-control/buy.ts";
|
|
9
11
|
export type { BuyAsk, BuyOutcome, BuyReads, BuyReview, BuySeams } from "./storage-control/buy.ts";
|
|
10
12
|
export type { StorageOpShape, StorageOpsReads } from "./storage-control-chain.ts";
|
|
11
13
|
export type { StorageResource } from "./shared/lib/storage-control/chain.ts";
|
|
12
|
-
export { DEFAULT_EXTEND_EPOCHS } from "./extend-plan.ts";
|
|
14
|
+
export { DEFAULT_EXTEND_EPOCHS, nothingToExtend } from "./extend-plan.ts";
|
|
13
15
|
export type { ExtendReads, SignExtension } from "./extend-plan.ts";
|
|
14
16
|
export type { Budget } from "./extend-budget.ts";
|
|
15
17
|
export type { SignStorageOp } from "./wallet-sign-seams.ts";
|
package/dist/storage-control.js
CHANGED
|
@@ -19,7 +19,9 @@
|
|
|
19
19
|
// entry bundles what this exports, and the chain and the signer are loaded only when no seam was
|
|
20
20
|
// supplied.
|
|
21
21
|
export { formatBytes, listStorage, readOrRefuse } from "./storage-control/list.js";
|
|
22
|
-
export { applyExtension,
|
|
22
|
+
export { applyExtension, planExtension } from "./storage-control/extend.js";
|
|
23
|
+
// A business extending a user's files from its own wallet, with the user's token and no user key.
|
|
24
|
+
export { extendFor } from "./storage-control/extend-for.js";
|
|
23
25
|
export { planStorageOp, reshapeStorage } from "./storage-control/reshape.js";
|
|
24
26
|
export { buyStorage } from "./storage-control/buy.js";
|
|
25
|
-
export { DEFAULT_EXTEND_EPOCHS } from "./extend-plan.js";
|
|
27
|
+
export { DEFAULT_EXTEND_EPOCHS, nothingToExtend } from "./extend-plan.js";
|
package/docs/commands/wallet.md
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
# nmts wallet — the wallet your NMTS key derives
|
|
2
2
|
|
|
3
3
|
Commands: wallet
|
|
4
|
-
Tiers: wallet=none · wallet.use=low · wallet.send=high(wallet) · wallet.swap=high(wallet) · wallet.donate=high(donate) · wallet.storage.reshape=high(wallet) · wallet.storage.give=high(wallet) · wallet.hall=none · wallet.hall.set=medium
|
|
4
|
+
Tiers: wallet=none · wallet.use=low · wallet.send=high(wallet) · wallet.swap=high(wallet) · wallet.donate=high(donate) · wallet.storage.reshape=high(wallet) · wallet.storage.give=high(wallet) · wallet.storage.buy=high(wallet) · wallet.storage.extend=high(wallet) · wallet.hall=none · wallet.hall.set=medium
|
|
5
5
|
|
|
6
6
|
`wallet` shows the address and its SUI and WAL balances; `wallet address` derives the address
|
|
7
7
|
offline (`--qr` draws it, `--index N` derives another of this key's wallets); `wallet activity`
|
|
@@ -52,6 +52,18 @@ resources are the person's, in their wallet, and every step is their signature):
|
|
|
52
52
|
— what goes is size and remaining time; files stay sealed with this account's keys. Not undoable,
|
|
53
53
|
and NMTS cannot recall it. Scope `all`.
|
|
54
54
|
|
|
55
|
+
Two more spend WAL from the wallet, under the wallet unlock, and sign only with `--yes`:
|
|
56
|
+
|
|
57
|
+
- `wallet storage buy --size <bytes> [--epochs <n>]` buys a storage resource with no file in it,
|
|
58
|
+
for 2 epochs unless `--epochs` says otherwise.
|
|
59
|
+
- `wallet storage extend [<item-id>...] --delegation-file <file>` is for a business that paid for
|
|
60
|
+
its users' uploads. The storage of those files is held by the business's wallet, so that wallet
|
|
61
|
+
extends it. The file holds a delegation token for the user's account with `files_read` and
|
|
62
|
+
`storage_spend`; the user's key is not needed. Without item ids it takes every file of that user
|
|
63
|
+
that runs out soon; `--force` takes files not due yet. A file whose storage another wallet holds
|
|
64
|
+
is named and left as it is before anything is signed. Each file is one transaction, and the new
|
|
65
|
+
end is written into the user's account.
|
|
66
|
+
|
|
55
67
|
Each prints the review with the chain fee measured by a dry run, and a refusal the chain would
|
|
56
68
|
give ends the run there; nothing is signed without `--yes` on that run. Sizes are what a resource
|
|
57
69
|
holds after the network's encoding, as the listing prints them.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@needmoretruth/nmts-cli",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.59.0",
|
|
4
4
|
"description": "Command-line and MCP access to NMTS (NeedMoreTruthStorage, https://nmts.me): open-source, end-to-end encrypted storage on the Walrus network. No fee to NMTS; you pay the network from your own wallet. For people and for their agents.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"publishConfig": {
|
package/server.json
CHANGED
|
@@ -6,13 +6,13 @@
|
|
|
6
6
|
"url": "https://github.com/needmoretruth/nmts-cli",
|
|
7
7
|
"source": "github"
|
|
8
8
|
},
|
|
9
|
-
"version": "0.
|
|
9
|
+
"version": "0.59.0",
|
|
10
10
|
"packages": [
|
|
11
11
|
{
|
|
12
12
|
"registryType": "npm",
|
|
13
13
|
"registryBaseUrl": "https://registry.npmjs.org",
|
|
14
14
|
"identifier": "@needmoretruth/nmts-cli",
|
|
15
|
-
"version": "0.
|
|
15
|
+
"version": "0.59.0",
|
|
16
16
|
"transport": {
|
|
17
17
|
"type": "stdio"
|
|
18
18
|
},
|