Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upgrade testing crypto libs #124

Merged
merged 1 commit into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,5 @@ tests_tools/target
fuzz-*.log

/scan-build

tests_zemu/bun.lockb
50 changes: 28 additions & 22 deletions tests_zemu/package.json
Original file line number Diff line number Diff line change
@@ -1,47 +1,53 @@
{
"name": "integration-tests",
"author": "Zondax AG",
"license": "Apache-2.0",
"version": "1.0.0",
"description": "",
"types": "./dist/index.d.ts",
"repository": {
"type": "git",
"url": "git+https://github.com/Zondax/ledger-cosmos"
},
"keywords": [
"Zondax",
"Ledger"
],
"repository": {
"type": "git",
"url": "git+https://github.com/Zondax/ledger-cosmos"
},
"license": "Apache-2.0",
"author": "Zondax AG",
"types": "./dist/index.d.ts",
"scripts": {
"clean": "ts-node tests/pullImageKillOld.ts",
"test": "yarn clean && jest --maxConcurrency 2"
"format": "FORCE_COLOR=1 prettier --write . && sort-package-json",
"format:check": "FORCE_COLOR=1 prettier --check .",
"lint": "eslint .",
"lint:fix": "eslint --fix .",
"test": "yarn clean && jest --maxConcurrency 3",
"try": "node try.mjs",
"upgrade": "bunx npm-check-updates -i"
},
"dependencies": {
"@zondax/ledger-cosmos-js": "^3.0.3",
"@zondax/ledger-cosmos-js": "^3",
"@zondax/zemu": "^0.50.2"
},
"devDependencies": {
"@ledgerhq/hw-transport-node-hid": "^6.29.3",
"@ledgerhq/logs": "^6.12.0",
"@noble/curves": "^1.5.0",
"@scure/base": "^1.1.7",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/jest": "^29.5.12",
"@types/ledgerhq__hw-transport": "^4.21.8",
"@typescript-eslint/eslint-plugin": "^7.17.0",
"@typescript-eslint/parser": "^7.17.0",
"bech32": "^2.0.0",
"blakejs": "^1.1.1",
"crypto-js": "4.2.0",
"eslint": "^9.7.0",
"@typescript-eslint/eslint-plugin": "^8.4.0",
"@typescript-eslint/parser": "^8.4.0",
"eslint": "^9.9.1",
"eslint-config-prettier": "^9.1.0",
"eslint-plugin-import": "^2.29.1",
"eslint-plugin-jest": "^28.6.0",
"eslint-plugin-jest": "^28.8.2",
"eslint-plugin-prettier": "^5.2.1",
"eslint-plugin-promise": "^7.1.0",
"eslint-plugin-tsdoc": "^0.3.0",
"eslint-plugin-unused-imports": "^4.1.3",
"jest": "29.7.0",
"jest-serial-runner": "^1.1.0",
"js-sha3": "0.9.3",
"jssha": "^3.2.0",
"keccak256": "^1.0.6",
"prettier": "^3.3.3",
"secp256k1": "^5.0.0",
"ts-jest": "^29.2.3",
"ts-jest": "^29.2.5",
"ts-node": "^10.9.2",
"typescript": "^5.5.4"
}
Expand Down
106 changes: 42 additions & 64 deletions tests_zemu/tests/amino.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
******************************************************************************* */

import Zemu, { ClickNavigation, TouchNavigation, isTouchDevice } from '@zondax/zemu'
// @ts-ignore
import { CosmosApp } from '@zondax/ledger-cosmos-js'
import {
defaultOptions,
Expand All @@ -29,11 +28,10 @@ import {
example_tx_str_msgMultiSend,
} from './common'

// @ts-ignore
import secp256k1 from 'secp256k1/elliptic'
// @ts-ignore
import crypto from 'crypto'
import { ButtonKind, IButton, SwipeDirection } from '@zondax/zemu/dist/types'
import { secp256k1 } from '@noble/curves/secp256k1'
import { sha256 } from '@noble/hashes/sha256'
import { keccak_256 } from '@noble/hashes/sha3'

jest.setTimeout(120000)

Expand All @@ -55,7 +53,7 @@ describe('Amino', function () {
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 tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')
const hrp = 'cosmos'

// get address / publickey
Expand All @@ -79,15 +77,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand Down Expand Up @@ -124,15 +120,13 @@ describe('Amino', function () {
expect(resp.error_message).toEqual('No errors')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand Down Expand Up @@ -170,15 +164,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand Down Expand Up @@ -216,15 +208,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand Down Expand Up @@ -262,15 +252,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand Down Expand Up @@ -308,15 +296,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand All @@ -330,7 +316,7 @@ 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 tx = Buffer.from(JSON.stringify(example_tx_str_msgMultiSend))
Expand All @@ -357,15 +343,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

// Now verify the signature
const hash = crypto.createHash('sha256')
const msgHash = Uint8Array.from(hash.update(tx).digest())
const msgHash = sha256(tx)

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand All @@ -379,7 +363,7 @@ 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 tx = Buffer.from(JSON.stringify(setWithdrawAddress))
Expand All @@ -406,15 +390,13 @@ describe('Amino', function () {
expect(resp).toHaveProperty('signature')

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

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand All @@ -428,10 +410,10 @@ 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 tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')
const hrp = 'inj'

// check with invalid HRP
Expand Down Expand Up @@ -460,15 +442,13 @@ describe('Amino', function () {
console.log(respPk)

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

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

const pk = Uint8Array.from(respPk.compressed_pk)

const signatureOk = secp256k1.ecdsaVerify(signature, msgHash, pk)
const signatureOk = secp256k1.verify(signature, msgHash, pk)
expect(signatureOk).toEqual(true)
} finally {
await sim.close()
Expand All @@ -482,7 +462,7 @@ describe('Amino', function () {
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")
const tx = Buffer.from(JSON.stringify(example_tx_str_basic), 'utf-8')

// get address / publickey
const respPk = await app.getAddressAndPubKey(path, 'inj')
Expand All @@ -495,22 +475,20 @@ describe('Amino', function () {

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

const resp = await signatureRequest
console.log(resp)
Expand Down
Loading
Loading