0.1.42 • Published 2 years ago

swarm-js v0.1.42

Weekly downloads
139,014
License
MIT
Repository
github
Last release
2 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-2861yjj-test1wan3-bzzweb3xweb3x-esweb3trueweb3gnc-bzzweb3-bzzweb3-bzz-danielweb_anmo-bzzvdc-web3zona-x@olympfin/olymp-swap-libxftest3-bzzxdc3-bzzxdc3-rud-bzz@quainetwork/web3-bzz@rangersprotocolweb3/web3-bzz@redbud-hk/web3-bzz@nirin100/quadratic-protocol@nirin100/quadratic-sdk@node-real/web3-bzz@o0o2022sg/crypto-wallet-core@o0o2022/crypto-wallet-core@nodeberry/solidity-payment-processoreubweb3-bzzesocialenefti-galeri@sudophunk/fulido@uhhhh2/web3x@uhhhh2/web3x-esdynamics-contracts-x@smilo-platform/web3-bzz@thismr/bitmindtest-core@sipc/web3-bzz@theqrl/web3-bzz@rocketprotocolweb3/web3-bzzgassefalsgassefals-2@arianelabs/hweb3-bzzfico-web3-bzz@yak-spirit/yak-swap-ui@alayanetwork/web3x@afria/afria-libraries@xswap/v2-corefaucet-sdk3f-web3-bzzdc-web3-bzzdc3-bzz@danielsmilo/web3-bzz@danielsmilo/web3bzzdaniel@dileepfrogorg/web3-bzz@ethofs/sdk@ethofs/sdk-web@ethoprotocol/sdkmeme3@binance-chain/web3-bzzdate-to-block-eth@eliteswap/v2-core@enslogin/sdk@chakra-swap/corebif-bzz@cobinhood/web3-bzz@didux-io/web3-bzzhtdf_web3cotrader-dex-sdk@borealisswap/borealis-swap-lib@bonsaiswapv3/core@bonsaiswap-lib/lib@bonsaiswapv3/deployjadis-js@ianaya89/web3-bzz@igniswap/igni-swap-lib@fksyuan/web3x@fundament/web3-bzzrue-mistopencollabopencollab-libpixiuswap-libs-sdkpixiu-swap-core@jackchim/web3-bzzsolidity-metadata-uploader@innoswap/corepec-bzzstorm3-bzz@kodinghandle/bullswap-libqudratic-uisusyweb-bzzquadratic-sdk@litedexdev/litedex-core-swap@materia-dex/materia-contracts-proxyquiofficiissweb3-bzztaiweb3jstest3-bzz
0.1.42

2 years ago

0.1.40

4 years ago

0.1.39

6 years ago

0.1.38

6 years ago

0.1.37

7 years ago

0.1.36

7 years ago

0.1.35

7 years ago

0.1.34

7 years ago

0.1.33

7 years ago

0.1.32

7 years ago

0.1.31

7 years ago

0.1.30

7 years ago

0.1.29

7 years ago

0.1.28

7 years ago

0.1.27

7 years ago

0.1.26

7 years ago

0.1.25

7 years ago

0.1.24

7 years ago

0.1.23

7 years ago

0.1.22

7 years ago

0.1.21

7 years ago

0.1.20

7 years ago

0.1.19

7 years ago

0.1.18

7 years ago

0.1.17

7 years ago

0.1.16

7 years ago

0.1.15

7 years ago

0.1.14

7 years ago

0.1.13

7 years ago

0.1.12

7 years ago

0.1.11

7 years ago

0.1.10

7 years ago

0.1.9

7 years ago

0.1.8

7 years ago

0.1.7

7 years ago

0.1.6

7 years ago

0.1.5

7 years ago

0.1.4

7 years ago

0.1.3

7 years ago

0.1.1

7 years ago

0.1.0

7 years ago