@signerreach/reserve 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.
- package/LICENSE +202 -0
- package/README.md +344 -2
- package/dist/account.d.ts +23 -0
- package/dist/account.js +86 -0
- package/dist/index.d.ts +3 -0
- package/dist/index.js +2 -0
- package/dist/plan.d.ts +20 -0
- package/dist/plan.js +158 -0
- package/dist/types.d.ts +106 -0
- package/dist/types.js +1 -0
- package/package.json +56 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
|
202
|
+
|
package/README.md
CHANGED
|
@@ -1,3 +1,345 @@
|
|
|
1
|
-
#
|
|
1
|
+
# reserve
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
**Experimental companion library to [SignerReach](https://github.com/signerreach/signerreach).
|
|
4
|
+
It is published to npm as an experimental `0.x` package so that it can be tried, and external
|
|
5
|
+
user demand has not yet been established. The project is being evaluated as a lightweight way
|
|
6
|
+
for TypeScript applications to check reserve and fee constraints before submitting supported
|
|
7
|
+
signer-change transactions.**
|
|
8
|
+
|
|
9
|
+
- [The problem](#the-problem)
|
|
10
|
+
- [What this project proposes](#what-this-project-proposes)
|
|
11
|
+
- [Scope and limitations](#scope-and-limitations)
|
|
12
|
+
- [Relationship to SignerReach](#relationship-to-signerreach)
|
|
13
|
+
- [Architecture](#architecture)
|
|
14
|
+
- [Install](#install)
|
|
15
|
+
- [Usage](#usage)
|
|
16
|
+
- [The rules, and how fees are modelled](#the-rules-and-how-fees-are-modelled)
|
|
17
|
+
- [What it does not do](#what-it-does-not-do)
|
|
18
|
+
- [Evidence and how much to trust it](#evidence-and-how-much-to-trust-it)
|
|
19
|
+
- [Known differences from the network](#known-differences-from-the-network)
|
|
20
|
+
- [Develop](#develop)
|
|
21
|
+
- [Status and licence](#status-and-licence)
|
|
22
|
+
|
|
23
|
+
## The problem
|
|
24
|
+
|
|
25
|
+
On Stellar, an account must keep a minimum balance that grows with every entry it holds:
|
|
26
|
+
signers, trust lines, offers and data entries each add one base reserve, and sponsorship
|
|
27
|
+
moves reserves between accounts. A transaction that adds a signer can therefore carry every
|
|
28
|
+
signature it needs and still fail, because the account cannot afford the extra reserve. The
|
|
29
|
+
network includes such a transaction in a ledger, fails it with `op_low_reserve`, and still
|
|
30
|
+
charges its fee.
|
|
31
|
+
|
|
32
|
+
This is not hypothetical. On Testnet on 2026-11-20, a plan that SignerReach's checker had
|
|
33
|
+
called safe failed this way at ledger 5831809 and paid its fee
|
|
34
|
+
([record](https://github.com/signerreach/signerreach/blob/main/docs/evidence/testnet-2026-11-20-reserve-gap/README.md)).
|
|
35
|
+
|
|
36
|
+
Whether a plan will be accepted depends on several figures that interact: the balance, the
|
|
37
|
+
minimum balance (sub-entries and sponsorship), selling liabilities, the fee, and the **order**
|
|
38
|
+
of the operations inside a transaction, since removing a signer before adding one can fit
|
|
39
|
+
where the reverse order does not. The arithmetic has confused people in public:
|
|
40
|
+
the Stellar documentation's sponsored-reserves formula was reported in 2026 to differ from
|
|
41
|
+
Stellar Core's by a selling-liabilities term
|
|
42
|
+
([stellar-docs#2771](https://github.com/stellar/stellar-docs/issues/2771), closed), and a
|
|
43
|
+
Freighter user reported 0 XLM available after adding 2 XLM to an account with no trust lines
|
|
44
|
+
or offers ([freighter#1879](https://github.com/stellar/freighter/issues/1879), closed). This
|
|
45
|
+
README does not claim more about those two reports than that they were made.
|
|
46
|
+
|
|
47
|
+
## What this project proposes
|
|
48
|
+
|
|
49
|
+
A small, dependency-free TypeScript function. Given an account's figures, read from one
|
|
50
|
+
ledger, and a plan of signer changes, it says for each transaction whether the network would
|
|
51
|
+
**accept** it, **reject** it before applying it without charging anything, or **include it and
|
|
52
|
+
fail it** while keeping the fee, together with the account that would result. It is meant to be
|
|
53
|
+
called before submitting, by wallets, bots and scripts that build signer changes.
|
|
54
|
+
|
|
55
|
+
That is a proposal, not an established need. No user has been identified, and whether the
|
|
56
|
+
check is worth having is what this project is being used to find out. What it can offer today
|
|
57
|
+
is a narrow model whose predictions have been compared with Testnet in 73 case runs (two
|
|
58
|
+
passes of 31 cases, one of 10, and a re-run of the one case that could not be used the first
|
|
59
|
+
time) and whose differences from the network are documented below.
|
|
60
|
+
|
|
61
|
+
## Scope and limitations
|
|
62
|
+
|
|
63
|
+
Reserve checks balance, fee, and reserve constraints for a limited set of Stellar
|
|
64
|
+
classic-account signer changes. It does not determine whether a transaction has sufficient
|
|
65
|
+
signatures, whether its signer configuration is safe, or whether the account will retain
|
|
66
|
+
administrative access. For authorization and signer-availability analysis, use SignerReach.
|
|
67
|
+
Neither tool guarantees that a transaction will succeed when eventually submitted. Account
|
|
68
|
+
state and network conditions can change after a check.
|
|
69
|
+
|
|
70
|
+
The model is narrow. It has been compared against Testnet observations, it differs from the
|
|
71
|
+
network in two documented ways, and some of its rules rest on the stellar-core source alone.
|
|
72
|
+
It does not claim to reproduce the network's behaviour in general. Details are in
|
|
73
|
+
[Evidence](#evidence-and-how-much-to-trust-it) and
|
|
74
|
+
[Known differences](#known-differences-from-the-network).
|
|
75
|
+
|
|
76
|
+
**Supported:** one classic account's native balance; `SetOptions` signer changes (add a
|
|
77
|
+
signer, change a signer's weight, remove a signer with weight 0) in transactions of 1 to 100
|
|
78
|
+
operations, with an optional fee bid.
|
|
79
|
+
|
|
80
|
+
## Relationship to SignerReach
|
|
81
|
+
|
|
82
|
+
Reserve is a companion TypeScript library implementing the reserve-and-fee portion of the
|
|
83
|
+
SignerReach model. SignerReach focuses on authorization and future administrative access,
|
|
84
|
+
while Reserve focuses on account balance, fee, and reserve constraints. The two projects have
|
|
85
|
+
related but different responsibilities:
|
|
86
|
+
|
|
87
|
+
| | SignerReach | Reserve |
|
|
88
|
+
|---|---|---|
|
|
89
|
+
| Question | Will the account stay administrable after this plan, under the signers you say are available? | Can the account pay the fee and the reserve for this plan, in the order the network applies it? |
|
|
90
|
+
| Looks at | Signers, weights, thresholds, signatures, availability | Balance, minimum balance, selling liabilities, fee, order of operations |
|
|
91
|
+
| Language | Rust (a command-line tool) | TypeScript (a library) |
|
|
92
|
+
| Evidence | Its own Testnet and Mainnet records | Agreement with SignerReach's published binary, plus its own Testnet records |
|
|
93
|
+
|
|
94
|
+
They share rules R10 to R14 of
|
|
95
|
+
[SignerReach's specification](https://github.com/signerreach/signerreach/blob/main/docs/SPEC.md).
|
|
96
|
+
A change to one of those rules is made there first (see [CONTRIBUTING.md](CONTRIBUTING.md)).
|
|
97
|
+
|
|
98
|
+
## Architecture
|
|
99
|
+
|
|
100
|
+
Reserve is a set of pure functions. It makes no network request, holds no key, signs and
|
|
101
|
+
submits nothing, and does not change the objects it is given. Everything it needs is passed in.
|
|
102
|
+
|
|
103
|
+
```mermaid
|
|
104
|
+
flowchart LR
|
|
105
|
+
H["Your code reads an account from Horizon, from one ledger"] --> A["Account: balance, liabilities, sub-entries, sponsorship, base reserve, signers"]
|
|
106
|
+
P["Your plan: transactions of signer changes, with an optional fee bid"] --> C
|
|
107
|
+
A --> C["checkPlan"]
|
|
108
|
+
C --> R["Per transaction: accepted, failed, rejected or unknown, plus the account afterwards"]
|
|
109
|
+
R --> D["Your code decides whether to submit"]
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
| File | Responsibility |
|
|
113
|
+
|---|---|
|
|
114
|
+
| `src/types.ts` | The data types: `Account`, `Transaction`, the four result types, `PlanResult`. |
|
|
115
|
+
| `src/account.ts` | The constants (1000 sub-entries, 20 signers, 100 stroops for each operation), `InputError`, `validateAccount`, `minimumBalance` and `available`. |
|
|
116
|
+
| `src/plan.ts` | `checkPlan`: evaluates a plan one transaction after another, in the order the network applies them. |
|
|
117
|
+
| `src/index.ts` | The public API. |
|
|
118
|
+
|
|
119
|
+
**What `checkPlan` does with each transaction.** It works on a copy of the account and, for
|
|
120
|
+
each transaction in turn:
|
|
121
|
+
|
|
122
|
+
1. If the fee is below 100 stroops for each operation, it returns `unknown` and stops.
|
|
123
|
+
2. If `available` is less than the fee bid, it returns `rejected` and charges nothing.
|
|
124
|
+
3. Otherwise it charges the fee, then applies the operations in order on a working copy. An
|
|
125
|
+
operation that cannot be applied makes the whole transaction `failed`: the fee stays
|
|
126
|
+
charged and none of its operations take effect. If every operation applies, the
|
|
127
|
+
transaction is `accepted` and its changes are kept.
|
|
128
|
+
4. The next transaction sees the balance and sub-entries the previous one left.
|
|
129
|
+
|
|
130
|
+
**Inputs are validated** and refused with an `InputError` if Stellar could not hold them:
|
|
131
|
+
negative amounts, liabilities above the balance, more than 1000 sub-entries or 20 signers, a
|
|
132
|
+
duplicated signer or the account's own key among the signers, a weight above 255, an empty
|
|
133
|
+
transaction, a fee outside 1 to 4294967295, or an amount that is not a `bigint`.
|
|
134
|
+
|
|
135
|
+
**How it is verified.** Each layer checks something the others do not:
|
|
136
|
+
|
|
137
|
+
| Layer | What it checks | Where |
|
|
138
|
+
|---|---|---|
|
|
139
|
+
| Rule tests | The rules, at their boundaries, with numbers worked out from the specification | `test/rules.test.ts` |
|
|
140
|
+
| Agreement vectors | That the library agrees with SignerReach's published binary, on 621 generated cases | `vectors/`, `test/vectors.test.ts` |
|
|
141
|
+
| Bug injection | That the tests would notice a wrong rule: 23 deliberate bugs, all caught | `scripts/mutation-check.mjs` |
|
|
142
|
+
| Testnet tool | That predictions match what the network does, with each prediction written before the plan is submitted | `tools/testnet/`, `docs/evidence/` |
|
|
143
|
+
| Package check | That the packed package installs and works: it is unpacked into a scratch project, used from JavaScript and type-checked from TypeScript (including a call that must be refused), and it fails if source, tests or anything unreviewed would be published | `scripts/pack-check.mjs` |
|
|
144
|
+
| CI | The rule tests and agreement vectors on Node 22 and 24, the bug injection, the vectors regenerated from the published Linux binary, the package check on Node 22 and 24, and the Testnet tool's unit tests and dry run. The Testnet runs themselves are not run in CI | `.github/workflows/ci.yml` |
|
|
145
|
+
|
|
146
|
+
`tools/testnet/compare.mjs` contains a helper that builds an `Account` from a Horizon account
|
|
147
|
+
record. It is part of the Testnet tool, not of the library.
|
|
148
|
+
|
|
149
|
+
## Install
|
|
150
|
+
|
|
151
|
+
```
|
|
152
|
+
npm install @signerreach/reserve
|
|
153
|
+
```
|
|
154
|
+
|
|
155
|
+
It has no dependencies. It is written in TypeScript and shipped compiled, with its type
|
|
156
|
+
declarations, as an ES module (`"type": "module"`), and it needs Node 22 or newer. From
|
|
157
|
+
CommonJS, load it with `await import("@signerreach/reserve")`.
|
|
158
|
+
|
|
159
|
+
It is a `0.x` release: the API may change between releases. The two differences from the
|
|
160
|
+
network described under [Known differences](#known-differences-from-the-network) are **not
|
|
161
|
+
fixed in 0.1.0**. Read them, and [Scope and limitations](#scope-and-limitations), before
|
|
162
|
+
relying on it.
|
|
163
|
+
|
|
164
|
+
## Usage
|
|
165
|
+
|
|
166
|
+
```ts
|
|
167
|
+
import { available, checkPlan, minimumBalance, type Account } from "@signerreach/reserve";
|
|
168
|
+
|
|
169
|
+
const account: Account = {
|
|
170
|
+
accountId: "GA...OWNER",
|
|
171
|
+
balance: 20_000_100n, // stroops
|
|
172
|
+
sellingLiabilities: 0n,
|
|
173
|
+
subentryCount: 1,
|
|
174
|
+
numSponsoring: 0,
|
|
175
|
+
numSponsored: 0,
|
|
176
|
+
baseReserve: 5_000_000n,
|
|
177
|
+
signers: ["GB...B"],
|
|
178
|
+
};
|
|
179
|
+
|
|
180
|
+
minimumBalance(account); // 15000000n
|
|
181
|
+
available(account); // 5000100n
|
|
182
|
+
|
|
183
|
+
const result = checkPlan(account, [
|
|
184
|
+
{ id: "add-c", operations: [{ key: "GC...C", weight: 1 }] },
|
|
185
|
+
]);
|
|
186
|
+
result.allAccepted; // true
|
|
187
|
+
result.account.available; // 0n: nothing is left to spend
|
|
188
|
+
|
|
189
|
+
// One stroop less: the network would include the transaction, fail it, and keep the fee.
|
|
190
|
+
const short = checkPlan({ ...account, balance: 20_000_099n }, [
|
|
191
|
+
{ id: "add-c", operations: [{ key: "GC...C", weight: 1 }] },
|
|
192
|
+
]);
|
|
193
|
+
short.transactions[0]; // { outcome: "failed", reason: "low_reserve", shortfall: 1n, feeCharged: 100n, ... }
|
|
194
|
+
```
|
|
195
|
+
|
|
196
|
+
(`examples/basic.ts` is this program, importing from the source tree instead of the package;
|
|
197
|
+
run it from a clone with `node examples/basic.ts`.)
|
|
198
|
+
|
|
199
|
+
Every amount is a `bigint` number of stroops. There are no floats anywhere, and a `number`
|
|
200
|
+
where an amount is expected is refused.
|
|
201
|
+
|
|
202
|
+
## The rules, and how fees are modelled
|
|
203
|
+
|
|
204
|
+
These are rules R10 to R14 of the
|
|
205
|
+
[SignerReach specification](https://github.com/signerreach/signerreach/blob/main/docs/SPEC.md):
|
|
206
|
+
|
|
207
|
+
- `minimumBalance = (2 + subentries + sponsoring - sponsored) * baseReserve`
|
|
208
|
+
- `available = balance - minimumBalance - sellingLiabilities` (it can be negative)
|
|
209
|
+
- **Fee first (R13):** if `available` is less than the fee bid, the transaction is `rejected`
|
|
210
|
+
(`tx_insufficient_balance`). Nothing is charged and nothing changes.
|
|
211
|
+
- **Failed transactions keep the fee (R12):** if it passes that check and then fails in an
|
|
212
|
+
operation, it is `failed`: the fee is charged and no operation takes effect.
|
|
213
|
+
- **Adding a signer (R11)** needs `available >= baseReserve`, checked after the 20-signer limit
|
|
214
|
+
and the 1000-sub-entry limit.
|
|
215
|
+
- **Changing the weight of an existing signer needs no reserve (R10).** Weight 0 removes the
|
|
216
|
+
signer and lowers the sub-entry count.
|
|
217
|
+
- **In order (R14):** operations apply one after another, so `[remove C, add D]` can fit where
|
|
218
|
+
`[add D, remove C]` does not.
|
|
219
|
+
|
|
220
|
+
**Fee bid and fee charged are different things.** A transaction states a fee *bid*. The model
|
|
221
|
+
does two things with it:
|
|
222
|
+
|
|
223
|
+
- *The check that the account can pay* is made against the **bid**. The Testnet runs observed
|
|
224
|
+
this: a bid of 101 stroops with 100 available was rejected, and a bid of 1000 stroops was
|
|
225
|
+
rejected with 999 available and accepted with exactly 1000.
|
|
226
|
+
- *The amount charged* is modelled as the **bid**, which is **not** what the network did. In
|
|
227
|
+
the three accepted cases whose bid was above the minimum (bids of 101 and 1000), the network
|
|
228
|
+
charged 100 stroops, which is 100 for each operation, not the bid. The library therefore
|
|
229
|
+
predicts a lower remaining balance than the network produced, by up to 900 stroops in those
|
|
230
|
+
cases.
|
|
231
|
+
|
|
232
|
+
In the cases observed this difference is conservative: the library predicted less balance than
|
|
233
|
+
the account had, never more. **Surge pricing was not tested.** Under surge the network could
|
|
234
|
+
charge more than 100 stroops for each operation, and whether the difference stays
|
|
235
|
+
conservative then has not been checked. With no explicit fee, the model assumes 100 stroops for
|
|
236
|
+
each operation, which matched what the network charged in every such case on Testnet.
|
|
237
|
+
|
|
238
|
+
A fee bid of less than 100 stroops for each operation gives `unknown`: the network's minimum
|
|
239
|
+
fee is a setting that can change, so the library does not claim to know, and evaluation stops
|
|
240
|
+
at that transaction.
|
|
241
|
+
|
|
242
|
+
## What it does not do
|
|
243
|
+
|
|
244
|
+
- **Authorization.** It does not check signatures, thresholds or whether anyone holds a key.
|
|
245
|
+
It assumes the plan is authorized. For that, use SignerReach.
|
|
246
|
+
- **Anything but signer changes.** Trust lines, offers, data entries, `createAccount`,
|
|
247
|
+
claimable balances, liquidity pools, sponsorship operations, fee bumps and Soroban fees are
|
|
248
|
+
not modelled. Each would need its own rule read from the stellar-core source, a Testnet run
|
|
249
|
+
at the boundary and a test before it could be added. (Trust lines, offers, data entries and
|
|
250
|
+
sponsorship appear in the Testnet records only as ways to put an account into a state before
|
|
251
|
+
the signer change is tested.)
|
|
252
|
+
- **Validate addresses.** Keys are compared as plain strings.
|
|
253
|
+
- **Fetch an account.** You give it the numbers. Reading them from Horizon is your code's job,
|
|
254
|
+
and the numbers must come from one ledger.
|
|
255
|
+
- **Say anything about the account now.** A balance can change between your check and the
|
|
256
|
+
submission.
|
|
257
|
+
|
|
258
|
+
## Evidence and how much to trust it
|
|
259
|
+
|
|
260
|
+
Not every rule has the same level of evidence. This table says which is which.
|
|
261
|
+
|
|
262
|
+
| Rule or behaviour | Level of evidence |
|
|
263
|
+
|---|---|
|
|
264
|
+
| The reserve, fee and operation-order rules at their boundaries (R10 to R14) | **Observed on Testnet.** SignerReach ran seven cases on 2026-11-23 ([record](https://github.com/signerreach/signerreach/blob/main/docs/evidence/testnet-2026-11-23-reserve-boundaries/README.md)); this library ran 31 cases twice on 2026-11-27 ([run 1](docs/evidence/testnet-2026-11-27/README.md), [run 2](docs/evidence/testnet-2026-11-27-run-2/README.md)) |
|
|
265
|
+
| The 20-signer limit is checked before the reserve | **Observed on Testnet**, one case (`cap-21st-lowbal`), the same result in both runs |
|
|
266
|
+
| The 1000-sub-entry limit is checked before the reserve | **Observed on Testnet**, one case with less than a reserve available (`sub1000-low`, 2026-11-28). A second case with plenty of balance (`sub1000-rich`) was unusable the first time because of a fault in the tool, and matched when run again the same day ([re-run](docs/evidence/testnet-2026-11-28-tier3-rerun/README.md)). This was previously from the stellar-core source only |
|
|
267
|
+
| The 1000th sub-entry is allowed; selling liabilities reduce the available balance by their amount; sponsorship moves the minimum by one reserve each way | **Observed on Testnet**, one exact-and-one-short pair of cases for each of the first two, and two pairs for sponsorship, one for each direction ([tier 3](docs/evidence/testnet-2026-11-28-tier3/README.md)) |
|
|
268
|
+
| Which of the 20-signer limit and the 1000-sub-entry limit is checked first | **Source inspection only.** Not tested on any network |
|
|
269
|
+
| Base reserves other than 5,000,000 stroops; sponsorship of more than one entry or of entries other than data entries | **Source inspection only.** Not tested on any network |
|
|
270
|
+
| An explicit fee bid is the amount charged | **Contradicted by Testnet**: three cases, the same three in both runs (see below) |
|
|
271
|
+
| The account's own key as a signer fails in a ledger and keeps the fee | **Contradicted by Testnet**: two cases (adding and removing the key), the same two in both runs (see below) |
|
|
272
|
+
|
|
273
|
+
SignerReach's specification still lists some of the tier-3 rules as source-only; it has not
|
|
274
|
+
yet been updated to reflect these records.
|
|
275
|
+
|
|
276
|
+
**What the library's own Testnet runs showed.** Two runs of 31 cases each on 2026-11-27 at
|
|
277
|
+
protocol 30: **25 matched, 5 differed, 1 was an observation only, none unusable, the same in
|
|
278
|
+
both.** One pass of 10 tier-3 cases on 2026-11-28: **9 matched, none differed, 1 unusable**; the
|
|
279
|
+
unusable case was run again the same day and **matched**.
|
|
280
|
+
In every run each prediction was written before the plan was submitted. In run 2 and in tier 3
|
|
281
|
+
the account's balance also confirmed the fee of every transaction; run 1 did not record it.
|
|
282
|
+
These are repetitions on one network state with no surge pricing, not confirmation across
|
|
283
|
+
conditions.
|
|
284
|
+
|
|
285
|
+
**What agreement with SignerReach means.** `vectors/oracle.json` holds 621 cases generated by
|
|
286
|
+
running the **published v0.1.2 binary**, and the library agrees with it on every result, fee,
|
|
287
|
+
available balance and code. These are two implementations of the same rules by the same
|
|
288
|
+
author. Agreement is a consistency check between them. **It is not independent verification
|
|
289
|
+
against the network.**
|
|
290
|
+
|
|
291
|
+
**What the bug injection means.** 23 deliberate bugs injected into the rules were all caught
|
|
292
|
+
(`node scripts/mutation-check.mjs`). Bugs outside those 23 may not be.
|
|
293
|
+
|
|
294
|
+
Known difference from the binary: for a fee below the minimum, the binary rejects the whole
|
|
295
|
+
input and evaluates nothing, while this library evaluates the earlier transactions and stops
|
|
296
|
+
at that one. Shortfall amounts are checked by the hand-written tests only, because the binary
|
|
297
|
+
reports them in message text and not as a field.
|
|
298
|
+
|
|
299
|
+
## Known differences from the network
|
|
300
|
+
|
|
301
|
+
Found by the Testnet run of 2026-11-27, and the same in both runs. They are **not fixed**.
|
|
302
|
+
They are wrong in the library and in SignerReach's specification alike. Under this project's
|
|
303
|
+
contribution rule (see [CONTRIBUTING.md](CONTRIBUTING.md)), a rule changes in SignerReach's
|
|
304
|
+
specification, code and release first, and only then here, so that the library keeps agreeing
|
|
305
|
+
with the published binary.
|
|
306
|
+
|
|
307
|
+
1. **An explicit fee bid is treated as the amount charged.** The network checks the bid, as
|
|
308
|
+
the library does, but charged 100 stroops for each operation and not the bid (three cases,
|
|
309
|
+
bids of 101 and 1000). The balance the library predicts afterwards is up to 900 stroops too
|
|
310
|
+
low in those cases. In the cases observed that is the conservative direction: a later
|
|
311
|
+
transaction in the same plan could be predicted rejected when the network would accept it.
|
|
312
|
+
Surge pricing was not tested. With no explicit fee the difference did not arise.
|
|
313
|
+
2. **The account's own key named as a signer is predicted to fail in a ledger and keep its
|
|
314
|
+
fee.** The network refused it at submission (`op_bad_signer`), with no ledger record after
|
|
315
|
+
twelve lookups and an unchanged balance, so no fee was charged. This was the same for adding
|
|
316
|
+
and for removing the key, in both runs. The verdict, unsafe, is the same. The balance
|
|
317
|
+
afterwards is not.
|
|
318
|
+
|
|
319
|
+
## Develop
|
|
320
|
+
|
|
321
|
+
Node 22.18 or newer (it runs TypeScript directly).
|
|
322
|
+
|
|
323
|
+
```
|
|
324
|
+
npm ci
|
|
325
|
+
npm run typecheck
|
|
326
|
+
npm test # 26 rule tests + 621 agreement cases
|
|
327
|
+
npm run build
|
|
328
|
+
npm run pack:check # packs it, installs it in a scratch project, uses it from JS and TS
|
|
329
|
+
node scripts/mutation-check.mjs
|
|
330
|
+
```
|
|
331
|
+
|
|
332
|
+
To regenerate or re-check the vectors, you need the published binary. See
|
|
333
|
+
[vectors/README.md](vectors/README.md).
|
|
334
|
+
|
|
335
|
+
[tools/testnet](tools/testnet/README.md) runs the library's predictions against Stellar
|
|
336
|
+
Testnet and records whether the network agrees. Its records so far:
|
|
337
|
+
[run 1](docs/evidence/testnet-2026-11-27/README.md) and, with a corrected tool,
|
|
338
|
+
[run 2](docs/evidence/testnet-2026-11-27-run-2/README.md), both tiers 1 and 2;
|
|
339
|
+
[tier 3](docs/evidence/testnet-2026-11-28-tier3/README.md); and the
|
|
340
|
+
[re-run of its one unusable case](docs/evidence/testnet-2026-11-28-tier3-rerun/README.md).
|
|
341
|
+
|
|
342
|
+
## Status and licence
|
|
343
|
+
|
|
344
|
+
Experimental. Version `0.1.0`, published to npm as `@signerreach/reserve`. Unaudited.
|
|
345
|
+
Apache-2.0. Not affiliated with the Stellar Development Foundation.
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { Account, Stroops } from "./types.ts";
|
|
2
|
+
/** The most sub-entries an account can hold. */
|
|
3
|
+
export declare const SUB_ENTRY_LIMIT = 1000;
|
|
4
|
+
/** The most additional signers an account can hold. */
|
|
5
|
+
export declare const MAX_ADDITIONAL_SIGNERS = 20;
|
|
6
|
+
/** The fee assumed for each operation when a transaction gives none, in stroops. */
|
|
7
|
+
export declare const ASSUMED_FEE_PER_OPERATION: Stroops;
|
|
8
|
+
/** The largest amount Stellar can hold (a signed 64-bit integer). */
|
|
9
|
+
export declare const MAX_AMOUNT: Stroops;
|
|
10
|
+
/** The largest fee a transaction can carry (an unsigned 32-bit integer). */
|
|
11
|
+
export declare const MAX_FEE: Stroops;
|
|
12
|
+
/** The largest signer weight. */
|
|
13
|
+
export declare const MAX_WEIGHT = 255;
|
|
14
|
+
/** Thrown for input that Stellar could not hold, or that this library refuses to guess about. */
|
|
15
|
+
export declare class InputError extends Error {
|
|
16
|
+
constructor(message: string);
|
|
17
|
+
}
|
|
18
|
+
/** Refuses an account Stellar could not hold. Returns the account unchanged. */
|
|
19
|
+
export declare function validateAccount(account: Account): Account;
|
|
20
|
+
/** `(2 + subentries + sponsoring - sponsored) * baseReserve`. */
|
|
21
|
+
export declare function minimumBalance(account: Pick<Account, "subentryCount" | "numSponsoring" | "numSponsored" | "baseReserve">): Stroops;
|
|
22
|
+
/** `balance - minimumBalance - sellingLiabilities`. Negative when the account is below its minimum. */
|
|
23
|
+
export declare function available(account: Pick<Account, "balance" | "sellingLiabilities" | "subentryCount" | "numSponsoring" | "numSponsored" | "baseReserve">): Stroops;
|
package/dist/account.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/** The most sub-entries an account can hold. */
|
|
2
|
+
export const SUB_ENTRY_LIMIT = 1000;
|
|
3
|
+
/** The most additional signers an account can hold. */
|
|
4
|
+
export const MAX_ADDITIONAL_SIGNERS = 20;
|
|
5
|
+
/** The fee assumed for each operation when a transaction gives none, in stroops. */
|
|
6
|
+
export const ASSUMED_FEE_PER_OPERATION = 100n;
|
|
7
|
+
/** The largest amount Stellar can hold (a signed 64-bit integer). */
|
|
8
|
+
export const MAX_AMOUNT = 2n ** 63n - 1n;
|
|
9
|
+
/** The largest fee a transaction can carry (an unsigned 32-bit integer). */
|
|
10
|
+
export const MAX_FEE = 4294967295n;
|
|
11
|
+
/** The largest signer weight. */
|
|
12
|
+
export const MAX_WEIGHT = 255;
|
|
13
|
+
/** Thrown for input that Stellar could not hold, or that this library refuses to guess about. */
|
|
14
|
+
export class InputError extends Error {
|
|
15
|
+
constructor(message) {
|
|
16
|
+
super(message);
|
|
17
|
+
this.name = "InputError";
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
function isCount(value) {
|
|
21
|
+
return typeof value === "number" && Number.isSafeInteger(value) && value >= 0 && value <= 4294967295;
|
|
22
|
+
}
|
|
23
|
+
/** Refuses an account Stellar could not hold. Returns the account unchanged. */
|
|
24
|
+
export function validateAccount(account) {
|
|
25
|
+
if (typeof account.accountId !== "string" || account.accountId === "") {
|
|
26
|
+
throw new InputError("accountId must be a non-empty string");
|
|
27
|
+
}
|
|
28
|
+
for (const [name, value] of [
|
|
29
|
+
["balance", account.balance],
|
|
30
|
+
["sellingLiabilities", account.sellingLiabilities],
|
|
31
|
+
["baseReserve", account.baseReserve],
|
|
32
|
+
]) {
|
|
33
|
+
if (typeof value !== "bigint") {
|
|
34
|
+
throw new InputError(`${name} must be a bigint number of stroops`);
|
|
35
|
+
}
|
|
36
|
+
if (value < 0n)
|
|
37
|
+
throw new InputError(`${name} is negative`);
|
|
38
|
+
if (value > MAX_AMOUNT)
|
|
39
|
+
throw new InputError(`${name} is more than a signed 64-bit integer`);
|
|
40
|
+
}
|
|
41
|
+
if (account.sellingLiabilities > account.balance) {
|
|
42
|
+
throw new InputError("the selling liabilities are more than the balance");
|
|
43
|
+
}
|
|
44
|
+
if (account.baseReserve < 1n)
|
|
45
|
+
throw new InputError("the base reserve is not above 0");
|
|
46
|
+
for (const [name, value] of [
|
|
47
|
+
["subentryCount", account.subentryCount],
|
|
48
|
+
["numSponsoring", account.numSponsoring],
|
|
49
|
+
["numSponsored", account.numSponsored],
|
|
50
|
+
]) {
|
|
51
|
+
if (!isCount(value))
|
|
52
|
+
throw new InputError(`${name} must be a whole number`);
|
|
53
|
+
}
|
|
54
|
+
if (account.subentryCount > SUB_ENTRY_LIMIT) {
|
|
55
|
+
throw new InputError("the sub-entry count is above 1000");
|
|
56
|
+
}
|
|
57
|
+
if (2 + account.subentryCount + account.numSponsoring - account.numSponsored < 0) {
|
|
58
|
+
throw new InputError("the sponsorship counts leave a negative number of reserved entries");
|
|
59
|
+
}
|
|
60
|
+
if (account.signers.length > MAX_ADDITIONAL_SIGNERS) {
|
|
61
|
+
throw new InputError("an account holds at most 20 additional signers");
|
|
62
|
+
}
|
|
63
|
+
if (account.subentryCount < account.signers.length) {
|
|
64
|
+
throw new InputError("the sub-entry count is below the number of additional signers");
|
|
65
|
+
}
|
|
66
|
+
const seen = new Set();
|
|
67
|
+
for (const key of account.signers) {
|
|
68
|
+
if (typeof key !== "string" || key === "")
|
|
69
|
+
throw new InputError("a signer key must be a non-empty string");
|
|
70
|
+
if (key === account.accountId)
|
|
71
|
+
throw new InputError("the account's own key is not an additional signer");
|
|
72
|
+
if (seen.has(key))
|
|
73
|
+
throw new InputError("a signer key is listed twice");
|
|
74
|
+
seen.add(key);
|
|
75
|
+
}
|
|
76
|
+
return account;
|
|
77
|
+
}
|
|
78
|
+
/** `(2 + subentries + sponsoring - sponsored) * baseReserve`. */
|
|
79
|
+
export function minimumBalance(account) {
|
|
80
|
+
const entries = 2 + account.subentryCount + account.numSponsoring - account.numSponsored;
|
|
81
|
+
return BigInt(entries) * account.baseReserve;
|
|
82
|
+
}
|
|
83
|
+
/** `balance - minimumBalance - sellingLiabilities`. Negative when the account is below its minimum. */
|
|
84
|
+
export function available(account) {
|
|
85
|
+
return account.balance - minimumBalance(account) - account.sellingLiabilities;
|
|
86
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export { ASSUMED_FEE_PER_OPERATION, InputError, MAX_ADDITIONAL_SIGNERS, MAX_AMOUNT, MAX_FEE, MAX_WEIGHT, SUB_ENTRY_LIMIT, available, minimumBalance, validateAccount, } from "./account.ts";
|
|
2
|
+
export { checkPlan } from "./plan.ts";
|
|
3
|
+
export type { Account, AccountAfter, AcceptedResult, FailedResult, FailureReason, PlanResult, RejectedResult, SignerOperation, Stroops, Transaction, TransactionResult, UnknownResult, } from "./types.ts";
|
package/dist/index.js
ADDED
package/dist/plan.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Account, PlanResult, Transaction } from "./types.ts";
|
|
2
|
+
/**
|
|
3
|
+
* Evaluates a plan of signer changes against an account's balance, one
|
|
4
|
+
* transaction after another, in the order the network applies them.
|
|
5
|
+
*
|
|
6
|
+
* What it models (rules R10 to R14 of the SignerReach specification):
|
|
7
|
+
* - R13: before a transaction is applied, `available >= fee` must hold, else it
|
|
8
|
+
* is rejected and nothing is charged.
|
|
9
|
+
* - R12: a transaction that passes and then fails in an operation is included in
|
|
10
|
+
* a ledger and keeps its fee; none of its operations take effect.
|
|
11
|
+
* - R11, R10: adding a new signer needs one base reserve of available balance;
|
|
12
|
+
* changing the weight of an existing signer needs none.
|
|
13
|
+
* - R14: operations apply in order against the state earlier ones left.
|
|
14
|
+
*
|
|
15
|
+
* What it does not model: signatures and thresholds (it assumes the plan is
|
|
16
|
+
* authorized), any operation other than a signer change, fee bumps,
|
|
17
|
+
* preconditions, and sponsorship operations. The network's minimum fee is not
|
|
18
|
+
* known to it: a fee below 100 stroops for each operation gives `unknown`.
|
|
19
|
+
*/
|
|
20
|
+
export declare function checkPlan(account: Account, transactions: readonly Transaction[]): PlanResult;
|
package/dist/plan.js
ADDED
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { ASSUMED_FEE_PER_OPERATION, InputError, MAX_ADDITIONAL_SIGNERS, MAX_FEE, MAX_WEIGHT, SUB_ENTRY_LIMIT, available, minimumBalance, validateAccount, } from "./account.js";
|
|
2
|
+
function validateTransaction(tx, position) {
|
|
3
|
+
if (tx.operations.length === 0) {
|
|
4
|
+
throw new InputError(`transaction ${position}: a transaction needs at least one operation`);
|
|
5
|
+
}
|
|
6
|
+
if (tx.operations.length > 100) {
|
|
7
|
+
throw new InputError(`transaction ${position}: a transaction holds at most 100 operations`);
|
|
8
|
+
}
|
|
9
|
+
if (tx.fee !== undefined) {
|
|
10
|
+
if (typeof tx.fee !== "bigint") {
|
|
11
|
+
throw new InputError(`transaction ${position}: fee must be a bigint number of stroops`);
|
|
12
|
+
}
|
|
13
|
+
if (tx.fee < 1n || tx.fee > MAX_FEE) {
|
|
14
|
+
throw new InputError(`transaction ${position}: fee must be from 1 to 4294967295 stroops`);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
for (const op of tx.operations) {
|
|
18
|
+
if (typeof op.key !== "string" || op.key === "") {
|
|
19
|
+
throw new InputError(`transaction ${position}: a signer key must be a non-empty string`);
|
|
20
|
+
}
|
|
21
|
+
if (!Number.isInteger(op.weight) || op.weight < 0 || op.weight > MAX_WEIGHT) {
|
|
22
|
+
throw new InputError(`transaction ${position}: a signer weight is a whole number from 0 to 255`);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
function snapshot(account, state) {
|
|
27
|
+
return {
|
|
28
|
+
balance: state.balance,
|
|
29
|
+
sellingLiabilities: account.sellingLiabilities,
|
|
30
|
+
subentryCount: state.subentryCount,
|
|
31
|
+
numSponsoring: account.numSponsoring,
|
|
32
|
+
numSponsored: account.numSponsored,
|
|
33
|
+
baseReserve: account.baseReserve,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Applies one operation to `state` or says why it cannot be applied (R10, R11).
|
|
38
|
+
* The caller discards `state` when this returns a reason.
|
|
39
|
+
*/
|
|
40
|
+
function applyOperation(account, state, op) {
|
|
41
|
+
if (op.key === account.accountId)
|
|
42
|
+
return { reason: "bad_signer", shortfall: 0n };
|
|
43
|
+
const present = state.signers.has(op.key);
|
|
44
|
+
if (op.weight === 0) {
|
|
45
|
+
// R10: weight 0 removes a signer; for a key that is not a signer it changes nothing.
|
|
46
|
+
if (present) {
|
|
47
|
+
state.signers.delete(op.key);
|
|
48
|
+
state.subentryCount -= 1;
|
|
49
|
+
}
|
|
50
|
+
return undefined;
|
|
51
|
+
}
|
|
52
|
+
// R10: a key that is already a signer only changes weight and needs no reserve.
|
|
53
|
+
if (present)
|
|
54
|
+
return undefined;
|
|
55
|
+
// R11, in this order: signer limit, sub-entry limit, reserve.
|
|
56
|
+
if (state.signers.size >= MAX_ADDITIONAL_SIGNERS) {
|
|
57
|
+
return { reason: "too_many_signers", shortfall: 0n };
|
|
58
|
+
}
|
|
59
|
+
if (state.subentryCount >= SUB_ENTRY_LIMIT) {
|
|
60
|
+
return { reason: "too_many_subentries", shortfall: 0n };
|
|
61
|
+
}
|
|
62
|
+
const room = available(snapshot(account, state));
|
|
63
|
+
if (room < account.baseReserve) {
|
|
64
|
+
return { reason: "low_reserve", shortfall: account.baseReserve - room };
|
|
65
|
+
}
|
|
66
|
+
state.signers.add(op.key);
|
|
67
|
+
state.subentryCount += 1;
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Evaluates a plan of signer changes against an account's balance, one
|
|
72
|
+
* transaction after another, in the order the network applies them.
|
|
73
|
+
*
|
|
74
|
+
* What it models (rules R10 to R14 of the SignerReach specification):
|
|
75
|
+
* - R13: before a transaction is applied, `available >= fee` must hold, else it
|
|
76
|
+
* is rejected and nothing is charged.
|
|
77
|
+
* - R12: a transaction that passes and then fails in an operation is included in
|
|
78
|
+
* a ledger and keeps its fee; none of its operations take effect.
|
|
79
|
+
* - R11, R10: adding a new signer needs one base reserve of available balance;
|
|
80
|
+
* changing the weight of an existing signer needs none.
|
|
81
|
+
* - R14: operations apply in order against the state earlier ones left.
|
|
82
|
+
*
|
|
83
|
+
* What it does not model: signatures and thresholds (it assumes the plan is
|
|
84
|
+
* authorized), any operation other than a signer change, fee bumps,
|
|
85
|
+
* preconditions, and sponsorship operations. The network's minimum fee is not
|
|
86
|
+
* known to it: a fee below 100 stroops for each operation gives `unknown`.
|
|
87
|
+
*/
|
|
88
|
+
export function checkPlan(account, transactions) {
|
|
89
|
+
validateAccount(account);
|
|
90
|
+
transactions.forEach((tx, i) => validateTransaction(tx, i));
|
|
91
|
+
const state = {
|
|
92
|
+
balance: account.balance,
|
|
93
|
+
subentryCount: account.subentryCount,
|
|
94
|
+
signers: new Set(account.signers),
|
|
95
|
+
};
|
|
96
|
+
const results = [];
|
|
97
|
+
let complete = true;
|
|
98
|
+
for (const [index, tx] of transactions.entries()) {
|
|
99
|
+
const assumedMinimum = ASSUMED_FEE_PER_OPERATION * BigInt(tx.operations.length);
|
|
100
|
+
const feeAssumed = tx.fee === undefined;
|
|
101
|
+
const fee = tx.fee ?? assumedMinimum;
|
|
102
|
+
const availableBefore = available(snapshot(account, state));
|
|
103
|
+
const base = { index, id: tx.id, fee, feeAssumed, availableBefore };
|
|
104
|
+
if (fee < assumedMinimum) {
|
|
105
|
+
results.push({ ...base, outcome: "unknown", reason: "fee_below_assumed_minimum", feeCharged: 0n });
|
|
106
|
+
complete = false;
|
|
107
|
+
break;
|
|
108
|
+
}
|
|
109
|
+
// R13: checked before the transaction is applied; nothing is charged on failure.
|
|
110
|
+
if (availableBefore < fee) {
|
|
111
|
+
results.push({
|
|
112
|
+
...base,
|
|
113
|
+
outcome: "rejected",
|
|
114
|
+
reason: "insufficient_balance",
|
|
115
|
+
feeCharged: 0n,
|
|
116
|
+
shortfall: fee - availableBefore,
|
|
117
|
+
});
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
// The fee is charged first, so the operations see the balance after it (R13).
|
|
121
|
+
state.balance -= fee;
|
|
122
|
+
const working = {
|
|
123
|
+
balance: state.balance,
|
|
124
|
+
subentryCount: state.subentryCount,
|
|
125
|
+
signers: new Set(state.signers),
|
|
126
|
+
};
|
|
127
|
+
let failure;
|
|
128
|
+
for (const [operationIndex, op] of tx.operations.entries()) {
|
|
129
|
+
const problem = applyOperation(account, working, op);
|
|
130
|
+
if (problem) {
|
|
131
|
+
failure = { ...problem, operationIndex };
|
|
132
|
+
break;
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
if (failure) {
|
|
136
|
+
// R12: included in a ledger, keeps its fee, no operation takes effect.
|
|
137
|
+
results.push({ ...base, outcome: "failed", feeCharged: fee, ...failure });
|
|
138
|
+
}
|
|
139
|
+
else {
|
|
140
|
+
state.subentryCount = working.subentryCount;
|
|
141
|
+
state.signers = working.signers;
|
|
142
|
+
results.push({ ...base, outcome: "accepted", feeCharged: fee });
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
const final = snapshot(account, state);
|
|
146
|
+
return {
|
|
147
|
+
transactions: results,
|
|
148
|
+
account: {
|
|
149
|
+
balance: state.balance,
|
|
150
|
+
subentryCount: state.subentryCount,
|
|
151
|
+
signers: [...state.signers],
|
|
152
|
+
minimumBalance: minimumBalance(final),
|
|
153
|
+
available: available(final),
|
|
154
|
+
},
|
|
155
|
+
complete,
|
|
156
|
+
allAccepted: complete && results.every((r) => r.outcome === "accepted"),
|
|
157
|
+
};
|
|
158
|
+
}
|
package/dist/types.d.ts
ADDED
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
/** Every amount is a whole number of stroops (1 XLM = 10,000,000 stroops). No decimals, no floats. */
|
|
2
|
+
export type Stroops = bigint;
|
|
3
|
+
/** The balance side of one classic account at one moment. */
|
|
4
|
+
export interface Account {
|
|
5
|
+
/**
|
|
6
|
+
* The account's own G address. Used only to refuse a plan that names the
|
|
7
|
+
* account's own key as a signer. Keys are compared as plain strings; this
|
|
8
|
+
* library does not check that an address is well formed.
|
|
9
|
+
*/
|
|
10
|
+
readonly accountId: string;
|
|
11
|
+
/** Native balance. */
|
|
12
|
+
readonly balance: Stroops;
|
|
13
|
+
/** Native selling liabilities (open offers). */
|
|
14
|
+
readonly sellingLiabilities: Stroops;
|
|
15
|
+
/** All sub-entries: additional signers, trust lines, offers and data entries. */
|
|
16
|
+
readonly subentryCount: number;
|
|
17
|
+
readonly numSponsoring: number;
|
|
18
|
+
readonly numSponsored: number;
|
|
19
|
+
/** The network's base reserve at the ledger the figures come from. */
|
|
20
|
+
readonly baseReserve: Stroops;
|
|
21
|
+
/** Keys of the account's additional signers (not the account's own key). */
|
|
22
|
+
readonly signers: readonly string[];
|
|
23
|
+
}
|
|
24
|
+
/** One signer change, as `SetOptions` makes it. Weight 0 removes the signer. */
|
|
25
|
+
export interface SignerOperation {
|
|
26
|
+
readonly key: string;
|
|
27
|
+
/** A whole number from 0 to 255. */
|
|
28
|
+
readonly weight: number;
|
|
29
|
+
}
|
|
30
|
+
export interface Transaction {
|
|
31
|
+
readonly id?: string;
|
|
32
|
+
/**
|
|
33
|
+
* The whole fee of the transaction in stroops, 1 to 4294967295. When absent,
|
|
34
|
+
* 100 stroops for each operation is assumed and the result says so.
|
|
35
|
+
*/
|
|
36
|
+
readonly fee?: Stroops;
|
|
37
|
+
readonly operations: readonly SignerOperation[];
|
|
38
|
+
}
|
|
39
|
+
/** Why an operation cannot be applied (the transaction then fails but keeps its fee). */
|
|
40
|
+
export type FailureReason = "bad_signer" | "too_many_signers" | "too_many_subentries" | "low_reserve";
|
|
41
|
+
interface TransactionResultBase {
|
|
42
|
+
readonly index: number;
|
|
43
|
+
readonly id: string | undefined;
|
|
44
|
+
/** The fee that was used: the given one, or the assumed one. */
|
|
45
|
+
readonly fee: Stroops;
|
|
46
|
+
readonly feeAssumed: boolean;
|
|
47
|
+
/** `balance - minimumBalance - sellingLiabilities` before the transaction. May be negative. */
|
|
48
|
+
readonly availableBefore: Stroops;
|
|
49
|
+
}
|
|
50
|
+
/** Applied. The fee was charged and every operation took effect. */
|
|
51
|
+
export interface AcceptedResult extends TransactionResultBase {
|
|
52
|
+
readonly outcome: "accepted";
|
|
53
|
+
readonly feeCharged: Stroops;
|
|
54
|
+
}
|
|
55
|
+
/**
|
|
56
|
+
* Included in a ledger, then failed in an operation: nothing took effect
|
|
57
|
+
* except that the fee was charged (`op_low_reserve` and the like).
|
|
58
|
+
*/
|
|
59
|
+
export interface FailedResult extends TransactionResultBase {
|
|
60
|
+
readonly outcome: "failed";
|
|
61
|
+
readonly feeCharged: Stroops;
|
|
62
|
+
readonly reason: FailureReason;
|
|
63
|
+
/** Index of the operation that failed. */
|
|
64
|
+
readonly operationIndex: number;
|
|
65
|
+
/** For `low_reserve`: stroops short of one base reserve. Otherwise 0n. */
|
|
66
|
+
readonly shortfall: Stroops;
|
|
67
|
+
}
|
|
68
|
+
/**
|
|
69
|
+
* Refused before it is applied (`tx_insufficient_balance`): the fee is more
|
|
70
|
+
* than the available balance. Nothing is charged and nothing changes.
|
|
71
|
+
*/
|
|
72
|
+
export interface RejectedResult extends TransactionResultBase {
|
|
73
|
+
readonly outcome: "rejected";
|
|
74
|
+
readonly feeCharged: 0n;
|
|
75
|
+
readonly reason: "insufficient_balance";
|
|
76
|
+
/** `fee - availableBefore`. */
|
|
77
|
+
readonly shortfall: Stroops;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* The library does not claim to know. Only one case: a fee below 100 stroops
|
|
81
|
+
* for each operation, because the network's minimum fee is a setting that can
|
|
82
|
+
* change. Evaluation stops here; later transactions are not evaluated.
|
|
83
|
+
*/
|
|
84
|
+
export interface UnknownResult extends TransactionResultBase {
|
|
85
|
+
readonly outcome: "unknown";
|
|
86
|
+
readonly reason: "fee_below_assumed_minimum";
|
|
87
|
+
readonly feeCharged: 0n;
|
|
88
|
+
}
|
|
89
|
+
export type TransactionResult = AcceptedResult | FailedResult | RejectedResult | UnknownResult;
|
|
90
|
+
/** The account after the transactions that were evaluated. */
|
|
91
|
+
export interface AccountAfter {
|
|
92
|
+
readonly balance: Stroops;
|
|
93
|
+
readonly subentryCount: number;
|
|
94
|
+
readonly signers: readonly string[];
|
|
95
|
+
readonly minimumBalance: Stroops;
|
|
96
|
+
readonly available: Stroops;
|
|
97
|
+
}
|
|
98
|
+
export interface PlanResult {
|
|
99
|
+
readonly transactions: readonly TransactionResult[];
|
|
100
|
+
readonly account: AccountAfter;
|
|
101
|
+
/** False when evaluation stopped at an `unknown` transaction. */
|
|
102
|
+
readonly complete: boolean;
|
|
103
|
+
/** True only when `complete` and every transaction was accepted. */
|
|
104
|
+
readonly allAccepted: boolean;
|
|
105
|
+
}
|
|
106
|
+
export {};
|
package/dist/types.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,58 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@signerreach/reserve",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Experimental: check whether a Stellar classic account can pay the fee and the reserve for a plan of signer changes, in the order the network applies them.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"keywords": [
|
|
8
|
+
"stellar",
|
|
9
|
+
"signer",
|
|
10
|
+
"reserve",
|
|
11
|
+
"fee",
|
|
12
|
+
"multisig",
|
|
13
|
+
"typescript"
|
|
14
|
+
],
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/signerreach/reserve.git"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/signerreach/reserve/issues"
|
|
21
|
+
},
|
|
22
|
+
"homepage": "https://github.com/signerreach/reserve#readme",
|
|
23
|
+
"engines": {
|
|
24
|
+
"node": ">=22"
|
|
25
|
+
},
|
|
26
|
+
"sideEffects": false,
|
|
27
|
+
"main": "./dist/index.js",
|
|
28
|
+
"types": "./dist/index.d.ts",
|
|
29
|
+
"exports": {
|
|
30
|
+
".": {
|
|
31
|
+
"types": "./dist/index.d.ts",
|
|
32
|
+
"default": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"./package.json": "./package.json"
|
|
35
|
+
},
|
|
36
|
+
"files": [
|
|
37
|
+
"dist",
|
|
38
|
+
"LICENSE",
|
|
39
|
+
"README.md"
|
|
40
|
+
],
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"scripts": {
|
|
45
|
+
"typecheck": "tsc --noEmit",
|
|
46
|
+
"prebuild": "node -e \"require('node:fs').rmSync('dist',{recursive:true,force:true})\"",
|
|
47
|
+
"build": "tsc -p tsconfig.build.json",
|
|
48
|
+
"test": "node --test --test-reporter=spec \"test/**/*.test.ts\"",
|
|
49
|
+
"vectors": "node scripts/generate-vectors.mjs",
|
|
50
|
+
"vectors:check": "node scripts/generate-vectors.mjs --check",
|
|
51
|
+
"pack:check": "node scripts/pack-check.mjs",
|
|
52
|
+
"prepublishOnly": "npm run typecheck && npm run build && npm test && npm run pack:check"
|
|
53
|
+
},
|
|
54
|
+
"devDependencies": {
|
|
55
|
+
"@types/node": "24.19.4",
|
|
56
|
+
"typescript": "5.9.3"
|
|
57
|
+
}
|
|
58
|
+
}
|