Skip to content

Commit 925c208

Browse files
authored
feat!: rework library for better developer experience (#977)
* chore: initial commit * chore: apply tar fixes from other branch * docs: readme * build: make sure it works in vite * docs: readme * chore: consistency * style: make eslint happier * fix: preserve split node forks * chore: lint * feat: add tokens classes, fullPath to mantaray * feat: remove workaround for readable tag handling * feat: add gsoc * refactor: minor code improvements, cheatsheet * feat: add dev mode compatible BeeDev class * refactor: options and headers rework * fix: fix #376 and #975 * fix: allow async function as function in handler * chore: minor code re-ordering and format * fix: revert AxiosError import * refactor: make gas options bigint and string compatible * fix: add download and request options to manifest * refactor: enhance token handling * fix: change manifest save return type to UploadResult * fix: add missing request options * test: test for stake txid * test: test passing bzz instance to deposit stake * fix: change currentPrice to number * feat: add manifest devex methods * feat: restore ens support * fix: do not fetch feed update if no metadata is present * feat: add exchange methods to tokens * chore: bump util * feat: add stamp ux * chore: remove deprecations * chore: simplify * fix: add workaround for wrong batchttl and currentprice * feat(wip): add stamper * feat: add Feed, Identity, Signature, Bytes, FeedIndex improvements * feat(wip): support feed payload above 4096 bytes * feat: add getExtensionCost method on bee * fix: make it easier to mine gsoc signer * feat: add getStampEffectiveBytesBreakpoints * fix: disable websocket features when running in browser * feat: add size class * chore: bump utility * feat: add fromMilliseconds to Duration class * docs: update calls * chore: update amount * docs: fix jsdoc links * ci: no cache * docs: increase duration in docs
1 parent 8c801e9 commit 925c208

File tree

211 files changed

+11182
-13805
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

211 files changed

+11182
-13805
lines changed

.github/workflows/check.yaml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,7 @@ jobs:
2525
with:
2626
node-version: ${{ matrix.node-version }}
2727

28-
## Try getting the node modules from cache, if failed npm ci
29-
- uses: actions/cache@v2
30-
id: cache-npm
31-
with:
32-
path: node_modules
33-
key: ${{ runner.os }}-node-${{ hashFiles('**/package-lock.json') }}
34-
restore-keys: |
35-
${{ runner.OS }}-node-${{ env.cache-name }}-
36-
${{ runner.OS }}-node-
37-
${{ runner.OS }}-
3828
- name: Install npm deps
39-
if: steps.cache-npm.outputs.cache-hit != 'true'
4029
run: npm ci
4130

4231
- name: Commit linting

.github/workflows/tests.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ jobs:
3131
uses: actions/setup-node@v3
3232
with:
3333
node-version: ${{ matrix.node-version }}
34-
cache: 'npm'
3534

3635
- name: Install npm deps
3736
run: npm install -g npm && npm ci && npm install -g @fairdatasociety/fdp-play

CHANGELOG.md

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,6 @@
11
# Changelog
22

33

4-
## [8.3.1](https://github.com/ethersphere/bee-js/compare/v8.3.0...v8.3.1) (2024-11-06)
5-
6-
7-
### Bug Fixes
8-
9-
* set tar header size ([#972](https://github.com/ethersphere/bee-js/issues/972)) ([dc1ab57](https://github.com/ethersphere/bee-js/commit/dc1ab57b1a65bd9f3eae94795fdf38f4f89c2f10))
10-
114
## [8.3.0](https://github.com/ethersphere/bee-js/compare/v8.2.0...v8.3.0) (2024-10-24)
125

136

README.md

Lines changed: 192 additions & 50 deletions
Large diffs are not rendered by default.

build-fixup

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ cat >dist/cjs/package.json <<!EOF
1313
"./utils/tar.js": "./utils/tar.browser.js",
1414
"./utils/tar-writer.js": "./utils/tar-writer.browser.js",
1515
"./utils/tar-uploader.js": "./utils/tar-uploader.browser.js",
16+
"./utils/chunk-stream.js": "./utils/chunk-stream.browser.js",
1617
"./utils/data.js": "./utils/data.browser.js",
1718
"./utils/collection.node.js": "./utils/collection.browser.js"
1819
}
@@ -27,7 +28,8 @@ cat >dist/mjs/package.json <<!EOF
2728
"fs": false,
2829
"./utils/tar.js": "./utils/tar.browser.js",
2930
"./utils/tar-writer.js": "./utils/tar-writer.browser.js",
30-
"./utils/tar-uploader.js": "./utils/tar-uploader.browser.js",
31+
"./utils/tar-uploader.js": "./utils/tar-uploader.browser.js",
32+
"./utils/chunk-stream.js": "./utils/chunk-stream.browser.js",
3133
"./utils/data.js": "./utils/data.browser.js",
3234
"./utils/collection.node.js": "./utils/collection.browser.js"
3335
}

cheatsheet.ts

Lines changed: 276 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,276 @@
1+
import {
2+
Bee,
3+
Bytes,
4+
BZZ,
5+
DAI,
6+
EthAddress,
7+
FeedIndex,
8+
Identifier,
9+
MantarayNode,
10+
MerkleTree,
11+
NULL_IDENTIFIER,
12+
PrivateKey,
13+
PublicKey,
14+
Reference,
15+
Span,
16+
Topic,
17+
Utils,
18+
} from './src'
19+
20+
main()
21+
22+
async function main() {
23+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24+
TOKENS
25+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
26+
27+
{
28+
const bzz = BZZ.fromDecimalString('1.51')
29+
console.log(bzz.toPLURString())
30+
// 15100000000000000
31+
}
32+
33+
{
34+
const dai = DAI.fromWei(14349652349855834010n)
35+
console.log(dai.toDecimalString())
36+
// 14.349652349855834010
37+
}
38+
39+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
40+
ELLIPTIC
41+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
42+
43+
{
44+
const signer = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
45+
const message = 'Hello world!'
46+
const signature = signer.sign(message)
47+
const recovered = signature.recoverPublicKey(message)
48+
const address = recovered.address()
49+
console.log(address.toHex(), '==', signer.publicKey().address().toHex())
50+
// fcad0b19bb29d4674531d6f115237e16afce377c == fcad0b19bb29d4674531d6f115237e16afce377c
51+
}
52+
53+
{
54+
const privateKey = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
55+
const publicKey = privateKey.publicKey()
56+
console.log(publicKey.toCompressedHex())
57+
// 034646ae5047316b4230d0086c8acec687f00b1cd9d1dc634f6cb358ac0a9a8fff
58+
}
59+
60+
{
61+
const publicKey = new PublicKey('034646ae5047316b4230d0086c8acec687f00b1cd9d1dc634f6cb358ac0a9a8fff')
62+
const address = publicKey.address()
63+
console.log(address.toChecksum())
64+
// 0xFCAd0B19bB29D4674531d6f115237E16AfCE377c
65+
}
66+
67+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68+
BYTES
69+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
70+
71+
{
72+
console.log(Bytes.keccak256(Bytes.fromUtf8('hello')).toHex())
73+
// 1c8aff950685c2ed4bc3174f3472287b56d9517b9c948127319a09a7a36deac8
74+
}
75+
76+
{
77+
const reference = new Reference('f69f6bf21e4e97abef7fb52239265373479f14309d70f98f81a335936dd21d8b')
78+
console.log(reference.toCid('feed'))
79+
// bah5qcgza62pwx4q6j2l2x337wurdsjstondz6fbqtvyptd4bum2zg3osdwfq
80+
}
81+
82+
{
83+
console.log(Span.LENGTH, FeedIndex.LENGTH, Identifier.LENGTH, EthAddress.LENGTH)
84+
// 8 8 32 20
85+
}
86+
87+
{
88+
console.log(Topic.fromString('human readable').toHex())
89+
// 648198b984056286aef8399dfa219578a6e04eb16030c278e783320606ce2404
90+
}
91+
92+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93+
MANTARAY
94+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
95+
96+
if (0) {
97+
const mantaray = new MantarayNode()
98+
mantaray.addFork('foo', '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
99+
mantaray.addFork('foobar', '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
100+
101+
const values = mantaray.collect().map(x => x.fullPathString)
102+
103+
console.log(values)
104+
// [ 'foo', 'foobar' ]
105+
}
106+
107+
if (0) {
108+
const bee = new Bee('http://localhost:1633')
109+
const node = await MantarayNode.unmarshal(bee, '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
110+
await node.loadRecursively(bee)
111+
node.removeFork('unwanted-path')
112+
const newManifestAddress = await node.saveRecursively(
113+
bee,
114+
'0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef',
115+
)
116+
console.log(newManifestAddress.reference.toHex())
117+
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
118+
}
119+
120+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
121+
CAC / SOC
122+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
123+
124+
if (0) {
125+
const bee = new Bee('http://localhost:1633')
126+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
127+
128+
const data = Bytes.fromUtf8('hello')
129+
const span = Span.fromBigInt(BigInt(data.length))
130+
131+
// TODO: this is clumsy
132+
const result = await bee.uploadChunk(stamp, new Uint8Array([...span.toUint8Array(), ...data.toUint8Array()]))
133+
134+
console.log(result.reference.toHex())
135+
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
136+
}
137+
138+
if (0) {
139+
const bee = new Bee('http://localhost:1633')
140+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
141+
142+
const signer = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
143+
const identifier = NULL_IDENTIFIER
144+
145+
const socWriter = bee.makeSOCWriter(signer)
146+
147+
const result = await socWriter.upload(stamp, identifier, Bytes.fromUtf8('soc payload').toUint8Array())
148+
console.log(result.reference.toHex())
149+
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
150+
}
151+
152+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
153+
MERKLE TREE
154+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
155+
156+
{
157+
const tree = new MerkleTree(async chunk => {
158+
console.log('span:', chunk.span)
159+
console.log('reference:', new Bytes(chunk.hash()).toHex())
160+
})
161+
162+
tree.append(Bytes.fromUtf8('hello').toUint8Array())
163+
tree.append(Bytes.fromUtf8('world').toUint8Array())
164+
165+
const rootChunk = await tree.finalize()
166+
167+
console.log('root:', new Bytes(rootChunk.hash()).toHex())
168+
// span: 10n
169+
// reference: c3d78c959eb23a464619e893358a1d90e467f37c72742985ccc89159350098b4
170+
// root: c3d78c959eb23a464619e893358a1d90e467f37c72742985ccc89159350098b4
171+
}
172+
173+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
174+
PSS
175+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
176+
177+
// receiver
178+
{
179+
const bee = new Bee('http://localhost:1633')
180+
181+
bee.pssSubscribe(Topic.fromString('Alice <> Bob private chat'), {
182+
onMessage: message => {
183+
console.log(message.toUtf8())
184+
},
185+
onError: error => {
186+
console.error(error)
187+
},
188+
})
189+
}
190+
191+
// sender
192+
{
193+
const bee = new Bee('http://localhost:1633')
194+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
195+
const topic = Topic.fromString('Alice <> Bob private chat')
196+
const overlay = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
197+
198+
const target = Utils.makeMaxTarget(overlay)
199+
200+
await bee.pssSend(stamp, topic, target, Bytes.fromUtf8('Hello, Bob!').toUint8Array())
201+
}
202+
203+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
204+
GSOC
205+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
206+
207+
{
208+
const bee = new Bee('http://localhost:1633')
209+
const overlay = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
210+
const identifier = NULL_IDENTIFIER
211+
212+
const signer = bee.gsocMine(overlay, identifier)
213+
console.log(signer.toHex())
214+
// 000000000000000000000000000000000000000000000000000000000000bd72
215+
}
216+
217+
// receiver
218+
if (0) {
219+
const bee = new Bee('http://localhost:1633')
220+
const signer = new PrivateKey('000000000000000000000000000000000000000000000000000000000000bd72')
221+
const identifier = NULL_IDENTIFIER
222+
223+
bee.gsocSubscribe(signer.publicKey().address(), identifier, {
224+
onMessage: message => {
225+
console.log(message.toUtf8())
226+
},
227+
onError: error => {
228+
console.error(error)
229+
},
230+
})
231+
}
232+
233+
// sender
234+
if (0) {
235+
const bee = new Bee('http://localhost:1633')
236+
const signer = new PrivateKey('000000000000000000000000000000000000000000000000000000000000bd72')
237+
const identifier = NULL_IDENTIFIER
238+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
239+
240+
await bee.gsocSend(stamp, signer, identifier, 'GSOC!')
241+
}
242+
243+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
244+
API / SEMVER
245+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
246+
247+
if (0) {
248+
const bee = new Bee('http://localhost:1633')
249+
console.log(await bee.isSupportedExactVersion())
250+
// true
251+
}
252+
253+
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
254+
FEED
255+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */
256+
257+
if (0) {
258+
const bee = new Bee('http://localhost:1633')
259+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
260+
const topic = Topic.fromString('Alice <> Bob private chat')
261+
const signer = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
262+
263+
const feedWriter = bee.makeFeedWriter(topic, signer)
264+
const { reference } = await bee.uploadData(stamp, 'First update')
265+
feedWriter.upload(stamp, reference)
266+
}
267+
268+
if (0) {
269+
const bee = new Bee('http://localhost:1633')
270+
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
271+
const topic = Topic.fromString('Alice <> Bob private chat')
272+
const signer = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')
273+
274+
bee.createFeedManifest(stamp, topic, signer.publicKey().address())
275+
}
276+
}

0 commit comments

Comments
 (0)