1.3.27 • Published 5 years ago

oip-hdmw v1.3.27

Weekly downloads
6
License
MIT
Repository
github
Last release
5 years ago

Build Status npm.io Coverage Status

OIP HD-MultiWallet

oip-hdmw is a BIP44 Javascript Lite Wallet. You can spawn and recover the entire wallet for each coin using just a single BIP-39 Mnemonic. We use an insight-api server as the source of truth for Wallet balances and unspent outputs instead of syncing Block Headers like most SPV wallets do.

Table of Contents

Installation Instructions

You can install the latest version by running the following npm install command.

$ npm install --save oip-hdmw

Getting Started

Creating your first Wallet

Creating a wallet is extremely simple! To create a new wallet with a random new Mnemonic, all we need to do is create a Wallet with no paramaters. After the wallet is created, we log the Mnemonic so that we can use it in our other examples

const HDMW = require('oip-hdmw')
const Wallet = HDMW.Wallet;

var myWallet = new Wallet();

console.log("My Mnemonic: '" + myWallet.getMnemonic() + "'")
// My Mnemonic: 'carbon panda replace drum guess heart inside useless random bulb hint industry'

Getting the Coins from your Wallet

Now that you have a Mnemonic for your wallet, lets go ahead and create the Wallet again, but this time, we will give it the Mnemonic to start from.

const HDMW = require('oip-hdmw')
const Wallet = HDMW.Wallet;

var myWallet = new Wallet('carbon panda replace drum guess heart inside useless random bulb hint industry');

console.log("My Wallets Coins: ", myWallet.getCoins())
// My Wallets Coins: {
//	bitcoin: Coin,
//	litecoin: Coin,
//	flo: Coin
//}

As you can see, we get back a JSON object containing each Coin along with an identifier that is the Coin name.

Getting your first Address

Now that we have created a new Wallet and accessed the Coins on the wallet, lets go ahead and get the Main Address for one of the coins. To do this, we will first need to get a Coin from the Wallet. To do this, we use the getCoin function and pass it the Coin name that we wish to get the Coin for. After we have grabbed the Coin, we run the getMainAddress function in order to get the main address for the Coin. After we have stored the Address returned to us by the Coin, we need to get the human readable Public key of the Address.

const HDMW = require('oip-hdmw')
const Wallet = HDMW.Wallet;

var myWallet = new Wallet('carbon panda replace drum guess heart inside useless random bulb hint industry');

var bitcoin = myWallet.getCoin('bitcoin');

var myMainAddress = bitcoin.getMainAddress();

console.log("My Wallets Bitcoin Main Address: ", myMainAddress.getPublicAddress());
// My Wallets Bitcoin Main Address: 13BW4eTvNFXBLeTjJQRgVxuiuStAFp1HfL

Sending your first Transaction

In order to send a transaction, we will need to have a balance on our Wallet first. Send some funds to the Address that you got in the last step. After you have sent some money to the Wallet, we can send our first transaction. To send the Transaction, use the sendPayment method.

const HDMW = require('oip-hdmw')
const Wallet = HDMW.Wallet;

var myWallet = new Wallet('carbon panda replace drum guess heart inside useless random bulb hint industry');

myWallet.sendPayment({
	to: { "12nP3k9tFKgQPJNkDDyNWqgjtm2bt3qq1b": 0.001 }
}).then(function(txid){
	console.log("Successfully sent Transaction! " + txid);
}).catch(function(error){
	console.error("Unable to send Transaction!", error)
})

When we send the transaction, it broadcasts it out to the Coin p2p network. After a few minutes your transaction should recieve its initial confirmation, and you would be ok to send another transaction.

If you wanted to send second transaction, before the first recieves its initial confirmation, you can do so by calling the same Wallet instance that you ran sendPayment on. OIP HDMW keeps track of the transactions it is using to spend from when it sends a payment, so if your application stops running, then restarts BEFORE the first transaction recieves a confirmation, it would not see the payment that it spent, and thus try to spend the same value as the first transaction. If your application is going to "restart" between sending transactions, it is suggested that you read the next section called "Saving and Reloading the Wallet"

Saving and Reloading the Wallet

After you have loaded a wallet, you might want to save its current "state" so that on the next load, it can immediately know its balance and be ready to spend without having to "re-discover" the wallet addresses/state.

Here is an example that, after sending a transaction, saves the current wallet state to a local file. Since I am using Node.js in this example, we are going to use the NPM module node-localstorage in order to store the wallet state easily and effortlessly. If you are using a Browser however, you can use the Native "LocalStorage" using the same code.

You can run this example again right after it finishes as well. Since it saves its "Spent Transaction" state, it doesn't need to wait for a confirmation on the Blockchain to send the next transaction.

const HDMW = require('oip-hdmw')
const Wallet = HDMW.Wallet;

if (typeof localStorage === "undefined" || localStorage === null) {
  const LocalStorage = require('node-localstorage').LocalStorage;
  localStorage = new LocalStorage('./storage');
}

let wallet_data = localStorage.getItem('oip-hdmw')

if (wallet_data)
	wallet_data = JSON.parse(wallet_data)

let myWallet = new Wallet('carbon panda replace drum guess heart inside useless random bulb hint industry', {
	serialized_data: wallet_data
});

let send_a_payment = async () => {
	let txid = await myWallet.sendPayment({
		to: { "12nP3k9tFKgQPJNkDDyNWqgjtm2bt3qq1b": 0.001 }
	})

	console.log("Successfully sent Transaction! " + txid);

	// Save the Wallet State
	localStorage.setItem('oip-hdmw', JSON.stringify(myWallet.serialize(), null, 4))
}

// Run the payment send
send_a_payment().catch(() => { 
	console.error("Unable to send Transaction!", error) 
})

Understanding the Wallet Topology

npm.io

API Documentation

Learn more about how each Class works, or take a look at all functions available to you.

License

MIT License

Copyright (c) 2018 Open Index Protocol Working Group

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

1.3.27

5 years ago

1.3.26

5 years ago

1.3.25

5 years ago

1.3.24

5 years ago

1.3.23

5 years ago

1.3.22

5 years ago

1.3.21

5 years ago

1.3.20

5 years ago

1.3.19

5 years ago

1.3.18

5 years ago

1.3.17

5 years ago

1.3.16

5 years ago

1.3.15

5 years ago

1.3.14

5 years ago

1.3.13

5 years ago

1.3.12

5 years ago

1.3.11

5 years ago

1.3.10

5 years ago

1.3.9

5 years ago

1.3.8

5 years ago

1.3.7

5 years ago

1.3.6

5 years ago

1.3.5

6 years ago

1.3.4

6 years ago

1.3.3

6 years ago

1.3.2

6 years ago

1.3.1

6 years ago

1.3.0

6 years ago

1.2.0

6 years ago

1.1.0

6 years ago

1.0.13

6 years ago

1.0.12

6 years ago

1.0.11

6 years ago

1.0.10

6 years ago

1.0.9

6 years ago

1.0.8

6 years ago

1.0.7

6 years ago

1.0.6

6 years ago

1.0.5

6 years ago

1.0.4

6 years ago

1.0.3

6 years ago

1.0.2

6 years ago

1.0.1

6 years ago

1.0.0

6 years ago