Skip to content

Commit

Permalink
Fix build
Browse files Browse the repository at this point in the history
Add test's for native TX
  • Loading branch information
nduchak committed Nov 14, 2018
1 parent 3b8e914 commit 9db55e4
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 19 deletions.
26 changes: 12 additions & 14 deletions es/tx/js.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ const createSalt = salt
* @example JsTx()
*/


/**
* Decode data using the default encoding/decoding algorithm
*
Expand Down Expand Up @@ -160,17 +159,18 @@ async function commitmentHash (name, salt = createSalt()) {
*/
function buildPointers (pointers) {
const POINTERS_TAGS = {
"account_pubkey": ID_TAG_ACCOUNT,
"oracle_pubkey": ID_TAG_ORACLE,
"contract_pubkey": ID_TAG_CONTRACT,
"channel_pubkey": ID_TAG_CHANNEL
'account_pubkey': ID_TAG_ACCOUNT,
'oracle_pubkey': ID_TAG_ORACLE,
'contract_pubkey': ID_TAG_CONTRACT,
'channel_pubkey': ID_TAG_CHANNEL
}
return pointers.map(p => [toBytes(p['key']), _id(POINTERS_TAGS[p['key']], p['id'])])
}

/**
* Create a spend transaction
*
* @param {string} senderId The public key of the sender
* @param {string} recipientId The public key of the recipient
* @param {number} amount The amount to send
* @param {string} payload The payload associated with the data
Expand All @@ -179,13 +179,11 @@ function buildPointers (pointers) {
* @param {number} nonce the nonce of the transaction
* @return {Object} { tx } Encrypted spend tx hash
*/
async function spendTxNative ({ recipientId, amount, payload, fee, ttl, nonce }) {
const address = await this.address()

function spendTxNative ({ senderId, recipientId, amount, payload, fee, ttl, nonce }) {
let tx = [
toBytes(OBJECT_TAG_SPEND_TRANSACTION),
toBytes(VSN),
_id(ID_TAG_ACCOUNT, address, 'ak'),
_id(ID_TAG_ACCOUNT, senderId, 'ak'),
_id(ID_TAG_ACCOUNT, recipientId, 'ak'),
toBytes(amount, true),
toBytes(fee),
Expand Down Expand Up @@ -216,7 +214,7 @@ function namePreclaimTxNative ({ accountId, nonce, commitmentId, fee, ttl }) {
toBytes(nonce),
_id(ID_TAG_COMMITMENT, commitmentId, 'cm'),
toBytes(fee),
toBytes(ttl),
toBytes(ttl)
]

// Encode RLP
Expand Down Expand Up @@ -244,7 +242,7 @@ function nameClaimTxNative ({ accountId, nonce, name, nameSalt, fee, ttl }) {
decode(name, 'nm'),
toBytes(nameSalt),
toBytes(fee),
toBytes(ttl),
toBytes(ttl)
]

// Encode RLP
Expand Down Expand Up @@ -279,7 +277,7 @@ function nameUpdateTxNative ({ accountId, nonce, nameId, nameTtl, pointers, clie
pointers,
toBytes(clientTtl),
toBytes(fee),
toBytes(ttl),
toBytes(ttl)
]

// Encode RLP
Expand Down Expand Up @@ -307,7 +305,7 @@ function nameTransferTxNative ({ accountId, nonce, nameId, recipientId, fee, ttl
_id(ID_TAG_NAME, nameId, 'nm'),
_id(ID_TAG_ACCOUNT, recipientId, 'ak'),
toBytes(fee),
toBytes(ttl),
toBytes(ttl)
]

// Encode RLP
Expand All @@ -333,7 +331,7 @@ function nameRevokeTxNative ({ accountId, nonce, nameId, fee, ttl }) {
toBytes(nonce),
_id(ID_TAG_NAME, nameId, 'nm'),
toBytes(fee),
toBytes(ttl),
toBytes(ttl)
]

// Encode RLP
Expand Down
4 changes: 2 additions & 2 deletions es/tx/tx.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ async function spendTx ({ senderId, recipientId, amount, fee, ttl, nonce, payloa

// Build transaction using sdk (if nativeMode) or build on `EPOCH` side
const { tx } = this.nativeMode
? await this.spendTxNative(R.merge(R.head(arguments), { recipientId, nonce, ttl }))
: await this.api.postSpend(R.merge(R.head(arguments), { recipientId, nonce, ttl }))
? await this.spendTxNative(R.merge(R.head(arguments), { recipientId, senderId, nonce, ttl }))
: await this.api.postSpend(R.merge(R.head(arguments), { recipientId, senderId, nonce, ttl }))

return tx
}
Expand Down
6 changes: 3 additions & 3 deletions es/utils/bytes.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

import bignum from 'bignum'
/**
* Left pad the input data with 0 bytes
* @param length to pad to
Expand Down Expand Up @@ -59,8 +59,8 @@ export function toBytes (val, big = false) {
let v = bignum(val)
let s = Math.ceil(v.bitLength(val) / 8)
return v.toBuffer({
endian : 'big',
size : s,
endian: 'big',
size: s
})
}
if (typeof val === 'string') {
Expand Down
103 changes: 103 additions & 0 deletions test/integration/transaction.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* ISC License (ISC)
* Copyright (c) 2018 aeternity developers
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/

import { describe, it, before } from 'mocha'
import { expect } from 'chai'
import { configure, url, internalUrl } from './'
import { encodeBase58Check, salt } from '../../es/utils/crypto'
import Ae from '../../es/ae/universal'

const nonce = 1
const ttl = 1
const nameTtl = 1
const clientTtl = 1
const fee = 1
const amount = 1
const senderId = 'ak_2iBPH7HUz3cSDVEUWiHg76MZJ6tZooVNBmmxcgVK6VV8KAE688'
const recipientId = 'ak_2iBPH7HUz3cSDVEUWiHg76MZJ6tZooVNBmmxcgVK6VV8KAE688'
const name = 'test123test.test'
const nameHash = `nm_${encodeBase58Check(Buffer.from(name))}`
const nameId = 'nm_2sFnPHi5ziAqhdApSpRBsYdomCahtmk3YGNZKYUTtUNpVSMccC'
const pointers = [{ key: 'account_pubkey', id: senderId }]

let _salt;
let commitmentId;

describe('Native Transaction', function () {
configure(this)

let clientNative
let client

before(async () => {
client = await Ae({ url, internalUrl, nativeMode: false })
clientNative = await Ae({ url, internalUrl })

_salt = salt()
commitmentId = await client.commitmentHash(name, _salt)
})

it('native build of spend tx', async () => {
const txFromAPI = await client.spendTx({ senderId, recipientId, amount, fee, ttl, nonce, payload: 'test' })
const nativeTx = await clientNative.spendTx({ senderId, recipientId, amount, fee, ttl, nonce, payload: 'test' })
txFromAPI.should.be.equal(nativeTx)
})

it('native build of name pre-claim tx', async () => {
const txFromAPI = await client.namePreclaimTx({ accountId: senderId, nonce, fee, ttl, commitmentId })
const nativeTx = await clientNative.namePreclaimTx( {accountId: senderId, nonce, fee, ttl, commitmentId })
txFromAPI.should.be.equal(nativeTx)
})

it('native build of claim tx', async () => {
const txFromAPI = await client.nameClaimTx({
accountId: senderId,
nonce,
name: nameHash,
nameSalt: _salt,
fee,
ttl
})
const nativeTx = await clientNative.nameClaimTx({
accountId: senderId,
nonce,
name: nameHash,
nameSalt: _salt,
fee,
ttl
})
txFromAPI.should.be.equal(nativeTx)
})

it('native build of update tx', async () => {
const nativeTx = await clientNative.nameUpdateTx({ accountId: senderId, nonce, nameId, nameTtl, pointers, clientTtl, fee, ttl })
const txFromAPI = await client.nameUpdateTx({ accountId: senderId, nonce, nameId, nameTtl, pointers, clientTtl, fee, ttl })
txFromAPI.should.be.equal(nativeTx)
})

it('native build of revoke tx', async () => {
const txFromAPI = await client.nameRevokeTx({ accountId: senderId, nonce, nameId, fee, ttl })
const nativeTx = await clientNative.nameRevokeTx({ accountId: senderId, nonce, nameId, fee, ttl })
txFromAPI.should.be.equal(nativeTx)
})

it('native build of transfer tx', async () => {
const txFromAPI = await client.nameTransferTx({ accountId: senderId, nonce, nameId, recipientId, fee, ttl })
const nativeTx = await clientNative.nameTransferTx({ accountId: senderId, nonce, nameId, recipientId, fee, ttl })
txFromAPI.should.be.equal(nativeTx)
})
})
3 changes: 3 additions & 0 deletions webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function configure (filename, opts = {}) {
entry: './es/index.js',
mode: 'development', // automatically overriden by production flag
devtool: argv.mode === 'production' ? 'source-map' : 'eval-source-map',
node: {
fs: 'empty'
},
module: {
rules: [
{
Expand Down

0 comments on commit 9db55e4

Please sign in to comment.