UNPKG

6.15 kBJavaScriptView Raw
1import { before, describe, it } from 'mocha'
2import { getSdk } from '.'
3import { generateKeyPair } from '../../es/utils/crypto'
4import { BASE_VERIFICATION_SCHEMA, SIGNATURE_VERIFICATION_SCHEMA } from '../../es/tx/builder/schema'
5import MemoryAccount from '../../es/account/memory'
6
7const WARNINGS = [...SIGNATURE_VERIFICATION_SCHEMA, ...BASE_VERIFICATION_SCHEMA].reduce((acc, [msg, v, error]) => error.type === 'warning' ? [...acc, error.txKey] : acc, [])
8const ERRORS = [...BASE_VERIFICATION_SCHEMA, ...SIGNATURE_VERIFICATION_SCHEMA].reduce((acc, [msg, v, error]) => error.type === 'error' ? [...acc, error.txKey] : acc, [])
9const channelCreate = 'tx_+NkLAfhCuECIIeWttRUiZ32uriBdmM1t+dCg90KuG2ABxOiuXqzpAul6uTWvsyfx3EFJDah6trudrityh+6XSX3mkPEimhgGuJH4jzIBoQELtO15J/l7UeG8teE0DRIzWyorEsi8UiHWPEvLOdQeYYgbwW1nTsgAAKEB6bv2BOYRtUYKOzmZ6Xcbb2BBfXPOfFUZ4S9+EnoSJcqIG8FtZ07IAACIAWNFeF2KAAAKAIYSMJzlQADAoDBrIcoop8JfZ4HOD9p3nDTiNthj7jjl+ArdHwEMUrvQgitwOr/v3Q=='
10
11describe('Verify Transaction', function () {
12 let client
13
14 before(async () => {
15 client = await getSdk()
16 await client.spend(1234, 'ak_LAqgfAAjAbpt4hhyrAfHyVg9xfVQWsk1kaHaii6fYXt6AJAGe')
17 })
18 it('validate params', async () => {
19 return client.spendTx({}).should.be.rejectedWith({
20 code: 'TX_BUILD_VALIDATION_ERROR',
21 msg: 'Validation error'
22 })
23 })
24 it('check warnings', async () => {
25 const spendTx = await client.spendTx({
26 senderId: await client.address(),
27 recipientId: await client.address(),
28 amount: '1242894753985394725983479583427598237459328752353245345',
29 nonce: '100',
30 ttl: 2,
31 absoluteTtl: true
32 })
33
34 const { validation } = await client.unpackAndVerify(spendTx)
35 const warning = validation
36 .filter(({ type }) => type === 'warning')
37 .map(({ txKey }) => txKey)
38
39 JSON.stringify(WARNINGS).should.be.equals(JSON.stringify(warning))
40 })
41 it('check errors', async () => {
42 const spendTx = await client.spendTx({
43 senderId: await client.address(),
44 recipientId: await client.address(),
45 amount: 1,
46 fee: '1000',
47 nonce: '1',
48 ttl: 2,
49 absoluteTtl: true
50 })
51
52 await client.addAccount(MemoryAccount({ keypair: generateKeyPair() }), { select: true })
53 // Sign using another account
54 const signedTxHash = await client.signTransaction(spendTx)
55 const signedTxFull = await client.signTransaction(spendTx, { signHash: false })
56
57 const checkErrors = async (signedTx) => {
58 const { validation } = await client.unpackAndVerify(signedTx)
59 const error = validation
60 .filter(({ type }) => type === 'error') // exclude contract vm/abi, has separated test for it
61 .map(({ txKey }) => txKey)
62
63 JSON.stringify(ERRORS.filter(e => e !== 'gasPrice' && e !== 'ctVersion')).should.be.equals(JSON.stringify(error))
64 }
65 await checkErrors(signedTxHash)
66 await checkErrors(signedTxFull)
67 })
68 it('verify transaction before broadcast', async () => {
69 client = await getSdk()
70 const spendTx = await client.spendTx({
71 senderId: await client.address(),
72 recipientId: await client.address(),
73 amount: 1,
74 ttl: 2,
75 absoluteTtl: true
76 })
77
78 try {
79 await client.send(spendTx, { verify: true })
80 } catch ({ errorData }) {
81 const atLeastOneError = !!errorData.validation.length
82 atLeastOneError.should.be.equal(true)
83 }
84 })
85 it('Verify vmVersion/abiVersion for contract transactions', async () => {
86 // Contract create transaction with wrong abi/vm version (vm: 3, abi: 0)
87 const contractCreateTx = 'tx_+QSaKgGhASLDuRmSBJZv91HE219uqXb2L0adh+bilzBWUi93m5blArkD+PkD9UYCoI2tdssfNdXZOclcaOwkTNB2S/SXIVsLDi7KUoxJ3Jki+QL7+QEqoGjyZ2M4/1CIOaukd0nv+ovofvKE8gf7PZmYcBzVOIfFhG1haW64wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAYAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACg//////////////////////////////////////////8AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAALhAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAPkBy6C5yVbyizFJqfWYeqUF89obIgnMVzkjQAYrtsG9n5+Z6oRpbml0uGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD//////////////////////////////////////////+5AUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAoAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABAP//////////////////////////////////////////AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP//////////////////////////////////////////7jMYgAAZGIAAISRgICAUX+5yVbyizFJqfWYeqUF89obIgnMVzkjQAYrtsG9n5+Z6hRiAADAV1CAUX9o8mdjOP9QiDmrpHdJ7/qL6H7yhPIH+z2ZmHAc1TiHxRRiAACvV1BgARlRAFtgABlZYCABkIFSYCCQA2ADgVKQWWAAUVlSYABSYADzW2AAgFJgAPNbWVlgIAGQgVJgIJADYAAZWWAgAZCBUmAgkANgA4FSgVKQVltgIAFRUVlQgJFQUICQUJBWW1BQgpFQUGIAAIxWhTIuMS4wgwMAAIcF9clYKwgAAAAAgxgX+IQ7msoAuGAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAILnJVvKLMUmp9Zh6pQXz2hsiCcxXOSNABiu2wb2fn5nqAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABkansY'
88 const { validation } = await client.unpackAndVerify(contractCreateTx)
89 const vmAbiError = validation.find(el => el.txKey === 'ctVersion')
90 vmAbiError.msg.split(',')[0].should.be.equal('Wrong abi/vm version')
91 })
92 it('Verify channel create tx', async () => {
93 const res = await client.unpackAndVerify(channelCreate)
94 Array.isArray(res.validation).should.be.equal(true)
95 })
96 it('Verify nameFee for nameClaim transaction', async () => {
97 const tx = 'tx_+KILAfhCuEAtbc38n/FH8jZHO0DkEkiLZZm8ypEzZEhbjyHtaoEYkENOE9tD+Xp6smFMou9X521oI4gkFBQGwSQaQk6Z7XMNuFr4WCACoQHkWpoidhJW2EZEega88I1P9Ktw1DFBUWwrzkr5jC5zUAORc29tZUF1Y3Rpb24uY2hhaW6HDwTrMteR15AJQ0VVyE5TcqKSstgfbGV6hg9HjghAAAAGpIPS'
98 const res = await client.unpackAndVerify(tx)
99 const nameFeeError = res.validation.find(err => err.txKey === 'nameFee')
100 nameFeeError.should.be.an('object')
101 nameFeeError.type.should.be.equal('error')
102 nameFeeError.msg.indexOf('The account balance').should.not.be.equal(-1)
103 })
104})