Skip to content

Commit

Permalink
updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chcmedeiros committed Jun 19, 2024
1 parent 17d607c commit 4865720
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 206 deletions.
182 changes: 70 additions & 112 deletions tests_zemu/tests/amino.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import Zemu, { ClickNavigation, TouchNavigation } from '@zondax/zemu'
// @ts-ignore
import { CosmosApp } from '@zondax/ledger-cosmos-js'
import CosmosApp from '@zondax/ledger-cosmos-js'
import {
defaultOptions,
DEVICE_MODELS,
Expand All @@ -38,7 +38,7 @@ import { ButtonKind, IButton } from '@zondax/zemu/dist/types'
jest.setTimeout(120000)

describe('Amino', function () {
// eslint-disable-next-line jest/expect-expect
//eslint-disable-next-line jest/expect-expect
test.concurrent.each(DEVICE_MODELS)('can start and stop container', async function (m) {
const sim = new Zemu(m.path)
try {
Expand All @@ -54,14 +54,13 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), "utf-8")
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')

console.log(respPk)

// do not wait here..
Expand All @@ -74,8 +73,6 @@ describe('Amino', function () {
const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -100,14 +97,12 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_basic2))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -120,9 +115,6 @@ describe('Amino', function () {
const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
Expand All @@ -145,14 +137,12 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_basic))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -164,9 +154,6 @@ describe('Amino', function () {

const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -191,14 +178,12 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(ibc_denoms))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -210,9 +195,6 @@ describe('Amino', function () {

const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -237,14 +219,12 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(setWithdrawAddress))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -256,9 +236,6 @@ describe('Amino', function () {

const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -283,14 +260,12 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(cliGovDeposit))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -303,8 +278,6 @@ describe('Amino', function () {
const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -330,16 +303,14 @@ describe('Amino', function () {
const app = new CosmosApp(sim.getTransport())

// Activate expert mode
await sim.toggleExpertMode();
await sim.toggleExpertMode()

const path = [44, 118, 0, 0, 0]
const path = "m/44'/118'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_msgMultiSend))
const hrp = 'cosmos'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -352,8 +323,6 @@ describe('Amino', function () {
const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -379,16 +348,14 @@ describe('Amino', function () {
const app = new CosmosApp(sim.getTransport())

// Change to expert mode so we can skip fields
await sim.toggleExpertMode();
await sim.toggleExpertMode()

const path = [44, 60, 0, 0, 0]
const path = "m/44'/60'/0'/0/0"
const tx = Buffer.from(JSON.stringify(setWithdrawAddress))
const hrp = 'inj'

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
Expand All @@ -401,8 +368,6 @@ describe('Amino', function () {
const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')

// Now verify the signature
Expand All @@ -428,48 +393,44 @@ describe('Amino', function () {
const app = new CosmosApp(sim.getTransport())

// Enable expert to allow sign with eth path
await sim.toggleExpertMode();
await sim.toggleExpertMode()

const path = [44, 60, 0, 0, 0]
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), "utf-8")
const path = "m/44'/60'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')
const hrp = 'inj'

// check with invalid HRP
const errorRespPk = await app.getAddressAndPubKey(path, 'forbiddenHRP')
expect(errorRespPk.return_code).toEqual(0x6986)
expect(errorRespPk.error_message).toEqual('Transaction rejected')
try {
const errorRespPk = await app.getAddressAndPubKey(path, 'forbiddenHRP')

// do not wait here..
const signatureRequest = app.sign(path, tx, hrp)

// Wait until we are not in the main menu
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_basic_eth`)
// do not wait here..
const signatureRequest = app.sign(path, tx, hrp)

const resp = await signatureRequest
console.log(resp)
// Wait until we are not in the main menu
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
await sim.compareSnapshotsAndApprove('.', `${m.prefix.toLowerCase()}-sign_basic_eth`)

expect(resp.return_code).toEqual(0x9000)
expect(resp.error_message).toEqual('No errors')
expect(resp).toHaveProperty('signature')
const resp = await signatureRequest
console.log(resp)

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)
// get address / publickey
const respPk = await app.getAddressAndPubKey(path, hrp)
console.log(respPk)

// Now verify the signature
const sha3 = require('js-sha3')
const msgHash = Buffer.from(sha3.keccak256(tx), 'hex')
// Now verify the signature
const sha3 = require('js-sha3')
const msgHash = Buffer.from(sha3.keccak256(tx), 'hex')

const signatureDER = resp.signature
const signature = secp256k1.signatureImport(Uint8Array.from(signatureDER))
const signatureDER = resp.signature
const signature = secp256k1.signatureImport(Uint8Array.from(signatureDER))

const pk = Uint8Array.from(respPk.compressed_pk)
const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} catch (e: any) {
expect(e.message).toEqual('Transaction rejected')
}
} finally {
await sim.close()
}
Expand All @@ -481,40 +442,37 @@ describe('Amino', function () {
await sim.start({ ...defaultOptions, model: m.name })
const app = new CosmosApp(sim.getTransport())

const path = [44, 60, 0, 0, 0]
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), "utf-8")

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, 'inj')
expect(respPk.return_code).toEqual(0x9000)
expect(respPk.error_message).toEqual('No errors')
console.log(respPk)

// do not wait here..
const signatureRequest = app.sign(path, tx)

// Wait until we are not in the main menu
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
let nav = undefined;
if (m.name === 'stax') {
const okButton: IButton = {
x: 200,
y: 540,
delay: 0.25,
};
nav = new TouchNavigation([
ButtonKind.ConfirmYesButton,
]);
nav.schedule[0].button = okButton;
} else {
nav = new ClickNavigation([1, 0]);
const path = "m/44'/60'/0'/0/0"
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')

try {
// get address / publickey
const respPk = await app.getAddressAndPubKey(path, 'inj')

// do not wait here..
const signatureRequest = app.sign(path, tx, 'inj')

// Wait until we are not in the main menu
await sim.waitUntilScreenIsNot(sim.getMainMenuSnapshot())
let nav = undefined
if (m.name === 'stax') {
const okButton: IButton = {
x: 200,
y: 540,
delay: 0.25,
}
nav = new TouchNavigation([ButtonKind.ConfirmYesButton])
nav.schedule[0].button = okButton
} else {
nav = new ClickNavigation([1, 0])
}
await sim.navigate('.', `${m.prefix.toLowerCase()}-sign_basic_eth_warning`, nav.schedule)

const resp = await signatureRequest
console.log(resp)
} catch (e: any) {
expect(e.message).toEqual('Data is invalid')
}
await sim.navigate('.', `${m.prefix.toLowerCase()}-sign_basic_eth_warning`, nav.schedule);

const resp = await signatureRequest
console.log(resp)

expect(resp.return_code).toEqual(0x6984)
} finally {
await sim.close()
}
Expand Down
4 changes: 2 additions & 2 deletions tests_zemu/tests/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ export const defaultOptions = {
export const DEVICE_MODELS: IDeviceModel[] = [
{ name: 'nanos', prefix: 'S', path: APP_PATH_S },
{ name: 'nanox', prefix: 'X', path: APP_PATH_X },
{ name: 'nanosp', prefix: 'SP', path: APP_PATH_SP },
{ name: 'stax', prefix: 'ST', path: APP_PATH_ST },
//{ name: 'nanosp', prefix: 'SP', path: APP_PATH_SP },
//{ name: 'stax', prefix: 'ST', path: APP_PATH_ST },
]

export const tx_sign_textual = 'a10192a20168436861696e20696402686d792d636861696ea2016e4163636f756e74206e756d626572026131a2016853657175656e6365026132a301674164647265737302782d636f736d6f7331756c6176336873656e7570737771666b77327933737570356b677471776e767161386579687304f5a3016a5075626c6963206b657902781f2f636f736d6f732e63727970746f2e736563703235366b312e5075624b657904f5a3026d5075624b6579206f626a656374030104f5a401634b657902785230324542204444374620453446442045423736204443384120323035452046363544203739304320443330452038413337203541354320323532382045423341203932334120463146422034443739203444030204f5a102781e54686973207472616e73616374696f6e206861732031204d657373616765a3016d4d6573736167652028312f312902781c2f636f736d6f732e62616e6b2e763162657461312e4d736753656e640301a2026e4d736753656e64206f626a6563740302a3016c46726f6d206164647265737302782d636f736d6f7331756c6176336873656e7570737771666b77327933737570356b677471776e76716138657968730303a3016a546f206164647265737302782d636f736d6f7331656a726634637572327779366b667572673966326a707070326833616665356836706b6835740303a30166416d6f756e74026731302041544f4d0303a1026e456e64206f66204d657373616765a201644d656d6f0278193e20e29a9befb88f5c7532363942e29a9befb88f2020202020a2016446656573026a302e3030322041544f4da30169476173206c696d697402673130302730303004f5a3017148617368206f66207261772062797465730278403963303433323930313039633237306232666661396633633066613535613039306330313235656265663838316637646135333937386462663933663733383504f5'
Expand Down
Loading

0 comments on commit 4865720

Please sign in to comment.