@esseyonsui/sui-sdk-v3 0.0.0-stage → 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.
Files changed (197) hide show
  1. package/README.md +45 -2
  2. package/dist/config.d.ts +82 -0
  3. package/dist/config.d.ts.map +1 -0
  4. package/dist/config.js +180 -0
  5. package/dist/errors.d.ts +38 -0
  6. package/dist/errors.d.ts.map +1 -0
  7. package/dist/errors.js +32 -0
  8. package/dist/essey.d.ts +866 -0
  9. package/dist/essey.d.ts.map +1 -0
  10. package/dist/essey.js +3592 -0
  11. package/dist/index.d.ts +9 -0
  12. package/dist/index.d.ts.map +1 -0
  13. package/dist/index.js +4 -0
  14. package/dist/keeper.d.ts +54 -0
  15. package/dist/keeper.d.ts.map +1 -0
  16. package/dist/keeper.js +44 -0
  17. package/dist/network.d.ts +17 -0
  18. package/dist/network.d.ts.map +1 -0
  19. package/dist/network.js +62 -0
  20. package/dist/v3/addr-field.d.ts +3 -0
  21. package/dist/v3/addr-field.d.ts.map +1 -0
  22. package/dist/v3/addr-field.js +10 -0
  23. package/dist/v3/bodies.d.ts +54 -0
  24. package/dist/v3/bodies.d.ts.map +1 -0
  25. package/dist/v3/bodies.js +68 -0
  26. package/dist/v3/body-args.d.ts +29 -0
  27. package/dist/v3/body-args.d.ts.map +1 -0
  28. package/dist/v3/body-args.js +78 -0
  29. package/dist/v3/collateral/bodies.d.ts +78 -0
  30. package/dist/v3/collateral/bodies.d.ts.map +1 -0
  31. package/dist/v3/collateral/bodies.js +136 -0
  32. package/dist/v3/collateral/ext.d.ts +21 -0
  33. package/dist/v3/collateral/ext.d.ts.map +1 -0
  34. package/dist/v3/collateral/ext.js +19 -0
  35. package/dist/v3/collateral/health.d.ts +115 -0
  36. package/dist/v3/collateral/health.d.ts.map +1 -0
  37. package/dist/v3/collateral/health.js +255 -0
  38. package/dist/v3/collateral/index.d.ts +9 -0
  39. package/dist/v3/collateral/index.d.ts.map +1 -0
  40. package/dist/v3/collateral/index.js +8 -0
  41. package/dist/v3/collateral/local-health.d.ts +23 -0
  42. package/dist/v3/collateral/local-health.d.ts.map +1 -0
  43. package/dist/v3/collateral/local-health.js +27 -0
  44. package/dist/v3/collateral/math.d.ts +33 -0
  45. package/dist/v3/collateral/math.d.ts.map +1 -0
  46. package/dist/v3/collateral/math.js +38 -0
  47. package/dist/v3/collateral/openings.d.ts +27 -0
  48. package/dist/v3/collateral/openings.d.ts.map +1 -0
  49. package/dist/v3/collateral/openings.js +28 -0
  50. package/dist/v3/collateral/scan.d.ts +41 -0
  51. package/dist/v3/collateral/scan.d.ts.map +1 -0
  52. package/dist/v3/collateral/scan.js +76 -0
  53. package/dist/v3/collateral/seal.d.ts +31 -0
  54. package/dist/v3/collateral/seal.d.ts.map +1 -0
  55. package/dist/v3/collateral/seal.js +59 -0
  56. package/dist/v3/credential/issue.d.ts +27 -0
  57. package/dist/v3/credential/issue.d.ts.map +1 -0
  58. package/dist/v3/credential/issue.js +109 -0
  59. package/dist/v3/credential/membership.d.ts +37 -0
  60. package/dist/v3/credential/membership.d.ts.map +1 -0
  61. package/dist/v3/credential/membership.js +34 -0
  62. package/dist/v3/credential/pow.d.ts +10 -0
  63. package/dist/v3/credential/pow.d.ts.map +1 -0
  64. package/dist/v3/credential/pow.js +22 -0
  65. package/dist/v3/domains.d.ts +141 -0
  66. package/dist/v3/domains.d.ts.map +1 -0
  67. package/dist/v3/domains.js +158 -0
  68. package/dist/v3/ext.d.ts +29 -0
  69. package/dist/v3/ext.d.ts.map +1 -0
  70. package/dist/v3/ext.js +82 -0
  71. package/dist/v3/faucet.d.ts +66 -0
  72. package/dist/v3/faucet.d.ts.map +1 -0
  73. package/dist/v3/faucet.js +64 -0
  74. package/dist/v3/fee-witness.d.ts +5 -0
  75. package/dist/v3/fee-witness.d.ts.map +1 -0
  76. package/dist/v3/fee-witness.js +18 -0
  77. package/dist/v3/fees.d.ts +78 -0
  78. package/dist/v3/fees.d.ts.map +1 -0
  79. package/dist/v3/fees.js +190 -0
  80. package/dist/v3/index.d.ts +24 -0
  81. package/dist/v3/index.d.ts.map +1 -0
  82. package/dist/v3/index.js +23 -0
  83. package/dist/v3/inputs.d.ts +89 -0
  84. package/dist/v3/inputs.d.ts.map +1 -0
  85. package/dist/v3/inputs.js +152 -0
  86. package/dist/v3/keeper-badge.d.ts +48 -0
  87. package/dist/v3/keeper-badge.d.ts.map +1 -0
  88. package/dist/v3/keeper-badge.js +49 -0
  89. package/dist/v3/keeper-payload.d.ts +36 -0
  90. package/dist/v3/keeper-payload.d.ts.map +1 -0
  91. package/dist/v3/keeper-payload.js +116 -0
  92. package/dist/v3/kinds.d.ts +23 -0
  93. package/dist/v3/kinds.d.ts.map +1 -0
  94. package/dist/v3/kinds.js +50 -0
  95. package/dist/v3/loans/bodies.d.ts +215 -0
  96. package/dist/v3/loans/bodies.d.ts.map +1 -0
  97. package/dist/v3/loans/bodies.js +255 -0
  98. package/dist/v3/loans/offer-plan.d.ts +66 -0
  99. package/dist/v3/loans/offer-plan.d.ts.map +1 -0
  100. package/dist/v3/loans/offer-plan.js +141 -0
  101. package/dist/v3/loans/open-plan.d.ts +62 -0
  102. package/dist/v3/loans/open-plan.d.ts.map +1 -0
  103. package/dist/v3/loans/open-plan.js +92 -0
  104. package/dist/v3/loans/reclaim-plan.d.ts +65 -0
  105. package/dist/v3/loans/reclaim-plan.d.ts.map +1 -0
  106. package/dist/v3/loans/reclaim-plan.js +125 -0
  107. package/dist/v3/loans/refi-plan.d.ts +62 -0
  108. package/dist/v3/loans/refi-plan.d.ts.map +1 -0
  109. package/dist/v3/loans/refi-plan.js +157 -0
  110. package/dist/v3/loans/repay-plan.d.ts +40 -0
  111. package/dist/v3/loans/repay-plan.d.ts.map +1 -0
  112. package/dist/v3/loans/repay-plan.js +77 -0
  113. package/dist/v3/loans/settle-plan.d.ts +152 -0
  114. package/dist/v3/loans/settle-plan.d.ts.map +1 -0
  115. package/dist/v3/loans/settle-plan.js +210 -0
  116. package/dist/v3/market.d.ts +38 -0
  117. package/dist/v3/market.d.ts.map +1 -0
  118. package/dist/v3/market.js +30 -0
  119. package/dist/v3/notes/denominations.d.ts +13 -0
  120. package/dist/v3/notes/denominations.d.ts.map +1 -0
  121. package/dist/v3/notes/denominations.js +35 -0
  122. package/dist/v3/notes/deposit.d.ts +57 -0
  123. package/dist/v3/notes/deposit.d.ts.map +1 -0
  124. package/dist/v3/notes/deposit.js +103 -0
  125. package/dist/v3/notes/derive.d.ts +16 -0
  126. package/dist/v3/notes/derive.d.ts.map +1 -0
  127. package/dist/v3/notes/derive.js +34 -0
  128. package/dist/v3/notes/faucet.d.ts +3 -0
  129. package/dist/v3/notes/faucet.d.ts.map +1 -0
  130. package/dist/v3/notes/faucet.js +8 -0
  131. package/dist/v3/notes/index.d.ts +9 -0
  132. package/dist/v3/notes/index.d.ts.map +1 -0
  133. package/dist/v3/notes/index.js +8 -0
  134. package/dist/v3/notes/keys.d.ts +5 -0
  135. package/dist/v3/notes/keys.d.ts.map +1 -0
  136. package/dist/v3/notes/keys.js +11 -0
  137. package/dist/v3/notes/opening.d.ts +16 -0
  138. package/dist/v3/notes/opening.d.ts.map +1 -0
  139. package/dist/v3/notes/opening.js +62 -0
  140. package/dist/v3/notes/scan.d.ts +32 -0
  141. package/dist/v3/notes/scan.d.ts.map +1 -0
  142. package/dist/v3/notes/scan.js +71 -0
  143. package/dist/v3/notes/tree.d.ts +9 -0
  144. package/dist/v3/notes/tree.d.ts.map +1 -0
  145. package/dist/v3/notes/tree.js +31 -0
  146. package/dist/v3/notes/withdraw.d.ts +12 -0
  147. package/dist/v3/notes/withdraw.d.ts.map +1 -0
  148. package/dist/v3/notes/withdraw.js +27 -0
  149. package/dist/v3/proof-bytes.d.ts +4 -0
  150. package/dist/v3/proof-bytes.d.ts.map +1 -0
  151. package/dist/v3/proof-bytes.js +22 -0
  152. package/dist/v3/prover.d.ts +34 -0
  153. package/dist/v3/prover.d.ts.map +1 -0
  154. package/dist/v3/prover.js +76 -0
  155. package/dist/v3/relay/client.d.ts +57 -0
  156. package/dist/v3/relay/client.d.ts.map +1 -0
  157. package/dist/v3/relay/client.js +80 -0
  158. package/dist/v3/relay/possession.d.ts +21 -0
  159. package/dist/v3/relay/possession.d.ts.map +1 -0
  160. package/dist/v3/relay/possession.js +45 -0
  161. package/dist/v3/sales/auctioneer.d.ts +60 -0
  162. package/dist/v3/sales/auctioneer.d.ts.map +1 -0
  163. package/dist/v3/sales/auctioneer.js +130 -0
  164. package/dist/v3/sales/bcs.d.ts +4 -0
  165. package/dist/v3/sales/bcs.d.ts.map +1 -0
  166. package/dist/v3/sales/bcs.js +16 -0
  167. package/dist/v3/sales/bid.d.ts +51 -0
  168. package/dist/v3/sales/bid.d.ts.map +1 -0
  169. package/dist/v3/sales/bid.js +85 -0
  170. package/dist/v3/sales/bodies.d.ts +83 -0
  171. package/dist/v3/sales/bodies.d.ts.map +1 -0
  172. package/dist/v3/sales/bodies.js +78 -0
  173. package/dist/v3/sales/claim.d.ts +40 -0
  174. package/dist/v3/sales/claim.d.ts.map +1 -0
  175. package/dist/v3/sales/claim.js +44 -0
  176. package/dist/v3/sales/index.d.ts +10 -0
  177. package/dist/v3/sales/index.d.ts.map +1 -0
  178. package/dist/v3/sales/index.js +9 -0
  179. package/dist/v3/sales/open-kind.d.ts +42 -0
  180. package/dist/v3/sales/open-kind.d.ts.map +1 -0
  181. package/dist/v3/sales/open-kind.js +43 -0
  182. package/dist/v3/sales/outcome.d.ts +20 -0
  183. package/dist/v3/sales/outcome.d.ts.map +1 -0
  184. package/dist/v3/sales/outcome.js +21 -0
  185. package/dist/v3/sales/reclaim.d.ts +46 -0
  186. package/dist/v3/sales/reclaim.d.ts.map +1 -0
  187. package/dist/v3/sales/reclaim.js +57 -0
  188. package/dist/v3/sales/rows.d.ts +55 -0
  189. package/dist/v3/sales/rows.d.ts.map +1 -0
  190. package/dist/v3/sales/rows.js +98 -0
  191. package/dist/v3/sales/seller.d.ts +39 -0
  192. package/dist/v3/sales/seller.d.ts.map +1 -0
  193. package/dist/v3/sales/seller.js +67 -0
  194. package/dist/v3/stage1.d.ts +204 -0
  195. package/dist/v3/stage1.d.ts.map +1 -0
  196. package/dist/v3/stage1.js +575 -0
  197. package/package.json +33 -4
@@ -0,0 +1,38 @@
1
+ export type Rates = {
2
+ ltvBps: bigint;
3
+ liqBps: bigint;
4
+ bonusBps: bigint;
5
+ keeperLiqBps: bigint;
6
+ keeperDefaultBps: bigint;
7
+ };
8
+ /** One `market::Params`, each u64 as a canonical decimal string. */
9
+ export type MarketParams = {
10
+ ltv_bps: string;
11
+ liq_bps: string;
12
+ bonus_bps: string;
13
+ keeper_liq_bps: string;
14
+ keeper_default_bps: string;
15
+ };
16
+ /** The rate fields of a `Market<C, L>` object. `pending` is null when no change is proposed. */
17
+ export type MarketFields = {
18
+ params: MarketParams;
19
+ pending: MarketParams | null;
20
+ effective_at_ms: string;
21
+ /** The oracle type name and the feed its readings must carry: a Pyth market's feed is its PriceInfoObject id. */
22
+ oracle?: string;
23
+ feed_id?: string;
24
+ };
25
+ /** `committable`: `market::commit` takes no admin cap, so anyone may apply this change before an open lands. */
26
+ export type PendingRates = {
27
+ rates: Rates;
28
+ effectiveAtMs: bigint;
29
+ committable: boolean;
30
+ };
31
+ /** `current` is what `request::new_request` copies into a new loan (request.move:52). */
32
+ export type MarketRates = {
33
+ market: string;
34
+ current: Rates;
35
+ pending: PendingRates | null;
36
+ };
37
+ export declare function marketRates(market: string, fields: MarketFields, nowMs: number): MarketRates;
38
+ //# sourceMappingURL=market.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"market.d.ts","sourceRoot":"","sources":["../../src/v3/market.ts"],"names":[],"mappings":"AAKA,MAAM,MAAM,KAAK,GAAG;IAClB,MAAM,EAAE,MAAM,CAAC;IACf,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;IACjB,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;CAC1B,CAAC;AAEF,oEAAoE;AACpE,MAAM,MAAM,YAAY,GAAG;IACzB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,cAAc,EAAE,MAAM,CAAC;IACvB,kBAAkB,EAAE,MAAM,CAAC;CAC5B,CAAC;AAEF,gGAAgG;AAChG,MAAM,MAAM,YAAY,GAAG;IACzB,MAAM,EAAE,YAAY,CAAC;IACrB,OAAO,EAAE,YAAY,GAAG,IAAI,CAAC;IAC7B,eAAe,EAAE,MAAM,CAAC;IACxB,iHAAiH;IACjH,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,gHAAgH;AAChH,MAAM,MAAM,YAAY,GAAG;IAAE,KAAK,EAAE,KAAK,CAAC;IAAC,aAAa,EAAE,MAAM,CAAC;IAAC,WAAW,EAAE,OAAO,CAAA;CAAE,CAAC;AAEzF,yFAAyF;AACzF,MAAM,MAAM,WAAW,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,KAAK,CAAC;IAAC,OAAO,EAAE,YAAY,GAAG,IAAI,CAAA;CAAE,CAAC;AAkB3F,wBAAgB,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,GAAG,WAAW,CAY5F"}
@@ -0,0 +1,30 @@
1
+ import { EsseyError } from "../errors.js";
2
+ import { decimal } from "../network.js";
3
+ const U64 = (1n << 64n) - 1n;
4
+ function u64(value, what) {
5
+ const n = decimal(value, what);
6
+ if (n > U64)
7
+ throw new EsseyError("INVALID_ARGUMENT", `${what} is not a u64`);
8
+ return n;
9
+ }
10
+ function rates(p, what) {
11
+ return {
12
+ ltvBps: u64(p.ltv_bps, `${what}.ltv_bps`),
13
+ liqBps: u64(p.liq_bps, `${what}.liq_bps`),
14
+ bonusBps: u64(p.bonus_bps, `${what}.bonus_bps`),
15
+ keeperLiqBps: u64(p.keeper_liq_bps, `${what}.keeper_liq_bps`),
16
+ keeperDefaultBps: u64(p.keeper_default_bps, `${what}.keeper_default_bps`),
17
+ };
18
+ }
19
+ export function marketRates(market, fields, nowMs) {
20
+ const current = rates(fields.params, "params");
21
+ const effectiveAtMs = u64(fields.effective_at_ms, "effective_at_ms");
22
+ const pending = fields.pending === null
23
+ ? null
24
+ : {
25
+ rates: rates(fields.pending, "pending"),
26
+ effectiveAtMs,
27
+ committable: BigInt(nowMs) >= effectiveAtMs,
28
+ };
29
+ return { market, current, pending };
30
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * NEEDS-BORIS: a piece is two relayer requests (sponsor, execute) and one address gets 60 in a window
3
+ * (`services/relayer-v3/src/config.ts:260`, every POST counted at `server.ts:431`); it serves no route that names
4
+ * the limit. 29 leaves two spare. Any more from that address in the window still stop a piece, and one stopped at
5
+ * its execute is already funded: `recoverStranded` lands it.
6
+ */
7
+ export declare const MAX_SPLIT_PIECES = 29;
8
+ /**
9
+ * D-SPEC:76: fixed edge amounts. Greedy, largest first; an amount the list cannot sum to exactly is refused.
10
+ * Counted before a piece is built, so no amount allocates past the cap.
11
+ */
12
+ export declare function splitDenominations(amount: bigint, list: string[]): bigint[];
13
+ //# sourceMappingURL=denominations.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"denominations.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/denominations.ts"],"names":[],"mappings":"AAOA;;;;;GAKG;AACH,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC;;;GAGG;AACH,wBAAgB,kBAAkB,CAAC,MAAM,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,EAAE,GAAG,MAAM,EAAE,CAiB3E"}
@@ -0,0 +1,35 @@
1
+ import { EsseyError } from "../../errors.js";
2
+ import { decimal } from "../../network.js";
3
+ const refuse = (what) => {
4
+ throw new EsseyError("INVALID_ARGUMENT", what);
5
+ };
6
+ /**
7
+ * NEEDS-BORIS: a piece is two relayer requests (sponsor, execute) and one address gets 60 in a window
8
+ * (`services/relayer-v3/src/config.ts:260`, every POST counted at `server.ts:431`); it serves no route that names
9
+ * the limit. 29 leaves two spare. Any more from that address in the window still stop a piece, and one stopped at
10
+ * its execute is already funded: `recoverStranded` lands it.
11
+ */
12
+ export const MAX_SPLIT_PIECES = 29;
13
+ /**
14
+ * D-SPEC:76: fixed edge amounts. Greedy, largest first; an amount the list cannot sum to exactly is refused.
15
+ * Counted before a piece is built, so no amount allocates past the cap.
16
+ */
17
+ export function splitDenominations(amount, list) {
18
+ if (amount <= 0n)
19
+ refuse("amount must be above 0");
20
+ const sizes = list.map((d) => decimal(d, "denomination")).sort((a, b) => (a > b ? -1 : a < b ? 1 : 0));
21
+ if (sizes.length === 0 || sizes.some((d) => d === 0n))
22
+ refuse("the coin has no usable denominations");
23
+ let left = amount;
24
+ const counts = sizes.map((size) => {
25
+ const count = left / size;
26
+ left -= count * size;
27
+ return count;
28
+ });
29
+ if (left !== 0n)
30
+ refuse("amount is not a sum of the coin's denominations");
31
+ const total = counts.reduce((sum, count) => sum + count, 0n);
32
+ if (total > BigInt(MAX_SPLIT_PIECES))
33
+ refuse(`refused: ${total} pieces is more than the ${MAX_SPLIT_PIECES} one call deposits, which an otherwise idle relayer window sponsors; nothing was funded`);
34
+ return sizes.flatMap((size, i) => Array.from({ length: Number(counts[i]) }, () => size));
35
+ }
@@ -0,0 +1,57 @@
1
+ import { type Fetch } from "../../network.ts";
2
+ import type { Possession } from "../relay/possession.ts";
3
+ export type SharedRef = {
4
+ objectId: string;
5
+ initialSharedVersion: string;
6
+ };
7
+ export type DepositKindParams = {
8
+ pkg: string;
9
+ coinType: string;
10
+ pool: SharedRef;
11
+ feeConfig: SharedRef;
12
+ value: bigint;
13
+ proof: Uint8Array;
14
+ note: bigint;
15
+ feeNote: bigint;
16
+ ephX: bigint;
17
+ ephY: bigint;
18
+ feeCt: bigint;
19
+ feeCommit: bigint;
20
+ encNote: string;
21
+ encFee: string;
22
+ };
23
+ /** NEEDS-BORIS: the sponsor pass room. The relayer parses the pass and limits its tag (services/relayer-v3/src/server.ts:545,575). */
24
+ export declare const DEPOSIT_ROOM: string;
25
+ /** NEEDS-BORIS: the most gas a sponsor may set on a deposit; the relayer reserves 5e6 (services/relayer-v3/src/gas-pool.ts:12). */
26
+ export declare const MAX_SPONSOR_BUDGET = 1000000000n;
27
+ /**
28
+ * `pool::deposit` (`move/v3/pool/sources/pool.move:186-188`) on a coin redeemed from the fresh sender's address
29
+ * balance. Only allow-listed calls (`services/relayer-v3/src/sponsor.ts:33-70`).
30
+ */
31
+ export declare function depositKind(p: DepositKindParams): Promise<string>;
32
+ /**
33
+ * The fresh key signs only the kind it built, sent from itself: the returned bytes are that kind, or a V1
34
+ * `TransactionData` (variant byte 0) whose kind and sender are ours and whose gas the relayer pays from its own
35
+ * coins, so the deposit's value never pays gas.
36
+ */
37
+ export declare function carriesKind(txBytesB64: string, kindB64: string, sender: string, gasOwner: string | null): boolean;
38
+ export type SponsorPass = {
39
+ tag: string;
40
+ expiryMs: string;
41
+ proof: string;
42
+ };
43
+ /** `POST /v3/sponsor` answers the bytes unsigned by the relayer (D-SPEC:305; `services/relayer-v3/src/server.ts:607`). */
44
+ export declare function sponsor(fetch: Fetch, base: string, body: {
45
+ txKindB64: string;
46
+ sender: string;
47
+ pass: SponsorPass;
48
+ possession: Possession;
49
+ }): Promise<{
50
+ txBytesB64: string;
51
+ }>;
52
+ /** `POST /v3/sponsor/execute`. */
53
+ export declare function executeSponsored(fetch: Fetch, base: string, body: {
54
+ txBytesB64: string;
55
+ userSig: string;
56
+ }): Promise<string>;
57
+ //# sourceMappingURL=deposit.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"deposit.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/deposit.ts"],"names":[],"mappings":"AAKA,OAAO,EAAY,KAAK,KAAK,EAAkB,MAAM,kBAAkB,CAAC;AAExE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,wBAAwB,CAAC;AAEzD,MAAM,MAAM,SAAS,GAAG;IAAE,QAAQ,EAAE,MAAM,CAAC;IAAC,oBAAoB,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E,MAAM,MAAM,iBAAiB,GAAG;IAC9B,GAAG,EAAE,MAAM,CAAC;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,SAAS,CAAC;IAChB,SAAS,EAAE,SAAS,CAAC;IACrB,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,OAAO,EAAE,MAAM,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;CAChB,CAAC;AAEF,sIAAsI;AACtI,eAAO,MAAM,YAAY,QAA+E,CAAC;AAEzG,mIAAmI;AACnI,eAAO,MAAM,kBAAkB,cAAiB,CAAC;AAIjD;;;GAGG;AACH,wBAAsB,WAAW,CAAC,CAAC,EAAE,iBAAiB,GAAG,OAAO,CAAC,MAAM,CAAC,CAuBvE;AA2BD;;;;GAIG;AACH,wBAAgB,WAAW,CACzB,UAAU,EAAE,MAAM,EAClB,OAAO,EAAE,MAAM,EACf,MAAM,EAAE,MAAM,EACd,QAAQ,EAAE,MAAM,GAAG,IAAI,GACtB,OAAO,CAQT;AAYD,MAAM,MAAM,WAAW,GAAG;IAAE,GAAG,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAE3E,0HAA0H;AAC1H,wBAAsB,OAAO,CAC3B,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE;IAAE,SAAS,EAAE,MAAM,CAAC;IAAC,MAAM,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,WAAW,CAAC;IAAC,UAAU,EAAE,UAAU,CAAA;CAAE,GACrF,OAAO,CAAC;IAAE,UAAU,EAAE,MAAM,CAAA;CAAE,CAAC,CAKjC;AAED,kCAAkC;AAClC,wBAAsB,gBAAgB,CACpC,KAAK,EAAE,KAAK,EACZ,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE;IAAE,UAAU,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,MAAM,CAAA;CAAE,GAC5C,OAAO,CAAC,MAAM,CAAC,CAKjB"}
@@ -0,0 +1,103 @@
1
+ import { bcs } from "@mysten/sui/bcs";
2
+ import { Transaction } from "@mysten/sui/transactions";
3
+ import { fromBase64, normalizeSuiAddress, toBase64 } from "@mysten/sui/utils";
4
+ import { EsseyError } from "../../errors.js";
5
+ import { postJson } from "../../network.js";
6
+ import { refusal } from "../relay/client.js";
7
+ /** NEEDS-BORIS: the sponsor pass room. The relayer parses the pass and limits its tag (services/relayer-v3/src/server.ts:545,575). */
8
+ export const DEPOSIT_ROOM = `0x${Buffer.from("ess3/sponsor/deposit").toString("hex").padStart(64, "0")}`;
9
+ /** NEEDS-BORIS: the most gas a sponsor may set on a deposit; the relayer reserves 5e6 (services/relayer-v3/src/gas-pool.ts:12). */
10
+ export const MAX_SPONSOR_BUDGET = 1000000000n;
11
+ const bytes = (hex) => Uint8Array.from(Buffer.from(hex.slice(2), "hex"));
12
+ /**
13
+ * `pool::deposit` (`move/v3/pool/sources/pool.move:186-188`) on a coin redeemed from the fresh sender's address
14
+ * balance. Only allow-listed calls (`services/relayer-v3/src/sponsor.ts:33-70`).
15
+ */
16
+ export async function depositKind(p) {
17
+ const tx = new Transaction();
18
+ const shared = (r) => tx.sharedObjectRef({ ...r, mutable: true });
19
+ const coin = tx.moveCall({
20
+ target: "0x2::coin::redeem_funds",
21
+ typeArguments: [p.coinType],
22
+ arguments: [tx.withdrawal({ amount: p.value, type: p.coinType })],
23
+ });
24
+ tx.moveCall({
25
+ target: `${normalizeSuiAddress(p.pkg)}::pool::deposit`,
26
+ typeArguments: [p.coinType],
27
+ arguments: [
28
+ shared(p.pool),
29
+ tx.sharedObjectRef({ ...p.feeConfig, mutable: false }),
30
+ coin,
31
+ tx.pure.vector("u8", p.proof),
32
+ ...[p.note, p.feeNote, p.ephX, p.ephY, p.feeCt, p.feeCommit].map((v) => tx.pure.u256(v)),
33
+ tx.pure.vector("u8", bytes(p.encNote)),
34
+ tx.pure.vector("u8", bytes(p.encFee)),
35
+ tx.object.clock(),
36
+ ],
37
+ });
38
+ return toBase64(await tx.build({ onlyTransactionKind: true }));
39
+ }
40
+ function equal(a, b) {
41
+ return a.length === b.length && a.every((v, i) => v === b[i]);
42
+ }
43
+ function gasOf(tx) {
44
+ try {
45
+ return bcs.TransactionData.parse(tx).V1?.gasData ?? null;
46
+ }
47
+ catch {
48
+ return null;
49
+ }
50
+ }
51
+ /** The relayer pays from its own coins: the chain takes gas payment only from objects the gas owner holds. */
52
+ function sponsorPays(tx, gasOwner) {
53
+ const gas = gasOf(tx);
54
+ if (!gas || gasOwner === null)
55
+ return false;
56
+ const budget = BigInt(gas.budget);
57
+ return (normalizeSuiAddress(gas.owner) === normalizeSuiAddress(gasOwner) &&
58
+ gas.payment.length > 0 &&
59
+ budget > 0n &&
60
+ budget <= MAX_SPONSOR_BUDGET);
61
+ }
62
+ /**
63
+ * The fresh key signs only the kind it built, sent from itself: the returned bytes are that kind, or a V1
64
+ * `TransactionData` (variant byte 0) whose kind and sender are ours and whose gas the relayer pays from its own
65
+ * coins, so the deposit's value never pays gas.
66
+ */
67
+ export function carriesKind(txBytesB64, kindB64, sender, gasOwner) {
68
+ const tx = fromBase64(txBytesB64);
69
+ const kind = fromBase64(kindB64);
70
+ if (equal(tx, kind))
71
+ return true;
72
+ if (tx[0] !== 0 || !equal(tx.subarray(1, 1 + kind.length), kind))
73
+ return false;
74
+ if (!equal(tx.subarray(1 + kind.length, 33 + kind.length), bytes(normalizeSuiAddress(sender))))
75
+ return false;
76
+ return sponsorPays(tx, gasOwner);
77
+ }
78
+ /**
79
+ * Only a refusal is "nothing landed". An `OUTCOME_UNKNOWN` or `submitted: true` reply
80
+ * (`services/relayer-v3/src/server.ts:679-683`) keeps the relay client's code and wording.
81
+ */
82
+ function refused(reply) {
83
+ const error = refusal(reply);
84
+ if (error.code !== "RELAY_REFUSED")
85
+ return error;
86
+ return new EsseyError("RELAY_REFUSED", `${error.message}; nothing landed`, error.detail);
87
+ }
88
+ /** `POST /v3/sponsor` answers the bytes unsigned by the relayer (D-SPEC:305; `services/relayer-v3/src/server.ts:607`). */
89
+ export async function sponsor(fetch, base, body) {
90
+ const reply = await postJson(fetch, `${base}/v3/sponsor`, body);
91
+ const { ok, txBytesB64 } = reply.body;
92
+ if (reply.status !== 200 || ok !== true || typeof txBytesB64 !== "string")
93
+ throw refused(reply);
94
+ return { txBytesB64 };
95
+ }
96
+ /** `POST /v3/sponsor/execute`. */
97
+ export async function executeSponsored(fetch, base, body) {
98
+ const reply = await postJson(fetch, `${base}/v3/sponsor/execute`, body);
99
+ const { ok, digest } = reply.body;
100
+ if (reply.status !== 200 || ok !== true || typeof digest !== "string")
101
+ throw refused(reply);
102
+ return digest;
103
+ }
@@ -0,0 +1,16 @@
1
+ import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
2
+ export type NoteKey = {
3
+ sk: bigint;
4
+ blinding: bigint;
5
+ };
6
+ export declare const SEED_BYTES = 32;
7
+ /** A-SPEC:166 `sk_i = H(seed, "ess3/notekey", counter_i) mod R`, H = blake2b-512. The blinding is derived the same way. */
8
+ export declare function deriveNoteKey(seed: Uint8Array, index: number): NoteKey;
9
+ /** The one secp256k1 key the wallet's note openings are sealed to. */
10
+ export declare function noteEncKey(seed: Uint8Array): {
11
+ encKey: Uint8Array;
12
+ encPub: Uint8Array;
13
+ };
14
+ /** D-SPEC:564, :596: the fresh one-time sender of deposit call `index`. */
15
+ export declare function depositSigner(seed: Uint8Array, index: number): Ed25519Keypair;
16
+ //# sourceMappingURL=derive.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"derive.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/derive.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,cAAc,EAAE,MAAM,8BAA8B,CAAC;AAI9D,MAAM,MAAM,OAAO,GAAG;IAAE,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAA;CAAE,CAAC;AAEvD,eAAO,MAAM,UAAU,KAAK,CAAC;AAoB7B,2HAA2H;AAC3H,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,OAAO,CAEtE;AAED,sEAAsE;AACtE,wBAAgB,UAAU,CAAC,IAAI,EAAE,UAAU,GAAG;IAAE,MAAM,EAAE,UAAU,CAAC;IAAC,MAAM,EAAE,UAAU,CAAA;CAAE,CAKvF;AAED,2EAA2E;AAC3E,wBAAgB,aAAa,CAAC,IAAI,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,GAAG,cAAc,CAE7E"}
@@ -0,0 +1,34 @@
1
+ import { blake2b } from "@noble/hashes/blake2.js";
2
+ import { secp256k1 } from "@noble/curves/secp256k1.js";
3
+ import { Ed25519Keypair } from "@mysten/sui/keypairs/ed25519";
4
+ import { R } from "../domains.js";
5
+ export const SEED_BYTES = 32;
6
+ const utf8 = (s) => new TextEncoder().encode(s);
7
+ function u64be(index) {
8
+ if (!Number.isSafeInteger(index) || index < 0)
9
+ throw new Error("index is not a whole number");
10
+ const out = new Uint8Array(8);
11
+ new DataView(out.buffer).setBigUint64(0, BigInt(index));
12
+ return out;
13
+ }
14
+ function digest(seed, tag, index, dkLen) {
15
+ if (seed.length !== SEED_BYTES)
16
+ throw new Error("seed is not 32 bytes");
17
+ return blake2b(Uint8Array.from([...seed, ...utf8(tag), ...index]), { dkLen });
18
+ }
19
+ function field(seed, tag, index) {
20
+ return BigInt(`0x${Buffer.from(digest(seed, tag, u64be(index), 64)).toString("hex")}`) % R;
21
+ }
22
+ /** A-SPEC:166 `sk_i = H(seed, "ess3/notekey", counter_i) mod R`, H = blake2b-512. The blinding is derived the same way. */
23
+ export function deriveNoteKey(seed, index) {
24
+ return { sk: field(seed, "ess3/notekey", index), blinding: field(seed, "ess3/noteblind", index) };
25
+ }
26
+ /** The one secp256k1 key the wallet's note openings are sealed to. */
27
+ export function noteEncKey(seed) {
28
+ const encKey = secp256k1.utils.randomSecretKey(digest(seed, "ess3/noteenc", new Uint8Array(), secp256k1.lengths.seed));
29
+ return { encKey, encPub: secp256k1.getPublicKey(encKey, true) };
30
+ }
31
+ /** D-SPEC:564, :596: the fresh one-time sender of deposit call `index`. */
32
+ export function depositSigner(seed, index) {
33
+ return Ed25519Keypair.fromSecretKey(digest(seed, "ess3/signer", u64be(index), 32));
34
+ }
@@ -0,0 +1,3 @@
1
+ import type { CoinKey, V3Config } from "../../config.ts";
2
+ export declare function faucetGrant(coin: CoinKey, config: V3Config): bigint;
3
+ //# sourceMappingURL=faucet.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"faucet.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/faucet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAKzD,wBAAgB,WAAW,CAAC,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,GAAG,MAAM,CAInE"}
@@ -0,0 +1,8 @@
1
+ /** Mirror of `services/v3api/src/config.ts:35` `GRANT_UNITS`. The grant is not on the wire, yet the proof folds it. */
2
+ const GRANT_UNITS = { tusdsui: 10n, usdc: 2n, sui: 1n };
3
+ export function faucetGrant(coin, config) {
4
+ const decimals = config.coins[coin]?.decimals;
5
+ if (!Number.isInteger(decimals) || decimals < 0)
6
+ throw new Error("faucet coin decimals");
7
+ return GRANT_UNITS[coin] * 10n ** BigInt(decimals);
8
+ }
@@ -0,0 +1,9 @@
1
+ export * from "./denominations.ts";
2
+ export * from "./deposit.ts";
3
+ export * from "./derive.ts";
4
+ export * from "./faucet.ts";
5
+ export * from "./opening.ts";
6
+ export * from "./scan.ts";
7
+ export * from "./tree.ts";
8
+ export * from "./withdraw.ts";
9
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/index.ts"],"names":[],"mappings":"AAAA,cAAc,oBAAoB,CAAC;AACnC,cAAc,cAAc,CAAC;AAC7B,cAAc,aAAa,CAAC;AAC5B,cAAc,aAAa,CAAC;AAC5B,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,eAAe,CAAC"}
@@ -0,0 +1,8 @@
1
+ export * from "./denominations.js";
2
+ export * from "./deposit.js";
3
+ export * from "./derive.js";
4
+ export * from "./faucet.js";
5
+ export * from "./opening.js";
6
+ export * from "./scan.js";
7
+ export * from "./tree.js";
8
+ export * from "./withdraw.js";
@@ -0,0 +1,5 @@
1
+ export declare function notePk(sk: bigint): bigint;
2
+ export declare function noteCommitment(amount: bigint, pk: bigint, blinding: bigint): bigint;
3
+ /** `pathIndex` is the leaf's index in the pool tree, so one note has one nullifier. */
4
+ export declare function noteNullifier(commitment: bigint, sk: bigint, pathIndex: bigint): bigint;
5
+ //# sourceMappingURL=keys.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"keys.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/keys.ts"],"names":[],"mappings":"AAEA,wBAAgB,MAAM,CAAC,EAAE,EAAE,MAAM,GAAG,MAAM,CAEzC;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAEnF;AAED,uFAAuF;AACvF,wBAAgB,aAAa,CAAC,UAAU,EAAE,MAAM,EAAE,EAAE,EAAE,MAAM,EAAE,SAAS,EAAE,MAAM,GAAG,MAAM,CAEvF"}
@@ -0,0 +1,11 @@
1
+ import { poseidonHash } from "@mysten/sui/zklogin";
2
+ export function notePk(sk) {
3
+ return poseidonHash([sk]);
4
+ }
5
+ export function noteCommitment(amount, pk, blinding) {
6
+ return poseidonHash([amount, pk, blinding]);
7
+ }
8
+ /** `pathIndex` is the leaf's index in the pool tree, so one note has one nullifier. */
9
+ export function noteNullifier(commitment, sk, pathIndex) {
10
+ return poseidonHash([commitment, pathIndex, poseidonHash([sk, commitment, pathIndex])]);
11
+ }
@@ -0,0 +1,16 @@
1
+ /** What an owner-scanned note ciphertext carries: the note's value and blinding, and the counter its key came from. */
2
+ export type Opening = {
3
+ value: bigint;
4
+ blinding: bigint;
5
+ index: number;
6
+ };
7
+ /**
8
+ * E-27: 128 bytes, ephemeral key ‖ nonce ‖ XChaCha20-Poly1305 of value (u64) ‖ blinding (32) ‖ index (u64) ‖ zeros.
9
+ * V2's `decryptOpening` box is 137 bytes, so the value shrinks to the u64 the pool holds.
10
+ */
11
+ export declare function sealOpening(o: Opening, encPub: Uint8Array): string;
12
+ /** Never throws: a ciphertext sealed to someone else, filler, or garbage is simply not ours. */
13
+ export declare function openOpening(enc: string, encKey: Uint8Array): Opening | null;
14
+ /** E-27 filler for a leaf whose opening travels another way (the fee note, the pad). */
15
+ export declare function fillerEnc(): string;
16
+ //# sourceMappingURL=opening.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opening.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/opening.ts"],"names":[],"mappings":"AAUA,uHAAuH;AACvH,MAAM,MAAM,OAAO,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAoBzE;;;GAGG;AACH,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,UAAU,GAAG,MAAM,CAWlE;AAED,gGAAgG;AAChG,wBAAgB,WAAW,CAAC,GAAG,EAAE,MAAM,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,GAAG,IAAI,CAgB3E;AAED,wFAAwF;AACxF,wBAAgB,SAAS,IAAI,MAAM,CAElC"}
@@ -0,0 +1,62 @@
1
+ import { randomBytes } from "node:crypto";
2
+ import { xchacha20poly1305 } from "@noble/ciphers/chacha.js";
3
+ import { secp256k1 } from "@noble/curves/secp256k1.js";
4
+ import { hkdf } from "@noble/hashes/hkdf.js";
5
+ import { sha256 } from "@noble/hashes/sha2.js";
6
+ import { R } from "../domains.js";
7
+ import { ENC_BYTES } from "../kinds.js";
8
+ const INFO = new TextEncoder().encode("ess3/note-enc/v1");
9
+ const EPH = 33;
10
+ const NONCE = 24;
11
+ const PLAIN = ENC_BYTES - EPH - NONCE - 16;
12
+ const U64 = (1n << 64n) - 1n;
13
+ const hex = (b) => `0x${Buffer.from(b).toString("hex")}`;
14
+ function be(x, n) {
15
+ return Array.from({ length: n }, (_, i) => Number((x >> BigInt(8 * (n - 1 - i))) & 0xffn));
16
+ }
17
+ const num = (b) => BigInt(`0x${Buffer.from(b).toString("hex")}`);
18
+ function boxKey(secret, pub) {
19
+ return hkdf(sha256, secp256k1.getSharedSecret(secret, pub, true).slice(1), undefined, INFO, 32);
20
+ }
21
+ /**
22
+ * E-27: 128 bytes, ephemeral key ‖ nonce ‖ XChaCha20-Poly1305 of value (u64) ‖ blinding (32) ‖ index (u64) ‖ zeros.
23
+ * V2's `decryptOpening` box is 137 bytes, so the value shrinks to the u64 the pool holds.
24
+ */
25
+ export function sealOpening(o, encPub) {
26
+ if (o.value < 0n || o.value > U64)
27
+ throw new Error("value is not a u64");
28
+ if (o.blinding < 0n || o.blinding >= R)
29
+ throw new Error("blinding is not a field element");
30
+ if (!Number.isSafeInteger(o.index) || o.index < 0)
31
+ throw new Error("index is not a whole number");
32
+ const plain = Uint8Array.from([...be(o.value, 8), ...be(o.blinding, 32), ...be(BigInt(o.index), 8)]);
33
+ const padded = new Uint8Array(PLAIN);
34
+ padded.set(plain);
35
+ const eph = secp256k1.utils.randomSecretKey();
36
+ const nonce = randomBytes(NONCE);
37
+ const sealed = xchacha20poly1305(boxKey(eph, encPub), nonce).encrypt(padded);
38
+ return hex(Uint8Array.from([...secp256k1.getPublicKey(eph, true), ...nonce, ...sealed]));
39
+ }
40
+ /** Never throws: a ciphertext sealed to someone else, filler, or garbage is simply not ours. */
41
+ export function openOpening(enc, encKey) {
42
+ try {
43
+ const blob = Buffer.from(enc.slice(2), "hex");
44
+ if (blob.length !== ENC_BYTES)
45
+ return null;
46
+ const plain = xchacha20poly1305(boxKey(encKey, blob.subarray(0, EPH)), blob.subarray(EPH, EPH + NONCE)).decrypt(blob.subarray(EPH + NONCE));
47
+ if (plain.subarray(48).some((b) => b !== 0))
48
+ return null;
49
+ const blinding = num(plain.subarray(8, 40));
50
+ const index = Number(num(plain.subarray(40, 48)));
51
+ if (blinding >= R || !Number.isSafeInteger(index))
52
+ return null;
53
+ return { value: num(plain.subarray(0, 8)), blinding, index };
54
+ }
55
+ catch {
56
+ return null;
57
+ }
58
+ }
59
+ /** E-27 filler for a leaf whose opening travels another way (the fee note, the pad). */
60
+ export function fillerEnc() {
61
+ return hex(randomBytes(ENC_BYTES));
62
+ }
@@ -0,0 +1,32 @@
1
+ import type { CoinKey } from "../../config.ts";
2
+ /** One `privacy_v3::pool::NewCommitment` event. */
3
+ export type PoolCommitment = {
4
+ index: bigint;
5
+ commitment: bigint;
6
+ enc: string;
7
+ };
8
+ /** Every commitment and nullifier event of one pool. Reading the whole pool names none of the caller's notes. */
9
+ export type PoolEvents = {
10
+ commitments: PoolCommitment[];
11
+ nullifiers: bigint[];
12
+ };
13
+ /** An unspent note this seed owns. Never returned to a caller: it holds the spend key. */
14
+ export type OwnNote = {
15
+ value: bigint;
16
+ sk: bigint;
17
+ blinding: bigint;
18
+ index: number;
19
+ };
20
+ export type NoteSummary = Partial<Record<CoinKey, {
21
+ balance: bigint;
22
+ notes: number;
23
+ }>>;
24
+ /** The leaves in index order. A gap means the reader missed an event, and every path would be wrong. */
25
+ export declare function orderedLeaves(events: PoolEvents): bigint[];
26
+ /** The key index of every commitment this seed made, spent or not, including a zero-value change. */
27
+ export declare function usedIndices(events: PoolEvents, seed: Uint8Array, encKey: Uint8Array): number[];
28
+ /** A nullifier as a field, whether the reader sent a bigint, a decimal, `0x` hex, or 1..32 big-endian bytes. */
29
+ export declare function asNullifier(value: unknown): bigint | null;
30
+ /** Trial-opens every ciphertext, keeps the ones whose commitment rebuilds under this seed's key, and drops the spent and the empty. */
31
+ export declare function ownNotes(events: PoolEvents, seed: Uint8Array, encKey: Uint8Array): OwnNote[];
32
+ //# sourceMappingURL=scan.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"scan.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/scan.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAO/C,mDAAmD;AACnD,MAAM,MAAM,cAAc,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,UAAU,EAAE,MAAM,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAAC;AAEhF,iHAAiH;AACjH,MAAM,MAAM,UAAU,GAAG;IAAE,WAAW,EAAE,cAAc,EAAE,CAAC;IAAC,UAAU,EAAE,MAAM,EAAE,CAAA;CAAE,CAAC;AAEjF,0FAA0F;AAC1F,MAAM,MAAM,OAAO,GAAG;IAAE,KAAK,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,QAAQ,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC;AAErF,MAAM,MAAM,WAAW,GAAG,OAAO,CAAC,MAAM,CAAC,OAAO,EAAE;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC,CAAC;AAEvF,wGAAwG;AACxG,wBAAgB,aAAa,CAAC,MAAM,EAAE,UAAU,GAAG,MAAM,EAAE,CAO1D;AAUD,qGAAqG;AACrG,wBAAgB,WAAW,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,MAAM,EAAE,CAE9F;AAED,gHAAgH;AAChH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,GAAG,IAAI,CAWzD;AAED,uIAAuI;AACvI,wBAAgB,QAAQ,CAAC,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,UAAU,EAAE,MAAM,EAAE,UAAU,GAAG,OAAO,EAAE,CAqB5F"}
@@ -0,0 +1,71 @@
1
+ import { EsseyError } from "../../errors.js";
2
+ import { R } from "../domains.js";
3
+ import { noteCommitment, noteNullifier, notePk } from "./keys.js";
4
+ import { deriveNoteKey } from "./derive.js";
5
+ import { openOpening } from "./opening.js";
6
+ /** The leaves in index order. A gap means the reader missed an event, and every path would be wrong. */
7
+ export function orderedLeaves(events) {
8
+ const leaves = [...events.commitments].sort((a, b) => (a.index < b.index ? -1 : 1));
9
+ leaves.forEach((c, i) => {
10
+ if (c.index !== BigInt(i))
11
+ throw new EsseyError("SERVICE_REFUSED", "refused: the pool reader returned a gap in the commitments");
12
+ });
13
+ return leaves.map((c) => c.commitment);
14
+ }
15
+ function opens(c, seed, encKey) {
16
+ const opened = openOpening(c.enc, encKey);
17
+ if (!opened)
18
+ return null;
19
+ const { sk } = deriveNoteKey(seed, opened.index);
20
+ if (noteCommitment(opened.value, notePk(sk), opened.blinding) !== c.commitment)
21
+ return null;
22
+ return { ...opened, sk };
23
+ }
24
+ /** The key index of every commitment this seed made, spent or not, including a zero-value change. */
25
+ export function usedIndices(events, seed, encKey) {
26
+ return events.commitments.flatMap((c) => opens(c, seed, encKey)?.index ?? []);
27
+ }
28
+ /** A nullifier as a field, whether the reader sent a bigint, a decimal, `0x` hex, or 1..32 big-endian bytes. */
29
+ export function asNullifier(value) {
30
+ let n = null;
31
+ if (typeof value === "bigint")
32
+ n = value;
33
+ else if (typeof value === "string" && /^(0|[1-9][0-9]*)$/.test(value))
34
+ n = BigInt(value);
35
+ else if (typeof value === "string" && /^0x[0-9a-fA-F]{1,64}$/.test(value))
36
+ n = BigInt(value);
37
+ else if (value instanceof Uint8Array && value.length > 0 && value.length <= 32) {
38
+ n = 0n;
39
+ for (const byte of value)
40
+ n = (n << 8n) | BigInt(byte);
41
+ }
42
+ if (n === null || n < 0n || n >= R)
43
+ return null;
44
+ return n;
45
+ }
46
+ /** Trial-opens every ciphertext, keeps the ones whose commitment rebuilds under this seed's key, and drops the spent and the empty. */
47
+ export function ownNotes(events, seed, encKey) {
48
+ orderedLeaves(events);
49
+ const spent = new Set();
50
+ for (const raw of events.nullifiers) {
51
+ const n = asNullifier(raw);
52
+ if (n !== null)
53
+ spent.add(n);
54
+ }
55
+ const owned = [];
56
+ for (const c of events.commitments) {
57
+ const opened = opens(c, seed, encKey);
58
+ if (!opened || opened.value === 0n)
59
+ continue;
60
+ const { sk } = opened;
61
+ if (spent.has(noteNullifier(c.commitment, sk, c.index)))
62
+ continue;
63
+ owned.push({
64
+ value: opened.value,
65
+ sk,
66
+ blinding: opened.blinding,
67
+ index: Number(c.index),
68
+ });
69
+ }
70
+ return owned;
71
+ }
@@ -0,0 +1,9 @@
1
+ /** `privacy_v3::merkle_tree::zero_leaf` (`move/v3/pool/sources/merkle_tree.move:101-103`). */
2
+ export declare const ZERO_LEAF: bigint;
3
+ export type PoolTree = {
4
+ root: bigint;
5
+ path(index: number): bigint[];
6
+ };
7
+ /** The tree `merkle_tree::insert_pair` builds from these leaves, in index order. */
8
+ export declare function poolTree(leaves: bigint[]): PoolTree;
9
+ //# sourceMappingURL=tree.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"tree.d.ts","sourceRoot":"","sources":["../../../src/v3/notes/tree.ts"],"names":[],"mappings":"AAMA,8FAA8F;AAC9F,eAAO,MAAM,SAAS,QAC2E,CAAC;AAQlG,MAAM,MAAM,QAAQ,GAAG;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,CAAC;AAEvE,oFAAoF;AACpF,wBAAgB,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,QAAQ,CAenD"}