Skip to content

Commit

Permalink
support typescript and rename project to monero-ts (#108)
Browse files Browse the repository at this point in the history
update classes and data models to typescript
add .babelrc, tsconfig.ts, and other ts config
improve stack traces from source maps
replace biginteger.js with BigInt
test and distribute commonjs
update typedocs and other documentation
  • Loading branch information
woodser committed Oct 1, 2023
1 parent 330e75a commit eae1da9
Show file tree
Hide file tree
Showing 187 changed files with 23,887 additions and 25,160 deletions.
26 changes: 26 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"presets": [
[
"@babel/preset-env",
{
"targets": {
"node": "current"
}
}
],
"@babel/preset-typescript"
],
"plugins": [
"@babel/plugin-transform-runtime"
],
"sourceType": "unambiguous",
"sourceMaps": "inline",
"retainLines": true,
"ignore": [
"node_modules",
"build",
"browser_build",
"dist",
"configs"
]
}
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ test_products
/test_wallets/
/build
/browser_build/
/dist/dist/
/dist/src/test/
2 changes: 1 addition & 1 deletion .project
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>monero-javascript</name>
<name>monero-ts</name>
<comment></comment>
<projects>
</projects>
Expand Down
9 changes: 5 additions & 4 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.4.1)

project(monero-javascript-wasm)
project(monero-ts-wasm)

# build with exception whitelist from file
file(STRINGS wasm_exception_whitelist.txt WASM_EXCEPTION_WHITELIST)
Expand Down Expand Up @@ -151,7 +151,7 @@ set_target_properties(
set(
MONERO_WALLET_KEYS_SRC_FILES

# monero-javascript WASM bridge
# monero-ts WASM bridge
src/main/cpp/index.cpp
src/main/cpp/monero_wasm_bridge.cpp

Expand Down Expand Up @@ -226,7 +226,7 @@ set(
set(
MONERO_WALLET_FULL_SRC_FILES

# monero-javascript WASM bridge
# monero-ts WASM bridge
src/main/cpp/http_client_wasm.cpp

# monero-cpp (modified for WASM)
Expand Down Expand Up @@ -278,7 +278,8 @@ EMCC_LINKER_FLAGS_BASE
# TODO? does EXPORT_NAME need to be the same for both targets? (or should it be set per-target with …_WASM, …_ASMJS?)

"-Wall -Werror -Wl,--allow-undefined -std=c++14 -Oz \
--bind -s MODULARIZE=1 \
--bind \
-s MODULARIZE=1 \
-s 'EXPORT_NAME=\"monero_javascript\"' \
-s ERROR_ON_UNDEFINED_SYMBOLS=0 \
-s ASSERTIONS=0 \
Expand Down
96 changes: 49 additions & 47 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Monero JavaScript Library
# Monero TypeScript Library

A JavaScript library for creating Monero applications using RPC and WebAssembly bindings to [monero v0.18.2.2 'Flourine Fermie'](https://github.com/monero-project/monero/tree/v0.18.2.2).
A TypeScript library for creating Monero applications using RPC and WebAssembly bindings to [monero v0.18.2.2 'Flourine Fermie'](https://github.com/monero-project/monero/tree/v0.18.2.2).

* Supports client-side wallets in Node.js and the browser using WebAssembly.
* Supports wallet and daemon RPC clients.
* Supports multisig, view-only, and offline wallets.
* Wallet types are interchangeable by conforming to a [common interface](https://moneroecosystem.org/monero-javascript/MoneroWallet.html).
* Wallet types are interchangeable by conforming to a [common interface](https://moneroecosystem.org/monero-ts/MoneroWallet.html).
* Uses a clearly defined [data model and API specification](https://moneroecosystem.org/monero-java/monero-spec.pdf) intended to be intuitive and robust.
* [Query wallet transactions, transfers, and outputs](docs/developer_guide/query_data_model.md) by their properties.
* Fetch and process binary data from the daemon (e.g. raw blocks).
Expand All @@ -17,7 +17,7 @@ A JavaScript library for creating Monero applications using RPC and WebAssembly
* [Architecture](#architecture)
* [Sample code](#sample-code)
* [Documentation](#documentation)
* [Using monero-javascript in your project](#using-monero-javascript-in-your-project)
* [Using monero-ts in your project](#using-monero-ts-in-your-project)
* [Building WebAssembly binaries from source](#building-webassembly-binaries-from-source)
* [Running tests](#running-tests)
* [Related projects](#related-projects)
Expand All @@ -28,66 +28,68 @@ A JavaScript library for creating Monero applications using RPC and WebAssembly

<p align="center">
<img width="85%" height="auto" src="docs/img/architecture.png"/><br>
<i>Build browser or Node.js applications using RPC or WebAssembly bindings to <a href="https://github.com/monero-project/monero">monero-project/monero</a>. Wallet implementations are interchangeable by conforming to a common interface, <a href="https://moneroecosystem.org/monero-javascript/MoneroWallet.html">MoneroWallet.js</a>.</i>
<i>Build browser or Node.js applications using RPC or WebAssembly bindings to <a href="https://github.com/monero-project/monero">monero-project/monero</a>. Wallet implementations are interchangeable by conforming to a common interface, <a href="https://moneroecosystem.org/monero-ts/MoneroWallet.html">MoneroWallet.ts</a>.</i>
</p>

## Sample code

```js
// import library
const monerojs = require("monero-javascript");
```typescript
// import monero-ts (or import types individually)
import * as moneroTs from "monero-ts";

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

// open wallet on monero-wallet-rpc
let walletRpc = await monerojs.connectToWalletRpc("http://localhost:38084", "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 seed phrase using WebAssembly bindings to monero-project
let walletFull = await monerojs.createWalletFull({
path: "sample_wallet_full",
// create wallet from mnemonic phrase using WebAssembly bindings to monero-project
let walletFull = await moneroTs.createWalletFull({
path: "sample_wallet_full"
password: "supersecretpassword123",
networkType: "stagenet",
serverUri: "http://localhost:38081",
serverUsername: "superuser",
serverPassword: "abctesting123",
networkType: moneroTs.MoneroNetworkType.TESTNET,
seed: "hefty value scenic...",
restoreHeight: 573936,
server: { // provide url or MoneroRpcConnection
uri: "http://localhost:28081",
username: "superuser",
password: "abctesting123"
}
});

// synchronize with progress notifications
await walletFull.sync(new class extends monerojs.MoneroWalletListener {
onSyncProgress(height, startHeight, endHeight, percentDone, message) {
await walletFull.sync(new class extends moneroTs.MoneroWalletListener {
async onSyncProgress(height: number, startHeight: number, endHeight: number, percentDone: number, message: string) {
// feed a progress bar?
}
});
} as moneroTs.MoneroWalletListener);

// synchronize in the background every 5 seconds
await walletFull.startSyncing(5000);

// receive notifications when funds are received, confirmed, and unlocked
let fundsReceived = false;
await walletFull.addListener(new class extends monerojs.MoneroWalletListener {
onOutputReceived(output) {
await walletFull.addListener(new class extends moneroTs.MoneroWalletListener {
async onOutputReceived(output: moneroTs.MoneroOutputWallet) {
let amount = output.getAmount();
let txHash = output.getTx().getHash();
let isConfirmed = output.getTx().isConfirmed();
let isLocked = output.getTx().isLocked();
let isConfirmed = output.getTx().getIsConfirmed();
let isLocked = output.getTx().getIsLocked();
fundsReceived = true;
}
});

// connect to wallet RPC endpoint and open wallet
let walletRpc = await moneroTs.connectToWalletRpc("http://localhost:28084", "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

// send funds from RPC wallet to WebAssembly wallet
let createdTx = await walletRpc.createTx({
accountIndex: 0,
address: await walletFull.getAddress(1, 0),
amount: "250000000000", // send 0.25 XMR (denominated in atomic units)
amount: BigInt("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... ?"
Expand All @@ -103,12 +105,8 @@ await walletFull.close(true);

## Documentation

* [JSDocs](https://moneroecosystem.org/monero-javascript/MoneroWallet.html)
* [TypeDocs](https://moneroecosystem.org/monero-ts/MoneroWallet.html)
* [API and model overview with visual diagrams](https://moneroecosystem.org/monero-java/monero-spec.pdf)
* [Mocha tests](src/test)
* [Installing prerequisites](docs/developer_guide/installing_prerequisites.md)
* [Getting started part 1: creating a Node.js application](docs/developer_guide/getting_started_p1.md)
* [Getting started part 2: creating a web application](docs/developer_guide/getting_started_p2.md)
* [Creating wallets](docs/developer_guide/creating_wallets.md)
* [The data model: blocks, transactions, transfers, and outputs](docs/developer_guide/data_model.md)
* [Getting transactions, transfers, and outputs](docs/developer_guide/query_data_model.md)
Expand All @@ -117,12 +115,16 @@ await walletFull.close(true);
* [View-only and offline wallets](docs/developer_guide/view_only_offline.md)
* [Connection manager](docs/developer_guide/connection_manager.md)
* [HTTPS and self-signed certificates](./docs/developer_guide/https_and_self_signed_certificates.md)
* [Mocha tests](src/test)
* [Installing prerequisites](docs/developer_guide/installing_prerequisites.md)
* [Getting started part 1: creating a Node.js application](docs/developer_guide/getting_started_p1.md)
* [Getting started part 2: creating a web application](docs/developer_guide/getting_started_p2.md)

## Using monero-javascript in your project
## Using monero-ts in your project

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

#### Running in Node.js

Expand All @@ -148,22 +150,22 @@ Compiled WebAssembly binaries are committed to ./dist for convenience, but these
2. `cd emsdk`
3. `git pull && ./emsdk install 3.1.10 && ./emsdk activate 3.1.10 && source ./emsdk_env.sh`
4. `export EMSCRIPTEN=path/to/emsdk/upstream/emscripten` (change for your system)
2. Clone monero-javascript repository: `git clone --recursive https://github.com/monero-ecosystem/monero-javascript.git`
3. `cd monero-javascript`
2. Clone monero-ts repository: `git clone --recursive https://github.com/monero-ecosystem/monero-ts.git`
3. `cd monero-ts`
4. `./bin/update_submodules.sh`
5. Modify ./external/monero-cpp/external/monero-project/src/crypto/wallet/CMakeLists.txt from `set(MONERO_WALLET_CRYPTO_LIBRARY "auto" ...` to `set(MONERO_WALLET_CRYPTO_LIBRARY "cn" ...`.
6. [Download and install](https://unbound.docs.nlnetlabs.nl/en/latest/getting-started/installation.html) unbound 1.17.0 to your home directory (`~`).
7. `./bin/build_all.sh` (install [monero-project dependencies](https://github.com/monero-project/monero#dependencies) as needed for your system)

## Running tests

1. Clone the project repository: `git clone https://github.com/monero-ecosystem/monero-javascript.git`
2. `cd monero-javascript`
1. Clone the project repository: `git clone https://github.com/monero-ecosystem/monero-ts.git`
2. `cd monero-ts`
3. Start RPC servers:
1. Download and install [Monero CLI](https://web.getmonero.org/downloads/).
2. Start monerod, e.g.: `./monerod --testnet` (or use a remote daemon).
3. Start monero-wallet-rpc, e.g.: `./monero-wallet-rpc --daemon-address http://localhost:38081 --testnet --rpc-bind-port 28084 --rpc-login rpc_user:abc123 --wallet-dir ./`
4. Configure the appropriate RPC endpoints, authentication, and other settings in [TestUtils.js](src/test/utils/TestUtils.js) (e.g. `WALLET_RPC_CONFIG` and `DAEMON_RPC_CONFIG`).
4. Configure the appropriate RPC endpoints, authentication, and other settings in [TestUtils.ts](src/test/utils/TestUtils.ts) (e.g. `WALLET_RPC_CONFIG` and `DAEMON_RPC_CONFIG`).

#### Running tests in Node.js

Expand All @@ -180,7 +182,7 @@ Compiled WebAssembly binaries are committed to ./dist for convenience, but these

* [monero-java](https://github.com/monero-ecosystem/monero-java)
* [monero-cpp](https://github.com/monero-ecosystem/monero-cpp)
* [xmr-sample-app](https://github.com/woodser/xmr-sample-app) - sample web application using monero-javascript
* [xmr-sample-app](https://github.com/woodser/xmr-sample-app) - sample web application using monero-ts
* [monerostresstester.com](https://github.com/woodser/monerostresstester.com) - repeatedly sends txs to self to stress test the network (under development)
* [monero-deposit-scanner](https://github.com/woodser/monero-deposit-scanner) - scan for incoming deposits to an address using a view key (under development)
* [monerowebwallet.com](https://github.com/woodser/monerowebwallet.com) - open-source, client-side web wallet (under development)
Expand Down
11 changes: 5 additions & 6 deletions bin/build_all.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,17 @@ HOST_NCORES=$(nproc 2>/dev/null || shell nproc 2>/dev/null || sysctl -n hw.ncpu
make release-static -j$HOST_NCORES # don't exit because this will build translations directory even if build fails
cd ../../../../ || exit 1

# install node modules
npm install || exit 1

# build boost
./bin/build_boost_emscripten.sh || exit 1

# build openssl
./bin/build_openssl_emscripten.sh || exit 1

# build webassembly for distribution
./bin/build_wasm_emscripten.sh || exit 1

# build web worker
npm install --force || exit 1
./bin/build_web_worker.sh || exit 1
# build dist (wasm, commonjs , web worker)
./bin/build_dist.sh || exit 1

# build browser tests
./bin/build_browser_tests.sh || exit 1
1 change: 1 addition & 0 deletions bin/build_browser_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ cp -R dist/* browser_build/ || exit 1
cp src/test/browser/tests.html browser_build/tests.html || exit 1
cp src/test/browser/favicon.ico browser_build/favicon.ico || exit 1
cp node_modules/mocha/mocha.js browser_build/mocha.js || exit 1
cp node_modules/mocha/mocha.js.map browser_build/mocha.js.map || exit 1
cp node_modules/mocha/mocha.css browser_build/mocha.css || exit 1

# start server
Expand Down
3 changes: 2 additions & 1 deletion bin/build_dist.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/sh

./bin/build_wasm_emscripten.sh || exit 1
./bin/build_web_worker.sh || exit 1
./bin/build_web_worker.sh || exit 1
npm run build_commonjs || exit 1
4 changes: 2 additions & 2 deletions bin/start_wallet_rpc_test_servers.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/sh

# run monero-wallet-rpc servers until terminated
node ./src/test/utils/RunWalletRpcTestServers.js
# run monero-wallet-rpc servers until terminated
node ./dist/src/test/utils/RunWalletRpcTestServers.js
23 changes: 0 additions & 23 deletions configs/jsdoc_config.js

This file was deleted.

45 changes: 0 additions & 45 deletions configs/jsdoc_plugins.js

This file was deleted.

Loading

0 comments on commit eae1da9

Please sign in to comment.