v1.4.1-rc1 正在测试中,并将很快部署到引导节点!
Swarm 生态系统还很年轻,总是有很多很酷的实用程序、应用程序和用例的空间。我们支持使用一组库、CLI 和实用程序在 Swarm 上进行开发。我将演示如何通过一个小型黑客会话来利用其中的一些组件。
目标是什么?开发人员相互共享应用程序日志是很常见的。使用 Swarm 存储日志怎么样?将命令的输出直接通过管道传输到 Swarm 会很棒,所以让我们来开发它!
首先,我们需要为我们未来的工具取一个合适的名字。由于有一个类似的将笔记粘贴到 Swarm 的项目,称为Pastebee,我决定将其命名为 Pastebee CLI。
其次,我们需要有一个 Bee 实例用于开发。有几种方法可以实现这一点。您可以使用 testnet 部署来部署 Bee 节点,使用Bee Factory 之类的工具,该工具使用 Docker 生成本地 Bee 集群,或者最简单的解决方案,我们将使用的解决方案:Bee 的开发模式。开发模式模拟了所有的区块链要求,并且在其功能上有所限制,但主要功能,数据的上传和下载,可用于本地开发,这就是我们所需要的。因此,如果您还没有 Bee,请先安装它。有关更多详细信息,请参阅我们的文档。安装 Bee 后,只需bee dev在命令提示符中运行该命令,即可使用端点启动开发服务器http://localhost:1633对于普通端点和http://localhost:1635调试端点。现在,我们可以继续进行黑客攻击。
让我们设置一个环境并使用 TypeScript 连接到我们的节点。创建一个新包并安装我们需要的依赖项:
$ npm init -y
$ npm install --save @ethersphere/bee-js yargs
$ npm install --save-dev ts-node typescript @ethersphere/swarm-cli @types/yargs
要将数据上传到 Swarm,我们必须获得邮票。我们将使用 Swarm 的便捷实用程序:swarm-cli,通过命令提示符与 Bee 节点进行通信。
$ npx swarm-cli 邮票购买 --depth 20 --amount 1_000_000
邮票 ID:a28a6fe8bd11deff49beb77005884f8043a5048aa4100c757f65e886ddcb1123
有关指定深度和数量的更多信息,请查看Bee 文档中的相关部分。
在开始编码之前,创建一个 env。BEE_STAMP带有您从上一步获得的 Stamp ID 的变量,以便我们可以在我们的代码中使用它:
$ export BEE_STAMP="<<在此处插入您的邮票!>>"
现在最后,一些连接到 Bee 节点的代码。
import { Bee } from '@ethersphere/bee-js'
const BEE_URL = process.env.BEE_URL || 'http://localhost:1633'
const BEE_STAMP = process.env.BEE_STAMP
async function main(): Promise<void> {
if(!BEE_STAMP) throw new Error ('You have to configure Postage Stamp env. variable BEE_STAMP!')
const bee = new Bee(BEE_URL)
const result = await bee.uploadFile(BEE_STAMP, 'Hello world Swarm!')
console.log(`${BEE_URL}/bzz/${result.reference}`)
}
main()
使用此代码,我们连接到在BEE_URL常量下定义的 Bee 节点,并使用我们之前创建BEE_STAMP的将第一个字节上传到 Bee。执行此文件时,您将获得一个链接,您可以在浏览器中访问该链接,该链接应显示“'Hello world Swarm!”:
$ npx ts-node index.ts
http://localhost:1633/bzz/f20e1a0fdacd0c6e95de2e63979765e34100418639050c6835b27d6fa8f57d0c
让我们扩展它以使用自定义函数从标准输入 (STDIN) 获取数据 loadStdin()
import { Bee } from '@ethersphere/bee-js'
const BEE_URL = process.env.BEE_URL || 'http://localhost:1633'
const BEE_STAMP = process.env.BEE_STAMP
async function loadStdin(): Promise<string> {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk)
return Buffer.concat(chunks).toString('utf8')
}
async function main(): Promise<void> {
if(!BEE_STAMP) throw new Error ('You have to configure Postage Stamp env. variable BEE_STAMP!')
const bee = new Bee(BEE_URL)
const data = await loadStdin()
const result = await bee.uploadFile(BEE_STAMP, data)
console.log(`${BEE_URL}/bzz/${result.reference}`)
}
main(
现在我们可以将一些数据通过管道传输到 Swarm!
$ echo “来自 STDIN 的 Swarm 世界你好!” | npx ts-node index.ts
http://localhost:1633/bzz/f78dc7a4cc6b3fb5df79b3f219f21265bad67ffd38be8552d2536e5e2b02ac92
最后,我们将添加一些 CLI 选项yargs来配置 Bee 节点和邮票,并将用户指向显示日志的 Pastebee 应用程序。
import { Bee } from '@ethersphere/bee-js'
import yargs from 'yargs'
async function loadStdin(): Promise<string> {
const chunks = []
for await (const chunk of process.stdin) chunks.push(chunk)
return Buffer.concat(chunks).toString('utf8')
}
async function main(): Promise<void> {
const argv = await yargs(process.argv.slice(2))
.usage('Usage: <some STDOUT producing command> | $0 [options]')
.option('bee', {
alias: 'b',
type: 'string',
describe: 'Bee node URL.',
})
.option('stamp', {
alias: 'p',
type: 'string',
describe: 'Postage Batch Stamp ID. Required if custom Bee node is used.',
}).argv
const BEE_URL = argv.bee || process.env.BEE_URL || 'http://localhost:1633'
const BEE_STAMP =
argv.stamp || process.env.BEE_STAMP
if(!BEE_STAMP) throw new Error ('You have to configure Postage Stamp env. variable BEE_STAMP!')
const bee = new Bee(BEE_URL)
const data = await loadStdin()
const result = await bee.uploadFile(BEE_STAMP, data)
process.stdout.write(`Uploaded! https://pastebee.bzz.link/?${result.reference}`)
}
main()
那是我们功能齐全的pastebee-cli实用程序!您可以使用已发布的包来完成所有这些工作,并且还具有更完善的 UX 和附加功能。
免责声明:以上内容源自网络,版权归原作者所有,如有侵权,请联系删除!本文旨在传递更多市场信息,不构成任何投资建议!请谨慎对待!
,