@needmoretruth/nmts-cli 0.57.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 +26 -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-buy.d.ts +22 -0
- package/dist/commands/wallet-storage-buy.js +111 -0
- 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 +5 -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 +8 -3
- package/dist/help.js +5 -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 +7 -2
- package/dist/portable.js +7 -2
- 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/sponsor-check.d.ts +56 -0
- package/dist/sponsor-check.js +270 -0
- package/dist/sponsor-facts.d.ts +10 -0
- package/dist/sponsor-facts.js +75 -0
- package/dist/storage-control/buy.d.ts +65 -0
- package/dist/storage-control/buy.js +145 -0
- 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-chain.d.ts +7 -0
- package/dist/storage-control-chain.js +19 -1
- package/dist/storage-control.d.ts +6 -2
- package/dist/storage-control.js +5 -2
- package/dist/upload-quote.d.ts +93 -0
- package/dist/upload-quote.js +135 -0
- package/dist/wallet-sign.d.ts +24 -0
- package/dist/wallet-sign.js +31 -1
- package/docs/commands/wallet.md +13 -1
- package/package.json +1 -1
- package/server.json +2 -2
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
// Buying a storage resource on its own — a size and a term, with no file — decided, priced, signed
|
|
2
|
+
// and handed back rather than printed.
|
|
3
|
+
//
|
|
4
|
+
// ⛔ WHY (spec 2026-09-20 Deponejo · gap B2 · plan 2026-11-10 §1.2). NMTS bought storage only inside
|
|
5
|
+
// an upload. A business that keeps one large resource and cuts a piece off it for each customer
|
|
6
|
+
// (`split` → `transfer`) had to buy that resource with the Walrus client — a reason to use Walrus
|
|
7
|
+
// directly. This sits beside split, merge and transfer, with the same two callers: `nmts wallet
|
|
8
|
+
// storage buy` and the SDK's `buyStorage`.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ THE ORDER IS THE SAFETY, the one `reshape.ts` keeps: ① the term is judged against what the
|
|
11
|
+
// network sells ② the price is read fresh ③ both balances are read and a wallet known to be short
|
|
12
|
+
// is refused with both numbers ④ the exact transaction is dry-run, and a refusal there ends the
|
|
13
|
+
// run ⑤ the review is handed out ⑥ `dryRun` stops here ⑦ `agree` is the gate ⑧ only then the
|
|
14
|
+
// signature, and the resource the chain created is read back by its type.
|
|
15
|
+
//
|
|
16
|
+
// ⚠ THE SIZE IS A RESOURCE'S SIZE — the encoded bytes a storage listing shows and `split` cuts by —
|
|
17
|
+
// not a file's. A file of N bytes needs more once encoded; an upload's dry run says how much.
|
|
18
|
+
//
|
|
19
|
+
// ⚠ NOTHING HERE WRITES TO A STREAM, ASKS ANYBODY ANYTHING OR REACHES FOR `node:`.
|
|
20
|
+
import { NmtsError } from "../errors.js";
|
|
21
|
+
import { chooseUploadEpochs } from "../upload-wallet-plan.js";
|
|
22
|
+
import { coinAmount } from "../wallet.js";
|
|
23
|
+
/** A size worth buying: a positive whole number of bytes. */
|
|
24
|
+
function requireSize(sizeBytes) {
|
|
25
|
+
if (!Number.isSafeInteger(sizeBytes) || sizeBytes <= 0) {
|
|
26
|
+
throw new NmtsError(`A storage resource's size is a positive whole number of bytes: ${String(sizeBytes)}.`, {
|
|
27
|
+
exitCode: 2,
|
|
28
|
+
nextStep: "Nothing was signed. The size is encoded bytes, the unit a storage listing shows.",
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
return sizeBytes;
|
|
32
|
+
}
|
|
33
|
+
/** Read, judge, price and — unless somebody stops it — sign one purchase of a storage resource. */
|
|
34
|
+
export async function buyStorage(context, ask, seams = {}) {
|
|
35
|
+
const sizeBytes = requireSize(ask.sizeBytes);
|
|
36
|
+
const reads = seams.reads === undefined ? await defaultBuyReads(context.network) : await seams.reads(context.network);
|
|
37
|
+
// ① The term.
|
|
38
|
+
const window = await reads.readWindow();
|
|
39
|
+
if (window === null) {
|
|
40
|
+
throw new NmtsError(`The ${context.network} storage network could not be read.`, {
|
|
41
|
+
exitCode: 1,
|
|
42
|
+
nextStep: "Nothing was signed. Which epoch it is and how far ahead the network sells are facts only the chain has.",
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
const epochs = chooseUploadEpochs(ask.epochs, window);
|
|
46
|
+
// ② The price, fresh. ③ The wallet.
|
|
47
|
+
const [priceFrost, purse, walrusPackageId] = await Promise.all([
|
|
48
|
+
reads.price(sizeBytes, epochs),
|
|
49
|
+
reads.readWallet(context.address),
|
|
50
|
+
reads.walrusPackageId(),
|
|
51
|
+
]);
|
|
52
|
+
const shape = {
|
|
53
|
+
kind: "buy",
|
|
54
|
+
sizeBytes,
|
|
55
|
+
epochs,
|
|
56
|
+
priceFrost,
|
|
57
|
+
walType: reads.walType(),
|
|
58
|
+
systemObjectId: reads.systemObjectId(),
|
|
59
|
+
};
|
|
60
|
+
const walFrost = purse.wal.read ? purse.wal.baseUnits : null;
|
|
61
|
+
const suiMist = purse.sui.read ? purse.sui.baseUnits : null;
|
|
62
|
+
// ④ The dry run — skipped when the wallet is known to hold too little WAL, since the chain would
|
|
63
|
+
// only say the same thing less clearly.
|
|
64
|
+
const short = walFrost !== null && walFrost < priceFrost;
|
|
65
|
+
const verdict = short ? { feeMist: null, refusal: null } : await reads.dryRun(shape, context.address);
|
|
66
|
+
if (verdict.refusal !== null) {
|
|
67
|
+
throw new NmtsError(`The chain would refuse this: ${verdict.refusal}`, {
|
|
68
|
+
exitCode: 4,
|
|
69
|
+
nextStep: "Nothing was signed and no fee was spent.",
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
let shortfall = null;
|
|
73
|
+
if (short) {
|
|
74
|
+
shortfall = `The wallet holds ${coinAmount(walFrost)} WAL and this storage costs ${coinAmount(priceFrost)} WAL.`;
|
|
75
|
+
}
|
|
76
|
+
else if (suiMist !== null && verdict.feeMist !== null && suiMist < verdict.feeMist) {
|
|
77
|
+
shortfall = `The wallet holds ${coinAmount(suiMist)} SUI and the chain fee is about ${coinAmount(verdict.feeMist)} SUI.`;
|
|
78
|
+
}
|
|
79
|
+
// ⑤ The review.
|
|
80
|
+
const review = {
|
|
81
|
+
network: context.network,
|
|
82
|
+
address: context.address,
|
|
83
|
+
sizeBytes,
|
|
84
|
+
epochs,
|
|
85
|
+
startEpoch: window.clock.current,
|
|
86
|
+
endEpoch: window.clock.current + epochs,
|
|
87
|
+
priceFrost,
|
|
88
|
+
feeMist: verdict.feeMist,
|
|
89
|
+
walFrost,
|
|
90
|
+
suiMist,
|
|
91
|
+
shortfall,
|
|
92
|
+
};
|
|
93
|
+
seams.onReview?.(review);
|
|
94
|
+
// ⑥ Stopped here, with nothing signed.
|
|
95
|
+
if (seams.dryRun === true)
|
|
96
|
+
return { kind: "review", review };
|
|
97
|
+
if (shortfall !== null) {
|
|
98
|
+
throw new NmtsError(shortfall, {
|
|
99
|
+
exitCode: 4,
|
|
100
|
+
nextStep: `Nothing was signed and nothing was spent. Send what is missing to ${context.address} and ask again.`,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
// ⑦ The gate.
|
|
104
|
+
seams.agree?.(review);
|
|
105
|
+
// ⑧ The signature, and the resource it made.
|
|
106
|
+
const sign = seams.sign ?? (await import("../wallet-sign.js")).signStorageOp;
|
|
107
|
+
const digest = await sign({ network: context.network, code: context.code, wallet: context.wallet, shape, walrusPackageId });
|
|
108
|
+
const objectId = await reads.createdResource(digest).catch(() => null);
|
|
109
|
+
return { kind: "done", review, digest, objectId };
|
|
110
|
+
}
|
|
111
|
+
/** The live reads, loaded on first use so nothing here imports a chain client by itself. */
|
|
112
|
+
async function defaultBuyReads(network) {
|
|
113
|
+
const [chain, { walrusClient }, { freshWalrus }, { readBalances, walCoinType }, { chainReader }, { packageConfig }, execute] = await Promise.all([
|
|
114
|
+
import("../storage-control-chain.js"),
|
|
115
|
+
import("../extend-chain.js"),
|
|
116
|
+
import("../walrus-fresh.js"),
|
|
117
|
+
import("../wallet.js"),
|
|
118
|
+
import("../wallet-chain.js"),
|
|
119
|
+
import("../upload-wallet-chain.js"),
|
|
120
|
+
import("../sui-execute.js"),
|
|
121
|
+
]);
|
|
122
|
+
const client = walrusClient(network);
|
|
123
|
+
const ops = chain.storageOpsReads(network);
|
|
124
|
+
return {
|
|
125
|
+
readWindow: async () => (await import("../extend-chain.js")).extendReads(network).readWindow(),
|
|
126
|
+
async price(sizeBytes, epochs) {
|
|
127
|
+
// ⚠ THE PROTOCOL'S UNIT IS ONE MEBIBYTE OF ENCODED SIZE, rounded up, and reserving space
|
|
128
|
+
// charges storage alone. If this arithmetic ever drifted from the contract's, the dry run
|
|
129
|
+
// below aborts (`destroy_zero` or `split`) and nothing is signed.
|
|
130
|
+
const state = await freshWalrus(client).systemState();
|
|
131
|
+
const units = BigInt(Math.ceil(sizeBytes / 2 ** 20));
|
|
132
|
+
return units * BigInt(state.storage_price_per_unit_size) * BigInt(epochs);
|
|
133
|
+
},
|
|
134
|
+
readWallet: (address) => readBalances(chainReader(network, address), walCoinType(network)),
|
|
135
|
+
walrusPackageId: () => ops.walrusPackageId(),
|
|
136
|
+
walType: () => walCoinType(network),
|
|
137
|
+
systemObjectId: () => packageConfig(network).systemObjectId,
|
|
138
|
+
dryRun: (shape, sender) => ops.dryRun(shape, sender),
|
|
139
|
+
async createdResource(digest) {
|
|
140
|
+
const answer = await client.core.getTransaction({ digest, include: execute.EXECUTE_INCLUDE });
|
|
141
|
+
const found = execute.readAnswer(answer).created.find((o) => o.type?.endsWith("::storage_resource::Storage") === true);
|
|
142
|
+
return found?.objectId ?? null;
|
|
143
|
+
},
|
|
144
|
+
};
|
|
145
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { type ExtendReads } from "../extend-plan.ts";
|
|
2
|
+
import type { StorageHints } from "./hints.ts";
|
|
3
|
+
/** Where to talk and with what. ⛔ No NMTS key: the user's is not here and none is needed. */
|
|
4
|
+
export interface ExtendForInput {
|
|
5
|
+
server: string;
|
|
6
|
+
network: string;
|
|
7
|
+
/** The user's delegation token. It must carry `files_read` and `storage_spend`. */
|
|
8
|
+
token: string;
|
|
9
|
+
/** The address that holds the storage and pays. Every blob must be held by it. */
|
|
10
|
+
payer: string;
|
|
11
|
+
}
|
|
12
|
+
/** What to extend and how. */
|
|
13
|
+
export interface ExtendForAsk {
|
|
14
|
+
/** Item ids. Absent = every file the user's account holds that is not in the trash. */
|
|
15
|
+
items?: readonly string[] | undefined;
|
|
16
|
+
/** Epochs to add to each file. Default `DEFAULT_EXTEND_EPOCHS`. */
|
|
17
|
+
epochs?: string | number | undefined;
|
|
18
|
+
/** Extend files that are not near their end yet too. Without it they are skipped as not due. */
|
|
19
|
+
force?: boolean | undefined;
|
|
20
|
+
/** Price every file and sign nothing. */
|
|
21
|
+
dryRun?: boolean | undefined;
|
|
22
|
+
}
|
|
23
|
+
/** The chain and the signature. ⚠ Seams, not options. */
|
|
24
|
+
export interface ExtendForSeams {
|
|
25
|
+
readChain?: ((network: string) => Promise<ExtendReads> | ExtendReads) | undefined;
|
|
26
|
+
/** Signs one file's extension from the paying wallet and answers the transaction digest. */
|
|
27
|
+
sign?: ((input: {
|
|
28
|
+
objectIds: readonly string[];
|
|
29
|
+
epochs: number;
|
|
30
|
+
}) => Promise<string>) | undefined;
|
|
31
|
+
/** Told the instant a file's signature exists, before the server is asked to write it down. */
|
|
32
|
+
onSigned?: ((itemId: string, digest: string) => void) | undefined;
|
|
33
|
+
now: number;
|
|
34
|
+
hints?: StorageHints | undefined;
|
|
35
|
+
}
|
|
36
|
+
/** What happened to one file. */
|
|
37
|
+
export interface ExtendForFile {
|
|
38
|
+
itemId: string;
|
|
39
|
+
/** `extended` = signed and paid; `priced` = a dry run's price; `skipped` = nothing signed. */
|
|
40
|
+
outcome: "extended" | "priced" | "skipped";
|
|
41
|
+
/** Why it was skipped, as a code a program can match (`EXTEND_NOT_DUE`, `EXTEND_NOT_OWNED`, …). */
|
|
42
|
+
code: string | null;
|
|
43
|
+
/** Why, in words. Null when nothing needs saying. */
|
|
44
|
+
reason: string | null;
|
|
45
|
+
epochs: number | null;
|
|
46
|
+
/** The blobs' sizes added up — what a ledger that sells size × time charges by. */
|
|
47
|
+
bytes: number | null;
|
|
48
|
+
/** The chain's end of the soonest blob before, and after this extension. */
|
|
49
|
+
endEpoch: number | null;
|
|
50
|
+
newEndEpoch: number | null;
|
|
51
|
+
/** Price in FROST and the fee measured in MIST, as strings. */
|
|
52
|
+
wal: string | null;
|
|
53
|
+
sui: string | null;
|
|
54
|
+
digest: string | null;
|
|
55
|
+
}
|
|
56
|
+
/** The whole run. */
|
|
57
|
+
export interface ExtendForResult {
|
|
58
|
+
dryRun: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* The paying wallet as the first plan read it, in FROST and MIST as decimal strings — the units of
|
|
61
|
+
* every amount here. ⛔ NULL IS NOT ZERO: the chain would not say, or no file was priced.
|
|
62
|
+
*/
|
|
63
|
+
wallet: {
|
|
64
|
+
address: string;
|
|
65
|
+
wal: string | null;
|
|
66
|
+
sui: string | null;
|
|
67
|
+
};
|
|
68
|
+
files: ExtendForFile[];
|
|
69
|
+
/** What the extended (or, on a dry run, priced) files cost together, in FROST and MIST. */
|
|
70
|
+
wal: string;
|
|
71
|
+
sui: string;
|
|
72
|
+
/** Why the run stopped before the end, or null when it reached every file. */
|
|
73
|
+
stopped: {
|
|
74
|
+
code: string;
|
|
75
|
+
reason: string;
|
|
76
|
+
} | null;
|
|
77
|
+
}
|
|
78
|
+
/**
|
|
79
|
+
* Extend a user's files from the paying wallet. Files not due, held by another wallet, already at the
|
|
80
|
+
* network's ceiling or past their end are skipped and said so; the rest are bought one by one.
|
|
81
|
+
*
|
|
82
|
+
* ⛔ THIS SIGNS unless `dryRun` is set, and nothing it buys comes back.
|
|
83
|
+
*/
|
|
84
|
+
export declare function extendFor(input: ExtendForInput, ask: ExtendForAsk, seams: ExtendForSeams): Promise<ExtendForResult>;
|
|
@@ -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>;
|