@kynesyslabs/demosdk 2.12.2 → 4.0.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.
Files changed (81) hide show
  1. package/build/bridge/nativeBridge.js +1 -0
  2. package/build/bridge/nativeBridge.js.map +1 -1
  3. package/build/bridge/nativeBridgeTypes.d.ts +4 -3
  4. package/build/bridge/nativeBridgeTypes.js.map +1 -1
  5. package/build/d402/client/types.d.ts +6 -3
  6. package/build/d402/server/D402Server.d.ts +25 -0
  7. package/build/d402/server/D402Server.js +63 -2
  8. package/build/d402/server/D402Server.js.map +1 -1
  9. package/build/d402/server/middleware.d.ts +7 -4
  10. package/build/d402/server/middleware.js.map +1 -1
  11. package/build/d402/server/types.d.ts +15 -7
  12. package/build/denomination/conversion.test.js +1 -1
  13. package/build/denomination/conversion.test.js.map +1 -1
  14. package/build/denomination/index.d.ts +3 -0
  15. package/build/denomination/index.js +6 -0
  16. package/build/denomination/index.js.map +1 -1
  17. package/build/denomination/networkInfo.d.ts +69 -0
  18. package/build/denomination/networkInfo.js +38 -0
  19. package/build/denomination/networkInfo.js.map +1 -0
  20. package/build/denomination/networkInfo.test.d.ts +1 -0
  21. package/build/denomination/networkInfo.test.js +28 -0
  22. package/build/denomination/networkInfo.test.js.map +1 -0
  23. package/build/denomination/roundTripHash.test.d.ts +1 -0
  24. package/build/denomination/roundTripHash.test.js +231 -0
  25. package/build/denomination/roundTripHash.test.js.map +1 -0
  26. package/build/denomination/serializerGate.d.ts +46 -0
  27. package/build/denomination/serializerGate.js +283 -0
  28. package/build/denomination/serializerGate.js.map +1 -0
  29. package/build/denomination/serializerGate.test.d.ts +1 -0
  30. package/build/denomination/serializerGate.test.js +255 -0
  31. package/build/denomination/serializerGate.test.js.map +1 -0
  32. package/build/escrow/EscrowTransaction.d.ts +36 -5
  33. package/build/escrow/EscrowTransaction.js +91 -10
  34. package/build/escrow/EscrowTransaction.js.map +1 -1
  35. package/build/ipfs/IPFSOperations.d.ts +37 -10
  36. package/build/ipfs/IPFSOperations.js +38 -8
  37. package/build/ipfs/IPFSOperations.js.map +1 -1
  38. package/build/storage/StorageProgram.d.ts +16 -8
  39. package/build/storage/StorageProgram.js +16 -8
  40. package/build/storage/StorageProgram.js.map +1 -1
  41. package/build/tlsnotary/TLSNotaryService.d.ts +19 -8
  42. package/build/tlsnotary/TLSNotaryService.js +22 -7
  43. package/build/tlsnotary/TLSNotaryService.js.map +1 -1
  44. package/build/tlsnotary/helpers.d.ts +11 -5
  45. package/build/tlsnotary/helpers.js +17 -5
  46. package/build/tlsnotary/helpers.js.map +1 -1
  47. package/build/types/blockchain/CustomCharges.d.ts +28 -16
  48. package/build/types/blockchain/CustomCharges.js +15 -5
  49. package/build/types/blockchain/CustomCharges.js.map +1 -1
  50. package/build/types/blockchain/GCREdit.d.ts +26 -2
  51. package/build/types/blockchain/NetworkParameters.d.ts +33 -0
  52. package/build/types/blockchain/Transaction.d.ts +14 -1
  53. package/build/types/blockchain/Transaction.js.map +1 -1
  54. package/build/types/blockchain/TransactionSubtypes/D402PaymentTransaction.d.ts +8 -3
  55. package/build/types/blockchain/TransactionSubtypes/StorageProgramTransaction.d.ts +6 -5
  56. package/build/types/blockchain/TransactionSubtypes/StorageProgramTransaction.js +15 -3
  57. package/build/types/blockchain/TransactionSubtypes/StorageProgramTransaction.js.map +1 -1
  58. package/build/types/blockchain/TxFee.d.ts +33 -3
  59. package/build/types/blockchain/address.d.ts +23 -0
  60. package/build/types/blockchain/rawTransaction.d.ts +20 -4
  61. package/build/types/blockchain/statusNative.d.ts +9 -1
  62. package/build/types/bridge/bridgeTradePayload.d.ts +1 -1
  63. package/build/types/gls/StateChange.d.ts +29 -3
  64. package/build/types/native/INativePayload.d.ts +6 -1
  65. package/build/wallet/Wallet.d.ts +27 -1
  66. package/build/wallet/Wallet.js +30 -17
  67. package/build/wallet/Wallet.js.map +1 -1
  68. package/build/websdk/DemosTokens.js +2 -0
  69. package/build/websdk/DemosTokens.js.map +1 -1
  70. package/build/websdk/DemosTransactions.d.ts +30 -4
  71. package/build/websdk/DemosTransactions.js +88 -9
  72. package/build/websdk/DemosTransactions.js.map +1 -1
  73. package/build/websdk/GCRGeneration.d.ts +10 -2
  74. package/build/websdk/GCRGeneration.js +30 -14
  75. package/build/websdk/GCRGeneration.js.map +1 -1
  76. package/build/websdk/demosclass.d.ts +152 -23
  77. package/build/websdk/demosclass.js +308 -38
  78. package/build/websdk/demosclass.js.map +1 -1
  79. package/build/websdk/utils/skeletons.js +1 -0
  80. package/build/websdk/utils/skeletons.js.map +1 -1
  81. package/package.json +1 -1
@@ -8,6 +8,7 @@ import type { NetworkParameters, NetworkUpgradeProposal, ProposalVoteInfo } from
8
8
  import { RPCRequest, RPCResponse, RPCResponseWithValidityData } from "../types/communication/rpc";
9
9
  import { IKeyPair } from "./types/KeyPair";
10
10
  import { UnifiedCrypto } from "../encryption/unifiedCrypto";
11
+ import { type NetworkInfo } from "../denomination/networkInfo";
11
12
  import { TweetSimplified } from "../types";
12
13
  import { GetDiscordMessageResult } from "../types/web2/discord";
13
14
  import type { TLSNotary, TLSNotaryConfig } from "../tlsnotary";
@@ -28,6 +29,18 @@ export declare class Demos {
28
29
  private _cachedNetworkParameters;
29
30
  private _cachedNetworkParametersAt;
30
31
  private _cachedNetworkParametersRpcUrl;
32
+ private _cachedNetworkInfo;
33
+ private _cachedNetworkInfoRpcUrl;
34
+ private _cachedNetworkInfoFailed;
35
+ private _cachedNetworkInfoFailedAt;
36
+ private _cachedNetworkInfoWarned;
37
+ /**
38
+ * TTL for the failed-detection memo. After this elapses we re-attempt
39
+ * `getNetworkInfo` so a transient outage doesn't poison the instance
40
+ * forever. The warn-once flag is sticky across retries — operators
41
+ * still see the warning exactly once per instance lifetime.
42
+ */
43
+ private static readonly _NETWORK_INFO_FAILURE_TTL_MS;
31
44
  /** Connection status of the RPC URL */
32
45
  connected: boolean;
33
46
  dual_sign: boolean;
@@ -95,21 +108,50 @@ export declare class Demos {
95
108
  /**
96
109
  * Create a signed DEMOS transaction to send native tokens to a given address.
97
110
  *
98
- * @param to - The reciever
99
- * @param amount - The amount in DEM
111
+ * P4 dual-input:
112
+ * - `bigint` (preferred, post-v3): amount in OS (smallest unit;
113
+ * 1 DEM = 10^9 OS). Use `denomination.demToOs(...)` to convert
114
+ * a human-readable DEM input.
115
+ * - `number` (deprecated, v2 callers): amount in DEM. Auto-converted
116
+ * to OS internally via `OS_PER_DEM`. Will be removed in v4.
100
117
  *
118
+ * Sub-DEM precision is rejected with `SubDemPrecisionError` when
119
+ * the connected node is pre-fork — its legacy DEM-`number` wire
120
+ * cannot carry < 1 DEM and silent truncation is unacceptable. The
121
+ * caller can either round to a whole DEM or upgrade the target
122
+ * node.
123
+ *
124
+ * @example
125
+ * ```ts
126
+ * import { denomination } from "@kynesyslabs/demosdk"
127
+ * await demos.pay("0x...", denomination.demToOs(100)) // 100 DEM
128
+ * await demos.pay("0x...", 100_000_000_000n) // raw OS
129
+ * await demos.pay("0x...", 100) // legacy DEM number (deprecated)
130
+ * ```
131
+ *
132
+ * @param to - The receiver address (0x-prefixed hex).
133
+ * @param amount - DEM `number` (legacy) or OS `bigint` (preferred).
101
134
  * @returns The signed transaction.
102
135
  */
103
- pay(to: string, amount: number): Promise<Transaction>;
136
+ pay(to: string, amount: number | bigint): Promise<Transaction>;
104
137
  /**
105
138
  * Create a signed DEMOS transaction to send native tokens to a given address.
106
139
  *
107
- * @param to - The reciever
108
- * @param amount - The amount in DEM
140
+ * Alias of {@link pay}. Same dual-input semantics — `bigint` is the
141
+ * preferred OS shape; `number` is the deprecated legacy DEM shape.
142
+ *
143
+ * @example
144
+ * ```ts
145
+ * import { denomination } from "@kynesyslabs/demosdk"
146
+ * await demos.transfer("0x...", denomination.demToOs("1.5")) // 1.5 DEM
147
+ * await demos.transfer("0x...", 1_500_000_000n) // raw OS
148
+ * ```
109
149
  *
150
+ * @param to - The receiver address (0x-prefixed hex).
151
+ * @param amount - DEM `number` (legacy) or OS `bigint` (preferred).
110
152
  * @returns The signed transaction.
111
153
  */
112
- transfer(to: string, amount: number): Promise<Transaction>;
154
+ transfer(to: string, amount: number | bigint): Promise<Transaction>;
113
155
  /**
114
156
  * Create a signed DEMOS transaction to store binary data on the blockchain.
115
157
  * Data is stored in the sender's account.
@@ -212,17 +254,40 @@ export declare class Demos {
212
254
  /**
213
255
  * @private
214
256
  * Calculates and applies the gas fee for a transaction (SDK-level fallback).
215
- * NOTE: We infer the fee by analyzing the generated GCR (Gas Consumption Record) edits:
216
- * - Sum all "balance" edits with operation "remove" for the sender
217
- * - Subtract the declared transaction `amount`
218
- * The remainder is treated as the network fee. If a fee already exists on the tx, we only raise
219
- * `network_fee` if the newly inferred fee is higher (prevents double-charging on re-sign).
220
- * This is an interim approach; the preferred design is for the node to return fees explicitly.
257
+ *
258
+ * NOTE: We infer the fee by analyzing the generated GCR (Gas Consumption
259
+ * Record) edits:
260
+ * - Sum all "balance" edits with operation "remove" for the sender.
261
+ * - Subtract the declared transaction `amount`.
262
+ * The remainder is treated as the network fee. If a fee already exists on
263
+ * the tx, we only raise `network_fee` if the newly inferred fee is higher
264
+ * (prevents double-charging on re-sign). This is an interim approach; the
265
+ * preferred design is for the node to return fees explicitly.
266
+ *
267
+ * P4: arithmetic uses `bigint` internally so OS-magnitude amounts
268
+ * (~10^19 OS for whole-network supply) don't overflow JS `number`'s
269
+ * 2^53 safe-integer ceiling. The function tolerates legacy `number`
270
+ * (DEM) and post-fork `string` (OS) inputs in any field, normalises
271
+ * to OS-bigint via `_coerceWireAmountToOs`, then writes the
272
+ * derived fee back in DEM-`number` (legacy wire) — the
273
+ * serializerGate (P4 commit 2) re-encodes to OS at hash time when
274
+ * the connected node is post-fork.
221
275
  *
222
276
  * @param raw_tx - The transaction for which to calculate the fee.
223
277
  * @returns The updated transaction with the fee applied.
224
278
  */
225
279
  private _calculateAndApplyGasFee;
280
+ /**
281
+ * Coerce a wire-format amount/fee value (legacy DEM `number`,
282
+ * post-fork OS decimal `string`, or in-flight `bigint`) to an OS
283
+ * `bigint`. Used by `_calculateAndApplyGasFee` and other internal
284
+ * arithmetic paths to stay in OS-bigint regardless of which wire
285
+ * shape the caller produced. Mirrors the node's `toOsBigint` helper
286
+ * in `forks/serializerGate.ts`.
287
+ *
288
+ * @internal
289
+ */
290
+ static _coerceWireAmountToOs(value: number | string | bigint): bigint;
226
291
  /**
227
292
  * Single transport wrapper for axios.post against the Demos RPC node.
228
293
  *
@@ -324,6 +389,18 @@ export declare class Demos {
324
389
  /**
325
390
  * Get information about an address.
326
391
  *
392
+ * P4: `balance` is `bigint` in **OS** (smallest unit, 1 DEM = 10^9 OS).
393
+ * Use `denomination.osToDem(info.balance)` for display.
394
+ *
395
+ * @example
396
+ * ```ts
397
+ * import { denomination } from "@kynesyslabs/demosdk"
398
+ * const info = await demos.getAddressInfo("0x...")
399
+ * if (info) {
400
+ * console.log("balance:", denomination.osToDem(info.balance), "DEM")
401
+ * }
402
+ * ```
403
+ *
327
404
  * @param address - The address
328
405
  */
329
406
  getAddressInfo(address: string): Promise<AddressInfo | null>;
@@ -364,6 +441,50 @@ export declare class Demos {
364
441
  * the response shape is invalid.
365
442
  */
366
443
  private _getNetworkParametersCached;
444
+ /**
445
+ * Fetches the connected node's per-fork activation status.
446
+ *
447
+ * Mirrors the node's `getNetworkInfo` `nodeCall`
448
+ * (`libs/network/handlers/forkHandlers.ts`). The response carries
449
+ * activation height, current chain head, and the `activated` boolean
450
+ * for every known fork.
451
+ *
452
+ * Caches the result on this `Demos` instance for the instance's
453
+ * lifetime (no TTL). To re-fetch after a node upgrade, construct a
454
+ * fresh `Demos` instance.
455
+ *
456
+ * On RPC failure (404, malformed response, network error), this
457
+ * method returns `null` and the SDK assumes pre-fork wire format.
458
+ * A `console.warn` is emitted exactly once per `Demos` instance
459
+ * recommending the operator upgrade the target node.
460
+ *
461
+ * @returns The fork-status payload, or `null` if the RPC failed.
462
+ */
463
+ getNetworkInfo(): Promise<NetworkInfo | null>;
464
+ /**
465
+ * @internal
466
+ * Cached fork-status accessor. Returns `osDenomination` activation
467
+ * status as a boolean. `false` is the safe default (legacy wire
468
+ * format) when the node is unreachable or pre-P3c.
469
+ */
470
+ private _isPostForkCached;
471
+ /**
472
+ * @internal
473
+ * Reset the cached fork status. Intended for tests; production code
474
+ * should construct a fresh `Demos` instance instead.
475
+ */
476
+ _resetForkStatusCacheForTesting(): void;
477
+ /**
478
+ * @internal
479
+ * Sub-DEM precision guard. Run by every public-API entry point that
480
+ * takes a user-supplied OS amount before tx construction. Throws
481
+ * `SubDemPrecisionError` when the connected node is pre-fork and
482
+ * the amount carries sub-DEM precision (would silently truncate on
483
+ * the legacy DEM-`number` wire).
484
+ *
485
+ * @param amountOs - The OS amount the caller is sending.
486
+ */
487
+ private _assertAmountAcceptableOnTargetNode;
367
488
  /**
368
489
  * Lists currently-open proposals (pending tally or activating after
369
490
  * approval). Rejected/active historical proposals are not included.
@@ -431,9 +552,12 @@ export declare class Demos {
431
552
  /**
432
553
  * Get a cost quote for an IPFS operation without submitting a transaction.
433
554
  *
434
- * Use this to estimate costs and populate custom_charges before signing.
435
- * The returned cost should be used as max_cost_dem in the transaction's
436
- * custom_charges field.
555
+ * Use this to estimate costs and populate `custom_charges` before signing.
556
+ * Pipe the response through `IPFSOperations.quoteToCustomCharges`
557
+ * (or `createCustomCharges`) to obtain a `max_cost_os` decimal-string
558
+ * suitable for the transaction's `custom_charges.ipfs.max_cost_os`
559
+ * field. The helpers handle both pre-fork (`cost_dem`) and
560
+ * post-fork (`cost_os`) node response shapes.
437
561
  *
438
562
  * @param fileSizeBytes - Size of file in bytes
439
563
  * @param operation - IPFS operation type ('IPFS_ADD', 'IPFS_PIN', or 'IPFS_UNPIN')
@@ -444,7 +568,6 @@ export declare class Demos {
444
568
  * ```typescript
445
569
  * // Get quote for add operation
446
570
  * const quote = await demos.ipfs.quote(content.length, 'IPFS_ADD')
447
- * console.log(`Cost: ${quote.cost_dem} DEM`)
448
571
  *
449
572
  * // Use quote to build transaction with cost control
450
573
  * const payload = IPFSOperations.createAddPayload(content, {
@@ -453,7 +576,10 @@ export declare class Demos {
453
576
  * ```
454
577
  */
455
578
  quote: (fileSizeBytes: number, operation?: "IPFS_ADD" | "IPFS_PIN" | "IPFS_UNPIN", durationBlocks?: number) => Promise<{
456
- cost_dem: string;
579
+ /** Pre-fork node: cost in DEM as decimal string. */
580
+ cost_dem?: string;
581
+ /** Post-fork node: cost in OS as decimal string. */
582
+ cost_os?: string;
457
583
  file_size_bytes: number;
458
584
  is_genesis: boolean;
459
585
  breakdown: {
@@ -478,8 +604,9 @@ export declare class Demos {
478
604
  sign: (raw_tx: Transaction) => Promise<Transaction>;
479
605
  empty: () => Transaction;
480
606
  prepare: (data?: any) => Promise<Transaction>;
481
- pay(to: string, amount: number, demos: Demos): Promise<Transaction>;
482
- transfer(to: string, amount: number, demos: Demos): Promise<Transaction>;
607
+ pay(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
608
+ _demNumberToOsBigint(amountDem: number): bigint;
609
+ transfer(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
483
610
  signWithAlgorithm: (data: string, keypair: IKeyPair, options: {
484
611
  algorithm: SigningAlgorithm;
485
612
  }) => Promise<{
@@ -515,8 +642,9 @@ export declare class Demos {
515
642
  DemosTransactions: {
516
643
  empty: () => Transaction;
517
644
  prepare: (data?: any) => Promise<Transaction>;
518
- pay(to: string, amount: number, demos: Demos): Promise<Transaction>;
519
- transfer(to: string, amount: number, demos: Demos): Promise<Transaction>;
645
+ pay(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
646
+ _demNumberToOsBigint(amountDem: number): bigint;
647
+ transfer(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
520
648
  sign: (raw_tx: Transaction, keypair: IKeyPair, options: {
521
649
  algorithm: SigningAlgorithm;
522
650
  }) => Promise<Transaction>;
@@ -555,8 +683,9 @@ export declare class Demos {
555
683
  transactions: {
556
684
  empty: () => Transaction;
557
685
  prepare: (data?: any) => Promise<Transaction>;
558
- pay(to: string, amount: number, demos: Demos): Promise<Transaction>;
559
- transfer(to: string, amount: number, demos: Demos): Promise<Transaction>;
686
+ pay(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
687
+ _demNumberToOsBigint(amountDem: number): bigint;
688
+ transfer(to: string, amount: number | bigint, demos: Demos): Promise<Transaction>;
560
689
  sign: (raw_tx: Transaction, keypair: IKeyPair, options: {
561
690
  algorithm: SigningAlgorithm;
562
691
  }) => Promise<Transaction>;