Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: revamp bee-js #559

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
4 changes: 2 additions & 2 deletions jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ export default async (): Promise<Config.InitialOptions> => {
* only consists a single queen node as well
*/
if (!process.env.SKIP_WORKER) {
process.env.WORKER_PSS_ADDRESS = await getPssAddress('http://localhost:11633')
process.env.WORKER_PSS_ADDRESS = (await getPssAddress('http://localhost:11633')).toCompressedHex()
}

if (!process.env.TEST_STAMP) {
process.env.TEST_STAMP = await getOrBuyStamp()
process.env.TEST_STAMP = (await getOrBuyStamp()).toHex()
}

return {
Expand Down
269 changes: 132 additions & 137 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,16 +62,14 @@
"typescript": "^4.8.4"
},
"dependencies": {
"@ethersphere/bee-js": "^8.3.0",
"@fairdatasociety/bmt-js": "^2.1.0",
"cafe-utility": "^26.2.1",
"@upcoming/bee-js": "^0.5.3",
"cafe-utility": "^27.9.1",
"chalk": "^2.4.2",
"cli-progress": "^3.11.2",
"ethereumjs-wallet": "^1.0.2",
"ethers": "^5.7.2",
"furious-commander": "^1.7.1",
"inquirer": "^8.2.5",
"mantaray-js": "^1.0.3",
"node-fetch": "^2.7.0",
"ora": "^5.3.0",
"ws": "^8.11.0"
Expand Down
12 changes: 6 additions & 6 deletions src/command/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChequebookAddressResponse, NodeAddresses } from '@ethersphere/bee-js'
import { ChequebookAddressResponse, NodeAddresses } from '@upcoming/bee-js'
import { Optional } from 'cafe-utility'
import chalk from 'chalk'
import { LeafCommand } from 'furious-commander'
Expand All @@ -13,7 +13,7 @@ export class Addresses extends RootCommand implements LeafCommand {
public nodeAddresses!: NodeAddresses

public async run(): Promise<void> {
await super.init()
super.init()

this.nodeAddresses = await this.bee.getNodeAddresses()
const wrappedChequebookAddress = await this.bee
Expand All @@ -33,10 +33,10 @@ export class Addresses extends RootCommand implements LeafCommand {
const longest = 'PSS Public Key'.length
this.console.log(chalk.bold('Node Addresses'))
this.console.divider()
this.console.log(createKeyValue('Ethereum', this.nodeAddresses.ethereum, longest))
this.console.log(createKeyValue('Overlay', this.nodeAddresses.overlay, longest))
this.console.log(createKeyValue('PSS Public Key', this.nodeAddresses.pssPublicKey, longest))
this.console.log(createKeyValue('Public Key', this.nodeAddresses.publicKey, longest))
this.console.log(createKeyValue('Ethereum', this.nodeAddresses.ethereum.toHex(), longest))
this.console.log(createKeyValue('Overlay', this.nodeAddresses.overlay.toHex(), longest))
this.console.log(createKeyValue('PSS Public Key', this.nodeAddresses.pssPublicKey.toCompressedHex(), longest))
this.console.log(createKeyValue('Public Key', this.nodeAddresses.publicKey.toCompressedHex(), longest))
this.console.log(createKeyValue('Underlay', this.nodeAddresses.underlay.join(' '), longest))

wrappedChequebookAddress.ifPresent(chequebookAddress => {
Expand Down
13 changes: 7 additions & 6 deletions src/command/cheque/cashout.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { BZZ } from '@upcoming/bee-js'
import chalk from 'chalk'
import { LeafCommand, Option } from 'furious-commander'
import { createKeyValue } from '../../utils/text'
Expand Down Expand Up @@ -48,7 +49,7 @@ export class Cashout extends ChequeCommand implements LeafCommand {
public gasPrice!: bigint

public async run(): Promise<void> {
await super.init()
super.init()

if (this.all) {
await this.cashoutAll()
Expand All @@ -69,16 +70,16 @@ export class Cashout extends ChequeCommand implements LeafCommand {
}
}

private async cashoutOne(address: string, amount: bigint): Promise<void> {
private async cashoutOne(address: string, amount: BZZ): Promise<void> {
try {
this.console.log(chalk.green('Cashing out:'))
this.printCheque({ address, amount })
const transaction = await this.bee.cashoutLastCheque(address, {
gasLimit: this.gasLimit?.toString(),
gasPrice: this.gasPrice?.toString(),
gasLimit: this.gasLimit,
gasPrice: this.gasPrice,
})
this.console.log(createKeyValue('Tx', transaction))
this.console.quiet(transaction)
this.console.log(createKeyValue('Tx', transaction.toHex()))
this.console.quiet(transaction.toHex())
} catch (error) {
this.console.error('Could not cashout ' + address)
this.console.printBeeError(error, { notFoundMessage: 'No peer found with that address.' })
Expand Down
13 changes: 7 additions & 6 deletions src/command/cheque/cheque-command.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { BZZ } from '@upcoming/bee-js'
import { getFieldOrNull } from '../../utils'
import { createKeyValue } from '../../utils/text'
import { RootCommand } from '../root-command'

interface Cashable {
address: string
amount: bigint
amount: BZZ
}

export class ChequeCommand extends RootCommand {
protected async getFilteredCheques(minimum: bigint): Promise<Cashable[]> {
const cheques = await this.getCashableCheques()

return cheques.filter(({ amount }) => amount >= minimum)
return cheques.filter(({ amount }) => amount.toPLURBigInt() >= minimum)
}

protected async getCashableCheques(): Promise<Cashable[]> {
Expand All @@ -35,18 +36,18 @@ export class ChequeCommand extends RootCommand {
protected printCheque(cashable: Cashable): void {
this.console.divider('-')
this.console.log(createKeyValue('Peer Address', cashable.address))
this.console.log(createKeyValue('Cheque Value', cashable.amount + ' PLUR'))
this.console.log(createKeyValue('Cheque Value', cashable.amount.toDecimalString() + ' xBZZ'))
this.console.quiet(cashable.address + ' ' + cashable.amount)
}

protected async getUncashedAmount(address: string): Promise<bigint> {
protected async getUncashedAmount(address: string): Promise<BZZ> {
try {
const lastCashout = await this.bee.getLastCashoutAction(address)

return BigInt(lastCashout.uncashedAmount)
return lastCashout.uncashedAmount
} catch (error) {
if (getFieldOrNull(error, 'message') === 'Not Found') {
return BigInt(0)
return BZZ.fromPLUR('0')
}
throw error
}
Expand Down
6 changes: 3 additions & 3 deletions src/command/cheque/deposit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class Deposit extends ChequeCommand implements LeafCommand {
public amount!: bigint

public async run(): Promise<void> {
await super.init()
super.init()

const response = await this.bee.depositTokens(this.amount.toString())
this.console.log(createKeyValue('Tx', response))
this.console.quiet(response)
this.console.log(createKeyValue('Tx', response.toHex()))
this.console.quiet(response.toHex())
}
}
2 changes: 1 addition & 1 deletion src/command/cheque/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class List extends ChequeCommand implements LeafCommand {
public minimum!: bigint

public async run(): Promise<void> {
await super.init()
super.init()

this.console.info(`Looking up cheques with value at least ${this.minimum}...`)
const cheques = await this.getFilteredCheques(this.minimum)
Expand Down
13 changes: 6 additions & 7 deletions src/command/cheque/withdraw-all.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Numbers } from 'cafe-utility'
import { LeafCommand } from 'furious-commander'
import { createKeyValue } from '../../utils/text'
import { ChequeCommand } from './cheque-command'
Expand All @@ -11,18 +10,18 @@ export class WithdrawAll extends ChequeCommand implements LeafCommand {
public readonly description = 'Withdraw all available tokens from the chequebook to the overlay address'

public async run(): Promise<void> {
await super.init()
super.init()

const balance = await this.bee.getChequebookBalance()

if (balance.availableBalance === '0') {
if (balance.availableBalance.toPLURBigInt() === BigInt(0)) {
this.console.error('No tokens to withdraw.')

return
}
this.console.log(`Withdrawing ${Numbers.fromDecimals(balance.availableBalance, 16)} xBZZ from the chequebook`)
const response = await this.bee.withdrawTokens(balance.availableBalance)
this.console.log(createKeyValue('Tx', response))
this.console.quiet(response)
this.console.log(`Withdrawing ${balance.availableBalance.toDecimalString()} xBZZ from the chequebook`)
const response = await this.bee.withdrawTokens(balance.availableBalance.toPLURString())
this.console.log(createKeyValue('Tx', response.toHex()))
this.console.quiet(response.toHex())
}
}
6 changes: 3 additions & 3 deletions src/command/cheque/withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ export class Withdraw extends ChequeCommand implements LeafCommand {
public amount!: bigint

public async run(): Promise<void> {
await super.init()
super.init()

const response = await this.bee.withdrawTokens(this.amount.toString())
this.console.log(createKeyValue('Tx', response))
this.console.quiet(response)
this.console.log(createKeyValue('Tx', response.toHex()))
this.console.quiet(response.toHex())
}
}
13 changes: 6 additions & 7 deletions src/command/download.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MantarayNode } from '@upcoming/bee-js'
import fs from 'fs'
import { Aggregation, LeafCommand } from 'furious-commander'
import { MantarayNode } from 'mantaray-js'
import { BzzAddress, makeBzzAddress } from '../utils/bzz-address'
import { Download as ManifestDownload } from './manifest/download'
import { RootCommand } from './root-command'
Expand All @@ -16,7 +16,7 @@ export class Download extends RootCommand implements LeafCommand {
private address!: BzzAddress

public async run(): Promise<void> {
await super.init()
super.init()

this.address = await makeBzzAddress(this.bee, this.manifestDownload.bzzUrl)

Expand All @@ -33,18 +33,17 @@ export class Download extends RootCommand implements LeafCommand {
const { name, data } = response

if (this.manifestDownload.stdout) {
process.stdout.write(data)
process.stdout.write(data.toUint8Array())
} else {
const path = this.manifestDownload.destination || name || this.address.hash
await fs.promises.writeFile(path, data)
await fs.promises.writeFile(path, data.toUint8Array())
}
}

private async isManifest(): Promise<boolean> {
try {
const response = await this.bee.downloadData(this.address.hash)
const node = new MantarayNode()
node.deserialize(response)
const node = await MantarayNode.unmarshal(this.bee, this.address.hash)
await node.loadRecursively(this.bee)

return true
} catch {
Expand Down
35 changes: 15 additions & 20 deletions src/command/feed/feed-command.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Reference } from '@ethersphere/bee-js'
import { Reference, Topic } from '@upcoming/bee-js'
import Wallet from 'ethereumjs-wallet'
import { Option } from 'furious-commander'
import { exit } from 'process'
Expand All @@ -12,8 +12,8 @@ import { RootCommand } from '../root-command'
import { VerbosityLevel } from '../root-command/command-log'

interface FeedInfo {
reference: string
manifest: string
reference: Reference
manifest: Reference
}

export class FeedCommand extends RootCommand {
Expand All @@ -35,18 +35,18 @@ export class FeedCommand extends RootCommand {
@Option({ key: 'password', alias: 'P', description: 'Password for the wallet' })
public password!: string

protected async updateFeedAndPrint(stamp: string, chunkReference: string): Promise<string> {
protected async updateFeedAndPrint(stamp: string, chunkReference: Reference): Promise<Reference> {
const wallet = await this.getWallet()
const topic = this.topic || this.bee.makeFeedTopic(this.topicString)
const topic = this.topic ? new Topic(this.topic) : Topic.fromString(this.topicString)
const { reference, manifest } = await this.writeFeed(stamp, wallet, topic, chunkReference)

this.console.verbose(createKeyValue('Chunk Reference', chunkReference))
this.console.verbose(createKeyValue('Chunk Reference', chunkReference.toHex()))
this.console.verbose(createKeyValue('Chunk Reference URL', `${this.bee.url}/bzz/${chunkReference}/`))
this.console.verbose(createKeyValue('Feed Reference', reference))
this.console.verbose(createKeyValue('Feed Manifest', manifest))
this.console.verbose(createKeyValue('Feed Reference', reference.toHex()))
this.console.verbose(createKeyValue('Feed Manifest', manifest.toHex()))
this.console.log(createKeyValue('Feed Manifest URL', `${this.bee.url}/bzz/${manifest}/`))

this.console.quiet(manifest)
this.console.quiet(manifest.toHex())

if (!this.quiet) {
printStamp(await this.bee.getPostageBatch(stamp), this.console, { shortenBatchId: true })
Expand Down Expand Up @@ -76,24 +76,19 @@ export class FeedCommand extends RootCommand {
return identities[this.identity] || identities[await pickIdentity(this.commandConfig, this.console)]
}

private async writeFeed(stamp: string, wallet: Wallet, topic: string, chunkReference: string): Promise<FeedInfo> {
private async writeFeed(stamp: string, wallet: Wallet, topic: Topic, chunkReference: Reference): Promise<FeedInfo> {
const spinner = createSpinner('Writing feed...')

if (this.verbosity !== VerbosityLevel.Quiet && !this.curl) {
spinner.start()
}

try {
const writer = this.bee.makeFeedWriter('sequence', topic, wallet.getPrivateKey())
const { reference: manifest } = await this.bee.createFeedManifest(
stamp,
'sequence',
topic,
wallet.getAddressString(),
)
const { reference } = await writer.upload(stamp, chunkReference as Reference)

return { reference, manifest }
const writer = this.bee.makeFeedWriter(topic, wallet.getPrivateKey())
const feedManifestResult = await this.bee.createFeedManifest(stamp, topic, wallet.getAddressString())
const { reference } = await writer.upload(stamp, chunkReference)

return { reference, manifest: feedManifestResult }
} finally {
spinner.stop()
}
Expand Down
31 changes: 17 additions & 14 deletions src/command/feed/print.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Utils } from '@ethersphere/bee-js'
import { makeChunk } from '@fairdatasociety/bmt-js'
import { MerkleTree, Topic } from '@upcoming/bee-js'
import { Binary } from 'cafe-utility'
import Wallet from 'ethereumjs-wallet'
import { LeafCommand, Option } from 'furious-commander'
Expand Down Expand Up @@ -30,14 +29,14 @@
public list!: boolean

public async run(): Promise<void> {
await super.init()
super.init()

if (!this.address) {
const wallet = await this.getWallet()
this.address = wallet.getAddressString()
}

const topic = this.topic || this.bee.makeFeedTopic(this.topicString)
const topic = this.topic ? new Topic(this.topic) : Topic.fromString(this.topicString)

// construct the feed manifest chunk
const body = Binary.concatBytes(
Expand All @@ -64,7 +63,9 @@
new Uint8Array(12).fill(0x0a),
)

const manifest = Binary.uint8ArrayToHex(makeChunk(body).address())
const root = (await MerkleTree.root(body)).hash()

const manifest = Binary.uint8ArrayToHex(root)
this.console.quiet(manifest)

if (this.quiet) {
Expand All @@ -77,25 +78,27 @@

try {
const addressString = this.address || (await this.getAddressString())
const reader = this.bee.makeFeedReader('sequence', topic, addressString)
const { reference, feedIndex, feedIndexNext } = await reader.download()
const reader = this.bee.makeFeedReader(topic, addressString)
const { payload, feedIndex, feedIndexNext } = await reader.download()
// TODO: verify this

Check warning on line 83 in src/command/feed/print.ts

View workflow job for this annotation

GitHub Actions / check (18.x)

Unexpected 'todo' comment: 'TODO: verify this'
const reference = payload
spinner.stop()
this.console.verbose(createKeyValue('Chunk Reference', reference))
this.console.verbose(createKeyValue('Chunk Reference', reference.toHex()))
this.console.verbose(createKeyValue('Chunk Reference URL', `${this.bee.url}/bzz/${reference}/`))
this.console.verbose(createKeyValue('Feed Index', feedIndex as string))
this.console.verbose(createKeyValue('Next Index', feedIndexNext))
this.console.verbose(createKeyValue('Feed Index', feedIndex.toBigInt().toString()))
this.console.verbose(createKeyValue('Next Index', feedIndexNext?.toBigInt().toString() ?? 'N/A'))
this.console.verbose(createKeyValue('Feed Manifest', manifest))

this.console.log(createKeyValue('Topic', `${topic}`))
const numberOfUpdates = parseInt(feedIndex as string, 16) + 1
this.console.log(createKeyValue('Number of Updates', numberOfUpdates))
const numberOfUpdates = feedIndex.toBigInt() + BigInt(1)
this.console.log(createKeyValue('Number of Updates', numberOfUpdates.toString(0)))

if (this.list) {
for (let i = 0; i < numberOfUpdates; i++) {
const indexBytes = Binary.numberToUint64(BigInt(i), 'BE')
const identifier = Utils.keccak256Hash(Binary.hexToUint8Array(topic), indexBytes)
const identifier = Binary.keccak256(Binary.concatBytes(topic.toUint8Array(), indexBytes))
const owner = Binary.hexToUint8Array(this.address)
const soc = Binary.uint8ArrayToHex(Utils.keccak256Hash(identifier, owner))
const soc = Binary.uint8ArrayToHex(Binary.keccak256(Binary.concatBytes(identifier, owner)))
const chunk = await this.bee.downloadChunk(soc)
// span + identifier + signature + span
const cac = Binary.uint8ArrayToHex(chunk.slice(8 + 32 + 65 + 8, 8 + 32 + 65 + 32 + 8))
Expand Down
Loading
Loading