Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

broadcast done #15

Merged
merged 1 commit into from
Nov 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module.exports = {
roots: [
'<rootDir>/test',
],
testRegex: '(/__tests__/.*|(\\.|/)(test))\\.tsx?$',
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$',
transform: {
"^.+\\.(ts|tsx)?$": "ts-jest"
},
Expand Down
43 changes: 38 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@
"url": "https://github.com/ebceu4/waves-transactions.git"
},
"dependencies": {
"@types/axios": "^0.14.0",
"ajv": "^6.5.5",
"axios": "^0.18.0",
"npm": "^6.4.1",
"waves-crypto": "^1.0.39"
}
Expand Down
84 changes: 84 additions & 0 deletions src/general.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { Order, TransactionType, Tx } from './transactions'
import { SeedTypes } from "./types";
import { issue, issueToBytes } from "./transactions/issue";
import { transfer, transferToBytes } from "./transactions/transfer";
import { reissue, reissueToBytes } from "./transactions/reissue";
import { burn, burnToBytes } from "./transactions/burn";
import { lease, leaseToBytes } from "./transactions/lease";
import { cancelLease, cancelLeaseToBytes } from "./transactions/cancel-lease";
import { data, dataToBytes } from "./transactions/data";
import { massTransfer, massTransferToBytes } from "./transactions/mass-transfer";
import { alias, aliasToBytes } from "./transactions/alias";
import { setScript, setScriptToBytes } from "./transactions/set-script";
import { isOrder, orderToBytes } from "./transactions/order";
import axios from "axios";

export function signTx(tx: Tx, seed: SeedTypes): Tx {
if (seed == null) throw new Error("Seed is not provided");
switch (tx.type) {
case TransactionType.Issue:
return issue(tx, seed);
case TransactionType.Transfer:
return transfer(tx, seed);
case TransactionType.Reissue:
return reissue(tx, seed);
case TransactionType.Burn:
return burn(tx, seed);
case TransactionType.Lease:
return lease(tx, seed);
case TransactionType.CancelLease:
return cancelLease(tx, seed);
case TransactionType.Alias:
return alias(tx, seed);
case TransactionType.MassTransfer:
return massTransfer(tx, seed);
case TransactionType.Data:
return data(tx, seed);
case TransactionType.SetScript:
return setScript(tx, seed);
default:
throw new Error(`Unknown tx type: ${tx!.type}`)
}
}

export function serialize(obj: Tx | Order): Uint8Array {
if (isOrder(obj)) return orderToBytes(obj)
switch (obj.type) {
case TransactionType.Issue:
return issueToBytes(obj);
case TransactionType.Transfer:
return transferToBytes(obj);
case TransactionType.Reissue:
return reissueToBytes(obj);
case TransactionType.Burn:
return burnToBytes(obj);
case TransactionType.Lease:
return leaseToBytes(obj);
case TransactionType.CancelLease:
return cancelLeaseToBytes(obj);
case TransactionType.Alias:
return aliasToBytes(obj);
case TransactionType.MassTransfer:
return massTransferToBytes(obj);
case TransactionType.Data:
return dataToBytes(obj);
case TransactionType.SetScript:
return setScriptToBytes(obj);
default:
throw new Error(`Unknown object type: ${obj}`)
}
}

export async function broadcast(tx: Tx, apiBase: string) {
const instance = axios.create({
baseURL: apiBase
});
try{
const resp = await instance.post('transactions/broadcast', tx);
return resp.data
}catch (e) {
if (e.response && e.response.status === 400){
throw new Error(e.response.data.message)
}else throw e
}
};
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ export { transfer } from './transactions/transfer'
export { alias } from './transactions/alias'
export { setScript } from './transactions/set-script'
export { order } from './transactions/order'
export { signTx } from './signTx'
export { signTx } from './general'
40 changes: 0 additions & 40 deletions src/signTx.ts

This file was deleted.

7 changes: 4 additions & 3 deletions src/transactions/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ export interface OrderParams extends Params {
expiration?: number
}

export const isOrder = (p: any): p is Order => (<Order>p).assetPair !== undefined

export const orderValidation = (ord: Order): ValidationResult => []

export const orderToBytes = (ord: Order) => concat(
Expand Down Expand Up @@ -55,7 +57,7 @@ export const orderToBytes = (ord: Order) => concat(
* }
*
*
* const signedOrder = burn(seed, params)
* const signedOrder = order(params, seed)
* ```
* ### Output
* ```json
Expand All @@ -78,13 +80,12 @@ export const orderToBytes = (ord: Order) => concat(
* }
* ```
*
* @param seed
* @param paramsOrOrder
* @param [seed]
* @returns
*
*/
export function order(paramsOrOrder: OrderParams | Order, seed?: SeedTypes): Order {
const isOrder = (p: OrderParams | Order): p is Order => (<Order>p).assetPair !== undefined

const amountAsset = isOrder(paramsOrOrder) ? paramsOrOrder.assetPair.amountAsset : paramsOrOrder.amountAsset
const priceAsset = isOrder(paramsOrOrder) ? paramsOrOrder.assetPair.priceAsset : paramsOrOrder.priceAsset
Expand Down
25 changes: 25 additions & 0 deletions test/general.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { publicKey, verifySignature } from "waves-crypto";
import { reissue, signTx } from '../src';
import { reissueToBytes } from "../src/transactions/reissue";
import { broadcast } from "../src/general";
import { data } from "./sandbox.spec";

export const reissueMinimalParams = {
assetId: 'test',
Expand Down Expand Up @@ -29,4 +31,27 @@ describe('signTx', () => {
const signedTwoTimes = () => signTx(tx, [stringSeed])
expect(signedTwoTimes).toThrow('Proof at index 0 is already exists.')
})

it('Should send tx to node', async () => {
const dataParams = {data: [
{
key: 'oneTwo',
value: false
},
{
key: 'twoThree',
value: 2
},
{
key: 'three',
value: Uint8Array.from([1,2,3,4,5,6])
}
],
timestamp: 100000
}
const result = data('seed', dataParams)

await expect(broadcast(result, 'https://nodes.wavesplatform.com/')).rejects
.toEqual(new Error("Transaction not allowed by account-script"))
})
})