FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
JavaScriptSolidServer/src/webledger.js at gh-pages · JavaScriptSolidServer/JavaScriptSolidServer · GitHub
JavaScriptSolidServer
JavaScriptSolidServer
Repository navigation
Code
Issues
164
(164)
Pull requests
14
(14)
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
JavaScriptSolidServer
/
src
/
webledger.js
Copy path
More file actions
More file actions
Latest commit
History
History
History
227 lines (214 loc) · 7.78 KB
Breadcrumbs
JavaScriptSolidServer
/
src
/
webledger.js
Copy path
File metadata and controls
227 lines (214 loc) · 7.78 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/**
* Web Ledger — spec-compliant balance tracking
*
* Implements the Web Ledgers specification (https://webledgers.org/)
* for mapping URIs to numerical balances. Default unit: satoshi.
*
* JSON-LD context: https://w3id.org/webledgers
*
*
@module
webledger
*/
import
*
as
storage
from
'./storage/filesystem.js'
;
const
DEFAULT_PATH
=
'/.well-known/webledgers/webledgers.json'
;
const
CONTEXT
=
'https://w3id.org/webledgers'
;
/**
* Create an empty spec-compliant ledger
*
@param
{
object
} options
*
@param
{
string
} options.name - Ledger name
*
@param
{
string
} options.description - Ledger description
*
@param
{
string
} options.id - Ledger URI identifier
*
@param
{
string
} options.defaultCurrency - Default currency (default 'satoshi')
*
@returns
{
object
} Empty WebLedger object
*/
export
function
createLedger
(
options
=
{
}
)
{
const
now
=
Math
.
floor
(
Date
.
now
(
)
/
1000
)
;
return
{
'@context'
:
CONTEXT
,
type
:
'WebLedger'
,
...
(
options
.
id
&&
{
id
:
options
.
id
}
)
,
name
:
options
.
name
??
'Pod Credits'
,
description
:
options
.
description
??
'Paid API balance ledger'
,
defaultCurrency
:
options
.
defaultCurrency
??
'satoshi'
,
created
:
now
,
updated
:
now
,
entries
:
[
]
,
// Idempotency keys for deposits already absorbed into balances. Seeded
// here so a freshly created ledger has the same shape readLedger's
// migration produces for a legacy one.
credited
:
[
]
}
;
}
/**
* Read a webledger from storage
*
@param
{
string
} ledgerPath - URL path to ledger file
*
@returns
{
Promise<object>
} WebLedger object
*/
export
async
function
readLedger
(
ledgerPath
=
DEFAULT_PATH
)
{
const
buf
=
await
storage
.
read
(
ledgerPath
)
;
if
(
!
buf
)
{
return
createLedger
(
)
;
}
try
{
const
ledger
=
JSON
.
parse
(
buf
.
toString
(
'utf8'
)
)
;
// Migrate legacy format: add missing spec fields
if
(
!
ledger
[
'@context'
]
)
ledger
[
'@context'
]
=
CONTEXT
;
if
(
!
ledger
.
type
)
ledger
.
type
=
'WebLedger'
;
if
(
!
ledger
.
defaultCurrency
)
ledger
.
defaultCurrency
=
'satoshi'
;
if
(
!
ledger
.
created
)
ledger
.
created
=
Math
.
floor
(
Date
.
now
(
)
/
1000
)
;
if
(
!
ledger
.
entries
)
ledger
.
entries
=
[
]
;
// Idempotency keys for deposits already absorbed into balances. Kept in
// the ledger itself so crediting is crash-safe: the balance and the "this
// deposit was counted" marker commit together in one writeLedger, instead
// of the balance landing in one file and the spent-marker in another.
if
(
!
ledger
.
credited
)
ledger
.
credited
=
[
]
;
return
ledger
;
}
catch
{
return
createLedger
(
)
;
}
}
/**
* Write a webledger to storage (updates the `updated` timestamp)
*
@param
{
object
} ledger - WebLedger object
*
@param
{
string
} ledgerPath - URL path to ledger file
*
@returns
{
Promise<boolean>
}
*/
export
async
function
writeLedger
(
ledger
,
ledgerPath
=
DEFAULT_PATH
)
{
ledger
.
updated
=
Math
.
floor
(
Date
.
now
(
)
/
1000
)
;
return
storage
.
write
(
ledgerPath
,
JSON
.
stringify
(
ledger
,
null
,
2
)
)
;
}
/**
* Get balance for a URI
*
@param
{
object
} ledger - WebLedger object
*
@param
{
string
} uri - Agent URI (e.g. did:nostr:...)
*
@param
{
string
} [currency] - Currency code (e.g. 'tbtc3', 'tbtc4'). If omitted, reads default/simple amount.
*
@returns
{
number
} Balance as integer
*/
export
function
getBalance
(
ledger
,
uri
,
currency
)
{
const
entry
=
ledger
.
entries
.
find
(
e
=>
e
.
url
===
uri
)
;
if
(
!
entry
)
return
0
;
// Handle array amount format
if
(
Array
.
isArray
(
entry
.
amount
)
)
{
const
target
=
currency
?
entry
.
amount
.
find
(
a
=>
a
.
currency
===
currency
)
:
entry
.
amount
.
find
(
a
=>
a
.
currency
===
'satoshi'
||
a
.
currency
===
'sat'
)
;
return
target
?
parseInt
(
target
.
value
,
10
)
||
0
:
0
;
}
// Simple string format — only if no specific currency requested, or currency matches default
if
(
currency
)
return
0
;
return
parseInt
(
entry
.
amount
,
10
)
||
0
;
}
/**
* Set balance for a URI
*
@param
{
object
} ledger - WebLedger object
*
@param
{
string
} uri - Agent URI
*
@param
{
number
} amount - New balance
*
@param
{
string
} [currency] - Currency code. If provided, uses array amount format.
*/
export
function
setBalance
(
ledger
,
uri
,
amount
,
currency
)
{
let
entry
=
ledger
.
entries
.
find
(
e
=>
e
.
url
===
uri
)
;
if
(
!
currency
)
{
// Simple string format (backward compatible)
if
(
entry
)
{
entry
.
amount
=
String
(
amount
)
;
}
else
{
ledger
.
entries
.
push
(
{
type
:
'Entry'
,
url
:
uri
,
amount
:
String
(
amount
)
}
)
;
}
return
;
}
// Multi-currency: use array format
if
(
!
entry
)
{
entry
=
{
type
:
'Entry'
,
url
:
uri
,
amount
:
[
]
}
;
ledger
.
entries
.
push
(
entry
)
;
}
// Migrate simple string to array if needed
if
(
!
Array
.
isArray
(
entry
.
amount
)
)
{
const
oldVal
=
parseInt
(
entry
.
amount
,
10
)
||
0
;
entry
.
amount
=
oldVal
>
0
?
[
{
currency
:
'satoshi'
,
value
:
String
(
oldVal
)
}
]
:
[
]
;
}
const
existing
=
entry
.
amount
.
find
(
a
=>
a
.
currency
===
currency
)
;
if
(
existing
)
{
existing
.
value
=
String
(
amount
)
;
}
else
{
entry
.
amount
.
push
(
{
currency
,
value
:
String
(
amount
)
}
)
;
}
}
/**
* Credit (add to) a balance
*
@param
{
object
} ledger - WebLedger object
*
@param
{
string
} uri - Agent URI
*
@param
{
number
} amount - Amount to add
*
@param
{
string
} [currency] - Currency code
*
@returns
{
number
} New balance
*/
export
function
credit
(
ledger
,
uri
,
amount
,
currency
)
{
const
current
=
getBalance
(
ledger
,
uri
,
currency
)
;
const
newBalance
=
current
+
amount
;
setBalance
(
ledger
,
uri
,
newBalance
,
currency
)
;
return
newBalance
;
}
/**
* Debit (subtract from) a balance
*
@param
{
object
} ledger - WebLedger object
*
@param
{
string
} uri - Agent URI
*
@param
{
number
} amount - Amount to subtract
*
@param
{
string
} [currency] - Currency code
*
@returns
{
{success: boolean, balance: number}
} Result
*/
export
function
debit
(
ledger
,
uri
,
amount
,
currency
)
{
const
current
=
getBalance
(
ledger
,
uri
,
currency
)
;
if
(
current
<
amount
)
{
return
{
success
:
false
,
balance
:
current
}
;
}
const
newBalance
=
current
-
amount
;
setBalance
(
ledger
,
uri
,
newBalance
,
currency
)
;
return
{
success
:
true
,
balance
:
newBalance
}
;
}
/**
* Idempotently credit a keyed deposit exactly once.
*
* The dedup key (e.g. `${chain}:${txid}:${vout}` for an on-chain UTXO) is
* recorded in the ledger's own `credited` list, so the balance increment and
* the "already counted" marker are committed together by a single writeLedger.
* A crash after that single write can never re-credit the same deposit — which
* is exactly what happened when the credit and the UTXO-spent record lived in
* two separate files written one after another.
*
*
@param
{
object
} ledger - WebLedger object (must be persisted afterwards)
*
@param
{
string
} key - Stable idempotency key for this deposit
*
@param
{
string
} uri - Agent URI
*
@param
{
number
} amount - Amount to add
*
@param
{
string
} [currency] - Currency code
*
@returns
{
{credited: boolean, balance: number}
} `credited` is false when the
* key was already absorbed (no change made)
*/
export
function
creditOnce
(
ledger
,
key
,
uri
,
amount
,
currency
)
{
if
(
!
ledger
.
credited
)
ledger
.
credited
=
[
]
;
if
(
ledger
.
credited
.
includes
(
key
)
)
{
return
{
credited
:
false
,
balance
:
getBalance
(
ledger
,
uri
,
currency
)
}
;
}
const
balance
=
credit
(
ledger
,
uri
,
amount
,
currency
)
;
ledger
.
credited
.
push
(
key
)
;
return
{
credited
:
true
,
balance
}
;
}
/**
* List all entries with non-zero balances
*
@param
{
object
} ledger - WebLedger object
*
@returns
{
Array<{url: string, amount: number}>
}
*/
export
function
listBalances
(
ledger
)
{
return
ledger
.
entries
.
map
(
e
=>
(
{
url
:
e
.
url
,
amount
:
getBalance
(
ledger
,
e
.
url
)
}
)
)
.
filter
(
e
=>
e
.
amount
>
0
)
;
}
/**
* Remove entries with zero balance
*
@param
{
object
} ledger - WebLedger object
*/
export
function
compact
(
ledger
)
{
ledger
.
entries
=
ledger
.
entries
.
filter
(
e
=>
{
const
bal
=
getBalance
(
ledger
,
e
.
url
)
;
return
bal
>
0
;
}
)
;
}
// Re-export the default path for consumers
export
{
DEFAULT_PATH
as
LEDGER_PATH
}
;
Back
|
FazBrowse Home
|
New Git URL