0.1.42 • Published 3 years ago

swarm-js v0.1.42

Weekly downloads
139,014
License
MIT
Repository
github
Last release
3 years ago

Disclaimer

This library isn't activelly maintained as I moved on to other things. If you'd like to maintain it, please let me know. For now, I think I can point to Erebos: https://erebos.js.org/

Swarm.js

This library allows you to interact with the Swarm network from JavaScript.

Getting started

  1. Install

    npm install swarm-js
  2. Import

    // Loads the Swarm API pointing to the official gateway
    const swarm = require("swarm-js").at("http://swarm-gateways.net");

Examples

Uploads

  • With JSON:

    • Raw data:

      const file = "test file"; // could also be an Uint8Array of binary data
      swarm.upload(file).then(hash => {
        console.log("Uploaded file. Address:", hash);
      })
    • Directory:

      To upload a directory, just call swarm.upload(directory), where directory is an object mapping paths to entries, those containing a mime-type and the data (Uint8Array or UTF-8 String).

      const dir = {
        "/foo.txt": {type: "text/plain", data: "file 0"},
        "/bar.txt": {type: "text/plain", data: "file 1"}
      };
      swarm.upload(dir).then(hash => {
        console.log("Uploaded directory. Address:", hash);
      });
  • From disk:

    • On Node.js:

      swarm.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);
    • On browsers:

      // only works inside an event
      document.onClick = function() {
        swarm.upload({pick: "file"}) // could also be "directory" or "data"
          .then(alert);
      };

Downloads

  • With JSON:

    • Raw data:

      const fileHash = "a5c10851ef054c268a2438f10a21f6efe3dc3dcdcc2ea0e6a1a7a38bf8c91e23";
      swarm.download(fileHash).then(array => {
        console.log("Downloaded file:", swarm.toString(array));
      });
    • Directory:

      const dirHash = "7e980476df218c05ecfcb0a2ca73597193a34c5a9d6da84d54e295ecd8e0c641";
      swarm.download(dirHash).then(dir => {
        console.log("Downloaded directory:");
        for (let path in dir) {
          console.log("-", path, ":", dir[path].data.toString());
        }
      });
  • To disk:

    • On Node.js:

      swarm.download("DAPP_HASH", "/target/dir")
        .then(path => console.log(`Downloaded DApp to ${path}.`))
        .catch(console.log);
    • On browser:

      (Just link the Swarm URL.)

SwarmHash

console.log(swarm.hash("unicode string áéíóú λ"));
console.log(swarm.hash("0x41414141"));
console.log(swarm.hash([65, 65, 65, 65]));
console.log(swarm.hash(new Uint8Array([65, 65, 65, 65])));

More

For more examples, check out examples.

web3_htdfweb3ycaver-web3-01@micdeb-ariane/hweb3-bzz@infinitebrahmanuniverse/nolb-swar@everything-registry/sub-chunk-2861taiweb3jssweb3-bzzwan3-bzzweb3xweb3trueweb3x-esweb3gnc-bzzweb3bzweb_anmo-bzzwebb3-bzzxdc3-bzzxdc3-rud-bzzxftest3-bzzstorm3-bzztest3-bzztest3js-bzzsusyweb-bzz@cobinhood/web3-bzz@dileepfrogorg/web3-bzz@borealisswap/borealis-swap-libjadis-jssolidity-metadata-uploader@arianelabs/hweb3-bzz@afria/afria-libraries@alayanetwork/web3x@binance-chain/web3-bzz@chakra-swap/core3f-web3-bzz@bonsaiswap-lib/lib@bonsaiswapv3/core@bonsaiswapv3/deploy@didux-io/web3-bzz@danielsmilo/web3-bzz@danielsmilo/web3bzzdanielyjj-test1zona-xweb-bzweb-bzzweb3-bzzweb3-bzz-danielvdc-web3@ianaya89/web3-bzz@fksyuan/web3x@fundament/web3-bzz@eliteswap/v2-core@ethofs/sdk@ethofs/sdk-web@ethoprotocol/sdk@innoswap/core@litedexdev/litedex-core-swap@materia-dex/materia-contracts-proxy@nirin100/quadratic-protocol@nirin100/quadratic-sdk@node-real/web3-bzz@nodeberry/solidity-payment-processor@enslogin/sdk@jackchim/web3-bzz@kodinghandle/bullswap-lib@igniswap/igni-swap-lib@olympfin/olymp-swap-lib@rangersprotocolweb3/web3-bzz@quainetwork/web3-bzz@redbud-hk/web3-bzz@o0o2022/crypto-wallet-core@o0o2022sg/crypto-wallet-core@rocketprotocolweb3/web3-bzzopencollabopencollab-libpec-bzzrue-mistpixiu-swap-corepixiuswap-libs-sdkqudratic-uiquiofficiisquadratic-sdkshowgalleryhtdf_web3loading-animatedmeme3@theqrl/web3-bzz@thismr/bitmindtest-core@uhhhh2/web3x@uhhhh2/web3x-es@xswap/v2-core@yak-spirit/yak-swap-uicotrader-dex-sdkdc-web3-bzzdc3-bzzdate-to-block-ethbif-bzzdynamics-contracts-xenefti-galeriesocialeubweb3-bzz
0.1.42

3 years ago

0.1.40

5 years ago

0.1.39

7 years ago

0.1.38

7 years ago

0.1.37

8 years ago

0.1.36

8 years ago

0.1.35

8 years ago

0.1.34

8 years ago

0.1.33

8 years ago

0.1.32

8 years ago

0.1.31

8 years ago

0.1.30

8 years ago

0.1.29

8 years ago

0.1.28

8 years ago

0.1.27

8 years ago

0.1.26

8 years ago

0.1.25

8 years ago

0.1.24

8 years ago

0.1.23

8 years ago

0.1.22

8 years ago

0.1.21

8 years ago

0.1.20

8 years ago

0.1.19

8 years ago

0.1.18

8 years ago

0.1.17

8 years ago

0.1.16

8 years ago

0.1.15

8 years ago

0.1.14

8 years ago

0.1.13

8 years ago

0.1.12

8 years ago

0.1.11

8 years ago

0.1.10

8 years ago

0.1.9

8 years ago

0.1.8

8 years ago

0.1.7

8 years ago

0.1.6

8 years ago

0.1.5

8 years ago

0.1.4

8 years ago

0.1.3

8 years ago

0.1.1

8 years ago

0.1.0

8 years ago