Skip to content

Commit

Permalink
get pubkey for legacy
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Aug 27, 2024
1 parent 99195bd commit 3d92edf
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 3 deletions.
18 changes: 18 additions & 0 deletions app/src/apdu_handler.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

#include "actions.h"
#include "addr.h"
#include "apdu_handler_legacy.h"
#include "app_main.h"
#include "coin.h"
#include "crypto.h"
Expand Down Expand Up @@ -244,6 +245,23 @@ void handleApdu(volatile uint32_t *flags, volatile uint32_t *tx, uint32_t rx) {
break;
}

case BCOMP_GET_VERSION: {
CHECK_PIN_VALIDATED()
//handleGetPublicKey(flags, tx, rx);
break;
}

case BCOMP_VERIFY_ADDRESS: {
CHECK_PIN_VALIDATED()
break;
}

case BCOMP_GET_PUBKEY: {
CHECK_PIN_VALIDATED()
legacy_handleGetAddr(flags, tx, rx, 0);
break;
}

#if defined(APP_TESTING)
case INS_TEST: {
handleTest(flags, tx, rx);
Expand Down
65 changes: 65 additions & 0 deletions app/src/apdu_handler_legacy.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*******************************************************************************

Check notice on line 1 in app/src/apdu_handler_legacy.c

View workflow job for this annotation

GitHub Actions / lint

Run clang-format on app/src/apdu_handler_legacy.c

File app/src/apdu_handler_legacy.c does not conform to Custom style guidelines. (lines 24, 31)
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#include "apdu_handler_legacy.h"

#include "actions.h"
#include "addr.h"
#include "view_internal.h"

void legacy_extractHDPath(uint32_t rx, uint32_t offset) {
if (rx < LEGACY_OFFSET_HDPATH_LEN) {
THROW(APDU_CODE_WRONG_LENGTH);
}

uint8_t hdPathQty = G_io_apdu_buffer[LEGACY_OFFSET_HDPATH_LEN];
uint8_t hdPathLen = hdPathQty * sizeof(uint32_t);

if ( rx - offset != hdPathLen) {
THROW(APDU_CODE_WRONG_LENGTH);
}

memcpy(hdPath, G_io_apdu_buffer + offset, hdPathLen);

const bool mainnet = hdPath[0] == HDPATH_0_DEFAULT && hdPath[1] == HDPATH_1_DEFAULT;

if (!mainnet) {
THROW(APDU_CODE_DATA_INVALID);
}
}

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);

zxerr_t zxerr = app_fill_address();
if (zxerr != zxerr_ok) {
*tx = 0;
THROW(APDU_CODE_DATA_INVALID);
}
if (requireConfirmation) {
view_review_init(addr_getItem, addr_getNumItems, app_reply_address);
view_review_show(REVIEW_ADDRESS);
*flags |= IO_ASYNCH_REPLY;
return;
}

// 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;

*tx = action_addrResponseLen + 1;
THROW(APDU_CODE_OK);
}
32 changes: 32 additions & 0 deletions app/src/apdu_handler_legacy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#pragma once

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>

#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);

#ifdef __cplusplus
}
#endif
10 changes: 10 additions & 0 deletions app/src/coin.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,16 @@ extern "C" {
#define MAX_SIGN_SIZE 256u
#define BLAKE2B_DIGEST_SIZE 32u

// Legacy commands
#define BCOMP_GET_VERSION 0x00
#define BCOMP_VERIFY_ADDRESS 0x01
#define BCOMP_GET_PUBKEY 0x02
#define BCOMP_SIGN_JSON_TX 0x03
#define BCOMP_SIGN_TX_HASH 0x04
#define BCOMP_MAKE_TRANSFER_TX 0x10
#define BCOMP_GET_VERSION_STR 0xFF

// New commands
#define INS_GET_VERSION_KDA 0x20
#define INS_GET_ADDR_KDA 0x21
#define INS_SIGN_KDA 0x22
Expand Down
5 changes: 3 additions & 2 deletions tests_zemu/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"format:check": "FORCE_COLOR=1 prettier --check .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "jest tests/transactions.test.ts",
"test": "jest tests/backward.test.ts",
"try": "node try.mjs",
"upgrade": "bunx npm-check-updates -i"
},
Expand Down Expand Up @@ -51,6 +51,7 @@
"sort-package-json": "^1.52.0",
"ts-jest": "^29.2.3",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
"typescript": "^5.5.4",
"hw-app-kda": "git+https://github.com/obsidiansystems/hw-app-kda"
}
}
100 changes: 100 additions & 0 deletions tests_zemu/tests/backward.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/** ******************************************************************************
* (c) 2018 - 2024 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************* */

import Zemu, { ButtonKind, zondaxMainmenuNavigation, isTouchDevice } from '@zondax/zemu'
import Kda from "hw-app-kda";
import { PATH, defaultOptions, models, simpleTxNormal } from './common'
import { blake2bFinal, blake2bInit, blake2bUpdate } from 'blakejs'

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_TEST_CASES)('Hash 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 Kda(sim.getTransport());

const responseAddr = await app.getPublicKey(data.path)
const pubKey = responseAddr.publicKey
console.log(pubKey)

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

// // 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()
}
})
})

describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
test.concurrent.each(models)('sign', async function (m) {
const sim = new Zemu(m.path)
try {
await sim.start({ ...defaultOptions, model: m.name })
const app = new Kda(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()
}
})
})
6 changes: 5 additions & 1 deletion tests_zemu/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,9 @@
"skipLibCheck": true,
"outDir": "./dist"
},
"exclude": ["node_modules", "./dist/**"]
"exclude": ["node_modules", "./dist/**"],
"include": ["src", "tests", "types"],
"ts-node": {
"files": true
}
}
1 change: 1 addition & 0 deletions tests_zemu/types/hw-app-kda.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module 'hw-app-kda';

1 comment on commit 3d92edf

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format reports: 6 file(s) not formatted
  • app/src/items.h
  • app/src/parser_txdef.h
  • app/src/common/tx.h
  • app/src/parser_impl.c
  • app/src/apdu_handler.c
  • app/src/apdu_handler_legacy.c

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.