Skip to content

Commit

Permalink
add try tests
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Sep 4, 2024
1 parent 1c9114f commit eb2dc64
Show file tree
Hide file tree
Showing 82 changed files with 138 additions and 48 deletions.
9 changes: 3 additions & 6 deletions app/src/apdu_handler_legacy.c
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,11 @@ void legacy_handleSignTransferTx(volatile uint32_t *flags, volatile uint32_t *tx
tx_type = 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);
tx_reset();
*tx = 0;
THROW(APDU_CODE_LEGACY_PARSER_ERROR);
}

THROW(APDU_CODE_OK);

view_review_init(tx_getItem, tx_getNumItems, legacy_app_sign);
view_review_show(REVIEW_TXN);
*flags |= IO_ASYNCH_REPLY;
Expand Down
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_1/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00000.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00001.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00002.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00003.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00004.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00005.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00006.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00007.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_hash_2/00008.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00000.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00001.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00002.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00003.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00004.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00005.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00006.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00007.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00008.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00009.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00010.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00011.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00012.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00013.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00014.png
Binary file added tests_zemu/snapshots/sp-sign_transfer_1/00015.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00000.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00001.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00002.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00003.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00004.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00005.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00006.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00007.png
Binary file added tests_zemu/snapshots/x-sign_hash_1/00008.png
38 changes: 15 additions & 23 deletions tests_zemu/tests/legacy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ 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'
import { TRANSACTIONS_LEGACY_TEST_CASES } from './testscases/transactions'
import { APDU_TEST_CASES } from './testscases/legacy_apdu'
// @ts-expect-error
import ed25519 from 'ed25519-supercop'
Expand Down Expand Up @@ -181,37 +181,29 @@ describe.each(HASH_TEST_CASES)('Hash transactions', function (data) {
})
})

describe.each(TRANSACTIONS_TEST_CASES)('Tx transactions', function (data) {
describe.each(TRANSACTIONS_LEGACY_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 Kda(sim.getTransport());

const responseAddr = await app.getPublicKey(data.txParams.path)
const pubKey = responseAddr.publicKey
console.log(pubKey)
const { publicKey } = await app.getPublicKey(data.txParams.path);


// do not wait here... we need to navigate
let signatureRequest = await app["signTransferTx"](data.txParams);
let signatureRequest = app["signTransferTx"](data.txParams);

// // 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)
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_${data.name}_legacy`)

// Wait until we are not in the main menu
const signatureResponse = await signatureRequest
console.log("Pubkey: ", publicKey.toString('hex'))
console.log("Signature: ", signatureResponse.signature)

// Now verify the signature
const valid = ed25519.verify(signatureResponse.signature, signatureResponse.hash, publicKey)
expect(valid).toEqual(true)
} finally {
await sim.close()
}
Expand Down
1 change: 1 addition & 0 deletions tests_zemu/tests/testscases/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const TRANSACTIONS_TEST_CASES = [
ttl: '600',
nonce: '2022-10-13 07:56:50.893257 UTC',
},
recipient_chainId: 0,
},
{
name: 'transfer_create_1',
Expand Down
83 changes: 70 additions & 13 deletions tests_zemu/try.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,30 @@ import { ed25519 } from '@noble/curves/ed25519'
import pkg from 'blakejs'
const { blake2bFinal, blake2bInit, blake2bUpdate } = pkg

async function verifySignature(signature, message, publicKey, computeBlake2b = true) {
let hash = message
if (computeBlake2b) {
const context = blake2bInit(32)
blake2bUpdate(context, message)
hash = Buffer.from(blake2bFinal(context))
}

const valid = ed25519.verify(signature, hash, publicKey)
if (valid) {
console.log('Valid signature')
} else {
console.log('Invalid signature')
}
}

function decodeHash(encodedHash) {
let base64Hash = encodedHash.replace(/-/g, '+').replace(/_/g, '/');
while (base64Hash.length % 4) {
base64Hash += '=';
}
return Buffer.from(base64Hash, 'base64');
}

async function main() {
const transport = await TransportNodeHid.default.open()

Expand All @@ -21,26 +45,59 @@ async function main() {
resp = await app.getVersion()
console.log('Version', resp)

// Sign a transaction
console.log("Signing a transaction")
const blob =
'{"networkId":"mainnet01","payload":{"exec":{"data":{"ks":{"pred":"keys-all","keys":["368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca"]}},"code":"(coin.transfer-create \\"alice\\" \\"bob\\" (read-keyset \\"ks\\") 100.1)\\n(coin.transfer \\"bob\\" \\"alice\\" 0.1)"}},"signers":[{"pubKey":"6be2f485a7af75fedb4b7f153a903f7e6000ca4aa501179c91a2450b777bd2a7","clist":[{"args":["alice","bob",100.1],"name":"coin.TRANSFER"},{"args":[],"name":"coin.GAS"}]},{"pubKey":"368820f80c324bbc7c2b0610688a7da43e39f91d118732671cd9c7500ff43cca","clist":[{"args":["bob","alice",0.1],"name":"coin.TRANSFER"}]}],"meta":{"creationTime":1580316382,"ttl":7200,"gasLimit":1200,"chainId":"0","gasPrice":1.0e-5,"sender":"alice"},"nonce":"2020-01-29 16:46:22.916695 UTC"}'
const messageToSign = Buffer.from(blob)
let messageToSign = Buffer.from(blob)
console.log(messageToSign.toString())
const signatureRequest = app.sign(PATH, messageToSign)
let signatureRequest = app.sign(PATH, messageToSign)

const signatureResponse = await signatureRequest
let signatureResponse = await signatureRequest
console.log(signatureResponse.signature.toString('hex'))

const context = blake2bInit(32)
blake2bUpdate(context, messageToSign)
const hash = Buffer.from(blake2bFinal(context))
await verifySignature(signatureResponse.signature, messageToSign, pubKey)

// Now verify the signature
const valid = ed25519.verify(signatureResponse.signature, hash, pubKey)
if (valid) {
console.log('Valid signature')
} else {
console.log('Invalid signature')
}
// Sign a hash
console.log("Signing a hash")
messageToSign = 'ffd8cd79deb956fa3c7d9be0f836f20ac84b140168a087a842be4760e40e2b1c'
console.log(messageToSign.toString())
signatureRequest = app.signHash(PATH, messageToSign);

signatureResponse = await signatureRequest
console.log(signatureResponse.signature.toString('hex'))

const rawHash =
typeof messageToSign== 'string'
? messageToSign.length == 64
? Buffer.from(messageToSign, 'hex')
: Buffer.from(messageToSign, 'base64')
: Buffer.from(messageToSign)

await verifySignature(signatureResponse.signature, rawHash, pubKey, false)

console.log("Signing a transfer")
const txParams = {
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',
};

signatureRequest = app.signTransferTx(txParams.path, txParams)
signatureResponse = await signatureRequest
const decodedHash = decodeHash(signatureResponse.hash)

console.log(signatureResponse.signature.toString('hex'))
console.log(decodedHash.toString('hex'))

await verifySignature(signatureResponse.signature, decodedHash, pubKey, false)
}

;(async () => {
Expand Down
55 changes: 49 additions & 6 deletions tests_zemu/try_legacy.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import { ed25519 } from '@noble/curves/ed25519'
import pkg from 'blakejs'
const { blake2bFinal, blake2bInit, blake2bUpdate } = pkg

async function verifySignature(signature, message, publicKey) {
const context = blake2bInit(32)
blake2bUpdate(context, message)
const hash = Buffer.from(blake2bFinal(context))
async function verifySignature(signature, message, publicKey, computeBlake2b = true) {
let hash = message
if (computeBlake2b) {
const context = blake2bInit(32)
blake2bUpdate(context, message)
hash = Buffer.from(blake2bFinal(context))
}

const valid = ed25519.verify(signature, hash, publicKey)
if (valid) {
Expand All @@ -17,6 +20,16 @@ async function verifySignature(signature, message, publicKey) {
}
}

function decodeHash(encodedHash) {
let base64Hash = encodedHash.replace(/-/g, '+').replace(/_/g, '/');

while (base64Hash.length % 4) {
base64Hash += '=';
}

return Buffer.from(base64Hash, 'base64');
}

async function main() {
const transport = await TransportNodeHid.default.open()
const app = new Kda.default(transport);;
Expand Down Expand Up @@ -46,12 +59,42 @@ async function main() {
signatureRequest = app.signHash(PATH, messageToSign);

signatureResponse = await signatureRequest
console.log(messageToSign.toString())
console.log(signatureResponse.signature.toString('hex'))

await verifySignature(signatureResponse.signature, messageToSign, publicKey)

await verifySignature(signatureResponse.signature, messageToSign, publicKey, false)

// Sign a hash
console.log("Signing a transfer")
const txParams = {
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',
};

signatureRequest = app["signTransferTx"](txParams);
signatureResponse = await signatureRequest

// Convert hex string signature to Uint8Array
const signatureHex = signatureResponse.pact_command.sigs[0].sig;
const signatureBytes = new Uint8Array(Buffer.from(signatureHex, 'hex'));

const hashBytes = decodeHash(signatureResponse.pact_command.hash)

console.log(signatureResponse.pact_command.sigs[0].sig.toString('hex'))
console.log(hashBytes.toString('hex'))

await verifySignature(signatureBytes, hashBytes, publicKey, false)
}

;(async () => {
await main()
})()

0 comments on commit eb2dc64

Please sign in to comment.