Skip to content

Commit

Permalink
add zemu test for transaction sign
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Aug 27, 2024
1 parent 2e81b33 commit 2da665e
Show file tree
Hide file tree
Showing 4 changed files with 154 additions and 20 deletions.
33 changes: 17 additions & 16 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,22 +152,23 @@ __Z_INLINE void handleSignHash(volatile uint32_t *flags, volatile uint32_t *tx,

__Z_INLINE void handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
zemu_log("handleSignTransaction\n");
// if (!process_chunk(tx, rx)) {
// THROW(APDU_CODE_OK);
// }

// const char *error_msg = tx_parse();
// CHECK_APP_CANARY()
// if (error_msg != NULL) {
// const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
// memcpy(G_io_apdu_buffer, error_msg, error_msg_length);
// *tx += (error_msg_length);
// THROW(APDU_CODE_DATA_INVALID);
// }

// view_review_init(tx_getItem, tx_getNumItems, app_sign);
// view_review_show(REVIEW_TXN);
// *flags |= IO_ASYNCH_REPLY;
if (!process_chunk(tx, rx)) {
THROW(APDU_CODE_OK);
}


const char *error_msg = tx_parse();
CHECK_APP_CANARY()
if (error_msg != NULL) {
const int error_msg_length = strnlen(error_msg, sizeof(G_io_apdu_buffer));
memcpy(G_io_apdu_buffer, error_msg, error_msg_length);
*tx += (error_msg_length);
THROW(APDU_CODE_DATA_INVALID);
}

view_review_init(tx_getItem, tx_getNumItems, app_sign);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
}
__Z_INLINE void handle_getversion(__Z_UNUSED volatile uint32_t *flags, volatile uint32_t *tx) {
G_io_apdu_buffer[0] = 0;
Expand Down
3 changes: 1 addition & 2 deletions tests_zemu/tests/testscases/hash.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@

import { PATH } from '../common'

export const HASH_TESTCASES = [
export const HASH_TEST_CASES = [
{
name: 'hash_1',
hash: 'ffd8cd79deb956fa3c7d9be0f836f20ac84b140168a087a842be4760e40e2b1c',
Expand Down
98 changes: 98 additions & 0 deletions tests_zemu/tests/testscases/transactions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { PATH } from '../common'
import { TransferTxType } from '@zondax/ledger-kadena'

export const TRANSACTIONS_TEST_CASES = [
{
name: 'transfer_1',
type: TransferTxType.TRANSFER,
path: PATH,
recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
amount: "1.23",
network: "testnet04",
chainId: 0,
gasPrice: "1.0e-6",
gasLimit: "2300",
creationTime: 1665647810,
ttl: "600",
nonce: "2022-10-13 07:56:50.893257 UTC",
blob: '{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)"}},"signers":[{"pubKey":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","clist":[{"args":[],"name":"coin.GAS"},{"args":["83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","adfas",4,5,6,7,8],"name":"mycoin.MY_TRANSFER"}]}],"meta":{"creationTime":1634009214,"ttl":28800,"gasLimit":600,"chainId":"0","gasPrice":1.0e-5,"sender":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790"},"nonce":"\\"2021-10-12T03:27:53.700Z\\""}',
},
// {
// name: 'transfer_create_1',
// type: TransferTxType.TRANSFER_CREATE,
// path: PATH,
// recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
// amount: "23.67",
// network: "testnet04",
// chainId: 1,
// gasPrice: "1.0e-6",
// gasLimit: "2300",
// creationTime: 1665722463,
// ttl: "600",
// nonce: "2022-10-14 04:41:03.193557 UTC"
// },
// {
// name: 'transfer_cross_chain_1',
// type: TransferTxType.TRANSFER_CROSS_CHAIN,
// path: PATH,
// recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
// recipient_chainId: 2,
// amount: "23.67",
// network: "testnet04",
// chainId: 1,
// gasPrice: "1.0e-6",
// gasLimit: "2300",
// creationTime: 1665722463,
// ttl: "600",
// nonce: "2022-10-14 04:41:03.193557 UTC"
// },
// {
// name: 'transfer_2',
// type: TransferTxType.TRANSFER,
// path: PATH,
// recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
// amount: "1.23",
// namespace: "free",
// module: "mytoken-123",
// network: "testnet04",
// chainId: 0,
// gasPrice: "1.0e-6",
// gasLimit: "2300",
// creationTime: 1665647810,
// ttl: "600",
// nonce: "2022-10-13 07:56:50.893257 UTC"
// },
// {
// name: 'transfer_create_2',
// type: TransferTxType.TRANSFER_CREATE,
// path: PATH,
// recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
// amount: "23.67",
// namespace: "free",
// module: "mytoken-123",
// network: "testnet04",
// chainId: 1,
// gasPrice: "1.0e-6",
// gasLimit: "2300",
// creationTime: 1665722463,
// ttl: "600",
// nonce: "2022-10-14 04:41:03.193557 UTC"
// },
// {
// name: 'transfer_cross_chain_2',
// type: TransferTxType.TRANSFER_CROSS_CHAIN,
// path: PATH,
// recipient: '83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790',
// recipient_chainId: 2,
// amount: "23.67",
// namespace: "free",
// module: "mytoken-123",
// network: "testnet04",
// chainId: 1,
// gasPrice: "1.0e-6",
// gasLimit: "2300",
// creationTime: 1665722463,
// ttl: "600",
// nonce: "2022-10-14 04:41:03.193557 UTC"
// }
]
40 changes: 38 additions & 2 deletions tests_zemu/tests/transactions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@ import { KadenaApp } from '@zondax/ledger-kadena'
import { PATH, defaultOptions, models, simpleTxNormal } from './common'
import { blake2bFinal, blake2bInit, blake2bUpdate } from 'blakejs'

import { HASH_TESTCASES } from './testscases/hash'
import { HASH_TEST_CASES } from './testscases/hash'
import { TRANSACTIONS_TEST_CASES } from './testscases/transactions'

// @ts-expect-error
import ed25519 from 'ed25519-supercop'

jest.setTimeout(60000)

describe.each(HASH_TESTCASES)('Hash transactions', function (data) {
describe.each(HASH_TEST_CASES)('Hash transactions', function (data) {
test.concurrent.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
try {
Expand Down Expand Up @@ -60,3 +61,38 @@ describe.each(HASH_TESTCASES)('Hash transactions', function (data) {
}
})
})

describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
test.only.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new KadenaApp(sim.getTransport())

const responseAddr = await app.getAddressAndPubKey(data.path)
const pubKey = responseAddr.pubkey

// do not wait here... we need to navigate
const signatureRequest = app.signTransferTx(data.path, data)

// // Wait until we are not in the main menu
// await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
// await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_${data.name}`)

// const signatureResponse = await signatureRequest
// console.log(signatureResponse)

// const rawHash =
// typeof data.hash == 'string'
// ? data.hash.length == 64
// ? Buffer.from(data.hash, 'hex')
// : Buffer.from(data.hash, 'base64')
// : Buffer.from(data.hash)
// // Now verify the signature
// const valid = ed25519.verify(signatureResponse.signature, rawHash, pubKey)
// expect(valid).toEqual(true)
} finally {
await sim.close()
}
})
})

0 comments on commit 2da665e

Please sign in to comment.