diff --git a/jest.config.js b/jest.config.js index 8996913..89e38ad 100644 --- a/jest.config.js +++ b/jest.config.js @@ -2,7 +2,7 @@ module.exports = { roots: [ '/test', ], - testRegex: '(/__tests__/.*|(\\.|/)(test))\\.tsx?$', + testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.tsx?$', transform: { "^.+\\.(ts|tsx)?$": "ts-jest" }, diff --git a/package-lock.json b/package-lock.json index b62a2a8..d9b01ae 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "waves-transactions", - "version": "1.0.1", + "version": "2.0.0", "lockfileVersion": 1, "requires": true, "dependencies": { @@ -32,6 +32,14 @@ } } }, + "@types/axios": { + "version": "0.14.0", + "resolved": "https://registry.npmjs.org/@types/axios/-/axios-0.14.0.tgz", + "integrity": "sha1-7CMA++fX3d1+udOr+HmZlkyvzkY=", + "requires": { + "axios": "*" + } + }, "@types/events": { "version": "1.2.0", "resolved": "http://registry.npmjs.org/@types/events/-/events-1.2.0.tgz", @@ -878,6 +886,15 @@ "integrity": "sha512-ReZxvNHIOv88FlT7rxcXIIC0fPt4KZqZbOlivyWtXLt8ESx84zd3kMC6iK5jVeS2qt+g7ftS7ye4fi06X5rtRQ==", "dev": true }, + "axios": { + "version": "0.18.0", + "resolved": "http://registry.npmjs.org/axios/-/axios-0.18.0.tgz", + "integrity": "sha1-MtU+SFHv3AoRmTts0AB4nXDAUQI=", + "requires": { + "follow-redirects": "^1.3.0", + "is-buffer": "^1.1.5" + } + }, "babel-code-frame": { "version": "6.26.0", "resolved": "https://registry.npmjs.org/babel-code-frame/-/babel-code-frame-6.26.0.tgz", @@ -2637,6 +2654,24 @@ "readable-stream": "^2.0.4" } }, + "follow-redirects": { + "version": "1.5.9", + "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.5.9.tgz", + "integrity": "sha512-Bh65EZI/RU8nx0wbYF9shkFZlqLP+6WT/5FnA3cE/djNSuKNHJEinGGZgu/cQEkeeb2GdFOgenAmn8qaqYke2w==", + "requires": { + "debug": "=3.1.0" + }, + "dependencies": { + "debug": { + "version": "3.1.0", + "resolved": "https://registry.npmjs.org/debug/-/debug-3.1.0.tgz", + "integrity": "sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==", + "requires": { + "ms": "2.0.0" + } + } + } + }, "for-in": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/for-in/-/for-in-1.0.2.tgz", @@ -3744,8 +3779,7 @@ "is-buffer": { "version": "1.1.6", "resolved": "https://registry.npmjs.org/is-buffer/-/is-buffer-1.1.6.tgz", - "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==", - "dev": true + "integrity": "sha512-NcdALwpXkTm5Zvvbk7owOUSvVvBKDgKP5/ewfXEznmQFfs4ZRmanOeKBTjRVjka3QFoN6XJ+9F3USqfHqTaU5w==" }, "is-builtin-module": { "version": "1.0.0", @@ -5135,8 +5169,7 @@ "ms": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/ms/-/ms-2.0.0.tgz", - "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=", - "dev": true + "integrity": "sha1-VgiurfwAvmwpAd9fmGF4jeDVl8g=" }, "nan": { "version": "2.11.1", diff --git a/package.json b/package.json index ca64fe2..4681ac5 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/src/general.ts b/src/general.ts new file mode 100644 index 0000000..dfa60cb --- /dev/null +++ b/src/general.ts @@ -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 + } +}; \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 66b96d2..c68ed69 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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' \ No newline at end of file +export { signTx } from './general' \ No newline at end of file diff --git a/src/signTx.ts b/src/signTx.ts deleted file mode 100644 index 062b806..0000000 --- a/src/signTx.ts +++ /dev/null @@ -1,40 +0,0 @@ -import { TransactionType, Tx } from './transactions' -import { SeedTypes } from "./types"; -import { issue } from "./transactions/issue"; -import { transfer } from "./transactions/transfer"; -import { reissue } from "./transactions/reissue"; -import { burn } from "./transactions/burn"; -import { lease } from "./transactions/lease"; -import { cancelLease } from "./transactions/cancel-lease"; -import { data } from "./transactions/data"; -import { massTransfer } from "./transactions/mass-transfer"; -import { alias } from "./transactions/alias"; -import { setScript } from "./transactions/set-script"; - -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}`) - } -} \ No newline at end of file diff --git a/src/transactions/order.ts b/src/transactions/order.ts index 62d29a5..753f014 100644 --- a/src/transactions/order.ts +++ b/src/transactions/order.ts @@ -19,6 +19,8 @@ export interface OrderParams extends Params { expiration?: number } +export const isOrder = (p: any): p is Order => (p).assetPair !== undefined + export const orderValidation = (ord: Order): ValidationResult => [] export const orderToBytes = (ord: Order) => concat( @@ -55,7 +57,7 @@ export const orderToBytes = (ord: Order) => concat( * } * * - * const signedOrder = burn(seed, params) + * const signedOrder = order(params, seed) * ``` * ### Output * ```json @@ -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 => (p).assetPair !== undefined const amountAsset = isOrder(paramsOrOrder) ? paramsOrOrder.assetPair.amountAsset : paramsOrOrder.amountAsset const priceAsset = isOrder(paramsOrOrder) ? paramsOrOrder.assetPair.priceAsset : paramsOrOrder.priceAsset diff --git a/test/general.test.ts b/test/general.test.ts index 08797d6..dd39c2e 100644 --- a/test/general.test.ts +++ b/test/general.test.ts @@ -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', @@ -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")) + }) }) \ No newline at end of file