0.0.23 • Published 3 months ago

hydrajs-wallet v0.0.23

Weekly downloads
164
License
ISC
Repository
github
Last release
3 months ago

hydrajs-wallet

A toolkit for building HYDRA light wallets

This is a client-side wallet library that can generate private keys from a mnemonic, or import private keys from other HYDRA wallets.

It can sign transactions locally, and submit the raw transaction data to a remote HYDRA node. The blockchain data is provided by the Insight API (which powers https://explorer.hydrachain.org/), rather than the raw hydrad RPC calls.

This library makes it possible to run DApp without the users having to run a full HYDRA node.

Install

yarn add hydrajs-wallet

API

Examples

Create Mnemonic+Password Wallet

import { networks, generateMnemonic } from "hydrajs-wallet";

async function main() {
  const network = networks.testnet;
  const mnemonic = generateMnemonic();
  const password = "password";

  const wallet = network.fromMnemonic(mnemonic, password);

  console.log("mnemonic:", mnemonic);
  console.log("public address:", wallet.address);
  console.log("private key (WIF):", wallet.toWIF());
}

main().catch(err => console.log(err));

Example Output:

mnemonic: hold struggle ready lonely august napkin enforce retire pipe where avoid drip
public address: TLUHmrFGexxpyHwQphLpE1czZNFE5m1xmV
private key (WIF): cNQKccYYQyGX9G9Qxq2DJev9jHygbZpb2UG7EvUapbtDx5XhkhYE

Send Fund

This example restores a wallet from a private key (in WIF format), then sending value to another address.

The transaction is signed locally, and the transaction submitted to a remote API.

The currency unit used is satoshi. To convert HYDRA to satoshi you should multiply the amount you want with 1e8.

import { networks } from "hydrajs-wallet";

async function main() {
  // Use the test network. Or `networks.mainnet`
  const network = networks.testnet;

  const wif = "cU4ficvRNvR7jnbtczCWo5s9rB9Tdg1U4LkArVpGU6cKnDq7LFoP";
  const wallet = network.fromWIF(wif);

  console.log(wallet.address);

  const toAddr = "TS3ThpDn4HRH9we2hZUdF3F3uR7TTvpZ9v";
  // Sending 0.1 HYDRA
  const sendtx = await wallet.send(toAddr, 0.01 * 1e8);
  console.log("sendtx", sendtx);
}

main().catch(err => console.log(err));

Send To Contract

Let's burn some money using the Burn contract:

pragma solidity ^0.4.18;

contract Burn {
  uint256 public totalburned;
  event DidBurn(address burnerAddress, uint256 burnedAmount);

  function burnbabyburn() public payable {
    totalburned = msg.value;
    DidBurn(msg.sender, msg.value);
  }
}

The ABI encoding for the burnbabyburn() invokation is e179b912. We'll burn 0.05 HYDRA, expressed in unit of satoshi.

import { networks } from "hydrajs-wallet";

async function main() {
  const network = networks.testnet;

  const privateKey = "cU4ficvRNvR7jnbtczCWo5s9rB9Tdg1U4LkArVpGU6cKnDq7LFoP";

  const wallet = network.fromWIF(privateKey);

  const contractAddress = "b10071ee33512ce8a0c06ecbc14a5f585a27a3e2";
  const encodedData = "e179b912"; // burnbabyburn()

  const tx = await wallet.contractSend(contractAddress, encodedData, {
    amount: 0.05 * 1e8 // 0.05 HYDRA in satoshi
  });

  console.log(tx);
}

main().catch(err => console.log(err));

Networks

Two networks are predefined:

import { networks } from "hydrajs-wallet";

// Main Network
networks.mainnet;

// Test Network
networks.testnet;

fromPrivateKey

Alias for fromWIF.

fromWIF

fromWIF constructs a wallet from private key (in WIF format).

Suppose you want to import the public address Tg3HYD8c4bAVLeEzA9t3Ken3Y3Mni1HZSS. Use hydra-cli to dump the private key from wallet:

hydra-cli dumpprivkey Tg3HYD8c4bAVLeEzA9t3Ken3Y3Mni1HZSS

cVHzWuEKUxoRKba9ySZFqUKZ9G5W8NkzthRcPaB65amUJs95RM3d
const network = networks.testnet;

const privateKey = "cVEwiJ5NMTdnkW4ZW2ykUopawtLPXQWtPDmvpTh5jmXYMtg8itAz";

const wallet = network.fromWIF(privateKey);
console.log("public address:", wallet.address);

Output:

public address: TWAnfBnRNhZBqtgSdgHjSfS2D5Jawmafra

fromMnemonic

fromMnemonic constructs a wallet from mnemonic. User can optionally specify a password to add to the mnemonic entropy.

const network = networks.testnet;
const mnemonic =
  "hold struggle ready lonely august napkin enforce retire pipe where avoid drip";
const password = "password";

const wallet = network.fromMnemonic(mnemonic, password);

console.log("public address:", wallet.address);
console.log("private key (WIF):", wallet.toWIF());

Example Output:

public address: TLUHmrFGexxpyHwQphLpE1czZNFE5m1xmV
private key (WIF): cNQKccYYQyGX9G9Qxq2DJev9jHygbZpb2UG7EvUapbtDx5XhkhYE
0.0.22

3 months ago

0.0.23

3 months ago

0.0.20

1 year ago

0.0.21

1 year ago

0.0.12

2 years ago

0.0.13

2 years ago

0.0.14

2 years ago

0.0.15

2 years ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.11

2 years ago

0.0.10

3 years ago

0.0.9

3 years ago

0.0.8

3 years ago

0.0.7

3 years ago

0.0.5

3 years ago

0.0.4

3 years ago

0.0.6

3 years ago

0.0.3

3 years ago

0.0.2

3 years ago

0.0.1

3 years ago