0.1.1 • Published 6 years ago

bitsong-js-tx v0.1.1

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

License: MIT

A simple module for creating, manipulating and signing BitSong transactions.

It is complemented by the following packages:

Install

npm install bitsong-js-tx

Usage

Full example

example

import BitsongTx from 'bitsong-js-tx';
import BitsongSendTxData from 'bitsong-js-tx/src/data/send';
import {TX_TYPE_SEND} from 'bitsong-js-tx/src/tx-types';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongSendTxData({
    to: '0x0000000000000000000000000000000000000000',
    coin: formatCoin('BTSG'),
    value: `0x01`,
});
const txParams = {
    nonce: '0x00',
    gasPrice: '0x01',
    gasCoin: formatCoin('BTSG'), 
    type: TX_TYPE_SEND,
    data: txData.serialize(),
};

const tx = new BitsongTx(txParams);

const privateKey = Buffer.from('5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da', 'hex');
tx.sign(privateKey);

const serializedTx = tx.serialize();

Initialization

import BitsongTx from 'bitsong-js-tx';

const tx = new BitsongTx(txParams);

Tx params

All tx params can be passed as Buffer or Hex string

  • nonce - int, used for prevent transaction reply (count of txs for this private key + 1)
  • gasPrice - big int, used for managing transaction fees
  • gasCoin - symbol of a coin to pay fee
  • type - type of transaction (see below).
  • data - data of transaction (depends on transaction type, see below).
  • payload (arbitrary bytes) - arbitrary user-defined bytes, e.g. tx message
  • serviceData - reserved field.
  • ECDSA fields (r, s and v) - digital signature of transaction

Methods

.sign(privateKey)

Sign a transaction with a given private key. privateKey - 32 bytes Buffer.

tx.sign(privateKey);

.verifySignature()

Determines if the signature is valid. Returns boolean.

const isValid = tx.verifySignature();

.validate(stringError)

Validates the signature. stringError - whether to return a string with a description of why the validation failed. Return boolean or string with errors.

const isValid = tx.validate();
const validationErrors = tx.validate(true);

.hash(includeSignature)

Computes a sha3-256 hash of the serialized tx. includeSignature - whether or not to include the signature, default true. Returns Buffer.

// hash of tx with signature
const hash = tx.hash();
// hash of tx without signature
const hashWithoutSignature = tx.hash(false);

.getSenderAddress()

Returns the sender's address. Returns Buffer.

const address = tx.getSenderAddress();

.getSenderPublicKey()

Returns the sender's public key. Returns Buffer.

const publicKey = tx.getSenderPublicKey();

Tx types

TX_TYPE_SEND: '0x01' TX_TYPE_SELL_COIN: '0x02' TX_TYPE_SELL_ALL_COIN: '0x03' TX_TYPE_BUY_COIN: '0x04' TX_TYPE_CREATE_COIN: '0x05' TX_TYPE_DECLARE_CANDIDACY: '0x06' TX_TYPE_DELEGATE: '0x07' TX_TYPE_UNBOND: '0x08' TX_TYPE_REDEEM_CHECK: '0x09' TX_TYPE_SET_CANDIDATE_ON: '0x0A' TX_TYPE_SET_CANDIDATE_OFF: '0x0B'

Tx data

Send

import {toBuffer} from 'bitsong-js-util';
import BitsongSendTxData from 'bitsong-js-tx/src/tx-data/send';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongSendTxData({
   coin: formatCoin('BTSG'),
   to: toBuffer('Mx7633980c000139dd3bd24a3f54e06474fa941e16'),
   value: 10,
});

Sell

import BitsongSellTxData from 'bitsong-js-tx/src/tx-data/sell';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongSellTxData({
   coinToSell: formatCoin('BTSG'),
   valueToSell: 10,
   coinToBuy: formatCoin('ARTISTCOIN'),
});

Sell All

import BitsongSellAllTxData from 'bitsong-js-tx/src/tx-data/sell-all';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongSellAllTxData({
   coinToSell: formatCoin('BTSG'),
   coinToBuy: formatCoin('ARTISTCOIN'),
});

Buy

import BitsongBuyTxData from 'bitsong-js-tx/src/tx-data/buy';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongBuyTxData({
     coinToBuy: formatCoin('BTSG'),
     valueToBuy: 10,
     coinToSell: formatCoin('ARTISTCOIN'),
 });

Create Coin

import BitsongCreateCoinTxData from 'bitsong-js-tx/src/tx-data/create-coin';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongCreateCoinTxData({
   name: 'My coin',
   symbol: formatCoin('MYCOIN'),
   initialAmount: 10,
   initialReserve: 50,
   constantReserveRatio: 100,
});

Declare Candidacy

import {toBuffer} from 'bitsong-js-util';
import BitsongDeclareCandidacyTxData from 'bitsong-js-tx/src/tx-data/declare-candidacy';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongDeclareCandidacyTxData({
   address: toBuffer('Mx7633980c000139dd3bd24a3f54e06474fa941e16'),
   pubKey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
   commission: 10,
   coin: formatCoin('BTSG'),
   stake: 1000,
});

Delegate

import {toBuffer} from 'bitsong-js-util';
import BitsongDelegateTxData from 'bitsong-js-tx/src/tx-data/delegate';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongDelegateTxData({
   pubKey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
   coin: formatCoin('BTSG'),
   stake: 1000,
});

Unbond

import {toBuffer} from 'bitsong-js-util';
import BitsongUnbondTxData from 'bitsong-js-tx/src/tx-data/unbond';
import {formatCoin} from 'bitsong-js-tx/src/helpers';

const txData = new BitsongUnbondTxData({
   pubKey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
   coin: formatCoin('BTSG'),
   stake: 1000,
});

Redeem Check

import {toBuffer} from 'bitsong-js-util';
import {Buffer} from 'safe-buffer';
import BitsongRedeemCheckTxData from 'bitsong-js-tx/src/tx-data/redeem-check';

const txData = new BitsongRedeemCheckTxData({
   rawCheck: toBuffer('Mcf89f01830f423f8a4d4e5400000000000000888ac7230489e80000b841ada7ad273bef8a1d22f3e314fdfad1e19b90b1fe8dc7eeb30bd1d391e89af8642af029c138c2e379b95d6bc71b26c531ea155d9435e156a3d113a14c912dfebf001ca0781a7b7d781634bcf632579b99d583887ab093dfbd50b65de5c0e5813028a277a071272d8e1be721f5307f40f87daa4ab632781640f18fd424839396442cc7ff17'),
   proof: Buffer.from('7f8b6d3ed18d2fe131bbdc9f9bce3b96724ac354ce2cfb49b4ffc4bd71aabf580a8dfed407a34122e45d290941d855d744a62110fa1c11448078b13d3117bdfc01', 'hex'),
});

Set Candidate On

import {toBuffer} from 'bitsong-js-util';
import BitsongSetCandidateOnTxData from 'bitsong-js-tx/src/tx-data/set-candidate-on';

const txData = new BitsongSetCandidateOnTxData({
   pubKey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
});

Set Candidate Off

import {toBuffer} from 'bitsong-js-util';
import BitsongSetCandidateOffTxData from 'bitsong-js-tx/src/tx-data/set-candidate-off';

const txData = new BitsongSetCandidateOffTxData({
   pubKey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
});

Resources

Community

License

MIT License

0.1.1

6 years ago

0.1.0

6 years ago