From 105adb8304ba945090d6bd61d4876351c9d14146 Mon Sep 17 00:00:00 2001 From: abenso Date: Wed, 28 Aug 2024 18:04:09 -0300 Subject: [PATCH] wip - process chunk --- app/src/apdu_handler.c | 7 + app/src/apdu_handler_legacy.c | 160 +++++++++++++++++-- app/src/apdu_handler_legacy.h | 5 +- tests_zemu/package.json | 2 +- tests_zemu/snapshots/x-sign_hash_1/00000.png | Bin 0 -> 439 bytes tests_zemu/snapshots/x-sign_hash_1/00001.png | Bin 0 -> 406 bytes tests_zemu/snapshots/x-sign_hash_1/00002.png | Bin 0 -> 413 bytes tests_zemu/snapshots/x-sign_hash_1/00003.png | Bin 0 -> 442 bytes tests_zemu/snapshots/x-sign_hash_1/00004.png | Bin 0 -> 909 bytes tests_zemu/snapshots/x-sign_hash_1/00005.png | Bin 0 -> 459 bytes tests_zemu/snapshots/x-sign_hash_1/00006.png | Bin 0 -> 333 bytes tests_zemu/snapshots/x-sign_hash_1/00007.png | Bin 0 -> 808 bytes tests_zemu/snapshots/x-sign_hash_1/00008.png | Bin 0 -> 874 bytes tests_zemu/snapshots/x-sign_hash_1/00009.png | Bin 0 -> 884 bytes tests_zemu/snapshots/x-sign_hash_1/00010.png | Bin 0 -> 284 bytes tests_zemu/snapshots/x-sign_hash_1/00011.png | Bin 0 -> 550 bytes tests_zemu/snapshots/x-sign_hash_1/00012.png | Bin 0 -> 896 bytes tests_zemu/snapshots/x-sign_hash_1/00013.png | Bin 0 -> 1007 bytes tests_zemu/snapshots/x-sign_hash_1/00014.png | Bin 0 -> 587 bytes tests_zemu/snapshots/x-sign_hash_1/00015.png | Bin 0 -> 355 bytes tests_zemu/snapshots/x-sign_hash_1/00016.png | Bin 0 -> 467 bytes tests_zemu/tests/backward.test.ts | 50 +++++- tests_zemu/tests/common.ts | 3 - tests_zemu/tests/standard.test.ts | 65 ++++---- tests_zemu/tests/testscases/json.ts | 19 +++ 25 files changed, 263 insertions(+), 48 deletions(-) create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00000.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00001.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00002.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00003.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00004.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00005.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00006.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00007.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00008.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00009.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00010.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00011.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00012.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00013.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00014.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00015.png create mode 100644 tests_zemu/snapshots/x-sign_hash_1/00016.png create mode 100644 tests_zemu/tests/testscases/json.ts diff --git a/app/src/apdu_handler.c b/app/src/apdu_handler.c index ed8515f..e27bd7a 100644 --- a/app/src/apdu_handler.c +++ b/app/src/apdu_handler.c @@ -171,6 +171,7 @@ __Z_INLINE void handleSignTransaction(volatile uint32_t *flags, volatile uint32_ 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; @@ -263,6 +264,12 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) { break; } + case BCOMP_SIGN_JSON_TX: { + CHECK_PIN_VALIDATED() + legacy_handleSignTransaction(flags, tx, rx); + break; + } + #if defined(APP_TESTING) case INS_TEST: { handleTest(flags, tx, rx); diff --git a/app/src/apdu_handler_legacy.c b/app/src/apdu_handler_legacy.c index d8de98b..0b17db7 100644 --- a/app/src/apdu_handler_legacy.c +++ b/app/src/apdu_handler_legacy.c @@ -20,7 +20,12 @@ #include "addr.h" #include "view_internal.h" -__Z_INLINE void app_reply_address_legacy() { +#include "zxformat.h" + +static bool tx_initialized = false; +static uint32_t payload_length = 0; + +void legacy_app_reply_address() { // Add the pubkey length to the beginning of the buffer MEMMOVE(G_io_apdu_buffer + 1, G_io_apdu_buffer, action_addrResponseLen); G_io_apdu_buffer[0] = action_addrResponseLen; @@ -31,29 +36,39 @@ __Z_INLINE void app_reply_address_legacy() { } -void legacy_extractHDPath(uint32_t rx, uint32_t offset) { - if (rx < LEGACY_OFFSET_HDPATH_LEN) { +uint8_t legacy_extractHDPath(uint8_t *buffer, uint32_t rx, uint32_t offset) { + if (rx < offset) { THROW(APDU_CODE_WRONG_LENGTH); } - uint8_t hdPathQty = G_io_apdu_buffer[LEGACY_OFFSET_HDPATH_LEN]; + uint8_t hdPathQty = buffer[offset]; uint8_t hdPathLen = hdPathQty * sizeof(uint32_t); + uint32_t offset_hdpath_data = offset + 1; + + ZEMU_LOGF(50, "hdPathQty: %d\n", hdPathQty); + ZEMU_LOGF(50, "hdPathLen: %d\n", hdPathLen); + ZEMU_LOGF(50, "offset_hdpath_data: %d\n", offset_hdpath_data); - if ( rx - offset != hdPathLen) { + if ( rx - offset_hdpath_data != hdPathLen) { THROW(APDU_CODE_WRONG_LENGTH); } - memcpy(hdPath, G_io_apdu_buffer + offset, hdPathLen); + MEMCPY(hdPath, buffer + offset_hdpath_data, hdPathLen); const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT && hdPath[1] == HDPATH_1_DEFAULT; + ZEMU_LOGF(50, "mainnet: %d\n", mainnet); + if (!mainnet) { THROW(APDU_CODE_DATA_INVALID); } + + // return hdPathLen + 1 byte for the hdPathQty + return hdPathLen + 1; } void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx, const uint8_t requireConfirmation) { - legacy_extractHDPath(rx, LEGACY_OFFSET_HDPATH_DATA); + legacy_extractHDPath(G_io_apdu_buffer, rx, LEGACY_OFFSET_HDPATH_LEN); zxerr_t zxerr = app_fill_address(); if (zxerr != zxerr_ok) { @@ -61,7 +76,7 @@ void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint3 THROW(APDU_CODE_DATA_INVALID); } if (requireConfirmation) { - view_review_init(addr_getItem, addr_getNumItems, app_reply_address_legacy); + view_review_init(addr_getItem, addr_getNumItems, legacy_app_reply_address); view_review_show(REVIEW_ADDRESS); *flags |= IO_ASYNCH_REPLY; return; @@ -73,4 +88,131 @@ void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint3 *tx = action_addrResponseLen + 1; THROW(APDU_CODE_OK); -} \ No newline at end of file +} + +bool legacy_process_chunk(__Z_UNUSED volatile uint32_t *tx, uint32_t rx) { + + ZEMU_LOGF(50, "rx: %d\n", rx); + + uint8_t chunk_length = G_io_apdu_buffer[LEGACY_OFFSET_CHUNK_LENGTH]; + ZEMU_LOGF(50, "chunk_length: %d\n", chunk_length); + + // TODO: check case payload = 230 + uint32_t added = 0; + if (!tx_initialized) { + // read length of data + uint32_t data_length = 0; + data_length |= (uint32_t)G_io_apdu_buffer[5]; + data_length |= (uint32_t)G_io_apdu_buffer[6] << 8; + data_length |= (uint32_t)G_io_apdu_buffer[7] << 16; + data_length |= (uint32_t)G_io_apdu_buffer[8] << 24; + + payload_length = data_length; + + uint8_t bytes[4]; + for (int i = 0; i < 4; i++) { + bytes[i] = (data_length >> (i * 8)) & 0xFF; + } + for (int i = 0; i < 4; i++) { + ZEMU_LOGF(50, "payload_length %d: 0x%02X\n", i, bytes[i]) + } + + uint32_t offset = LEGACY_OFFSET_CHUNK_LENGTH + LEGACY_HEADER_LENGTH; + uint32_t payload_size = rx - offset; + tx_initialize(); + tx_reset(); + added = tx_append(&(G_io_apdu_buffer[offset]), payload_size); + if (added != payload_size) { + tx_initialized = false; + THROW(APDU_CODE_OUTPUT_BUFFER_TOO_SMALL); + } + + if (rx != LEGACY_CHUNCK_SIZE + LEGACY_HEADER_LENGTH) { + return true; + } + tx_initialized = true; + } else { + uint32_t offset = LEGACY_HEADER_LENGTH; + uint32_t payload_size = rx - offset; + + added = tx_append(&(G_io_apdu_buffer[offset]), payload_size); + if (added != payload_size) { + tx_initialized = false; + THROW(APDU_CODE_OUTPUT_BUFFER_TOO_SMALL); + } + + if (rx != LEGACY_CHUNCK_SIZE + LEGACY_HEADER_LENGTH) { + tx_initialized = false; + return true; + } + } + + return false; +} + +void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) { + zemu_log("handleSignTransactionLegacy\n"); + if (!legacy_process_chunk(tx, rx)) { + THROW(APDU_CODE_OK); + } + zemu_log("handleSignTransactionLegacy finish\n"); + // check buffer length + uint32_t tx_buffer_length = tx_get_buffer_length(); + uint8_t *tx_buffer = tx_get_buffer(); + int64_t hdpath_size = tx_buffer_length - payload_length; + + + uint8_t bytes[4]; + + for (int i = 0; i < 4; i++) { + bytes[i] = (payload_length >> (i * 8)) & 0xFF; + } + for (int i = 0; i < 4; i++) { + ZEMU_LOGF(50, "payload_length %d: 0x%02X\n", i, bytes[i]) + } + + for (int i = 0; i < 4; i++) { + bytes[i] = (tx_buffer_length >> (i * 8)) & 0xFF; + } + for (int i = 0; i < 4; i++) { + ZEMU_LOGF(50, "tx_buffer_length %d: 0x%02X\n", i, bytes[i]) + } + + for (int i = 0; i < 4; i++) { + bytes[i] = (hdpath_size >> (i * 8)) & 0xFF; + } + for (int i = 0; i < 4; i++) { + ZEMU_LOGF(50, "hdpath_size %d: 0x%02X\n", i, bytes[i]) + } + + if (hdpath_size < 0) { + THROW(APDU_CODE_DATA_INVALID); + } + + // get hdpath + uint8_t hdpath_length = legacy_extractHDPath(tx_buffer, tx_buffer_length, payload_length); + + // verify sizes + if (tx_buffer_length != hdpath_length + payload_length) { + THROW(APDU_CODE_DATA_INVALID); + } + + char print[200] = {0}; + ZEMU_LOGF(200, "tx_buffer: %s\n", tx_buffer); + + + THROW(APDU_CODE_OK); + +// const char *error_msg = tx_parse(tx_type_transaction); +// 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; +} diff --git a/app/src/apdu_handler_legacy.h b/app/src/apdu_handler_legacy.h index 491da89..d568a36 100644 --- a/app/src/apdu_handler_legacy.h +++ b/app/src/apdu_handler_legacy.h @@ -22,10 +22,13 @@ extern "C" { #include +#define LEGACY_CHUNCK_SIZE 230 +#define LEGACY_HEADER_LENGTH 5 +#define LEGACY_OFFSET_CHUNK_LENGTH 4 #define LEGACY_OFFSET_HDPATH_LEN 5 -#define LEGACY_OFFSET_HDPATH_DATA 6 void legacy_handleGetAddr(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx, const uint8_t requireConfirmation); +void legacy_handleSignTransaction(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx); #ifdef __cplusplus } diff --git a/tests_zemu/package.json b/tests_zemu/package.json index 69329a1..59ecad8 100644 --- a/tests_zemu/package.json +++ b/tests_zemu/package.json @@ -52,6 +52,6 @@ "ts-jest": "^29.2.3", "ts-node": "^10.9.2", "typescript": "^5.5.4", - "hw-app-kda": "git+https://github.com/obsidiansystems/hw-app-kda" + "hw-app-kda": "link:hw-app-kda" } } diff --git a/tests_zemu/snapshots/x-sign_hash_1/00000.png b/tests_zemu/snapshots/x-sign_hash_1/00000.png new file mode 100644 index 0000000000000000000000000000000000000000..33a7bd8942d58f005037cf3642c3e3aea8dac4bf GIT binary patch literal 439 zcmV;o0Z9IdP)#DbD*d zxf9Tz{k%EIodC(7gY*fIjLC&tQ0#YLZ&PV_N+{Jj13v>)MlRLa{zJOY*{yO0{ z;VZ@ZP$@+}YRP_((Not;M7L};x@b9Qa|pKr7J4}{)ycGUy?r*;i>^Z9R=_MYz1?(J zgt7;HHr9pN1HvWXV{yoHy9IrNmPP%W2ftnd)&KmCgF~uW{QjdJvW(LY`-jF)TcH?{Etqy5Yf=qf_TZoItIft!VIJ!SeJ1oUR%X|Ju1|Ns1_>$#<_G>rDf8SY h000000002D@CMJ4?X8rIYaRdq002ovPDHLkV1hI*$-4jm literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00001.png b/tests_zemu/snapshots/x-sign_hash_1/00001.png new file mode 100644 index 0000000000000000000000000000000000000000..ba22ea500c05e2486be36ec2643b41506505e233 GIT binary patch literal 406 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`!2~2@x4h6`U|@{(ba4!+nDh2#^rk}!JPnB@ z=lpK`pLC;}e@@~AZwGJBsQ>R*T6LIePG3+A)O4VX|E=^7aVL-K_kRDlRQ=yoeSRI6 zUdsga2h0Ct-gk)pr*nO0r?U$0ltrn#-kECbvU(qWrs7pyt)Co=X3$HewupBdZZ3G| zHL;WBiQ(0~lmGgt9PoXT@Q^7xYpvNXwku-CPJ6si7UtCWwxDSC2VuKpFZzM?FB ztu>;Kua>^~RfgdV#0 z`{9lL+^(08b{V&=WjUi=R1o`AWXbNp*_|u*RP6eG=SqF(w_9%vIo{jO(|o+kQSH}l y6DQv<=F04|X0)2{Hu>=RxmGX&0|*H;T;9XbuK%@ggKdZnNZ8ZW&t;ucLK6VmwX^O3 literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00002.png b/tests_zemu/snapshots/x-sign_hash_1/00002.png new file mode 100644 index 0000000000000000000000000000000000000000..cb84a2b3f9c0d78012344b12433a93b9cdf89732 GIT binary patch literal 413 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`!2~2@x4h6`U|>w~ba4!+nDh2#;G|{+o`%G- z^!!7A`;WwaJE%EXsAG%Jgm3@n>2Td(Sb0x9%~QHll_Lrrf11smzTGG&1bmw zc>MKipRZf6n94C*7iHbNeScM7u$SSnq-ERnF1P-SFbkT}{=9Qdf9n2Ebt(P#ybqSM z-dJ_;?MhL()vGd&L}V>jUbs5MyDi$1VR?_l*C%sco!YQ?&T;?yZIe?qUo4twe_~zU zUZy6llJq;>OH{byXFXWQ&&PhB@kG2rde3X)@a>Tz-?m?hHTmsTf?k4jglX9eEPx#huFsB6jcPf3r^}aHO}Y{$8I*-^D0Ax^ z1DReDofoS;GoV?QOT6m9y}3Yr%A&t#1}JmCaBkABdv%B8QIFjh6Cd-Kz1+E;LQa{$mhg33@W`H{ms=cgXJB1_0n;Oc7oH00000 k0000000000006+u4>KfDrxs3imjD0&07*qoM6N<$f=xro5dZ)H literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00004.png b/tests_zemu/snapshots/x-sign_hash_1/00004.png new file mode 100644 index 0000000000000000000000000000000000000000..82de24c2ca8e9df402f99d7a62dfbafa8f4229e6 GIT binary patch literal 909 zcmV;819JR{P)w!3FQy^KA%qY@2qDDdeC-Z!o@amh zdcEvVy|LLZV^n4;0UI>3*l@$;<^0*_`snI*adb88e@gw_X?<`y_;-5(Tvfu~eXaqv z8$dC4(p>hT0OJi9w~q9Py)z)9RTaztuvWA|+K*Pew%}N4s{1Cvdpdw;m@C&oRdcvA z22Z1M-m?TTv=zqMnPO{fUG7~zdIs2gW5n{XbuW4HX0W#dS_`&yW36t({)AkG@bYc# z?ke~2iu5f3{c-<>QFzO7`RFC+J|!bp!ymDGgbE?V4~9}hUCHHu&;3}%#1dmFD%jH~ z3Ylc&+!aUhC28%Zv0?!D36$wA;I^6iWP;*iQ`x2^$$TQ7mLjgv5w)ouIFZoi7Q?qtYot4x^^Wyue7!0Ez^g`}vtme^E=Od2Is?g^iS?bs<{R^4=s z_Is1%4BdQ~=dY^v2|K`@#rz{5aGS0wuEux%|0QUYq3aD~=;p&{nN^Y$H3NpIEv+bt zWleah7_($mWtav6Mhp_;>`Z5vts6g-tlQ-|QL?D6MIR{sFzfSW&QHq_&46giyrv7s^lnekY-wUQQs$K>hKmKp3pAUK?Hr!S4hBC6)tJX9^uw|Tj88_>}m1c2M jEN3W$5JCtc#6$c8mS0d%o5`Hw00000NkvXXu0mjfNeZ%` literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00005.png b/tests_zemu/snapshots/x-sign_hash_1/00005.png new file mode 100644 index 0000000000000000000000000000000000000000..d6099040ba72b169715f6e850b8a9427270b4af2 GIT binary patch literal 459 zcmV;+0W|)JP)XilpP2UG;p5kJys%41E-!tn*-uaQ%kJEznE{kW2pS9WqBh1yHj|tXF)R8O7<;<@%}lb-(Cgqw&9nCtqYLt?Ad+z z?({F-pO@RZ0NHV6^UdU$yZ|$=Q~s=&KHncc4B4|BfVW0^w_2NTRZTM4pQey%vOgI$ zSczR|rACsefh*Dr0000000000000000002E`2vZEUDRE^Hthfa002ovPDHLkV1kBm B)rpSZ(YJWSh(=WD;>#Cy>C00tbv|>CzH8A?xKG*I zTQjur#{Cao`>%gC_sd|?N;SInB4pw(Yu)wF=bUz##_L^u=&x17wWEdm+{!OZtkXTe zFuvdVblKU1&v;iiUvFD|jbYWsxpQs^F7$UWJz0GFqUcq*8qB$vs~k8YCfM8Dy@D5JCtcgpkMiGdcdgZ_)C4 zy(VRPD$w|@3v8Qt1ay`YX>nr$Is;WVu3fqdFCbSC1SRF-HPhg>8;=AV=x;4 zlVTRKe8lP&J?l>q1}#u%Y%Ku|1{;_2cavP92O6HqVqF z%t(*&^x|xJ0t)7WMF`SP*WF_#j@VG$y(INlHU^;3&ZHX&k#>T4?s(rEVOI)D6K8`b z{r-%u?N2~i)QOj&H-8~8=cXzoPIR$h`}Tfc0lqBuN%(f=1F*^xLI@$`D0)*7Y%{n6 zSxoV!s1#k!PUDkYzx^NvqK`A)0jCX&sBoA+FLjKX|G@CE#79uCQ9n20?{4Ek-mVt( zZ7=|bVE_&z1Mnu9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uUMOpbLtF?lhww*SA@&F7$JFS#jcW^^q6L9dpoP^#({xQcI+%bdF|v$RYoGKqIpN(@?hYp4 z!^Oz$AF$?l!xl7p=;_mlcyj7DP^RKI^$<3pib)6|gb+f0IR5}(MHZ?4bR9SV0000< KMNUMnLSTYg8L2z~ literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00010.png b/tests_zemu/snapshots/x-sign_hash_1/00010.png new file mode 100644 index 0000000000000000000000000000000000000000..131116ba86e6b3afbb9bef1c7be68952aff07f09 GIT binary patch literal 284 zcmeAS@N?(olHy`uVBq!ia0vp^4M6O`!2~2@x4h5*Qja}d978JRyuBXF*JQxMkYI8z z<-2{*`$xlqsjC{N0>8 zvUgs%n3f%goF(Bkwd_FQ;T0?Y?Ae>k7&XzX>DT4_k8|#b{LszXXUD)W=di2s0@?kEP)#O}ws` z9Eo)psmjjZ-3=%j?62|st>GmB>D8LwA2G&l+m146fmBO(=YdKG*YJ{nUFGCO4$`l* z3}r`wwA1%GTcX?Xl>m!zL!>QFp=tC}zbk`Pq^Di!qg+?79fd{t%hAUtxjLTu*F_>4S2C5t!iT+-qAJ<>Gvv_$1yw>WO@I+rR(FFiZ6F?-eO)Ec$g%J28ei{2=U^-aXYgk^u5F9xW^f;R0^kCdt%bgl7f^GYXDnibAhztGev zlKPcWJ*8^J5OnPh7-@VvzC(VsThCvSPDfz_}2jdo9Rl(JI||nOn0O#kBq| zzLp}?EthALcfjd2sQwh`%6$7{SzX{v%d{N1(D=q@sW=h!yjsH5+V?`%olvZw-rwEx ztfu!5Yr29Q+=)@>PA8pA4ov0U>v~7*^ao|vzfpE%W7#6&1>R@%61V oyLJKq0000000000003|P0rnTp&1qo)vH$=807*qoM6N<$f`fJofdBvi literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00012.png b/tests_zemu/snapshots/x-sign_hash_1/00012.png new file mode 100644 index 0000000000000000000000000000000000000000..5824ac080cc3599a01717ece33b87b13d7eb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5cc!1_ryAe9w9}zGZ%X&;TUp>FVdQ&MBb@086r+N&o-= literal 0 HcmV?d00001 diff --git a/tests_zemu/snapshots/x-sign_hash_1/00016.png b/tests_zemu/snapshots/x-sign_hash_1/00016.png new file mode 100644 index 0000000000000000000000000000000000000000..58ec4be3d27307c755798bbdf1be16718aad388d GIT binary patch literal 467 zcmV;^0WAKBP)|E<4q%6z^RDqJ zrGJX#MBlRmcsdF&83mZ^XLbMwmG!;s3d>r*cau;Mz5_yM-=Y^FdiE`P0gAZUw|f*I z#Pj}|*acYTe%u!BUr`JMsPCoOAu(*6(oqJ+D-vD&H>D0ZsPH=fbMQ`C;`% zKe!p$z30X1sI)r3{s!k0v_1C+caF~j{8Te1gbL6sj`d6*%-Y)}<-k49u?AebelKwYLJ@sl9zgpJE5B?E$IM^u)*wH7ORL zF`mBdk%H@w$rhk#0)N=K>#P*>$sR$Ab^vZgcKQPV0002+IKL!v4l@0>R(t>e002ov JPDHLkV1k$i;A;Q? literal 0 HcmV?d00001 diff --git a/tests_zemu/tests/backward.test.ts b/tests_zemu/tests/backward.test.ts index ae25c25..e0f4a92 100644 --- a/tests_zemu/tests/backward.test.ts +++ b/tests_zemu/tests/backward.test.ts @@ -16,16 +16,17 @@ import Zemu, { ButtonKind, zondaxMainmenuNavigation, isTouchDevice } from '@zondax/zemu' import Kda from "hw-app-kda"; -import { PATH, defaultOptions, models, simpleTxNormal } from './common' +import { PATH, defaultOptions, models } from './common' import { blake2bFinal, blake2bInit, blake2bUpdate } from 'blakejs' +import { JSON_TEST_CASES } from './testscases/json' 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) +jest.setTimeout(10000) const expected_pk = 'de12b5e16b93fe81ca4d70656bee4334f2e40f9f28b9796e792d28f2cead74ad' @@ -77,7 +78,7 @@ test.concurrent.each(models)('legacy show address', async function (m) { } }) -test.only.each(models)('show address - reject', async function (m) { +test.concurrent.each(models)('legacy show address - reject', async function (m) { const sim = new Zemu(m.path) try { await sim.start({ @@ -103,6 +104,39 @@ test.only.each(models)('show address - reject', async function (m) { } }) +describe.each(JSON_TEST_CASES)('Tx transactions', function (data) { + test.only.each(models)('sign json', async function (m) { + const sim = new Zemu(m.path) + try { + await sim.start({ ...defaultOptions, model: m.name }) + const app = new Kda(sim.getTransport()); + + const txBlob = Buffer.from(data.json, "utf-8") + const { pubKey } = await app.getPublicKey(data.path); + + // do not wait here.. we need to navigate + const signatureRequest = app.signTransaction(data.path, txBlob); + + // // 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 context = blake2bInit(32) + // blake2bUpdate(context, txBlob) + // const hash = Buffer.from(blake2bFinal(context)) + + // // Now verify the signature + // const valid = ed25519.verify(signatureResponse.signature, hash, pubKey) + // expect(valid).toEqual(true) + } finally { + await sim.close() + } + }) +}) + describe.each(HASH_TEST_CASES)('Hash transactions', function (data) { test.concurrent.each(models)('sign', async function (m) { const sim = new Zemu(m.path) @@ -173,3 +207,13 @@ describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) { } }) }) +// de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d + + +// c7b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d052c00008072020080000000800000000000000000 +// 00030000e6de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c227369676e657273223a5b7b227075624b6579223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c22636c697374223a5b7b2261726773223a5b5d2c226e616d65223a22636f696e2e474153227d2c7b2261726773223a5b2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930222c2239373930643131393538396132363131346531613432643932353938623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063343731373930227d2c226e6f6e6365223a225c22323032312d31302d31325430333a32373a35332e3730305a5c22227d052c00008072020080000000800000000000000000 + +// 052c00008072020080000000800000000000000000 + +// 00030000e6de0200007b226e6574776f726b4964223a226d61696e6e65743031222c227061796c6f6164223a7b2265786563223a7b2264617461223a7b7d2c22636f6465223a2228636f696e2e7472616e73666572205c22383339333463306639623030356633373862613335323066396465613935326662306139306535616133366631623566663833376439623330633437313739305c22205c22393739306431313935383961323631313465316134326439323539386233663633323535316335363638313965633438653065386335346461653665626234325c222031312e3029227d7d2c2273 +// 00030000e6623366363332353531633536363831396563343865306538633534646165366562623432222c31315d2c226e616d65223a22636f696e2e5452414e53464552227d5d7d5d2c226d657461223a7b226372656174696f6e54696d65223a313633343030393231342c2274746c223a32383830302c226761734c696d6974223a3630302c22636861696e4964223a2230222c226761735072696365223a312e30652d352c2273656e646572223a2238333933346330663962303035663337386261333532306639646561393532666230613930653561613336663162356666383337643962333063 \ No newline at end of file diff --git a/tests_zemu/tests/common.ts b/tests_zemu/tests/common.ts index d60a30d..23bce42 100644 --- a/tests_zemu/tests/common.ts +++ b/tests_zemu/tests/common.ts @@ -26,6 +26,3 @@ export const defaultOptions = { custom: `-s "${APP_SEED}"`, X11: false, } - -export const simpleTxNormal = - '{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)"}},"signers":[{"pubKey":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","clist":[{"args":[],"name":"coin.GAS"},{"args":["83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42",11],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1634009214,"ttl":28800,"gasLimit":600,"chainId":"0","gasPrice":1.0e-5,"sender":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790"},"nonce":"\\"2021-10-12T03:27:53.700Z\\""}' diff --git a/tests_zemu/tests/standard.test.ts b/tests_zemu/tests/standard.test.ts index 2f3a117..05ebf3a 100644 --- a/tests_zemu/tests/standard.test.ts +++ b/tests_zemu/tests/standard.test.ts @@ -16,11 +16,12 @@ import Zemu, { ButtonKind, zondaxMainmenuNavigation, isTouchDevice } from '@zondax/zemu' import { KadenaApp } from '@zondax/ledger-kadena' -import { PATH, defaultOptions, models, simpleTxNormal } from './common' +import { PATH, defaultOptions, models } from './common' import { blake2bFinal, blake2bInit, blake2bUpdate } from 'blakejs' // @ts-expect-error import ed25519 from 'ed25519-supercop' +import { JSON_TEST_CASES } from './testscases/json' jest.setTimeout(60000) @@ -147,35 +148,37 @@ describe('Standard', function () { } }) - test.concurrent.each(models)('sign tx normal', async function (m) { - const sim = new Zemu(m.path) - try { - await sim.start({ ...defaultOptions, model: m.name }) - const app = new KadenaApp(sim.getTransport()) - - const txBlob = Buffer.from(simpleTxNormal) - const responseAddr = await app.getAddressAndPubKey(PATH, false) - const pubKey = responseAddr.pubkey - - // do not wait here.. we need to navigate - const signatureRequest = app.sign(PATH, txBlob) - - // Wait until we are not in the main menu - await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot()) - await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_tx_normal`) - - const signatureResponse = await signatureRequest - console.log(signatureResponse) - - const context = blake2bInit(32) - blake2bUpdate(context, txBlob) - const hash = Buffer.from(blake2bFinal(context)) - - // Now verify the signature - const valid = ed25519.verify(signatureResponse.signature, hash, pubKey) - expect(valid).toEqual(true) - } finally { - await sim.close() - } + describe.each(JSON_TEST_CASES)('Tx transactions', function (data) { + test.only.each(models)('sign json', async function (m) { + const sim = new Zemu(m.path) + try { + await sim.start({ ...defaultOptions, model: m.name }) + const app = new KadenaApp(sim.getTransport()) + + const txBlob = Buffer.from(data.json, "utf-8") + const responseAddr = await app.getAddressAndPubKey(data.path, false) + const pubKey = responseAddr.pubkey + + // do not wait here.. we need to navigate + const signatureRequest = app.sign(data.path, txBlob) + + // 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 context = blake2bInit(32) + blake2bUpdate(context, txBlob) + const hash = Buffer.from(blake2bFinal(context)) + + // Now verify the signature + const valid = ed25519.verify(signatureResponse.signature, hash, pubKey) + expect(valid).toEqual(true) + } finally { + await sim.close() + } + }) }) }) diff --git a/tests_zemu/tests/testscases/json.ts b/tests_zemu/tests/testscases/json.ts new file mode 100644 index 0000000..4924b21 --- /dev/null +++ b/tests_zemu/tests/testscases/json.ts @@ -0,0 +1,19 @@ +import { PATH } from '../common' + +export const JSON_TEST_CASES = [ + // { + // name: 'test_apdu_legacy_blob_100', + // json:'{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba35', + // path: PATH, + // }, + // { + // name: 'test_apdu_legacy_blob_220', + // json:'{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)', + // path: PATH, + // }, + { + name: 'hash_1', + json:'{"networkId":"mainnet01","payload":{"exec":{"data":{},"code":"(coin.transfer \\"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790\\" \\"9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42\\" 11.0)"}},"signers":[{"pubKey":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","clist":[{"args":[],"name":"coin.GAS"},{"args":["83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790","9790d119589a26114e1a42d92598b3f632551c566819ec48e0e8c54dae6ebb42",11],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1634009214,"ttl":28800,"gasLimit":600,"chainId":"0","gasPrice":1.0e-5,"sender":"83934c0f9b005f378ba3520f9dea952fb0a90e5aa36f1b5ff837d9b30c471790"},"nonce":"\\"2021-10-12T03:27:53.700Z\\""}', + path: PATH, + }, +]