Skip to content

Commit

Permalink
rename tx-data files: remove postfix, add bipToPip and pipToBip alias…
Browse files Browse the repository at this point in the history
…es, update readme
  • Loading branch information
shrpne committed Aug 14, 2018
1 parent 1b2f073 commit 436b5d6
Show file tree
Hide file tree
Showing 21 changed files with 285 additions and 31 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,3 +1,7 @@
## 1.4.0 - 2018-08-14
- **BREAKING** rename tx-data files: `sell-coin` -> `sell`, `sell-all-coin` -> `sell-all`, `buy-coin` -> `buy`
- add `converter.bipToPip` and `converter.pipToBip` alias methods

## 1.3.0 - 2018-08-11
- **BREAKING** rename `unbound` to `unbond`, tx data and tx type are affected

Expand Down
243 changes: 239 additions & 4 deletions README.md
Expand Up @@ -18,31 +18,266 @@ It is complemented by the following packages:

## Usage

- [example](https://github.com/MinterTeam/minterjs-tx/blob/master/examples/transaction.js)
### Full example

[example](https://github.com/MinterTeam/minterjs-tx/blob/master/examples/transaction.js)

```javascript
import MinterTx from 'minterjs-tx';
import MinterSendTxData from 'minterjs-tx/src/data/send';
import {TX_TYPE_SEND} from 'minterjs-tx/src/tx-types';
const privateKey = Buffer.from('5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da', 'hex');
import {formatCoin} from 'minterjs-tx/src/helpers';

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

const tx = new MinterTx(txParams);

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

const serializedTx = tx.serialize();
```


### Initialization
```javascript
import MinterTx from 'minterjs-tx';

const tx = new MinterTx(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.

```javascript
tx.sign(privateKey);
```

#### `.verifySignature()`
Determines if the signature is valid.
Returns boolean.

```javascript
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.

```javascript
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.

```javascript
// 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.
```javascript
const address = tx.getSenderAddress();
```
#### `.getSenderPublicKey()`
Returns the sender's public key.
Returns Buffer.

```javascript
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
```javascript
import {toBuffer} from 'minterjs-util';
import MinterSendTxData from 'minterjs-tx/src/tx-data/send';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterSendTxData({
coin: formatCoin('MNT'),
to: toBuffer('Mx7633980c000139dd3bd24a3f54e06474fa941e16'),
value: 10,
});
```

#### Sell
```javascript
import MinterSellTxData from 'minterjs-tx/src/tx-data/sell';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterSellTxData({
coin_to_sell: formatCoin('MNT'),
value_to_sell: 10,
coin_to_buy: formatCoin('BELTCOIN'),
});
```

#### Sell All
```javascript
import MinterSellAllTxData from 'minterjs-tx/src/tx-data/sell-all';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterSellAllTxData({
coin_to_sell: formatCoin('MNT'),
coin_to_buy: formatCoin('BELTCOIN'),
});
```

#### Buy
```javascript
import MinterBuyTxData from 'minterjs-tx/src/tx-data/buy';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterBuyTxData({
coin_to_buy: formatCoin('MNT'),
value_to_buy: 10,
coin_to_sell: formatCoin('BELTCOIN'),
});
```

#### Create Coin
```javascript
import MinterCreateCoinTxData from 'minterjs-tx/src/tx-data/create-coin';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterCreateCoinTxData({
name: 'My coin',
symbol: formatCoin('MYCOIN'),
initialAmount: 10,
initialReserve: 50,
crr: 100,
});
```

#### Declare Candidacy
```javascript
import {toBuffer} from 'minterjs-util';
import MinterDeclareCandidacyTxData from 'minterjs-tx/src/tx-data/declare-candidacy';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterDeclareCandidacyTxData({
address: toBuffer('Mx7633980c000139dd3bd24a3f54e06474fa941e16'),
pubkey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
commission: 10,
coin: formatCoin('MNT'),
stake: 1000,
});
```

#### Delegate
```javascript
import {toBuffer} from 'minterjs-util';
import MinterDelegateTxData from 'minterjs-tx/src/tx-data/delegate';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterDelegateTxData({
pubkey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
coin: formatCoin('MNT'),
stake: 1000,
});
```

#### Unbond
```javascript
import {toBuffer} from 'minterjs-util';
import MinterUnbondTxData from 'minterjs-tx/src/tx-data/unbond';
import {formatCoin} from 'minterjs-tx/src/helpers';
const txData = new MinterUnbondTxData({
pubkey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
coin: formatCoin('MNT'),
stake: 1000,
});
```

#### Redeem Check
```javascript
import {toBuffer} from 'minterjs-util';
import {Buffer} from 'safe-buffer';
import MinterRedeemCheckTxData from 'minterjs-tx/src/tx-data/redeem-check';
const txData = new MinterRedeemCheckTxData({
check: toBuffer('Mcf89f01830f423f8a4d4e5400000000000000888ac7230489e80000b841ada7ad273bef8a1d22f3e314fdfad1e19b90b1fe8dc7eeb30bd1d391e89af8642af029c138c2e379b95d6bc71b26c531ea155d9435e156a3d113a14c912dfebf001ca0781a7b7d781634bcf632579b99d583887ab093dfbd50b65de5c0e5813028a277a071272d8e1be721f5307f40f87daa4ab632781640f18fd424839396442cc7ff17'),
proof: Buffer.from('7f8b6d3ed18d2fe131bbdc9f9bce3b96724ac354ce2cfb49b4ffc4bd71aabf580a8dfed407a34122e45d290941d855d744a62110fa1c11448078b13d3117bdfc01', 'hex'),
});
```

#### Set Candidate On
```javascript
import {toBuffer} from 'minterjs-util';
import MinterSetCandidateOnTxData from 'minterjs-tx/src/tx-data/set-candidate-on';
const txData = new MinterSetCandidateOnTxData({
pubkey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
});
```

#### Set Candidate Off
```javascript
import {toBuffer} from 'minterjs-util';
import MinterSetCandidateOffTxData from 'minterjs-tx/src/tx-data/set-candidate-off';
const txData = new MinterSetCandidateOffTxData({
pubkey: toBuffer('Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3'),
});
```


## License

MIT License
1 change: 1 addition & 0 deletions examples/transaction.js
Expand Up @@ -31,6 +31,7 @@ getNonce().then((nonce) => {
const txParams = {
nonce: `0x${nonce.toString(16)}`,
gasPrice: '0x01',
gasCoin: formatCoin(FORM_DATA.coin),
type: TX_TYPE_SEND,
data: txData.serialize(),
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "minterjs-tx",
"version": "1.3.0",
"version": "1.4.0",
"description": "A simple module for creating, manipulating and signing Minter transactions",
"main": "src/index.js",
"files": [
Expand Down
6 changes: 6 additions & 0 deletions src/converter.js
Expand Up @@ -24,4 +24,10 @@ export default {

throw String('Unknown type');
},
bipToPip(num) {
return this.convert(num, 'pip');
},
pipToBip(num) {
return this.convert(num, 'bip');
},
};
2 changes: 1 addition & 1 deletion src/index.js
Expand Up @@ -84,7 +84,7 @@ class Transaction {

/**
* Computes a sha3-256 hash of the serialized tx
* @param {Boolean} [includeSignature=true] whether or not to inculde the signature
* @param {Boolean} [includeSignature=true] whether or not to include the signature
* @return {Buffer}
*/
hash(includeSignature) {
Expand Down
4 changes: 2 additions & 2 deletions src/tx-data/buy-coin.js → src/tx-data/buy.js
@@ -1,7 +1,7 @@
import ethUtil from 'ethereumjs-util';
import {Buffer} from 'safe-buffer';

class SellCoinTransactionData {
class BuyTransactionData {
constructor(data) {
data = data || {};
// Define Properties
Expand Down Expand Up @@ -38,4 +38,4 @@ class SellCoinTransactionData {
}
}

export default SellCoinTransactionData;
export default BuyTransactionData;
4 changes: 2 additions & 2 deletions src/tx-data/sell-all-coin.js → src/tx-data/sell-all.js
@@ -1,7 +1,7 @@
import ethUtil from 'ethereumjs-util';
import {Buffer} from 'safe-buffer';

class SellAllCoinTransactionData {
class SellAllTransactionData {
constructor(data) {
data = data || {};
// Define Properties
Expand Down Expand Up @@ -31,4 +31,4 @@ class SellAllCoinTransactionData {
}
}

export default SellAllCoinTransactionData;
export default SellAllTransactionData;
4 changes: 2 additions & 2 deletions src/tx-data/sell-coin.js → src/tx-data/sell.js
@@ -1,7 +1,7 @@
import ethUtil from 'ethereumjs-util';
import {Buffer} from 'safe-buffer';

class SellCoinTransactionData {
class SellTransactionData {
constructor(data) {
data = data || {};
// Define Properties
Expand Down Expand Up @@ -38,4 +38,4 @@ class SellCoinTransactionData {
}
}

export default SellCoinTransactionData;
export default SellTransactionData;
10 changes: 10 additions & 0 deletions test/converter.test.js
Expand Up @@ -6,11 +6,21 @@ describe('converter', () => {
expect(converter.convert(bips, 'pip').toString()).toEqual('1234000000000000000');
});

test('bip to pip', () => {
const bips = 1.234;
expect(converter.bipToPip(bips).toString()).toEqual('1234000000000000000');
});

test('convert to bip', () => {
const pips = 1234;
expect(converter.convert(pips, 'bip').toString()).toEqual((0.000000000000001234).toString());
});

test('pip to bip', () => {
const pips = 1234;
expect(converter.pipToBip(pips).toString()).toEqual((0.000000000000001234).toString());
});

test('convert 0x', () => {
const bips = '0x';

Expand Down
6 changes: 3 additions & 3 deletions test/tx-data/buy-coin.test.js → test/tx-data/buy.test.js
@@ -1,10 +1,10 @@
import MinterBuyCoinTxData from '../../src/tx-data/buy-coin';
import MinterBuyTxData from '../../src/tx-data/buy';
import {formatCoin} from '../../src/helpers';
import decodeToArray from '../decode-to-array';

describe('MinterBuyCoinTxData', () => {
describe('MinterBuyTxData', () => {
test('rlp encoded fields', () => {
const serializedTxData = (new MinterBuyCoinTxData({
const serializedTxData = (new MinterBuyTxData({
coin_to_buy: formatCoin('MNT'),
value_to_buy: 10,
coin_to_sell: formatCoin('BELTCOIN'),
Expand Down

0 comments on commit 436b5d6

Please sign in to comment.