GithubHelp home page GithubHelp logo

goshiz / haven-web-core Goto Github PK

View Code? Open in Web Editor NEW

This project forked from haven-protocol-org/haven-web-core

0.0 0.0 0.0 55.91 MB

A Node.js library for using Haven

License: MIT License

JavaScript 90.76% Shell 1.39% CMake 0.97% C++ 6.83% Python 0.04% HTML 0.02%

haven-web-core's Introduction

Haven Web Core a Fork of Monero Javascript

A Node.js library for creating Haven Apps using RPC or WebAssembly

  • Supports wallet and daemon RPC clients.
  • Supports client-side wallets in Node.js or the browser using WebAssembly.
  • Supports multisig, view-only, and offline wallets.
  • Wallet types are interchangeable by conforming to a common interface.
  • Uses a clearly defined data model and API specification intended to be intuitive and robust.
  • Query wallet transactions, transfers, and outputs by their properties.
  • Fetch and process binary data from the daemon (e.g. raw blocks).
  • Receive notifications when blocks are added to the chain or when wallets sync, send, or receive.
  • Over 250 passing Mocha tests.

Table of contents

Architecture


Build Node.js or browser applications using RPC or WebAssembly bindings to monero-project/monero. Wallet implementations are interchangeable by conforming to a common interface, MoneroWallet.js.

Sample code

This code introduces the API used in monero-javascript. See the JSDocs, API specification, or Mocha tests for more detail.

// import library
const monerojs = require("monero-javascript");

// connect to a daemon
let daemon = monerojs.connectToDaemonRpc("http://localhost:38081", "superuser", "abctesting123");
let height = await daemon.getHeight();            // 1523651
let feeEstimate = await daemon.getFeeEstimate();  // 1014313512
let txsInPool = await daemon.getTxPool();         // get transactions in the pool

// open wallet on monero-wallet-rpc
let walletRpc = monerojs.connectToWalletRpc("http://localhost:38083", "rpc_user", "abc123");
await walletRpc.openWallet("sample_wallet_rpc", "supersecretpassword123");
let primaryAddress = await walletRpc.getPrimaryAddress(); // 555zgduFhmKd2o8rPUz...
let balance = await walletRpc.getBalance();               // 533648366742
let txs = await walletRpc.getTxs();                       // get transactions containing transfers to/from the wallet

// create wallet from mnemonic phrase using WebAssembly bindings to Monero Core
let walletWasm = await monerojs.createWalletWasm({
  path: "sample_wallet_wasm",
  password: "supersecretpassword123",
  networkType: "stagenet",
  serverUri: "http://localhost:38081",
  serverUsername: "superuser",
  serverPassword: "abctesting123",
  mnemonic: "hefty value scenic...",
  restoreHeight: 573936,
});

// synchronize with progress notifications
await walletWasm.sync(new class extends monerojs.MoneroWalletListener {
  onSyncProgress(height, startHeight, endHeight, percentDone, message) {
    // feed a progress bar?
  }
});

// synchronize in the background
await walletWasm.startSyncing();

// listen for incoming transfers
let fundsReceived = false;
await walletWasm.addListener(new class extends monerojs.MoneroWalletListener {
  onOutputReceived(output) {
    let amount = output.getAmount();
    let txHash = output.getTx().getHash();
    fundsReceived = true;
  }
});

// send funds from RPC wallet to WebAssembly wallet
let createdTx = await walletRpc.createTx({
  accountIndex: 0,
  address: await walletWasm.getAddress(1, 0),
  amount: "250000000000", // send 0.25 XMR (denominated in atomic units)
  relay: false // create transaction and relay to the network if true
});
let fee = createdTx.getFee(); // "Are you sure you want to send... ?"
await walletRpc.relayTx(createdTx); // relay the transaction

// recipient receives unconfirmed funds within 10 seconds
await new Promise(function(resolve) { setTimeout(resolve, 10000); });
assert(fundsReceived);

// save and close WebAssembly wallet
await walletWasm.close(true);

Using monero-javascript in your project

  1. cd your_project or mkdir your_project && cd your_project && npm init
  2. npm install monero-javascript
  3. Add require("monero-javascript") to your application code.

If using RPC servers:

  1. Download and install Monero CLI.
  2. Start monero-daemon-rpc, e.g.: ./monerod --stagenet (or use a remote daemon).
  3. Start monero-wallet-rpc, e.g.: ./monero-wallet-rpc --daemon-address http://localhost:38081 --stagenet --rpc-bind-port 38083 --rpc-login rpc_user:abc123 --wallet-dir ./

Building WebAssembly binaries from source

This project uses WebAssembly to package and execute Monero's source code for use in a browser or other WebAssembly-supported environments.

Compiled WebAssembly binaries are committed to ./dist for convenience, but these files can be built independently from source code:

  1. Install and activate emscripten
    1. Clone emscripten repository: git clone https://github.com/emscripten-core/emsdk.git
    2. cd emsdk
    3. git pull && ./emsdk install latest-upstream && ./emsdk activate latest-upstream && source ./emsdk_env.sh
    4. export EMSCRIPTEN=/absolute/path/to/emsdk/upstream/emscripten (change for your system)
  2. Clone monero-javascript repository: git clone https://github.com/monero-ecosystem/monero-javascript.git
  3. cd monero-javascript
  4. ./bin/build_all.sh

Developer guide

Running tests

  1. Clone the project repository: git clone https://github.com/monero-ecosystem/monero-javascript.git
  2. cd monero-javascript
  3. Start RPC servers:
    1. Download and install Monero CLI.
    2. Start monero-daemon-rpc, e.g.: ./monerod --stagenet (or use a remote daemon).
    3. Start monero-wallet-rpc, e.g.: ./monero-wallet-rpc --daemon-address http://localhost:38081 --stagenet --rpc-bind-port 38083 --rpc-login rpc_user:abc123 --wallet-dir ./
  4. Configure the appropriate RPC endpoints and authentication by modifying WALLET_RPC_CONFIG and DAEMON_RPC_CONFIG in TestUtils.js.

Running tests in Node.js

  • Run all tests: npm test
  • Run tests by their description: node_modules/mocha/bin/mocha src/test/TestAll --grep "Can get transactions" --timeout 2000000

Running tests in the browser

  1. ./bin/build_browser_tests.sh
  2. Access http://localhost:9100/tests.html in a browser

See also

License

This project is licensed under MIT.

Donations

If this library brings you value, please consider donating.


46FR1GKVqFNQnDiFkH7AuzbUBrGQwz2VdaXTDD4jcjRE8YkkoTYTmZ2Vohsz9gLSqkj5EM6ai9Q7sBoX4FPPYJdGKQQXPVz

haven-web-core's People

Contributors

beantosser avatar dependabot[bot] avatar goshiz avatar martyhav avatar trasherdk avatar woodser avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.