npm package: @neuraiproject/neurai-jswallet
Description: Neurai JavaScript wallet library
Latest version: 0.12.12

# neurai-jswallet

Neurai wallet library for JavaScript.
Non-custodial.
By default it interacts with the Neurai blockchain using public RPC services from
https://rpc.ting.finance/ for both testnet and mainnet.
You are free to use any RPC-service you like, including your own.
See section [Run your own blockchain node](#run-your-own-blockchain-node) for more info

##

EXPERIMENTAL.

This lib needs a lot of testing before being used in production.
Only use on mainnet if you "play around".
### 
## Example code

To run these code examples

1. Create an empty npm project
2. Install `@neuraiproject/neurai-jswallet`
3. Create a .mjs file called `index.mjs`

### Minimalistic example

```
import NeuraiWallet from "@neuraiproject/neurai-jswallet";

NeuraiWallet.createInstance({
   mnemonic: "horse sort develop lab chest talk gift damp session sun festival squirrel",
   network: "xna-test"
})
   .then(wallet => wallet.getBalance())
   .then(console.log);
```
### Some stuff you can do
```
import NeuraiWallet from "@neuraiproject/neurai-jswallet";
const wallet = await NeuraiWallet.createInstance({
  mnemonic:
    "horse sort develop lab chest talk gift damp session sun festival squirrel",
  network: "xna-test",
});

//OK now you have your wallet

//Example, get your addresses
const addresses = wallet.getAddresses();

//Address objects contains meta data about addresses, such as path/private key
const addressObjects = wallet.getAddressObjects();
 
//Get assets the wallet holds (not including mempool transactions) 
const assets = await wallet.getAssets();

//Get balance of base currency, like XNA, not including mempool transactions
const balance = await wallet.getBalance();


const changeAddress = await wallet.getChangeAddress();
const receiveAddress = await wallet.getReceiveAddress();

const firstPrivateKey = wallet.getPrivateKeyByAddress(addresses[0]);

//History, is the list of deltas for all the addresses in this wallet
const history = await wallet.getHistory();

//Get this wallets entries in the mempool right now
const mempool = await wallet.getMempool();
 
//Example send and print out the id, will throw exception if fails
const sendResult = await wallet.send({
  toAddress: "muTv54qzXc6ozEc1RH2JbM92jzpBtVJBbw",
  amount: 1,
});
console.log(sendResult.transactionId);
```
### Configure to use with your local node

In this example we run a local node in testnet mode, and RPC port is set to 8888

```
const wallet = await NeuraiWallet.createInstance({
    mnemonic,
    network: "xna-test",
    rpc_password: "mypassword",
    rpc_username: "myuser",
    rpc_url: "http://localhost:8888",
  });
```

### Send XNA and ASSETS

```
//index.mjs very important that file extension is .mjs
import NeuraiWallet from "@neuraiproject/neurai-jswallet";

//This wallet belongs to account "Crazy Cat" on https://rpc-testnet.neurai.org/signin/
const options = {
  mnemonic:
    "mesh beef tuition ensure apart picture rabbit tomato ancient someone alter embrace",
  network: "xna-test",
};
const wallet = await NeuraiWallet.createInstance(options);
const addy = await wallet.getReceiveAddress();
console.log("My receive address", addy);

//Send 100 XNA to Barry Crump on https://rpc-testnet.neurai.org/
await wallet.send({
  //Send 100 XNA
  toAddress: "mhBKhj5FxzBu1h8U6pSB16pwmjP7xo4ehG",
  amount: 100,
  assetName:"XNA",
});

//Send 313 BUTTER tokens to Barry Crump on https://rpc-testnet.neurai.org/
const transactionId = await wallet.send({
  assetName: "BUTTER",
  amount: 313,
  toAddress: "mhBKhj5FxzBu1h8U6pSB16pwmjP7xo4ehG",
});
console.log("Sending", transactionId);
```

### Send many

```
//index.mjs very important that file extension is .mjs
import NeuraiWallet from "@neuraiproject/neurai-jswallet";

//This wallet belongs to account "Crazy Cat" on https://rpc-testnet.neurai.org/signin/
const options = {
  mnemonic:
    "mesh beef tuition ensure apart picture rabbit tomato ancient someone alter embrace",
  network: "xna-test",
};
const wallet = await NeuraiWallet.createInstance(options);

//Send asset BUTTER to multiple recipients
const result = await wallet.sendMany({
  assetName: "BUTTER",
  outputs: {
    muTv54qzXc6ozEc1RH2JbM92jzpBtVJBbw: 1,
    mhWahrbRX6xBBrRjCo6ZkazaugXftD1CbM: 2,
  },
});

console.log("Sending", result.transactionId);

```

## Passphrase Support (BIP39 25th Word)

For enhanced security, you can use an optional passphrase (also known as the "25th word"). 
This creates a completely different set of addresses from the same mnemonic.

**Important**: If you lose your passphrase, you cannot recover your wallet even with the mnemonic!

```javascript
import NeuraiWallet from "@neuraiproject/neurai-jswallet";

const mnemonic = "your twelve word mnemonic phrase here";
const passphrase = "my secret passphrase"; // Optional but highly secure

// Create wallet with passphrase
const wallet = await NeuraiWallet.createInstance({
  mnemonic: mnemonic,
  network: "xna-test",
  passphrase: passphrase  // This creates a completely different wallet
});

// Without passphrase (or empty string) - creates different addresses
const walletNoPass = await NeuraiWallet.createInstance({
  mnemonic: mnemonic,
  network: "xna-test"
  // No passphrase = different wallet than above
});
```

**Use cases for passphrase:**
- Extra layer of security beyond the mnemonic
- Create multiple wallets from a single mnemonic
- Plausible deniability (different passphrases = different wallets)

## API

When you create your instance of a wallet you can specify some stuff.

You can specify your own RPC node URL and username/password.

```
export interface IOptions {
    mnemonic: string;
    network?: ChainType; (that is "xna" | "xna-test")
    rpc_username?: string;
    rpc_password?: string;
    rpc_url?: string;
    passphrase?: string;
}
```

[Check the TypeScript definitions ](./dist/types.d.ts) for all the details

### Run your own blockchain node

If you want to run your own internet exposed Node, checkout our RPC proxy.
With **RPC proxy** and **Cloudlare** you can get a secure endpoint like
https://rpc.mydomain.com/rpc
checkout

- https://github.com/neuraiproject/neurai-rpc-proxy
- https://www.cloudflare.com/products/tunnel/

## Advanced - pure RPC

You have access to the underlaying RPC function, wallet.rpc.
See example

```
import NeuraiWallet from "@neuraiproject/neurai-jswallet";
async function main(){
  const wallet = await NeuraiWallet.createInstance({
    mnemonic: "horse sort develop lab chest talk gift damp session sun festival squirrel",
    network: "xna-test",
  });
  const blockhash = await wallet.rpc("getbestblockhash", []);
  const block = await wallet.rpc("getblock", [blockhash]);
  console.log(block);
}
main();
```