# blockchain-gateway

> Bitcoin and Ethereum transaction management

Latest version **0.2.0** (published 2020-09-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install blockchain-gateway
pnpm add blockchain-gateway
yarn add blockchain-gateway
bun add blockchain-gateway
```

Provides the command `blockchain-gateway`.

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.0 |
| Published | 2020-09-23 |
| First published | 2020-05-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 5 |
| Unpacked size | 40.6 KB |
| Known vulnerabilities | 0 (+25 in 1 direct dependencies) |
| Install scripts | no |
| Author | scifier |
| Maintainers | scifier |
| Keywords | blockchain, bitcoin, ethereum |

## Links

- npm: https://www.npmjs.com/package/blockchain-gateway
- Repository: https://github.com/scifier/blockchain-gateway
- Homepage: https://scifier.dev/blockchain-gateway
- Issues: https://github.com/scifier/blockchain-gateway/issues
- npm.io page: https://npm.io/package/blockchain-gateway

## Dependencies (5)

- [web3](https://npm.io/package/web3.md) ^1.3.0
- [axios](https://npm.io/package/axios.md) ^0.20.0
- [bottleneck](https://npm.io/package/bottleneck.md) ^2.19.5
- [bignumber.js](https://npm.io/package/bignumber.js.md) ^9.0.0
- [bitcoinjs-lib](https://npm.io/package/bitcoinjs-lib.md) ^5.2.0

## Recent versions

- 0.2.0 (latest) — 2020-09-23
- 0.1.0 — 2020-05-22

## README

# Blockchain Gateway

[![npm version](https://badge.fury.io/js/blockchain-gateway.svg)](https://badge.fury.io/js/blockchain-gateway)

- [Usage](#usage)
  - [Library](#library)
  - [CLI](#cli)
- [Testing and Development](#testing-and-development)
- [Versioning](#versioning)
- [Authors](#authors)
- [License](#license)

## Usage

### Library

```bash
npm install blockchain-gateway
```

Sending Bitcoin transactions is pretty easy. You don't need to deal with UTXOs and computing fees:

```javascript
const { BitcoinNetwork } = require('blockchain-gateway');

const { BLOCKCYPHER_TOKEN, PRIVATE_KEY } = process.env;

// Connect to Bitcoin testnet3 with your BlockCypher Access Token
const bitcoin = new BitcoinNetwork({
  networkType: 'testnet',
  accessToken: BLOCKCYPHER_TOKEN,
});

(async () => {
  // Connect to your Bitcoin account with your compressed WIF private key
  await bitcoin.connect(PRIVATE_KEY);

  // Generate a random Bitcoin account
  const { address } = ethereum.generateKeypair();

  // Try to send 0.5 tBTC from your account to the generated account
  try {
    // Compute fees and select suitable transaction outputs
    const tx = await bitcoin.createTransaction(
      bitcoin.defaultAccount,
      address,
      0.5,
    );

    // Sign the transaction inputs
    const { rawTx, txHash } = await bitcoin.signTransaction(tx);
    console.log('Your transaction was successfully signed. txHash:', txHash);

    // Broadcast the raw transaction
    await bitcoin.broadcastTransaction(rawTx);

    // Check if the transaction has at least 1 confirmation
    await bitcoin.getTxStatus(txHash);
    console.log('Your transaction was successfully mined!', bitcoin.getTxLink(txHash));
  } catch (e) {
    console.log('Something went wrong!', e.message);
  }
})();
```

Ethereum transactions are sent in the same way:

```javascript
import { EthereumNetwork } from 'blockchain-gateway';
// or import EthereumNetwork from 'blockchain-gateway/EthereumNetwork';

const { INFURA_PROJECT_ID, PRIVATE_KEY } = process.env;

// Connect to Ethereum mainnet with your Infura Project Id
const ethereum = new EthereumNetwork({
  networkType: 'mainnet',
  accessToken: INFURA_PROJECT_ID,
});

(async () => {
  // Connect to your Ethereum account with your private key
  await ethereum.connect(PRIVATE_KEY);

  // Generate a random Ethereum account
  const { address } = ethereum.generateKeypair();

  // Try to send 0.5 ETH from your account to the generated account
  try {
    // Compute fees and fetch the nonce
    const tx = await ethereum.createTransaction(
      ethereum.defaultAccount,
      address,
      0.5,
    );

    // Sign the transaction
    const { rawTx, txHash } = await ethereum.signTransaction(tx);
    console.log('Your transaction was successfully signed. txHash:', txHash);

    // Broadcast the raw transaction
    await ethereum.broadcastTransaction(rawTx);

    // Check if the transaction was mined
    await ethereum.getTxStatus(txHash);
    console.log('Your transaction was successfully mined!', ethereum.getTxLink(txHash));
  } catch (e) {
    console.log('Something went wrong!', e.message);
  }
})();
```

Also, signing Ethereum transactions using MetaMask is also supported. You don't have to pass the `accessToken` to the `EthereumNetwork` constructor if you are using a browser with installed MetaMask.

### CLI

```bash
# Install blockchain-gateway globally
npm install -g blockchain-gateway

# Run in REPL CLI mode (https://nodejs.org/api/repl.html#repl_commands_and_special_keys)
blockchain-gateway

# Example of CLI usage
> const ethereum = new EthereumNetwork({ accessToken: 'YOUR_INFURA_PROJECT_ID' })
> ethereum.generateKeypair()
{ address: '0xa71F7F4611BA0c6E836671210a97DCD2DCcF1738',
  privateKey: '0x1c98fa0487eba75ea5819850759fbba0c3ff79a811b9bdcb541a36b0fc2dd286' }
> await ethereum.getBalance('0xa71F7F4611BA0c6E836671210a97DCD2DCcF1738')
'0'
> await ethereum.connect('0x1c98fa0487eba75ea5819850759fbba0c3ff79a811b9bdcb541a36b0fc2dd286')
[ '0xa71F7F4611BA0c6E836671210a97DCD2DCcF1738' ]
> await ethereum.createTransaction('0xa71F7F4611BA0c6E836671210a97DCD2DCcF1738', '0xa71F7F4611BA0c6E836671210a97DCD2DCcF1738', '0.5')
Uncaught TransactionError: Insufficient Funds
  at Proxy.createTransaction (/Users/user/.nvm/versions/node/v12.18.3/lib/node_modules/blockchain-gateway/EthereumNetwork.js:216:13)
  at processTicksAndRejections (internal/process/task_queues.js:97:5)
```

## Testing and Development

```bash
# Install project
git clone https://github.com/scifier/blockchain-gateway.git
cd blockchain-gateway
npm install

# Prepare your testing environment
echo "BLOCKCYPHER_TOKEN=$YOUR_BLOCKCYPHER_TOKEN" > .env
echo "INFURA_TOKEN=$YOUR_INFURA_PROJECT_ID" >> .env
echo "NETWORK_TYPE=mainnet" >> .env
# Variables required by test/bitcoin-transactions
echo "BTC_PRIVATE_KEY=$COMPRESSED_WIF_PRIVATE_KEY" >> .env
echo "BTC_ADDRESS=$BITCOIN_RECIPIENT_ADDRESS" >> .env
echo "BTC_AMOUNT=$BTC_AMOUNT_TO_SEND" >> .env
# Variables required by test/ethereum-transactions
echo "ETH_PRIVATE_KEY=$ETHEREUM_PRIVATE_KEY" >> .env
echo "ETH_ADDRESS=$ETHEREUM_RECIPIENT_ADDRESS" >> .env
echo "ETH_AMOUNT=$ETH_AMOUNT_TO_SEND" >> .env
# KMS variables are required by test/wallet-pkey-management
echo "KMS_KEY_ARN=$KMS_KEY_ARN" >> .env
echo "KMS_ACCESS_KEY=$KMS_ACCESS_KEY" >> .env
echo "KMS_SECRET_KEY=$KMS_SECRET_KEY" >> .env
echo "KMS_ENCRYPTION_KEY=$KMS_ENCRYPTION_KEY" >> .env
echo "KMS_REGION=$KMS_REGION" >> .env

# Run tests
npm run test

# Run specific test
node test/bitcoin-transactions
node test/ethereum-transactions
node test/wallet-pkey-management

# Also you can run blockchain-gateway in CLI mode
npm run start
```

## Versioning

This project use [SemVer](http://semver.org/) for versioning. For the versions available, see the [tags on this repository](https://github.com/scifier/blockchain-gateway/tags).

## Authors

- **Niţa Radu** - *Initial work* - [scifier](https://github.com/scifier)

## License

This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details

---
_Source: https://npm.io/package/blockchain-gateway · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
