Web3.js 中文Api文档

🌙
手机阅读
本文目录结构

.. _bzz:

======== web3.bzz

该API可能会随着时间而改变。

web3-bzz 软件包允许您与分散的文件存储库swarm进行交互。更多参考 Swarm Docs http://swarm-guide.readthedocs.io/en/latest/


    var Bzz = require('web3-bzz');

    // will autodetect if the "ethereum" object is present and will either connect to the local swarm node, or the swarm-gateways.net.
    // Optional you can give your own provider URL; If no provider URL is given it will use "http://swarm-gateways.net"
    var bzz = new Bzz(Bzz.givenProvider || 'http://swarm-gateways.net');


    // or using the web3 umbrella package

    var Web3 = require('web3');
    var web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');

    // -> web3.bzz.currentProvider // if Web3.givenProvider was an ethereum provider it will set: "http://localhost:8500" otherwise it will set: "http://swarm-gateways.net"

    // set the provider manually if necessary
    web3.bzz.setProvider("http://localhost:8500");

setProvider

web3.bzz.setProvider(myProvider)

将更改其模块的 provider 。

注意:在 web3 调用的时候,它还会设置子模块web3.eth, web3.shh,web3.contract,除了 web3.bzz 始终需要设置 provider 。


参数

  1. Object - myProvider: 一个可用的 provider .

返回值

Boolean


例子

var Bzz = require('web3-bzz');
var bzz = new Bzz('http://localhost:8500');

// change provider
bzz.setProvider('http://swarm-gateways.net');

givenProvider

web3.bzz.givenProvider

在以太坊兼容的浏览器中使用web3.js时(比如使用安装MateMask扩展的Chrome浏览器),它将由该浏览器使用当前的本机提供程序进行设置。 通过(浏览器)环境返回给定的提供程序,否则返回null


返回值

Object: 给定的 Provider 或 null;


例子

bzz.givenProvider;
> {
    send: function(),
    on: function(),
    bzz: "http://localhost:8500",
    shh: true,
    ...
}

bzz.setProvider(bzz.givenProvider || "http://swarm-gateways.net");

currentProvider

bzz.currentProvider

返回当前提供者的URL,否则返回null


返回值

Object: 当前提供者网址 或 null.


例子

bzz.currentProvider;
> "http://localhost:8500"


if(!bzz.currentProvider) {
bzz.setProvider("http://swarm-gateways.net");
}

upload

web3.bzz.upload(mixed)

将文件,文件夹或原始数据上传到 swarm 。


参数

  1. mixed - String|Buffer|Uint8Array|Object: The data to upload, can be a file content, file Buffer/Uint8Array, multiple files, or a directory or file (only in node.js). The following types are allowed:
    • String|Buffer|Uint8Array: A file content, file Uint8Array or Buffer to upload, or:
    • Object:
      1. Multiple key values for files and directories. The paths will be kept the same:
        • key must be the files path, or name, e.g. "/foo.txt" and its value is an object with:
          • type: The mime-type of the file, e.g. "text/html".
          • data: A file content, file Uint8Array or Buffer to upload.
      2. Upload a file or a directory from disk in Node.js. Requires and object with the following properties:
        • path: The path to the file or directory.
        • kind: The type of the source "directory", "file" or "data".
        • defaultFile (optional): Path of the “defaultFile” when "kind": "directory", e.g. "/index.html".
      3. Upload file or folder in the browser. Requres and object with the following properties:
        • pick: The file picker to launch. Can be "file", "directory" or "data".

返回值

Promise returning String: Returns the content hash of the manifest.


例子


    var bzz = web3.bzz;

    // raw data
    bzz.upload("test file").then(function(hash) {
        console.log("Uploaded file. Address:", hash);
    })

    // raw directory
    var dir = {
        "/foo.txt": {type: "text/plain", data: "sample file"},
        "/bar.txt": {type: "text/plain", data: "another file"}
    };
    bzz.upload(dir).then(function(hash) {
        console.log("Uploaded directory. Address:", hash);
    });

    // upload from disk in node.js
    bzz.upload({
        path: "/path/to/thing",      // path to data / file / directory
        kind: "directory",           // could also be "file" or "data"
        defaultFile: "/index.html"   // optional, and only for kind === "directory"
    })
    .then(console.log)
    .catch(console.log);

    // upload from disk in the browser
    bzz.upload({pick: "file"}) // could also be "directory" or "data"
    .then(console.log);

download


   web3.bzz.download(bzzHash [, localpath])

Downloads files and folders from swarm as buffer or to disk (only node.js).


参数

  1. bzzHash - String: The file or directory to download. If the hash is a raw file it will return a Buffer, if a manifest file, it will return the directory structure. If the localpath is given, it will return that path where it downloaded the files to.
  2. localpath - String: The local folder to download the content into. (only node.js)

返回值

Promise returning Buffer|Object|String: The Buffer of the file downloaded, an object with the directory structure, or the path where it was downloaded to.


例子


    var bzz = web3.bzz;

    // download raw file
    var fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
    bzz.download(fileHash).then(function(buffer) {
        console.log("Downloaded file:", buffer.toString());
    });

    // download directory, if the hash is manifest file.
    var dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";
    bzz.download(dirHash).then(function(dir) {
        console.log("Downloaded directory:");
        > {
            'bar.txt': { type: 'text/plain', data: <Buffer 61 6e 6f 74 68 65 72 20 66 69 6c 65> },
            'foo.txt': { type: 'text/plain', data: <Buffer 73 61 6d 70 6c 65 20 66 69 6c 65> }
        }
    });

    // download file/directory to disk (only node.js)
    var dirHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
    bzz.download(dirHash, "/target/dir")
    .then(path => console.log(`Downloaded directory to ${path}.`))
    .catch(console.log);

pick


   web3.bzz.pick.file()
   web3.bzz.pick.directory()
   web3.bzz.pick.data()

Opens a file picker in the browser to select file(s), directory or data.


参数

none


返回值

Promise returning Object: Returns the file or multiple files.


例子


    web3.bzz.pick.file()
    .then(console.log);
    > {
        ...
    }

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了