Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
101 changes: 63 additions & 38 deletions cheatsheet.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,30 @@
import {
Bee,
Bytes,
BZZ,
DAI,
ChunkEntry,
ChunkJoiner,
ChunkSplitter,
EthAddress,
FeedIndex,
Identifier,
MantarayNode,
MerkleTree,
NULL_IDENTIFIER,
PrivateKey,
PublicKey,
Reference,
Span,
Topic,
Utils,
} from 'swarm-core'
import {
Bee,
BZZ,
DAI,
MantarayNode,
NULL_IDENTIFIER,
Utils
} from './src'

main()

async function main() {
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
TOKENS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand All @@ -36,7 +40,7 @@ async function main() {
// 14.349652349855834010
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
ELLIPTIC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand Down Expand Up @@ -64,7 +68,7 @@ async function main() {
// 0xFCAd0B19bB29D4674531d6f115237E16AfCE377c
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
BYTES
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand All @@ -89,7 +93,7 @@ async function main() {
// 648198b984056286aef8399dfa219578a6e04eb16030c278e783320606ce2404
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MANTARAY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand Down Expand Up @@ -117,7 +121,7 @@ async function main() {
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CAC / SOC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand All @@ -129,7 +133,7 @@ async function main() {
const span = Span.fromBigInt(BigInt(data.length))

// TODO: this is clumsy
const result = await bee.uploadChunk(stamp, new Uint8Array([...span.toUint8Array(), ...data.toUint8Array()]))
const result = await bee.chunk.upload(stamp, new Uint8Array([...span.toUint8Array(), ...data.toUint8Array()]))

console.log(result.reference.toHex())
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
Expand All @@ -142,43 +146,64 @@ async function main() {
const signer = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
const identifier = NULL_IDENTIFIER

const socWriter = bee.makeSOCWriter(signer)
const socWriter = bee.soc.makeWriter(signer)

const result = await socWriter.upload(stamp, identifier, Bytes.fromUtf8('soc payload').toUint8Array())
console.log(result.reference.toHex())
// 0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef (dummy)
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MERKLE TREE
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
CHUNK TREE
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

{
const tree = new MerkleTree(async chunk => {
console.log('span:', chunk.span)
console.log('reference:', new Bytes(chunk.hash()).toHex())
})
// In-memory stand-in for a real chunk store (e.g. bee.chunk.upload per entry).
const store = new Map<string, Uint8Array>()

async function onBatch(batch: ChunkEntry[]): Promise<ChunkEntry[]> {
for (const { chunk } of batch) {
store.set(chunk.hash().toHex(), chunk.build())
}

tree.append(Bytes.fromUtf8('hello').toUint8Array())
tree.append(Bytes.fromUtf8('world').toUint8Array())
return []
}

const tree = new ChunkSplitter(onBatch)
await tree.append(Bytes.fromUtf8('hello').toUint8Array())
await tree.append(Bytes.fromUtf8('world').toUint8Array())

const rootChunk = await tree.finalize()
store.set(rootChunk.hash().toHex(), rootChunk.build())

console.log('root:', new Bytes(rootChunk.hash()).toHex())
console.log('span:', rootChunk.span)
console.log('root:', rootChunk.hash().toHex())
// span: 10n
// reference: c3d78c959eb23a464619e893358a1d90e467f37c72742985ccc89159350098b4
// root: c3d78c959eb23a464619e893358a1d90e467f37c72742985ccc89159350098b4

const joined = await ChunkJoiner.collect(rootChunk.hash().toUint8Array(), async address => {
const bytes = store.get(new Reference(address).toHex())

if (!bytes) {
throw new Error(`not found: ${new Reference(address).toHex()}`)
}

return bytes
})

console.log('joined:', new Bytes(joined).toUtf8())
// joined: helloworld
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
PSS
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

// receiver
{
const bee = new Bee('http://localhost:1633')

bee.pssSubscribe(Topic.fromString('Alice <> Bob private chat'), {
bee.messaging.pssSubscribe(Topic.fromString('Alice <> Bob private chat'), {
onMessage: message => {
console.log(message.toUtf8())
},
Expand All @@ -200,10 +225,10 @@ async function main() {

const target = Utils.makeMaxTarget(overlay)

await bee.pssSend(stamp, topic, target, Bytes.fromUtf8('Hello, Bob!').toUint8Array())
await bee.messaging.pssSend(stamp, topic, target, Bytes.fromUtf8('Hello, Bob!').toUint8Array())
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
GSOC
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand All @@ -212,7 +237,7 @@ async function main() {
const overlay = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'
const identifier = NULL_IDENTIFIER

const signer = bee.gsocMine(overlay, identifier)
const signer = bee.messaging.gsocMine(overlay, identifier)
console.log(signer.toHex())
// 000000000000000000000000000000000000000000000000000000000000bd72
}
Expand All @@ -223,7 +248,7 @@ async function main() {
const signer = new PrivateKey('000000000000000000000000000000000000000000000000000000000000bd72')
const identifier = NULL_IDENTIFIER

bee.gsocSubscribe(signer.publicKey().address(), identifier, {
bee.messaging.gsocSubscribe(signer.publicKey().address(), identifier, {
onMessage: message => {
console.log(message.toUtf8())
},
Expand All @@ -243,20 +268,20 @@ async function main() {
const identifier = NULL_IDENTIFIER
const stamp = '0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef'

await bee.gsocSend(stamp, signer, identifier, 'GSOC!')
await bee.messaging.gsocSend(stamp, signer, identifier, 'GSOC!')
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
API / SEMVER
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

if (0) {
const bee = new Bee('http://localhost:1633')
console.log(await bee.isSupportedExactVersion())
console.log(await bee.status.isSupportedExactVersion())
// true
}

/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FEED
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */

Expand All @@ -266,9 +291,9 @@ async function main() {
const topic = Topic.fromString('Alice <> Bob private chat')
const signer = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')

const feedWriter = bee.makeFeedWriter(topic, signer)
const { reference } = await bee.uploadData(stamp, 'First update')
feedWriter.upload(stamp, reference)
const feedWriter = bee.feed.makeWriter(topic, signer)
const { reference } = await bee.data.upload(stamp, 'First update')
feedWriter.uploadReference(stamp, reference)
}

if (0) {
Expand All @@ -277,6 +302,6 @@ async function main() {
const topic = Topic.fromString('Alice <> Bob private chat')
const signer = new PrivateKey('0x0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef')

bee.createFeedManifest(stamp, topic, signer.publicKey().address())
bee.feed.createManifest(stamp, topic, signer.publicKey().address())
}
}
41 changes: 41 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"debug": "^4.4.3",
"isomorphic-ws": "^5.0.0",
"semver": "^7.3.5",
"swarm-core": "npm:@upcoming/swarm-core@0.0.8",
"ws": "^8.21.0",
"zod": "^4.4.3"
},
Expand Down
2 changes: 1 addition & 1 deletion src/api/balance.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { PeerAddress } from 'swarm-core'
import type { BalanceResponse, BeeRequestOptions, PeerBalance } from '../types'
import { GetAllBalancesResponse, GetPeerBalanceResponse } from '../types/schema/balance'
import { http } from '../utils/http'
import { PeerAddress } from '../utils/typed-bytes'

const balancesEndpoint = 'balances'

Expand Down
2 changes: 1 addition & 1 deletion src/api/batches.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BatchId } from 'swarm-core'
import type { BeeRequestOptions, GlobalPostageBatch } from '../types'
import { GetGlobalPostageBatchResponse, GetGlobalPostageBatchesResponse } from '../types/schema/stamps'
import { http } from '../utils/http'
import { BatchId } from '../utils/typed-bytes'

const batchesEndpoint = 'batches'

Expand Down
3 changes: 1 addition & 2 deletions src/api/bytes.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { BatchId, Bytes, Reference } from 'swarm-core'
import { Optional } from 'cafe-utility'
import type { BeeRequestOptions, DownloadOptions, RedundantUploadOptions, ReferenceInformation } from '../types'
import { UploadResult } from '../types'
import { UploadResultBody } from '../types/schema/upload'
import { Bytes } from '../utils/bytes'
import { prepareRequestHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { ResourceLocator } from '../utils/resource-locator'
import { DownloadOptionsSchema } from '../utils/schema'
import { makeTagUid } from '../utils/type'
import { BatchId, Reference } from '../utils/typed-bytes'

const endpoint = 'bytes'

Expand Down
3 changes: 1 addition & 2 deletions src/api/bzz.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BatchId, Bytes, Reference } from 'swarm-core'
import { Optional } from 'cafe-utility'
import { Readable } from 'stream'
import {
Expand All @@ -10,14 +11,12 @@ import {
UploadResult,
} from '../types'
import { UploadResultBody } from '../types/schema/upload'
import { Bytes } from '../utils/bytes'
import { assertCollection } from '../utils/collection'
import { prepareRequestHeaders, readFileHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { ResourceLocator } from '../utils/resource-locator'
import { uploadTar } from '../utils/tar-uploader'
import { isReadable, makeTagUid } from '../utils/type'
import { BatchId, Reference } from '../utils/typed-bytes'

const bzzEndpoint = 'bzz'

Expand Down
2 changes: 1 addition & 1 deletion src/api/chequebook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { PeerAddress, TransactionId } from 'swarm-core'
import type {
BeeRequestOptions,
ChequebookAddressResponse,
Expand All @@ -17,7 +18,6 @@ import {
} from '../types/schema/chequebook'
import { prepareRequestHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { PeerAddress, TransactionId } from '../utils/typed-bytes'

const chequebookEndpoint = 'chequebook'

Expand Down
2 changes: 1 addition & 1 deletion src/api/chunk.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { BatchId, Reference } from 'swarm-core'
import { Optional } from 'cafe-utility'
import type { BeeRequestOptions, DownloadOptions, EnvelopeWithBatchId, UploadOptions, UploadResult } from '../types'
import { UploadResultBody } from '../types/schema/upload'
import { prepareRequestHeaders } from '../utils/headers'
import { http } from '../utils/http'
import { makeTagUid } from '../utils/type'
import { BatchId, Reference } from '../utils/typed-bytes'

const endpoint = 'chunks'

Expand Down
Loading
Loading